1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 // Both the import "C" prologue and the main file are sorted by issue number.
7 // This file contains C definitions (not just declarations)
8 // and so it must NOT contain any //export directives on Go functions.
9 // See testx.go for exports.
26 #cgo !darwin LDFLAGS: -lm
35 typedef unsigned char Uint8;
36 typedef unsigned short Uint16;
49 typedef struct SDL_keysym {
56 typedef struct SDL_KeyboardEvent {
63 void makeEvent(SDL_KeyboardEvent *event) {
67 p = (unsigned char*)event;
68 for (i=0; i<sizeof *event; i++) {
73 int same(SDL_KeyboardEvent* e, Uint8 typ, Uint8 which, Uint8 state, Uint8 scan, SDLKey sym, SDLMod mod, Uint16 uni) {
74 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;
77 void cTest(SDL_KeyboardEvent *event) {
78 printf("C: %#x %#x %#x %#x %#x %#x %#x\n", event->typ, event->which, event->state,
79 event->keysym.scancode, event->keysym.sym, event->keysym.mod, event->keysym.unicode);
85 const char *greeting = "hello, world";
89 #define SHIFT(x, y) ((x)<<(y))
90 #define KILO SHIFT(1, 10)
91 #define UINT32VAL 0xc008427bU
98 typedef unsigned char cgo_uuid_t[20];
100 void uuid_generate(cgo_uuid_t x) {
108 const char *cstr = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890";
110 extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter);
112 enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; }
114 int add(int x, int y) {
118 // escape vs noescape
120 #cgo noescape handleGoStringPointerNoescape
121 void handleGoStringPointerNoescape(void *s) {}
123 void handleGoStringPointerEscape(void *s) {}
125 // Following mimics vulkan complex definitions for benchmarking cgocheck overhead.
127 typedef uint32_t VkFlags;
128 typedef VkFlags VkDeviceQueueCreateFlags;
129 typedef uint32_t VkStructureType;
131 typedef struct VkDeviceQueueCreateInfo {
132 VkStructureType sType;
134 VkDeviceQueueCreateFlags flags;
135 uint32_t queueFamilyIndex;
137 const float* pQueuePriorities;
138 } VkDeviceQueueCreateInfo;
140 typedef struct VkPhysicalDeviceFeatures {
142 } VkPhysicalDeviceFeatures;
144 typedef struct VkDeviceCreateInfo {
145 VkStructureType sType;
148 uint32_t queueCreateInfoCount;
149 const VkDeviceQueueCreateInfo* pQueueCreateInfos;
150 uint32_t enabledLayerCount;
151 const char* const* ppEnabledLayerNames;
152 uint32_t enabledExtensionCount;
153 const char* const* ppEnabledExtensionNames;
154 const VkPhysicalDeviceFeatures* pEnabledFeatures;
155 } VkDeviceCreateInfo;
157 void handleComplexPointer(VkDeviceCreateInfo *a0) {}
158 void handleComplexPointer8(
159 VkDeviceCreateInfo *a0, VkDeviceCreateInfo *a1, VkDeviceCreateInfo *a2, VkDeviceCreateInfo *a3,
160 VkDeviceCreateInfo *a4, VkDeviceCreateInfo *a5, VkDeviceCreateInfo *a6, VkDeviceCreateInfo *a7
168 } cplxAlign = { 3.14, 2.17 };
170 // constants and pointer checking
172 #define CheckConstVal 0
178 static void CheckConstFunc(CheckConstStruct *p, int e) {}
183 #define alias_one base_symbol
184 #define alias_two base_symbol
186 // function pointer variables
188 typedef int (*intFunc) ();
191 bridge_int_func(intFunc f)
205 struct ibv_async_event {
211 xxpthread_mutex_t mutex;
215 // Mac OS X's gcc will generate scattered relocation 2/1 for
216 // this function on Darwin/386, and 8l couldn't handle it.
217 // this example is in issue 1635
220 printf("scatter = %p\n", p);
223 // Adding this explicit extern declaration makes this a test for
224 // https://gcc.gnu.org/PR68072 aka https://golang.org/issue/13344 .
225 // It used to cause a cgo error when building with GCC 6.
228 // this example is in issue 3253
230 int testHola() { return hola; }
234 void testSendSIG() {}
236 static void *thread(void *p) {
240 for (i = 0; i < M; i++) {
241 pthread_kill(pthread_self(), SIGCHLD);
242 usleep(rand() % 20 + 5);
250 for (i = 0; i < N; i++) {
251 usleep(rand() % 200 + 100);
252 pthread_create(&tid[i], 0, thread, NULL);
254 for (i = 0; i < N; i++)
255 pthread_join(tid[i], 0);
260 // libgcc on ARM might be compiled as thumb code, but our 5l
261 // can't handle that, so we have to disable this test on arm.
264 puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library.");
265 return (x < 0) ? -x : x;
267 #elif defined(__arm64__) && defined(__clang__)
269 puts("testLibgcc is disabled on ARM64 with clang due to lack of libgcc.");
270 return (x < 0) ? -x : x;
273 int __absvsi2(int); // dummy prototype for libgcc function
274 // we shouldn't name the function abs, as gcc might use
276 int vabs(int x) { return __absvsi2(x); }
281 // access errno from void C function
282 const char _expA = 0x42;
283 const float _expB = 3.14159;
284 const short _expC = 0x55aa;
285 const int _expD = 0xdeadbeef;
289 void g2(int x, char a, float b, short c, int d) {}
296 // try to pass some non-trivial arguments to function g2
297 void g2(int x, char a, float b, short c, int d) {
298 if (a == _expA && b == _expB && c == _expC && d == _expD)
306 // Test that cgo reserves enough stack space during cgo call.
307 // See https://golang.org/issue/3945 for details.
309 printf("%s from C\n", "hello");
312 // issue 4054 part 1 - other half in testx.go
328 // We've historically permitted #include <>, so test it here. Issue 29333.
329 // Also see issue 41059.
330 #include <issue4339.h>
333 // cmd/cgo: bool alignment/padding issue.
334 // bool alignment is wrong and causing wrong arguments when calling functions.
335 static int c_bool(bool a, bool b, int c, bool d, bool e) {
341 const struct { int a; } *issue4857() { return (void *)0; }
344 // Test that the #cgo CFLAGS directive works,
345 // with and without platform filters.
346 #cgo CFLAGS: -DCOMMON_VALUE=123
347 #cgo windows CFLAGS: -DIS_WINDOWS=1
348 #cgo !windows CFLAGS: -DIS_WINDOWS=0
349 int common = COMMON_VALUE;
350 int is_windows = IS_WINDOWS;
353 // linker incorrectly treats common symbols and
354 // leaves them undefined.
360 Fontinfo SansTypeface;
364 Fontinfo loadfont() {
370 SansTypeface = loadfont();
374 // Cgo incorrectly computed the alignment of structs
375 // with no Go accessible fields as 0, and then panicked on
376 // modulo-by-zero computations.
379 // disable arm64 GCC warnings
380 #cgo CFLAGS: -Wno-psabi -Wno-unknown-warning-option
389 int issue5242(foo f, bar b) {
394 // Verify that we can withstand SIGPROF received on foreign threads
399 static void *thread1(void *p) {
401 pthread_kill(pthread_self(), SIGPROF);
406 pthread_create(&tid, 0, thread1, NULL);
407 pthread_join(tid, 0);
413 const long long issue5603exp = 0x12345678;
414 long long issue5603foo0() { return issue5603exp; }
415 long long issue5603foo1(void *p) { return issue5603exp; }
416 long long issue5603foo2(void *p, void *q) { return issue5603exp; }
417 long long issue5603foo3(void *p, void *q, void *r) { return issue5603exp; }
418 long long issue5603foo4(void *p, void *q, void *r, void *s) { return issue5603exp; }
422 int test5740a(void), test5740b(void);
425 static void output5986()
427 int current_row = 0, row_count = 0;
428 double sum_squares = 0;
431 if (current_row == 10) {
436 while (current_row++ != 1);
437 d = sqrt(sum_squares / row_count);
438 printf("sqrt is: %g\n", d);
442 // Test handling of #defined names in clang.
443 // NOTE: Must use hex, or else a shortcut for decimals
444 // in cgo avoids trying to pass this to clang.
457 // Test new scheme for deciding whether C.name is an expression, type, constant.
458 // Clang silences some warnings when the name is a #defined macro, so test those too
459 // (even though we now use errors exclusively, not warnings).
463 const char *mytext = "abcdef";
469 #define myfunc_def myfunc
470 #define myvar_def myvar
471 #define mytext_def mytext
472 #define mytype_def mytype
473 #define myenum_def myenum
474 #define myint_def 12345
475 #define myfloat_def 1.5
476 #define mystring_def "hello"
479 char* Issue6907CopyString(_GoString_ s) {
496 } __attribute__((__packed__)) misaligned;
501 return (uintptr_t)&((misaligned*)0)->y;
505 // No runtime test, just make sure that typedef and struct/union/class are interchangeable at compile time.
508 typedef struct test7786 typedef_test7786;
509 void f7786(struct test7786 *ctx) {}
510 void g7786(typedef_test7786 *ctx) {}
512 typedef struct body7786 typedef_body7786;
513 struct body7786 { int x; };
514 void b7786(struct body7786 *ctx) {}
515 void c7786(typedef_body7786 *ctx) {}
517 typedef union union7786 typedef_union7786;
518 void u7786(union union7786 *ctx) {}
519 void v7786(typedef_union7786 *ctx) {}
522 // Test that linker defined symbols (e.g., text, data) don't
523 // conflict with C symbols.
524 char text[] = "text";
525 char data[] = "data";
526 char *ctext(void) { return text; }
527 char *cdata(void) { return data; }
530 // Cgo inconsistently translated zero size arrays.
532 struct issue8428one {
537 struct issue8428two {
544 struct issue8428three {
551 // issue 8331 part 1 - part 2 in testx.go
552 // A typedef of an unnamed struct is the same struct when
553 // #include'd twice. No runtime test; just make sure it compiles.
554 #include "issue8331.h"
556 // issue 8368 and 8441
557 // Recursive struct definitions didn't work.
558 // No runtime test; just make sure it compiles.
559 typedef struct one one;
560 typedef struct two two;
570 extern int issue8811Initialized;
571 extern void issue8811Init();
573 void issue8811Execute() {
574 if(!issue8811Initialized)
580 typedef void (*PFunc8945)();
587 } test9557bar = { 42 };
588 struct issue9557_t *issue9557foo = &test9557bar;
591 // Pointers passed to C were not marked as escaping (bug in cgo).
595 void setintstar(int *x) {
599 void setintptr(intptr x) {
603 void setvoidptr(void *x) {
607 typedef struct Struct Struct;
612 void setstruct(Struct s) {
617 // Structs with zero-length trailing fields are now padded by the Go compiler.
632 void issue12030conv(char *buf, double x) {
633 sprintf(buf, "d=%g", x);
638 int check_cbytes(char *b, size_t l) {
640 for (i = 0; i < l; i++) {
649 // Test that C symbols larger than a page play nicely with the race detector.
653 // The void* cast introduced by cgo to avoid problems
654 // with const/volatile qualifiers breaks C preprocessor macros that
655 // emulate functions.
661 int I17537(S17537 *p);
663 #define I17537(p) ((p)->i)
665 // Calling this function used to fail without the cast.
666 const int F17537(const char **p) {
671 // API compatibility checks
673 typedef char *cstring_pointer;
674 static void cstring_pointer_fun(cstring_pointer dummy) { }
675 const char *api_hello = "hello!";
677 // Calling this function used to trigger an error from the C compiler
679 void F18298(const void *const *p) {
682 // Test that conversions between typedefs work as they used to.
683 typedef const void *T18298_1;
684 struct S18298 { int i; };
685 typedef const struct S18298 *T18298_2;
686 void G18298(T18298_1 t) {
690 // cgo check of void function returning errno.
691 void Issue18126C(void **p) {}
695 #define HELLO "hello"
696 #define WORLD "world"
697 #define HELLO_WORLD HELLO "\000" WORLD
699 struct foo { char c; };
700 #define SIZE_OF(x) sizeof(x)
701 #define SIZE_OF_FOO SIZE_OF(struct foo)
716 typedef void issue20129Void;
717 issue20129Void issue20129Foo() {
720 typedef issue20129Void issue20129Void2;
721 issue20129Void2 issue20129Bar() {
726 #define XUINT64_MAX 18446744073709551615ULL
729 // Fail to guess the kind of the constant "x".
730 // No runtime test; just make sure it compiles.
731 const int x21668 = 42;
734 #define CAST_TO_INT64 (int64_t)(-1)
737 // Compile C `typedef` to go type aliases.
739 typedef long MySigned_t;
740 // tests alias-to-alias
741 typedef MySigned_t MySigned2_t;
742 long takes_long(long x) { return x * x; }
743 MySigned_t takes_typedef(MySigned_t x) { return x * x; }
747 // It's going to be hard to include a whole real JVM to test this.
748 // So we'll simulate a really easy JVM using just the parts we need.
749 // This is the relevant part of jni.h.
753 typedef struct _jobject *jobject;
754 typedef jobject jclass;
755 typedef jobject jthrowable;
756 typedef jobject jstring;
757 typedef jobject jarray;
758 typedef jarray jbooleanArray;
759 typedef jarray jbyteArray;
760 typedef jarray jcharArray;
761 typedef jarray jshortArray;
762 typedef jarray jintArray;
763 typedef jarray jlongArray;
764 typedef jarray jfloatArray;
765 typedef jarray jdoubleArray;
766 typedef jarray jobjectArray;
768 typedef jobject jweak;
770 // Note: jvalue is already a non-pointer type due to it being a C union.
775 unsigned long long f8 : 8;
776 unsigned long long f16 : 16;
777 unsigned long long f24 : 24;
778 unsigned long long f32 : 32;
779 unsigned long long f40 : 40;
780 unsigned long long f48 : 48;
781 unsigned long long f56 : 56;
782 unsigned long long f64 : 64;
786 int a(void) { return 5; };
787 int r(void) { return 3; };
790 typedef int *issue23720A;
791 typedef const int *issue23720B;
792 void issue23720F(issue23720B a) {}
795 #if defined(__linux__) && defined(__x86_64__)
796 #include <sys/mman.h>
797 // Returns string with null byte at the last valid address
798 char* dangerousString1() {
800 char *data = mmap(0, 2 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
801 mprotect(data + pageSize,pageSize,PROT_NONE);
802 int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
804 for (; i < pageSize; i++) {
807 data[pageSize -1 ] = 0;
811 char* dangerousString2() {
813 char *data = mmap(0, 3 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
814 mprotect(data + 2 * pageSize,pageSize,PROT_NONE);
815 int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
817 for (; i < 2 * pageSize; i++) {
820 data[2*pageSize -1 ] = 0;
824 char *dangerousString1() { return NULL; }
825 char *dangerousString2() { return NULL; }
829 const unsigned long long int issue26066 = (const unsigned long long) -1;
832 // Introduce two pointer types which are distinct, but have the same
833 // base type. Make sure that both of those pointer types get resolved
834 // correctly. Before the fix for 26517 if one of these pointer types
835 // was resolved before the other one was processed, the second one
836 // would never be resolved.
837 // Before this issue was fixed this test failed on Windows,
838 // where va_list expands to a named char* type.
839 typedef va_list TypeOne;
840 typedef char *TypeTwo;
844 static void twoargs1(void *p, int n) {}
845 static void *twoargs2() { return 0; }
846 static int twoargs3(void * p) { return 0; }
849 // Failed to add type conversion for negative constant.
851 static void issue28545F(char **p, int n, complex double a) {}
853 // issue 28772 part 1 - part 2 in testx.go
854 // Failed to add type conversion for Go constant set to C constant.
855 // No runtime test; just make sure it compiles.
857 #define issue28772Constant 1
860 // cgo was incorrectly adding padding after a packed struct.
864 } __attribute__((__packed__)) innerPacked;
881 size_t offset(int x) {
884 return offsetof(innerPacked, f2);
886 return offsetof(outerPacked, g2);
888 return offsetof(innerUnpacked, f2);
890 return offsetof(outerUnpacked, g2);
898 typedef struct { char **p; } S29748;
899 static int f29748(S29748 *p) { return 0; }
902 // Error with newline inserted into constant expression.
903 // Compilation test only, nothing to run.
905 static void issue29781F(char **p, int n) {}
906 #define ISSUE29781C 0
909 static uint16_t issue31093F(uint16_t v) { return v; }
912 typedef struct S32579 { unsigned char data[1]; } S32579;
914 // issue 37033, cgo.Handle
915 extern void GoFunc37033(uintptr_t handle);
916 void cFunc37033(uintptr_t handle) { GoFunc37033(handle); }
919 // Test that #define'd type aliases work.
920 #define netbsd_gid unsigned int
923 // Inconsistent handling of tagged enum and union types.
924 enum Enum40494 { X_40494 };
925 union Union40494 { int x; };
926 void issue40494(enum Enum40494 e, union Union40494* up) {}
928 // Issue 45451, bad handling of go:notinheap types.
929 typedef struct issue45451Undefined issue45451;
931 // Issue 49633, example of cgo.Handle with void*.
932 extern void GoFunc49633(void*);
933 void cfunc49633(void *context) { GoFunc49633(context); }
957 func testAlign(t *testing.T) {
958 var evt C.SDL_KeyboardEvent
960 if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 {
961 t.Error("*** bad alignment")
963 t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n",
964 evt.typ, evt.which, evt.state, evt.keysym.scancode,
965 evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode)
972 const greeting = "hello, world"
974 type testPair struct {
976 Got, Want interface{}
979 var testPairs = []testPair{
980 {"GoString", C.GoString(C.greeting), greeting},
981 {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
982 {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
985 func testHelpers(t *testing.T) {
986 for _, pair := range testPairs {
987 if !reflect.DeepEqual(pair.Got, pair.Want) {
988 t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
995 const EINVAL = C.EINVAL /* test #define */
1000 var uuid C.cgo_uuid_t
1001 C.uuid_generate(&uuid[0])
1004 func Strtol(s string, base int) (int, error) {
1006 n, err := C.strtol(p, nil, C.int(base))
1007 C.free(unsafe.Pointer(p))
1011 func Atol(s string) int {
1014 C.free(unsafe.Pointer(p))
1018 func testConst(t *testing.T) {
1019 C.myConstFunc(nil, 0, nil)
1022 func testEnum(t *testing.T) {
1023 if C.Enum1 != 1 || C.Enum2 != 2 {
1024 t.Error("bad enum", C.Enum1, C.Enum2)
1028 func testNamedEnum(t *testing.T) {
1033 t.Error("bad enum", C.Enum1)
1038 t.Error("bad enum", C.Enum2)
1042 func testCastToEnum(t *testing.T) {
1043 e := C.enum_E(C.Enum1)
1045 t.Error("bad enum", C.Enum1)
1048 e = C.enum_E(C.Enum2)
1050 t.Error("bad enum", C.Enum2)
1054 func testAtol(t *testing.T) {
1057 t.Error("Atol 123: ", l)
1061 func testErrno(t *testing.T) {
1062 p := C.CString("no-such-file")
1064 f, err := C.fopen(p, m)
1065 C.free(unsafe.Pointer(p))
1066 C.free(unsafe.Pointer(m))
1069 t.Fatalf("C.fopen: should fail")
1071 if err != syscall.ENOENT {
1072 t.Fatalf("C.fopen: unexpected error: %v", err)
1076 func testMultipleAssign(t *testing.T) {
1077 p := C.CString("234")
1078 n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
1079 if runtime.GOOS == "openbsd" {
1080 // Bug in OpenBSD strtol(3) - base > 36 succeeds.
1081 if (n != 0 && n != 239089) || m != 234 {
1082 t.Fatal("Strtol x2: ", n, m)
1084 } else if n != 0 || m != 234 {
1085 t.Fatal("Strtol x2: ", n, m)
1087 C.free(unsafe.Pointer(p))
1096 type Context struct {
1097 ctx *C.struct_ibv_context
1100 func benchCgoCall(b *testing.B) {
1101 b.Run("add-int", func(b *testing.B) {
1105 for i := 0; i < b.N; i++ {
1110 b.Run("one-pointer", func(b *testing.B) {
1111 var a0 C.VkDeviceCreateInfo
1112 for i := 0; i < b.N; i++ {
1113 C.handleComplexPointer(&a0)
1116 b.Run("string-pointer-escape", func(b *testing.B) {
1117 for i := 0; i < b.N; i++ {
1119 C.handleGoStringPointerEscape(unsafe.Pointer(&s))
1122 b.Run("string-pointer-noescape", func(b *testing.B) {
1123 for i := 0; i < b.N; i++ {
1125 C.handleGoStringPointerNoescape(unsafe.Pointer(&s))
1128 b.Run("eight-pointers", func(b *testing.B) {
1129 var a0, a1, a2, a3, a4, a5, a6, a7 C.VkDeviceCreateInfo
1130 for i := 0; i < b.N; i++ {
1131 C.handleComplexPointer8(&a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7)
1134 b.Run("eight-pointers-nil", func(b *testing.B) {
1135 var a0, a1, a2, a3, a4, a5, a6, a7 *C.VkDeviceCreateInfo
1136 for i := 0; i < b.N; i++ {
1137 C.handleComplexPointer8(a0, a1, a2, a3, a4, a5, a6, a7)
1140 b.Run("eight-pointers-array", func(b *testing.B) {
1141 var a [8]C.VkDeviceCreateInfo
1142 for i := 0; i < b.N; i++ {
1143 C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
1146 b.Run("eight-pointers-slice", func(b *testing.B) {
1147 a := make([]C.VkDeviceCreateInfo, 8)
1148 for i := 0; i < b.N; i++ {
1149 C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
1154 // Benchmark measuring overhead from Go to C and back to Go (via a callback)
1155 func benchCallback(b *testing.B) {
1157 for i := 0; i < b.N; i++ {
1158 nestedCall(func() { x = true })
1161 b.Fatal("nestedCall was not invoked")
1165 var sinkString string
1167 func benchGoString(b *testing.B) {
1168 for i := 0; i < b.N; i++ {
1169 sinkString = C.GoString(C.cstr)
1171 const want = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890"
1172 if sinkString != want {
1173 b.Fatalf("%q != %q", sinkString, want)
1177 // Static (build-time) test that syntax traversal visits all operands of s[i:j:k].
1178 func sliceOperands(array [2000]int) {
1179 _ = array[C.KILO:C.KILO:C.KILO] // no type error
1182 // set in cgo_thread_lock.go init
1183 var testThreadLockFunc = func(*testing.T) {}
1185 // complex alignment
1187 func TestComplexAlign(t *testing.T) {
1188 if C.cplxAlign.x != 3.14 {
1189 t.Errorf("got %v, expected 3.14", C.cplxAlign.x)
1191 if C.cplxAlign.y != 2.17 {
1192 t.Errorf("got %v, expected 2.17", C.cplxAlign.y)
1196 // constants and pointer checking
1198 func testCheckConst(t *testing.T) {
1199 // The test is that this compiles successfully.
1200 p := C.malloc(C.size_t(unsafe.Sizeof(C.int(0))))
1202 C.CheckConstFunc(&C.CheckConstStruct{(*C.int)(p)}, C.CheckConstVal)
1207 func duplicateSymbols() {
1208 fmt.Printf("%v %v %v\n", C.base_symbol, C.alias_one, C.alias_two)
1213 // This is really an os package test but here for convenience.
1214 func testSetEnv(t *testing.T) {
1215 if runtime.GOOS == "windows" {
1216 // Go uses SetEnvironmentVariable on windows. However,
1217 // C runtime takes a *copy* at process startup of the
1218 // OS environment, and stores it in environ/envp.
1219 // It is this copy that getenv/putenv manipulate.
1220 t.Logf("skipping test")
1223 const key = "CGO_OS_TEST_KEY"
1224 const val = "CGO_OS_TEST_VALUE"
1226 keyc := C.CString(key)
1227 defer C.free(unsafe.Pointer(keyc))
1229 if uintptr(unsafe.Pointer(v)) == 0 {
1230 t.Fatal("getenv returned NULL")
1234 t.Fatalf("getenv() = %q; want %q", vs, val)
1238 // function pointer variables
1240 func callBridge(f C.intFunc) int {
1241 return int(C.bridge_int_func(f))
1244 func callCBridge(f C.intFunc) C.int {
1245 return C.bridge_int_func(f)
1248 func testFpVar(t *testing.T) {
1250 f := C.intFunc(C.fortytwo)
1251 res1 := C.bridge_int_func(f)
1252 if r1 := int(res1); r1 != expected {
1253 t.Errorf("got %d, want %d", r1, expected)
1255 res2 := callCBridge(f)
1256 if r2 := int(res2); r2 != expected {
1257 t.Errorf("got %d, want %d", r2, expected)
1261 t.Errorf("got %d, want %d", r3, expected)
1266 type AsyncEvent struct {
1267 event C.struct_ibv_async_event
1272 func test1635(t *testing.T) {
1274 if v := C.hola; v != 0 {
1275 t.Fatalf("C.hola is %d, should be 0", v)
1277 if v := C.testHola(); v != 0 {
1278 t.Fatalf("C.testHola() is %d, should be 0", v)
1284 func testUnsignedInt(t *testing.T) {
1285 a := (int64)(C.UINT32VAL)
1286 b := (int64)(0xc008427b)
1288 t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
1294 func test3250(t *testing.T) {
1295 if runtime.GOOS == "windows" {
1296 t.Skip("not applicable on windows")
1299 t.Skip("skipped, see golang.org/issue/5885")
1302 sig = syscall_dot_SIGCHLD
1304 type result struct {
1309 sigCh = make(chan os.Signal, 10)
1310 waitStart = make(chan struct{})
1311 waitDone = make(chan result)
1314 signal.Notify(sigCh, sig)
1318 alarm := time.After(time.Second * 3)
1325 if v != sig || n > thres {
1326 waitDone <- result{n, v}
1330 waitDone <- result{n, sig}
1336 waitStart <- struct{}{}
1340 t.Fatalf("received signal %v, but want %v", r.sig, sig)
1342 t.Logf("got %d signals\n", r.n)
1344 t.Fatalf("expected more than %d", thres)
1350 func testLibgcc(t *testing.T) {
1351 var table = []struct {
1358 {1 - 1<<31, 1<<31 - 1},
1360 for _, v := range table {
1361 if o := C.vabs(v.in); o != v.out {
1362 t.Fatalf("abs(%d) got %d, should be %d", v.in, o, v.out)
1370 func test3729(t *testing.T) {
1371 if runtime.GOOS == "windows" {
1372 t.Skip("skipping on windows")
1376 if e != syscall.E2BIG {
1377 t.Errorf("got %q, expect %q", e, syscall.E2BIG)
1379 _, e = C.g2(C.EINVAL, C._expA, C._expB, C._expC, C._expD)
1380 if e != syscall.EINVAL {
1381 t.Errorf("got %q, expect %q", e, syscall.EINVAL)
1387 func testPrintf(t *testing.T) {
1393 var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J}
1397 func test4339(t *testing.T) {
1398 C.handle4339(&C.exported4339)
1403 func testBoolAlign(t *testing.T) {
1404 b := C.c_bool(true, true, 10, true, false)
1406 t.Fatalf("found %d expected 10\n", b)
1408 b = C.c_bool(true, true, 5, true, true)
1410 t.Fatalf("found %d expected 5\n", b)
1412 b = C.c_bool(true, true, 3, true, false)
1414 t.Fatalf("found %d expected 3\n", b)
1416 b = C.c_bool(false, false, 1, true, false)
1418 t.Fatalf("found %d expected 1\n", b)
1420 b = C.c_bool(false, true, 200, true, false)
1422 t.Fatalf("found %d expected 200\n", b)
1434 func testCflags(t *testing.T) {
1435 is_windows := C.is_windows == 1
1436 if is_windows != (runtime.GOOS == "windows") {
1437 t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS)
1439 if C.common != 123 {
1440 t.Errorf("common: %v (expected 123)", C.common)
1446 func test5227(t *testing.T) {
1450 func selectfont() C.Fontinfo {
1451 return C.SansTypeface
1456 func test5242(t *testing.T) {
1457 if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 {
1458 t.Errorf("got %v", got)
1462 func test5603(t *testing.T) {
1464 exp := int64(C.issue5603exp)
1465 x[0] = int64(C.issue5603foo0())
1466 x[1] = int64(C.issue5603foo1(nil))
1467 x[2] = int64(C.issue5603foo2(nil, nil))
1468 x[3] = int64(C.issue5603foo3(nil, nil, nil))
1469 x[4] = int64(C.issue5603foo4(nil, nil, nil, nil))
1470 for i, v := range x {
1472 t.Errorf("issue5603foo%d() returns %v, expected %v", i, v, exp)
1479 func test5337(t *testing.T) {
1485 func test5740(t *testing.T) {
1486 if v := C.test5740a() + C.test5740b(); v != 5 {
1487 t.Errorf("expected 5, got %v", v)
1493 func test5986(t *testing.T) {
1500 // nothing to run, just make sure this compiles.
1506 func test6390(t *testing.T) {
1507 p1 := C.malloc(1024)
1509 t.Fatalf("C.malloc(1024) returned nil")
1513 t.Fatalf("C.malloc(0) returned nil")
1520 // nothing to run, just make sure this compiles
1528 // nothing to run, just make sure this compiles
1534 C.memcpy(nil, nil, x)
1535 C.memcmp(nil, nil, x)
1536 C.memmove(nil, nil, x)
1537 C.strncpy(nil, nil, x)
1538 C.strncmp(nil, nil, x)
1539 C.strncat(nil, nil, x)
1540 x = C.strxfrm(nil, nil, x)
1542 x = C.strcspn(nil, nil)
1543 x = C.strspn(nil, nil)
1551 func testNaming(t *testing.T) {
1554 if v := C.myvar; v != 5 {
1555 t.Errorf("C.myvar = %d, want 5", v)
1557 if v := C.myvar_def; v != 5 {
1558 t.Errorf("C.myvar_def = %d, want 5", v)
1560 if s := C.GoString(C.mytext); s != "abcdef" {
1561 t.Errorf("C.mytext = %q, want %q", s, "abcdef")
1563 if s := C.GoString(C.mytext_def); s != "abcdef" {
1564 t.Errorf("C.mytext_def = %q, want %q", s, "abcdef")
1566 if c := C.myenum; c != 1234 {
1567 t.Errorf("C.myenum = %v, want 1234", c)
1569 if c := C.myenum_def; c != 1234 {
1570 t.Errorf("C.myenum_def = %v, want 1234", c)
1575 t.Errorf("C.myenum as const = %v, want 1234", c)
1579 const c = C.myenum_def
1581 t.Errorf("C.myenum as const = %v, want 1234", c)
1584 if c := C.myint_def; c != 12345 {
1585 t.Errorf("C.myint_def = %v, want 12345", c)
1588 const c = C.myint_def
1590 t.Errorf("C.myint as const = %v, want 12345", c)
1594 if c := C.myfloat_def; c != 1.5 {
1595 t.Errorf("C.myint_def = %v, want 1.5", c)
1598 const c = C.myfloat_def
1600 t.Errorf("C.myint as const = %v, want 1.5", c)
1604 if s := C.mystring_def; s != "hello" {
1605 t.Errorf("C.mystring_def = %q, want %q", s, "hello")
1611 func test6907(t *testing.T) {
1613 if got := C.GoString(C.Issue6907CopyString(want)); got != want {
1614 t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want)
1620 func test7560(t *testing.T) {
1621 // some mingw don't implement __packed__ correctly.
1622 if C.offset7560() != 1 {
1623 t.Skip("C compiler did not pack struct")
1626 // C.misaligned should have x but then a padding field to get to the end of the struct.
1627 // There should not be a field named 'y'.
1629 rt := reflect.TypeOf(&v).Elem()
1630 if rt.NumField() != 2 || rt.Field(0).Name != "x" || rt.Field(1).Name != "_" {
1631 t.Errorf("unexpected fields in C.misaligned:\n")
1632 for i := 0; i < rt.NumField(); i++ {
1633 t.Logf("%+v\n", rt.Field(i))
1641 var x1 *C.typedef_test7786
1642 var x2 *C.struct_test7786
1650 var b1 *C.typedef_body7786
1651 var b2 *C.struct_body7786
1659 var u1 *C.typedef_union7786
1660 var u2 *C.union_union7786
1671 func test8092(t *testing.T) {
1676 {"text", &C.text[0], C.ctext()},
1677 {"data", &C.data[0], C.cdata()},
1679 for _, test := range tests {
1680 if test.a != test.b {
1681 t.Errorf("%s: pointer mismatch: %v != %v", test.s, test.a, test.b)
1683 if got := C.GoString(test.a); got != test.s {
1684 t.Errorf("%s: points at %#v, want %#v", test.s, got, test.s)
1689 // issues 8368 and 8441
1691 func issue8368(one *C.struct_one, two *C.struct_two) {
1694 func issue8441(one *C.one, two *C.two) {
1695 issue8441(two.x, one.x)
1700 var _ = C.struct_issue8428one{
1702 // The trailing rest field is not available in cgo.
1704 // rest: [0]C.char{},
1707 var _ = C.struct_issue8428two{
1708 p: unsafe.Pointer(nil),
1713 var _ = C.struct_issue8428three{
1714 w: [1][2][3][0]C.char{},
1715 x: [2][3][0][1]C.char{},
1716 y: [3][0][1][2]C.char{},
1717 z: [0][1][2][3]C.char{},
1722 func test8811(t *testing.T) {
1723 C.issue8811Execute()
1728 func test9557(t *testing.T) {
1729 // implicitly dereference a Go variable
1730 foo := C.issue9557foo
1731 if v := foo.a; v != 42 {
1732 t.Fatalf("foo.a expected 42, but got %d", v)
1735 // explicitly dereference a C variable
1736 if v := (*C.issue9557foo).a; v != 42 {
1737 t.Fatalf("(*C.issue9557foo).a expected 42, but is %d", v)
1740 // implicitly dereference a C variable
1741 if v := C.issue9557foo.a; v != 42 {
1742 t.Fatalf("C.issue9557foo.a expected 42, but is %d", v)
1746 // issue 8331 part 1
1748 func issue8331a() C.issue8331 {
1754 func test10303(t *testing.T, n int) {
1755 if runtime.Compiler == "gccgo" {
1756 t.Skip("gccgo permits C pointers on the stack")
1759 // Run at a few different stack depths just to avoid an unlucky pass
1760 // due to variables ending up on different pages.
1767 var x, y, z, v, si C.int
1771 C.setvoidptr(unsafe.Pointer(&v))
1775 if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1776 t.Error("C int* argument on stack")
1778 if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1779 t.Error("C intptr argument on stack")
1781 if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1782 t.Error("C void* argument on stack")
1784 if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1785 t.Error("C struct field pointer on stack")
1791 func test11925(t *testing.T) {
1792 if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) {
1793 t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{}))
1795 if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) {
1796 t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{}))
1802 func test12030(t *testing.T) {
1803 buf := (*C.char)(C.malloc(256))
1804 defer C.free(unsafe.Pointer(buf))
1805 for _, f := range []float64{1.0, 2.0, 3.14} {
1806 C.issue12030conv(buf, C.double(f))
1807 got := C.GoString(buf)
1808 if want := fmt.Sprintf("d=%g", f); got != want {
1809 t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want)
1816 var _ C.complexfloat
1817 var _ C.complexdouble
1820 // Test that cgo's multiple-value special form for
1821 // C function calls works in variable declaration statements.
1827 func test14838(t *testing.T) {
1828 data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
1829 cData := C.CBytes(data)
1832 if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 {
1833 t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:])
1841 func test17065(t *testing.T) {
1842 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
1843 t.Skip("broken on darwin; issue 17065")
1845 for i := range C.ii {
1852 func test17537(t *testing.T) {
1853 v := C.S17537{i: 17537}
1854 if got, want := C.I17537(&v), C.int(17537); got != want {
1855 t.Errorf("got %d, want %d", got, want)
1858 p := (*C.char)(C.malloc(1))
1860 if got, want := C.F17537(&p), C.int(17); got != want {
1861 t.Errorf("got %d, want %d", got, want)
1865 var v18298 C.T18298_2
1866 C.G18298(C.T18298_1(v18298))
1873 cs = C.CString("hello")
1874 defer C.free(unsafe.Pointer(cs))
1876 s = C.GoString((*C.char)(C.api_hello))
1877 s = C.GoStringN((*C.char)(C.api_hello), C.int(6))
1879 b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6))
1881 C.cstring_pointer_fun(nil)
1886 func test18126(t *testing.T) {
1888 _, err := C.Issue18126C(&p)
1895 func test18720(t *testing.T) {
1896 if got, want := C.HELLO_WORLD, "hello\000world"; got != want {
1897 t.Errorf("C.HELLO_WORLD == %q, expected %q", got, want)
1900 if got, want := C.VAR1, C.int(5); got != want {
1901 t.Errorf("C.VAR1 == %v, expected %v", got, want)
1904 if got, want := *C.ADDR, C.int(5); got != want {
1905 t.Errorf("*C.ADDR == %v, expected %v", got, want)
1908 if got, want := C.CALL, C.int(6); got != want {
1909 t.Errorf("C.CALL == %v, expected %v", got, want)
1912 if got, want := C.CALL, C.int(7); got != want {
1913 t.Errorf("C.CALL == %v, expected %v", got, want)
1917 if got, want := C.SIZE_OF_FOO, 1; got != want {
1918 t.Errorf("C.SIZE_OF_FOO == %v, expected %v", got, want)
1924 func test20129(t *testing.T) {
1925 if C.issue20129 != 0 {
1926 t.Fatal("test is broken")
1929 if C.issue20129 != 1 {
1930 t.Errorf("got %v but expected %v", C.issue20129, 1)
1933 if C.issue20129 != 2 {
1934 t.Errorf("got %v but expected %v", C.issue20129, 2)
1940 func test20369(t *testing.T) {
1941 if C.XUINT64_MAX != math.MaxUint64 {
1942 t.Fatalf("got %v, want %v", uint64(C.XUINT64_MAX), uint64(math.MaxUint64))
1948 var issue21668_X = C.x21668
1952 func test21708(t *testing.T) {
1953 if got, want := C.CAST_TO_INT64, -1; got != want {
1954 t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want)
1960 func test21809(t *testing.T) {
1961 longVar := C.long(3)
1962 typedefVar := C.MySigned_t(4)
1963 typedefTypedefVar := C.MySigned2_t(5)
1965 // all three should be considered identical to `long`
1966 if ret := C.takes_long(longVar); ret != 9 {
1967 t.Errorf("got %v but expected %v", ret, 9)
1969 if ret := C.takes_long(typedefVar); ret != 16 {
1970 t.Errorf("got %v but expected %v", ret, 16)
1972 if ret := C.takes_long(typedefTypedefVar); ret != 25 {
1973 t.Errorf("got %v but expected %v", ret, 25)
1976 // They should also be identical to the typedef'd type
1977 if ret := C.takes_typedef(longVar); ret != 9 {
1978 t.Errorf("got %v but expected %v", ret, 9)
1980 if ret := C.takes_typedef(typedefVar); ret != 16 {
1981 t.Errorf("got %v but expected %v", ret, 16)
1983 if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
1984 t.Errorf("got %v but expected %v", ret, 25)
1990 func test22906(t *testing.T) {
1991 var x1 C.jobject = 0 // Note: 0, not nil. That makes sure we use uintptr for these types.
1995 var x3 C.jthrowable = 0
1997 var x4 C.jstring = 0
2001 var x6 C.jbooleanArray = 0
2003 var x7 C.jbyteArray = 0
2005 var x8 C.jcharArray = 0
2007 var x9 C.jshortArray = 0
2009 var x10 C.jintArray = 0
2011 var x11 C.jlongArray = 0
2013 var x12 C.jfloatArray = 0
2015 var x13 C.jdoubleArray = 0
2017 var x14 C.jobjectArray = 0
2024 // Nothing to run, just make sure this compiles.
2025 var Vissue22958 C.issue22958Type
2027 func test23356(t *testing.T) {
2028 if got, want := C.a(), C.int(5); got != want {
2029 t.Errorf("C.a() == %v, expected %v", got, want)
2031 if got, want := C.r(), C.int(3); got != want {
2032 t.Errorf("C.r() == %v, expected %v", got, want)
2038 func Issue23720F() {
2045 func test24206(t *testing.T) {
2046 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
2047 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
2050 if l := len(C.GoString(C.dangerousString1())); l != 123 {
2051 t.Errorf("Incorrect string length - got %d, want 123", l)
2053 if l := len(C.GoString(C.dangerousString2())); l != 4096+123 {
2054 t.Errorf("Incorrect string length - got %d, want %d", l, 4096+123)
2060 func issue25143sum(ns ...C.int) C.int {
2062 for _, n := range ns {
2068 func test25143(t *testing.T) {
2069 if got, want := issue25143sum(1, 2, 3), C.int(6); got != want {
2070 t.Errorf("issue25143sum(1, 2, 3) == %v, expected %v", got, want)
2075 // Wrong type of constant with GCC 8 and newer.
2077 func test26066(t *testing.T) {
2078 var i = int64(C.issue26066)
2080 t.Errorf("got %d, want -1", i)
2089 // Stress the interaction between the race detector and cgo in an
2090 // attempt to reproduce the memory corruption described in #27660.
2091 // The bug was very timing sensitive; at the time of writing this
2092 // test would only trigger the bug about once out of every five runs.
2094 func test27660(t *testing.T) {
2095 ctx, cancel := context.WithCancel(context.Background())
2097 ints := make([]int, 100)
2098 locks := make([]sync.Mutex, 100)
2099 // Slowly create threads so that ThreadSanitizer is forced to
2100 // frequently resize its SyncClocks.
2101 for i := 0; i < 100; i++ {
2103 for ctx.Err() == nil {
2104 // Sleep in C for long enough that it is likely that the runtime
2105 // will retake this goroutine's currently wired P.
2106 C.usleep(1000 /* 1ms */)
2107 runtime.Gosched() // avoid starvation (see #28701)
2111 // Trigger lots of synchronization and memory reads/writes to
2112 // increase the likelihood that the race described in #27660
2113 // results in corruption of ThreadSanitizer's internal state
2114 // and thus an assertion failure or segfault.
2116 for ctx.Err() == nil {
2121 // needed for gccgo, to avoid creation of an
2122 // unpreemptible "fast path" in this loop. Choice
2123 // of (1<<24) is somewhat arbitrary.
2131 time.Sleep(time.Millisecond)
2138 var v struct{ p *byte }
2139 C.twoargs1(C.twoargs2(), C.twoargs3(unsafe.Pointer(&v)))
2144 func issue28545G(p **C.char) {
2145 C.issue28545F(p, -1, (0))
2146 C.issue28545F(p, 2+3, complex(1, 1))
2147 C.issue28545F(p, issue28772Constant, issue28772Constant2)
2150 // issue 28772 part 1 - part 2 in testx.go
2152 const issue28772Constant = C.issue28772Constant
2156 func offset(i int) uintptr {
2157 var pi C.innerPacked
2158 var po C.outerPacked
2159 var ui C.innerUnpacked
2160 var uo C.outerUnpacked
2163 return unsafe.Offsetof(pi.f2)
2165 return unsafe.Offsetof(po.g2)
2167 return unsafe.Offsetof(ui.f2)
2169 return unsafe.Offsetof(uo.g2)
2171 panic("can't happen")
2175 func test28896(t *testing.T) {
2176 for i := 0; i < 4; i++ {
2177 c := uintptr(C.offset(C.int(i)))
2180 t.Errorf("%d: C: %d != Go %d", i, c, g)
2186 // cgo's /*line*/ comments failed when inserted after '/',
2187 // because the result looked like a "//" comment.
2188 // No runtime test; just make sure it compiles.
2190 func Issue29383(n, size uint) int {
2191 if ^C.size_t(0)/C.size_t(n) < C.size_t(size) {
2198 // Error handling a struct initializer that requires pointer checking.
2199 // Compilation test only, nothing to run.
2201 var Vissue29748 = C.f29748(&C.S29748{
2205 func Fissue299748() {
2213 var issue29781X struct{ X int }
2215 func issue29781F(...int) int { return 0 }
2217 func issue29781G() {
2219 C.issue29781F(&p, C.ISSUE29781C+1)
2220 C.issue29781F(nil, (C.int)(
2222 C.issue29781F(&p, (C.int)(0))
2223 C.issue29781F(&p, (C.int)(
2225 C.issue29781F(&p, (C.int)(issue29781X.
2231 func test30065(t *testing.T) {
2234 C.memcpy(unsafe.Pointer(&a), unsafe.Pointer(&b[0]), 1)
2236 t.Errorf("&a failed: got %c, want %c", a[0], 'a')
2240 C.memcpy(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), 1)
2242 t.Errorf("&a[0] failed: got %c, want %c", a[0], 'b')
2245 d := make([]byte, 256)
2247 C.memcpy(unsafe.Pointer(&d[0]), unsafe.Pointer(&b[0]), 1)
2249 t.Errorf("&d[0] failed: got %c, want %c", d[0], 'c')
2254 // No runtime test; just make sure it compiles.
2257 C.issue31093F(C.ushort(0))
2262 func test32579(t *testing.T) {
2263 var s [1]C.struct_S32579
2264 C.memset(unsafe.Pointer(&s[0].data[0]), 1, 1)
2265 if s[0].data[0] != 1 {
2266 t.Errorf("&s[0].data[0] failed: got %d, want %d", s[0].data[0], 1)
2270 // issue 37033, check if cgo.Handle works properly
2272 func testHandle(t *testing.T) {
2273 ch := make(chan int)
2275 for i := 0; i < 42; i++ {
2276 h := cgo.NewHandle(ch)
2278 C.cFunc37033(C.uintptr_t(h))
2280 if v := <-ch; issue37033 != v {
2281 t.Fatalf("unexpected receiving value: got %d, want %d", v, issue37033)
2289 var issue38649 C.netbsd_gid = 42
2293 var issue39877 *C.void = nil
2296 // No runtime test; just make sure it compiles.
2299 C.issue40494(C.enum_Enum40494(C.X_40494), (*C.union_Union40494)(nil))
2303 func test45451(t *testing.T) {
2305 typ := reflect.ValueOf(u).Type().Elem()
2307 // The type is undefined in C so allocating it should panic.
2309 if r := recover(); r == nil {
2310 t.Error("expected panic")
2314 _ = reflect.New(typ)
2315 t.Errorf("reflect.New(%v) should have panicked", typ)
2320 func func52542[T ~[]C.int]() {}
2322 type type52542[T ~*C.float] struct{}