test.go 47 KB

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