1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 // Both the import "C" prologue and the main file are sorted by issue number.
7 // This file contains //export directives on Go functions
8 // and so it must NOT contain C definitions (only declarations).
9 // See test.go for C definitions.
26 extern void doAdd(int, int);
32 extern void Issue1560InC(void);
34 // twoSleep returns the absolute start time of the first sleep
36 long long twoSleep(int);
39 void lockOSThreadC(void);
40 int usleep(unsigned usec);
42 // issue 4054 part 2 - part 1 in test.go
58 extern int issue5548_in_c(void);
62 extern unsigned long long issue6833Func(unsigned int, unsigned long long);
66 extern int CheckIssue6907C(_GoString_);
70 extern void f7665(void);
73 // Stack tracing didn't work during cgo code after calling a Go
74 // callback. Make sure GC works and the stack trace is correct.
78 // use ugly atomic variable sync since that doesn't require calling back into
79 // Go code or OS dependencies
80 void issue7978c(uint32_t *sync);
82 // issue 8331 part 2 - part 1 in test.go
83 // A typedef of an unnamed struct is the same struct when
84 // #include'd twice. No runtime test; just make sure it compiles.
85 #include "issue8331.h"
89 typedef void (*PFunc8945)();
90 extern PFunc8945 func8945; // definition is in test.go
95 // issue 28772 part 2 - part 1 in issuex.go
96 #define issue28772Constant2 2
108 void callIssue31891(void);
112 } Issue38408, *PIssue38408;
114 extern void cfunc49633(void*); // definition is in test.go
120 //export ReturnIntLong
121 func ReturnIntLong() (int, C.long) {
149 func testCthread(t *testing.T) {
150 if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && runtime.GOARCH == "arm64" {
151 t.Skip("the iOS exec wrapper is unable to properly handle the panic from Add")
156 want := 10 * (10 - 1) / 2 * 6
158 t.Fatalf("sum=%d, want %d", sum.i, want)
168 for i := 0; i < 10000; i++ {
176 func xvariadic(x ...interface{}) {
179 func test1328(t *testing.T) {
184 // Test that C functions and Go functions run in parallel.
189 issue1560Ch = make(chan bool, 2)
192 //export Issue1560FromC
193 func Issue1560FromC() {
194 for atomic.LoadInt32(&issue1560) != 1 {
197 atomic.AddInt32(&issue1560, 1)
198 for atomic.LoadInt32(&issue1560) != 3 {
204 func Issue1560FromGo() {
205 atomic.AddInt32(&issue1560, 1)
206 for atomic.LoadInt32(&issue1560) != 2 {
209 atomic.AddInt32(&issue1560, 1)
213 func test1560(t *testing.T) {
223 func exportbyte() byte {
228 func exportbool() bool {
233 func exportrune() rune {
238 func exporterror() error {
243 func exportint() int {
248 func exportuint() uint {
252 //export exportuintptr
253 func exportuintptr() uintptr {
258 func exportint8() int8 {
263 func exportuint8() uint8 {
268 func exportint16() int16 {
272 //export exportuint16
273 func exportuint16() uint16 {
278 func exportint32() int32 {
282 //export exportuint32
283 func exportuint32() uint32 {
288 func exportint64() int64 {
292 //export exportuint64
293 func exportuint64() uint64 {
297 //export exportfloat32
298 func exportfloat32() float32 {
302 //export exportfloat64
303 func exportfloat64() float64 {
307 //export exportcomplex64
308 func exportcomplex64() complex64 {
312 //export exportcomplex128
313 func exportcomplex128() complex128 {
319 //export exportSliceIn
320 func exportSliceIn(s []byte) bool {
321 return len(s) == cap(s)
324 //export exportSliceOut
325 func exportSliceOut() []byte {
329 //export exportSliceInOut
330 func exportSliceInOut(s []byte) []byte {
337 if runtime.GOOS == "android" {
340 // Same as test3775 but run during init so that
341 // there are two levels of internal runtime lock
342 // (1 for init, 1 for cgo).
343 // This would have been broken by CL 11663043.
347 func test3775(t *testing.T) {
348 if runtime.GOOS == "android" {
351 // Used to panic because of the UnlockOSThread below.
355 //export lockOSThreadCallback
356 func lockOSThreadCallback() {
357 runtime.LockOSThread()
358 runtime.UnlockOSThread()
363 // issue 4054 part 2 - part 1 in test.go
365 var issue4054b = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.II, C.J}
367 //export issue5548FromC
368 func issue5548FromC(s string, i int) int {
369 if len(s) == 4 && s == "test" && i == 42 {
372 println("got", len(s), i)
376 func test5548(t *testing.T) {
377 if x := C.issue5548_in_c(); x != 12345 {
378 t.Errorf("issue5548_in_c = %d, want %d", x, 12345)
384 //export GoIssue6833Func
385 func GoIssue6833Func(aui uint, aui64 uint64) uint64 {
386 return aui64 + uint64(aui)
389 func test6833(t *testing.T) {
391 ull := uint64(0x4000300020001000)
392 v := uint64(C.issue6833Func(C.uint(ui), C.ulonglong(ull)))
393 exp := uint64(ui) + ull
395 t.Errorf("issue6833Func() returns %x, expected %x", v, exp)
401 const CString = "C string"
403 //export CheckIssue6907Go
404 func CheckIssue6907Go(s string) C.int {
411 func test6907Go(t *testing.T) {
412 if got := C.CheckIssue6907C(CString); got != 1 {
413 t.Errorf("C.CheckIssue6907C() == %d, want %d", got, 1)
419 var bad7665 unsafe.Pointer = C.f7665
420 var good7665 uintptr = uintptr(C.f7665)
422 func test7665(t *testing.T) {
423 if bad7665 == nil || uintptr(bad7665) != good7665 {
424 t.Errorf("ptrs = %p, %#x, want same non-nil pointer", bad7665, good7665)
430 var issue7978sync uint32
432 func issue7978check(t *testing.T, wantFunc string, badFunc string, depth int) {
434 buf := make([]byte, 65536)
435 trace := string(buf[:runtime.Stack(buf, true)])
436 for _, goroutine := range strings.Split(trace, "\n\n") {
437 if strings.Contains(goroutine, "test.issue7978go") {
438 trace := strings.Split(goroutine, "\n")
439 // look for the expected function in the stack
440 for i := 0; i < depth; i++ {
441 if badFunc != "" && strings.Contains(trace[1+2*i], badFunc) {
442 t.Errorf("bad stack: found %s in the stack:\n%s", badFunc, goroutine)
445 if strings.Contains(trace[1+2*i], wantFunc) {
449 t.Errorf("bad stack: didn't find %s in the stack:\n%s", wantFunc, goroutine)
453 t.Errorf("bad stack: goroutine not found. Full stack dump:\n%s", trace)
456 func issue7978wait(store uint32, wait uint32) {
458 atomic.StoreUint32(&issue7978sync, store)
460 for atomic.LoadUint32(&issue7978sync) != wait {
467 // Force a stack growth from the callback to put extra
468 // pressure on the runtime. See issue #17785.
473 func growStack(n int) int {
478 return buf[growStack(n-1)]
482 C.issue7978c((*C.uint32_t)(&issue7978sync))
486 func test7978(t *testing.T) {
487 if runtime.Compiler == "gccgo" {
488 t.Skip("gccgo can not do stack traces of C code")
490 debug.SetTraceback("2")
493 // test in c code, before callback
495 issue7978check(t, "_Cfunc_issue7978c(", "", 1)
496 // test in go code, during callback
498 issue7978check(t, "test.issue7978cb(", "test.issue7978go", 3)
499 // test in c code, after callback
501 issue7978check(t, "_Cfunc_issue7978c(", "_cgoexpwrap", 1)
502 // test in go code, after return from cgo
504 issue7978check(t, "test.issue7978go(", "", 3)
505 atomic.StoreUint32(&issue7978sync, 8)
510 var issue8331Var C.issue8331
522 func multi() (*C.char, C.int) {
523 return C.CString("multi"), 0
526 func test20910(t *testing.T) {
530 // issue 28772 part 2
532 const issue28772Constant2 = C.issue28772Constant2
536 //export useIssue31891A
537 func useIssue31891A(c *C.Issue31891A) {}
539 //export useIssue31891B
540 func useIssue31891B(c *C.Issue31891B) {}
542 func test31891(t *testing.T) {
546 // issue 37033, check if cgo.Handle works properly
551 func GoFunc37033(handle C.uintptr_t) {
552 h := cgo.Handle(handle)
553 ch := h.Value().(chan int)
558 // A typedef pointer can be used as the element type.
559 // No runtime test; just make sure it compiles.
560 var _ C.PIssue38408 = &C.Issue38408{i: 1}
562 // issue 49633, example use of cgo.Handle with void*
564 type data49633 struct {
569 func GoFunc49633(context unsafe.Pointer) {
570 h := *(*cgo.Handle)(context)
571 v := h.Value().(*data49633)
575 func test49633(t *testing.T) {
577 h := cgo.NewHandle(v)
579 C.cfunc49633(unsafe.Pointer(&h))
580 if v.msg != "hello" {
581 t.Errorf("msg = %q, want 'hello'", v.msg)