callback.go 2.71 KB
Newer Older
1 2 3 4
// 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.

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
package cgotest

/*
void callback(void *f);
void callGoFoo(void) {
	extern void goFoo(void);
	goFoo();
}
*/
import "C"

import (
	"runtime"
	"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)))()
}

Russ Cox's avatar
Russ Cox committed
34
func testCallback(t *testing.T) {
35
	var x = false
36
	nestedCall(func() { x = true })
37 38 39 40 41
	if !x {
		t.Fatal("nestedCall did not call func")
	}
}

Russ Cox's avatar
Russ Cox committed
42
func testCallbackGC(t *testing.T) {
43 44 45
	nestedCall(runtime.GC)
}

46
func lockedOSThread() bool // in runtime.c
47

Russ Cox's avatar
Russ Cox committed
48
func testCallbackPanic(t *testing.T) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	// 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")
		}
	}()
65
	nestedCall(func() { panic("callback panic") })
66 67 68
	panic("nestedCall returned")
}

Russ Cox's avatar
Russ Cox committed
69
func testCallbackPanicLoop(t *testing.T) {
70 71
	// Make sure we don't blow out m->g0 stack.
	for i := 0; i < 100000; i++ {
Gustavo Niemeyer's avatar
Gustavo Niemeyer committed
72
		testCallbackPanic(t)
73 74 75
	}
}

Russ Cox's avatar
Russ Cox committed
76
func testCallbackPanicLocked(t *testing.T) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	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")
		}
	}()
95
	nestedCall(func() { panic("callback panic") })
96 97 98 99 100
	panic("nestedCall returned")
}

// Callback with zero arguments used to make the stack misaligned,
// which broke the garbage collector and other things.
Russ Cox's avatar
Russ Cox committed
101
func testZeroArgCallback(t *testing.T) {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	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{}) {}

Russ Cox's avatar
Russ Cox committed
125
func testBlocking(t *testing.T) {
126 127 128 129 130 131
	c := make(chan int)
	go func() {
		for i := 0; i < 10; i++ {
			c <- <-c
		}
	}()
132
	nestedCall(func() {
133 134 135 136 137 138 139 140
		for i := 0; i < 10; i++ {
			c <- i
			if j := <-c; j != i {
				t.Errorf("out of sync %d != %d", j, i)
			}
		}
	})
}