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.
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 // Following mimicks vulkan complex definitions for benchmarking cgocheck overhead.
120 typedef uint32_t VkFlags;
121 typedef VkFlags VkDeviceQueueCreateFlags;
122 typedef uint32_t VkStructureType;
124 typedef struct VkDeviceQueueCreateInfo {
125 VkStructureType sType;
127 VkDeviceQueueCreateFlags flags;
128 uint32_t queueFamilyIndex;
130 const float* pQueuePriorities;
131 } VkDeviceQueueCreateInfo;
133 typedef struct VkPhysicalDeviceFeatures {
135 } VkPhysicalDeviceFeatures;
137 typedef struct VkDeviceCreateInfo {
138 VkStructureType sType;
141 uint32_t queueCreateInfoCount;
142 const VkDeviceQueueCreateInfo* pQueueCreateInfos;
143 uint32_t enabledLayerCount;
144 const char* const* ppEnabledLayerNames;
145 uint32_t enabledExtensionCount;
146 const char* const* ppEnabledExtensionNames;
147 const VkPhysicalDeviceFeatures* pEnabledFeatures;
148 } VkDeviceCreateInfo;
150 void handleComplexPointer(VkDeviceCreateInfo *a0) {}
151 void handleComplexPointer8(
152 VkDeviceCreateInfo *a0, VkDeviceCreateInfo *a1, VkDeviceCreateInfo *a2, VkDeviceCreateInfo *a3,
153 VkDeviceCreateInfo *a4, VkDeviceCreateInfo *a5, VkDeviceCreateInfo *a6, VkDeviceCreateInfo *a7
161 } cplxAlign = { 3.14, 2.17 };
163 // constants and pointer checking
165 #define CheckConstVal 0
171 static void CheckConstFunc(CheckConstStruct *p, int e) {}
176 #define alias_one base_symbol
177 #define alias_two base_symbol
179 // function pointer variables
181 typedef int (*intFunc) ();
184 bridge_int_func(intFunc f)
198 struct ibv_async_event {
204 xxpthread_mutex_t mutex;
208 // Mac OS X's gcc will generate scattered relocation 2/1 for
209 // this function on Darwin/386, and 8l couldn't handle it.
210 // this example is in issue 1635
213 printf("scatter = %p\n", p);
216 // Adding this explicit extern declaration makes this a test for
217 // https://gcc.gnu.org/PR68072 aka https://golang.org/issue/13344 .
218 // It used to cause a cgo error when building with GCC 6.
221 // this example is in issue 3253
223 int testHola() { return hola; }
227 void testSendSIG() {}
229 static void *thread(void *p) {
233 for (i = 0; i < M; i++) {
234 pthread_kill(pthread_self(), SIGCHLD);
235 usleep(rand() % 20 + 5);
243 for (i = 0; i < N; i++) {
244 usleep(rand() % 200 + 100);
245 pthread_create(&tid[i], 0, thread, NULL);
247 for (i = 0; i < N; i++)
248 pthread_join(tid[i], 0);
253 // libgcc on ARM might be compiled as thumb code, but our 5l
254 // can't handle that, so we have to disable this test on arm.
257 puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library.");
258 return (x < 0) ? -x : x;
260 #elif defined(__arm64__) && defined(__clang__)
262 puts("testLibgcc is disabled on ARM64 with clang due to lack of libgcc.");
263 return (x < 0) ? -x : x;
266 int __absvsi2(int); // dummy prototype for libgcc function
267 // we shouldn't name the function abs, as gcc might use
269 int vabs(int x) { return __absvsi2(x); }
274 // access errno from void C function
275 const char _expA = 0x42;
276 const float _expB = 3.14159;
277 const short _expC = 0x55aa;
278 const int _expD = 0xdeadbeef;
282 void g2(int x, char a, float b, short c, int d) {}
289 // try to pass some non-trivial arguments to function g2
290 void g2(int x, char a, float b, short c, int d) {
291 if (a == _expA && b == _expB && c == _expC && d == _expD)
299 // Test that cgo reserves enough stack space during cgo call.
300 // See https://golang.org/issue/3945 for details.
302 printf("%s from C\n", "hello");
305 // issue 4054 part 1 - other half in testx.go
321 // We've historically permitted #include <>, so test it here. Issue 29333.
322 // Also see issue 41059.
323 #include <issue4339.h>
326 // cmd/cgo: bool alignment/padding issue.
327 // bool alignment is wrong and causing wrong arguments when calling functions.
328 static int c_bool(bool a, bool b, int c, bool d, bool e) {
334 const struct { int a; } *issue4857() { return (void *)0; }
337 // Test that the #cgo CFLAGS directive works,
338 // with and without platform filters.
339 #cgo CFLAGS: -DCOMMON_VALUE=123
340 #cgo windows CFLAGS: -DIS_WINDOWS=1
341 #cgo !windows CFLAGS: -DIS_WINDOWS=0
342 int common = COMMON_VALUE;
343 int is_windows = IS_WINDOWS;
346 // linker incorrectly treats common symbols and
347 // leaves them undefined.
353 Fontinfo SansTypeface;
357 Fontinfo loadfont() {
363 SansTypeface = loadfont();
367 // Cgo incorrectly computed the alignment of structs
368 // with no Go accessible fields as 0, and then panicked on
369 // modulo-by-zero computations.
372 // disable arm64 GCC warnings
373 #cgo CFLAGS: -Wno-psabi -Wno-unknown-warning-option
382 int issue5242(foo f, bar b) {
387 // Verify that we can withstand SIGPROF received on foreign threads
392 static void *thread1(void *p) {
394 pthread_kill(pthread_self(), SIGPROF);
399 pthread_create(&tid, 0, thread1, NULL);
400 pthread_join(tid, 0);
406 const long long issue5603exp = 0x12345678;
407 long long issue5603foo0() { return issue5603exp; }
408 long long issue5603foo1(void *p) { return issue5603exp; }
409 long long issue5603foo2(void *p, void *q) { return issue5603exp; }
410 long long issue5603foo3(void *p, void *q, void *r) { return issue5603exp; }
411 long long issue5603foo4(void *p, void *q, void *r, void *s) { return issue5603exp; }
415 int test5740a(void), test5740b(void);
418 static void output5986()
420 int current_row = 0, row_count = 0;
421 double sum_squares = 0;
424 if (current_row == 10) {
429 while (current_row++ != 1);
430 d = sqrt(sum_squares / row_count);
431 printf("sqrt is: %g\n", d);
435 // Test handling of #defined names in clang.
436 // NOTE: Must use hex, or else a shortcut for decimals
437 // in cgo avoids trying to pass this to clang.
450 // Test new scheme for deciding whether C.name is an expression, type, constant.
451 // Clang silences some warnings when the name is a #defined macro, so test those too
452 // (even though we now use errors exclusively, not warnings).
456 const char *mytext = "abcdef";
462 #define myfunc_def myfunc
463 #define myvar_def myvar
464 #define mytext_def mytext
465 #define mytype_def mytype
466 #define myenum_def myenum
467 #define myint_def 12345
468 #define myfloat_def 1.5
469 #define mystring_def "hello"
472 char* Issue6907CopyString(_GoString_ s) {
489 } __attribute__((__packed__)) misaligned;
494 return (uintptr_t)&((misaligned*)0)->y;
498 // No runtime test, just make sure that typedef and struct/union/class are interchangeable at compile time.
501 typedef struct test7786 typedef_test7786;
502 void f7786(struct test7786 *ctx) {}
503 void g7786(typedef_test7786 *ctx) {}
505 typedef struct body7786 typedef_body7786;
506 struct body7786 { int x; };
507 void b7786(struct body7786 *ctx) {}
508 void c7786(typedef_body7786 *ctx) {}
510 typedef union union7786 typedef_union7786;
511 void u7786(union union7786 *ctx) {}
512 void v7786(typedef_union7786 *ctx) {}
515 // Test that linker defined symbols (e.g., text, data) don't
516 // conflict with C symbols.
517 char text[] = "text";
518 char data[] = "data";
519 char *ctext(void) { return text; }
520 char *cdata(void) { return data; }
523 // Cgo inconsistently translated zero size arrays.
525 struct issue8428one {
530 struct issue8428two {
537 struct issue8428three {
544 // issue 8331 part 1 - part 2 in testx.go
545 // A typedef of an unnamed struct is the same struct when
546 // #include'd twice. No runtime test; just make sure it compiles.
547 #include "issue8331.h"
549 // issue 8368 and 8441
550 // Recursive struct definitions didn't work.
551 // No runtime test; just make sure it compiles.
552 typedef struct one one;
553 typedef struct two two;
563 extern int issue8811Initialized;
564 extern void issue8811Init();
566 void issue8811Execute() {
567 if(!issue8811Initialized)
573 typedef void (*PFunc8945)();
580 } test9557bar = { 42 };
581 struct issue9557_t *issue9557foo = &test9557bar;
584 // Pointers passed to C were not marked as escaping (bug in cgo).
588 void setintstar(int *x) {
592 void setintptr(intptr x) {
596 void setvoidptr(void *x) {
600 typedef struct Struct Struct;
605 void setstruct(Struct s) {
610 // Structs with zero-length trailing fields are now padded by the Go compiler.
625 void issue12030conv(char *buf, double x) {
626 sprintf(buf, "d=%g", x);
631 int check_cbytes(char *b, size_t l) {
633 for (i = 0; i < l; i++) {
642 // Test that C symbols larger than a page play nicely with the race detector.
646 // The void* cast introduced by cgo to avoid problems
647 // with const/volatile qualifiers breaks C preprocessor macros that
648 // emulate functions.
654 int I17537(S17537 *p);
656 #define I17537(p) ((p)->i)
658 // Calling this function used to fail without the cast.
659 const int F17537(const char **p) {
664 // API compatibility checks
666 typedef char *cstring_pointer;
667 static void cstring_pointer_fun(cstring_pointer dummy) { }
668 const char *api_hello = "hello!";
670 // Calling this function used to trigger an error from the C compiler
672 void F18298(const void *const *p) {
675 // Test that conversions between typedefs work as they used to.
676 typedef const void *T18298_1;
677 struct S18298 { int i; };
678 typedef const struct S18298 *T18298_2;
679 void G18298(T18298_1 t) {
683 // cgo check of void function returning errno.
684 void Issue18126C(void **p) {}
688 #define HELLO "hello"
689 #define WORLD "world"
690 #define HELLO_WORLD HELLO "\000" WORLD
692 struct foo { char c; };
693 #define SIZE_OF(x) sizeof(x)
694 #define SIZE_OF_FOO SIZE_OF(struct foo)
709 typedef void issue20129Void;
710 issue20129Void issue20129Foo() {
713 typedef issue20129Void issue20129Void2;
714 issue20129Void2 issue20129Bar() {
719 #define XUINT64_MAX 18446744073709551615ULL
722 // Fail to guess the kind of the constant "x".
723 // No runtime test; just make sure it compiles.
724 const int x21668 = 42;
727 #define CAST_TO_INT64 (int64_t)(-1)
730 // Compile C `typedef` to go type aliases.
732 typedef long MySigned_t;
733 // tests alias-to-alias
734 typedef MySigned_t MySigned2_t;
735 long takes_long(long x) { return x * x; }
736 MySigned_t takes_typedef(MySigned_t x) { return x * x; }
740 // It's going to be hard to include a whole real JVM to test this.
741 // So we'll simulate a really easy JVM using just the parts we need.
742 // This is the relevant part of jni.h.
746 typedef struct _jobject *jobject;
747 typedef jobject jclass;
748 typedef jobject jthrowable;
749 typedef jobject jstring;
750 typedef jobject jarray;
751 typedef jarray jbooleanArray;
752 typedef jarray jbyteArray;
753 typedef jarray jcharArray;
754 typedef jarray jshortArray;
755 typedef jarray jintArray;
756 typedef jarray jlongArray;
757 typedef jarray jfloatArray;
758 typedef jarray jdoubleArray;
759 typedef jarray jobjectArray;
761 typedef jobject jweak;
763 // Note: jvalue is already a non-pointer type due to it being a C union.
768 unsigned long long f8 : 8;
769 unsigned long long f16 : 16;
770 unsigned long long f24 : 24;
771 unsigned long long f32 : 32;
772 unsigned long long f40 : 40;
773 unsigned long long f48 : 48;
774 unsigned long long f56 : 56;
775 unsigned long long f64 : 64;
779 int a(void) { return 5; };
780 int r(void) { return 3; };
783 typedef int *issue23720A;
784 typedef const int *issue23720B;
785 void issue23720F(issue23720B a) {}
788 #if defined(__linux__) && defined(__x86_64__)
789 #include <sys/mman.h>
790 // Returns string with null byte at the last valid address
791 char* dangerousString1() {
793 char *data = mmap(0, 2 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
794 mprotect(data + pageSize,pageSize,PROT_NONE);
795 int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
797 for (; i < pageSize; i++) {
800 data[pageSize -1 ] = 0;
804 char* dangerousString2() {
806 char *data = mmap(0, 3 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
807 mprotect(data + 2 * pageSize,pageSize,PROT_NONE);
808 int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
810 for (; i < 2 * pageSize; i++) {
813 data[2*pageSize -1 ] = 0;
817 char *dangerousString1() { return NULL; }
818 char *dangerousString2() { return NULL; }
822 const unsigned long long int issue26066 = (const unsigned long long) -1;
825 // Introduce two pointer types which are distinct, but have the same
826 // base type. Make sure that both of those pointer types get resolved
827 // correctly. Before the fix for 26517 if one of these pointer types
828 // was resolved before the other one was processed, the second one
829 // would never be resolved.
830 // Before this issue was fixed this test failed on Windows,
831 // where va_list expands to a named char* type.
832 typedef va_list TypeOne;
833 typedef char *TypeTwo;
837 static void twoargs1(void *p, int n) {}
838 static void *twoargs2() { return 0; }
839 static int twoargs3(void * p) { return 0; }
842 // Failed to add type conversion for negative constant.
844 static void issue28545F(char **p, int n, complex double a) {}
846 // issue 28772 part 1 - part 2 in testx.go
847 // Failed to add type conversion for Go constant set to C constant.
848 // No runtime test; just make sure it compiles.
850 #define issue28772Constant 1
853 // cgo was incorrectly adding padding after a packed struct.
857 } __attribute__((__packed__)) innerPacked;
874 size_t offset(int x) {
877 return offsetof(innerPacked, f2);
879 return offsetof(outerPacked, g2);
881 return offsetof(innerUnpacked, f2);
883 return offsetof(outerUnpacked, g2);
891 typedef struct { char **p; } S29748;
892 static int f29748(S29748 *p) { return 0; }
895 // Error with newline inserted into constant expression.
896 // Compilation test only, nothing to run.
898 static void issue29781F(char **p, int n) {}
899 #define ISSUE29781C 0
902 static uint16_t issue31093F(uint16_t v) { return v; }
905 typedef struct S32579 { unsigned char data[1]; } S32579;
907 // issue 37033, cgo.Handle
908 extern void GoFunc37033(uintptr_t handle);
909 void cFunc37033(uintptr_t handle) { GoFunc37033(handle); }
912 // Test that #define'd type aliases work.
913 #define netbsd_gid unsigned int
916 // Inconsistent handling of tagged enum and union types.
917 enum Enum40494 { X_40494 };
918 union Union40494 { int x; };
919 void issue40494(enum Enum40494 e, union Union40494* up) {}
921 // Issue 45451, bad handling of go:notinheap types.
922 typedef struct issue45451Undefined issue45451;
924 // Issue 49633, example of cgo.Handle with void*.
925 extern void GoFunc49633(void*);
926 void cfunc49633(void *context) { GoFunc49633(context); }
950 func testAlign(t *testing.T) {
951 var evt C.SDL_KeyboardEvent
953 if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 {
954 t.Error("*** bad alignment")
956 t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n",
957 evt.typ, evt.which, evt.state, evt.keysym.scancode,
958 evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode)
965 const greeting = "hello, world"
967 type testPair struct {
969 Got, Want interface{}
972 var testPairs = []testPair{
973 {"GoString", C.GoString(C.greeting), greeting},
974 {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
975 {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
978 func testHelpers(t *testing.T) {
979 for _, pair := range testPairs {
980 if !reflect.DeepEqual(pair.Got, pair.Want) {
981 t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
988 const EINVAL = C.EINVAL /* test #define */
993 var uuid C.cgo_uuid_t
994 C.uuid_generate(&uuid[0])
997 func Strtol(s string, base int) (int, error) {
999 n, err := C.strtol(p, nil, C.int(base))
1000 C.free(unsafe.Pointer(p))
1004 func Atol(s string) int {
1007 C.free(unsafe.Pointer(p))
1011 func testConst(t *testing.T) {
1012 C.myConstFunc(nil, 0, nil)
1015 func testEnum(t *testing.T) {
1016 if C.Enum1 != 1 || C.Enum2 != 2 {
1017 t.Error("bad enum", C.Enum1, C.Enum2)
1021 func testNamedEnum(t *testing.T) {
1026 t.Error("bad enum", C.Enum1)
1031 t.Error("bad enum", C.Enum2)
1035 func testCastToEnum(t *testing.T) {
1036 e := C.enum_E(C.Enum1)
1038 t.Error("bad enum", C.Enum1)
1041 e = C.enum_E(C.Enum2)
1043 t.Error("bad enum", C.Enum2)
1047 func testAtol(t *testing.T) {
1050 t.Error("Atol 123: ", l)
1054 func testErrno(t *testing.T) {
1055 p := C.CString("no-such-file")
1057 f, err := C.fopen(p, m)
1058 C.free(unsafe.Pointer(p))
1059 C.free(unsafe.Pointer(m))
1062 t.Fatalf("C.fopen: should fail")
1064 if err != syscall.ENOENT {
1065 t.Fatalf("C.fopen: unexpected error: %v", err)
1069 func testMultipleAssign(t *testing.T) {
1070 p := C.CString("234")
1071 n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
1072 if runtime.GOOS == "openbsd" {
1073 // Bug in OpenBSD strtol(3) - base > 36 succeeds.
1074 if (n != 0 && n != 239089) || m != 234 {
1075 t.Fatal("Strtol x2: ", n, m)
1077 } else if n != 0 || m != 234 {
1078 t.Fatal("Strtol x2: ", n, m)
1080 C.free(unsafe.Pointer(p))
1089 type Context struct {
1090 ctx *C.struct_ibv_context
1093 func benchCgoCall(b *testing.B) {
1094 b.Run("add-int", func(b *testing.B) {
1098 for i := 0; i < b.N; i++ {
1103 b.Run("one-pointer", func(b *testing.B) {
1104 var a0 C.VkDeviceCreateInfo
1105 for i := 0; i < b.N; i++ {
1106 C.handleComplexPointer(&a0)
1109 b.Run("eight-pointers", func(b *testing.B) {
1110 var a0, a1, a2, a3, a4, a5, a6, a7 C.VkDeviceCreateInfo
1111 for i := 0; i < b.N; i++ {
1112 C.handleComplexPointer8(&a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7)
1115 b.Run("eight-pointers-nil", func(b *testing.B) {
1116 var a0, a1, a2, a3, a4, a5, a6, a7 *C.VkDeviceCreateInfo
1117 for i := 0; i < b.N; i++ {
1118 C.handleComplexPointer8(a0, a1, a2, a3, a4, a5, a6, a7)
1121 b.Run("eight-pointers-array", func(b *testing.B) {
1122 var a [8]C.VkDeviceCreateInfo
1123 for i := 0; i < b.N; i++ {
1124 C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
1127 b.Run("eight-pointers-slice", func(b *testing.B) {
1128 a := make([]C.VkDeviceCreateInfo, 8)
1129 for i := 0; i < b.N; i++ {
1130 C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
1135 // Benchmark measuring overhead from Go to C and back to Go (via a callback)
1136 func benchCallback(b *testing.B) {
1138 for i := 0; i < b.N; i++ {
1139 nestedCall(func() { x = true })
1142 b.Fatal("nestedCall was not invoked")
1146 var sinkString string
1148 func benchGoString(b *testing.B) {
1149 for i := 0; i < b.N; i++ {
1150 sinkString = C.GoString(C.cstr)
1152 const want = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890"
1153 if sinkString != want {
1154 b.Fatalf("%q != %q", sinkString, want)
1158 // Static (build-time) test that syntax traversal visits all operands of s[i:j:k].
1159 func sliceOperands(array [2000]int) {
1160 _ = array[C.KILO:C.KILO:C.KILO] // no type error
1163 // set in cgo_thread_lock.go init
1164 var testThreadLockFunc = func(*testing.T) {}
1166 // complex alignment
1168 func TestComplexAlign(t *testing.T) {
1169 if C.cplxAlign.x != 3.14 {
1170 t.Errorf("got %v, expected 3.14", C.cplxAlign.x)
1172 if C.cplxAlign.y != 2.17 {
1173 t.Errorf("got %v, expected 2.17", C.cplxAlign.y)
1177 // constants and pointer checking
1179 func testCheckConst(t *testing.T) {
1180 // The test is that this compiles successfully.
1181 p := C.malloc(C.size_t(unsafe.Sizeof(C.int(0))))
1183 C.CheckConstFunc(&C.CheckConstStruct{(*C.int)(p)}, C.CheckConstVal)
1188 func duplicateSymbols() {
1189 fmt.Printf("%v %v %v\n", C.base_symbol, C.alias_one, C.alias_two)
1194 // This is really an os package test but here for convenience.
1195 func testSetEnv(t *testing.T) {
1196 if runtime.GOOS == "windows" {
1197 // Go uses SetEnvironmentVariable on windows. However,
1198 // C runtime takes a *copy* at process startup of the
1199 // OS environment, and stores it in environ/envp.
1200 // It is this copy that getenv/putenv manipulate.
1201 t.Logf("skipping test")
1204 const key = "CGO_OS_TEST_KEY"
1205 const val = "CGO_OS_TEST_VALUE"
1207 keyc := C.CString(key)
1208 defer C.free(unsafe.Pointer(keyc))
1210 if uintptr(unsafe.Pointer(v)) == 0 {
1211 t.Fatal("getenv returned NULL")
1215 t.Fatalf("getenv() = %q; want %q", vs, val)
1219 // function pointer variables
1221 func callBridge(f C.intFunc) int {
1222 return int(C.bridge_int_func(f))
1225 func callCBridge(f C.intFunc) C.int {
1226 return C.bridge_int_func(f)
1229 func testFpVar(t *testing.T) {
1231 f := C.intFunc(C.fortytwo)
1232 res1 := C.bridge_int_func(f)
1233 if r1 := int(res1); r1 != expected {
1234 t.Errorf("got %d, want %d", r1, expected)
1236 res2 := callCBridge(f)
1237 if r2 := int(res2); r2 != expected {
1238 t.Errorf("got %d, want %d", r2, expected)
1242 t.Errorf("got %d, want %d", r3, expected)
1247 type AsyncEvent struct {
1248 event C.struct_ibv_async_event
1253 func test1635(t *testing.T) {
1255 if v := C.hola; v != 0 {
1256 t.Fatalf("C.hola is %d, should be 0", v)
1258 if v := C.testHola(); v != 0 {
1259 t.Fatalf("C.testHola() is %d, should be 0", v)
1265 func testUnsignedInt(t *testing.T) {
1266 a := (int64)(C.UINT32VAL)
1267 b := (int64)(0xc008427b)
1269 t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
1275 func test3250(t *testing.T) {
1276 if runtime.GOOS == "windows" {
1277 t.Skip("not applicable on windows")
1280 t.Skip("skipped, see golang.org/issue/5885")
1283 sig = syscall_dot_SIGCHLD
1285 type result struct {
1290 sigCh = make(chan os.Signal, 10)
1291 waitStart = make(chan struct{})
1292 waitDone = make(chan result)
1295 signal.Notify(sigCh, sig)
1299 alarm := time.After(time.Second * 3)
1306 if v != sig || n > thres {
1307 waitDone <- result{n, v}
1311 waitDone <- result{n, sig}
1317 waitStart <- struct{}{}
1321 t.Fatalf("received signal %v, but want %v", r.sig, sig)
1323 t.Logf("got %d signals\n", r.n)
1325 t.Fatalf("expected more than %d", thres)
1331 func testLibgcc(t *testing.T) {
1332 var table = []struct {
1339 {1 - 1<<31, 1<<31 - 1},
1341 for _, v := range table {
1342 if o := C.vabs(v.in); o != v.out {
1343 t.Fatalf("abs(%d) got %d, should be %d", v.in, o, v.out)
1351 func test3729(t *testing.T) {
1352 if runtime.GOOS == "windows" {
1353 t.Skip("skipping on windows")
1357 if e != syscall.E2BIG {
1358 t.Errorf("got %q, expect %q", e, syscall.E2BIG)
1360 _, e = C.g2(C.EINVAL, C._expA, C._expB, C._expC, C._expD)
1361 if e != syscall.EINVAL {
1362 t.Errorf("got %q, expect %q", e, syscall.EINVAL)
1368 func testPrintf(t *testing.T) {
1374 var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J}
1378 func test4339(t *testing.T) {
1379 C.handle4339(&C.exported4339)
1384 func testBoolAlign(t *testing.T) {
1385 b := C.c_bool(true, true, 10, true, false)
1387 t.Fatalf("found %d expected 10\n", b)
1389 b = C.c_bool(true, true, 5, true, true)
1391 t.Fatalf("found %d expected 5\n", b)
1393 b = C.c_bool(true, true, 3, true, false)
1395 t.Fatalf("found %d expected 3\n", b)
1397 b = C.c_bool(false, false, 1, true, false)
1399 t.Fatalf("found %d expected 1\n", b)
1401 b = C.c_bool(false, true, 200, true, false)
1403 t.Fatalf("found %d expected 200\n", b)
1415 func testCflags(t *testing.T) {
1416 is_windows := C.is_windows == 1
1417 if is_windows != (runtime.GOOS == "windows") {
1418 t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS)
1420 if C.common != 123 {
1421 t.Errorf("common: %v (expected 123)", C.common)
1427 func test5227(t *testing.T) {
1431 func selectfont() C.Fontinfo {
1432 return C.SansTypeface
1437 func test5242(t *testing.T) {
1438 if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 {
1439 t.Errorf("got %v", got)
1443 func test5603(t *testing.T) {
1445 exp := int64(C.issue5603exp)
1446 x[0] = int64(C.issue5603foo0())
1447 x[1] = int64(C.issue5603foo1(nil))
1448 x[2] = int64(C.issue5603foo2(nil, nil))
1449 x[3] = int64(C.issue5603foo3(nil, nil, nil))
1450 x[4] = int64(C.issue5603foo4(nil, nil, nil, nil))
1451 for i, v := range x {
1453 t.Errorf("issue5603foo%d() returns %v, expected %v", i, v, exp)
1460 func test5337(t *testing.T) {
1466 func test5740(t *testing.T) {
1467 if v := C.test5740a() + C.test5740b(); v != 5 {
1468 t.Errorf("expected 5, got %v", v)
1474 func test5986(t *testing.T) {
1481 // nothing to run, just make sure this compiles.
1487 func test6390(t *testing.T) {
1488 p1 := C.malloc(1024)
1490 t.Fatalf("C.malloc(1024) returned nil")
1494 t.Fatalf("C.malloc(0) returned nil")
1501 // nothing to run, just make sure this compiles
1509 // nothing to run, just make sure this compiles
1515 C.memcpy(nil, nil, x)
1516 C.memcmp(nil, nil, x)
1517 C.memmove(nil, nil, x)
1518 C.strncpy(nil, nil, x)
1519 C.strncmp(nil, nil, x)
1520 C.strncat(nil, nil, x)
1521 x = C.strxfrm(nil, nil, x)
1523 x = C.strcspn(nil, nil)
1524 x = C.strspn(nil, nil)
1532 func testNaming(t *testing.T) {
1535 if v := C.myvar; v != 5 {
1536 t.Errorf("C.myvar = %d, want 5", v)
1538 if v := C.myvar_def; v != 5 {
1539 t.Errorf("C.myvar_def = %d, want 5", v)
1541 if s := C.GoString(C.mytext); s != "abcdef" {
1542 t.Errorf("C.mytext = %q, want %q", s, "abcdef")
1544 if s := C.GoString(C.mytext_def); s != "abcdef" {
1545 t.Errorf("C.mytext_def = %q, want %q", s, "abcdef")
1547 if c := C.myenum; c != 1234 {
1548 t.Errorf("C.myenum = %v, want 1234", c)
1550 if c := C.myenum_def; c != 1234 {
1551 t.Errorf("C.myenum_def = %v, want 1234", c)
1556 t.Errorf("C.myenum as const = %v, want 1234", c)
1560 const c = C.myenum_def
1562 t.Errorf("C.myenum as const = %v, want 1234", c)
1565 if c := C.myint_def; c != 12345 {
1566 t.Errorf("C.myint_def = %v, want 12345", c)
1569 const c = C.myint_def
1571 t.Errorf("C.myint as const = %v, want 12345", c)
1575 if c := C.myfloat_def; c != 1.5 {
1576 t.Errorf("C.myint_def = %v, want 1.5", c)
1579 const c = C.myfloat_def
1581 t.Errorf("C.myint as const = %v, want 1.5", c)
1585 if s := C.mystring_def; s != "hello" {
1586 t.Errorf("C.mystring_def = %q, want %q", s, "hello")
1592 func test6907(t *testing.T) {
1594 if got := C.GoString(C.Issue6907CopyString(want)); got != want {
1595 t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want)
1601 func test7560(t *testing.T) {
1602 // some mingw don't implement __packed__ correctly.
1603 if C.offset7560() != 1 {
1604 t.Skip("C compiler did not pack struct")
1607 // C.misaligned should have x but then a padding field to get to the end of the struct.
1608 // There should not be a field named 'y'.
1610 rt := reflect.TypeOf(&v).Elem()
1611 if rt.NumField() != 2 || rt.Field(0).Name != "x" || rt.Field(1).Name != "_" {
1612 t.Errorf("unexpected fields in C.misaligned:\n")
1613 for i := 0; i < rt.NumField(); i++ {
1614 t.Logf("%+v\n", rt.Field(i))
1622 var x1 *C.typedef_test7786
1623 var x2 *C.struct_test7786
1631 var b1 *C.typedef_body7786
1632 var b2 *C.struct_body7786
1640 var u1 *C.typedef_union7786
1641 var u2 *C.union_union7786
1652 func test8092(t *testing.T) {
1657 {"text", &C.text[0], C.ctext()},
1658 {"data", &C.data[0], C.cdata()},
1660 for _, test := range tests {
1661 if test.a != test.b {
1662 t.Errorf("%s: pointer mismatch: %v != %v", test.s, test.a, test.b)
1664 if got := C.GoString(test.a); got != test.s {
1665 t.Errorf("%s: points at %#v, want %#v", test.s, got, test.s)
1670 // issues 8368 and 8441
1672 func issue8368(one *C.struct_one, two *C.struct_two) {
1675 func issue8441(one *C.one, two *C.two) {
1676 issue8441(two.x, one.x)
1681 var _ = C.struct_issue8428one{
1683 // The trailing rest field is not available in cgo.
1685 // rest: [0]C.char{},
1688 var _ = C.struct_issue8428two{
1689 p: unsafe.Pointer(nil),
1694 var _ = C.struct_issue8428three{
1695 w: [1][2][3][0]C.char{},
1696 x: [2][3][0][1]C.char{},
1697 y: [3][0][1][2]C.char{},
1698 z: [0][1][2][3]C.char{},
1703 func test8811(t *testing.T) {
1704 C.issue8811Execute()
1709 func test9557(t *testing.T) {
1710 // implicitly dereference a Go variable
1711 foo := C.issue9557foo
1712 if v := foo.a; v != 42 {
1713 t.Fatalf("foo.a expected 42, but got %d", v)
1716 // explicitly dereference a C variable
1717 if v := (*C.issue9557foo).a; v != 42 {
1718 t.Fatalf("(*C.issue9557foo).a expected 42, but is %d", v)
1721 // implicitly dereference a C variable
1722 if v := C.issue9557foo.a; v != 42 {
1723 t.Fatalf("C.issue9557foo.a expected 42, but is %d", v)
1727 // issue 8331 part 1
1729 func issue8331a() C.issue8331 {
1735 func test10303(t *testing.T, n int) {
1736 if runtime.Compiler == "gccgo" {
1737 t.Skip("gccgo permits C pointers on the stack")
1740 // Run at a few different stack depths just to avoid an unlucky pass
1741 // due to variables ending up on different pages.
1748 var x, y, z, v, si C.int
1752 C.setvoidptr(unsafe.Pointer(&v))
1756 if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1757 t.Error("C int* argument on stack")
1759 if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1760 t.Error("C intptr argument on stack")
1762 if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1763 t.Error("C void* argument on stack")
1765 if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1766 t.Error("C struct field pointer on stack")
1772 func test11925(t *testing.T) {
1773 if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) {
1774 t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{}))
1776 if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) {
1777 t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{}))
1783 func test12030(t *testing.T) {
1784 buf := (*C.char)(C.malloc(256))
1785 defer C.free(unsafe.Pointer(buf))
1786 for _, f := range []float64{1.0, 2.0, 3.14} {
1787 C.issue12030conv(buf, C.double(f))
1788 got := C.GoString(buf)
1789 if want := fmt.Sprintf("d=%g", f); got != want {
1790 t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want)
1797 var _ C.complexfloat
1798 var _ C.complexdouble
1801 // Test that cgo's multiple-value special form for
1802 // C function calls works in variable declaration statements.
1808 func test14838(t *testing.T) {
1809 data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
1810 cData := C.CBytes(data)
1813 if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 {
1814 t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:])
1822 func test17065(t *testing.T) {
1823 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
1824 t.Skip("broken on darwin; issue 17065")
1826 for i := range C.ii {
1833 func test17537(t *testing.T) {
1834 v := C.S17537{i: 17537}
1835 if got, want := C.I17537(&v), C.int(17537); got != want {
1836 t.Errorf("got %d, want %d", got, want)
1839 p := (*C.char)(C.malloc(1))
1841 if got, want := C.F17537(&p), C.int(17); got != want {
1842 t.Errorf("got %d, want %d", got, want)
1846 var v18298 C.T18298_2
1847 C.G18298(C.T18298_1(v18298))
1854 cs = C.CString("hello")
1855 defer C.free(unsafe.Pointer(cs))
1857 s = C.GoString((*C.char)(C.api_hello))
1858 s = C.GoStringN((*C.char)(C.api_hello), C.int(6))
1860 b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6))
1862 C.cstring_pointer_fun(nil)
1867 func test18126(t *testing.T) {
1869 _, err := C.Issue18126C(&p)
1876 func test18720(t *testing.T) {
1877 if got, want := C.HELLO_WORLD, "hello\000world"; got != want {
1878 t.Errorf("C.HELLO_WORLD == %q, expected %q", got, want)
1881 if got, want := C.VAR1, C.int(5); got != want {
1882 t.Errorf("C.VAR1 == %v, expected %v", got, want)
1885 if got, want := *C.ADDR, C.int(5); got != want {
1886 t.Errorf("*C.ADDR == %v, expected %v", got, want)
1889 if got, want := C.CALL, C.int(6); got != want {
1890 t.Errorf("C.CALL == %v, expected %v", got, want)
1893 if got, want := C.CALL, C.int(7); got != want {
1894 t.Errorf("C.CALL == %v, expected %v", got, want)
1898 if got, want := C.SIZE_OF_FOO, 1; got != want {
1899 t.Errorf("C.SIZE_OF_FOO == %v, expected %v", got, want)
1905 func test20129(t *testing.T) {
1906 if C.issue20129 != 0 {
1907 t.Fatal("test is broken")
1910 if C.issue20129 != 1 {
1911 t.Errorf("got %v but expected %v", C.issue20129, 1)
1914 if C.issue20129 != 2 {
1915 t.Errorf("got %v but expected %v", C.issue20129, 2)
1921 func test20369(t *testing.T) {
1922 if C.XUINT64_MAX != math.MaxUint64 {
1923 t.Fatalf("got %v, want %v", uint64(C.XUINT64_MAX), uint64(math.MaxUint64))
1929 var issue21668_X = C.x21668
1933 func test21708(t *testing.T) {
1934 if got, want := C.CAST_TO_INT64, -1; got != want {
1935 t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want)
1941 func test21809(t *testing.T) {
1942 longVar := C.long(3)
1943 typedefVar := C.MySigned_t(4)
1944 typedefTypedefVar := C.MySigned2_t(5)
1946 // all three should be considered identical to `long`
1947 if ret := C.takes_long(longVar); ret != 9 {
1948 t.Errorf("got %v but expected %v", ret, 9)
1950 if ret := C.takes_long(typedefVar); ret != 16 {
1951 t.Errorf("got %v but expected %v", ret, 16)
1953 if ret := C.takes_long(typedefTypedefVar); ret != 25 {
1954 t.Errorf("got %v but expected %v", ret, 25)
1957 // They should also be identical to the typedef'd type
1958 if ret := C.takes_typedef(longVar); ret != 9 {
1959 t.Errorf("got %v but expected %v", ret, 9)
1961 if ret := C.takes_typedef(typedefVar); ret != 16 {
1962 t.Errorf("got %v but expected %v", ret, 16)
1964 if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
1965 t.Errorf("got %v but expected %v", ret, 25)
1971 func test22906(t *testing.T) {
1972 var x1 C.jobject = 0 // Note: 0, not nil. That makes sure we use uintptr for these types.
1976 var x3 C.jthrowable = 0
1978 var x4 C.jstring = 0
1982 var x6 C.jbooleanArray = 0
1984 var x7 C.jbyteArray = 0
1986 var x8 C.jcharArray = 0
1988 var x9 C.jshortArray = 0
1990 var x10 C.jintArray = 0
1992 var x11 C.jlongArray = 0
1994 var x12 C.jfloatArray = 0
1996 var x13 C.jdoubleArray = 0
1998 var x14 C.jobjectArray = 0
2005 // Nothing to run, just make sure this compiles.
2006 var Vissue22958 C.issue22958Type
2008 func test23356(t *testing.T) {
2009 if got, want := C.a(), C.int(5); got != want {
2010 t.Errorf("C.a() == %v, expected %v", got, want)
2012 if got, want := C.r(), C.int(3); got != want {
2013 t.Errorf("C.r() == %v, expected %v", got, want)
2019 func Issue23720F() {
2026 func test24206(t *testing.T) {
2027 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
2028 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
2031 if l := len(C.GoString(C.dangerousString1())); l != 123 {
2032 t.Errorf("Incorrect string length - got %d, want 123", l)
2034 if l := len(C.GoString(C.dangerousString2())); l != 4096+123 {
2035 t.Errorf("Incorrect string length - got %d, want %d", l, 4096+123)
2041 func issue25143sum(ns ...C.int) C.int {
2043 for _, n := range ns {
2049 func test25143(t *testing.T) {
2050 if got, want := issue25143sum(1, 2, 3), C.int(6); got != want {
2051 t.Errorf("issue25143sum(1, 2, 3) == %v, expected %v", got, want)
2056 // Wrong type of constant with GCC 8 and newer.
2058 func test26066(t *testing.T) {
2059 var i = int64(C.issue26066)
2061 t.Errorf("got %d, want -1", i)
2070 // Stress the interaction between the race detector and cgo in an
2071 // attempt to reproduce the memory corruption described in #27660.
2072 // The bug was very timing sensitive; at the time of writing this
2073 // test would only trigger the bug about once out of every five runs.
2075 func test27660(t *testing.T) {
2076 ctx, cancel := context.WithCancel(context.Background())
2078 ints := make([]int, 100)
2079 locks := make([]sync.Mutex, 100)
2080 // Slowly create threads so that ThreadSanitizer is forced to
2081 // frequently resize its SyncClocks.
2082 for i := 0; i < 100; i++ {
2084 for ctx.Err() == nil {
2085 // Sleep in C for long enough that it is likely that the runtime
2086 // will retake this goroutine's currently wired P.
2087 C.usleep(1000 /* 1ms */)
2088 runtime.Gosched() // avoid starvation (see #28701)
2092 // Trigger lots of synchronization and memory reads/writes to
2093 // increase the likelihood that the race described in #27660
2094 // results in corruption of ThreadSanitizer's internal state
2095 // and thus an assertion failure or segfault.
2097 for ctx.Err() == nil {
2102 // needed for gccgo, to avoid creation of an
2103 // unpreemptible "fast path" in this loop. Choice
2104 // of (1<<24) is somewhat arbitrary.
2112 time.Sleep(time.Millisecond)
2119 var v struct{ p *byte }
2120 C.twoargs1(C.twoargs2(), C.twoargs3(unsafe.Pointer(&v)))
2125 func issue28545G(p **C.char) {
2126 C.issue28545F(p, -1, (0))
2127 C.issue28545F(p, 2+3, complex(1, 1))
2128 C.issue28545F(p, issue28772Constant, issue28772Constant2)
2131 // issue 28772 part 1 - part 2 in testx.go
2133 const issue28772Constant = C.issue28772Constant
2137 func offset(i int) uintptr {
2138 var pi C.innerPacked
2139 var po C.outerPacked
2140 var ui C.innerUnpacked
2141 var uo C.outerUnpacked
2144 return unsafe.Offsetof(pi.f2)
2146 return unsafe.Offsetof(po.g2)
2148 return unsafe.Offsetof(ui.f2)
2150 return unsafe.Offsetof(uo.g2)
2152 panic("can't happen")
2156 func test28896(t *testing.T) {
2157 for i := 0; i < 4; i++ {
2158 c := uintptr(C.offset(C.int(i)))
2161 t.Errorf("%d: C: %d != Go %d", i, c, g)
2167 // cgo's /*line*/ comments failed when inserted after '/',
2168 // because the result looked like a "//" comment.
2169 // No runtime test; just make sure it compiles.
2171 func Issue29383(n, size uint) int {
2172 if ^C.size_t(0)/C.size_t(n) < C.size_t(size) {
2179 // Error handling a struct initializer that requires pointer checking.
2180 // Compilation test only, nothing to run.
2182 var Vissue29748 = C.f29748(&C.S29748{
2186 func Fissue299748() {
2194 var issue29781X struct{ X int }
2196 func issue29781F(...int) int { return 0 }
2198 func issue29781G() {
2200 C.issue29781F(&p, C.ISSUE29781C+1)
2201 C.issue29781F(nil, (C.int)(
2203 C.issue29781F(&p, (C.int)(0))
2204 C.issue29781F(&p, (C.int)(
2206 C.issue29781F(&p, (C.int)(issue29781X.
2212 func test30065(t *testing.T) {
2215 C.memcpy(unsafe.Pointer(&a), unsafe.Pointer(&b[0]), 1)
2217 t.Errorf("&a failed: got %c, want %c", a[0], 'a')
2221 C.memcpy(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), 1)
2223 t.Errorf("&a[0] failed: got %c, want %c", a[0], 'b')
2226 d := make([]byte, 256)
2228 C.memcpy(unsafe.Pointer(&d[0]), unsafe.Pointer(&b[0]), 1)
2230 t.Errorf("&d[0] failed: got %c, want %c", d[0], 'c')
2235 // No runtime test; just make sure it compiles.
2238 C.issue31093F(C.ushort(0))
2243 func test32579(t *testing.T) {
2244 var s [1]C.struct_S32579
2245 C.memset(unsafe.Pointer(&s[0].data[0]), 1, 1)
2246 if s[0].data[0] != 1 {
2247 t.Errorf("&s[0].data[0] failed: got %d, want %d", s[0].data[0], 1)
2251 // issue 37033, check if cgo.Handle works properly
2253 func testHandle(t *testing.T) {
2254 ch := make(chan int)
2256 for i := 0; i < 42; i++ {
2257 h := cgo.NewHandle(ch)
2259 C.cFunc37033(C.uintptr_t(h))
2261 if v := <-ch; issue37033 != v {
2262 t.Fatalf("unexpected receiving value: got %d, want %d", v, issue37033)
2270 var issue38649 C.netbsd_gid = 42
2274 var issue39877 *C.void = nil
2277 // No runtime test; just make sure it compiles.
2280 C.issue40494(C.enum_Enum40494(C.X_40494), (*C.union_Union40494)(nil))
2284 func test45451(t *testing.T) {
2286 typ := reflect.ValueOf(u).Type().Elem()
2288 // The type is undefined in C so allocating it should panic.
2290 if r := recover(); r == nil {
2291 t.Error("expected panic")
2295 _ = reflect.New(typ)
2296 t.Errorf("reflect.New(%v) should have panicked", typ)
2301 func func52542[T ~[]C.int]() {}
2303 type type52542[T ~*C.float] struct{}