| 1 |
/* Area: ffi_call |
| 2 |
Purpose: Test longjmp over ffi_call frames */ |
| 3 |
|
| 4 |
/* Test code adapted from Lars Kanis' bug report: |
| 5 |
https://github.com/libffi/libffi/issues/905 */ |
| 6 |
|
| 7 |
/* { dg-do run } */ |
| 8 |
|
| 9 |
#include "ffitest.h" |
| 10 |
#include "ffi_common.h" |
| 11 |
|
| 12 |
#include <setjmp.h> |
| 13 |
|
| 14 |
static jmp_buf buf; |
| 15 |
|
| 16 |
static void ABI_ATTR lev2(const char *str) { |
| 17 |
printf("lev2 %s\n", str); |
| 18 |
// jumps back to where setjmp was called - making setjmp now return 1 |
| 19 |
longjmp(buf, 1); |
| 20 |
} |
| 21 |
|
| 22 |
static void ABI_ATTR lev1(const char *str) { |
| 23 |
lev2(str); |
| 24 |
|
| 25 |
// will not be reached |
| 26 |
printf("lev1 %s\n", str); |
| 27 |
} |
| 28 |
|
| 29 |
int main() |
| 30 |
{ |
| 31 |
ffi_cif cif; |
| 32 |
ffi_type *args[1]; |
| 33 |
void *values[1]; |
| 34 |
char *s; |
| 35 |
ffi_arg rc; |
| 36 |
/* Initialize the argument info vectors */ |
| 37 |
args[0] = &ffi_type_pointer; |
| 38 |
values[0] = &s; |
| 39 |
/* Initialize the cif */ |
| 40 |
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, |
| 41 |
&ffi_type_sint, args) == FFI_OK) |
| 42 |
{ |
| 43 |
s = "direct call"; |
| 44 |
if (!setjmp(buf)){ |
| 45 |
// works on x64 and arm64 |
| 46 |
lev1(s); |
| 47 |
} else { |
| 48 |
printf("back to main\n"); |
| 49 |
} |
| 50 |
|
| 51 |
s = "through libffi"; |
| 52 |
if (!setjmp(buf)){ |
| 53 |
// works on x64 but segfaults on arm64 |
| 54 |
ffi_call(&cif, (void (*)(void))lev1, &rc, values); |
| 55 |
} else { |
| 56 |
printf("back to main\n"); |
| 57 |
} |
| 58 |
} |
| 59 |
return 0; |
| 60 |
} |