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