diff options
Diffstat (limited to 'misc/cgo')
42 files changed, 825 insertions, 102 deletions
diff --git a/misc/cgo/life/life.go b/misc/cgo/life/life.go index ec000ce3a..fda5495e5 100644 --- a/misc/cgo/life/life.go +++ b/misc/cgo/life/life.go @@ -1,3 +1,5 @@ +// skip + // Copyright 2010 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. @@ -9,8 +11,8 @@ import "C" import "unsafe" -func Run(gen, x, y int, a []int) { - n := make([]int, x*y) +func Run(gen, x, y int, a []int32) { + n := make([]int32, x*y) for i := 0; i < gen; i++ { C.Step(C.int(x), C.int(y), (*C.int)(unsafe.Pointer(&a[0])), (*C.int)(unsafe.Pointer(&n[0]))) copy(a, n) diff --git a/misc/cgo/life/main.go b/misc/cgo/life/main.go index 47ae0e18c..725e10f76 100644 --- a/misc/cgo/life/main.go +++ b/misc/cgo/life/main.go @@ -1,3 +1,5 @@ +// cmpout + // Copyright 2010 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. @@ -22,7 +24,7 @@ var gen = flag.Int("gen", 10, "generations") func main() { flag.Parse() - var a [MAXDIM * MAXDIM]int + var a [MAXDIM * MAXDIM]int32 for i := 2; i < *dim; i += 8 { for j := 2; j < *dim-3; j += 8 { for y := 0; y < 3; y++ { diff --git a/misc/cgo/life/golden.out b/misc/cgo/life/main.out index 539d2106d..26fc9c6e3 100644 --- a/misc/cgo/life/golden.out +++ b/misc/cgo/life/main.out @@ -1,4 +1,3 @@ -* life XXX XXX diff --git a/misc/cgo/life/test.bash b/misc/cgo/life/test.bash deleted file mode 100755 index bb483522c..000000000 --- a/misc/cgo/life/test.bash +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# Copyright 2010 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. - -set -e -go build -o life main.go - -echo '*' life >run.out -./life >>run.out -diff run.out golden.out - -rm -f life - diff --git a/misc/cgo/stdio/chain.go b/misc/cgo/stdio/chain.go index 1cf0b1fe5..a55cefa40 100644 --- a/misc/cgo/stdio/chain.go +++ b/misc/cgo/stdio/chain.go @@ -1,3 +1,5 @@ +// cmpout + // Copyright 2009 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. diff --git a/misc/cgo/stdio/chain.out b/misc/cgo/stdio/chain.out new file mode 100644 index 000000000..963cf9b66 --- /dev/null +++ b/misc/cgo/stdio/chain.out @@ -0,0 +1,55 @@ +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/misc/cgo/stdio/fib.go b/misc/cgo/stdio/fib.go index 6d3ccfd52..981ffeb9a 100644 --- a/misc/cgo/stdio/fib.go +++ b/misc/cgo/stdio/fib.go @@ -1,3 +1,5 @@ +// cmpout + // Copyright 2009 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. diff --git a/misc/cgo/stdio/fib.out b/misc/cgo/stdio/fib.out new file mode 100644 index 000000000..17ff50335 --- /dev/null +++ b/misc/cgo/stdio/fib.out @@ -0,0 +1,91 @@ +0 +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 +144 +233 +377 +610 +987 +1597 +2584 +4181 +6765 +10946 +17711 +28657 +46368 +75025 +121393 +196418 +317811 +514229 +832040 +1346269 +2178309 +3524578 +5702887 +9227465 +14930352 +24157817 +39088169 +63245986 +102334155 +165580141 +267914296 +433494437 +701408733 +1134903170 +1836311903 +2971215073 +4807526976 +7778742049 +12586269025 +20365011074 +32951280099 +53316291173 +86267571272 +139583862445 +225851433717 +365435296162 +591286729879 +956722026041 +1548008755920 +2504730781961 +4052739537881 +6557470319842 +10610209857723 +17167680177565 +27777890035288 +44945570212853 +72723460248141 +117669030460994 +190392490709135 +308061521170129 +498454011879264 +806515533049393 +1304969544928657 +2111485077978050 +3416454622906707 +5527939700884757 +8944394323791464 +14472334024676221 +23416728348467685 +37889062373143906 +61305790721611591 +99194853094755497 +160500643816367088 +259695496911122585 +420196140727489673 +679891637638612258 +1100087778366101931 +1779979416004714189 +2880067194370816120 diff --git a/misc/cgo/stdio/file.go b/misc/cgo/stdio/file.go index 6e7d479ad..e7bb906a5 100644 --- a/misc/cgo/stdio/file.go +++ b/misc/cgo/stdio/file.go @@ -1,3 +1,5 @@ +// skip + // Copyright 2009 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. @@ -23,9 +25,6 @@ import "unsafe" type File C.FILE -var Stdout = (*File)(C.stdout) -var Stderr = (*File)(C.stderr) - // Test reference to library symbol. // Stdout and stderr are too special to be a reliable test. //var = C.environ diff --git a/misc/cgo/stdio/hello.go b/misc/cgo/stdio/hello.go index 4ab3c7447..9cfeefbba 100644 --- a/misc/cgo/stdio/hello.go +++ b/misc/cgo/stdio/hello.go @@ -1,3 +1,5 @@ +// cmpout + // Copyright 2009 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. diff --git a/misc/cgo/stdio/hello.out b/misc/cgo/stdio/hello.out new file mode 100644 index 000000000..4b5fa6370 --- /dev/null +++ b/misc/cgo/stdio/hello.out @@ -0,0 +1 @@ +hello, world diff --git a/misc/cgo/stdio/golden.out b/misc/cgo/stdio/run.out index c0e496547..c0e496547 100644 --- a/misc/cgo/stdio/golden.out +++ b/misc/cgo/stdio/run.out diff --git a/misc/cgo/stdio/stdio.go b/misc/cgo/stdio/stdio.go new file mode 100644 index 000000000..76cb8ad80 --- /dev/null +++ b/misc/cgo/stdio/stdio.go @@ -0,0 +1,22 @@ +// skip + +// Copyright 2009 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 stdio + +/* +#include <stdio.h> + +// on mingw, stderr and stdout are defined as &_iob[FILENO] +// on netbsd, they are defined as &__sF[FILENO] +// and cgo doesn't recognize them, so write a function to get them, +// instead of depending on internals of libc implementation. +FILE *getStdout(void) { return stdout; } +FILE *getStderr(void) { return stderr; } +*/ +import "C" + +var Stdout = (*File)(C.getStdout()) +var Stderr = (*File)(C.getStderr()) diff --git a/misc/cgo/stdio/test.bash b/misc/cgo/stdio/test.bash deleted file mode 100755 index 21829fa31..000000000 --- a/misc/cgo/stdio/test.bash +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# Copyright 2009 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. - -set -e -go build hello.go -go build fib.go -go build chain.go - -echo '*' hello >run.out -./hello >>run.out -echo '*' fib >>run.out -./fib >>run.out -echo '*' chain >>run.out -./chain >>run.out -diff run.out golden.out - -rm -f hello fib chain - diff --git a/misc/cgo/test/Makefile b/misc/cgo/test/Makefile deleted file mode 100644 index 2b7187acb..000000000 --- a/misc/cgo/test/Makefile +++ /dev/null @@ -1,48 +0,0 @@ -# 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. - -include ../../../src/Make.inc - -TARG=runtime/cgotest - -CGOFILES=\ - align.go\ - basic.go\ - callback.go\ - env.go\ - exports.go\ - helpers.go\ - issue1222.go\ - issue1328.go\ - issue1560.go\ - issue2462.go\ - duplicate_symbol.go\ - -CGO_OFILES=\ - callback_c.o\ - -OFILES=\ - runtime.$O\ - -ifeq ($(GOOS),windows) -GCCVERSION=$(shell gcc -dumpversion) -ifeq ($(GOARCH),386) -GCCLIBDIR=/mingw/lib/gcc/mingw32/$(GCCVERSION) -CHKSTK=_chkstk.o -else -GCCLIBDIR=/mingw/lib/gcc/x86_64-w64-mingw32/$(GCCVERSION) -CHKSTK=_chkstk_ms.o -endif - -CGOFILES+=sleep_windows.go -CGO_OFILES+=$(CHKSTK) - -$(CHKSTK): - ar -x "$(GCCLIBDIR)/libgcc.a" $@ -endif - -include ../../../src/Make.pkg - -test: - echo cgo: tests disabled. gotest is gone. TODO
\ No newline at end of file diff --git a/misc/cgo/test/backdoor/runtime.c b/misc/cgo/test/backdoor/runtime.c index 54e6a1ef8..194a9c8e4 100644 --- a/misc/cgo/test/backdoor/runtime.c +++ b/misc/cgo/test/backdoor/runtime.c @@ -6,6 +6,8 @@ // Must be in a non-cgo-using package so that // the go command compiles this file with 6c, not gcc. +// +build gc + typedef char bool; bool runtime·lockedOSThread(void); diff --git a/misc/cgo/test/backdoor/runtime_gccgo.c b/misc/cgo/test/backdoor/runtime_gccgo.c new file mode 100644 index 000000000..218b2c3eb --- /dev/null +++ b/misc/cgo/test/backdoor/runtime_gccgo.c @@ -0,0 +1,18 @@ +// Copyright 2012 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. + +// Expose some runtime functions for testing. +// This is the gccgo version of runtime.c. + +// +build gccgo + +_Bool runtime_lockedOSThread(void); + +_Bool LockedOSThread(void) asm(GOPKGPATH ".LockedOSThread"); + +_Bool +LockedOSThread(void) +{ + return runtime_lockedOSThread(); +} diff --git a/misc/cgo/test/basic.go b/misc/cgo/test/basic.go index 70ec5e43a..79cbf2b9c 100644 --- a/misc/cgo/test/basic.go +++ b/misc/cgo/test/basic.go @@ -14,15 +14,16 @@ package cgotest #define SHIFT(x, y) ((x)<<(y)) #define KILO SHIFT(1, 10) +#define UINT32VAL 0xc008427bU enum E { Enum1 = 1, Enum2 = 2, }; -typedef unsigned char uuid_t[20]; +typedef unsigned char cgo_uuid_t[20]; -void uuid_generate(uuid_t x) { +void uuid_generate(cgo_uuid_t x) { x[0] = 0; } @@ -55,6 +56,7 @@ int add(int x, int y) { */ import "C" import ( + "runtime" "syscall" "testing" "unsafe" @@ -65,7 +67,7 @@ const EINVAL = C.EINVAL /* test #define */ var KILO = C.KILO func uuidgen() { - var uuid C.uuid_t + var uuid C.cgo_uuid_t C.uuid_generate(&uuid[0]) } @@ -118,7 +120,12 @@ func testErrno(t *testing.T) { func testMultipleAssign(t *testing.T) { p := C.CString("234") n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10) - if n != 0 || m != 234 { + if runtime.GOOS == "openbsd" { + // Bug in OpenBSD strtol(3) - base > 36 succeeds. + if (n != 0 && n != 239089) || m != 234 { + t.Fatal("Strtol x2: ", n, m) + } + } else if n != 0 || m != 234 { t.Fatal("Strtol x2: ", n, m) } C.free(unsafe.Pointer(p)) @@ -141,3 +148,12 @@ func benchCgoCall(b *testing.B) { C.add(x, y) } } + +// Issue 2470. +func testUnsignedInt(t *testing.T) { + a := (int64)(C.UINT32VAL) + b := (int64)(0xc008427b) + if a != b { + t.Errorf("Incorrect unsigned int - got %x, want %x", a, b) + } +} diff --git a/misc/cgo/test/callback.go b/misc/cgo/test/callback.go index e6a1462b3..4f5d3f855 100644 --- a/misc/cgo/test/callback.go +++ b/misc/cgo/test/callback.go @@ -110,7 +110,7 @@ func testZeroArgCallback(t *testing.T) { func goFoo() { x := 1 for i := 0; i < 10000; i++ { - // variadic call mallocs + writes to + // variadic call mallocs + writes to variadic(x, x, x) if x != 1 { panic("bad x") diff --git a/misc/cgo/test/cgo_linux_test.go b/misc/cgo/test/cgo_linux_test.go new file mode 100644 index 000000000..056d67c96 --- /dev/null +++ b/misc/cgo/test/cgo_linux_test.go @@ -0,0 +1,9 @@ +// Copyright 2012 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 + +import "testing" + +func TestSetgid(t *testing.T) { testSetgid(t) } diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go index 34beee69d..536fa507a 100644 --- a/misc/cgo/test/cgo_test.go +++ b/misc/cgo/test/cgo_test.go @@ -6,7 +6,7 @@ package cgotest import "testing" -// The actual test functions are in non-_test.go files +// The actual test functions are in non-_test.go files // so that they can use cgo (import "C"). // These wrappers are here for gotest to find. @@ -16,6 +16,7 @@ func TestEnum(t *testing.T) { testEnum(t) } func TestAtol(t *testing.T) { testAtol(t) } func TestErrno(t *testing.T) { testErrno(t) } func TestMultipleAssign(t *testing.T) { testMultipleAssign(t) } +func TestUnsignedInt(t *testing.T) { testUnsignedInt(t) } func TestCallback(t *testing.T) { testCallback(t) } func TestCallbackGC(t *testing.T) { testCallbackGC(t) } func TestCallbackPanic(t *testing.T) { testCallbackPanic(t) } @@ -27,5 +28,13 @@ func Test1328(t *testing.T) { test1328(t) } func TestParallelSleep(t *testing.T) { testParallelSleep(t) } func TestSetEnv(t *testing.T) { testSetEnv(t) } func TestHelpers(t *testing.T) { testHelpers(t) } +func TestLibgcc(t *testing.T) { testLibgcc(t) } +func Test1635(t *testing.T) { test1635(t) } +func TestPrintf(t *testing.T) { testPrintf(t) } +func Test4029(t *testing.T) { test4029(t) } +func TestBoolAlign(t *testing.T) { testBoolAlign(t) } +func Test3729(t *testing.T) { test3729(t) } +func Test3775(t *testing.T) { test3775(t) } +func TestCthread(t *testing.T) { testCthread(t) } func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) } diff --git a/misc/cgo/test/cthread.go b/misc/cgo/test/cthread.go new file mode 100644 index 000000000..d918d033f --- /dev/null +++ b/misc/cgo/test/cthread.go @@ -0,0 +1,44 @@ +// Copyright 2013 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 + +// extern void doAdd(int, int); +import "C" + +import ( + "runtime" + "sync" + "testing" +) + +var sum struct { + sync.Mutex + i int +} + +//export Add +func Add(x int) { + defer func() { + recover() + }() + sum.Lock() + sum.i += x + sum.Unlock() + var p *int + *p = 2 +} + +func testCthread(t *testing.T) { + if runtime.GOARCH == "arm" { + t.Skip("testCthread disabled on arm") + } + + C.doAdd(10, 6) + + want := 10 * (10 - 1) / 2 * 6 + if sum.i != want { + t.Fatalf("sum=%d, want %d", sum.i, want) + } +} diff --git a/misc/cgo/test/cthread_unix.c b/misc/cgo/test/cthread_unix.c new file mode 100644 index 000000000..998bc00cb --- /dev/null +++ b/misc/cgo/test/cthread_unix.c @@ -0,0 +1,34 @@ +// Copyright 2013 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. + +// +build darwin freebsd linux netbsd openbsd + +#include <pthread.h> +#include "_cgo_export.h" + +static void* +addThread(void *p) +{ + int i, max; + + max = *(int*)p; + for(i=0; i<max; i++) + Add(i); + return 0; +} + +void +doAdd(int max, int nthread) +{ + enum { MaxThread = 20 }; + int i; + pthread_t thread_id[MaxThread]; + + if(nthread > MaxThread) + nthread = MaxThread; + for(i=0; i<nthread; i++) + pthread_create(&thread_id[i], 0, addThread, &max); + for(i=0; i<nthread; i++) + pthread_join(thread_id[i], 0); +} diff --git a/misc/cgo/test/cthread_windows.c b/misc/cgo/test/cthread_windows.c new file mode 100644 index 000000000..5f370a818 --- /dev/null +++ b/misc/cgo/test/cthread_windows.c @@ -0,0 +1,37 @@ +// Copyright 2013 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. + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <process.h> +#include "_cgo_export.h" + +__stdcall +static unsigned int +addThread(void *p) +{ + int i, max; + + max = *(int*)p; + for(i=0; i<max; i++) + Add(i); + return 0; +} + +void +doAdd(int max, int nthread) +{ + enum { MaxThread = 20 }; + int i; + uintptr_t thread_id[MaxThread]; + + if(nthread > MaxThread) + nthread = MaxThread; + for(i=0; i<nthread; i++) + thread_id[i] = _beginthreadex(0, 0, addThread, &max, 0, 0); + for(i=0; i<nthread; i++) { + WaitForSingleObject((HANDLE)thread_id[i], INFINITE); + CloseHandle((HANDLE)thread_id[i]); + } +} diff --git a/misc/cgo/test/issue1560.go b/misc/cgo/test/issue1560.go index 3faa966e7..147ce94b5 100644 --- a/misc/cgo/test/issue1560.go +++ b/misc/cgo/test/issue1560.go @@ -15,6 +15,7 @@ void twoSleep(int); import "C" import ( + "runtime" "testing" "time" ) @@ -27,19 +28,48 @@ func parallelSleep(n int) { } //export BackgroundSleep -func BackgroundSleep(n int) { +func BackgroundSleep(n int32) { go func() { C.sleep(C.uint(n)) sleepDone <- true }() } +// wasteCPU starts a background goroutine to waste CPU +// to cause the power management to raise the CPU frequency. +// On ARM this has the side effect of making sleep more accurate. +func wasteCPU() chan struct{} { + done := make(chan struct{}) + go func() { + for { + select { + case <-done: + return + default: + } + } + }() + // pause for a short amount of time to allow the + // power management to recognise load has risen. + <-time.After(300 * time.Millisecond) + return done +} + func testParallelSleep(t *testing.T) { + if runtime.GOARCH == "arm" { + // on ARM, the 1.3s deadline is frequently missed, + // and burning cpu seems to help + defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2)) + defer close(wasteCPU()) + } + + sleepSec := 1 start := time.Now() - parallelSleep(1) - dt := time.Now().Sub(start) - // bug used to run sleeps in serial, producing a 2-second delay. - if dt >= 1300*time.Millisecond { - t.Fatalf("parallel 1-second sleeps slept for %f seconds", dt.Seconds()) + parallelSleep(sleepSec) + dt := time.Since(start) + t.Logf("sleep(%d) slept for %v", sleepSec, dt) + // bug used to run sleeps in serial, producing a 2*sleepSec-second delay. + if dt >= time.Duration(sleepSec)*1300*time.Millisecond { + t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds()) } } diff --git a/misc/cgo/test/issue1635.go b/misc/cgo/test/issue1635.go new file mode 100644 index 000000000..6bfe110fd --- /dev/null +++ b/misc/cgo/test/issue1635.go @@ -0,0 +1,33 @@ +// Copyright 2012 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 + +/* +// Mac OS X's gcc will generate scattered relocation 2/1 for +// this function on Darwin/386, and 8l couldn't handle it. +// this example is in issue 1635 +#include <stdio.h> +void scatter() { + void *p = scatter; + printf("scatter = %p\n", p); +} + +// this example is in issue 3253 +int hola = 0; +int testHola() { return hola; } +*/ +import "C" + +import "testing" + +func test1635(t *testing.T) { + C.scatter() + if v := C.hola; v != 0 { + t.Fatalf("C.hola is %d, should be 0", v) + } + if v := C.testHola(); v != 0 { + t.Fatalf("C.testHola() is %d, should be 0", v) + } +} diff --git a/misc/cgo/test/issue3261.go b/misc/cgo/test/issue3261.go new file mode 100644 index 000000000..0411be892 --- /dev/null +++ b/misc/cgo/test/issue3261.go @@ -0,0 +1,43 @@ +// Copyright 2012 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 + +/* +// libgcc on ARM might be compiled as thumb code, but our 5l +// can't handle that, so we have to disable this test on arm. +#ifdef __ARMEL__ +#include <stdio.h> +int vabs(int x) { + puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library."); + return (x < 0) ? -x : x; +} +#else +int __absvsi2(int); // dummy prototype for libgcc function +// we shouldn't name the function abs, as gcc might use +// the builtin one. +int vabs(int x) { return __absvsi2(x); } +#endif +*/ +import "C" + +import "testing" + +func testLibgcc(t *testing.T) { + var table = []struct { + in, out C.int + }{ + {0, 0}, + {1, 1}, + {-42, 42}, + {1000300, 1000300}, + {1 - 1<<31, 1<<31 - 1}, + } + for _, v := range table { + if o := C.vabs(v.in); o != v.out { + t.Fatalf("abs(%d) got %d, should be %d", v.in, o, v.out) + return + } + } +} diff --git a/misc/cgo/test/issue3729.go b/misc/cgo/test/issue3729.go new file mode 100644 index 000000000..1bea38b6e --- /dev/null +++ b/misc/cgo/test/issue3729.go @@ -0,0 +1,47 @@ +// Copyright 2012 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. + +// Issue 3729: cmd/cgo: access errno from void C function +// void f(void) returns [0]byte, error in Go world. + +// +build !windows + +package cgotest + +/* +#include <errno.h> + +void g(void) { + errno = E2BIG; +} + +// try to pass some non-trivial arguments to function g2 +const char _expA = 0x42; +const float _expB = 3.14159; +const short _expC = 0x55aa; +const int _expD = 0xdeadbeef; +void g2(int x, char a, float b, short c, int d) { + if (a == _expA && b == _expB && c == _expC && d == _expD) + errno = x; + else + errno = -1; +} +*/ +import "C" + +import ( + "syscall" + "testing" +) + +func test3729(t *testing.T) { + _, e := C.g() + if e != syscall.E2BIG { + t.Errorf("got %q, expect %q", e, syscall.E2BIG) + } + _, e = C.g2(C.EINVAL, C._expA, C._expB, C._expC, C._expD) + if e != syscall.EINVAL { + t.Errorf("got %q, expect %q", e, syscall.EINVAL) + } +} diff --git a/misc/cgo/test/issue3729w.go b/misc/cgo/test/issue3729w.go new file mode 100644 index 000000000..702115b81 --- /dev/null +++ b/misc/cgo/test/issue3729w.go @@ -0,0 +1,16 @@ +// Copyright 2012 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. + +// Issue 3729: cmd/cgo: access errno from void C function +// void f(void) returns [0]byte, error in Go world. + +// +build windows + +package cgotest + +import "testing" + +func test3729(t *testing.T) { + t.Log("skip errno test on Windows") +} diff --git a/misc/cgo/test/issue3741.go b/misc/cgo/test/issue3741.go new file mode 100644 index 000000000..3d3bbf951 --- /dev/null +++ b/misc/cgo/test/issue3741.go @@ -0,0 +1,22 @@ +// Copyright 2012 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 + +import "C" + +//export exportSliceIn +func exportSliceIn(s []byte) bool { + return len(s) == cap(s) +} + +//export exportSliceOut +func exportSliceOut() []byte { + return []byte{1} +} + +//export exportSliceInOut +func exportSliceInOut(s []byte) []byte { + return s +} diff --git a/misc/cgo/test/issue3775.go b/misc/cgo/test/issue3775.go new file mode 100644 index 000000000..c05a5d4be --- /dev/null +++ b/misc/cgo/test/issue3775.go @@ -0,0 +1,29 @@ +package cgotest + +/* +void lockOSThreadCallback(void); +inline static void lockOSThreadC(void) +{ + lockOSThreadCallback(); +} +int usleep(unsigned usec); +*/ +import "C" + +import ( + "runtime" + "testing" +) + +func test3775(t *testing.T) { + // Used to panic because of the UnlockOSThread below. + C.lockOSThreadC() +} + +//export lockOSThreadCallback +func lockOSThreadCallback() { + runtime.LockOSThread() + runtime.UnlockOSThread() + go C.usleep(10000) + runtime.Gosched() +} diff --git a/misc/cgo/test/issue3945.go b/misc/cgo/test/issue3945.go new file mode 100644 index 000000000..331cd0baf --- /dev/null +++ b/misc/cgo/test/issue3945.go @@ -0,0 +1,22 @@ +// Copyright 2012 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 + +// Test that cgo reserves enough stack space during cgo call. +// See http://golang.org/issue/3945 for details. + +// #include <stdio.h> +// +// void say() { +// printf("%s from C\n", "hello"); +// } +// +import "C" + +import "testing" + +func testPrintf(t *testing.T) { + C.say() +} diff --git a/misc/cgo/test/issue4029.go b/misc/cgo/test/issue4029.go new file mode 100644 index 000000000..7495d38fe --- /dev/null +++ b/misc/cgo/test/issue4029.go @@ -0,0 +1,60 @@ +// Copyright 2012 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. + +// +build !windows + +package cgotest + +/* +#include <dlfcn.h> +#cgo linux LDFLAGS: -ldl +*/ +import "C" + +import ( + "fmt" + "testing" +) + +//export IMPIsOpaque +func IMPIsOpaque() { + fmt.Println("isOpaque") +} + +//export IMPInitWithFrame +func IMPInitWithFrame() { + fmt.Println("IInitWithFrame") +} + +//export IMPDrawRect +func IMPDrawRect() { + fmt.Println("drawRect:") +} + +//export IMPWindowResize +func IMPWindowResize() { + fmt.Println("windowDidResize:") +} + +func test4029(t *testing.T) { + loadThySelf(t, "IMPWindowResize") + loadThySelf(t, "IMPDrawRect") + loadThySelf(t, "IMPInitWithFrame") + loadThySelf(t, "IMPIsOpaque") +} + +func loadThySelf(t *testing.T, symbol string) { + this_process := C.dlopen(nil, C.RTLD_NOW) + if this_process == nil { + t.Fatal("dlopen:", C.GoString(C.dlerror())) + } + defer C.dlclose(this_process) + + symbol_address := C.dlsym(this_process, C.CString(symbol)) + if symbol_address == nil { + t.Fatal("dlsym:", C.GoString(C.dlerror())) + } else { + t.Log(symbol, symbol_address) + } +} diff --git a/misc/cgo/test/issue4029w.go b/misc/cgo/test/issue4029w.go new file mode 100644 index 000000000..1cf43df37 --- /dev/null +++ b/misc/cgo/test/issue4029w.go @@ -0,0 +1,12 @@ +// Copyright 2012 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. + +// +build windows + +package cgotest + +import "testing" + +func test4029(t *testing.T) { +} diff --git a/misc/cgo/test/issue4054a.go b/misc/cgo/test/issue4054a.go new file mode 100644 index 000000000..2abdac590 --- /dev/null +++ b/misc/cgo/test/issue4054a.go @@ -0,0 +1,23 @@ +// Copyright 2012 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 + +/* +typedef enum { + A = 0, + B, + C, + D, + E, + F, + G, + H, + I, + J, +} issue4054a; +*/ +import "C" + +var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J} diff --git a/misc/cgo/test/issue4054b.go b/misc/cgo/test/issue4054b.go new file mode 100644 index 000000000..048964c89 --- /dev/null +++ b/misc/cgo/test/issue4054b.go @@ -0,0 +1,23 @@ +// Copyright 2012 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 + +/* +typedef enum { + A = 0, + B, + C, + D, + E, + F, + G, + H, + I, + J, +} issue4054b; +*/ +import "C" + +var issue4054b = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J} diff --git a/misc/cgo/test/issue4273.c b/misc/cgo/test/issue4273.c new file mode 100644 index 000000000..a3fcf3b0a --- /dev/null +++ b/misc/cgo/test/issue4273.c @@ -0,0 +1,10 @@ +// Copyright 2012 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. + +#ifdef __ELF__ +__attribute__((weak)) +__attribute__((visibility("hidden"))) +void _compilerrt_abort_impl(const char *file, int line, const char *func) { +} +#endif diff --git a/misc/cgo/test/issue4273b.c b/misc/cgo/test/issue4273b.c new file mode 100644 index 000000000..93e2f4fab --- /dev/null +++ b/misc/cgo/test/issue4273b.c @@ -0,0 +1,11 @@ +// Copyright 2012 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. + +#ifdef __ELF__ +extern void _compilerrt_abort_impl(const char *file, int line, const char *func); + +void __my_abort(const char *file, int line, const char *func) { + _compilerrt_abort_impl(file, line, func); +} +#endif diff --git a/misc/cgo/test/issue4417.go b/misc/cgo/test/issue4417.go new file mode 100644 index 000000000..0b48071d4 --- /dev/null +++ b/misc/cgo/test/issue4417.go @@ -0,0 +1,42 @@ +// Copyright 2012 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. + +// Issue 4417: cmd/cgo: bool alignment/padding issue. +// bool alignment is wrong and causing wrong arguments when calling functions. +// + +package cgotest + +/* +#include <stdbool.h> + +static int c_bool(bool a, bool b, int c, bool d, bool e) { + return c; +} +*/ +import "C" +import "testing" + +func testBoolAlign(t *testing.T) { + b := C.c_bool(true, true, 10, true, false) + if b != 10 { + t.Fatalf("found %d expected 10\n", b) + } + b = C.c_bool(true, true, 5, true, true) + if b != 5 { + t.Fatalf("found %d expected 5\n", b) + } + b = C.c_bool(true, true, 3, true, false) + if b != 3 { + t.Fatalf("found %d expected 3\n", b) + } + b = C.c_bool(false, false, 1, true, false) + if b != 1 { + t.Fatalf("found %d expected 1\n", b) + } + b = C.c_bool(false, true, 200, true, false) + if b != 200 { + t.Fatalf("found %d expected 200\n", b) + } +} diff --git a/misc/cgo/test/setgid_linux.go b/misc/cgo/test/setgid_linux.go new file mode 100644 index 000000000..829afce1b --- /dev/null +++ b/misc/cgo/test/setgid_linux.go @@ -0,0 +1,32 @@ +// Copyright 2012 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. + +// Test that setgid does not hang on GNU/Linux. +// See http://code.google.com/p/go/issues/detail?id=3871 for details. + +package cgotest + +/* +#include <sys/types.h> +#include <unistd.h> +*/ +import "C" + +import ( + "testing" + "time" +) + +func testSetgid(t *testing.T) { + c := make(chan bool) + go func() { + C.setgid(0) + c <- true + }() + select { + case <-c: + case <-time.After(5 * time.Second): + t.Error("setgid hung") + } +} diff --git a/misc/cgo/test/sleep_windows.go b/misc/cgo/test/sleep_windows_386.go index 007a1bb4c..75687d783 100644 --- a/misc/cgo/test/sleep_windows.go +++ b/misc/cgo/test/sleep_windows_386.go @@ -5,6 +5,10 @@ package cgotest /* +// mingw32 on windows/386 provides usleep() but not sleep(), +// as we don't want to require all other OSes to provide usleep, +// we emulate sleep(int s) using win32 API Sleep(int ms). + #include <windows.h> unsigned int sleep(unsigned int seconds) { diff --git a/misc/cgo/testso/test.bash b/misc/cgo/testso/test.bash index ecef873c8..5f113d216 100755 --- a/misc/cgo/testso/test.bash +++ b/misc/cgo/testso/test.bash @@ -4,7 +4,7 @@ # license that can be found in the LICENSE file. set -e -gcc $(go env GOGCCFLAGS) -shared -o libcgosotest.so cgoso_c.c +$(go env CC) $(go env GOGCCFLAGS) -shared -o libcgosotest.so cgoso_c.c go build main.go LD_LIBRARY_PATH=. ./main rm -f libcgosotest.so main |