modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue11656.go (about)

     1  // run
     2  
     3  // Copyright 2015 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // windows doesn't work, because Windows exception handling
     8  // delivers signals based on the current PC, and that current PC
     9  // doesn't go into the Go runtime.
    10  // +build !windows
    11  
    12  package main
    13  
    14  import (
    15  	"encoding/binary"
    16  	"runtime"
    17  	"runtime/debug"
    18  	"unsafe"
    19  )
    20  
    21  func main() {
    22  	debug.SetPanicOnFault(true)
    23  	defer func() {
    24  		if err := recover(); err == nil {
    25  			panic("not panicking")
    26  		}
    27  		pc, _, _, _ := runtime.Caller(10)
    28  		f := runtime.FuncForPC(pc)
    29  		if f == nil || f.Name() != "main.f" {
    30  			if f == nil {
    31  				println("no func for ", unsafe.Pointer(pc))
    32  			} else {
    33  				println("found func:", f.Name())
    34  			}
    35  			panic("cannot find main.f on stack")
    36  		}
    37  	}()
    38  	f(20)
    39  }
    40  
    41  func f(n int) {
    42  	if n > 0 {
    43  		f(n - 1)
    44  	}
    45  	var f struct {
    46  		x uintptr
    47  	}
    48  
    49  	// We want to force an illegal instruction, to get a crash
    50  	// at a PC value != 0.
    51  	// Not all systems make the data section non-executable.
    52  	ill := make([]byte, 64)
    53  	switch runtime.GOARCH {
    54  	case "386", "amd64":
    55  		binary.LittleEndian.PutUint16(ill, 0x0b0f) // ud2
    56  	case "arm":
    57  		binary.LittleEndian.PutUint32(ill, 0xe7f000f0) // no name, but permanently undefined
    58  	case "arm64":
    59  		binary.LittleEndian.PutUint32(ill, 0xd4207d00) // brk #1000
    60  	case "ppc64":
    61  		binary.BigEndian.PutUint32(ill, 0x7fe00008) // trap
    62  	case "ppc64le":
    63  		binary.LittleEndian.PutUint32(ill, 0x7fe00008) // trap
    64  	case "mips", "mips64":
    65  		binary.BigEndian.PutUint32(ill, 0x00000034) // trap
    66  	case "mipsle", "mips64le":
    67  		binary.LittleEndian.PutUint32(ill, 0x00000034) // trap
    68  	case "s390x":
    69  		binary.BigEndian.PutUint32(ill, 0) // undefined instruction
    70  	default:
    71  		// Just leave it as 0 and hope for the best.
    72  	}
    73  
    74  	f.x = uintptr(unsafe.Pointer(&ill[0]))
    75  	fn := *(*func())(unsafe.Pointer(&f))
    76  	fn()
    77  }