aboutsummaryrefslogtreecommitdiff
path: root/misc/cgo/test/callback.go
blob: c642d5f0fb7914481b257b1d3ab6312103e5c89b (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2011 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.

package cgotest

/*
void callback(void *f);
void callGoFoo(void);
*/
import "C"

import (
	"./backdoor"
	"path"
	"runtime"
	"strings"
	"testing"
	"unsafe"
)

// nestedCall calls into C, back into Go, and finally to f.
func nestedCall(f func()) {
	// NOTE: Depends on representation of f.
	// callback(x) calls goCallback(x)
	C.callback(*(*unsafe.Pointer)(unsafe.Pointer(&f)))
}

//export goCallback
func goCallback(p unsafe.Pointer) {
	(*(*func())(unsafe.Pointer(&p)))()
}

func testCallback(t *testing.T) {
	var x = false
	nestedCall(func() { x = true })
	if !x {
		t.Fatal("nestedCall did not call func")
	}
}

func testCallbackGC(t *testing.T) {
	nestedCall(runtime.GC)
}

var lockedOSThread = backdoor.LockedOSThread

func testCallbackPanic(t *testing.T) {
	// Make sure panic during callback unwinds properly.
	if lockedOSThread() {
		t.Fatal("locked OS thread on entry to TestCallbackPanic")
	}
	defer func() {
		s := recover()
		if s == nil {
			t.Fatal("did not panic")
		}
		if s.(string) != "callback panic" {
			t.Fatal("wrong panic:", s)
		}
		if lockedOSThread() {
			t.Fatal("locked OS thread on exit from TestCallbackPanic")
		}
	}()
	nestedCall(func() { panic("callback panic") })
	panic("nestedCall returned")
}

func testCallbackPanicLoop(t *testing.T) {
	// Make sure we don't blow out m->g0 stack.
	for i := 0; i < 100000; i++ {
		testCallbackPanic(t)
	}
}

func testCallbackPanicLocked(t *testing.T) {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	if !lockedOSThread() {
		t.Fatal("runtime.LockOSThread didn't")
	}
	defer func() {
		s := recover()
		if s == nil {
			t.Fatal("did not panic")
		}
		if s.(string) != "callback panic" {
			t.Fatal("wrong panic:", s)
		}
		if !lockedOSThread() {
			t.Fatal("lost lock on OS thread after panic")
		}
	}()
	nestedCall(func() { panic("callback panic") })
	panic("nestedCall returned")
}

// Callback with zero arguments used to make the stack misaligned,
// which broke the garbage collector and other things.
func testZeroArgCallback(t *testing.T) {
	defer func() {
		s := recover()
		if s != nil {
			t.Fatal("panic during callback:", s)
		}
	}()
	C.callGoFoo()
}

//export goFoo
func goFoo() {
	x := 1
	for i := 0; i < 10000; i++ {
		// variadic call mallocs + writes to
		variadic(x, x, x)
		if x != 1 {
			panic("bad x")
		}
	}
}

func variadic(x ...interface{}) {}

func testBlocking(t *testing.T) {
	c := make(chan int)
	go func() {
		for i := 0; i < 10; i++ {
			c <- <-c
		}
	}()
	nestedCall(func() {
		for i := 0; i < 10; i++ {
			c <- i
			if j := <-c; j != i {
				t.Errorf("out of sync %d != %d", j, i)
			}
		}
	})
}

// Test that the stack can be unwound through a call out and call back
// into Go.
func testCallbackCallers(t *testing.T) {
	if runtime.Compiler != "gc" {
		// The exact function names are not going to be the same.
		t.Skip("skipping for non-gc toolchain")
	}
	pc := make([]uintptr, 100)
	n := 0
	name := []string{
		"test.goCallback",
		"runtime.cgocallbackg",
		"runtime.cgocallback_gofunc",
		"return",
		"runtime.cgocall",
		"test._Cfunc_callback",
		"test.nestedCall",
		"test.testCallbackCallers",
		"test.TestCallbackCallers",
		"testing.tRunner",
		"runtime.goexit",
	}
	nestedCall(func() {
		n = runtime.Callers(2, pc)
	})
	if n != len(name) {
		t.Errorf("expected %d frames, got %d", len(name), n)
	}
	for i := 0; i < n; i++ {
		f := runtime.FuncForPC(pc[i])
		if f == nil {
			t.Fatalf("expected non-nil Func for pc %p", pc[i])
		}
		fname := f.Name()
		// Remove the prepended pathname from automatically
		// generated cgo function names.
		if strings.HasPrefix(fname, "_") {
			fname = path.Base(f.Name()[1:])
		}
		if fname != name[i] {
			t.Errorf("expected function name %s, got %s", name[i], fname)
		}
	}
}