diff options
author | Ian Lance Taylor <iant@golang.org> | 2019-09-06 18:12:46 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2019-09-06 18:12:46 +0000 |
commit | aa8901e9bb0399d2c16f988ba2fe46eb0c0c5d13 (patch) | |
tree | 7e63b06d1eec92beec6997c9d3ab47a5d6a835be /libgo/misc/cgo/test | |
parent | 920ea3b8ba3164b61ac9490dfdfceb6936eda6dd (diff) | |
download | gcc-aa8901e9bb0399d2c16f988ba2fe46eb0c0c5d13.zip gcc-aa8901e9bb0399d2c16f988ba2fe46eb0c0c5d13.tar.gz gcc-aa8901e9bb0399d2c16f988ba2fe46eb0c0c5d13.tar.bz2 |
libgo: update to Go 1.13beta1 release
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/193497
From-SVN: r275473
Diffstat (limited to 'libgo/misc/cgo/test')
156 files changed, 2929 insertions, 3343 deletions
diff --git a/libgo/misc/cgo/test/align.go b/libgo/misc/cgo/test/align.go deleted file mode 100644 index a23b44f..0000000 --- a/libgo/misc/cgo/test/align.go +++ /dev/null @@ -1,76 +0,0 @@ -// 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. - -package cgotest - -/* -#include <stdio.h> - -typedef unsigned char Uint8; -typedef unsigned short Uint16; - -typedef enum { - MOD1 = 0x0000, - MODX = 0x8000 -} SDLMod; - -typedef enum { - A = 1, - B = 322, - SDLK_LAST -} SDLKey; - -typedef struct SDL_keysym { - Uint8 scancode; - SDLKey sym; - SDLMod mod; - Uint16 unicode; -} SDL_keysym; - -typedef struct SDL_KeyboardEvent { - Uint8 typ; - Uint8 which; - Uint8 state; - SDL_keysym keysym; -} SDL_KeyboardEvent; - -void makeEvent(SDL_KeyboardEvent *event) { - unsigned char *p; - int i; - - p = (unsigned char*)event; - for (i=0; i<sizeof *event; i++) { - p[i] = i; - } -} - -int same(SDL_KeyboardEvent* e, Uint8 typ, Uint8 which, Uint8 state, Uint8 scan, SDLKey sym, SDLMod mod, Uint16 uni) { - return e->typ == typ && e->which == which && e->state == state && e->keysym.scancode == scan && e->keysym.sym == sym && e->keysym.mod == mod && e->keysym.unicode == uni; -} - -void cTest(SDL_KeyboardEvent *event) { - printf("C: %#x %#x %#x %#x %#x %#x %#x\n", event->typ, event->which, event->state, - event->keysym.scancode, event->keysym.sym, event->keysym.mod, event->keysym.unicode); - fflush(stdout); -} - -*/ -import "C" - -import ( - "testing" -) - -func testAlign(t *testing.T) { - var evt C.SDL_KeyboardEvent - C.makeEvent(&evt) - if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 { - t.Error("*** bad alignment") - C.cTest(&evt) - t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n", - evt.typ, evt.which, evt.state, evt.keysym.scancode, - evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) - t.Error(evt) - } -} diff --git a/libgo/misc/cgo/test/api.go b/libgo/misc/cgo/test/api.go deleted file mode 100644 index d2b09cb..0000000 --- a/libgo/misc/cgo/test/api.go +++ /dev/null @@ -1,30 +0,0 @@ -// 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. - -// API Compatibility Checks for cgo - -package cgotest - -// #include <stdlib.h> -// -// // Test for issue 17723. -// typedef char *cstring_pointer; -// static void cstring_pointer_fun(cstring_pointer dummy) { } -// -// const char *api_hello = "hello!"; -import "C" -import "unsafe" - -func testAPI() { - var cs *C.char - cs = C.CString("hello") - defer C.free(unsafe.Pointer(cs)) - var s string - s = C.GoString((*C.char)(C.api_hello)) - s = C.GoStringN((*C.char)(C.api_hello), C.int(6)) - var b []byte - b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6)) - _, _ = s, b - C.cstring_pointer_fun(nil) -} diff --git a/libgo/misc/cgo/test/basic.go b/libgo/misc/cgo/test/basic.go deleted file mode 100644 index 2655a66..0000000 --- a/libgo/misc/cgo/test/basic.go +++ /dev/null @@ -1,181 +0,0 @@ -// 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. - -// Basic test cases for cgo. - -package cgotest - -/* -#include <stdio.h> -#include <stdlib.h> -#include <sys/stat.h> -#include <errno.h> - -#define SHIFT(x, y) ((x)<<(y)) -#define KILO SHIFT(1, 10) -#define UINT32VAL 0xc008427bU - -enum E { - Enum1 = 1, - Enum2 = 2, -}; - -typedef unsigned char cgo_uuid_t[20]; - -void uuid_generate(cgo_uuid_t x) { - x[0] = 0; -} - -struct S { - int x; -}; - -const char *cstr = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890"; - -extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter); - -enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; } - -// issue 1222 -typedef union { - long align; -} xxpthread_mutex_t; - -struct ibv_async_event { - union { - int x; - } element; -}; - -struct ibv_context { - xxpthread_mutex_t mutex; -}; - -int add(int x, int y) { - return x+y; -}; -*/ -import "C" -import ( - "runtime" - "syscall" - "testing" - "unsafe" -) - -const EINVAL = C.EINVAL /* test #define */ - -var KILO = C.KILO - -func uuidgen() { - var uuid C.cgo_uuid_t - C.uuid_generate(&uuid[0]) -} - -func Strtol(s string, base int) (int, error) { - p := C.CString(s) - n, err := C.strtol(p, nil, C.int(base)) - C.free(unsafe.Pointer(p)) - return int(n), err -} - -func Atol(s string) int { - p := C.CString(s) - n := C.atol(p) - C.free(unsafe.Pointer(p)) - return int(n) -} - -func testConst(t *testing.T) { - C.myConstFunc(nil, 0, nil) -} - -func testEnum(t *testing.T) { - if C.Enum1 != 1 || C.Enum2 != 2 { - t.Error("bad enum", C.Enum1, C.Enum2) - } -} - -func testAtol(t *testing.T) { - l := Atol("123") - if l != 123 { - t.Error("Atol 123: ", l) - } -} - -func testErrno(t *testing.T) { - p := C.CString("no-such-file") - m := C.CString("r") - f, err := C.fopen(p, m) - C.free(unsafe.Pointer(p)) - C.free(unsafe.Pointer(m)) - if err == nil { - C.fclose(f) - t.Fatalf("C.fopen: should fail") - } - if err != syscall.ENOENT { - t.Fatalf("C.fopen: unexpected error: %v", err) - } -} - -func testMultipleAssign(t *testing.T) { - p := C.CString("234") - n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10) - 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)) -} - -var ( - cuint = (C.uint)(0) - culong C.ulong - cchar C.char -) - -type Context struct { - ctx *C.struct_ibv_context -} - -func benchCgoCall(b *testing.B) { - const x = C.int(2) - const y = C.int(3) - for i := 0; i < b.N; i++ { - C.add(x, y) - } -} - -var sinkString string - -func benchGoString(b *testing.B) { - for i := 0; i < b.N; i++ { - sinkString = C.GoString(C.cstr) - } - const want = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890" - if sinkString != want { - b.Fatalf("%q != %q", sinkString, want) - } -} - -// 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) - } -} - -// Static (build-time) test that syntax traversal visits all operands of s[i:j:k]. -func sliceOperands(array [2000]int) { - _ = array[C.KILO:C.KILO:C.KILO] // no type error -} - -// set in cgo_thread_lock.go init -var testThreadLockFunc = func(*testing.T) {} diff --git a/libgo/misc/cgo/test/callback.go b/libgo/misc/cgo/test/callback.go index 4fc6b39..e749650 100644 --- a/libgo/misc/cgo/test/callback.go +++ b/libgo/misc/cgo/test/callback.go @@ -199,7 +199,7 @@ func testCallbackCallers(t *testing.T) { t.Errorf("expected %d frames, got %d", len(name), n) } for i := 0; i < n; i++ { - f := runtime.FuncForPC(pc[i]) + f := runtime.FuncForPC(pc[i] - 1) // TODO: use runtime.CallersFrames if f == nil { t.Fatalf("expected non-nil Func for pc %d", pc[i]) } @@ -209,6 +209,10 @@ func testCallbackCallers(t *testing.T) { if strings.HasPrefix(fname, "_") { fname = path.Base(f.Name()[1:]) } + // In module mode, this package has a fully-qualified import path. + // Remove it if present. + fname = strings.TrimPrefix(fname, "misc/cgo/") + namei := "" if i < len(name) { namei = name[i] diff --git a/libgo/misc/cgo/test/cflags.go b/libgo/misc/cgo/test/cflags.go deleted file mode 100644 index bc290bf..0000000 --- a/libgo/misc/cgo/test/cflags.go +++ /dev/null @@ -1,32 +0,0 @@ -// 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. - -// Test that the #cgo CFLAGS directive works, -// with and without platform filters. -// See https://golang.org/issue/5224 for details. -package cgotest - -/* -#cgo CFLAGS: -DCOMMON_VALUE=123 -#cgo windows CFLAGS: -DIS_WINDOWS=1 -#cgo !windows CFLAGS: -DIS_WINDOWS=0 -int common = COMMON_VALUE; -int is_windows = IS_WINDOWS; -*/ -import "C" - -import ( - "runtime" - "testing" -) - -func testCflags(t *testing.T) { - is_windows := C.is_windows == 1 - if is_windows != (runtime.GOOS == "windows") { - t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS) - } - if C.common != 123 { - t.Errorf("common: %v (expected 123)", C.common) - } -} diff --git a/libgo/misc/cgo/test/cgo_linux_test.go b/libgo/misc/cgo/test/cgo_linux_test.go index 9c15f69..7b56e11 100644 --- a/libgo/misc/cgo/test/cgo_linux_test.go +++ b/libgo/misc/cgo/test/cgo_linux_test.go @@ -4,9 +4,16 @@ package cgotest -import "testing" +import ( + "runtime" + "testing" +) -func TestSetgid(t *testing.T) { testSetgid(t) } +func TestSetgid(t *testing.T) { + if runtime.GOOS == "android" { + t.Skip("unsupported on Android") + } + testSetgid(t) +} func Test6997(t *testing.T) { test6997(t) } func TestBuildID(t *testing.T) { testBuildID(t) } -func Test9400(t *testing.T) { test9400(t) } diff --git a/libgo/misc/cgo/test/cgo_stubs_android_test.go b/libgo/misc/cgo/test/cgo_stubs_android_test.go index 710e094..a1c2482 100644 --- a/libgo/misc/cgo/test/cgo_stubs_android_test.go +++ b/libgo/misc/cgo/test/cgo_stubs_android_test.go @@ -8,6 +8,5 @@ import "testing" // Stubs for tests that fails to build on Android func test6997(t *testing.T) {} -func test3775(t *testing.T) {} func test8694(t *testing.T) {} func testSigaltstack(t *testing.T) {} diff --git a/libgo/misc/cgo/test/cgo_test.go b/libgo/misc/cgo/test/cgo_test.go index 2cb93d9..c66df2c 100644 --- a/libgo/misc/cgo/test/cgo_test.go +++ b/libgo/misc/cgo/test/cgo_test.go @@ -10,91 +10,86 @@ import "testing" // so that they can use cgo (import "C"). // These wrappers are here for gotest to find. -func TestAlign(t *testing.T) { testAlign(t) } -func TestConst(t *testing.T) { testConst(t) } -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) } -func TestCallbackPanicLoop(t *testing.T) { testCallbackPanicLoop(t) } -func TestCallbackPanicLocked(t *testing.T) { testCallbackPanicLocked(t) } -func TestPanicFromC(t *testing.T) { testPanicFromC(t) } -func TestZeroArgCallback(t *testing.T) { testZeroArgCallback(t) } -func TestBlocking(t *testing.T) { testBlocking(t) } 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 Test3250(t *testing.T) { test3250(t) } func Test3729(t *testing.T) { test3729(t) } func Test3775(t *testing.T) { test3775(t) } -func TestCthread(t *testing.T) { testCthread(t) } -func TestCallbackCallers(t *testing.T) { testCallbackCallers(t) } +func Test4029(t *testing.T) { test4029(t) } +func Test4339(t *testing.T) { test4339(t) } func Test5227(t *testing.T) { test5227(t) } -func TestCflags(t *testing.T) { testCflags(t) } +func Test5242(t *testing.T) { test5242(t) } func Test5337(t *testing.T) { test5337(t) } func Test5548(t *testing.T) { test5548(t) } func Test5603(t *testing.T) { test5603(t) } -func Test6833(t *testing.T) { test6833(t) } -func Test3250(t *testing.T) { test3250(t) } -func TestCallbackStack(t *testing.T) { testCallbackStack(t) } -func TestFpVar(t *testing.T) { testFpVar(t) } -func Test4339(t *testing.T) { test4339(t) } -func Test6390(t *testing.T) { test6390(t) } func Test5986(t *testing.T) { test5986(t) } -func Test7665(t *testing.T) { test7665(t) } -func TestNaming(t *testing.T) { testNaming(t) } +func Test6390(t *testing.T) { test6390(t) } +func Test6833(t *testing.T) { test6833(t) } +func Test6907(t *testing.T) { test6907(t) } +func Test6907Go(t *testing.T) { test6907Go(t) } func Test7560(t *testing.T) { test7560(t) } -func Test5242(t *testing.T) { test5242(t) } -func Test8092(t *testing.T) { test8092(t) } +func Test7665(t *testing.T) { test7665(t) } func Test7978(t *testing.T) { test7978(t) } -func Test8694(t *testing.T) { test8694(t) } +func Test8092(t *testing.T) { test8092(t) } func Test8517(t *testing.T) { test8517(t) } +func Test8694(t *testing.T) { test8694(t) } func Test8811(t *testing.T) { test8811(t) } -func TestReturnAfterGrow(t *testing.T) { testReturnAfterGrow(t) } -func TestReturnAfterGrowFromGo(t *testing.T) { testReturnAfterGrowFromGo(t) } -func Test9026(t *testing.T) { test9026(t) } -func Test9510(t *testing.T) { test9510(t) } func Test9557(t *testing.T) { test9557(t) } func Test10303(t *testing.T) { test10303(t, 10) } func Test11925(t *testing.T) { test11925(t) } func Test12030(t *testing.T) { test12030(t) } -func TestGCC68255(t *testing.T) { testGCC68255(t) } -func TestCallGoWithString(t *testing.T) { testCallGoWithString(t) } func Test14838(t *testing.T) { test14838(t) } -func Test8756(t *testing.T) { test8756(t) } func Test17065(t *testing.T) { test17065(t) } -func TestThreadLock(t *testing.T) { testThreadLockFunc(t) } -func TestCheckConst(t *testing.T) { testCheckConst(t) } func Test17537(t *testing.T) { test17537(t) } func Test18126(t *testing.T) { test18126(t) } -func Test20369(t *testing.T) { test20369(t) } func Test18720(t *testing.T) { test18720(t) } -func Test20266(t *testing.T) { test20266(t) } func Test20129(t *testing.T) { test20129(t) } +func Test20369(t *testing.T) { test20369(t) } func Test20910(t *testing.T) { test20910(t) } func Test21708(t *testing.T) { test21708(t) } func Test21809(t *testing.T) { test21809(t) } -func Test6907(t *testing.T) { test6907(t) } -func Test6907Go(t *testing.T) { test6907Go(t) } func Test21897(t *testing.T) { test21897(t) } func Test22906(t *testing.T) { test22906(t) } +func Test23356(t *testing.T) { test23356(t) } func Test24206(t *testing.T) { test24206(t) } func Test25143(t *testing.T) { test25143(t) } -func Test23356(t *testing.T) { test23356(t) } func Test26066(t *testing.T) { test26066(t) } -func Test26213(t *testing.T) { test26213(t) } func Test27660(t *testing.T) { test27660(t) } func Test28896(t *testing.T) { test28896(t) } func Test30065(t *testing.T) { test30065(t) } +func Test32579(t *testing.T) { test32579(t) } +func TestAlign(t *testing.T) { testAlign(t) } +func TestAtol(t *testing.T) { testAtol(t) } +func TestBlocking(t *testing.T) { testBlocking(t) } +func TestBoolAlign(t *testing.T) { testBoolAlign(t) } +func TestCallGoWithString(t *testing.T) { testCallGoWithString(t) } +func TestCallback(t *testing.T) { testCallback(t) } +func TestCallbackCallers(t *testing.T) { testCallbackCallers(t) } +func TestCallbackGC(t *testing.T) { testCallbackGC(t) } +func TestCallbackPanic(t *testing.T) { testCallbackPanic(t) } +func TestCallbackPanicLocked(t *testing.T) { testCallbackPanicLocked(t) } +func TestCallbackPanicLoop(t *testing.T) { testCallbackPanicLoop(t) } +func TestCallbackStack(t *testing.T) { testCallbackStack(t) } +func TestCflags(t *testing.T) { testCflags(t) } +func TestCheckConst(t *testing.T) { testCheckConst(t) } +func TestConst(t *testing.T) { testConst(t) } +func TestCthread(t *testing.T) { testCthread(t) } +func TestEnum(t *testing.T) { testEnum(t) } +func TestErrno(t *testing.T) { testErrno(t) } +func TestFpVar(t *testing.T) { testFpVar(t) } +func TestHelpers(t *testing.T) { testHelpers(t) } +func TestLibgcc(t *testing.T) { testLibgcc(t) } +func TestMultipleAssign(t *testing.T) { testMultipleAssign(t) } +func TestNaming(t *testing.T) { testNaming(t) } +func TestPanicFromC(t *testing.T) { testPanicFromC(t) } +func TestParallelSleep(t *testing.T) { testParallelSleep(t) } +func TestPrintf(t *testing.T) { testPrintf(t) } +func TestReturnAfterGrow(t *testing.T) { testReturnAfterGrow(t) } +func TestReturnAfterGrowFromGo(t *testing.T) { testReturnAfterGrowFromGo(t) } +func TestSetEnv(t *testing.T) { testSetEnv(t) } +func TestThreadLock(t *testing.T) { testThreadLockFunc(t) } +func TestUnsignedInt(t *testing.T) { testUnsignedInt(t) } +func TestZeroArgCallback(t *testing.T) { testZeroArgCallback(t) } func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) } func BenchmarkGoString(b *testing.B) { benchGoString(b) } diff --git a/libgo/misc/cgo/test/checkconst.go b/libgo/misc/cgo/test/checkconst.go deleted file mode 100644 index 0160c1e..0000000 --- a/libgo/misc/cgo/test/checkconst.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2016 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 a constant in conjunction with pointer checking. - -package cgotest - -/* -#include <stdlib.h> - -#define CheckConstVal 0 - -typedef struct { - int *p; -} CheckConstStruct; - -static void CheckConstFunc(CheckConstStruct *p, int e) { -} -*/ -import "C" - -import ( - "testing" - "unsafe" -) - -func testCheckConst(t *testing.T) { - // The test is that this compiles successfully. - p := C.malloc(C.size_t(unsafe.Sizeof(C.int(0)))) - defer C.free(p) - C.CheckConstFunc(&C.CheckConstStruct{(*C.int)(p)}, C.CheckConstVal) -} diff --git a/libgo/misc/cgo/test/complex.go b/libgo/misc/cgo/test/complex.go deleted file mode 100644 index ca0a97d..0000000 --- a/libgo/misc/cgo/test/complex.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2016 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 - -/* -struct { - float x; - _Complex float y; -} cplxAlign = { 3.14, 2.17 }; -*/ -import "C" - -import "testing" - -func TestComplexAlign(t *testing.T) { - if C.cplxAlign.x != 3.14 { - t.Errorf("got %v, expected 3.14", C.cplxAlign.x) - } - if C.cplxAlign.y != 2.17 { - t.Errorf("got %v, expected 2.17", C.cplxAlign.y) - } -} diff --git a/libgo/misc/cgo/test/cthread.go b/libgo/misc/cgo/test/cthread.go deleted file mode 100644 index af44911..0000000 --- a/libgo/misc/cgo/test/cthread.go +++ /dev/null @@ -1,44 +0,0 @@ -// 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.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { - t.Skip("the iOS exec wrapper is unable to properly handle the panic from Add") - } - sum.i = 0 - 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/libgo/misc/cgo/test/duplicate_symbol.go b/libgo/misc/cgo/test/duplicate_symbol.go deleted file mode 100644 index 6144271..0000000 --- a/libgo/misc/cgo/test/duplicate_symbol.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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. - -// This file contains test cases for cgo. - -package cgotest - -/* -int base_symbol = 0; - -#define alias_one base_symbol -#define alias_two base_symbol -*/ -import "C" - -import "fmt" - -func duplicateSymbols() { - fmt.Printf("%v %v %v\n", C.base_symbol, C.alias_one, C.alias_two) -} diff --git a/libgo/misc/cgo/test/env.go b/libgo/misc/cgo/test/env.go deleted file mode 100644 index b2081b7..0000000 --- a/libgo/misc/cgo/test/env.go +++ /dev/null @@ -1,41 +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. - -package cgotest - -/* -#include <stdlib.h> -*/ -import "C" -import ( - "os" - "runtime" - "testing" - "unsafe" -) - -// This is really an os package test but here for convenience. -func testSetEnv(t *testing.T) { - if runtime.GOOS == "windows" { - // Go uses SetEnvironmentVariable on windows. Howerver, - // C runtime takes a *copy* at process startup of thei - // OS environment, and stores it in environ/envp. - // It is this copy that getenv/putenv manipulate. - t.Logf("skipping test") - return - } - const key = "CGO_OS_TEST_KEY" - const val = "CGO_OS_TEST_VALUE" - os.Setenv(key, val) - keyc := C.CString(key) - defer C.free(unsafe.Pointer(keyc)) - v := C.getenv(keyc) - if uintptr(unsafe.Pointer(v)) == 0 { - t.Fatal("getenv returned NULL") - } - vs := C.GoString(v) - if vs != val { - t.Fatalf("getenv() = %q; want %q", vs, val) - } -} diff --git a/libgo/misc/cgo/test/exports.go b/libgo/misc/cgo/test/exports.go deleted file mode 100644 index 71e5dcd..0000000 --- a/libgo/misc/cgo/test/exports.go +++ /dev/null @@ -1,18 +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. - -package cgotest - -import "C" -import "runtime" - -//export ReturnIntLong -func ReturnIntLong() (int, C.long) { - return 1, 2 -} - -//export gc -func gc() { - runtime.GC() -} diff --git a/libgo/misc/cgo/test/fpvar.go b/libgo/misc/cgo/test/fpvar.go deleted file mode 100644 index 7aab8ca..0000000 --- a/libgo/misc/cgo/test/fpvar.go +++ /dev/null @@ -1,50 +0,0 @@ -// 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. - -// This file contains test cases for cgo with function pointer variables. - -package cgotest - -/* -typedef int (*intFunc) (); - -int -bridge_int_func(intFunc f) -{ - return f(); -} - -int fortytwo() -{ - return 42; -} - -*/ -import "C" -import "testing" - -func callBridge(f C.intFunc) int { - return int(C.bridge_int_func(f)) -} - -func callCBridge(f C.intFunc) C.int { - return C.bridge_int_func(f) -} - -func testFpVar(t *testing.T) { - const expected = 42 - f := C.intFunc(C.fortytwo) - res1 := C.bridge_int_func(f) - if r1 := int(res1); r1 != expected { - t.Errorf("got %d, want %d", r1, expected) - } - res2 := callCBridge(f) - if r2 := int(res2); r2 != expected { - t.Errorf("got %d, want %d", r2, expected) - } - r3 := callBridge(f) - if r3 != expected { - t.Errorf("got %d, want %d", r3, expected) - } -} diff --git a/libgo/misc/cgo/test/helpers.go b/libgo/misc/cgo/test/helpers.go deleted file mode 100644 index f6a822a..0000000 --- a/libgo/misc/cgo/test/helpers.go +++ /dev/null @@ -1,35 +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. - -package cgotest - -// const char *greeting = "hello, world"; -import "C" - -import ( - "reflect" - "testing" - "unsafe" -) - -const greeting = "hello, world" - -type testPair struct { - Name string - Got, Want interface{} -} - -var testPairs = []testPair{ - {"GoString", C.GoString(C.greeting), greeting}, - {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]}, - {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])}, -} - -func testHelpers(t *testing.T) { - for _, pair := range testPairs { - if !reflect.DeepEqual(pair.Got, pair.Want) { - t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want) - } - } -} diff --git a/libgo/misc/cgo/test/issue10303.go b/libgo/misc/cgo/test/issue10303.go deleted file mode 100644 index 66e2644..0000000 --- a/libgo/misc/cgo/test/issue10303.go +++ /dev/null @@ -1,76 +0,0 @@ -// 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. - -// Issue 10303. Pointers passed to C were not marked as escaping (bug in cgo). - -package cgotest - -import "runtime" - -/* -typedef int *intptr; - -void setintstar(int *x) { - *x = 1; -} - -void setintptr(intptr x) { - *x = 1; -} - -void setvoidptr(void *x) { - *(int*)x = 1; -} - -typedef struct Struct Struct; -struct Struct { - int *P; -}; - -void setstruct(Struct s) { - *s.P = 1; -} - -*/ -import "C" - -import ( - "testing" - "unsafe" -) - -func test10303(t *testing.T, n int) { - if runtime.Compiler == "gccgo" { - t.Skip("gccgo permits C pointers on the stack") - } - - // Run at a few different stack depths just to avoid an unlucky pass - // due to variables ending up on different pages. - if n > 0 { - test10303(t, n-1) - } - if t.Failed() { - return - } - var x, y, z, v, si C.int - var s C.Struct - C.setintstar(&x) - C.setintptr(&y) - C.setvoidptr(unsafe.Pointer(&v)) - s.P = &si - C.setstruct(s) - - if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { - t.Error("C int* argument on stack") - } - if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { - t.Error("C intptr argument on stack") - } - if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { - t.Error("C void* argument on stack") - } - if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { - t.Error("C struct field pointer on stack") - } -} diff --git a/libgo/misc/cgo/test/issue11925.go b/libgo/misc/cgo/test/issue11925.go deleted file mode 100644 index c5c8a26..0000000 --- a/libgo/misc/cgo/test/issue11925.go +++ /dev/null @@ -1,37 +0,0 @@ -// 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. - -// Issue 11925. Structs with zero-length trailing fields are now -// padded by the Go compiler. - -package cgotest - -/* -struct a11925 { - int i; - char a[0]; - char b[0]; -}; - -struct b11925 { - int i; - char a[0]; - char b[]; -}; -*/ -import "C" - -import ( - "testing" - "unsafe" -) - -func test11925(t *testing.T) { - if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) { - t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{})) - } - if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) { - t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{})) - } -} diff --git a/libgo/misc/cgo/test/issue12030.go b/libgo/misc/cgo/test/issue12030.go deleted file mode 100644 index f863c58..0000000 --- a/libgo/misc/cgo/test/issue12030.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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. - -// Issue 12030. sprintf is defined in both ntdll and msvcrt, -// Normally we want the one in the msvcrt. - -package cgotest - -/* -#include <stdio.h> -#include <stdlib.h> -void issue12030conv(char *buf, double x) { - sprintf(buf, "d=%g", x); -} -*/ -import "C" - -import ( - "fmt" - "testing" - "unsafe" -) - -func test12030(t *testing.T) { - buf := (*C.char)(C.malloc(256)) - defer C.free(unsafe.Pointer(buf)) - for _, f := range []float64{1.0, 2.0, 3.14} { - C.issue12030conv(buf, C.double(f)) - got := C.GoString(buf) - if want := fmt.Sprintf("d=%g", f); got != want { - t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want) - } - } -} diff --git a/libgo/misc/cgo/test/issue1222.go b/libgo/misc/cgo/test/issue1222.go deleted file mode 100644 index 4868da8..0000000 --- a/libgo/misc/cgo/test/issue1222.go +++ /dev/null @@ -1,29 +0,0 @@ -// 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. - -// This file contains test cases for cgo. - -package cgotest - -/* -// issue 1222 -typedef union { - long align; -} xxpthread_mutex_t; - -struct ibv_async_event { - union { - int x; - } element; -}; - -struct ibv_context { - xxpthread_mutex_t mutex; -}; -*/ -import "C" - -type AsyncEvent struct { - event C.struct_ibv_async_event -} diff --git a/libgo/misc/cgo/test/issue1328.go b/libgo/misc/cgo/test/issue1328.go deleted file mode 100644 index 2401c10..0000000 --- a/libgo/misc/cgo/test/issue1328.go +++ /dev/null @@ -1,30 +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. - -package cgotest - -import "testing" - -// extern void BackIntoGo(void); -// void IntoC(void); -import "C" - -//export BackIntoGo -func BackIntoGo() { - x := 1 - - for i := 0; i < 10000; i++ { - xvariadic(x) - if x != 1 { - panic("x is not 1?") - } - } -} - -func xvariadic(x ...interface{}) { -} - -func test1328(t *testing.T) { - C.IntoC() -} diff --git a/libgo/misc/cgo/test/issue13930.go b/libgo/misc/cgo/test/issue13930.go deleted file mode 100644 index c4a08ee..0000000 --- a/libgo/misc/cgo/test/issue13930.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2016 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 13930. Test that cgo's multiple-value special form for -// C function calls works in variable declaration statements. - -package cgotest - -// #include <stdlib.h> -import "C" - -var _, _ = C.abs(0) diff --git a/libgo/misc/cgo/test/issue14838.go b/libgo/misc/cgo/test/issue14838.go deleted file mode 100644 index c8e1681..0000000 --- a/libgo/misc/cgo/test/issue14838.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2016 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 14838. add CBytes function - -package cgotest - -/* -#include <stdlib.h> - -int check_cbytes(char *b, size_t l) { - int i; - for (i = 0; i < l; i++) { - if (b[i] != i) { - return 0; - } - } - return 1; -} -*/ -import "C" - -import ( - "testing" - "unsafe" -) - -func test14838(t *testing.T) { - data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - cData := C.CBytes(data) - defer C.free(cData) - - if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 { - t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:]) - } -} diff --git a/libgo/misc/cgo/test/issue1560.go b/libgo/misc/cgo/test/issue1560.go deleted file mode 100644 index 30f6152..0000000 --- a/libgo/misc/cgo/test/issue1560.go +++ /dev/null @@ -1,50 +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. - -package cgotest - -/* -// mysleep returns the absolute start time in ms. -long long mysleep(int seconds); - -// twoSleep returns the absolute start time of the first sleep -// in ms. -long long twoSleep(int); -*/ -import "C" - -import ( - "testing" - "time" -) - -var sleepDone = make(chan int64) - -// parallelSleep returns the absolute difference between the start time -// of the two sleeps. -func parallelSleep(n int) int64 { - t := int64(C.twoSleep(C.int(n))) - <-sleepDone - if t < 0 { - return -t - } - return t -} - -//export BackgroundSleep -func BackgroundSleep(n int32) { - go func() { - sleepDone <- int64(C.mysleep(C.int(n))) - }() -} - -func testParallelSleep(t *testing.T) { - sleepSec := 1 - dt := time.Duration(parallelSleep(sleepSec)) * time.Millisecond - t.Logf("difference in start time for two sleep(%d) is %v", sleepSec, dt) - // bug used to run sleeps in serial, producing a 2*sleepSec-second delay. - // we detect if the start times of those sleeps are > 0.5*sleepSec-second. - if dt >= time.Duration(sleepSec)*time.Second/2 { - t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds()) - } -} diff --git a/libgo/misc/cgo/test/issue1635.go b/libgo/misc/cgo/test/issue1635.go deleted file mode 100644 index 2589927..0000000 --- a/libgo/misc/cgo/test/issue1635.go +++ /dev/null @@ -1,38 +0,0 @@ -// 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); -} - -// Adding this explicit extern declaration makes this a test for -// https://gcc.gnu.org/PR68072 aka https://golang.org/issue/13344 . -// It used to cause a cgo error when building with GCC 6. -extern int hola; - -// 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/libgo/misc/cgo/test/issue17065.go b/libgo/misc/cgo/test/issue17065.go deleted file mode 100644 index ede30bc..0000000 --- a/libgo/misc/cgo/test/issue17065.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2016 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 C symbols larger than a page play nicely with the race detector. -// See issue 17065. - -int ii[65537]; -*/ -import "C" - -import ( - "runtime" - "testing" -) - -var sink C.int - -func test17065(t *testing.T) { - if runtime.GOOS == "darwin" { - t.Skip("broken on darwin; issue 17065") - } - for i := range C.ii { - sink = C.ii[i] - } -} diff --git a/libgo/misc/cgo/test/issue17537.go b/libgo/misc/cgo/test/issue17537.go deleted file mode 100644 index 777104e..0000000 --- a/libgo/misc/cgo/test/issue17537.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2016 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 17537. The void* cast introduced by cgo to avoid problems -// with const/volatile qualifiers breaks C preprocessor macros that -// emulate functions. - -package cgotest - -/* -#include <stdlib.h> - -typedef struct { - int i; -} S17537; - -int I17537(S17537 *p); - -#define I17537(p) ((p)->i) - -// Calling this function used to fail without the cast. -const int F17537(const char **p) { - return **p; -} - -// Calling this function used to trigger an error from the C compiler -// (issue 18298). -void F18298(const void *const *p) { -} - -// Test that conversions between typedefs work as they used to. -typedef const void *T18298_1; -struct S18298 { int i; }; -typedef const struct S18298 *T18298_2; -void G18298(T18298_1 t) { -} -*/ -import "C" - -import "testing" - -func test17537(t *testing.T) { - v := C.S17537{i: 17537} - if got, want := C.I17537(&v), C.int(17537); got != want { - t.Errorf("got %d, want %d", got, want) - } - - p := (*C.char)(C.malloc(1)) - *p = 17 - if got, want := C.F17537(&p), C.int(17); got != want { - t.Errorf("got %d, want %d", got, want) - } - - C.F18298(nil) - var v18298 C.T18298_2 - C.G18298(C.T18298_1(v18298)) -} diff --git a/libgo/misc/cgo/test/issue18126.go b/libgo/misc/cgo/test/issue18126.go deleted file mode 100644 index ac94a66..0000000 --- a/libgo/misc/cgo/test/issue18126.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2016 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 18126: cgo check of void function returning errno. - -package cgotest - -/* -#include <stdlib.h> - -void Issue18126C(void **p) { -} -*/ -import "C" - -import ( - "testing" -) - -func test18126(t *testing.T) { - p := C.malloc(1) - _, err := C.Issue18126C(&p) - C.free(p) - _ = err -} diff --git a/libgo/misc/cgo/test/issue18146.go b/libgo/misc/cgo/test/issue18146.go index 8b7bb77..196d98f 100644 --- a/libgo/misc/cgo/test/issue18146.go +++ b/libgo/misc/cgo/test/issue18146.go @@ -8,8 +8,6 @@ package cgotest -import "C" - import ( "bytes" "crypto/md5" @@ -46,12 +44,12 @@ func test18146(t *testing.T) { switch runtime.GOOS { default: setNproc = false + case "aix": + nproc = 9 case "linux": nproc = 6 case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd": nproc = 7 - case "aix": - nproc = 9 } if setNproc { var rlim syscall.Rlimit diff --git a/libgo/misc/cgo/test/issue18720.go b/libgo/misc/cgo/test/issue18720.go deleted file mode 100644 index 3d64003..0000000 --- a/libgo/misc/cgo/test/issue18720.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2017 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 - -/* -#define HELLO "hello" -#define WORLD "world" -#define HELLO_WORLD HELLO "\000" WORLD - -struct foo { char c; }; -#define SIZE_OF(x) sizeof(x) -#define SIZE_OF_FOO SIZE_OF(struct foo) -#define VAR1 VAR -#define VAR var -int var = 5; - -#define ADDR &var - -#define CALL fn() -int fn(void) { - return ++var; -} -*/ -import "C" -import "testing" - -func test18720(t *testing.T) { - if got, want := C.HELLO_WORLD, "hello\000world"; got != want { - t.Errorf("C.HELLO_WORLD == %q, expected %q", got, want) - } - - if got, want := C.VAR1, C.int(5); got != want { - t.Errorf("C.VAR1 == %v, expected %v", got, want) - } - - if got, want := *C.ADDR, C.int(5); got != want { - t.Errorf("*C.ADDR == %v, expected %v", got, want) - } - - if got, want := C.CALL, C.int(6); got != want { - t.Errorf("C.CALL == %v, expected %v", got, want) - } - - if got, want := C.CALL, C.int(7); got != want { - t.Errorf("C.CALL == %v, expected %v", got, want) - } - - // Issue 20125. - if got, want := C.SIZE_OF_FOO, 1; got != want { - t.Errorf("C.SIZE_OF_FOO == %v, expected %v", got, want) - } -} diff --git a/libgo/misc/cgo/test/issue20129.go b/libgo/misc/cgo/test/issue20129.go deleted file mode 100644 index e69e0e1..0000000 --- a/libgo/misc/cgo/test/issue20129.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017 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 - -/* -int issue20129 = 0; -typedef void issue20129Void; -issue20129Void issue20129Foo() { - issue20129 = 1; -} -typedef issue20129Void issue20129Void2; -issue20129Void2 issue20129Bar() { - issue20129 = 2; -} -*/ -import "C" -import "testing" - -func test20129(t *testing.T) { - if C.issue20129 != 0 { - t.Fatal("test is broken") - } - C.issue20129Foo() - if C.issue20129 != 1 { - t.Errorf("got %v but expected %v", C.issue20129, 1) - } - C.issue20129Bar() - if C.issue20129 != 2 { - t.Errorf("got %v but expected %v", C.issue20129, 2) - } -} diff --git a/libgo/misc/cgo/test/issue20369.go b/libgo/misc/cgo/test/issue20369.go deleted file mode 100644 index 37b4b78..0000000 --- a/libgo/misc/cgo/test/issue20369.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017 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 - -/* -#define UINT64_MAX 18446744073709551615ULL -*/ -import "C" -import ( - "math" - "testing" -) - -func test20369(t *testing.T) { - if C.UINT64_MAX != math.MaxUint64 { - t.Fatalf("got %v, want %v", uint64(C.UINT64_MAX), uint64(math.MaxUint64)) - } -} diff --git a/libgo/misc/cgo/test/issue20910.go b/libgo/misc/cgo/test/issue20910.go deleted file mode 100644 index 69d7d92..0000000 --- a/libgo/misc/cgo/test/issue20910.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017 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 callMulti(void); -import "C" - -import "testing" - -//export multi -func multi() (*C.char, C.int) { - return C.CString("multi"), 0 -} - -func test20910(t *testing.T) { - C.callMulti() -} diff --git a/libgo/misc/cgo/test/issue21708.go b/libgo/misc/cgo/test/issue21708.go deleted file mode 100644 index d413e3c5..0000000 --- a/libgo/misc/cgo/test/issue21708.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017 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 - -// #include <stdint.h> -// #define CAST_TO_INT64 (int64_t)(-1) -import "C" -import "testing" - -func test21708(t *testing.T) { - if got, want := C.CAST_TO_INT64, -1; got != want { - t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want) - } -} diff --git a/libgo/misc/cgo/test/issue21809.go b/libgo/misc/cgo/test/issue21809.go deleted file mode 100644 index a3a6b88..0000000 --- a/libgo/misc/cgo/test/issue21809.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2017 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 - -// Issue 21809. Compile C `typedef` to go type aliases. - -// typedef long MySigned_t; -// /* tests alias-to-alias */ -// typedef MySigned_t MySigned2_t; -// -// long takes_long(long x) { return x * x; } -// MySigned_t takes_typedef(MySigned_t x) { return x * x; } -import "C" - -import "testing" - -func test21809(t *testing.T) { - longVar := C.long(3) - typedefVar := C.MySigned_t(4) - typedefTypedefVar := C.MySigned2_t(5) - - // all three should be considered identical to `long` - if ret := C.takes_long(longVar); ret != 9 { - t.Errorf("got %v but expected %v", ret, 9) - } - if ret := C.takes_long(typedefVar); ret != 16 { - t.Errorf("got %v but expected %v", ret, 16) - } - if ret := C.takes_long(typedefTypedefVar); ret != 25 { - t.Errorf("got %v but expected %v", ret, 25) - } - - // They should also be identical to the typedef'd type - if ret := C.takes_typedef(longVar); ret != 9 { - t.Errorf("got %v but expected %v", ret, 9) - } - if ret := C.takes_typedef(typedefVar); ret != 16 { - t.Errorf("got %v but expected %v", ret, 16) - } - if ret := C.takes_typedef(typedefTypedefVar); ret != 25 { - t.Errorf("got %v but expected %v", ret, 25) - } -} diff --git a/libgo/misc/cgo/test/issue22958.go b/libgo/misc/cgo/test/issue22958.go deleted file mode 100644 index a5f058f..0000000 --- a/libgo/misc/cgo/test/issue22958.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017 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 handling of bitfields. - -/* -typedef struct { - unsigned long long f8 : 8; - unsigned long long f16 : 16; - unsigned long long f24 : 24; - unsigned long long f32 : 32; - unsigned long long f40 : 40; - unsigned long long f48 : 48; - unsigned long long f56 : 56; - unsigned long long f64 : 64; -} issue22958Type; -*/ -import "C" - -// Nothing to run, just make sure this compiles. -var Vissue22958 C.issue22958Type diff --git a/libgo/misc/cgo/test/issue23356.go b/libgo/misc/cgo/test/issue23356.go deleted file mode 100644 index 1c39012..0000000 --- a/libgo/misc/cgo/test/issue23356.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2018 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 - -// int a(void) { return 5; }; -// int r(void) { return 3; }; -import "C" -import "testing" - -func test23356(t *testing.T) { - if got, want := C.a(), C.int(5); got != want { - t.Errorf("C.a() == %v, expected %v", got, want) - } - if got, want := C.r(), C.int(3); got != want { - t.Errorf("C.r() == %v, expected %v", got, want) - } -} diff --git a/libgo/misc/cgo/test/issue23720.go b/libgo/misc/cgo/test/issue23720.go deleted file mode 100644 index 934fff3..0000000 --- a/libgo/misc/cgo/test/issue23720.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018 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 we can pass compatible typedefs. -// No runtime test; just make sure it compiles. - -package cgotest - -/* -typedef int *issue23720A; - -typedef const int *issue23720B; - -void issue23720F(issue23720B a) {} -*/ -import "C" - -func Issue23720F() { - var x C.issue23720A - C.issue23720F(x) -} diff --git a/libgo/misc/cgo/test/issue24206.go b/libgo/misc/cgo/test/issue24206.go deleted file mode 100644 index 5fec68e..0000000 --- a/libgo/misc/cgo/test/issue24206.go +++ /dev/null @@ -1,54 +0,0 @@ -// +build amd64,linux - -// Copyright 2018 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 C.GoString uses IndexByte in safe manner. - -/* -#include <sys/mman.h> - -// Returns string with null byte at the last valid address -char* dangerousString1() { - int pageSize = 4096; - char *data = mmap(0, 2 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0); - mprotect(data + pageSize,pageSize,PROT_NONE); - int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte - int i = start; - for (; i < pageSize; i++) { - data[i] = 'x'; - } - data[pageSize -1 ] = 0; - return data+start; -} - -char* dangerousString2() { - int pageSize = 4096; - char *data = mmap(0, 3 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0); - mprotect(data + 2 * pageSize,pageSize,PROT_NONE); - int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte - int i = start; - for (; i < 2 * pageSize; i++) { - data[i] = 'x'; - } - data[2*pageSize -1 ] = 0; - return data+start; -} -*/ -import "C" - -import ( - "testing" -) - -func test24206(t *testing.T) { - if l := len(C.GoString(C.dangerousString1())); l != 123 { - t.Errorf("Incorrect string length - got %d, want 123", l) - } - if l := len(C.GoString(C.dangerousString2())); l != 4096+123 { - t.Errorf("Incorrect string length - got %d, want %d", l, 4096+123) - } -} diff --git a/libgo/misc/cgo/test/issue24206_generic.go b/libgo/misc/cgo/test/issue24206_generic.go deleted file mode 100644 index 27c4d65..0000000 --- a/libgo/misc/cgo/test/issue24206_generic.go +++ /dev/null @@ -1,13 +0,0 @@ -// +build !amd64 !linux - -// Copyright 2018 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 test24206(t *testing.T) { - t.Skip("Skipping on non-amd64 or non-linux system") -} diff --git a/libgo/misc/cgo/test/issue2462.go b/libgo/misc/cgo/test/issue2462.go deleted file mode 100644 index febca1e..0000000 --- a/libgo/misc/cgo/test/issue2462.go +++ /dev/null @@ -1,102 +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. - -package cgotest - -import "C" - -//export exportbyte -func exportbyte() byte { - return 0 -} - -//export exportbool -func exportbool() bool { - return false -} - -//export exportrune -func exportrune() rune { - return 0 -} - -//export exporterror -func exporterror() error { - return nil -} - -//export exportint -func exportint() int { - return 0 -} - -//export exportuint -func exportuint() uint { - return 0 -} - -//export exportuintptr -func exportuintptr() uintptr { - return (uintptr)(0) -} - -//export exportint8 -func exportint8() int8 { - return 0 -} - -//export exportuint8 -func exportuint8() uint8 { - return 0 -} - -//export exportint16 -func exportint16() int16 { - return 0 -} - -//export exportuint16 -func exportuint16() uint16 { - return 0 -} - -//export exportint32 -func exportint32() int32 { - return 0 -} - -//export exportuint32 -func exportuint32() uint32 { - return 0 -} - -//export exportint64 -func exportint64() int64 { - return 0 -} - -//export exportuint64 -func exportuint64() uint64 { - return 0 -} - -//export exportfloat32 -func exportfloat32() float32 { - return 0 -} - -//export exportfloat64 -func exportfloat64() float64 { - return 0 -} - -//export exportcomplex64 -func exportcomplex64() complex64 { - return 0 -} - -//export exportcomplex128 -func exportcomplex128() complex128 { - return 0 -} diff --git a/libgo/misc/cgo/test/issue25143.go b/libgo/misc/cgo/test/issue25143.go deleted file mode 100644 index 607bfe4..0000000 --- a/libgo/misc/cgo/test/issue25143.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018 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" -import "testing" - -func issue25143sum(ns ...C.int) C.int { - total := C.int(0) - for _, n := range ns { - total += n - } - return total -} - -func test25143(t *testing.T) { - if got, want := issue25143sum(1, 2, 3), C.int(6); got != want { - t.Errorf("issue25143sum(1, 2, 3) == %v, expected %v", got, want) - } -} diff --git a/libgo/misc/cgo/test/issue26066.go b/libgo/misc/cgo/test/issue26066.go deleted file mode 100644 index 21028e7..0000000 --- a/libgo/misc/cgo/test/issue26066.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2018 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. - -// Wrong type of constant with GCC 8 and newer. - -package cgotest - -// const unsigned long long int issue26066 = (const unsigned long long) -1; -import "C" - -import "testing" - -func test26066(t *testing.T) { - var i = int64(C.issue26066) - if i != -1 { - t.Errorf("got %d, want -1", i) - } -} diff --git a/libgo/misc/cgo/test/issue26517.go b/libgo/misc/cgo/test/issue26517.go deleted file mode 100644 index c1bf1c9..0000000 --- a/libgo/misc/cgo/test/issue26517.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2018 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 - -// Introduce two pointer types which are distinct, but have the same -// base type. Make sure that both of those pointer types get resolved -// correctly. Before the fix for 26517 if one of these pointer types -// was resolved before the other one was processed, the second one -// would never be resolved. -// Before this issue was fixed this test failed on Windows, -// where va_list expands to a named char* type. - -/* -#include <stdarg.h> -typedef va_list TypeOne; -typedef char *TypeTwo; -*/ -import "C" - -var a C.TypeOne -var b C.TypeTwo diff --git a/libgo/misc/cgo/test/issue28545.go b/libgo/misc/cgo/test/issue28545.go deleted file mode 100644 index 8419b89..0000000 --- a/libgo/misc/cgo/test/issue28545.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 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. - -// Failed to add type conversion for negative constant. -// Issue 28772: Failed to add type conversion for Go constant set to C constant. -// No runtime test; just make sure it compiles. - -package cgotest - -/* -#include <complex.h> - -#define issue28772Constant 1 - -static void issue28545F(char **p, int n, complex double a) {} -*/ -import "C" - -const issue28772Constant = C.issue28772Constant - -func issue28545G(p **C.char) { - C.issue28545F(p, -1, (0)) - C.issue28545F(p, 2+3, complex(1, 1)) - C.issue28545F(p, issue28772Constant, issue28772Constant2) -} diff --git a/libgo/misc/cgo/test/issue28772.go b/libgo/misc/cgo/test/issue28772.go deleted file mode 100644 index bed786b..0000000 --- a/libgo/misc/cgo/test/issue28772.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2018 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 - -// Constants didn't work if defined in different source file. - -// #define issue28772Constant2 2 -import "C" - -const issue28772Constant2 = C.issue28772Constant2 diff --git a/libgo/misc/cgo/test/issue28896.go b/libgo/misc/cgo/test/issue28896.go deleted file mode 100644 index 8796040f..0000000 --- a/libgo/misc/cgo/test/issue28896.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2018 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. - -// cgo was incorrectly adding padding after a packed struct. - -package cgotest - -/* -#include <stddef.h> -#include <stdint.h> -#include <stdlib.h> - -typedef struct { - void *f1; - uint32_t f2; -} __attribute__((__packed__)) innerPacked; - -typedef struct { - innerPacked g1; - uint64_t g2; -} outerPacked; - -typedef struct { - void *f1; - uint32_t f2; -} innerUnpacked; - -typedef struct { - innerUnpacked g1; - uint64_t g2; -} outerUnpacked; - -size_t offset(int x) { - switch (x) { - case 0: - return offsetof(innerPacked, f2); - case 1: - return offsetof(outerPacked, g2); - case 2: - return offsetof(innerUnpacked, f2); - case 3: - return offsetof(outerUnpacked, g2); - default: - abort(); - } -} -*/ -import "C" - -import ( - "testing" - "unsafe" -) - -func offset(i int) uintptr { - var pi C.innerPacked - var po C.outerPacked - var ui C.innerUnpacked - var uo C.outerUnpacked - switch i { - case 0: - return unsafe.Offsetof(pi.f2) - case 1: - return unsafe.Offsetof(po.g2) - case 2: - return unsafe.Offsetof(ui.f2) - case 3: - return unsafe.Offsetof(uo.g2) - default: - panic("can't happen") - } -} - -func test28896(t *testing.T) { - for i := 0; i < 4; i++ { - c := uintptr(C.offset(C.int(i))) - g := offset(i) - if c != g { - t.Errorf("%d: C: %d != Go %d", i, c, g) - } - } -} diff --git a/libgo/misc/cgo/test/issue29383.go b/libgo/misc/cgo/test/issue29383.go deleted file mode 100644 index 462c9a3..0000000 --- a/libgo/misc/cgo/test/issue29383.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2018 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. - -// cgo's /*line*/ comments failed when inserted after '/', -// because the result looked like a "//" comment. -// No runtime test; just make sure it compiles. - -package cgotest - -// #include <stddef.h> -import "C" - -func Issue29383(n, size uint) int { - if ^C.size_t(0)/C.size_t(n) < C.size_t(size) { - return 0 - } - return 0 -} diff --git a/libgo/misc/cgo/test/issue29748.go b/libgo/misc/cgo/test/issue29748.go deleted file mode 100644 index 8229b3b..0000000 --- a/libgo/misc/cgo/test/issue29748.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2019 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. - -// Error handling a struct initializer that requires pointer checking. -// Compilation test only, nothing to run. - -package cgotest - -// typedef struct { char **p; } S29748; -// static int f29748(S29748 *p) { return 0; } -import "C" - -var Vissue29748 = C.f29748(&C.S29748{ - nil, -}) - -func Fissue299748() { - C.f29748(&C.S29748{ - nil, - }) -} diff --git a/libgo/misc/cgo/test/issue29781.go b/libgo/misc/cgo/test/issue29781.go deleted file mode 100644 index 0fd8c08..0000000 --- a/libgo/misc/cgo/test/issue29781.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2019 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. - -// Error with newline inserted into constant expression. -// Compilation test only, nothing to run. - -package cgotest - -// static void issue29781F(char **p, int n) {} -// #define ISSUE29781C 0 -import "C" - -func issue29781G() { - var p *C.char - C.issue29781F(&p, C.ISSUE29781C+1) -} diff --git a/libgo/misc/cgo/test/issue30065.go b/libgo/misc/cgo/test/issue30065.go deleted file mode 100644 index 396d437..0000000 --- a/libgo/misc/cgo/test/issue30065.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2019 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. - -// Don't make a private copy of an array when taking the address of an -// element. - -package cgotest - -// #include <string.h> -import "C" - -import ( - "testing" - "unsafe" -) - -func test30065(t *testing.T) { - var a [256]byte - b := []byte("a") - C.memcpy(unsafe.Pointer(&a), unsafe.Pointer(&b[0]), 1) - if a[0] != 'a' { - t.Errorf("&a failed: got %c, want %c", a[0], 'a') - } - - b = []byte("b") - C.memcpy(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), 1) - if a[0] != 'b' { - t.Errorf("&a[0] failed: got %c, want %c", a[0], 'b') - } - - d := make([]byte, 256) - b = []byte("c") - C.memcpy(unsafe.Pointer(&d[0]), unsafe.Pointer(&b[0]), 1) - if d[0] != 'c' { - t.Errorf("&d[0] failed: got %c, want %c", d[0], 'c') - } -} diff --git a/libgo/misc/cgo/test/issue3250.go b/libgo/misc/cgo/test/issue3250.go deleted file mode 100644 index f85c16b..0000000 --- a/libgo/misc/cgo/test/issue3250.go +++ /dev/null @@ -1,95 +0,0 @@ -// 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 !windows - -package cgotest - -/* -#include <signal.h> -#include <pthread.h> -#include <unistd.h> -#include <stdlib.h> - -static void *thread(void *p) { - const int M = 100; - int i; - (void)p; - for (i = 0; i < M; i++) { - pthread_kill(pthread_self(), SIGCHLD); - usleep(rand() % 20 + 5); - } - return NULL; -} -void testSendSIG() { - const int N = 20; - int i; - pthread_t tid[N]; - for (i = 0; i < N; i++) { - usleep(rand() % 200 + 100); - pthread_create(&tid[i], 0, thread, NULL); - } - for (i = 0; i < N; i++) - pthread_join(tid[i], 0); -} -*/ -import "C" - -import ( - "os" - "os/signal" - "syscall" - "testing" - "time" -) - -func test3250(t *testing.T) { - t.Skip("skipped, see golang.org/issue/5885") - const ( - thres = 1 - sig = syscall.SIGCHLD - ) - type result struct { - n int - sig os.Signal - } - var ( - sigCh = make(chan os.Signal, 10) - waitStart = make(chan struct{}) - waitDone = make(chan result) - ) - - signal.Notify(sigCh, sig) - - go func() { - n := 0 - alarm := time.After(time.Second * 3) - for { - select { - case <-waitStart: - waitStart = nil - case v := <-sigCh: - n++ - if v != sig || n > thres { - waitDone <- result{n, v} - return - } - case <-alarm: - waitDone <- result{n, sig} - return - } - } - }() - - waitStart <- struct{}{} - C.testSendSIG() - r := <-waitDone - if r.sig != sig { - t.Fatalf("received signal %v, but want %v", r.sig, sig) - } - t.Logf("got %d signals\n", r.n) - if r.n <= thres { - t.Fatalf("expected more than %d", thres) - } -} diff --git a/libgo/misc/cgo/test/issue3250w.go b/libgo/misc/cgo/test/issue3250w.go deleted file mode 100644 index c2193aa..0000000 --- a/libgo/misc/cgo/test/issue3250w.go +++ /dev/null @@ -1,11 +0,0 @@ -// 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 windows - -package cgotest - -import "testing" - -func test3250(t *testing.T) {} diff --git a/libgo/misc/cgo/test/issue3261.go b/libgo/misc/cgo/test/issue3261.go deleted file mode 100644 index 7137569..0000000 --- a/libgo/misc/cgo/test/issue3261.go +++ /dev/null @@ -1,49 +0,0 @@ -// 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; -} -#elif defined(__arm64__) && defined(__clang__) -#include <stdio.h> -int vabs(int x) { - puts("testLibgcc is disabled on ARM64 with clang due to lack of libgcc."); - 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/libgo/misc/cgo/test/issue3729.go b/libgo/misc/cgo/test/issue3729.go deleted file mode 100644 index 947b90a..0000000 --- a/libgo/misc/cgo/test/issue3729.go +++ /dev/null @@ -1,47 +0,0 @@ -// 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/libgo/misc/cgo/test/issue3729w.go b/libgo/misc/cgo/test/issue3729w.go deleted file mode 100644 index 69296b5..0000000 --- a/libgo/misc/cgo/test/issue3729w.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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/libgo/misc/cgo/test/issue3741.go b/libgo/misc/cgo/test/issue3741.go deleted file mode 100644 index 314038c..0000000 --- a/libgo/misc/cgo/test/issue3741.go +++ /dev/null @@ -1,22 +0,0 @@ -// 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/libgo/misc/cgo/test/issue3775.go b/libgo/misc/cgo/test/issue3775.go deleted file mode 100644 index 5aca760..0000000 --- a/libgo/misc/cgo/test/issue3775.go +++ /dev/null @@ -1,39 +0,0 @@ -// +build !android - -package cgotest - -/* -void lockOSThreadCallback(void); -inline static void lockOSThreadC(void) -{ - lockOSThreadCallback(); -} -int usleep(unsigned usec); -*/ -import "C" - -import ( - "runtime" - "testing" -) - -func init() { - // Same as test3775 but run during init so that - // there are two levels of internal runtime lock - // (1 for init, 1 for cgo). - // This would have been broken by CL 11663043. - C.lockOSThreadC() -} - -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/libgo/misc/cgo/test/issue3945.go b/libgo/misc/cgo/test/issue3945.go deleted file mode 100644 index 2f9fe23..0000000 --- a/libgo/misc/cgo/test/issue3945.go +++ /dev/null @@ -1,22 +0,0 @@ -// 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 https://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/libgo/misc/cgo/test/issue4054a.go b/libgo/misc/cgo/test/issue4054a.go deleted file mode 100644 index 2abdac5..0000000 --- a/libgo/misc/cgo/test/issue4054a.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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/libgo/misc/cgo/test/issue4054b.go b/libgo/misc/cgo/test/issue4054b.go deleted file mode 100644 index 048964c..0000000 --- a/libgo/misc/cgo/test/issue4054b.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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/libgo/misc/cgo/test/issue4339.go b/libgo/misc/cgo/test/issue4339.go deleted file mode 100644 index 3715fde..0000000 --- a/libgo/misc/cgo/test/issue4339.go +++ /dev/null @@ -1,17 +0,0 @@ -// 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 - -/* -// We've historically permitted #include <>, so test it here. Issue 29333. -#include <issue4339.h> -*/ -import "C" - -import "testing" - -func test4339(t *testing.T) { - C.handle4339(&C.exported4339) -} diff --git a/libgo/misc/cgo/test/issue4417.go b/libgo/misc/cgo/test/issue4417.go deleted file mode 100644 index 9b18287..0000000 --- a/libgo/misc/cgo/test/issue4417.go +++ /dev/null @@ -1,42 +0,0 @@ -// 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/libgo/misc/cgo/test/issue4857.go b/libgo/misc/cgo/test/issue4857.go deleted file mode 100644 index b18979b..0000000 --- a/libgo/misc/cgo/test/issue4857.go +++ /dev/null @@ -1,15 +0,0 @@ -// 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 - -/* -#cgo CFLAGS: -Werror -const struct { int a; } *issue4857() { return (void *)0; } -*/ -import "C" - -func test4857() { - _ = C.issue4857() -} diff --git a/libgo/misc/cgo/test/issue5227.go b/libgo/misc/cgo/test/issue5227.go deleted file mode 100644 index 53c3bf1..0000000 --- a/libgo/misc/cgo/test/issue5227.go +++ /dev/null @@ -1,38 +0,0 @@ -// 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. - -// Issue 5227: linker incorrectly treats common symbols and -// leaves them undefined. - -package cgotest - -/* -typedef struct { - int Count; -} Fontinfo; - -Fontinfo SansTypeface; - -extern void init(); - -Fontinfo loadfont() { - Fontinfo f = {0}; - return f; -} - -void init() { - SansTypeface = loadfont(); -} -*/ -import "C" - -import "testing" - -func test5227(t *testing.T) { - C.init() -} - -func selectfont() C.Fontinfo { - return C.SansTypeface -} diff --git a/libgo/misc/cgo/test/issue5242.go b/libgo/misc/cgo/test/issue5242.go deleted file mode 100644 index c81cd40..0000000 --- a/libgo/misc/cgo/test/issue5242.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2014 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 5242. Cgo incorrectly computed the alignment of structs -// with no Go accessible fields as 0, and then panicked on -// modulo-by-zero computations. - -package cgotest - -/* -typedef struct { -} foo; - -typedef struct { - int x : 1; -} bar; - -int issue5242(foo f, bar b) { - return 5242; -} -*/ -import "C" - -import "testing" - -func test5242(t *testing.T) { - if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 { - t.Errorf("got %v", got) - } -} diff --git a/libgo/misc/cgo/test/issue5337.go b/libgo/misc/cgo/test/issue5337.go deleted file mode 100644 index 9041d95..0000000 --- a/libgo/misc/cgo/test/issue5337.go +++ /dev/null @@ -1,31 +0,0 @@ -// 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 !windows - -package cgotest - -/* -#include <signal.h> -#include <pthread.h> - -static void *thread1(void *p) { - (void)p; - pthread_kill(pthread_self(), SIGPROF); - return NULL; -} -void test5337() { - pthread_t tid; - pthread_create(&tid, 0, thread1, NULL); - pthread_join(tid, 0); -} -*/ -import "C" - -import "testing" - -// Verify that we can withstand SIGPROF received on foreign threads -func test5337(t *testing.T) { - C.test5337() -} diff --git a/libgo/misc/cgo/test/issue5548.go b/libgo/misc/cgo/test/issue5548.go deleted file mode 100644 index 0710da7..0000000 --- a/libgo/misc/cgo/test/issue5548.go +++ /dev/null @@ -1,27 +0,0 @@ -// 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 - -import "testing" - -/* -extern int issue5548_in_c(void); -*/ -import "C" - -//export issue5548FromC -func issue5548FromC(s string, i int) int { - if len(s) == 4 && s == "test" && i == 42 { - return 12345 - } - println("got", len(s), i) - return 9876 -} - -func test5548(t *testing.T) { - if x := C.issue5548_in_c(); x != 12345 { - t.Errorf("issue5548_in_c = %d, want %d", x, 12345) - } -} diff --git a/libgo/misc/cgo/test/issue5603.go b/libgo/misc/cgo/test/issue5603.go deleted file mode 100644 index ab84339..0000000 --- a/libgo/misc/cgo/test/issue5603.go +++ /dev/null @@ -1,32 +0,0 @@ -// 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 - -/* -const long long issue5603exp = 0x12345678; -long long issue5603foo0() { return issue5603exp; } -long long issue5603foo1(void *p) { return issue5603exp; } -long long issue5603foo2(void *p, void *q) { return issue5603exp; } -long long issue5603foo3(void *p, void *q, void *r) { return issue5603exp; } -long long issue5603foo4(void *p, void *q, void *r, void *s) { return issue5603exp; } -*/ -import "C" - -import "testing" - -func test5603(t *testing.T) { - var x [5]int64 - exp := int64(C.issue5603exp) - x[0] = int64(C.issue5603foo0()) - x[1] = int64(C.issue5603foo1(nil)) - x[2] = int64(C.issue5603foo2(nil, nil)) - x[3] = int64(C.issue5603foo3(nil, nil, nil)) - x[4] = int64(C.issue5603foo4(nil, nil, nil, nil)) - for i, v := range x { - if v != exp { - t.Errorf("issue5603foo%d() returns %v, expected %v", i, v, exp) - } - } -} diff --git a/libgo/misc/cgo/test/issue5740.go b/libgo/misc/cgo/test/issue5740.go deleted file mode 100644 index 059e316..0000000 --- a/libgo/misc/cgo/test/issue5740.go +++ /dev/null @@ -1,15 +0,0 @@ -// 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 - -// int test5740a(void), test5740b(void); -import "C" -import "testing" - -func test5740(t *testing.T) { - if v := C.test5740a() + C.test5740b(); v != 5 { - t.Errorf("expected 5, got %v", v) - } -} diff --git a/libgo/misc/cgo/test/issue5986.go b/libgo/misc/cgo/test/issue5986.go deleted file mode 100644 index 9be1614..0000000 --- a/libgo/misc/cgo/test/issue5986.go +++ /dev/null @@ -1,33 +0,0 @@ -// 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 - -/* -#cgo LDFLAGS: -lm -#include <stdio.h> -#include <math.h> - -static void output5986() -{ - int current_row = 0, row_count = 0; - double sum_squares = 0; - double d; - do { - if (current_row == 10) { - current_row = 0; - } - ++row_count; - } - while (current_row++ != 1); - d = sqrt(sum_squares / row_count); - printf("sqrt is: %g\n", d); -} -*/ -import "C" -import "testing" - -func test5986(t *testing.T) { - C.output5986() -} diff --git a/libgo/misc/cgo/test/issue6128.go b/libgo/misc/cgo/test/issue6128.go deleted file mode 100644 index 9832d79..0000000 --- a/libgo/misc/cgo/test/issue6128.go +++ /dev/null @@ -1,20 +0,0 @@ -// 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 - -// Test handling of #defined names in clang. -// golang.org/issue/6128. - -/* -// NOTE: Must use hex, or else a shortcut for decimals -// in cgo avoids trying to pass this to clang. -#define X 0x1 -*/ -import "C" - -func test6128() { - // nothing to run, just make sure this compiles. - _ = C.X -} diff --git a/libgo/misc/cgo/test/issue6390.go b/libgo/misc/cgo/test/issue6390.go deleted file mode 100644 index 5642899..0000000 --- a/libgo/misc/cgo/test/issue6390.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 - -// #include <stdlib.h> -import "C" - -import "testing" - -func test6390(t *testing.T) { - p1 := C.malloc(1024) - if p1 == nil { - t.Fatalf("C.malloc(1024) returned nil") - } - p2 := C.malloc(0) - if p2 == nil { - t.Fatalf("C.malloc(0) returned nil") - } - C.free(p1) - C.free(p2) -} diff --git a/libgo/misc/cgo/test/issue6472.go b/libgo/misc/cgo/test/issue6472.go deleted file mode 100644 index d416a05..0000000 --- a/libgo/misc/cgo/test/issue6472.go +++ /dev/null @@ -1,22 +0,0 @@ -// 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 - -/* -typedef struct -{ - struct - { - int x; - } y[16]; -} z; -*/ -import "C" - -func test6472() { - // nothing to run, just make sure this compiles - s := new(C.z) - println(s.y[0].x) -} diff --git a/libgo/misc/cgo/test/issue6506.go b/libgo/misc/cgo/test/issue6506.go deleted file mode 100644 index c54b54b..0000000 --- a/libgo/misc/cgo/test/issue6506.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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 - -// Test handling of size_t in the face of incorrect clang debug information. -// golang.org/issue/6506. - -/* -#include <stdlib.h> -#include <string.h> -*/ -import "C" - -func test6506() { - // nothing to run, just make sure this compiles - var x C.size_t - - C.calloc(x, x) - C.malloc(x) - C.realloc(nil, x) - C.memcpy(nil, nil, x) - C.memcmp(nil, nil, x) - C.memmove(nil, nil, x) - C.strncpy(nil, nil, x) - C.strncmp(nil, nil, x) - C.strncat(nil, nil, x) - x = C.strxfrm(nil, nil, x) - C.memchr(nil, 0, x) - x = C.strcspn(nil, nil) - x = C.strspn(nil, nil) - C.memset(nil, 0, x) - x = C.strlen(nil) - _ = x -} diff --git a/libgo/misc/cgo/test/issue6612.go b/libgo/misc/cgo/test/issue6612.go deleted file mode 100644 index 15a12fa..0000000 --- a/libgo/misc/cgo/test/issue6612.go +++ /dev/null @@ -1,90 +0,0 @@ -// 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. - -// golang.org/issue/6612 -// Test new scheme for deciding whether C.name is an expression, type, constant. -// Clang silences some warnings when the name is a #defined macro, so test those too -// (even though we now use errors exclusively, not warnings). - -package cgotest - -/* -void myfunc(void) {} -int myvar = 5; -const char *mytext = "abcdef"; -typedef int mytype; -enum { - myenum = 1234, -}; - -#define myfunc_def myfunc -#define myvar_def myvar -#define mytext_def mytext -#define mytype_def mytype -#define myenum_def myenum -#define myint_def 12345 -#define myfloat_def 1.5 -#define mystring_def "hello" -*/ -import "C" - -import "testing" - -func testNaming(t *testing.T) { - C.myfunc() - C.myfunc_def() - if v := C.myvar; v != 5 { - t.Errorf("C.myvar = %d, want 5", v) - } - if v := C.myvar_def; v != 5 { - t.Errorf("C.myvar_def = %d, want 5", v) - } - if s := C.GoString(C.mytext); s != "abcdef" { - t.Errorf("C.mytext = %q, want %q", s, "abcdef") - } - if s := C.GoString(C.mytext_def); s != "abcdef" { - t.Errorf("C.mytext_def = %q, want %q", s, "abcdef") - } - if c := C.myenum; c != 1234 { - t.Errorf("C.myenum = %v, want 1234", c) - } - if c := C.myenum_def; c != 1234 { - t.Errorf("C.myenum_def = %v, want 1234", c) - } - { - const c = C.myenum - if c != 1234 { - t.Errorf("C.myenum as const = %v, want 1234", c) - } - } - { - const c = C.myenum_def - if c != 1234 { - t.Errorf("C.myenum as const = %v, want 1234", c) - } - } - if c := C.myint_def; c != 12345 { - t.Errorf("C.myint_def = %v, want 12345", c) - } - { - const c = C.myint_def - if c != 12345 { - t.Errorf("C.myint as const = %v, want 12345", c) - } - } - - if c := C.myfloat_def; c != 1.5 { - t.Errorf("C.myint_def = %v, want 1.5", c) - } - { - const c = C.myfloat_def - if c != 1.5 { - t.Errorf("C.myint as const = %v, want 1.5", c) - } - } - - if s := C.mystring_def; s != "hello" { - t.Errorf("C.mystring_def = %q, want %q", s, "hello") - } -} diff --git a/libgo/misc/cgo/test/issue6833.go b/libgo/misc/cgo/test/issue6833.go deleted file mode 100644 index de60dbf..0000000 --- a/libgo/misc/cgo/test/issue6833.go +++ /dev/null @@ -1,27 +0,0 @@ -// 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 unsigned long long issue6833Func(unsigned int, unsigned long long); -*/ -import "C" - -import "testing" - -//export GoIssue6833Func -func GoIssue6833Func(aui uint, aui64 uint64) uint64 { - return aui64 + uint64(aui) -} - -func test6833(t *testing.T) { - ui := 7 - ull := uint64(0x4000300020001000) - v := uint64(C.issue6833Func(C.uint(ui), C.ulonglong(ull))) - exp := uint64(ui) + ull - if v != exp { - t.Errorf("issue6833Func() returns %x, expected %x", v, exp) - } -} diff --git a/libgo/misc/cgo/test/issue6907.go b/libgo/misc/cgo/test/issue6907.go deleted file mode 100644 index 00495ab..0000000 --- a/libgo/misc/cgo/test/issue6907.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017 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 - -/* -#include <stdlib.h> -#include <string.h> - -char* Issue6907CopyString(_GoString_ s) { - size_t n; - const char *p; - char *r; - - n = _GoStringLen(s); - p = _GoStringPtr(s); - r = malloc(n + 1); - memmove(r, p, n); - r[n] = '\0'; - return r; -} -*/ -import "C" - -import "testing" - -func test6907(t *testing.T) { - want := "yarn" - if got := C.GoString(C.Issue6907CopyString(want)); got != want { - t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want) - } -} diff --git a/libgo/misc/cgo/test/issue6907export.go b/libgo/misc/cgo/test/issue6907export.go deleted file mode 100644 index d41899e..0000000 --- a/libgo/misc/cgo/test/issue6907export.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017 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 int CheckIssue6907C(_GoString_); -*/ -import "C" - -import ( - "testing" -) - -const CString = "C string" - -//export CheckIssue6907Go -func CheckIssue6907Go(s string) C.int { - if s == CString { - return 1 - } - return 0 -} - -func test6907Go(t *testing.T) { - if got := C.CheckIssue6907C(CString); got != 1 { - t.Errorf("C.CheckIssue6907C() == %d, want %d", got, 1) - } -} diff --git a/libgo/misc/cgo/test/issue7560.go b/libgo/misc/cgo/test/issue7560.go deleted file mode 100644 index f36d8a1..0000000 --- a/libgo/misc/cgo/test/issue7560.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2014 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 - -/* -#include <stdint.h> - -typedef struct { - char x; - long y; -} __attribute__((__packed__)) misaligned; - -int -offset7560(void) -{ - return (uintptr_t)&((misaligned*)0)->y; -} -*/ -import "C" - -import ( - "reflect" - "testing" -) - -func test7560(t *testing.T) { - // some mingw don't implement __packed__ correctly. - if C.offset7560() != 1 { - t.Skip("C compiler did not pack struct") - } - - // C.misaligned should have x but then a padding field to get to the end of the struct. - // There should not be a field named 'y'. - var v C.misaligned - rt := reflect.TypeOf(&v).Elem() - if rt.NumField() != 2 || rt.Field(0).Name != "x" || rt.Field(1).Name != "_" { - t.Errorf("unexpected fields in C.misaligned:\n") - for i := 0; i < rt.NumField(); i++ { - t.Logf("%+v\n", rt.Field(i)) - } - } -} diff --git a/libgo/misc/cgo/test/issue7665.go b/libgo/misc/cgo/test/issue7665.go deleted file mode 100644 index ce03458..0000000 --- a/libgo/misc/cgo/test/issue7665.go +++ /dev/null @@ -1,25 +0,0 @@ -// 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 - -import ( - "testing" - "unsafe" -) - -// extern void f7665(void); -import "C" - -//export f7665 -func f7665() {} - -var bad7665 unsafe.Pointer = C.f7665 -var good7665 uintptr = uintptr(C.f7665) - -func test7665(t *testing.T) { - if bad7665 == nil || uintptr(bad7665) != good7665 { - t.Errorf("ptrs = %p, %#x, want same non-nil pointer", bad7665, good7665) - } -} diff --git a/libgo/misc/cgo/test/issue7786.go b/libgo/misc/cgo/test/issue7786.go deleted file mode 100644 index 1344e9e..0000000 --- a/libgo/misc/cgo/test/issue7786.go +++ /dev/null @@ -1,51 +0,0 @@ -// 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. - -// Issue 7786. No runtime test, just make sure that typedef and struct/union/class are interchangeable at compile time. - -package cgotest - -// struct test7786; -// typedef struct test7786 typedef_test7786; -// void f7786(struct test7786 *ctx) {} -// void g7786(typedef_test7786 *ctx) {} -// -// typedef struct body7786 typedef_body7786; -// struct body7786 { int x; }; -// void b7786(struct body7786 *ctx) {} -// void c7786(typedef_body7786 *ctx) {} -// -// typedef union union7786 typedef_union7786; -// void u7786(union union7786 *ctx) {} -// void v7786(typedef_union7786 *ctx) {} -import "C" - -func f() { - var x1 *C.typedef_test7786 - var x2 *C.struct_test7786 - x1 = x2 - x2 = x1 - C.f7786(x1) - C.f7786(x2) - C.g7786(x1) - C.g7786(x2) - - var b1 *C.typedef_body7786 - var b2 *C.struct_body7786 - b1 = b2 - b2 = b1 - C.b7786(b1) - C.b7786(b2) - C.c7786(b1) - C.c7786(b2) - - var u1 *C.typedef_union7786 - var u2 *C.union_union7786 - u1 = u2 - u2 = u1 - C.u7786(u1) - C.u7786(u2) - C.v7786(u1) - C.v7786(u2) -} diff --git a/libgo/misc/cgo/test/issue7978.go b/libgo/misc/cgo/test/issue7978.go deleted file mode 100644 index f0809d3..0000000 --- a/libgo/misc/cgo/test/issue7978.go +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2014 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 7978. Stack tracing didn't work during cgo code after calling a Go -// callback. Make sure GC works and the stack trace is correct. - -package cgotest - -/* -#include <stdint.h> - -void issue7978cb(void); - -// use ugly atomic variable sync since that doesn't require calling back into -// Go code or OS dependencies -static void issue7978c(uint32_t *sync) { - while(__atomic_load_n(sync, __ATOMIC_SEQ_CST) != 0) - ; - __atomic_add_fetch(sync, 1, __ATOMIC_SEQ_CST); - while(__atomic_load_n(sync, __ATOMIC_SEQ_CST) != 2) - ; - issue7978cb(); - __atomic_add_fetch(sync, 1, __ATOMIC_SEQ_CST); - while(__atomic_load_n(sync, __ATOMIC_SEQ_CST) != 6) - ; -} -*/ -import "C" - -import ( - "runtime" - "runtime/debug" - "strings" - "sync/atomic" - "testing" -) - -var issue7978sync uint32 - -func issue7978check(t *testing.T, wantFunc string, badFunc string, depth int) { - runtime.GC() - buf := make([]byte, 65536) - trace := string(buf[:runtime.Stack(buf, true)]) - for _, goroutine := range strings.Split(trace, "\n\n") { - if strings.Contains(goroutine, "test.issue7978go") { - trace := strings.Split(goroutine, "\n") - // look for the expected function in the stack - for i := 0; i < depth; i++ { - if badFunc != "" && strings.Contains(trace[1+2*i], badFunc) { - t.Errorf("bad stack: found %s in the stack:\n%s", badFunc, goroutine) - return - } - if strings.Contains(trace[1+2*i], wantFunc) { - return - } - } - t.Errorf("bad stack: didn't find %s in the stack:\n%s", wantFunc, goroutine) - return - } - } - t.Errorf("bad stack: goroutine not found. Full stack dump:\n%s", trace) -} - -func issue7978wait(store uint32, wait uint32) { - if store != 0 { - atomic.StoreUint32(&issue7978sync, store) - } - for atomic.LoadUint32(&issue7978sync) != wait { - runtime.Gosched() - } -} - -//export issue7978cb -func issue7978cb() { - // Force a stack growth from the callback to put extra - // pressure on the runtime. See issue #17785. - growStack(64) - issue7978wait(3, 4) -} - -func growStack(n int) int { - var buf [128]int - if n == 0 { - return 0 - } - return buf[growStack(n-1)] -} - -func issue7978go() { - C.issue7978c((*C.uint32_t)(&issue7978sync)) - issue7978wait(7, 8) -} - -func test7978(t *testing.T) { - if runtime.Compiler == "gccgo" { - t.Skip("gccgo can not do stack traces of C code") - } - debug.SetTraceback("2") - issue7978sync = 0 - go issue7978go() - // test in c code, before callback - issue7978wait(0, 1) - issue7978check(t, "_Cfunc_issue7978c(", "", 1) - // test in go code, during callback - issue7978wait(2, 3) - issue7978check(t, "test.issue7978cb(", "test.issue7978go", 3) - // test in c code, after callback - issue7978wait(4, 5) - issue7978check(t, "_Cfunc_issue7978c(", "_cgoexpwrap", 1) - // test in go code, after return from cgo - issue7978wait(6, 7) - issue7978check(t, "test.issue7978go(", "", 3) - atomic.StoreUint32(&issue7978sync, 8) -} diff --git a/libgo/misc/cgo/test/issue8092.go b/libgo/misc/cgo/test/issue8092.go deleted file mode 100644 index 19123e7..0000000 --- a/libgo/misc/cgo/test/issue8092.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2014 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 8092. Test that linker defined symbols (e.g., text, data) don't -// conflict with C symbols. - -package cgotest - -/* -char text[] = "text"; -char data[] = "data"; -char *ctext(void) { return text; } -char *cdata(void) { return data; } -*/ -import "C" - -import "testing" - -func test8092(t *testing.T) { - tests := []struct { - s string - a, b *C.char - }{ - {"text", &C.text[0], C.ctext()}, - {"data", &C.data[0], C.cdata()}, - } - for _, test := range tests { - if test.a != test.b { - t.Errorf("%s: pointer mismatch: %v != %v", test.s, test.a, test.b) - } - if got := C.GoString(test.a); got != test.s { - t.Errorf("%s: points at %#v, want %#v", test.s, got, test.s) - } - } -} diff --git a/libgo/misc/cgo/test/issue8331a.go b/libgo/misc/cgo/test/issue8331a.go deleted file mode 100644 index 92e2579..0000000 --- a/libgo/misc/cgo/test/issue8331a.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2014 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 8331. A typedef of an unnamed struct is the same struct when -// #include'd twice. No runtime test; just make sure it compiles. - -package cgotest - -// #include "issue8331.h" -import "C" - -func issue8331a() C.issue8331 { - return issue8331Var -} diff --git a/libgo/misc/cgo/test/issue8331b.go b/libgo/misc/cgo/test/issue8331b.go deleted file mode 100644 index 5324aa2..0000000 --- a/libgo/misc/cgo/test/issue8331b.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2014 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 8331. A typedef of an unnamed struct is the same struct when -// #include'd twice. No runtime test; just make sure it compiles. - -package cgotest - -// #include "issue8331.h" -import "C" - -var issue8331Var C.issue8331 diff --git a/libgo/misc/cgo/test/issue8428.go b/libgo/misc/cgo/test/issue8428.go deleted file mode 100644 index 2e5a555..0000000 --- a/libgo/misc/cgo/test/issue8428.go +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2014 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. - -// This test fails on older versions of OS X because they use older buggy -// versions of Clang that emit ambiguous DWARF info. See issue 8611. -// +build !darwin - -package cgotest - -// Issue 8428. Cgo inconsistently translated zero size arrays. - -/* -struct issue8428one { - char b; - char rest[]; -}; - -struct issue8428two { - void *p; - char b; - char rest[0]; - char pad; -}; - -struct issue8428three { - char w[1][2][3][0]; - char x[2][3][0][1]; - char y[3][0][1][2]; - char z[0][1][2][3]; -}; -*/ -import "C" - -import "unsafe" - -var _ = C.struct_issue8428one{ - b: C.char(0), - // The trailing rest field is not available in cgo. - // See issue 11925. - // rest: [0]C.char{}, -} - -var _ = C.struct_issue8428two{ - p: unsafe.Pointer(nil), - b: C.char(0), - rest: [0]C.char{}, -} - -var _ = C.struct_issue8428three{ - w: [1][2][3][0]C.char{}, - x: [2][3][0][1]C.char{}, - y: [3][0][1][2]C.char{}, - z: [0][1][2][3]C.char{}, -} diff --git a/libgo/misc/cgo/test/issue8441.go b/libgo/misc/cgo/test/issue8441.go deleted file mode 100644 index 4489ca9..0000000 --- a/libgo/misc/cgo/test/issue8441.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2014 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 8368 and 8441. Recursive struct definitions didn't work. -// No runtime test; just make sure it compiles. - -package cgotest - -/* -typedef struct one one; -typedef struct two two; -struct one { - two *x; -}; -struct two { - one *x; -}; -*/ -import "C" - -func issue8368(one *C.struct_one, two *C.struct_two) { -} - -func issue8441(one *C.one, two *C.two) { - issue8441(two.x, one.x) -} diff --git a/libgo/misc/cgo/test/issue8811.go b/libgo/misc/cgo/test/issue8811.go deleted file mode 100644 index f812732..0000000 --- a/libgo/misc/cgo/test/issue8811.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2014 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 int issue8811Initialized; -extern void issue8811Init(); - -void issue8811Execute() { - if(!issue8811Initialized) - issue8811Init(); -} -*/ -import "C" - -import "testing" - -func test8811(t *testing.T) { - C.issue8811Execute() -} diff --git a/libgo/misc/cgo/test/issue9557.go b/libgo/misc/cgo/test/issue9557.go deleted file mode 100644 index 4e8922a..0000000 --- a/libgo/misc/cgo/test/issue9557.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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. - -// cgo rewrote C.var to *_Cvar_var, but left -// C.var.field as _Cvar.var.field. It now rewrites -// the latter as (*_Cvar_var).field. -// See https://golang.org/issue/9557. - -package cgotest - -// struct issue9557_t { -// int a; -// } test9557bar = { 42 }; -// -// struct issue9557_t *issue9557foo = &test9557bar; -import "C" -import "testing" - -func test9557(t *testing.T) { - // implicitly dereference a Go variable - foo := C.issue9557foo - if v := foo.a; v != 42 { - t.Fatalf("foo.a expected 42, but got %d", v) - } - - // explicitly dereference a C variable - if v := (*C.issue9557foo).a; v != 42 { - t.Fatalf("(*C.issue9557foo).a expected 42, but is %d", v) - } - - // implicitly dereference a C variable - if v := C.issue9557foo.a; v != 42 { - t.Fatalf("C.issue9557foo.a expected 42, but is %d", v) - } -} diff --git a/libgo/misc/cgo/test/overlaydir_test.go b/libgo/misc/cgo/test/overlaydir_test.go new file mode 100644 index 0000000..1b5c67d --- /dev/null +++ b/libgo/misc/cgo/test/overlaydir_test.go @@ -0,0 +1,81 @@ +// Copyright 2019 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 ( + "io" + "os" + "path/filepath" + "strings" +) + +// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added. +// +// TODO: Once we no longer need to support the misc module in GOPATH mode, +// factor this function out into a package to reduce duplication. +func overlayDir(dstRoot, srcRoot string) error { + dstRoot = filepath.Clean(dstRoot) + if err := os.MkdirAll(dstRoot, 0777); err != nil { + return err + } + + symBase, err := filepath.Rel(srcRoot, dstRoot) + if err != nil { + symBase, err = filepath.Abs(srcRoot) + if err != nil { + return err + } + } + + return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error { + if err != nil || srcPath == srcRoot { + return err + } + + suffix := strings.TrimPrefix(srcPath, srcRoot) + for len(suffix) > 0 && suffix[0] == filepath.Separator { + suffix = suffix[1:] + } + dstPath := filepath.Join(dstRoot, suffix) + + perm := info.Mode() & os.ModePerm + if info.Mode()&os.ModeSymlink != 0 { + info, err = os.Stat(srcPath) + if err != nil { + return err + } + perm = info.Mode() & os.ModePerm + } + + // Always copy directories (don't symlink them). + // If we add a file in the overlay, we don't want to add it in the original. + if info.IsDir() { + return os.Mkdir(dstPath, perm) + } + + // If the OS supports symlinks, use them instead of copying bytes. + if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil { + return nil + } + + // Otherwise, copy the bytes. + src, err := os.Open(srcPath) + if err != nil { + return err + } + defer src.Close() + + dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm) + if err != nil { + return err + } + + _, err = io.Copy(dst, src) + if closeErr := dst.Close(); err == nil { + err = closeErr + } + return err + }) +} diff --git a/libgo/misc/cgo/test/pkg_test.go b/libgo/misc/cgo/test/pkg_test.go new file mode 100644 index 0000000..08e075c --- /dev/null +++ b/libgo/misc/cgo/test/pkg_test.go @@ -0,0 +1,69 @@ +// Copyright 2019 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 ( + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" +) + +// TestCrossPackageTests compiles and runs tests that depend on imports of other +// local packages, using source code stored in the testdata directory. +// +// The tests in the misc directory tree do not have a valid import path in +// GOPATH mode, so they previously used relative imports. However, relative +// imports do not work in module mode. In order to make the test work in both +// modes, we synthesize a GOPATH in which the module paths are equivalent, and +// run the tests as a subprocess. +// +// If and when we no longer support these tests in GOPATH mode, we can remove +// this shim and move the tests currently located in testdata back into the +// parent directory. +func TestCrossPackageTests(t *testing.T) { + switch runtime.GOOS { + case "android": + t.Skip("Can't exec cmd/go subprocess on Android.") + case "darwin": + switch runtime.GOARCH { + case "arm", "arm64": + t.Skip("Can't exec cmd/go subprocess on iOS.") + } + } + + GOPATH, err := ioutil.TempDir("", "cgotest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(GOPATH) + + modRoot := filepath.Join(GOPATH, "src", "cgotest") + if err := overlayDir(modRoot, "testdata"); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgotest\n"), 0666); err != nil { + t.Fatal(err) + } + + cmd := exec.Command("go", "test") + if testing.Verbose() { + cmd.Args = append(cmd.Args, "-v") + } + if testing.Short() { + cmd.Args = append(cmd.Args, "-short") + } + cmd.Dir = modRoot + cmd.Env = append(os.Environ(), "GOPATH="+GOPATH, "PWD="+cmd.Dir) + out, err := cmd.CombinedOutput() + if err == nil { + t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out) + } else { + t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out) + } +} diff --git a/libgo/misc/cgo/test/sigaltstack.go b/libgo/misc/cgo/test/sigaltstack.go index 2b7a1ec..2c9b81c 100644 --- a/libgo/misc/cgo/test/sigaltstack.go +++ b/libgo/misc/cgo/test/sigaltstack.go @@ -55,7 +55,7 @@ import ( func testSigaltstack(t *testing.T) { switch { - case runtime.GOOS == "solaris", runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"): + case runtime.GOOS == "solaris", runtime.GOOS == "illumos", runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"): t.Skipf("switching signal stack not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) case runtime.GOOS == "darwin" && runtime.GOARCH == "386": t.Skipf("sigaltstack fails on darwin/386") diff --git a/libgo/misc/cgo/test/test.go b/libgo/misc/cgo/test/test.go new file mode 100644 index 0000000..2d060bf --- /dev/null +++ b/libgo/misc/cgo/test/test.go @@ -0,0 +1,2104 @@ +// 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. + +// Test cases for cgo. +// Both the import "C" prologue and the main file are sorted by issue number. +// This file contains C definitions (not just declarations) +// and so it must NOT contain any //export directives on Go functions. +// See testx.go for exports. + +package cgotest + +/* +#include <complex.h> +#include <math.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> +#include <errno.h> +#cgo LDFLAGS: -lm + +#ifndef WIN32 +#include <pthread.h> +#include <signal.h> +#endif + +// alignment tests + +typedef unsigned char Uint8; +typedef unsigned short Uint16; + +typedef enum { + MOD1 = 0x0000, + MODX = 0x8000 +} SDLMod; + +typedef enum { + A1 = 1, + B1 = 322, + SDLK_LAST +} SDLKey; + +typedef struct SDL_keysym { + Uint8 scancode; + SDLKey sym; + SDLMod mod; + Uint16 unicode; +} SDL_keysym; + +typedef struct SDL_KeyboardEvent { + Uint8 typ; + Uint8 which; + Uint8 state; + SDL_keysym keysym; +} SDL_KeyboardEvent; + +void makeEvent(SDL_KeyboardEvent *event) { + unsigned char *p; + int i; + + p = (unsigned char*)event; + for (i=0; i<sizeof *event; i++) { + p[i] = i; + } +} + +int same(SDL_KeyboardEvent* e, Uint8 typ, Uint8 which, Uint8 state, Uint8 scan, SDLKey sym, SDLMod mod, Uint16 uni) { + return e->typ == typ && e->which == which && e->state == state && e->keysym.scancode == scan && e->keysym.sym == sym && e->keysym.mod == mod && e->keysym.unicode == uni; +} + +void cTest(SDL_KeyboardEvent *event) { + printf("C: %#x %#x %#x %#x %#x %#x %#x\n", event->typ, event->which, event->state, + event->keysym.scancode, event->keysym.sym, event->keysym.mod, event->keysym.unicode); + fflush(stdout); +} + +// api + +const char *greeting = "hello, world"; + +// basic test cases + +#define SHIFT(x, y) ((x)<<(y)) +#define KILO SHIFT(1, 10) +#define UINT32VAL 0xc008427bU + +enum E { + Enum1 = 1, + Enum2 = 2, +}; + +typedef unsigned char cgo_uuid_t[20]; + +void uuid_generate(cgo_uuid_t x) { + x[0] = 0; +} + +struct S { + int x; +}; + +const char *cstr = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + +extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter); + +enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; } + +int add(int x, int y) { + return x+y; +}; + +// complex alignment + +struct { + float x; + _Complex float y; +} cplxAlign = { 3.14, 2.17 }; + +// constants and pointer checking + +#define CheckConstVal 0 + +typedef struct { + int *p; +} CheckConstStruct; + +static void CheckConstFunc(CheckConstStruct *p, int e) {} + +// duplicate symbol + +int base_symbol = 0; +#define alias_one base_symbol +#define alias_two base_symbol + +// function pointer variables + +typedef int (*intFunc) (); + +int +bridge_int_func(intFunc f) +{ + return f(); +} + +int fortytwo() +{ + return 42; +} + +// issue 1222 +typedef union { + long align; +} xxpthread_mutex_t; +struct ibv_async_event { + union { + int x; + } element; +}; +struct ibv_context { + xxpthread_mutex_t mutex; +}; + +// issue 1635 +// 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 +void scatter() { + void *p = scatter; + printf("scatter = %p\n", p); +} + +// Adding this explicit extern declaration makes this a test for +// https://gcc.gnu.org/PR68072 aka https://golang.org/issue/13344 . +// It used to cause a cgo error when building with GCC 6. +extern int hola; + +// this example is in issue 3253 +int hola = 0; +int testHola() { return hola; } + +// issue 3250 +#ifdef WIN32 +void testSendSIG() {} +#else +static void *thread(void *p) { + const int M = 100; + int i; + (void)p; + for (i = 0; i < M; i++) { + pthread_kill(pthread_self(), SIGCHLD); + usleep(rand() % 20 + 5); + } + return NULL; +} +void testSendSIG() { + const int N = 20; + int i; + pthread_t tid[N]; + for (i = 0; i < N; i++) { + usleep(rand() % 200 + 100); + pthread_create(&tid[i], 0, thread, NULL); + } + for (i = 0; i < N; i++) + pthread_join(tid[i], 0); +} +#endif + +// issue 3261 +// 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__ +int vabs(int x) { + puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library."); + return (x < 0) ? -x : x; +} +#elif defined(__arm64__) && defined(__clang__) +int vabs(int x) { + puts("testLibgcc is disabled on ARM64 with clang due to lack of libgcc."); + 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 + + +// issue 3729 +// access errno from void C function +const char _expA = 0x42; +const float _expB = 3.14159; +const short _expC = 0x55aa; +const int _expD = 0xdeadbeef; + +#ifdef WIN32 +void g(void) {} +void g2(int x, char a, float b, short c, int d) {} +#else + +void g(void) { + errno = E2BIG; +} + +// try to pass some non-trivial arguments to function g2 +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; +} +#endif + +// issue 3945 +// Test that cgo reserves enough stack space during cgo call. +// See https://golang.org/issue/3945 for details. +void say() { + printf("%s from C\n", "hello"); +} + +// issue 4054 part 1 - other half in testx.go + +typedef enum { + A = 0, + B, + C, + D, + E, + F, + G, + H, + II, + J, +} issue4054a; + +// issue 4339 +// We've historically permitted #include <>, so test it here. Issue 29333. +#include <issue4339.h> + +// issue 4417 +// cmd/cgo: bool alignment/padding issue. +// bool alignment is wrong and causing wrong arguments when calling functions. +static int c_bool(bool a, bool b, int c, bool d, bool e) { + return c; +} + +// issue 4857 +#cgo CFLAGS: -Werror +const struct { int a; } *issue4857() { return (void *)0; } + +// issue 5224 +// Test that the #cgo CFLAGS directive works, +// with and without platform filters. +#cgo CFLAGS: -DCOMMON_VALUE=123 +#cgo windows CFLAGS: -DIS_WINDOWS=1 +#cgo !windows CFLAGS: -DIS_WINDOWS=0 +int common = COMMON_VALUE; +int is_windows = IS_WINDOWS; + +// issue 5227 +// linker incorrectly treats common symbols and +// leaves them undefined. + +typedef struct { + int Count; +} Fontinfo; + +Fontinfo SansTypeface; + +extern void init(); + +Fontinfo loadfont() { + Fontinfo f = {0}; + return f; +} + +void init() { + SansTypeface = loadfont(); +} + +// issue 5242 +// Cgo incorrectly computed the alignment of structs +// with no Go accessible fields as 0, and then panicked on +// modulo-by-zero computations. +typedef struct { +} foo; + +typedef struct { + int x : 1; +} bar; + +int issue5242(foo f, bar b) { + return 5242; +} + +// issue 5337 +// Verify that we can withstand SIGPROF received on foreign threads + +#ifdef WIN32 +void test5337() {} +#else +static void *thread1(void *p) { + (void)p; + pthread_kill(pthread_self(), SIGPROF); + return NULL; +} +void test5337() { + pthread_t tid; + pthread_create(&tid, 0, thread1, NULL); + pthread_join(tid, 0); +} +#endif + +// issue 5603 + +const long long issue5603exp = 0x12345678; +long long issue5603foo0() { return issue5603exp; } +long long issue5603foo1(void *p) { return issue5603exp; } +long long issue5603foo2(void *p, void *q) { return issue5603exp; } +long long issue5603foo3(void *p, void *q, void *r) { return issue5603exp; } +long long issue5603foo4(void *p, void *q, void *r, void *s) { return issue5603exp; } + +// issue 5740 + +int test5740a(void), test5740b(void); + +// issue 5986 +static void output5986() +{ + int current_row = 0, row_count = 0; + double sum_squares = 0; + double d; + do { + if (current_row == 10) { + current_row = 0; + } + ++row_count; + } + while (current_row++ != 1); + d = sqrt(sum_squares / row_count); + printf("sqrt is: %g\n", d); +} + +// issue 6128 +// Test handling of #defined names in clang. +// NOTE: Must use hex, or else a shortcut for decimals +// in cgo avoids trying to pass this to clang. +#define X 0x1 + +// issue 6472 +typedef struct +{ + struct + { + int x; + } y[16]; +} z; + +// issue 6612 +// Test new scheme for deciding whether C.name is an expression, type, constant. +// Clang silences some warnings when the name is a #defined macro, so test those too +// (even though we now use errors exclusively, not warnings). + +void myfunc(void) {} +int myvar = 5; +const char *mytext = "abcdef"; +typedef int mytype; +enum { + myenum = 1234, +}; + +#define myfunc_def myfunc +#define myvar_def myvar +#define mytext_def mytext +#define mytype_def mytype +#define myenum_def myenum +#define myint_def 12345 +#define myfloat_def 1.5 +#define mystring_def "hello" + +// issue 6907 +char* Issue6907CopyString(_GoString_ s) { + size_t n; + const char *p; + char *r; + + n = _GoStringLen(s); + p = _GoStringPtr(s); + r = malloc(n + 1); + memmove(r, p, n); + r[n] = '\0'; + return r; +} + +// issue 7560 +typedef struct { + char x; + long y; +} __attribute__((__packed__)) misaligned; + +int +offset7560(void) +{ + return (uintptr_t)&((misaligned*)0)->y; +} + +// issue 7786 +// No runtime test, just make sure that typedef and struct/union/class are interchangeable at compile time. + +struct test7786; +typedef struct test7786 typedef_test7786; +void f7786(struct test7786 *ctx) {} +void g7786(typedef_test7786 *ctx) {} + +typedef struct body7786 typedef_body7786; +struct body7786 { int x; }; +void b7786(struct body7786 *ctx) {} +void c7786(typedef_body7786 *ctx) {} + +typedef union union7786 typedef_union7786; +void u7786(union union7786 *ctx) {} +void v7786(typedef_union7786 *ctx) {} + +// issue 8092 +// Test that linker defined symbols (e.g., text, data) don't +// conflict with C symbols. +char text[] = "text"; +char data[] = "data"; +char *ctext(void) { return text; } +char *cdata(void) { return data; } + +// issue 8428 +// Cgo inconsistently translated zero size arrays. + +struct issue8428one { + char b; + char rest[]; +}; + +struct issue8428two { + void *p; + char b; + char rest[0]; + char pad; +}; + +struct issue8428three { + char w[1][2][3][0]; + char x[2][3][0][1]; + char y[3][0][1][2]; + char z[0][1][2][3]; +}; + +// issue 8331 part 1 - part 2 in testx.go +// A typedef of an unnamed struct is the same struct when +// #include'd twice. No runtime test; just make sure it compiles. +#include "issue8331.h" + +// issue 8368 and 8441 +// Recursive struct definitions didn't work. +// No runtime test; just make sure it compiles. +typedef struct one one; +typedef struct two two; +struct one { + two *x; +}; +struct two { + one *x; +}; + +// issue 8811 + +extern int issue8811Initialized; +extern void issue8811Init(); + +void issue8811Execute() { + if(!issue8811Initialized) + issue8811Init(); +} + +// issue 9557 + +struct issue9557_t { + int a; +} test9557bar = { 42 }; +struct issue9557_t *issue9557foo = &test9557bar; + +// issue 10303 +// Pointers passed to C were not marked as escaping (bug in cgo). + +typedef int *intptr; + +void setintstar(int *x) { + *x = 1; +} + +void setintptr(intptr x) { + *x = 1; +} + +void setvoidptr(void *x) { + *(int*)x = 1; +} + +typedef struct Struct Struct; +struct Struct { + int *P; +}; + +void setstruct(Struct s) { + *s.P = 1; +} + +// issue 11925 +// Structs with zero-length trailing fields are now padded by the Go compiler. + +struct a11925 { + int i; + char a[0]; + char b[0]; +}; + +struct b11925 { + int i; + char a[0]; + char b[]; +}; + +// issue 12030 +void issue12030conv(char *buf, double x) { + sprintf(buf, "d=%g", x); +} + +// issue 14838 + +int check_cbytes(char *b, size_t l) { + int i; + for (i = 0; i < l; i++) { + if (b[i] != i) { + return 0; + } + } + return 1; +} + +// issue 17065 +// Test that C symbols larger than a page play nicely with the race detector. +int ii[65537]; + +// issue 17537 +// The void* cast introduced by cgo to avoid problems +// with const/volatile qualifiers breaks C preprocessor macros that +// emulate functions. + +typedef struct { + int i; +} S17537; + +int I17537(S17537 *p); + +#define I17537(p) ((p)->i) + +// Calling this function used to fail without the cast. +const int F17537(const char **p) { + return **p; +} + +// issue 17723 +// API compatibility checks + +typedef char *cstring_pointer; +static void cstring_pointer_fun(cstring_pointer dummy) { } +const char *api_hello = "hello!"; + +// Calling this function used to trigger an error from the C compiler +// (issue 18298). +void F18298(const void *const *p) { +} + +// Test that conversions between typedefs work as they used to. +typedef const void *T18298_1; +struct S18298 { int i; }; +typedef const struct S18298 *T18298_2; +void G18298(T18298_1 t) { +} + +// issue 18126 +// cgo check of void function returning errno. +void Issue18126C(void **p) {} + +// issue 18720 + +#define HELLO "hello" +#define WORLD "world" +#define HELLO_WORLD HELLO "\000" WORLD + +struct foo { char c; }; +#define SIZE_OF(x) sizeof(x) +#define SIZE_OF_FOO SIZE_OF(struct foo) +#define VAR1 VAR +#define VAR var +int var = 5; + +#define ADDR &var + +#define CALL fn() +int fn(void) { + return ++var; +} + +// issue 20129 + +int issue20129 = 0; +typedef void issue20129Void; +issue20129Void issue20129Foo() { + issue20129 = 1; +} +typedef issue20129Void issue20129Void2; +issue20129Void2 issue20129Bar() { + issue20129 = 2; +} + +// issue 20369 +#define XUINT64_MAX 18446744073709551615ULL + +// issue 21668 +// Fail to guess the kind of the constant "x". +// No runtime test; just make sure it compiles. +const int x21668 = 42; + +// issue 21708 +#define CAST_TO_INT64 (int64_t)(-1) + +// issue 21809 +// Compile C `typedef` to go type aliases. + +typedef long MySigned_t; +// tests alias-to-alias +typedef MySigned_t MySigned2_t; +long takes_long(long x) { return x * x; } +MySigned_t takes_typedef(MySigned_t x) { return x * x; } + +// issue 22906 + +// It's going to be hard to include a whole real JVM to test this. +// So we'll simulate a really easy JVM using just the parts we need. +// This is the relevant part of jni.h. + +struct _jobject; + +typedef struct _jobject *jobject; +typedef jobject jclass; +typedef jobject jthrowable; +typedef jobject jstring; +typedef jobject jarray; +typedef jarray jbooleanArray; +typedef jarray jbyteArray; +typedef jarray jcharArray; +typedef jarray jshortArray; +typedef jarray jintArray; +typedef jarray jlongArray; +typedef jarray jfloatArray; +typedef jarray jdoubleArray; +typedef jarray jobjectArray; + +typedef jobject jweak; + +// Note: jvalue is already a non-pointer type due to it being a C union. + +// issue 22958 + +typedef struct { + unsigned long long f8 : 8; + unsigned long long f16 : 16; + unsigned long long f24 : 24; + unsigned long long f32 : 32; + unsigned long long f40 : 40; + unsigned long long f48 : 48; + unsigned long long f56 : 56; + unsigned long long f64 : 64; +} issue22958Type; + +// issue 23356 +int a(void) { return 5; }; +int r(void) { return 3; }; + +// issue 23720 +typedef int *issue23720A; +typedef const int *issue23720B; +void issue23720F(issue23720B a) {} + +// issue 24206 +#if defined(__linux__) && defined(__x86_64__) +#include <sys/mman.h> +// Returns string with null byte at the last valid address +char* dangerousString1() { + int pageSize = 4096; + char *data = mmap(0, 2 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0); + mprotect(data + pageSize,pageSize,PROT_NONE); + int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte + int i = start; + for (; i < pageSize; i++) { + data[i] = 'x'; + } + data[pageSize -1 ] = 0; + return data+start; +} + +char* dangerousString2() { + int pageSize = 4096; + char *data = mmap(0, 3 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0); + mprotect(data + 2 * pageSize,pageSize,PROT_NONE); + int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte + int i = start; + for (; i < 2 * pageSize; i++) { + data[i] = 'x'; + } + data[2*pageSize -1 ] = 0; + return data+start; +} +#else +char *dangerousString1() { return NULL; } +char *dangerousString2() { return NULL; } +#endif + +// issue 26066 +const unsigned long long int issue26066 = (const unsigned long long) -1; + +// issue 26517 +// Introduce two pointer types which are distinct, but have the same +// base type. Make sure that both of those pointer types get resolved +// correctly. Before the fix for 26517 if one of these pointer types +// was resolved before the other one was processed, the second one +// would never be resolved. +// Before this issue was fixed this test failed on Windows, +// where va_list expands to a named char* type. +typedef va_list TypeOne; +typedef char *TypeTwo; + +// issue 28540 + +static void twoargs1(void *p, int n) {} +static void *twoargs2() { return 0; } +static int twoargs3(void * p) { return 0; } + +// issue 28545 +// Failed to add type conversion for negative constant. + +static void issue28545F(char **p, int n, complex double a) {} + +// issue 28772 part 1 - part 2 in testx.go +// Failed to add type conversion for Go constant set to C constant. +// No runtime test; just make sure it compiles. + +#define issue28772Constant 1 + +// issue 28896 +// cgo was incorrectly adding padding after a packed struct. +typedef struct { + void *f1; + uint32_t f2; +} __attribute__((__packed__)) innerPacked; + +typedef struct { + innerPacked g1; + uint64_t g2; +} outerPacked; + +typedef struct { + void *f1; + uint32_t f2; +} innerUnpacked; + +typedef struct { + innerUnpacked g1; + uint64_t g2; +} outerUnpacked; + +size_t offset(int x) { + switch (x) { + case 0: + return offsetof(innerPacked, f2); + case 1: + return offsetof(outerPacked, g2); + case 2: + return offsetof(innerUnpacked, f2); + case 3: + return offsetof(outerUnpacked, g2); + default: + abort(); + } +} + +// issue 29748 + +typedef struct { char **p; } S29748; +static int f29748(S29748 *p) { return 0; } + +// issue 29781 +// Error with newline inserted into constant expression. +// Compilation test only, nothing to run. + +static void issue29781F(char **p, int n) {} +#define ISSUE29781C 0 + +// issue 31093 +static uint16_t issue31093F(uint16_t v) { return v; } + +// issue 32579 +typedef struct S32579 { unsigned char data[1]; } S32579; +*/ +import "C" + +import ( + "context" + "fmt" + "math" + "math/rand" + "os" + "os/signal" + "reflect" + "runtime" + "sync" + "syscall" + "testing" + "time" + "unsafe" +) + +// alignment + +func testAlign(t *testing.T) { + var evt C.SDL_KeyboardEvent + C.makeEvent(&evt) + if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 { + t.Error("*** bad alignment") + C.cTest(&evt) + t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n", + evt.typ, evt.which, evt.state, evt.keysym.scancode, + evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) + t.Error(evt) + } +} + +// api + +const greeting = "hello, world" + +type testPair struct { + Name string + Got, Want interface{} +} + +var testPairs = []testPair{ + {"GoString", C.GoString(C.greeting), greeting}, + {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]}, + {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])}, +} + +func testHelpers(t *testing.T) { + for _, pair := range testPairs { + if !reflect.DeepEqual(pair.Got, pair.Want) { + t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want) + } + } +} + +// basic test cases + +const EINVAL = C.EINVAL /* test #define */ + +var KILO = C.KILO + +func uuidgen() { + var uuid C.cgo_uuid_t + C.uuid_generate(&uuid[0]) +} + +func Strtol(s string, base int) (int, error) { + p := C.CString(s) + n, err := C.strtol(p, nil, C.int(base)) + C.free(unsafe.Pointer(p)) + return int(n), err +} + +func Atol(s string) int { + p := C.CString(s) + n := C.atol(p) + C.free(unsafe.Pointer(p)) + return int(n) +} + +func testConst(t *testing.T) { + C.myConstFunc(nil, 0, nil) +} + +func testEnum(t *testing.T) { + if C.Enum1 != 1 || C.Enum2 != 2 { + t.Error("bad enum", C.Enum1, C.Enum2) + } +} + +func testAtol(t *testing.T) { + l := Atol("123") + if l != 123 { + t.Error("Atol 123: ", l) + } +} + +func testErrno(t *testing.T) { + p := C.CString("no-such-file") + m := C.CString("r") + f, err := C.fopen(p, m) + C.free(unsafe.Pointer(p)) + C.free(unsafe.Pointer(m)) + if err == nil { + C.fclose(f) + t.Fatalf("C.fopen: should fail") + } + if err != syscall.ENOENT { + t.Fatalf("C.fopen: unexpected error: %v", err) + } +} + +func testMultipleAssign(t *testing.T) { + p := C.CString("234") + n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10) + 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)) +} + +var ( + cuint = (C.uint)(0) + culong C.ulong + cchar C.char +) + +type Context struct { + ctx *C.struct_ibv_context +} + +func benchCgoCall(b *testing.B) { + const x = C.int(2) + const y = C.int(3) + for i := 0; i < b.N; i++ { + C.add(x, y) + } +} + +var sinkString string + +func benchGoString(b *testing.B) { + for i := 0; i < b.N; i++ { + sinkString = C.GoString(C.cstr) + } + const want = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890" + if sinkString != want { + b.Fatalf("%q != %q", sinkString, want) + } +} + +// Static (build-time) test that syntax traversal visits all operands of s[i:j:k]. +func sliceOperands(array [2000]int) { + _ = array[C.KILO:C.KILO:C.KILO] // no type error +} + +// set in cgo_thread_lock.go init +var testThreadLockFunc = func(*testing.T) {} + +// complex alignment + +func TestComplexAlign(t *testing.T) { + if C.cplxAlign.x != 3.14 { + t.Errorf("got %v, expected 3.14", C.cplxAlign.x) + } + if C.cplxAlign.y != 2.17 { + t.Errorf("got %v, expected 2.17", C.cplxAlign.y) + } +} + +// constants and pointer checking + +func testCheckConst(t *testing.T) { + // The test is that this compiles successfully. + p := C.malloc(C.size_t(unsafe.Sizeof(C.int(0)))) + defer C.free(p) + C.CheckConstFunc(&C.CheckConstStruct{(*C.int)(p)}, C.CheckConstVal) +} + +// duplicate symbol + +func duplicateSymbols() { + fmt.Printf("%v %v %v\n", C.base_symbol, C.alias_one, C.alias_two) +} + +// environment + +// This is really an os package test but here for convenience. +func testSetEnv(t *testing.T) { + if runtime.GOOS == "windows" { + // Go uses SetEnvironmentVariable on windows. However, + // C runtime takes a *copy* at process startup of the + // OS environment, and stores it in environ/envp. + // It is this copy that getenv/putenv manipulate. + t.Logf("skipping test") + return + } + const key = "CGO_OS_TEST_KEY" + const val = "CGO_OS_TEST_VALUE" + os.Setenv(key, val) + keyc := C.CString(key) + defer C.free(unsafe.Pointer(keyc)) + v := C.getenv(keyc) + if uintptr(unsafe.Pointer(v)) == 0 { + t.Fatal("getenv returned NULL") + } + vs := C.GoString(v) + if vs != val { + t.Fatalf("getenv() = %q; want %q", vs, val) + } +} + +// function pointer variables + +func callBridge(f C.intFunc) int { + return int(C.bridge_int_func(f)) +} + +func callCBridge(f C.intFunc) C.int { + return C.bridge_int_func(f) +} + +func testFpVar(t *testing.T) { + const expected = 42 + f := C.intFunc(C.fortytwo) + res1 := C.bridge_int_func(f) + if r1 := int(res1); r1 != expected { + t.Errorf("got %d, want %d", r1, expected) + } + res2 := callCBridge(f) + if r2 := int(res2); r2 != expected { + t.Errorf("got %d, want %d", r2, expected) + } + r3 := callBridge(f) + if r3 != expected { + t.Errorf("got %d, want %d", r3, expected) + } +} + +// issue 1222 +type AsyncEvent struct { + event C.struct_ibv_async_event +} + +// issue 1635 + +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) + } +} + +// 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) + } +} + +// issue 3250 + +func test3250(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("not applicable on windows") + } + + t.Skip("skipped, see golang.org/issue/5885") + var ( + thres = 1 + sig = syscall_dot_SIGCHLD + ) + type result struct { + n int + sig os.Signal + } + var ( + sigCh = make(chan os.Signal, 10) + waitStart = make(chan struct{}) + waitDone = make(chan result) + ) + + signal.Notify(sigCh, sig) + + go func() { + n := 0 + alarm := time.After(time.Second * 3) + for { + select { + case <-waitStart: + waitStart = nil + case v := <-sigCh: + n++ + if v != sig || n > thres { + waitDone <- result{n, v} + return + } + case <-alarm: + waitDone <- result{n, sig} + return + } + } + }() + + waitStart <- struct{}{} + C.testSendSIG() + r := <-waitDone + if r.sig != sig { + t.Fatalf("received signal %v, but want %v", r.sig, sig) + } + t.Logf("got %d signals\n", r.n) + if r.n <= thres { + t.Fatalf("expected more than %d", thres) + } +} + +// issue 3261 + +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 + } + } +} + +// issue 3729 + +func test3729(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("skipping on windows") + } + + _, 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) + } +} + +// issue 3945 + +func testPrintf(t *testing.T) { + C.say() +} + +// issue 4054 + +var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J} + +// issue 4339 + +func test4339(t *testing.T) { + C.handle4339(&C.exported4339) +} + +// issue 4417 + +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) + } +} + +// issue 4857 + +func test4857() { + _ = C.issue4857() +} + +// issue 5224 + +func testCflags(t *testing.T) { + is_windows := C.is_windows == 1 + if is_windows != (runtime.GOOS == "windows") { + t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS) + } + if C.common != 123 { + t.Errorf("common: %v (expected 123)", C.common) + } +} + +// issue 5227 + +func test5227(t *testing.T) { + C.init() +} + +func selectfont() C.Fontinfo { + return C.SansTypeface +} + +// issue 5242 + +func test5242(t *testing.T) { + if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 { + t.Errorf("got %v", got) + } +} + +func test5603(t *testing.T) { + var x [5]int64 + exp := int64(C.issue5603exp) + x[0] = int64(C.issue5603foo0()) + x[1] = int64(C.issue5603foo1(nil)) + x[2] = int64(C.issue5603foo2(nil, nil)) + x[3] = int64(C.issue5603foo3(nil, nil, nil)) + x[4] = int64(C.issue5603foo4(nil, nil, nil, nil)) + for i, v := range x { + if v != exp { + t.Errorf("issue5603foo%d() returns %v, expected %v", i, v, exp) + } + } +} + +// issue 5337 + +func test5337(t *testing.T) { + C.test5337() +} + +// issue 5740 + +func test5740(t *testing.T) { + if v := C.test5740a() + C.test5740b(); v != 5 { + t.Errorf("expected 5, got %v", v) + } +} + +// issue 5986 + +func test5986(t *testing.T) { + C.output5986() +} + +// issue 6128 + +func test6128() { + // nothing to run, just make sure this compiles. + _ = C.X +} + +// issue 6390 + +func test6390(t *testing.T) { + p1 := C.malloc(1024) + if p1 == nil { + t.Fatalf("C.malloc(1024) returned nil") + } + p2 := C.malloc(0) + if p2 == nil { + t.Fatalf("C.malloc(0) returned nil") + } + C.free(p1) + C.free(p2) +} + +func test6472() { + // nothing to run, just make sure this compiles + s := new(C.z) + println(s.y[0].x) +} + +// issue 6506 + +func test6506() { + // nothing to run, just make sure this compiles + var x C.size_t + + C.calloc(x, x) + C.malloc(x) + C.realloc(nil, x) + C.memcpy(nil, nil, x) + C.memcmp(nil, nil, x) + C.memmove(nil, nil, x) + C.strncpy(nil, nil, x) + C.strncmp(nil, nil, x) + C.strncat(nil, nil, x) + x = C.strxfrm(nil, nil, x) + C.memchr(nil, 0, x) + x = C.strcspn(nil, nil) + x = C.strspn(nil, nil) + C.memset(nil, 0, x) + x = C.strlen(nil) + _ = x +} + +// issue 6612 + +func testNaming(t *testing.T) { + C.myfunc() + C.myfunc_def() + if v := C.myvar; v != 5 { + t.Errorf("C.myvar = %d, want 5", v) + } + if v := C.myvar_def; v != 5 { + t.Errorf("C.myvar_def = %d, want 5", v) + } + if s := C.GoString(C.mytext); s != "abcdef" { + t.Errorf("C.mytext = %q, want %q", s, "abcdef") + } + if s := C.GoString(C.mytext_def); s != "abcdef" { + t.Errorf("C.mytext_def = %q, want %q", s, "abcdef") + } + if c := C.myenum; c != 1234 { + t.Errorf("C.myenum = %v, want 1234", c) + } + if c := C.myenum_def; c != 1234 { + t.Errorf("C.myenum_def = %v, want 1234", c) + } + { + const c = C.myenum + if c != 1234 { + t.Errorf("C.myenum as const = %v, want 1234", c) + } + } + { + const c = C.myenum_def + if c != 1234 { + t.Errorf("C.myenum as const = %v, want 1234", c) + } + } + if c := C.myint_def; c != 12345 { + t.Errorf("C.myint_def = %v, want 12345", c) + } + { + const c = C.myint_def + if c != 12345 { + t.Errorf("C.myint as const = %v, want 12345", c) + } + } + + if c := C.myfloat_def; c != 1.5 { + t.Errorf("C.myint_def = %v, want 1.5", c) + } + { + const c = C.myfloat_def + if c != 1.5 { + t.Errorf("C.myint as const = %v, want 1.5", c) + } + } + + if s := C.mystring_def; s != "hello" { + t.Errorf("C.mystring_def = %q, want %q", s, "hello") + } +} + +// issue 6907 + +func test6907(t *testing.T) { + want := "yarn" + if got := C.GoString(C.Issue6907CopyString(want)); got != want { + t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want) + } +} + +// issue 7560 + +func test7560(t *testing.T) { + // some mingw don't implement __packed__ correctly. + if C.offset7560() != 1 { + t.Skip("C compiler did not pack struct") + } + + // C.misaligned should have x but then a padding field to get to the end of the struct. + // There should not be a field named 'y'. + var v C.misaligned + rt := reflect.TypeOf(&v).Elem() + if rt.NumField() != 2 || rt.Field(0).Name != "x" || rt.Field(1).Name != "_" { + t.Errorf("unexpected fields in C.misaligned:\n") + for i := 0; i < rt.NumField(); i++ { + t.Logf("%+v\n", rt.Field(i)) + } + } +} + +// issue 7786 + +func f() { + var x1 *C.typedef_test7786 + var x2 *C.struct_test7786 + x1 = x2 + x2 = x1 + C.f7786(x1) + C.f7786(x2) + C.g7786(x1) + C.g7786(x2) + + var b1 *C.typedef_body7786 + var b2 *C.struct_body7786 + b1 = b2 + b2 = b1 + C.b7786(b1) + C.b7786(b2) + C.c7786(b1) + C.c7786(b2) + + var u1 *C.typedef_union7786 + var u2 *C.union_union7786 + u1 = u2 + u2 = u1 + C.u7786(u1) + C.u7786(u2) + C.v7786(u1) + C.v7786(u2) +} + +// issue 8092 + +func test8092(t *testing.T) { + tests := []struct { + s string + a, b *C.char + }{ + {"text", &C.text[0], C.ctext()}, + {"data", &C.data[0], C.cdata()}, + } + for _, test := range tests { + if test.a != test.b { + t.Errorf("%s: pointer mismatch: %v != %v", test.s, test.a, test.b) + } + if got := C.GoString(test.a); got != test.s { + t.Errorf("%s: points at %#v, want %#v", test.s, got, test.s) + } + } +} + +// issues 8368 and 8441 + +func issue8368(one *C.struct_one, two *C.struct_two) { +} + +func issue8441(one *C.one, two *C.two) { + issue8441(two.x, one.x) +} + +// issue 8428 + +var _ = C.struct_issue8428one{ + b: C.char(0), + // The trailing rest field is not available in cgo. + // See issue 11925. + // rest: [0]C.char{}, +} + +var _ = C.struct_issue8428two{ + p: unsafe.Pointer(nil), + b: C.char(0), + rest: [0]C.char{}, +} + +var _ = C.struct_issue8428three{ + w: [1][2][3][0]C.char{}, + x: [2][3][0][1]C.char{}, + y: [3][0][1][2]C.char{}, + z: [0][1][2][3]C.char{}, +} + +// issue 8811 + +func test8811(t *testing.T) { + C.issue8811Execute() +} + +// issue 9557 + +func test9557(t *testing.T) { + // implicitly dereference a Go variable + foo := C.issue9557foo + if v := foo.a; v != 42 { + t.Fatalf("foo.a expected 42, but got %d", v) + } + + // explicitly dereference a C variable + if v := (*C.issue9557foo).a; v != 42 { + t.Fatalf("(*C.issue9557foo).a expected 42, but is %d", v) + } + + // implicitly dereference a C variable + if v := C.issue9557foo.a; v != 42 { + t.Fatalf("C.issue9557foo.a expected 42, but is %d", v) + } +} + +// issue 8331 part 1 + +func issue8331a() C.issue8331 { + return issue8331Var +} + +// issue 10303 + +func test10303(t *testing.T, n int) { + if runtime.Compiler == "gccgo" { + t.Skip("gccgo permits C pointers on the stack") + } + + // Run at a few different stack depths just to avoid an unlucky pass + // due to variables ending up on different pages. + if n > 0 { + test10303(t, n-1) + } + if t.Failed() { + return + } + var x, y, z, v, si C.int + var s C.Struct + C.setintstar(&x) + C.setintptr(&y) + C.setvoidptr(unsafe.Pointer(&v)) + s.P = &si + C.setstruct(s) + + if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { + t.Error("C int* argument on stack") + } + if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { + t.Error("C intptr argument on stack") + } + if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { + t.Error("C void* argument on stack") + } + if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { + t.Error("C struct field pointer on stack") + } +} + +// issue 11925 + +func test11925(t *testing.T) { + if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) { + t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{})) + } + if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) { + t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{})) + } +} + +// issue 12030 + +func test12030(t *testing.T) { + buf := (*C.char)(C.malloc(256)) + defer C.free(unsafe.Pointer(buf)) + for _, f := range []float64{1.0, 2.0, 3.14} { + C.issue12030conv(buf, C.double(f)) + got := C.GoString(buf) + if want := fmt.Sprintf("d=%g", f); got != want { + t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want) + } + } +} + +// issue 13402 + +var _ C.complexfloat +var _ C.complexdouble + +// issue 13930 +// Test that cgo's multiple-value special form for +// C function calls works in variable declaration statements. + +var _, _ = C.abs(0) + +// issue 14838 + +func test14838(t *testing.T) { + data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + cData := C.CBytes(data) + defer C.free(cData) + + if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 { + t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:]) + } +} + +// issue 17065 + +var sink C.int + +func test17065(t *testing.T) { + if runtime.GOOS == "darwin" { + t.Skip("broken on darwin; issue 17065") + } + for i := range C.ii { + sink = C.ii[i] + } +} + +// issue 17537 + +func test17537(t *testing.T) { + v := C.S17537{i: 17537} + if got, want := C.I17537(&v), C.int(17537); got != want { + t.Errorf("got %d, want %d", got, want) + } + + p := (*C.char)(C.malloc(1)) + *p = 17 + if got, want := C.F17537(&p), C.int(17); got != want { + t.Errorf("got %d, want %d", got, want) + } + + C.F18298(nil) + var v18298 C.T18298_2 + C.G18298(C.T18298_1(v18298)) +} + +// issue 17723 + +func testAPI() { + var cs *C.char + cs = C.CString("hello") + defer C.free(unsafe.Pointer(cs)) + var s string + s = C.GoString((*C.char)(C.api_hello)) + s = C.GoStringN((*C.char)(C.api_hello), C.int(6)) + var b []byte + b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6)) + _, _ = s, b + C.cstring_pointer_fun(nil) +} + +// issue 18126 + +func test18126(t *testing.T) { + p := C.malloc(1) + _, err := C.Issue18126C(&p) + C.free(p) + _ = err +} + +// issue 18720 + +func test18720(t *testing.T) { + if got, want := C.HELLO_WORLD, "hello\000world"; got != want { + t.Errorf("C.HELLO_WORLD == %q, expected %q", got, want) + } + + if got, want := C.VAR1, C.int(5); got != want { + t.Errorf("C.VAR1 == %v, expected %v", got, want) + } + + if got, want := *C.ADDR, C.int(5); got != want { + t.Errorf("*C.ADDR == %v, expected %v", got, want) + } + + if got, want := C.CALL, C.int(6); got != want { + t.Errorf("C.CALL == %v, expected %v", got, want) + } + + if got, want := C.CALL, C.int(7); got != want { + t.Errorf("C.CALL == %v, expected %v", got, want) + } + + // Issue 20125. + if got, want := C.SIZE_OF_FOO, 1; got != want { + t.Errorf("C.SIZE_OF_FOO == %v, expected %v", got, want) + } +} + +// issue 20129 + +func test20129(t *testing.T) { + if C.issue20129 != 0 { + t.Fatal("test is broken") + } + C.issue20129Foo() + if C.issue20129 != 1 { + t.Errorf("got %v but expected %v", C.issue20129, 1) + } + C.issue20129Bar() + if C.issue20129 != 2 { + t.Errorf("got %v but expected %v", C.issue20129, 2) + } +} + +// issue 20369 + +func test20369(t *testing.T) { + if C.XUINT64_MAX != math.MaxUint64 { + t.Fatalf("got %v, want %v", uint64(C.XUINT64_MAX), uint64(math.MaxUint64)) + } +} + +// issue 21668 + +var issue21668_X = C.x21668 + +// issue 21708 + +func test21708(t *testing.T) { + if got, want := C.CAST_TO_INT64, -1; got != want { + t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want) + } +} + +// issue 21809 + +func test21809(t *testing.T) { + longVar := C.long(3) + typedefVar := C.MySigned_t(4) + typedefTypedefVar := C.MySigned2_t(5) + + // all three should be considered identical to `long` + if ret := C.takes_long(longVar); ret != 9 { + t.Errorf("got %v but expected %v", ret, 9) + } + if ret := C.takes_long(typedefVar); ret != 16 { + t.Errorf("got %v but expected %v", ret, 16) + } + if ret := C.takes_long(typedefTypedefVar); ret != 25 { + t.Errorf("got %v but expected %v", ret, 25) + } + + // They should also be identical to the typedef'd type + if ret := C.takes_typedef(longVar); ret != 9 { + t.Errorf("got %v but expected %v", ret, 9) + } + if ret := C.takes_typedef(typedefVar); ret != 16 { + t.Errorf("got %v but expected %v", ret, 16) + } + if ret := C.takes_typedef(typedefTypedefVar); ret != 25 { + t.Errorf("got %v but expected %v", ret, 25) + } +} + +// issue 22906 + +func test22906(t *testing.T) { + var x1 C.jobject = 0 // Note: 0, not nil. That makes sure we use uintptr for these types. + _ = x1 + var x2 C.jclass = 0 + _ = x2 + var x3 C.jthrowable = 0 + _ = x3 + var x4 C.jstring = 0 + _ = x4 + var x5 C.jarray = 0 + _ = x5 + var x6 C.jbooleanArray = 0 + _ = x6 + var x7 C.jbyteArray = 0 + _ = x7 + var x8 C.jcharArray = 0 + _ = x8 + var x9 C.jshortArray = 0 + _ = x9 + var x10 C.jintArray = 0 + _ = x10 + var x11 C.jlongArray = 0 + _ = x11 + var x12 C.jfloatArray = 0 + _ = x12 + var x13 C.jdoubleArray = 0 + _ = x13 + var x14 C.jobjectArray = 0 + _ = x14 + var x15 C.jweak = 0 + _ = x15 +} + +// issue 22958 +// Nothing to run, just make sure this compiles. +var Vissue22958 C.issue22958Type + +func test23356(t *testing.T) { + if got, want := C.a(), C.int(5); got != want { + t.Errorf("C.a() == %v, expected %v", got, want) + } + if got, want := C.r(), C.int(3); got != want { + t.Errorf("C.r() == %v, expected %v", got, want) + } +} + +// issue 23720 + +func Issue23720F() { + var x C.issue23720A + C.issue23720F(x) +} + +// issue 24206 + +func test24206(t *testing.T) { + if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { + t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) + } + + if l := len(C.GoString(C.dangerousString1())); l != 123 { + t.Errorf("Incorrect string length - got %d, want 123", l) + } + if l := len(C.GoString(C.dangerousString2())); l != 4096+123 { + t.Errorf("Incorrect string length - got %d, want %d", l, 4096+123) + } +} + +// issue 25143 + +func issue25143sum(ns ...C.int) C.int { + total := C.int(0) + for _, n := range ns { + total += n + } + return total +} + +func test25143(t *testing.T) { + if got, want := issue25143sum(1, 2, 3), C.int(6); got != want { + t.Errorf("issue25143sum(1, 2, 3) == %v, expected %v", got, want) + } +} + +// issue 26066 +// Wrong type of constant with GCC 8 and newer. + +func test26066(t *testing.T) { + var i = int64(C.issue26066) + if i != -1 { + t.Errorf("got %d, want -1", i) + } +} + +// issue 26517 +var a C.TypeOne +var b C.TypeTwo + +// issue 27660 +// Stress the interaction between the race detector and cgo in an +// attempt to reproduce the memory corruption described in #27660. +// The bug was very timing sensitive; at the time of writing this +// test would only trigger the bug about once out of every five runs. + +func test27660(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + ints := make([]int, 100) + locks := make([]sync.Mutex, 100) + // Slowly create threads so that ThreadSanitizer is forced to + // frequently resize its SyncClocks. + for i := 0; i < 100; i++ { + go func() { + for ctx.Err() == nil { + // Sleep in C for long enough that it is likely that the runtime + // will retake this goroutine's currently wired P. + C.usleep(1000 /* 1ms */) + runtime.Gosched() // avoid starvation (see #28701) + } + }() + go func() { + // Trigger lots of synchronization and memory reads/writes to + // increase the likelihood that the race described in #27660 + // results in corruption of ThreadSanitizer's internal state + // and thus an assertion failure or segfault. + i := 0 + for ctx.Err() == nil { + j := rand.Intn(100) + locks[j].Lock() + ints[j]++ + locks[j].Unlock() + // Avoid making the loop unpreemptible + // for gccgo. + if i%0x1000000 == 0 { + runtime.Gosched() + } + i++ + } + }() + time.Sleep(time.Millisecond) + } +} + +// issue 28540 + +func twoargsF() { + v := []string{} + C.twoargs1(C.twoargs2(), C.twoargs3(unsafe.Pointer(&v))) +} + +// issue 28545 + +func issue28545G(p **C.char) { + C.issue28545F(p, -1, (0)) + C.issue28545F(p, 2+3, complex(1, 1)) + C.issue28545F(p, issue28772Constant, issue28772Constant2) +} + +// issue 28772 part 1 - part 2 in testx.go + +const issue28772Constant = C.issue28772Constant + +// issue 28896 + +func offset(i int) uintptr { + var pi C.innerPacked + var po C.outerPacked + var ui C.innerUnpacked + var uo C.outerUnpacked + switch i { + case 0: + return unsafe.Offsetof(pi.f2) + case 1: + return unsafe.Offsetof(po.g2) + case 2: + return unsafe.Offsetof(ui.f2) + case 3: + return unsafe.Offsetof(uo.g2) + default: + panic("can't happen") + } +} + +func test28896(t *testing.T) { + for i := 0; i < 4; i++ { + c := uintptr(C.offset(C.int(i))) + g := offset(i) + if c != g { + t.Errorf("%d: C: %d != Go %d", i, c, g) + } + } +} + +// issue 29383 +// cgo's /*line*/ comments failed when inserted after '/', +// because the result looked like a "//" comment. +// No runtime test; just make sure it compiles. + +func Issue29383(n, size uint) int { + if ^C.size_t(0)/C.size_t(n) < C.size_t(size) { + return 0 + } + return 0 +} + +// issue 29748 +// Error handling a struct initializer that requires pointer checking. +// Compilation test only, nothing to run. + +var Vissue29748 = C.f29748(&C.S29748{ + nil, +}) + +func Fissue299748() { + C.f29748(&C.S29748{ + nil, + }) +} + +// issue 29781 + +var issue29781X struct{ X int } + +func issue29781F(...int) int { return 0 } + +func issue29781G() { + var p *C.char + C.issue29781F(&p, C.ISSUE29781C+1) + C.issue29781F(nil, (C.int)( + 0)) + C.issue29781F(&p, (C.int)(0)) + C.issue29781F(&p, (C.int)( + 0)) + C.issue29781F(&p, (C.int)(issue29781X. + X)) +} + +// issue 30065 + +func test30065(t *testing.T) { + var a [256]byte + b := []byte("a") + C.memcpy(unsafe.Pointer(&a), unsafe.Pointer(&b[0]), 1) + if a[0] != 'a' { + t.Errorf("&a failed: got %c, want %c", a[0], 'a') + } + + b = []byte("b") + C.memcpy(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), 1) + if a[0] != 'b' { + t.Errorf("&a[0] failed: got %c, want %c", a[0], 'b') + } + + d := make([]byte, 256) + b = []byte("c") + C.memcpy(unsafe.Pointer(&d[0]), unsafe.Pointer(&b[0]), 1) + if d[0] != 'c' { + t.Errorf("&d[0] failed: got %c, want %c", d[0], 'c') + } +} + +// issue 31093 +// No runtime test; just make sure it compiles. + +func Issue31093() { + C.issue31093F(C.ushort(0)) +} + +// issue 32579 + +func test32579(t *testing.T) { + var s [1]C.struct_S32579 + C.memset(unsafe.Pointer(&s[0].data[0]), 1, 1) + if s[0].data[0] != 1 { + t.Errorf("&s[0].data[0] failed: got %d, want %d", s[0].data[0], 1) + } +} diff --git a/libgo/misc/cgo/test/test22906.go b/libgo/misc/cgo/test/test22906.go deleted file mode 100644 index 02bae9c..0000000 --- a/libgo/misc/cgo/test/test22906.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2017 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 cgo - -package cgotest - -/* - -// It's going to be hard to include a whole real JVM to test this. -// So we'll simulate a really easy JVM using just the parts we need. - -// This is the relevant part of jni.h. - -struct _jobject; - -typedef struct _jobject *jobject; -typedef jobject jclass; -typedef jobject jthrowable; -typedef jobject jstring; -typedef jobject jarray; -typedef jarray jbooleanArray; -typedef jarray jbyteArray; -typedef jarray jcharArray; -typedef jarray jshortArray; -typedef jarray jintArray; -typedef jarray jlongArray; -typedef jarray jfloatArray; -typedef jarray jdoubleArray; -typedef jarray jobjectArray; - -typedef jobject jweak; - -// Note: jvalue is already a non-pointer type due to it being a C union. - -*/ -import "C" -import ( - "testing" -) - -func test22906(t *testing.T) { - var x1 C.jobject = 0 // Note: 0, not nil. That makes sure we use uintptr for these types. - _ = x1 - var x2 C.jclass = 0 - _ = x2 - var x3 C.jthrowable = 0 - _ = x3 - var x4 C.jstring = 0 - _ = x4 - var x5 C.jarray = 0 - _ = x5 - var x6 C.jbooleanArray = 0 - _ = x6 - var x7 C.jbyteArray = 0 - _ = x7 - var x8 C.jcharArray = 0 - _ = x8 - var x9 C.jshortArray = 0 - _ = x9 - var x10 C.jintArray = 0 - _ = x10 - var x11 C.jlongArray = 0 - _ = x11 - var x12 C.jfloatArray = 0 - _ = x12 - var x13 C.jdoubleArray = 0 - _ = x13 - var x14 C.jobjectArray = 0 - _ = x14 - var x15 C.jweak = 0 - _ = x15 -} diff --git a/libgo/misc/cgo/test/test27660.go b/libgo/misc/cgo/test/test27660.go deleted file mode 100644 index 0345aa7..0000000 --- a/libgo/misc/cgo/test/test27660.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2018 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. - -// Stress the interaction between the race detector and cgo in an -// attempt to reproduce the memory corruption described in #27660. -// The bug was very timing sensitive; at the time of writing this -// test would only trigger the bug about once out of every five runs. - -package cgotest - -// #include <unistd.h> -import "C" - -import ( - "context" - "math/rand" - "runtime" - "sync" - "testing" - "time" -) - -func test27660(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - ints := make([]int, 100) - locks := make([]sync.Mutex, 100) - // Slowly create threads so that ThreadSanitizer is forced to - // frequently resize its SyncClocks. - for i := 0; i < 100; i++ { - go func() { - for ctx.Err() == nil { - // Sleep in C for long enough that it is likely that the runtime - // will retake this goroutine's currently wired P. - C.usleep(1000 /* 1ms */) - runtime.Gosched() // avoid starvation (see #28701) - } - }() - go func() { - // Trigger lots of synchronization and memory reads/writes to - // increase the likelihood that the race described in #27660 - // results in corruption of ThreadSanitizer's internal state - // and thus an assertion failure or segfault. - i := 0 - for ctx.Err() == nil { - j := rand.Intn(100) - locks[j].Lock() - ints[j]++ - locks[j].Unlock() - // Avoid making the loop unpreemptible - // for gccgo. - if i%0x1000000 == 0 { - runtime.Gosched() - } - i++ - } - }() - time.Sleep(time.Millisecond) - } -} diff --git a/libgo/misc/cgo/test/test_unix.go b/libgo/misc/cgo/test/test_unix.go new file mode 100644 index 0000000..4a23446 --- /dev/null +++ b/libgo/misc/cgo/test/test_unix.go @@ -0,0 +1,11 @@ +// Copyright 2019 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 "syscall" + +var syscall_dot_SIGCHLD = syscall.SIGCHLD diff --git a/libgo/misc/cgo/test/issue13402.go b/libgo/misc/cgo/test/test_windows.go index 3af24c2..7bfb33a 100644 --- a/libgo/misc/cgo/test/issue13402.go +++ b/libgo/misc/cgo/test/test_windows.go @@ -1,10 +1,9 @@ -// Copyright 2015 The Go Authors. All rights reserved. +// Copyright 2019 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" +import "syscall" -var _ C.complexfloat -var _ C.complexdouble +var syscall_dot_SIGCHLD syscall.Signal diff --git a/libgo/misc/cgo/test/issue5337w.go b/libgo/misc/cgo/test/testdata/cgo_linux_test.go index 7b46757..5cef09f 100644 --- a/libgo/misc/cgo/test/issue5337w.go +++ b/libgo/misc/cgo/test/testdata/cgo_linux_test.go @@ -1,11 +1,9 @@ -// Copyright 2013 The Go Authors. All rights reserved. +// 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 test5337(t *testing.T) {} +func Test9400(t *testing.T) { test9400(t) } diff --git a/libgo/misc/cgo/test/testdata/cgo_test.go b/libgo/misc/cgo/test/testdata/cgo_test.go new file mode 100644 index 0000000..ffa076f --- /dev/null +++ b/libgo/misc/cgo/test/testdata/cgo_test.go @@ -0,0 +1,18 @@ +// 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 + +import "testing" + +// 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. + +func Test8756(t *testing.T) { test8756(t) } +func Test9026(t *testing.T) { test9026(t) } +func Test9510(t *testing.T) { test9510(t) } +func Test20266(t *testing.T) { test20266(t) } +func Test26213(t *testing.T) { test26213(t) } +func TestGCC68255(t *testing.T) { testGCC68255(t) } diff --git a/libgo/misc/cgo/test/gcc68255.go b/libgo/misc/cgo/test/testdata/gcc68255.go index 23e103d..b431462 100644 --- a/libgo/misc/cgo/test/gcc68255.go +++ b/libgo/misc/cgo/test/testdata/gcc68255.go @@ -7,7 +7,7 @@ package cgotest import ( "testing" - "./gcc68255" + "cgotest/gcc68255" ) func testGCC68255(t *testing.T) { diff --git a/libgo/misc/cgo/test/gcc68255/a.go b/libgo/misc/cgo/test/testdata/gcc68255/a.go index e106dee..e106dee 100644 --- a/libgo/misc/cgo/test/gcc68255/a.go +++ b/libgo/misc/cgo/test/testdata/gcc68255/a.go diff --git a/libgo/misc/cgo/test/gcc68255/c.c b/libgo/misc/cgo/test/testdata/gcc68255/c.c index a4fe193..a4fe193 100644 --- a/libgo/misc/cgo/test/gcc68255/c.c +++ b/libgo/misc/cgo/test/testdata/gcc68255/c.c diff --git a/libgo/misc/cgo/test/gcc68255/c.h b/libgo/misc/cgo/test/testdata/gcc68255/c.h index 05ecd81..05ecd81 100644 --- a/libgo/misc/cgo/test/gcc68255/c.h +++ b/libgo/misc/cgo/test/testdata/gcc68255/c.h diff --git a/libgo/misc/cgo/test/issue20266.go b/libgo/misc/cgo/test/testdata/issue20266.go index 9f95086..9f95086 100644 --- a/libgo/misc/cgo/test/issue20266.go +++ b/libgo/misc/cgo/test/testdata/issue20266.go diff --git a/libgo/misc/cgo/test/issue20266/issue20266.h b/libgo/misc/cgo/test/testdata/issue20266/issue20266.h index 8d3258e..8d3258e 100644 --- a/libgo/misc/cgo/test/issue20266/issue20266.h +++ b/libgo/misc/cgo/test/testdata/issue20266/issue20266.h diff --git a/libgo/misc/cgo/test/issue23555.go b/libgo/misc/cgo/test/testdata/issue23555.go index 5fa44e6..4e944b5 100644 --- a/libgo/misc/cgo/test/issue23555.go +++ b/libgo/misc/cgo/test/testdata/issue23555.go @@ -7,5 +7,5 @@ package cgotest -import _ "./issue23555a" -import _ "./issue23555b" +import _ "cgotest/issue23555a" +import _ "cgotest/issue23555b" diff --git a/libgo/misc/cgo/test/issue23555a/a.go b/libgo/misc/cgo/test/testdata/issue23555a/a.go index cb6626b..cb6626b 100644 --- a/libgo/misc/cgo/test/issue23555a/a.go +++ b/libgo/misc/cgo/test/testdata/issue23555a/a.go diff --git a/libgo/misc/cgo/test/issue23555b/a.go b/libgo/misc/cgo/test/testdata/issue23555b/a.go index cb6626b..cb6626b 100644 --- a/libgo/misc/cgo/test/issue23555b/a.go +++ b/libgo/misc/cgo/test/testdata/issue23555b/a.go diff --git a/libgo/misc/cgo/test/issue24161_darwin_test.go b/libgo/misc/cgo/test/testdata/issue24161_darwin_test.go index 48072ff..64f4442 100644 --- a/libgo/misc/cgo/test/issue24161_darwin_test.go +++ b/libgo/misc/cgo/test/testdata/issue24161_darwin_test.go @@ -15,11 +15,11 @@ package cgotest import ( "testing" - "./issue24161arg" - "./issue24161e0" - "./issue24161e1" - "./issue24161e2" - "./issue24161res" + "cgotest/issue24161arg" + "cgotest/issue24161e0" + "cgotest/issue24161e1" + "cgotest/issue24161e2" + "cgotest/issue24161res" ) func Test24161Arg(t *testing.T) { diff --git a/libgo/misc/cgo/test/issue24161arg/def.go b/libgo/misc/cgo/test/testdata/issue24161arg/def.go index d33479a..d33479a 100644 --- a/libgo/misc/cgo/test/issue24161arg/def.go +++ b/libgo/misc/cgo/test/testdata/issue24161arg/def.go diff --git a/libgo/misc/cgo/test/issue24161arg/use.go b/libgo/misc/cgo/test/testdata/issue24161arg/use.go index 3e74944..3e74944 100644 --- a/libgo/misc/cgo/test/issue24161arg/use.go +++ b/libgo/misc/cgo/test/testdata/issue24161arg/use.go diff --git a/libgo/misc/cgo/test/issue24161e0/main.go b/libgo/misc/cgo/test/testdata/issue24161e0/main.go index cbc1dee..cbc1dee 100644 --- a/libgo/misc/cgo/test/issue24161e0/main.go +++ b/libgo/misc/cgo/test/testdata/issue24161e0/main.go diff --git a/libgo/misc/cgo/test/issue24161e1/main.go b/libgo/misc/cgo/test/testdata/issue24161e1/main.go index eb48fc0..eb48fc0 100644 --- a/libgo/misc/cgo/test/issue24161e1/main.go +++ b/libgo/misc/cgo/test/testdata/issue24161e1/main.go diff --git a/libgo/misc/cgo/test/issue24161e2/main.go b/libgo/misc/cgo/test/testdata/issue24161e2/main.go index 1951c86..1951c86 100644 --- a/libgo/misc/cgo/test/issue24161e2/main.go +++ b/libgo/misc/cgo/test/testdata/issue24161e2/main.go diff --git a/libgo/misc/cgo/test/issue24161res/restype.go b/libgo/misc/cgo/test/testdata/issue24161res/restype.go index e5719f2..e5719f2 100644 --- a/libgo/misc/cgo/test/issue24161res/restype.go +++ b/libgo/misc/cgo/test/testdata/issue24161res/restype.go diff --git a/libgo/misc/cgo/test/issue26213/jni.h b/libgo/misc/cgo/test/testdata/issue26213/jni.h index 0c76979..0c76979 100644 --- a/libgo/misc/cgo/test/issue26213/jni.h +++ b/libgo/misc/cgo/test/testdata/issue26213/jni.h diff --git a/libgo/misc/cgo/test/issue26213/test26213.go b/libgo/misc/cgo/test/testdata/issue26213/test26213.go index 5d1f637..5d1f637 100644 --- a/libgo/misc/cgo/test/issue26213/test26213.go +++ b/libgo/misc/cgo/test/testdata/issue26213/test26213.go diff --git a/libgo/misc/cgo/test/issue26430.go b/libgo/misc/cgo/test/testdata/issue26430.go index 3ad5420..14c7a7c 100644 --- a/libgo/misc/cgo/test/issue26430.go +++ b/libgo/misc/cgo/test/testdata/issue26430.go @@ -7,4 +7,4 @@ package cgotest -import _ "./issue26430" +import _ "cgotest/issue26430" diff --git a/libgo/misc/cgo/test/issue26430/a.go b/libgo/misc/cgo/test/testdata/issue26430/a.go index fbaa46b..fbaa46b 100644 --- a/libgo/misc/cgo/test/issue26430/a.go +++ b/libgo/misc/cgo/test/testdata/issue26430/a.go diff --git a/libgo/misc/cgo/test/issue26430/b.go b/libgo/misc/cgo/test/testdata/issue26430/b.go index a7c527c..a7c527c 100644 --- a/libgo/misc/cgo/test/issue26430/b.go +++ b/libgo/misc/cgo/test/testdata/issue26430/b.go diff --git a/libgo/misc/cgo/test/issue26743.go b/libgo/misc/cgo/test/testdata/issue26743.go index 35c8473..000fb2b 100644 --- a/libgo/misc/cgo/test/issue26743.go +++ b/libgo/misc/cgo/test/testdata/issue26743.go @@ -7,4 +7,4 @@ package cgotest -import _ "./issue26743" +import _ "cgotest/issue26743" diff --git a/libgo/misc/cgo/test/issue26743/a.go b/libgo/misc/cgo/test/testdata/issue26743/a.go index a3df179..a3df179 100644 --- a/libgo/misc/cgo/test/issue26743/a.go +++ b/libgo/misc/cgo/test/testdata/issue26743/a.go diff --git a/libgo/misc/cgo/test/issue26743/b.go b/libgo/misc/cgo/test/testdata/issue26743/b.go index c5f1ae4..c5f1ae4 100644 --- a/libgo/misc/cgo/test/issue26743/b.go +++ b/libgo/misc/cgo/test/testdata/issue26743/b.go diff --git a/libgo/misc/cgo/test/issue27054/egl.h b/libgo/misc/cgo/test/testdata/issue27054/egl.h index 33a759e..33a759e 100644 --- a/libgo/misc/cgo/test/issue27054/egl.h +++ b/libgo/misc/cgo/test/testdata/issue27054/egl.h diff --git a/libgo/misc/cgo/test/issue27054/test27054.go b/libgo/misc/cgo/test/testdata/issue27054/test27054.go index 186f5bd..186f5bd 100644 --- a/libgo/misc/cgo/test/issue27054/test27054.go +++ b/libgo/misc/cgo/test/testdata/issue27054/test27054.go diff --git a/libgo/misc/cgo/test/issue27340.go b/libgo/misc/cgo/test/testdata/issue27340.go index f8c8a87..337550f 100644 --- a/libgo/misc/cgo/test/issue27340.go +++ b/libgo/misc/cgo/test/testdata/issue27340.go @@ -7,6 +7,6 @@ package cgotest -import "./issue27340" +import "cgotest/issue27340" var issue27340Var = issue27340.Issue27340GoFunc diff --git a/libgo/misc/cgo/test/issue27340/a.go b/libgo/misc/cgo/test/testdata/issue27340/a.go index f5b120c..f5b120c 100644 --- a/libgo/misc/cgo/test/issue27340/a.go +++ b/libgo/misc/cgo/test/testdata/issue27340/a.go diff --git a/libgo/misc/cgo/test/issue21668.go b/libgo/misc/cgo/test/testdata/issue29563.go index f15b920..84def3c 100644 --- a/libgo/misc/cgo/test/issue21668.go +++ b/libgo/misc/cgo/test/testdata/issue29563.go @@ -1,13 +1,12 @@ -// Copyright 2017 The Go Authors. All rights reserved. +// Copyright 2019 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. -// Fail to guess the kind of the constant "x". +// +build !windows + +// Issue 29563: internal linker fails on duplicate weak symbols. // No runtime test; just make sure it compiles. package cgotest -// const int x = 42; -import "C" - -var issue21668_X = C.x +import _ "cgotest/issue29563" diff --git a/libgo/misc/cgo/test/testdata/issue29563/weak.go b/libgo/misc/cgo/test/testdata/issue29563/weak.go new file mode 100644 index 0000000..21cf635c --- /dev/null +++ b/libgo/misc/cgo/test/testdata/issue29563/weak.go @@ -0,0 +1,13 @@ +// Copyright 2019 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 issue29563 + +//int foo1(); +//int foo2(); +import "C" + +func Bar() int { + return int(C.foo1()) + int(C.foo2()) +} diff --git a/libgo/misc/cgo/test/testdata/issue29563/weak1.c b/libgo/misc/cgo/test/testdata/issue29563/weak1.c new file mode 100644 index 0000000..86a2273 --- /dev/null +++ b/libgo/misc/cgo/test/testdata/issue29563/weak1.c @@ -0,0 +1,11 @@ +// Copyright 2019 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. + +extern int weaksym __attribute__((__weak__)); +int weaksym = 42; + +int foo1() +{ + return weaksym; +} diff --git a/libgo/misc/cgo/test/testdata/issue29563/weak2.c b/libgo/misc/cgo/test/testdata/issue29563/weak2.c new file mode 100644 index 0000000..e01eae8 --- /dev/null +++ b/libgo/misc/cgo/test/testdata/issue29563/weak2.c @@ -0,0 +1,11 @@ +// Copyright 2019 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. + +extern int weaksym __attribute__((__weak__)); +int weaksym = 42; + +int foo2() +{ + return weaksym; +} diff --git a/libgo/misc/cgo/test/issue8756.go b/libgo/misc/cgo/test/testdata/issue8756.go index d8ee3b8..406c64c 100644 --- a/libgo/misc/cgo/test/issue8756.go +++ b/libgo/misc/cgo/test/testdata/issue8756.go @@ -8,7 +8,7 @@ import "C" import ( "testing" - "./issue8756" + "cgotest/issue8756" ) func test8756(t *testing.T) { diff --git a/libgo/misc/cgo/test/issue8756/issue8756.go b/libgo/misc/cgo/test/testdata/issue8756/issue8756.go index 5f6b777..5f6b777 100644 --- a/libgo/misc/cgo/test/issue8756/issue8756.go +++ b/libgo/misc/cgo/test/testdata/issue8756/issue8756.go diff --git a/libgo/misc/cgo/test/issue8828.go b/libgo/misc/cgo/test/testdata/issue8828.go index 304797c..0bca0f2 100644 --- a/libgo/misc/cgo/test/issue8828.go +++ b/libgo/misc/cgo/test/testdata/issue8828.go @@ -9,7 +9,7 @@ package cgotest -import "./issue8828" +import "cgotest/issue8828" func p() { issue8828.Bar() diff --git a/libgo/misc/cgo/test/issue8828/issue8828.c b/libgo/misc/cgo/test/testdata/issue8828/issue8828.c index 27ec23a..27ec23a 100644 --- a/libgo/misc/cgo/test/issue8828/issue8828.c +++ b/libgo/misc/cgo/test/testdata/issue8828/issue8828.c diff --git a/libgo/misc/cgo/test/issue8828/trivial.go b/libgo/misc/cgo/test/testdata/issue8828/trivial.go index e7b9a4e..e7b9a4e 100644 --- a/libgo/misc/cgo/test/issue8828/trivial.go +++ b/libgo/misc/cgo/test/testdata/issue8828/trivial.go diff --git a/libgo/misc/cgo/test/issue9026.go b/libgo/misc/cgo/test/testdata/issue9026.go index 8848d0e..3f48881 100644 --- a/libgo/misc/cgo/test/issue9026.go +++ b/libgo/misc/cgo/test/testdata/issue9026.go @@ -3,7 +3,7 @@ package cgotest import ( "testing" - "./issue9026" + "cgotest/issue9026" ) func test9026(t *testing.T) { issue9026.Test(t) } diff --git a/libgo/misc/cgo/test/issue9026/issue9026.go b/libgo/misc/cgo/test/testdata/issue9026/issue9026.go index 149c265..149c265 100644 --- a/libgo/misc/cgo/test/issue9026/issue9026.go +++ b/libgo/misc/cgo/test/testdata/issue9026/issue9026.go diff --git a/libgo/misc/cgo/test/issue9400/asm_386.s b/libgo/misc/cgo/test/testdata/issue9400/asm_386.s index 7f158b5..7f158b5 100644 --- a/libgo/misc/cgo/test/issue9400/asm_386.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_386.s diff --git a/libgo/misc/cgo/test/issue9400/asm_amd64x.s b/libgo/misc/cgo/test/testdata/issue9400/asm_amd64x.s index 48b8619..48b8619 100644 --- a/libgo/misc/cgo/test/issue9400/asm_amd64x.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_amd64x.s diff --git a/libgo/misc/cgo/test/issue9400/asm_arm.s b/libgo/misc/cgo/test/testdata/issue9400/asm_arm.s index 96c2785..96c2785 100644 --- a/libgo/misc/cgo/test/issue9400/asm_arm.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_arm.s diff --git a/libgo/misc/cgo/test/issue9400/asm_arm64.s b/libgo/misc/cgo/test/testdata/issue9400/asm_arm64.s index 2ebbfcc..2ebbfcc 100644 --- a/libgo/misc/cgo/test/issue9400/asm_arm64.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_arm64.s diff --git a/libgo/misc/cgo/test/issue9400/asm_mips64x.s b/libgo/misc/cgo/test/testdata/issue9400/asm_mips64x.s index 63dc906..63dc906 100644 --- a/libgo/misc/cgo/test/issue9400/asm_mips64x.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_mips64x.s diff --git a/libgo/misc/cgo/test/issue9400/asm_mipsx.s b/libgo/misc/cgo/test/testdata/issue9400/asm_mipsx.s index 7a92735..7a92735 100644 --- a/libgo/misc/cgo/test/issue9400/asm_mipsx.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_mipsx.s diff --git a/libgo/misc/cgo/test/issue9400/asm_ppc64x.s b/libgo/misc/cgo/test/testdata/issue9400/asm_ppc64x.s index c88ec3b..c88ec3b 100644 --- a/libgo/misc/cgo/test/issue9400/asm_ppc64x.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_ppc64x.s diff --git a/libgo/misc/cgo/test/issue9400/asm_s390x.s b/libgo/misc/cgo/test/testdata/issue9400/asm_s390x.s index fc9ad72..fc9ad72 100644 --- a/libgo/misc/cgo/test/issue9400/asm_s390x.s +++ b/libgo/misc/cgo/test/testdata/issue9400/asm_s390x.s diff --git a/libgo/misc/cgo/test/issue9400/gccgo.go b/libgo/misc/cgo/test/testdata/issue9400/gccgo.go index a9b62b0..a9b62b0 100644 --- a/libgo/misc/cgo/test/issue9400/gccgo.go +++ b/libgo/misc/cgo/test/testdata/issue9400/gccgo.go diff --git a/libgo/misc/cgo/test/issue9400/stubs.go b/libgo/misc/cgo/test/testdata/issue9400/stubs.go index e431c5a..e431c5a 100644 --- a/libgo/misc/cgo/test/issue9400/stubs.go +++ b/libgo/misc/cgo/test/testdata/issue9400/stubs.go diff --git a/libgo/misc/cgo/test/issue9400_linux.go b/libgo/misc/cgo/test/testdata/issue9400_linux.go index 7719535..e94a9bb 100644 --- a/libgo/misc/cgo/test/issue9400_linux.go +++ b/libgo/misc/cgo/test/testdata/issue9400_linux.go @@ -18,7 +18,7 @@ import ( "sync/atomic" "testing" - "./issue9400" + "cgotest/issue9400" ) func test9400(t *testing.T) { diff --git a/libgo/misc/cgo/test/issue9510.go b/libgo/misc/cgo/test/testdata/issue9510.go index efd3f77..2c79fab 100644 --- a/libgo/misc/cgo/test/issue9510.go +++ b/libgo/misc/cgo/test/testdata/issue9510.go @@ -11,8 +11,8 @@ import ( "runtime" "testing" - "./issue9510a" - "./issue9510b" + "cgotest/issue9510a" + "cgotest/issue9510b" ) func test9510(t *testing.T) { diff --git a/libgo/misc/cgo/test/issue9510a/a.go b/libgo/misc/cgo/test/testdata/issue9510a/a.go index 1a5224b..1a5224b 100644 --- a/libgo/misc/cgo/test/issue9510a/a.go +++ b/libgo/misc/cgo/test/testdata/issue9510a/a.go diff --git a/libgo/misc/cgo/test/issue9510b/b.go b/libgo/misc/cgo/test/testdata/issue9510b/b.go index 5016b39..5016b39 100644 --- a/libgo/misc/cgo/test/issue9510b/b.go +++ b/libgo/misc/cgo/test/testdata/issue9510b/b.go diff --git a/libgo/misc/cgo/test/test26213.go b/libgo/misc/cgo/test/testdata/test26213.go index 176a7ec..c80032c 100644 --- a/libgo/misc/cgo/test/test26213.go +++ b/libgo/misc/cgo/test/testdata/test26213.go @@ -7,7 +7,7 @@ package cgotest import ( "testing" - "./issue26213" + "cgotest/issue26213" ) func test26213(t *testing.T) { diff --git a/libgo/misc/cgo/test/testx.go b/libgo/misc/cgo/test/testx.go new file mode 100644 index 0000000..67def90 --- /dev/null +++ b/libgo/misc/cgo/test/testx.go @@ -0,0 +1,519 @@ +// 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. + +// Test cases for cgo. +// Both the import "C" prologue and the main file are sorted by issue number. +// This file contains //export directives on Go functions +// and so it must NOT contain C definitions (only declarations). +// See test.go for C definitions. + +package cgotest + +import ( + "runtime" + "runtime/debug" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + "unsafe" +) + +/* +// threads +extern void doAdd(int, int); + +// issue 1328 +extern void BackIntoGo(void); +void IntoC(void); + +// issue 1560 +// mysleep returns the absolute start time in ms. +long long mysleep(int seconds); + +// twoSleep returns the absolute start time of the first sleep +// in ms. +long long twoSleep(int); + +// issue 3775 +void lockOSThreadCallback(void); +inline static void lockOSThreadC(void) +{ + lockOSThreadCallback(); +} +int usleep(unsigned usec); + +// issue 4054 part 2 - part 1 in test.go +typedef enum { + A = 0, + B, + C, + D, + E, + F, + G, + H, + II, + J, +} issue4054b; + +// issue 5548 + +extern int issue5548_in_c(void); + +// issue 6833 + +extern unsigned long long issue6833Func(unsigned int, unsigned long long); + +// issue 6907 + +extern int CheckIssue6907C(_GoString_); + +// issue 7665 + +extern void f7665(void); + +// issue 7978 +// Stack tracing didn't work during cgo code after calling a Go +// callback. Make sure GC works and the stack trace is correct. + +#include <stdint.h> + +void issue7978cb(void); + +// use ugly atomic variable sync since that doesn't require calling back into +// Go code or OS dependencies +static void issue7978c(uint32_t *sync) { + while(__atomic_load_n(sync, __ATOMIC_SEQ_CST) != 0) + ; + __atomic_add_fetch(sync, 1, __ATOMIC_SEQ_CST); + while(__atomic_load_n(sync, __ATOMIC_SEQ_CST) != 2) + ; + issue7978cb(); + __atomic_add_fetch(sync, 1, __ATOMIC_SEQ_CST); + while(__atomic_load_n(sync, __ATOMIC_SEQ_CST) != 6) + ; +} + +// issue 8331 part 2 - part 1 in test.go +// A typedef of an unnamed struct is the same struct when +// #include'd twice. No runtime test; just make sure it compiles. +#include "issue8331.h" + +// issue 20910 +void callMulti(void); + +// issue 28772 part 2 - part 1 in issuex.go +#define issue28772Constant2 2 + +*/ +import "C" + +// exports + +//export ReturnIntLong +func ReturnIntLong() (int, C.long) { + return 1, 2 +} + +//export gc +func gc() { + runtime.GC() +} + +// threads + +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.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { + t.Skip("the iOS exec wrapper is unable to properly handle the panic from Add") + } + sum.i = 0 + C.doAdd(10, 6) + + want := 10 * (10 - 1) / 2 * 6 + if sum.i != want { + t.Fatalf("sum=%d, want %d", sum.i, want) + } +} + +// issue 1328 + +//export BackIntoGo +func BackIntoGo() { + x := 1 + + for i := 0; i < 10000; i++ { + xvariadic(x) + if x != 1 { + panic("x is not 1?") + } + } +} + +func xvariadic(x ...interface{}) { +} + +func test1328(t *testing.T) { + C.IntoC() +} + +// issue 1560 + +var sleepDone = make(chan int64) + +// parallelSleep returns the absolute difference between the start time +// of the two sleeps. +func parallelSleep(n int) int64 { + t := int64(C.twoSleep(C.int(n))) - <-sleepDone + if t < 0 { + return -t + } + return t +} + +//export BackgroundSleep +func BackgroundSleep(n int32) { + go func() { + sleepDone <- int64(C.mysleep(C.int(n))) + }() +} + +func testParallelSleep(t *testing.T) { + sleepSec := 1 + dt := time.Duration(parallelSleep(sleepSec)) * time.Millisecond + t.Logf("difference in start time for two sleep(%d) is %v", sleepSec, dt) + // bug used to run sleeps in serial, producing a 2*sleepSec-second delay. + // we detect if the start times of those sleeps are > 0.5*sleepSec-second. + if dt >= time.Duration(sleepSec)*time.Second/2 { + t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds()) + } +} + +// issue 2462 + +//export exportbyte +func exportbyte() byte { + return 0 +} + +//export exportbool +func exportbool() bool { + return false +} + +//export exportrune +func exportrune() rune { + return 0 +} + +//export exporterror +func exporterror() error { + return nil +} + +//export exportint +func exportint() int { + return 0 +} + +//export exportuint +func exportuint() uint { + return 0 +} + +//export exportuintptr +func exportuintptr() uintptr { + return (uintptr)(0) +} + +//export exportint8 +func exportint8() int8 { + return 0 +} + +//export exportuint8 +func exportuint8() uint8 { + return 0 +} + +//export exportint16 +func exportint16() int16 { + return 0 +} + +//export exportuint16 +func exportuint16() uint16 { + return 0 +} + +//export exportint32 +func exportint32() int32 { + return 0 +} + +//export exportuint32 +func exportuint32() uint32 { + return 0 +} + +//export exportint64 +func exportint64() int64 { + return 0 +} + +//export exportuint64 +func exportuint64() uint64 { + return 0 +} + +//export exportfloat32 +func exportfloat32() float32 { + return 0 +} + +//export exportfloat64 +func exportfloat64() float64 { + return 0 +} + +//export exportcomplex64 +func exportcomplex64() complex64 { + return 0 +} + +//export exportcomplex128 +func exportcomplex128() complex128 { + return 0 +} + +// issue 3741 + +//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 +} + +// issue 3775 + +func init() { + if runtime.GOOS == "android" { + return + } + // Same as test3775 but run during init so that + // there are two levels of internal runtime lock + // (1 for init, 1 for cgo). + // This would have been broken by CL 11663043. + C.lockOSThreadC() +} + +func test3775(t *testing.T) { + if runtime.GOOS == "android" { + return + } + // Used to panic because of the UnlockOSThread below. + C.lockOSThreadC() +} + +//export lockOSThreadCallback +func lockOSThreadCallback() { + runtime.LockOSThread() + runtime.UnlockOSThread() + go C.usleep(10000) + runtime.Gosched() +} + +// issue 4054 part 2 - part 1 in test.go + +var issue4054b = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.II, C.J} + +//export issue5548FromC +func issue5548FromC(s string, i int) int { + if len(s) == 4 && s == "test" && i == 42 { + return 12345 + } + println("got", len(s), i) + return 9876 +} + +func test5548(t *testing.T) { + if x := C.issue5548_in_c(); x != 12345 { + t.Errorf("issue5548_in_c = %d, want %d", x, 12345) + } +} + +// issue 6833 + +//export GoIssue6833Func +func GoIssue6833Func(aui uint, aui64 uint64) uint64 { + return aui64 + uint64(aui) +} + +func test6833(t *testing.T) { + ui := 7 + ull := uint64(0x4000300020001000) + v := uint64(C.issue6833Func(C.uint(ui), C.ulonglong(ull))) + exp := uint64(ui) + ull + if v != exp { + t.Errorf("issue6833Func() returns %x, expected %x", v, exp) + } +} + +// issue 6907 + +const CString = "C string" + +//export CheckIssue6907Go +func CheckIssue6907Go(s string) C.int { + if s == CString { + return 1 + } + return 0 +} + +func test6907Go(t *testing.T) { + if got := C.CheckIssue6907C(CString); got != 1 { + t.Errorf("C.CheckIssue6907C() == %d, want %d", got, 1) + } +} + +// issue 7665 + +//export f7665 +func f7665() {} + +var bad7665 unsafe.Pointer = C.f7665 +var good7665 uintptr = uintptr(C.f7665) + +func test7665(t *testing.T) { + if bad7665 == nil || uintptr(bad7665) != good7665 { + t.Errorf("ptrs = %p, %#x, want same non-nil pointer", bad7665, good7665) + } +} + +// issue 7978 + +var issue7978sync uint32 + +func issue7978check(t *testing.T, wantFunc string, badFunc string, depth int) { + runtime.GC() + buf := make([]byte, 65536) + trace := string(buf[:runtime.Stack(buf, true)]) + for _, goroutine := range strings.Split(trace, "\n\n") { + if strings.Contains(goroutine, "test.issue7978go") { + trace := strings.Split(goroutine, "\n") + // look for the expected function in the stack + for i := 0; i < depth; i++ { + if badFunc != "" && strings.Contains(trace[1+2*i], badFunc) { + t.Errorf("bad stack: found %s in the stack:\n%s", badFunc, goroutine) + return + } + if strings.Contains(trace[1+2*i], wantFunc) { + return + } + } + t.Errorf("bad stack: didn't find %s in the stack:\n%s", wantFunc, goroutine) + return + } + } + t.Errorf("bad stack: goroutine not found. Full stack dump:\n%s", trace) +} + +func issue7978wait(store uint32, wait uint32) { + if store != 0 { + atomic.StoreUint32(&issue7978sync, store) + } + for atomic.LoadUint32(&issue7978sync) != wait { + runtime.Gosched() + } +} + +//export issue7978cb +func issue7978cb() { + // Force a stack growth from the callback to put extra + // pressure on the runtime. See issue #17785. + growStack(64) + issue7978wait(3, 4) +} + +func growStack(n int) int { + var buf [128]int + if n == 0 { + return 0 + } + return buf[growStack(n-1)] +} + +func issue7978go() { + C.issue7978c((*C.uint32_t)(&issue7978sync)) + issue7978wait(7, 8) +} + +func test7978(t *testing.T) { + if runtime.Compiler == "gccgo" { + t.Skip("gccgo can not do stack traces of C code") + } + debug.SetTraceback("2") + issue7978sync = 0 + go issue7978go() + // test in c code, before callback + issue7978wait(0, 1) + issue7978check(t, "_Cfunc_issue7978c(", "", 1) + // test in go code, during callback + issue7978wait(2, 3) + issue7978check(t, "test.issue7978cb(", "test.issue7978go", 3) + // test in c code, after callback + issue7978wait(4, 5) + issue7978check(t, "_Cfunc_issue7978c(", "_cgoexpwrap", 1) + // test in go code, after return from cgo + issue7978wait(6, 7) + issue7978check(t, "test.issue7978go(", "", 3) + atomic.StoreUint32(&issue7978sync, 8) +} + +// issue 8331 part 2 + +var issue8331Var C.issue8331 + +// issue 20910 + +//export multi +func multi() (*C.char, C.int) { + return C.CString("multi"), 0 +} + +func test20910(t *testing.T) { + C.callMulti() +} + +// issue 28772 part 2 + +const issue28772Constant2 = C.issue28772Constant2 diff --git a/libgo/misc/cgo/test/twoargs.go b/libgo/misc/cgo/test/twoargs.go deleted file mode 100644 index ca0534c..0000000 --- a/libgo/misc/cgo/test/twoargs.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018 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. - -// Crash from call with two arguments that need pointer checking. -// No runtime test; just make sure it compiles. - -package cgotest - -/* -static void twoargs1(void *p, int n) {} -static void *twoargs2() { return 0; } -static int twoargs3(void * p) { return 0; } -*/ -import "C" - -import "unsafe" - -func twoargsF() { - v := []string{} - C.twoargs1(C.twoargs2(), C.twoargs3(unsafe.Pointer(&v))) -} |