aboutsummaryrefslogtreecommitdiff
path: root/test/fixedbugs/issue11656.go
blob: 62b36cf790fff1374f238ebd429d30f89826ae18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// run

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// windows doesn't work, because Windows exception handling
// delivers signals based on the current PC, and that current PC
// doesn't go into the Go runtime.
// +build !windows

// wasm does not work, because the linear memory is not executable.
// +build !wasm

// This test doesn't work on gccgo/GoLLVM, because they will not find
// any unwind information for the artificial function, and will not be
// able to unwind past that point.
// +build !gccgo

package main

import (
	"encoding/binary"
	"runtime"
	"runtime/debug"
	"unsafe"
)

func main() {
	debug.SetPanicOnFault(true)
	defer func() {
		if err := recover(); err == nil {
			panic("not panicking")
		}
		pc, _, _, _ := runtime.Caller(10)
		f := runtime.FuncForPC(pc)
		if f == nil || f.Name() != "main.f" {
			if f == nil {
				println("no func for ", unsafe.Pointer(pc))
			} else {
				println("found func:", f.Name())
			}
			panic("cannot find main.f on stack")
		}
	}()
	f(20)
}

func f(n int) {
	if n > 0 {
		f(n - 1)
	}
	var f struct {
		x uintptr
	}

	// We want to force an illegal instruction, to get a crash
	// at a PC value != 0.
	// Not all systems make the data section non-executable.
	ill := make([]byte, 64)
	switch runtime.GOARCH {
	case "386", "amd64":
		binary.LittleEndian.PutUint16(ill, 0x0b0f) // ud2
	case "arm":
		binary.LittleEndian.PutUint32(ill, 0xe7f000f0) // no name, but permanently undefined
	case "arm64":
		binary.LittleEndian.PutUint32(ill, 0xd4207d00) // brk #1000
	case "ppc64":
		binary.BigEndian.PutUint32(ill, 0x7fe00008) // trap
	case "ppc64le":
		binary.LittleEndian.PutUint32(ill, 0x7fe00008) // trap
	case "mips", "mips64":
		binary.BigEndian.PutUint32(ill, 0x00000034) // trap
	case "mipsle", "mips64le":
		binary.LittleEndian.PutUint32(ill, 0x00000034) // trap
	case "s390x":
		binary.BigEndian.PutUint32(ill, 0) // undefined instruction
	default:
		// Just leave it as 0 and hope for the best.
	}

	f.x = uintptr(unsafe.Pointer(&ill[0]))
	p := &f
	fn := *(*func())(unsafe.Pointer(&p))
	fn()
}