Source file misc/cgo/test/test.go

     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.
     4  
     5  // Test cases for cgo.
     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.
    10  
    11  package cgotest
    12  
    13  /*
    14  #include <complex.h>
    15  #include <math.h>
    16  #include <stdarg.h>
    17  #include <stdbool.h>
    18  #include <stddef.h>
    19  #include <stdint.h>
    20  #include <stdio.h>
    21  #include <stdlib.h>
    22  #include <string.h>
    23  #include <unistd.h>
    24  #include <sys/stat.h>
    25  #include <errno.h>
    26  #cgo LDFLAGS: -lm
    27  
    28  #ifndef WIN32
    29  #include <pthread.h>
    30  #include <signal.h>
    31  #endif
    32  
    33  // alignment tests
    34  
    35  typedef unsigned char Uint8;
    36  typedef unsigned short Uint16;
    37  
    38  typedef enum {
    39   MOD1 = 0x0000,
    40   MODX = 0x8000
    41  } SDLMod;
    42  
    43  typedef enum {
    44   A1 = 1,
    45   B1 = 322,
    46   SDLK_LAST
    47  } SDLKey;
    48  
    49  typedef struct SDL_keysym {
    50  	Uint8 scancode;
    51  	SDLKey sym;
    52  	SDLMod mod;
    53  	Uint16 unicode;
    54  } SDL_keysym;
    55  
    56  typedef struct SDL_KeyboardEvent {
    57  	Uint8 typ;
    58  	Uint8 which;
    59  	Uint8 state;
    60  	SDL_keysym keysym;
    61  } SDL_KeyboardEvent;
    62  
    63  void makeEvent(SDL_KeyboardEvent *event) {
    64   unsigned char *p;
    65   int i;
    66  
    67   p = (unsigned char*)event;
    68   for (i=0; i<sizeof *event; i++) {
    69     p[i] = i;
    70   }
    71  }
    72  
    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;
    75  }
    76  
    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);
    80   fflush(stdout);
    81  }
    82  
    83  // api
    84  
    85  const char *greeting = "hello, world";
    86  
    87  // basic test cases
    88  
    89  #define SHIFT(x, y)  ((x)<<(y))
    90  #define KILO SHIFT(1, 10)
    91  #define UINT32VAL 0xc008427bU
    92  
    93  enum E {
    94  	Enum1 = 1,
    95  	Enum2 = 2,
    96  };
    97  
    98  typedef unsigned char cgo_uuid_t[20];
    99  
   100  void uuid_generate(cgo_uuid_t x) {
   101  	x[0] = 0;
   102  }
   103  
   104  struct S {
   105  	int x;
   106  };
   107  
   108  const char *cstr = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890";
   109  
   110  extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter);
   111  
   112  enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; }
   113  
   114  int add(int x, int y) {
   115  	return x+y;
   116  };
   117  
   118  // Following mimicks vulkan complex definitions for benchmarking cgocheck overhead.
   119  
   120  typedef uint32_t VkFlags;
   121  typedef VkFlags  VkDeviceQueueCreateFlags;
   122  typedef uint32_t VkStructureType;
   123  
   124  typedef struct VkDeviceQueueCreateInfo {
   125      VkStructureType             sType;
   126      const void*                 pNext;
   127      VkDeviceQueueCreateFlags    flags;
   128      uint32_t                    queueFamilyIndex;
   129      uint32_t                    queueCount;
   130      const float*                pQueuePriorities;
   131  } VkDeviceQueueCreateInfo;
   132  
   133  typedef struct VkPhysicalDeviceFeatures {
   134      uint32_t bools[56];
   135  } VkPhysicalDeviceFeatures;
   136  
   137  typedef struct VkDeviceCreateInfo {
   138      VkStructureType                    sType;
   139      const void*                        pNext;
   140      VkFlags                            flags;
   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;
   149  
   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
   154  ) {}
   155  
   156  // complex alignment
   157  
   158  struct {
   159  	float x;
   160  	_Complex float y;
   161  } cplxAlign = { 3.14, 2.17 };
   162  
   163  // constants and pointer checking
   164  
   165  #define CheckConstVal 0
   166  
   167  typedef struct {
   168  	int *p;
   169  } CheckConstStruct;
   170  
   171  static void CheckConstFunc(CheckConstStruct *p, int e) {}
   172  
   173  // duplicate symbol
   174  
   175  int base_symbol = 0;
   176  #define alias_one base_symbol
   177  #define alias_two base_symbol
   178  
   179  // function pointer variables
   180  
   181  typedef int (*intFunc) ();
   182  
   183  int
   184  bridge_int_func(intFunc f)
   185  {
   186  	return f();
   187  }
   188  
   189  int fortytwo()
   190  {
   191  	return 42;
   192  }
   193  
   194  // issue 1222
   195  typedef union {
   196  	long align;
   197  } xxpthread_mutex_t;
   198  struct ibv_async_event {
   199  	union {
   200  		int x;
   201  	} element;
   202  };
   203  struct ibv_context {
   204  	xxpthread_mutex_t mutex;
   205  };
   206  
   207  // issue 1635
   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
   211  void scatter() {
   212  	void *p = scatter;
   213  	printf("scatter = %p\n", p);
   214  }
   215  
   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.
   219  extern int hola;
   220  
   221  // this example is in issue 3253
   222  int hola = 0;
   223  int testHola() { return hola; }
   224  
   225  // issue 3250
   226  #ifdef WIN32
   227  void testSendSIG() {}
   228  #else
   229  static void *thread(void *p) {
   230  	const int M = 100;
   231  	int i;
   232  	(void)p;
   233  	for (i = 0; i < M; i++) {
   234  		pthread_kill(pthread_self(), SIGCHLD);
   235  		usleep(rand() % 20 + 5);
   236  	}
   237  	return NULL;
   238  }
   239  void testSendSIG() {
   240  	const int N = 20;
   241  	int i;
   242  	pthread_t tid[N];
   243  	for (i = 0; i < N; i++) {
   244  		usleep(rand() % 200 + 100);
   245  		pthread_create(&tid[i], 0, thread, NULL);
   246  	}
   247  	for (i = 0; i < N; i++)
   248  		pthread_join(tid[i], 0);
   249  }
   250  #endif
   251  
   252  // issue 3261
   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.
   255  #ifdef __ARMEL__
   256  int vabs(int x) {
   257  	puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library.");
   258  	return (x < 0) ? -x : x;
   259  }
   260  #elif defined(__arm64__) && defined(__clang__)
   261  int vabs(int x) {
   262  	puts("testLibgcc is disabled on ARM64 with clang due to lack of libgcc.");
   263  	return (x < 0) ? -x : x;
   264  }
   265  #else
   266  int __absvsi2(int); // dummy prototype for libgcc function
   267  // we shouldn't name the function abs, as gcc might use
   268  // the builtin one.
   269  int vabs(int x) { return __absvsi2(x); }
   270  #endif
   271  
   272  
   273  // issue 3729
   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;
   279  
   280  #ifdef WIN32
   281  void g(void) {}
   282  void g2(int x, char a, float b, short c, int d) {}
   283  #else
   284  
   285  void g(void) {
   286  	errno = E2BIG;
   287  }
   288  
   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)
   292  		errno = x;
   293  	else
   294  		errno = -1;
   295  }
   296  #endif
   297  
   298  // issue 3945
   299  // Test that cgo reserves enough stack space during cgo call.
   300  // See https://golang.org/issue/3945 for details.
   301  void say() {
   302  	printf("%s from C\n", "hello");
   303  }
   304  
   305  // issue 4054 part 1 - other half in testx.go
   306  
   307  typedef enum {
   308  	A = 0,
   309  	B,
   310  	C,
   311  	D,
   312  	E,
   313  	F,
   314  	G,
   315  	H,
   316  	II,
   317  	J,
   318  } issue4054a;
   319  
   320  // issue 4339
   321  // We've historically permitted #include <>, so test it here.  Issue 29333.
   322  // Also see issue 41059.
   323  #include <issue4339.h>
   324  
   325  // issue 4417
   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)  {
   329     return c;
   330  }
   331  
   332  // issue 4857
   333  #cgo CFLAGS: -Werror
   334  const struct { int a; } *issue4857() { return (void *)0; }
   335  
   336  // issue 5224
   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;
   344  
   345  // issue 5227
   346  // linker incorrectly treats common symbols and
   347  // leaves them undefined.
   348  
   349  typedef struct {
   350          int Count;
   351  } Fontinfo;
   352  
   353  Fontinfo SansTypeface;
   354  
   355  extern void init();
   356  
   357  Fontinfo loadfont() {
   358          Fontinfo f = {0};
   359          return f;
   360  }
   361  
   362  void init() {
   363          SansTypeface = loadfont();
   364  }
   365  
   366  // issue 5242
   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.
   370  typedef struct {
   371  } foo;
   372  
   373  typedef struct {
   374  	int x : 1;
   375  } bar;
   376  
   377  int issue5242(foo f, bar b) {
   378  	return 5242;
   379  }
   380  
   381  // issue 5337
   382  // Verify that we can withstand SIGPROF received on foreign threads
   383  
   384  #ifdef WIN32
   385  void test5337() {}
   386  #else
   387  static void *thread1(void *p) {
   388  	(void)p;
   389  	pthread_kill(pthread_self(), SIGPROF);
   390  	return NULL;
   391  }
   392  void test5337() {
   393  	pthread_t tid;
   394  	pthread_create(&tid, 0, thread1, NULL);
   395  	pthread_join(tid, 0);
   396  }
   397  #endif
   398  
   399  // issue 5603
   400  
   401  const long long issue5603exp = 0x12345678;
   402  long long issue5603foo0() { return issue5603exp; }
   403  long long issue5603foo1(void *p) { return issue5603exp; }
   404  long long issue5603foo2(void *p, void *q) { return issue5603exp; }
   405  long long issue5603foo3(void *p, void *q, void *r) { return issue5603exp; }
   406  long long issue5603foo4(void *p, void *q, void *r, void *s) { return issue5603exp; }
   407  
   408  // issue 5740
   409  
   410  int test5740a(void), test5740b(void);
   411  
   412  // issue 5986
   413  static void output5986()
   414  {
   415      int current_row = 0, row_count = 0;
   416      double sum_squares = 0;
   417      double d;
   418      do {
   419          if (current_row == 10) {
   420              current_row = 0;
   421          }
   422          ++row_count;
   423      }
   424      while (current_row++ != 1);
   425      d =  sqrt(sum_squares / row_count);
   426      printf("sqrt is: %g\n", d);
   427  }
   428  
   429  // issue 6128
   430  // Test handling of #defined names in clang.
   431  // NOTE: Must use hex, or else a shortcut for decimals
   432  // in cgo avoids trying to pass this to clang.
   433  #define X 0x1
   434  
   435  // issue 6472
   436  typedef struct
   437  {
   438          struct
   439          {
   440              int x;
   441          } y[16];
   442  } z;
   443  
   444  // issue 6612
   445  // Test new scheme for deciding whether C.name is an expression, type, constant.
   446  // Clang silences some warnings when the name is a #defined macro, so test those too
   447  // (even though we now use errors exclusively, not warnings).
   448  
   449  void myfunc(void) {}
   450  int myvar = 5;
   451  const char *mytext = "abcdef";
   452  typedef int mytype;
   453  enum {
   454  	myenum = 1234,
   455  };
   456  
   457  #define myfunc_def myfunc
   458  #define myvar_def myvar
   459  #define mytext_def mytext
   460  #define mytype_def mytype
   461  #define myenum_def myenum
   462  #define myint_def 12345
   463  #define myfloat_def 1.5
   464  #define mystring_def "hello"
   465  
   466  // issue 6907
   467  char* Issue6907CopyString(_GoString_ s) {
   468  	size_t n;
   469  	const char *p;
   470  	char *r;
   471  
   472  	n = _GoStringLen(s);
   473  	p = _GoStringPtr(s);
   474  	r = malloc(n + 1);
   475  	memmove(r, p, n);
   476  	r[n] = '\0';
   477  	return r;
   478  }
   479  
   480  // issue 7560
   481  typedef struct {
   482  	char x;
   483  	long y;
   484  } __attribute__((__packed__)) misaligned;
   485  
   486  int
   487  offset7560(void)
   488  {
   489  	return (uintptr_t)&((misaligned*)0)->y;
   490  }
   491  
   492  // issue 7786
   493  // No runtime test, just make sure that typedef and struct/union/class are interchangeable at compile time.
   494  
   495  struct test7786;
   496  typedef struct test7786 typedef_test7786;
   497  void f7786(struct test7786 *ctx) {}
   498  void g7786(typedef_test7786 *ctx) {}
   499  
   500  typedef struct body7786 typedef_body7786;
   501  struct body7786 { int x; };
   502  void b7786(struct body7786 *ctx) {}
   503  void c7786(typedef_body7786 *ctx) {}
   504  
   505  typedef union union7786 typedef_union7786;
   506  void u7786(union union7786 *ctx) {}
   507  void v7786(typedef_union7786 *ctx) {}
   508  
   509  // issue 8092
   510  // Test that linker defined symbols (e.g., text, data) don't
   511  // conflict with C symbols.
   512  char text[] = "text";
   513  char data[] = "data";
   514  char *ctext(void) { return text; }
   515  char *cdata(void) { return data; }
   516  
   517  // issue 8428
   518  // Cgo inconsistently translated zero size arrays.
   519  
   520  struct issue8428one {
   521  	char b;
   522  	char rest[];
   523  };
   524  
   525  struct issue8428two {
   526  	void *p;
   527  	char b;
   528  	char rest[0];
   529  	char pad;
   530  };
   531  
   532  struct issue8428three {
   533  	char w[1][2][3][0];
   534  	char x[2][3][0][1];
   535  	char y[3][0][1][2];
   536  	char z[0][1][2][3];
   537  };
   538  
   539  // issue 8331 part 1 - part 2 in testx.go
   540  // A typedef of an unnamed struct is the same struct when
   541  // #include'd twice.  No runtime test; just make sure it compiles.
   542  #include "issue8331.h"
   543  
   544  // issue 8368 and 8441
   545  // Recursive struct definitions didn't work.
   546  // No runtime test; just make sure it compiles.
   547  typedef struct one one;
   548  typedef struct two two;
   549  struct one {
   550  	two *x;
   551  };
   552  struct two {
   553  	one *x;
   554  };
   555  
   556  // issue 8811
   557  
   558  extern int issue8811Initialized;
   559  extern void issue8811Init();
   560  
   561  void issue8811Execute() {
   562  	if(!issue8811Initialized)
   563  		issue8811Init();
   564  }
   565  
   566  // issue 8945
   567  
   568  typedef void (*PFunc8945)();
   569  PFunc8945 func8945;
   570  
   571  // issue 9557
   572  
   573  struct issue9557_t {
   574    int a;
   575  } test9557bar = { 42 };
   576  struct issue9557_t *issue9557foo = &test9557bar;
   577  
   578  // issue 10303
   579  // Pointers passed to C were not marked as escaping (bug in cgo).
   580  
   581  typedef int *intptr;
   582  
   583  void setintstar(int *x) {
   584  	*x = 1;
   585  }
   586  
   587  void setintptr(intptr x) {
   588  	*x = 1;
   589  }
   590  
   591  void setvoidptr(void *x) {
   592  	*(int*)x = 1;
   593  }
   594  
   595  typedef struct Struct Struct;
   596  struct Struct {
   597  	int *P;
   598  };
   599  
   600  void setstruct(Struct s) {
   601  	*s.P = 1;
   602  }
   603  
   604  // issue 11925
   605  // Structs with zero-length trailing fields are now padded by the Go compiler.
   606  
   607  struct a11925 {
   608  	int i;
   609  	char a[0];
   610  	char b[0];
   611  };
   612  
   613  struct b11925 {
   614  	int i;
   615  	char a[0];
   616  	char b[];
   617  };
   618  
   619  // issue 12030
   620  void issue12030conv(char *buf, double x) {
   621  	sprintf(buf, "d=%g", x);
   622  }
   623  
   624  // issue 14838
   625  
   626  int check_cbytes(char *b, size_t l) {
   627  	int i;
   628  	for (i = 0; i < l; i++) {
   629  		if (b[i] != i) {
   630  			return 0;
   631  		}
   632  	}
   633  	return 1;
   634  }
   635  
   636  // issue 17065
   637  // Test that C symbols larger than a page play nicely with the race detector.
   638  int ii[65537];
   639  
   640  // issue 17537
   641  // The void* cast introduced by cgo to avoid problems
   642  // with const/volatile qualifiers breaks C preprocessor macros that
   643  // emulate functions.
   644  
   645  typedef struct {
   646  	int i;
   647  } S17537;
   648  
   649  int I17537(S17537 *p);
   650  
   651  #define I17537(p) ((p)->i)
   652  
   653  // Calling this function used to fail without the cast.
   654  const int F17537(const char **p) {
   655  	return **p;
   656  }
   657  
   658  // issue 17723
   659  // API compatibility checks
   660  
   661  typedef char *cstring_pointer;
   662  static void cstring_pointer_fun(cstring_pointer dummy) { }
   663  const char *api_hello = "hello!";
   664  
   665  // Calling this function used to trigger an error from the C compiler
   666  // (issue 18298).
   667  void F18298(const void *const *p) {
   668  }
   669  
   670  // Test that conversions between typedefs work as they used to.
   671  typedef const void *T18298_1;
   672  struct S18298 { int i; };
   673  typedef const struct S18298 *T18298_2;
   674  void G18298(T18298_1 t) {
   675  }
   676  
   677  // issue 18126
   678  // cgo check of void function returning errno.
   679  void Issue18126C(void **p) {}
   680  
   681  // issue 18720
   682  
   683  #define HELLO "hello"
   684  #define WORLD "world"
   685  #define HELLO_WORLD HELLO "\000" WORLD
   686  
   687  struct foo { char c; };
   688  #define SIZE_OF(x) sizeof(x)
   689  #define SIZE_OF_FOO SIZE_OF(struct foo)
   690  #define VAR1 VAR
   691  #define VAR var
   692  int var = 5;
   693  
   694  #define ADDR &var
   695  
   696  #define CALL fn()
   697  int fn(void) {
   698  	return ++var;
   699  }
   700  
   701  // issue 20129
   702  
   703  int issue20129 = 0;
   704  typedef void issue20129Void;
   705  issue20129Void issue20129Foo() {
   706  	issue20129 = 1;
   707  }
   708  typedef issue20129Void issue20129Void2;
   709  issue20129Void2 issue20129Bar() {
   710  	issue20129 = 2;
   711  }
   712  
   713  // issue 20369
   714  #define XUINT64_MAX        18446744073709551615ULL
   715  
   716  // issue 21668
   717  // Fail to guess the kind of the constant "x".
   718  // No runtime test; just make sure it compiles.
   719  const int x21668 = 42;
   720  
   721  // issue 21708
   722  #define CAST_TO_INT64 (int64_t)(-1)
   723  
   724  // issue 21809
   725  // Compile C `typedef` to go type aliases.
   726  
   727  typedef long MySigned_t;
   728  // tests alias-to-alias
   729  typedef MySigned_t MySigned2_t;
   730  long takes_long(long x) { return x * x; }
   731  MySigned_t takes_typedef(MySigned_t x) { return x * x; }
   732  
   733  // issue 22906
   734  
   735  // It's going to be hard to include a whole real JVM to test this.
   736  // So we'll simulate a really easy JVM using just the parts we need.
   737  // This is the relevant part of jni.h.
   738  
   739  struct _jobject;
   740  
   741  typedef struct _jobject *jobject;
   742  typedef jobject jclass;
   743  typedef jobject jthrowable;
   744  typedef jobject jstring;
   745  typedef jobject jarray;
   746  typedef jarray jbooleanArray;
   747  typedef jarray jbyteArray;
   748  typedef jarray jcharArray;
   749  typedef jarray jshortArray;
   750  typedef jarray jintArray;
   751  typedef jarray jlongArray;
   752  typedef jarray jfloatArray;
   753  typedef jarray jdoubleArray;
   754  typedef jarray jobjectArray;
   755  
   756  typedef jobject jweak;
   757  
   758  // Note: jvalue is already a non-pointer type due to it being a C union.
   759  
   760  // issue 22958
   761  
   762  typedef struct {
   763  	unsigned long long f8  : 8;
   764  	unsigned long long f16 : 16;
   765  	unsigned long long f24 : 24;
   766  	unsigned long long f32 : 32;
   767  	unsigned long long f40 : 40;
   768  	unsigned long long f48 : 48;
   769  	unsigned long long f56 : 56;
   770  	unsigned long long f64 : 64;
   771  } issue22958Type;
   772  
   773  // issue 23356
   774  int a(void) { return 5; };
   775  int r(void) { return 3; };
   776  
   777  // issue 23720
   778  typedef int *issue23720A;
   779  typedef const int *issue23720B;
   780  void issue23720F(issue23720B a) {}
   781  
   782  // issue 24206
   783  #if defined(__linux__) && defined(__x86_64__)
   784  #include <sys/mman.h>
   785  // Returns string with null byte at the last valid address
   786  char* dangerousString1() {
   787  	int pageSize = 4096;
   788  	char *data = mmap(0, 2 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
   789  	mprotect(data + pageSize,pageSize,PROT_NONE);
   790  	int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
   791  	int i = start;
   792  	for (; i < pageSize; i++) {
   793  	data[i] = 'x';
   794  	}
   795  	data[pageSize -1 ] = 0;
   796  	return data+start;
   797  }
   798  
   799  char* dangerousString2() {
   800  	int pageSize = 4096;
   801  	char *data = mmap(0, 3 * pageSize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
   802  	mprotect(data + 2 * pageSize,pageSize,PROT_NONE);
   803  	int start = pageSize - 123 - 1; // last 123 bytes of first page + 1 null byte
   804  	int i = start;
   805  	for (; i < 2 * pageSize; i++) {
   806  	data[i] = 'x';
   807  	}
   808  	data[2*pageSize -1 ] = 0;
   809  	return data+start;
   810  }
   811  #else
   812  char *dangerousString1() { return NULL; }
   813  char *dangerousString2() { return NULL; }
   814  #endif
   815  
   816  // issue 26066
   817  const unsigned long long int issue26066 = (const unsigned long long) -1;
   818  
   819  // issue 26517
   820  // Introduce two pointer types which are distinct, but have the same
   821  // base type. Make sure that both of those pointer types get resolved
   822  // correctly. Before the fix for 26517 if one of these pointer types
   823  // was resolved before the other one was processed, the second one
   824  // would never be resolved.
   825  // Before this issue was fixed this test failed on Windows,
   826  // where va_list expands to a named char* type.
   827  typedef va_list TypeOne;
   828  typedef char *TypeTwo;
   829  
   830  // issue 28540
   831  
   832  static void twoargs1(void *p, int n) {}
   833  static void *twoargs2() { return 0; }
   834  static int twoargs3(void * p) { return 0; }
   835  
   836  // issue 28545
   837  // Failed to add type conversion for negative constant.
   838  
   839  static void issue28545F(char **p, int n, complex double a) {}
   840  
   841  // issue 28772 part 1 - part 2 in testx.go
   842  // Failed to add type conversion for Go constant set to C constant.
   843  // No runtime test; just make sure it compiles.
   844  
   845  #define issue28772Constant 1
   846  
   847  // issue 28896
   848  // cgo was incorrectly adding padding after a packed struct.
   849  typedef struct {
   850  	void *f1;
   851  	uint32_t f2;
   852  } __attribute__((__packed__)) innerPacked;
   853  
   854  typedef struct {
   855  	innerPacked g1;
   856  	uint64_t g2;
   857  } outerPacked;
   858  
   859  typedef struct {
   860  	void *f1;
   861  	uint32_t f2;
   862  } innerUnpacked;
   863  
   864  typedef struct {
   865  	innerUnpacked g1;
   866  	uint64_t g2;
   867  } outerUnpacked;
   868  
   869  size_t offset(int x) {
   870  	switch (x) {
   871  	case 0:
   872  		return offsetof(innerPacked, f2);
   873  	case 1:
   874  		return offsetof(outerPacked, g2);
   875  	case 2:
   876  		return offsetof(innerUnpacked, f2);
   877  	case 3:
   878  		return offsetof(outerUnpacked, g2);
   879  	default:
   880  		abort();
   881  	}
   882  }
   883  
   884  // issue 29748
   885  
   886  typedef struct { char **p; } S29748;
   887  static int f29748(S29748 *p) { return 0; }
   888  
   889  // issue 29781
   890  // Error with newline inserted into constant expression.
   891  // Compilation test only, nothing to run.
   892  
   893  static void issue29781F(char **p, int n) {}
   894  #define ISSUE29781C 0
   895  
   896  // issue 31093
   897  static uint16_t issue31093F(uint16_t v) { return v; }
   898  
   899  // issue 32579
   900  typedef struct S32579 { unsigned char data[1]; } S32579;
   901  
   902  // issue 37033, cgo.Handle
   903  extern void GoFunc37033(uintptr_t handle);
   904  void cFunc37033(uintptr_t handle) { GoFunc37033(handle); }
   905  
   906  // issue 38649
   907  // Test that #define'd type aliases work.
   908  #define netbsd_gid unsigned int
   909  
   910  // issue 40494
   911  // Inconsistent handling of tagged enum and union types.
   912  enum Enum40494 { X_40494 };
   913  union Union40494 { int x; };
   914  void issue40494(enum Enum40494 e, union Union40494* up) {}
   915  
   916  // Issue 45451, bad handling of go:notinheap types.
   917  typedef struct issue45451Undefined issue45451;
   918  */
   919  import "C"
   920  
   921  import (
   922  	"context"
   923  	"fmt"
   924  	"math"
   925  	"math/rand"
   926  	"os"
   927  	"os/signal"
   928  	"reflect"
   929  	"runtime"
   930  	"runtime/cgo"
   931  	"sync"
   932  	"syscall"
   933  	"testing"
   934  	"time"
   935  	"unsafe"
   936  )
   937  
   938  // alignment
   939  
   940  func testAlign(t *testing.T) {
   941  	var evt C.SDL_KeyboardEvent
   942  	C.makeEvent(&evt)
   943  	if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 {
   944  		t.Error("*** bad alignment")
   945  		C.cTest(&evt)
   946  		t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n",
   947  			evt.typ, evt.which, evt.state, evt.keysym.scancode,
   948  			evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode)
   949  		t.Error(evt)
   950  	}
   951  }
   952  
   953  // api
   954  
   955  const greeting = "hello, world"
   956  
   957  type testPair struct {
   958  	Name      string
   959  	Got, Want interface{}
   960  }
   961  
   962  var testPairs = []testPair{
   963  	{"GoString", C.GoString(C.greeting), greeting},
   964  	{"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
   965  	{"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
   966  }
   967  
   968  func testHelpers(t *testing.T) {
   969  	for _, pair := range testPairs {
   970  		if !reflect.DeepEqual(pair.Got, pair.Want) {
   971  			t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
   972  		}
   973  	}
   974  }
   975  
   976  // basic test cases
   977  
   978  const EINVAL = C.EINVAL /* test #define */
   979  
   980  var KILO = C.KILO
   981  
   982  func uuidgen() {
   983  	var uuid C.cgo_uuid_t
   984  	C.uuid_generate(&uuid[0])
   985  }
   986  
   987  func Strtol(s string, base int) (int, error) {
   988  	p := C.CString(s)
   989  	n, err := C.strtol(p, nil, C.int(base))
   990  	C.free(unsafe.Pointer(p))
   991  	return int(n), err
   992  }
   993  
   994  func Atol(s string) int {
   995  	p := C.CString(s)
   996  	n := C.atol(p)
   997  	C.free(unsafe.Pointer(p))
   998  	return int(n)
   999  }
  1000  
  1001  func testConst(t *testing.T) {
  1002  	C.myConstFunc(nil, 0, nil)
  1003  }
  1004  
  1005  func testEnum(t *testing.T) {
  1006  	if C.Enum1 != 1 || C.Enum2 != 2 {
  1007  		t.Error("bad enum", C.Enum1, C.Enum2)
  1008  	}
  1009  }
  1010  
  1011  func testNamedEnum(t *testing.T) {
  1012  	e := new(C.enum_E)
  1013  
  1014  	*e = C.Enum1
  1015  	if *e != 1 {
  1016  		t.Error("bad enum", C.Enum1)
  1017  	}
  1018  
  1019  	*e = C.Enum2
  1020  	if *e != 2 {
  1021  		t.Error("bad enum", C.Enum2)
  1022  	}
  1023  }
  1024  
  1025  func testCastToEnum(t *testing.T) {
  1026  	e := C.enum_E(C.Enum1)
  1027  	if e != 1 {
  1028  		t.Error("bad enum", C.Enum1)
  1029  	}
  1030  
  1031  	e = C.enum_E(C.Enum2)
  1032  	if e != 2 {
  1033  		t.Error("bad enum", C.Enum2)
  1034  	}
  1035  }
  1036  
  1037  func testAtol(t *testing.T) {
  1038  	l := Atol("123")
  1039  	if l != 123 {
  1040  		t.Error("Atol 123: ", l)
  1041  	}
  1042  }
  1043  
  1044  func testErrno(t *testing.T) {
  1045  	p := C.CString("no-such-file")
  1046  	m := C.CString("r")
  1047  	f, err := C.fopen(p, m)
  1048  	C.free(unsafe.Pointer(p))
  1049  	C.free(unsafe.Pointer(m))
  1050  	if err == nil {
  1051  		C.fclose(f)
  1052  		t.Fatalf("C.fopen: should fail")
  1053  	}
  1054  	if err != syscall.ENOENT {
  1055  		t.Fatalf("C.fopen: unexpected error: %v", err)
  1056  	}
  1057  }
  1058  
  1059  func testMultipleAssign(t *testing.T) {
  1060  	p := C.CString("234")
  1061  	n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
  1062  	if runtime.GOOS == "openbsd" {
  1063  		// Bug in OpenBSD strtol(3) - base > 36 succeeds.
  1064  		if (n != 0 && n != 239089) || m != 234 {
  1065  			t.Fatal("Strtol x2: ", n, m)
  1066  		}
  1067  	} else if n != 0 || m != 234 {
  1068  		t.Fatal("Strtol x2: ", n, m)
  1069  	}
  1070  	C.free(unsafe.Pointer(p))
  1071  }
  1072  
  1073  var (
  1074  	cuint  = (C.uint)(0)
  1075  	culong C.ulong
  1076  	cchar  C.char
  1077  )
  1078  
  1079  type Context struct {
  1080  	ctx *C.struct_ibv_context
  1081  }
  1082  
  1083  func benchCgoCall(b *testing.B) {
  1084  	b.Run("add-int", func(b *testing.B) {
  1085  		const x = C.int(2)
  1086  		const y = C.int(3)
  1087  
  1088  		for i := 0; i < b.N; i++ {
  1089  			C.add(x, y)
  1090  		}
  1091  	})
  1092  
  1093  	b.Run("one-pointer", func(b *testing.B) {
  1094  		var a0 C.VkDeviceCreateInfo
  1095  		for i := 0; i < b.N; i++ {
  1096  			C.handleComplexPointer(&a0)
  1097  		}
  1098  	})
  1099  	b.Run("eight-pointers", func(b *testing.B) {
  1100  		var a0, a1, a2, a3, a4, a5, a6, a7 C.VkDeviceCreateInfo
  1101  		for i := 0; i < b.N; i++ {
  1102  			C.handleComplexPointer8(&a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7)
  1103  		}
  1104  	})
  1105  	b.Run("eight-pointers-nil", func(b *testing.B) {
  1106  		var a0, a1, a2, a3, a4, a5, a6, a7 *C.VkDeviceCreateInfo
  1107  		for i := 0; i < b.N; i++ {
  1108  			C.handleComplexPointer8(a0, a1, a2, a3, a4, a5, a6, a7)
  1109  		}
  1110  	})
  1111  	b.Run("eight-pointers-array", func(b *testing.B) {
  1112  		var a [8]C.VkDeviceCreateInfo
  1113  		for i := 0; i < b.N; i++ {
  1114  			C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
  1115  		}
  1116  	})
  1117  	b.Run("eight-pointers-slice", func(b *testing.B) {
  1118  		a := make([]C.VkDeviceCreateInfo, 8)
  1119  		for i := 0; i < b.N; i++ {
  1120  			C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
  1121  		}
  1122  	})
  1123  }
  1124  
  1125  // Benchmark measuring overhead from Go to C and back to Go (via a callback)
  1126  func benchCallback(b *testing.B) {
  1127  	var x = false
  1128  	for i := 0; i < b.N; i++ {
  1129  		nestedCall(func() { x = true })
  1130  	}
  1131  	if !x {
  1132  		b.Fatal("nestedCall was not invoked")
  1133  	}
  1134  }
  1135  
  1136  var sinkString string
  1137  
  1138  func benchGoString(b *testing.B) {
  1139  	for i := 0; i < b.N; i++ {
  1140  		sinkString = C.GoString(C.cstr)
  1141  	}
  1142  	const want = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  1143  	if sinkString != want {
  1144  		b.Fatalf("%q != %q", sinkString, want)
  1145  	}
  1146  }
  1147  
  1148  // Static (build-time) test that syntax traversal visits all operands of s[i:j:k].
  1149  func sliceOperands(array [2000]int) {
  1150  	_ = array[C.KILO:C.KILO:C.KILO] // no type error
  1151  }
  1152  
  1153  // set in cgo_thread_lock.go init
  1154  var testThreadLockFunc = func(*testing.T) {}
  1155  
  1156  // complex alignment
  1157  
  1158  func TestComplexAlign(t *testing.T) {
  1159  	if C.cplxAlign.x != 3.14 {
  1160  		t.Errorf("got %v, expected 3.14", C.cplxAlign.x)
  1161  	}
  1162  	if C.cplxAlign.y != 2.17 {
  1163  		t.Errorf("got %v, expected 2.17", C.cplxAlign.y)
  1164  	}
  1165  }
  1166  
  1167  // constants and pointer checking
  1168  
  1169  func testCheckConst(t *testing.T) {
  1170  	// The test is that this compiles successfully.
  1171  	p := C.malloc(C.size_t(unsafe.Sizeof(C.int(0))))
  1172  	defer C.free(p)
  1173  	C.CheckConstFunc(&C.CheckConstStruct{(*C.int)(p)}, C.CheckConstVal)
  1174  }
  1175  
  1176  // duplicate symbol
  1177  
  1178  func duplicateSymbols() {
  1179  	fmt.Printf("%v %v %v\n", C.base_symbol, C.alias_one, C.alias_two)
  1180  }
  1181  
  1182  // environment
  1183  
  1184  // This is really an os package test but here for convenience.
  1185  func testSetEnv(t *testing.T) {
  1186  	if runtime.GOOS == "windows" {
  1187  		// Go uses SetEnvironmentVariable on windows. However,
  1188  		// C runtime takes a *copy* at process startup of the
  1189  		// OS environment, and stores it in environ/envp.
  1190  		// It is this copy that	getenv/putenv manipulate.
  1191  		t.Logf("skipping test")
  1192  		return
  1193  	}
  1194  	const key = "CGO_OS_TEST_KEY"
  1195  	const val = "CGO_OS_TEST_VALUE"
  1196  	os.Setenv(key, val)
  1197  	keyc := C.CString(key)
  1198  	defer C.free(unsafe.Pointer(keyc))
  1199  	v := C.getenv(keyc)
  1200  	if uintptr(unsafe.Pointer(v)) == 0 {
  1201  		t.Fatal("getenv returned NULL")
  1202  	}
  1203  	vs := C.GoString(v)
  1204  	if vs != val {
  1205  		t.Fatalf("getenv() = %q; want %q", vs, val)
  1206  	}
  1207  }
  1208  
  1209  // function pointer variables
  1210  
  1211  func callBridge(f C.intFunc) int {
  1212  	return int(C.bridge_int_func(f))
  1213  }
  1214  
  1215  func callCBridge(f C.intFunc) C.int {
  1216  	return C.bridge_int_func(f)
  1217  }
  1218  
  1219  func testFpVar(t *testing.T) {
  1220  	const expected = 42
  1221  	f := C.intFunc(C.fortytwo)
  1222  	res1 := C.bridge_int_func(f)
  1223  	if r1 := int(res1); r1 != expected {
  1224  		t.Errorf("got %d, want %d", r1, expected)
  1225  	}
  1226  	res2 := callCBridge(f)
  1227  	if r2 := int(res2); r2 != expected {
  1228  		t.Errorf("got %d, want %d", r2, expected)
  1229  	}
  1230  	r3 := callBridge(f)
  1231  	if r3 != expected {
  1232  		t.Errorf("got %d, want %d", r3, expected)
  1233  	}
  1234  }
  1235  
  1236  // issue 1222
  1237  type AsyncEvent struct {
  1238  	event C.struct_ibv_async_event
  1239  }
  1240  
  1241  // issue 1635
  1242  
  1243  func test1635(t *testing.T) {
  1244  	C.scatter()
  1245  	if v := C.hola; v != 0 {
  1246  		t.Fatalf("C.hola is %d, should be 0", v)
  1247  	}
  1248  	if v := C.testHola(); v != 0 {
  1249  		t.Fatalf("C.testHola() is %d, should be 0", v)
  1250  	}
  1251  }
  1252  
  1253  // issue 2470
  1254  
  1255  func testUnsignedInt(t *testing.T) {
  1256  	a := (int64)(C.UINT32VAL)
  1257  	b := (int64)(0xc008427b)
  1258  	if a != b {
  1259  		t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
  1260  	}
  1261  }
  1262  
  1263  // issue 3250
  1264  
  1265  func test3250(t *testing.T) {
  1266  	if runtime.GOOS == "windows" {
  1267  		t.Skip("not applicable on windows")
  1268  	}
  1269  
  1270  	t.Skip("skipped, see golang.org/issue/5885")
  1271  	var (
  1272  		thres = 1
  1273  		sig   = syscall_dot_SIGCHLD
  1274  	)
  1275  	type result struct {
  1276  		n   int
  1277  		sig os.Signal
  1278  	}
  1279  	var (
  1280  		sigCh     = make(chan os.Signal, 10)
  1281  		waitStart = make(chan struct{})
  1282  		waitDone  = make(chan result)
  1283  	)
  1284  
  1285  	signal.Notify(sigCh, sig)
  1286  
  1287  	go func() {
  1288  		n := 0
  1289  		alarm := time.After(time.Second * 3)
  1290  		for {
  1291  			select {
  1292  			case <-waitStart:
  1293  				waitStart = nil
  1294  			case v := <-sigCh:
  1295  				n++
  1296  				if v != sig || n > thres {
  1297  					waitDone <- result{n, v}
  1298  					return
  1299  				}
  1300  			case <-alarm:
  1301  				waitDone <- result{n, sig}
  1302  				return
  1303  			}
  1304  		}
  1305  	}()
  1306  
  1307  	waitStart <- struct{}{}
  1308  	C.testSendSIG()
  1309  	r := <-waitDone
  1310  	if r.sig != sig {
  1311  		t.Fatalf("received signal %v, but want %v", r.sig, sig)
  1312  	}
  1313  	t.Logf("got %d signals\n", r.n)
  1314  	if r.n <= thres {
  1315  		t.Fatalf("expected more than %d", thres)
  1316  	}
  1317  }
  1318  
  1319  // issue 3261
  1320  
  1321  func testLibgcc(t *testing.T) {
  1322  	var table = []struct {
  1323  		in, out C.int
  1324  	}{
  1325  		{0, 0},
  1326  		{1, 1},
  1327  		{-42, 42},
  1328  		{1000300, 1000300},
  1329  		{1 - 1<<31, 1<<31 - 1},
  1330  	}
  1331  	for _, v := range table {
  1332  		if o := C.vabs(v.in); o != v.out {
  1333  			t.Fatalf("abs(%d) got %d, should be %d", v.in, o, v.out)
  1334  			return
  1335  		}
  1336  	}
  1337  }
  1338  
  1339  // issue 3729
  1340  
  1341  func test3729(t *testing.T) {
  1342  	if runtime.GOOS == "windows" {
  1343  		t.Skip("skipping on windows")
  1344  	}
  1345  
  1346  	_, e := C.g()
  1347  	if e != syscall.E2BIG {
  1348  		t.Errorf("got %q, expect %q", e, syscall.E2BIG)
  1349  	}
  1350  	_, e = C.g2(C.EINVAL, C._expA, C._expB, C._expC, C._expD)
  1351  	if e != syscall.EINVAL {
  1352  		t.Errorf("got %q, expect %q", e, syscall.EINVAL)
  1353  	}
  1354  }
  1355  
  1356  // issue 3945
  1357  
  1358  func testPrintf(t *testing.T) {
  1359  	C.say()
  1360  }
  1361  
  1362  // issue 4054
  1363  
  1364  var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J}
  1365  
  1366  // issue 4339
  1367  
  1368  func test4339(t *testing.T) {
  1369  	C.handle4339(&C.exported4339)
  1370  }
  1371  
  1372  // issue 4417
  1373  
  1374  func testBoolAlign(t *testing.T) {
  1375  	b := C.c_bool(true, true, 10, true, false)
  1376  	if b != 10 {
  1377  		t.Fatalf("found %d expected 10\n", b)
  1378  	}
  1379  	b = C.c_bool(true, true, 5, true, true)
  1380  	if b != 5 {
  1381  		t.Fatalf("found %d expected 5\n", b)
  1382  	}
  1383  	b = C.c_bool(true, true, 3, true, false)
  1384  	if b != 3 {
  1385  		t.Fatalf("found %d expected 3\n", b)
  1386  	}
  1387  	b = C.c_bool(false, false, 1, true, false)
  1388  	if b != 1 {
  1389  		t.Fatalf("found %d expected 1\n", b)
  1390  	}
  1391  	b = C.c_bool(false, true, 200, true, false)
  1392  	if b != 200 {
  1393  		t.Fatalf("found %d expected 200\n", b)
  1394  	}
  1395  }
  1396  
  1397  // issue 4857
  1398  
  1399  func test4857() {
  1400  	_ = C.issue4857()
  1401  }
  1402  
  1403  // issue 5224
  1404  
  1405  func testCflags(t *testing.T) {
  1406  	is_windows := C.is_windows == 1
  1407  	if is_windows != (runtime.GOOS == "windows") {
  1408  		t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS)
  1409  	}
  1410  	if C.common != 123 {
  1411  		t.Errorf("common: %v (expected 123)", C.common)
  1412  	}
  1413  }
  1414  
  1415  // issue 5227
  1416  
  1417  func test5227(t *testing.T) {
  1418  	C.init()
  1419  }
  1420  
  1421  func selectfont() C.Fontinfo {
  1422  	return C.SansTypeface
  1423  }
  1424  
  1425  // issue 5242
  1426  
  1427  func test5242(t *testing.T) {
  1428  	if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 {
  1429  		t.Errorf("got %v", got)
  1430  	}
  1431  }
  1432  
  1433  func test5603(t *testing.T) {
  1434  	var x [5]int64
  1435  	exp := int64(C.issue5603exp)
  1436  	x[0] = int64(C.issue5603foo0())
  1437  	x[1] = int64(C.issue5603foo1(nil))
  1438  	x[2] = int64(C.issue5603foo2(nil, nil))
  1439  	x[3] = int64(C.issue5603foo3(nil, nil, nil))
  1440  	x[4] = int64(C.issue5603foo4(nil, nil, nil, nil))
  1441  	for i, v := range x {
  1442  		if v != exp {
  1443  			t.Errorf("issue5603foo%d() returns %v, expected %v", i, v, exp)
  1444  		}
  1445  	}
  1446  }
  1447  
  1448  // issue 5337
  1449  
  1450  func test5337(t *testing.T) {
  1451  	C.test5337()
  1452  }
  1453  
  1454  // issue 5740
  1455  
  1456  func test5740(t *testing.T) {
  1457  	if v := C.test5740a() + C.test5740b(); v != 5 {
  1458  		t.Errorf("expected 5, got %v", v)
  1459  	}
  1460  }
  1461  
  1462  // issue 5986
  1463  
  1464  func test5986(t *testing.T) {
  1465  	C.output5986()
  1466  }
  1467  
  1468  // issue 6128
  1469  
  1470  func test6128() {
  1471  	// nothing to run, just make sure this compiles.
  1472  	_ = C.X
  1473  }
  1474  
  1475  // issue 6390
  1476  
  1477  func test6390(t *testing.T) {
  1478  	p1 := C.malloc(1024)
  1479  	if p1 == nil {
  1480  		t.Fatalf("C.malloc(1024) returned nil")
  1481  	}
  1482  	p2 := C.malloc(0)
  1483  	if p2 == nil {
  1484  		t.Fatalf("C.malloc(0) returned nil")
  1485  	}
  1486  	C.free(p1)
  1487  	C.free(p2)
  1488  }
  1489  
  1490  func test6472() {
  1491  	// nothing to run, just make sure this compiles
  1492  	s := new(C.z)
  1493  	println(s.y[0].x)
  1494  }
  1495  
  1496  // issue 6506
  1497  
  1498  func test6506() {
  1499  	// nothing to run, just make sure this compiles
  1500  	var x C.size_t
  1501  
  1502  	C.calloc(x, x)
  1503  	C.malloc(x)
  1504  	C.realloc(nil, x)
  1505  	C.memcpy(nil, nil, x)
  1506  	C.memcmp(nil, nil, x)
  1507  	C.memmove(nil, nil, x)
  1508  	C.strncpy(nil, nil, x)
  1509  	C.strncmp(nil, nil, x)
  1510  	C.strncat(nil, nil, x)
  1511  	x = C.strxfrm(nil, nil, x)
  1512  	C.memchr(nil, 0, x)
  1513  	x = C.strcspn(nil, nil)
  1514  	x = C.strspn(nil, nil)
  1515  	C.memset(nil, 0, x)
  1516  	x = C.strlen(nil)
  1517  	_ = x
  1518  }
  1519  
  1520  // issue 6612
  1521  
  1522  func testNaming(t *testing.T) {
  1523  	C.myfunc()
  1524  	C.myfunc_def()
  1525  	if v := C.myvar; v != 5 {
  1526  		t.Errorf("C.myvar = %d, want 5", v)
  1527  	}
  1528  	if v := C.myvar_def; v != 5 {
  1529  		t.Errorf("C.myvar_def = %d, want 5", v)
  1530  	}
  1531  	if s := C.GoString(C.mytext); s != "abcdef" {
  1532  		t.Errorf("C.mytext = %q, want %q", s, "abcdef")
  1533  	}
  1534  	if s := C.GoString(C.mytext_def); s != "abcdef" {
  1535  		t.Errorf("C.mytext_def = %q, want %q", s, "abcdef")
  1536  	}
  1537  	if c := C.myenum; c != 1234 {
  1538  		t.Errorf("C.myenum = %v, want 1234", c)
  1539  	}
  1540  	if c := C.myenum_def; c != 1234 {
  1541  		t.Errorf("C.myenum_def = %v, want 1234", c)
  1542  	}
  1543  	{
  1544  		const c = C.myenum
  1545  		if c != 1234 {
  1546  			t.Errorf("C.myenum as const = %v, want 1234", c)
  1547  		}
  1548  	}
  1549  	{
  1550  		const c = C.myenum_def
  1551  		if c != 1234 {
  1552  			t.Errorf("C.myenum as const = %v, want 1234", c)
  1553  		}
  1554  	}
  1555  	if c := C.myint_def; c != 12345 {
  1556  		t.Errorf("C.myint_def = %v, want 12345", c)
  1557  	}
  1558  	{
  1559  		const c = C.myint_def
  1560  		if c != 12345 {
  1561  			t.Errorf("C.myint as const = %v, want 12345", c)
  1562  		}
  1563  	}
  1564  
  1565  	if c := C.myfloat_def; c != 1.5 {
  1566  		t.Errorf("C.myint_def = %v, want 1.5", c)
  1567  	}
  1568  	{
  1569  		const c = C.myfloat_def
  1570  		if c != 1.5 {
  1571  			t.Errorf("C.myint as const = %v, want 1.5", c)
  1572  		}
  1573  	}
  1574  
  1575  	if s := C.mystring_def; s != "hello" {
  1576  		t.Errorf("C.mystring_def = %q, want %q", s, "hello")
  1577  	}
  1578  }
  1579  
  1580  // issue 6907
  1581  
  1582  func test6907(t *testing.T) {
  1583  	want := "yarn"
  1584  	if got := C.GoString(C.Issue6907CopyString(want)); got != want {
  1585  		t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want)
  1586  	}
  1587  }
  1588  
  1589  // issue 7560
  1590  
  1591  func test7560(t *testing.T) {
  1592  	// some mingw don't implement __packed__ correctly.
  1593  	if C.offset7560() != 1 {
  1594  		t.Skip("C compiler did not pack struct")
  1595  	}
  1596  
  1597  	// C.misaligned should have x but then a padding field to get to the end of the struct.
  1598  	// There should not be a field named 'y'.
  1599  	var v C.misaligned
  1600  	rt := reflect.TypeOf(&v).Elem()
  1601  	if rt.NumField() != 2 || rt.Field(0).Name != "x" || rt.Field(1).Name != "_" {
  1602  		t.Errorf("unexpected fields in C.misaligned:\n")
  1603  		for i := 0; i < rt.NumField(); i++ {
  1604  			t.Logf("%+v\n", rt.Field(i))
  1605  		}
  1606  	}
  1607  }
  1608  
  1609  // issue 7786
  1610  
  1611  func f() {
  1612  	var x1 *C.typedef_test7786
  1613  	var x2 *C.struct_test7786
  1614  	x1 = x2
  1615  	x2 = x1
  1616  	C.f7786(x1)
  1617  	C.f7786(x2)
  1618  	C.g7786(x1)
  1619  	C.g7786(x2)
  1620  
  1621  	var b1 *C.typedef_body7786
  1622  	var b2 *C.struct_body7786
  1623  	b1 = b2
  1624  	b2 = b1
  1625  	C.b7786(b1)
  1626  	C.b7786(b2)
  1627  	C.c7786(b1)
  1628  	C.c7786(b2)
  1629  
  1630  	var u1 *C.typedef_union7786
  1631  	var u2 *C.union_union7786
  1632  	u1 = u2
  1633  	u2 = u1
  1634  	C.u7786(u1)
  1635  	C.u7786(u2)
  1636  	C.v7786(u1)
  1637  	C.v7786(u2)
  1638  }
  1639  
  1640  // issue 8092
  1641  
  1642  func test8092(t *testing.T) {
  1643  	tests := []struct {
  1644  		s    string
  1645  		a, b *C.char
  1646  	}{
  1647  		{"text", &C.text[0], C.ctext()},
  1648  		{"data", &C.data[0], C.cdata()},
  1649  	}
  1650  	for _, test := range tests {
  1651  		if test.a != test.b {
  1652  			t.Errorf("%s: pointer mismatch: %v != %v", test.s, test.a, test.b)
  1653  		}
  1654  		if got := C.GoString(test.a); got != test.s {
  1655  			t.Errorf("%s: points at %#v, want %#v", test.s, got, test.s)
  1656  		}
  1657  	}
  1658  }
  1659  
  1660  // issues 8368 and 8441
  1661  
  1662  func issue8368(one *C.struct_one, two *C.struct_two) {
  1663  }
  1664  
  1665  func issue8441(one *C.one, two *C.two) {
  1666  	issue8441(two.x, one.x)
  1667  }
  1668  
  1669  // issue 8428
  1670  
  1671  var _ = C.struct_issue8428one{
  1672  	b: C.char(0),
  1673  	// The trailing rest field is not available in cgo.
  1674  	// See issue 11925.
  1675  	// rest: [0]C.char{},
  1676  }
  1677  
  1678  var _ = C.struct_issue8428two{
  1679  	p:    unsafe.Pointer(nil),
  1680  	b:    C.char(0),
  1681  	rest: [0]C.char{},
  1682  }
  1683  
  1684  var _ = C.struct_issue8428three{
  1685  	w: [1][2][3][0]C.char{},
  1686  	x: [2][3][0][1]C.char{},
  1687  	y: [3][0][1][2]C.char{},
  1688  	z: [0][1][2][3]C.char{},
  1689  }
  1690  
  1691  // issue 8811
  1692  
  1693  func test8811(t *testing.T) {
  1694  	C.issue8811Execute()
  1695  }
  1696  
  1697  // issue 9557
  1698  
  1699  func test9557(t *testing.T) {
  1700  	// implicitly dereference a Go variable
  1701  	foo := C.issue9557foo
  1702  	if v := foo.a; v != 42 {
  1703  		t.Fatalf("foo.a expected 42, but got %d", v)
  1704  	}
  1705  
  1706  	// explicitly dereference a C variable
  1707  	if v := (*C.issue9557foo).a; v != 42 {
  1708  		t.Fatalf("(*C.issue9557foo).a expected 42, but is %d", v)
  1709  	}
  1710  
  1711  	// implicitly dereference a C variable
  1712  	if v := C.issue9557foo.a; v != 42 {
  1713  		t.Fatalf("C.issue9557foo.a expected 42, but is %d", v)
  1714  	}
  1715  }
  1716  
  1717  // issue 8331 part 1
  1718  
  1719  func issue8331a() C.issue8331 {
  1720  	return issue8331Var
  1721  }
  1722  
  1723  // issue 10303
  1724  
  1725  func test10303(t *testing.T, n int) {
  1726  	if runtime.Compiler == "gccgo" {
  1727  		t.Skip("gccgo permits C pointers on the stack")
  1728  	}
  1729  
  1730  	// Run at a few different stack depths just to avoid an unlucky pass
  1731  	// due to variables ending up on different pages.
  1732  	if n > 0 {
  1733  		test10303(t, n-1)
  1734  	}
  1735  	if t.Failed() {
  1736  		return
  1737  	}
  1738  	var x, y, z, v, si C.int
  1739  	var s C.Struct
  1740  	C.setintstar(&x)
  1741  	C.setintptr(&y)
  1742  	C.setvoidptr(unsafe.Pointer(&v))
  1743  	s.P = &si
  1744  	C.setstruct(s)
  1745  
  1746  	if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
  1747  		t.Error("C int* argument on stack")
  1748  	}
  1749  	if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
  1750  		t.Error("C intptr argument on stack")
  1751  	}
  1752  	if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
  1753  		t.Error("C void* argument on stack")
  1754  	}
  1755  	if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
  1756  		t.Error("C struct field pointer on stack")
  1757  	}
  1758  }
  1759  
  1760  // issue 11925
  1761  
  1762  func test11925(t *testing.T) {
  1763  	if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) {
  1764  		t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{}))
  1765  	}
  1766  	if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) {
  1767  		t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{}))
  1768  	}
  1769  }
  1770  
  1771  // issue 12030
  1772  
  1773  func test12030(t *testing.T) {
  1774  	buf := (*C.char)(C.malloc(256))
  1775  	defer C.free(unsafe.Pointer(buf))
  1776  	for _, f := range []float64{1.0, 2.0, 3.14} {
  1777  		C.issue12030conv(buf, C.double(f))
  1778  		got := C.GoString(buf)
  1779  		if want := fmt.Sprintf("d=%g", f); got != want {
  1780  			t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want)
  1781  		}
  1782  	}
  1783  }
  1784  
  1785  // issue 13402
  1786  
  1787  var _ C.complexfloat
  1788  var _ C.complexdouble
  1789  
  1790  // issue 13930
  1791  // Test that cgo's multiple-value special form for
  1792  // C function calls works in variable declaration statements.
  1793  
  1794  var _, _ = C.abs(0)
  1795  
  1796  // issue 14838
  1797  
  1798  func test14838(t *testing.T) {
  1799  	data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  1800  	cData := C.CBytes(data)
  1801  	defer C.free(cData)
  1802  
  1803  	if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 {
  1804  		t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:])
  1805  	}
  1806  }
  1807  
  1808  // issue 17065
  1809  
  1810  var sink C.int
  1811  
  1812  func test17065(t *testing.T) {
  1813  	if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
  1814  		t.Skip("broken on darwin; issue 17065")
  1815  	}
  1816  	for i := range C.ii {
  1817  		sink = C.ii[i]
  1818  	}
  1819  }
  1820  
  1821  // issue 17537
  1822  
  1823  func test17537(t *testing.T) {
  1824  	v := C.S17537{i: 17537}
  1825  	if got, want := C.I17537(&v), C.int(17537); got != want {
  1826  		t.Errorf("got %d, want %d", got, want)
  1827  	}
  1828  
  1829  	p := (*C.char)(C.malloc(1))
  1830  	*p = 17
  1831  	if got, want := C.F17537(&p), C.int(17); got != want {
  1832  		t.Errorf("got %d, want %d", got, want)
  1833  	}
  1834  
  1835  	C.F18298(nil)
  1836  	var v18298 C.T18298_2
  1837  	C.G18298(C.T18298_1(v18298))
  1838  }
  1839  
  1840  // issue 17723
  1841  
  1842  func testAPI() {
  1843  	var cs *C.char
  1844  	cs = C.CString("hello")
  1845  	defer C.free(unsafe.Pointer(cs))
  1846  	var s string
  1847  	s = C.GoString((*C.char)(C.api_hello))
  1848  	s = C.GoStringN((*C.char)(C.api_hello), C.int(6))
  1849  	var b []byte
  1850  	b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6))
  1851  	_, _ = s, b
  1852  	C.cstring_pointer_fun(nil)
  1853  }
  1854  
  1855  // issue 18126
  1856  
  1857  func test18126(t *testing.T) {
  1858  	p := C.malloc(1)
  1859  	_, err := C.Issue18126C(&p)
  1860  	C.free(p)
  1861  	_ = err
  1862  }
  1863  
  1864  // issue 18720
  1865  
  1866  func test18720(t *testing.T) {
  1867  	if got, want := C.HELLO_WORLD, "hello\000world"; got != want {
  1868  		t.Errorf("C.HELLO_WORLD == %q, expected %q", got, want)
  1869  	}
  1870  
  1871  	if got, want := C.VAR1, C.int(5); got != want {
  1872  		t.Errorf("C.VAR1 == %v, expected %v", got, want)
  1873  	}
  1874  
  1875  	if got, want := *C.ADDR, C.int(5); got != want {
  1876  		t.Errorf("*C.ADDR == %v, expected %v", got, want)
  1877  	}
  1878  
  1879  	if got, want := C.CALL, C.int(6); got != want {
  1880  		t.Errorf("C.CALL == %v, expected %v", got, want)
  1881  	}
  1882  
  1883  	if got, want := C.CALL, C.int(7); got != want {
  1884  		t.Errorf("C.CALL == %v, expected %v", got, want)
  1885  	}
  1886  
  1887  	// Issue 20125.
  1888  	if got, want := C.SIZE_OF_FOO, 1; got != want {
  1889  		t.Errorf("C.SIZE_OF_FOO == %v, expected %v", got, want)
  1890  	}
  1891  }
  1892  
  1893  // issue 20129
  1894  
  1895  func test20129(t *testing.T) {
  1896  	if C.issue20129 != 0 {
  1897  		t.Fatal("test is broken")
  1898  	}
  1899  	C.issue20129Foo()
  1900  	if C.issue20129 != 1 {
  1901  		t.Errorf("got %v but expected %v", C.issue20129, 1)
  1902  	}
  1903  	C.issue20129Bar()
  1904  	if C.issue20129 != 2 {
  1905  		t.Errorf("got %v but expected %v", C.issue20129, 2)
  1906  	}
  1907  }
  1908  
  1909  // issue 20369
  1910  
  1911  func test20369(t *testing.T) {
  1912  	if C.XUINT64_MAX != math.MaxUint64 {
  1913  		t.Fatalf("got %v, want %v", uint64(C.XUINT64_MAX), uint64(math.MaxUint64))
  1914  	}
  1915  }
  1916  
  1917  // issue 21668
  1918  
  1919  var issue21668_X = C.x21668
  1920  
  1921  // issue 21708
  1922  
  1923  func test21708(t *testing.T) {
  1924  	if got, want := C.CAST_TO_INT64, -1; got != want {
  1925  		t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want)
  1926  	}
  1927  }
  1928  
  1929  // issue 21809
  1930  
  1931  func test21809(t *testing.T) {
  1932  	longVar := C.long(3)
  1933  	typedefVar := C.MySigned_t(4)
  1934  	typedefTypedefVar := C.MySigned2_t(5)
  1935  
  1936  	// all three should be considered identical to `long`
  1937  	if ret := C.takes_long(longVar); ret != 9 {
  1938  		t.Errorf("got %v but expected %v", ret, 9)
  1939  	}
  1940  	if ret := C.takes_long(typedefVar); ret != 16 {
  1941  		t.Errorf("got %v but expected %v", ret, 16)
  1942  	}
  1943  	if ret := C.takes_long(typedefTypedefVar); ret != 25 {
  1944  		t.Errorf("got %v but expected %v", ret, 25)
  1945  	}
  1946  
  1947  	// They should also be identical to the typedef'd type
  1948  	if ret := C.takes_typedef(longVar); ret != 9 {
  1949  		t.Errorf("got %v but expected %v", ret, 9)
  1950  	}
  1951  	if ret := C.takes_typedef(typedefVar); ret != 16 {
  1952  		t.Errorf("got %v but expected %v", ret, 16)
  1953  	}
  1954  	if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
  1955  		t.Errorf("got %v but expected %v", ret, 25)
  1956  	}
  1957  }
  1958  
  1959  // issue 22906
  1960  
  1961  func test22906(t *testing.T) {
  1962  	var x1 C.jobject = 0 // Note: 0, not nil. That makes sure we use uintptr for these types.
  1963  	_ = x1
  1964  	var x2 C.jclass = 0
  1965  	_ = x2
  1966  	var x3 C.jthrowable = 0
  1967  	_ = x3
  1968  	var x4 C.jstring = 0
  1969  	_ = x4
  1970  	var x5 C.jarray = 0
  1971  	_ = x5
  1972  	var x6 C.jbooleanArray = 0
  1973  	_ = x6
  1974  	var x7 C.jbyteArray = 0
  1975  	_ = x7
  1976  	var x8 C.jcharArray = 0
  1977  	_ = x8
  1978  	var x9 C.jshortArray = 0
  1979  	_ = x9
  1980  	var x10 C.jintArray = 0
  1981  	_ = x10
  1982  	var x11 C.jlongArray = 0
  1983  	_ = x11
  1984  	var x12 C.jfloatArray = 0
  1985  	_ = x12
  1986  	var x13 C.jdoubleArray = 0
  1987  	_ = x13
  1988  	var x14 C.jobjectArray = 0
  1989  	_ = x14
  1990  	var x15 C.jweak = 0
  1991  	_ = x15
  1992  }
  1993  
  1994  // issue 22958
  1995  // Nothing to run, just make sure this compiles.
  1996  var Vissue22958 C.issue22958Type
  1997  
  1998  func test23356(t *testing.T) {
  1999  	if got, want := C.a(), C.int(5); got != want {
  2000  		t.Errorf("C.a() == %v, expected %v", got, want)
  2001  	}
  2002  	if got, want := C.r(), C.int(3); got != want {
  2003  		t.Errorf("C.r() == %v, expected %v", got, want)
  2004  	}
  2005  }
  2006  
  2007  // issue 23720
  2008  
  2009  func Issue23720F() {
  2010  	var x C.issue23720A
  2011  	C.issue23720F(x)
  2012  }
  2013  
  2014  // issue 24206
  2015  
  2016  func test24206(t *testing.T) {
  2017  	if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
  2018  		t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
  2019  	}
  2020  
  2021  	if l := len(C.GoString(C.dangerousString1())); l != 123 {
  2022  		t.Errorf("Incorrect string length - got %d, want 123", l)
  2023  	}
  2024  	if l := len(C.GoString(C.dangerousString2())); l != 4096+123 {
  2025  		t.Errorf("Incorrect string length - got %d, want %d", l, 4096+123)
  2026  	}
  2027  }
  2028  
  2029  // issue 25143
  2030  
  2031  func issue25143sum(ns ...C.int) C.int {
  2032  	total := C.int(0)
  2033  	for _, n := range ns {
  2034  		total += n
  2035  	}
  2036  	return total
  2037  }
  2038  
  2039  func test25143(t *testing.T) {
  2040  	if got, want := issue25143sum(1, 2, 3), C.int(6); got != want {
  2041  		t.Errorf("issue25143sum(1, 2, 3) == %v, expected %v", got, want)
  2042  	}
  2043  }
  2044  
  2045  // issue 26066
  2046  // Wrong type of constant with GCC 8 and newer.
  2047  
  2048  func test26066(t *testing.T) {
  2049  	var i = int64(C.issue26066)
  2050  	if i != -1 {
  2051  		t.Errorf("got %d, want -1", i)
  2052  	}
  2053  }
  2054  
  2055  // issue 26517
  2056  var a C.TypeOne
  2057  var b C.TypeTwo
  2058  
  2059  // issue 27660
  2060  // Stress the interaction between the race detector and cgo in an
  2061  // attempt to reproduce the memory corruption described in #27660.
  2062  // The bug was very timing sensitive; at the time of writing this
  2063  // test would only trigger the bug about once out of every five runs.
  2064  
  2065  func test27660(t *testing.T) {
  2066  	ctx, cancel := context.WithCancel(context.Background())
  2067  	defer cancel()
  2068  	ints := make([]int, 100)
  2069  	locks := make([]sync.Mutex, 100)
  2070  	// Slowly create threads so that ThreadSanitizer is forced to
  2071  	// frequently resize its SyncClocks.
  2072  	for i := 0; i < 100; i++ {
  2073  		go func() {
  2074  			for ctx.Err() == nil {
  2075  				// Sleep in C for long enough that it is likely that the runtime
  2076  				// will retake this goroutine's currently wired P.
  2077  				C.usleep(1000 /* 1ms */)
  2078  				runtime.Gosched() // avoid starvation (see #28701)
  2079  			}
  2080  		}()
  2081  		go func() {
  2082  			// Trigger lots of synchronization and memory reads/writes to
  2083  			// increase the likelihood that the race described in #27660
  2084  			// results in corruption of ThreadSanitizer's internal state
  2085  			// and thus an assertion failure or segfault.
  2086  			i := 0
  2087  			for ctx.Err() == nil {
  2088  				j := rand.Intn(100)
  2089  				locks[j].Lock()
  2090  				ints[j]++
  2091  				locks[j].Unlock()
  2092  				// needed for gccgo, to avoid creation of an
  2093  				// unpreemptible "fast path" in this loop. Choice
  2094  				// of (1<<24) is somewhat arbitrary.
  2095  				if i%(1<<24) == 0 {
  2096  					runtime.Gosched()
  2097  				}
  2098  				i++
  2099  
  2100  			}
  2101  		}()
  2102  		time.Sleep(time.Millisecond)
  2103  	}
  2104  }
  2105  
  2106  // issue 28540
  2107  
  2108  func twoargsF() {
  2109  	v := []string{}
  2110  	C.twoargs1(C.twoargs2(), C.twoargs3(unsafe.Pointer(&v)))
  2111  }
  2112  
  2113  // issue 28545
  2114  
  2115  func issue28545G(p **C.char) {
  2116  	C.issue28545F(p, -1, (0))
  2117  	C.issue28545F(p, 2+3, complex(1, 1))
  2118  	C.issue28545F(p, issue28772Constant, issue28772Constant2)
  2119  }
  2120  
  2121  // issue 28772 part 1 - part 2 in testx.go
  2122  
  2123  const issue28772Constant = C.issue28772Constant
  2124  
  2125  // issue 28896
  2126  
  2127  func offset(i int) uintptr {
  2128  	var pi C.innerPacked
  2129  	var po C.outerPacked
  2130  	var ui C.innerUnpacked
  2131  	var uo C.outerUnpacked
  2132  	switch i {
  2133  	case 0:
  2134  		return unsafe.Offsetof(pi.f2)
  2135  	case 1:
  2136  		return unsafe.Offsetof(po.g2)
  2137  	case 2:
  2138  		return unsafe.Offsetof(ui.f2)
  2139  	case 3:
  2140  		return unsafe.Offsetof(uo.g2)
  2141  	default:
  2142  		panic("can't happen")
  2143  	}
  2144  }
  2145  
  2146  func test28896(t *testing.T) {
  2147  	for i := 0; i < 4; i++ {
  2148  		c := uintptr(C.offset(C.int(i)))
  2149  		g := offset(i)
  2150  		if c != g {
  2151  			t.Errorf("%d: C: %d != Go %d", i, c, g)
  2152  		}
  2153  	}
  2154  }
  2155  
  2156  // issue 29383
  2157  // cgo's /*line*/ comments failed when inserted after '/',
  2158  // because the result looked like a "//" comment.
  2159  // No runtime test; just make sure it compiles.
  2160  
  2161  func Issue29383(n, size uint) int {
  2162  	if ^C.size_t(0)/C.size_t(n) < C.size_t(size) {
  2163  		return 0
  2164  	}
  2165  	return 0
  2166  }
  2167  
  2168  // issue 29748
  2169  // Error handling a struct initializer that requires pointer checking.
  2170  // Compilation test only, nothing to run.
  2171  
  2172  var Vissue29748 = C.f29748(&C.S29748{
  2173  	nil,
  2174  })
  2175  
  2176  func Fissue299748() {
  2177  	C.f29748(&C.S29748{
  2178  		nil,
  2179  	})
  2180  }
  2181  
  2182  // issue 29781
  2183  
  2184  var issue29781X struct{ X int }
  2185  
  2186  func issue29781F(...int) int { return 0 }
  2187  
  2188  func issue29781G() {
  2189  	var p *C.char
  2190  	C.issue29781F(&p, C.ISSUE29781C+1)
  2191  	C.issue29781F(nil, (C.int)(
  2192  		0))
  2193  	C.issue29781F(&p, (C.int)(0))
  2194  	C.issue29781F(&p, (C.int)(
  2195  		0))
  2196  	C.issue29781F(&p, (C.int)(issue29781X.
  2197  		X))
  2198  }
  2199  
  2200  // issue 30065
  2201  
  2202  func test30065(t *testing.T) {
  2203  	var a [256]byte
  2204  	b := []byte("a")
  2205  	C.memcpy(unsafe.Pointer(&a), unsafe.Pointer(&b[0]), 1)
  2206  	if a[0] != 'a' {
  2207  		t.Errorf("&a failed: got %c, want %c", a[0], 'a')
  2208  	}
  2209  
  2210  	b = []byte("b")
  2211  	C.memcpy(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), 1)
  2212  	if a[0] != 'b' {
  2213  		t.Errorf("&a[0] failed: got %c, want %c", a[0], 'b')
  2214  	}
  2215  
  2216  	d := make([]byte, 256)
  2217  	b = []byte("c")
  2218  	C.memcpy(unsafe.Pointer(&d[0]), unsafe.Pointer(&b[0]), 1)
  2219  	if d[0] != 'c' {
  2220  		t.Errorf("&d[0] failed: got %c, want %c", d[0], 'c')
  2221  	}
  2222  }
  2223  
  2224  // issue 31093
  2225  // No runtime test; just make sure it compiles.
  2226  
  2227  func Issue31093() {
  2228  	C.issue31093F(C.ushort(0))
  2229  }
  2230  
  2231  // issue 32579
  2232  
  2233  func test32579(t *testing.T) {
  2234  	var s [1]C.struct_S32579
  2235  	C.memset(unsafe.Pointer(&s[0].data[0]), 1, 1)
  2236  	if s[0].data[0] != 1 {
  2237  		t.Errorf("&s[0].data[0] failed: got %d, want %d", s[0].data[0], 1)
  2238  	}
  2239  }
  2240  
  2241  // issue 37033, check if cgo.Handle works properly
  2242  
  2243  func testHandle(t *testing.T) {
  2244  	ch := make(chan int)
  2245  
  2246  	for i := 0; i < 42; i++ {
  2247  		h := cgo.NewHandle(ch)
  2248  		go func() {
  2249  			C.cFunc37033(C.uintptr_t(h))
  2250  		}()
  2251  		if v := <-ch; issue37033 != v {
  2252  			t.Fatalf("unexpected receiving value: got %d, want %d", v, issue37033)
  2253  		}
  2254  		h.Delete()
  2255  	}
  2256  }
  2257  
  2258  // issue 38649
  2259  
  2260  var issue38649 C.netbsd_gid = 42
  2261  
  2262  // issue 39877
  2263  
  2264  var issue39877 *C.void = nil
  2265  
  2266  // issue 40494
  2267  // No runtime test; just make sure it compiles.
  2268  
  2269  func Issue40494() {
  2270  	C.issue40494(C.enum_Enum40494(C.X_40494), (*C.union_Union40494)(nil))
  2271  }
  2272  
  2273  // Issue 45451.
  2274  func test45451(t *testing.T) {
  2275  	var u *C.issue45451
  2276  	typ := reflect.ValueOf(u).Type().Elem()
  2277  
  2278  	// The type is undefined in C so allocating it should panic.
  2279  	defer func() {
  2280  		if r := recover(); r == nil {
  2281  			t.Error("expected panic")
  2282  		}
  2283  	}()
  2284  
  2285  	_ = reflect.New(typ)
  2286  	t.Errorf("reflect.New(%v) should have panicked", typ)
  2287  }
  2288  

View as plain text