ffi 1.12.1 → 1.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.appveyor.yml +3 -0
  3. data/.github/workflows/ci.yml +64 -0
  4. data/.travis.yml +19 -5
  5. data/CHANGELOG.md +47 -0
  6. data/Gemfile +4 -2
  7. data/Rakefile +24 -43
  8. data/ext/ffi_c/Buffer.c +2 -2
  9. data/ext/ffi_c/Call.c +1 -7
  10. data/ext/ffi_c/ClosurePool.c +11 -14
  11. data/ext/ffi_c/Function.c +8 -23
  12. data/ext/ffi_c/FunctionInfo.c +1 -2
  13. data/ext/ffi_c/LongDouble.c +5 -3
  14. data/ext/ffi_c/LongDouble.h +0 -4
  15. data/ext/ffi_c/MemoryPointer.c +1 -1
  16. data/ext/ffi_c/MethodHandle.c +18 -24
  17. data/ext/ffi_c/MethodHandle.h +3 -2
  18. data/ext/ffi_c/Platform.c +1 -0
  19. data/ext/ffi_c/Pointer.c +1 -1
  20. data/ext/ffi_c/Struct.c +47 -51
  21. data/ext/ffi_c/Struct.h +12 -6
  22. data/ext/ffi_c/StructLayout.c +20 -14
  23. data/ext/ffi_c/Thread.c +0 -3
  24. data/ext/ffi_c/Thread.h +0 -3
  25. data/ext/ffi_c/compat.h +4 -0
  26. data/ext/ffi_c/extconf.rb +16 -20
  27. data/ext/ffi_c/libffi/.travis.yml +4 -0
  28. data/ext/ffi_c/libffi/.travis/build.sh +4 -0
  29. data/ext/ffi_c/libffi/Makefile.am +2 -1
  30. data/ext/ffi_c/libffi/README.md +7 -1
  31. data/ext/ffi_c/libffi/configure.ac +25 -9
  32. data/ext/ffi_c/libffi/include/ffi.h.in +8 -0
  33. data/ext/ffi_c/libffi/libffi.map.in +8 -12
  34. data/ext/ffi_c/libffi/libtool-version +1 -1
  35. data/ext/ffi_c/libffi/src/aarch64/ffi.c +6 -0
  36. data/ext/ffi_c/libffi/src/aarch64/sysv.S +13 -2
  37. data/ext/ffi_c/libffi/src/closures.c +10 -4
  38. data/ext/ffi_c/libffi/src/pa/ffi.c +46 -91
  39. data/ext/ffi_c/libffi/src/pa/ffitarget.h +1 -6
  40. data/ext/ffi_c/libffi/src/pa/hpux32.S +4 -2
  41. data/ext/ffi_c/libffi/src/pa/linux.S +4 -2
  42. data/ext/ffi_c/libffi/src/powerpc/sysv.S +5 -7
  43. data/ext/ffi_c/libffi/src/x86/ffi.c +7 -4
  44. data/ext/ffi_c/libffi/src/x86/ffi64.c +10 -8
  45. data/ext/ffi_c/libffi/src/x86/ffitarget.h +15 -2
  46. data/ext/ffi_c/libffi/src/x86/ffiw64.c +10 -8
  47. data/ext/ffi_c/libffi/src/x86/sysv.S +13 -4
  48. data/ext/ffi_c/libffi/src/x86/unix64.S +58 -2
  49. data/ext/ffi_c/libffi/src/x86/win64.S +4 -1
  50. data/ffi.gemspec +1 -1
  51. data/lib/ffi.rb +10 -2
  52. data/lib/ffi/library.rb +5 -1
  53. data/lib/ffi/platform.rb +6 -2
  54. data/lib/ffi/platform/arm-linux/types.conf +32 -4
  55. data/lib/ffi/platform/i386-windows/types.conf +26 -79
  56. data/lib/ffi/platform/powerpc-linux/types.conf +32 -2
  57. data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
  58. data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
  59. data/lib/ffi/platform/x86_64-darwin/types.conf +4 -0
  60. data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +4 -22
  61. data/lib/ffi/platform/x86_64-linux/types.conf +21 -0
  62. data/lib/ffi/platform/x86_64-windows/types.conf +10 -78
  63. data/lib/ffi/pointer.rb +19 -12
  64. data/lib/ffi/struct.rb +9 -4
  65. data/lib/ffi/tools/types_generator.rb +2 -0
  66. data/lib/ffi/version.rb +1 -1
  67. data/samples/getlogin.rb +1 -1
  68. data/samples/getpid.rb +1 -1
  69. data/samples/gettimeofday.rb +8 -8
  70. data/samples/hello.rb +2 -1
  71. data/samples/inotify.rb +1 -1
  72. data/samples/pty.rb +1 -2
  73. data/samples/qsort.rb +0 -1
  74. metadata +6 -4
  75. data/samples/sample_helper.rb +0 -6
@@ -56,6 +56,14 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
56
56
  #define PTR_SIZE 4
57
57
  #else
58
58
  #define PTR_SIZE 8
59
+ #endif
60
+
61
+ #if FFI_EXEC_TRAMPOLINE_TABLE && defined(__MACH__) && defined(HAVE_PTRAUTH)
62
+ # define BR(r) braaz r
63
+ # define BLR(r) blraaz r
64
+ #else
65
+ # define BR(r) br r
66
+ # define BLR(r) blr r
59
67
  #endif
60
68
 
61
69
  .text
@@ -111,7 +119,7 @@ CNAME(ffi_call_SYSV):
111
119
  /* Deallocate the context, leaving the stacked arguments. */
112
120
  add sp, sp, #CALL_CONTEXT_SIZE
113
121
 
114
- blr x9 /* call fn */
122
+ BLR(x9) /* call fn */
115
123
 
116
124
  ldp x3, x4, [x29, #16] /* reload rvalue and flags */
117
125
 
@@ -271,6 +279,9 @@ CNAME(ffi_closure_SYSV):
271
279
  bl CNAME(ffi_closure_SYSV_inner)
272
280
 
273
281
  /* Load the return value as directed. */
282
+ #if FFI_EXEC_TRAMPOLINE_TABLE && defined(__MACH__) && defined(HAVE_PTRAUTH)
283
+ autiza x1
284
+ #endif
274
285
  adr x1, 0f
275
286
  and w0, w0, #AARCH64_RET_MASK
276
287
  add x1, x1, x0, lsl #3
@@ -365,7 +376,7 @@ CNAME(ffi_closure_trampoline_table_page):
365
376
  .rept PAGE_MAX_SIZE / FFI_TRAMPOLINE_SIZE
366
377
  adr x16, -PAGE_MAX_SIZE
367
378
  ldp x17, x16, [x16]
368
- br x16
379
+ BR(x16)
369
380
  nop /* each entry in the trampoline config page is 2*sizeof(void*) so the trampoline itself cannot be smaller that 16 bytes */
370
381
  .endr
371
382
 
@@ -148,6 +148,9 @@ ffi_closure_free (void *ptr)
148
148
 
149
149
  #include <mach/mach.h>
150
150
  #include <pthread.h>
151
+ #ifdef HAVE_PTRAUTH
152
+ #include <ptrauth.h>
153
+ #endif
151
154
  #include <stdio.h>
152
155
  #include <stdlib.h>
153
156
 
@@ -301,6 +304,9 @@ ffi_closure_alloc (size_t size, void **code)
301
304
 
302
305
  /* Initialize the return values */
303
306
  *code = entry->trampoline;
307
+ #ifdef HAVE_PTRAUTH
308
+ *code = ptrauth_sign_unauthenticated (*code, ptrauth_key_asia, 0);
309
+ #endif
304
310
  closure->trampoline_table = table;
305
311
  closure->trampoline_table_entry = entry;
306
312
 
@@ -921,7 +927,7 @@ ffi_closure_alloc (size_t size, void **code)
921
927
  if (!code)
922
928
  return NULL;
923
929
 
924
- ptr = dlmalloc (size);
930
+ ptr = FFI_CLOSURE_PTR (dlmalloc (size));
925
931
 
926
932
  if (ptr)
927
933
  {
@@ -961,7 +967,7 @@ ffi_closure_free (void *ptr)
961
967
  ptr = sub_segment_exec_offset (ptr, seg);
962
968
  #endif
963
969
 
964
- dlfree (ptr);
970
+ dlfree (FFI_RESTORE_PTR (ptr));
965
971
  }
966
972
 
967
973
  # else /* ! FFI_MMAP_EXEC_WRIT */
@@ -977,13 +983,13 @@ ffi_closure_alloc (size_t size, void **code)
977
983
  if (!code)
978
984
  return NULL;
979
985
 
980
- return *code = malloc (size);
986
+ return *code = FFI_CLOSURE_PTR (malloc (size));
981
987
  }
982
988
 
983
989
  void
984
990
  ffi_closure_free (void *ptr)
985
991
  {
986
- free (ptr);
992
+ free (FFI_RESTORE_PTR (ptr));
987
993
  }
988
994
 
989
995
  void *
@@ -421,12 +421,15 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
421
421
  ffi_cif *cif;
422
422
  void **avalue;
423
423
  void *rvalue;
424
- UINT32 ret[2]; /* function can return up to 64-bits in registers */
424
+ /* Functions can return up to 64-bits in registers. Return address
425
+ must be double word aligned. */
426
+ union { double rd; UINT32 ret[2]; } u;
425
427
  ffi_type **p_arg;
426
428
  char *tmp;
427
429
  int i, avn;
428
430
  unsigned int slot = FIRST_ARG_SLOT;
429
431
  register UINT32 r28 asm("r28");
432
+ ffi_closure *c = (ffi_closure *)FFI_RESTORE_PTR (closure);
430
433
 
431
434
  cif = closure->cif;
432
435
 
@@ -434,7 +437,7 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
434
437
  if (cif->flags == FFI_TYPE_STRUCT)
435
438
  rvalue = (void *)r28;
436
439
  else
437
- rvalue = &ret[0];
440
+ rvalue = &u;
438
441
 
439
442
  avalue = (void **)alloca(cif->nargs * FFI_SIZEOF_ARG);
440
443
  avn = cif->nargs;
@@ -529,35 +532,35 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
529
532
  }
530
533
 
531
534
  /* Invoke the closure. */
532
- (closure->fun) (cif, rvalue, avalue, closure->user_data);
535
+ (c->fun) (cif, rvalue, avalue, c->user_data);
533
536
 
534
- debug(3, "after calling function, ret[0] = %08x, ret[1] = %08x\n", ret[0],
535
- ret[1]);
537
+ debug(3, "after calling function, ret[0] = %08x, ret[1] = %08x\n", u.ret[0],
538
+ u.ret[1]);
536
539
 
537
540
  /* Store the result using the lower 2 bytes of the flags. */
538
541
  switch (cif->flags)
539
542
  {
540
543
  case FFI_TYPE_UINT8:
541
- *(stack - FIRST_ARG_SLOT) = (UINT8)(ret[0] >> 24);
544
+ *(stack - FIRST_ARG_SLOT) = (UINT8)(u.ret[0] >> 24);
542
545
  break;
543
546
  case FFI_TYPE_SINT8:
544
- *(stack - FIRST_ARG_SLOT) = (SINT8)(ret[0] >> 24);
547
+ *(stack - FIRST_ARG_SLOT) = (SINT8)(u.ret[0] >> 24);
545
548
  break;
546
549
  case FFI_TYPE_UINT16:
547
- *(stack - FIRST_ARG_SLOT) = (UINT16)(ret[0] >> 16);
550
+ *(stack - FIRST_ARG_SLOT) = (UINT16)(u.ret[0] >> 16);
548
551
  break;
549
552
  case FFI_TYPE_SINT16:
550
- *(stack - FIRST_ARG_SLOT) = (SINT16)(ret[0] >> 16);
553
+ *(stack - FIRST_ARG_SLOT) = (SINT16)(u.ret[0] >> 16);
551
554
  break;
552
555
  case FFI_TYPE_INT:
553
556
  case FFI_TYPE_SINT32:
554
557
  case FFI_TYPE_UINT32:
555
- *(stack - FIRST_ARG_SLOT) = ret[0];
558
+ *(stack - FIRST_ARG_SLOT) = u.ret[0];
556
559
  break;
557
560
  case FFI_TYPE_SINT64:
558
561
  case FFI_TYPE_UINT64:
559
- *(stack - FIRST_ARG_SLOT) = ret[0];
560
- *(stack - FIRST_ARG_SLOT - 1) = ret[1];
562
+ *(stack - FIRST_ARG_SLOT) = u.ret[0];
563
+ *(stack - FIRST_ARG_SLOT - 1) = u.ret[1];
561
564
  break;
562
565
 
563
566
  case FFI_TYPE_DOUBLE:
@@ -577,7 +580,7 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
577
580
  case FFI_TYPE_SMALL_STRUCT4:
578
581
  tmp = (void*)(stack - FIRST_ARG_SLOT);
579
582
  tmp += 4 - cif->rtype->size;
580
- memcpy((void*)tmp, &ret[0], cif->rtype->size);
583
+ memcpy((void*)tmp, &u, cif->rtype->size);
581
584
  break;
582
585
 
583
586
  case FFI_TYPE_SMALL_STRUCT5:
@@ -598,7 +601,7 @@ ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
598
601
  }
599
602
 
600
603
  memset (ret2, 0, sizeof (ret2));
601
- memcpy ((char *)ret2 + off, ret, 8 - off);
604
+ memcpy ((char *)ret2 + off, &u, 8 - off);
602
605
 
603
606
  *(stack - FIRST_ARG_SLOT) = ret2[0];
604
607
  *(stack - FIRST_ARG_SLOT - 1) = ret2[1];
@@ -630,89 +633,41 @@ ffi_prep_closure_loc (ffi_closure* closure,
630
633
  void *user_data,
631
634
  void *codeloc)
632
635
  {
633
- UINT32 *tramp = (UINT32 *)(closure->tramp);
634
- #ifdef PA_HPUX
635
- UINT32 *tmp;
636
- #endif
636
+ ffi_closure *c = (ffi_closure *)FFI_RESTORE_PTR (closure);
637
+
638
+ /* The layout of a function descriptor. A function pointer with the PLABEL
639
+ bit set points to a function descriptor. */
640
+ struct pa32_fd
641
+ {
642
+ UINT32 code_pointer;
643
+ UINT32 gp;
644
+ };
645
+
646
+ struct ffi_pa32_trampoline_struct
647
+ {
648
+ UINT32 code_pointer; /* Pointer to ffi_closure_unix. */
649
+ UINT32 fake_gp; /* Pointer to closure, installed as gp. */
650
+ UINT32 real_gp; /* Real gp value. */
651
+ };
652
+
653
+ struct ffi_pa32_trampoline_struct *tramp;
654
+ struct pa32_fd *fd;
637
655
 
638
656
  if (cif->abi != FFI_PA32)
639
657
  return FFI_BAD_ABI;
640
658
 
641
- /* Make a small trampoline that will branch to our
642
- handler function. Use PC-relative addressing. */
643
-
644
- #ifdef PA_LINUX
645
- tramp[0] = 0xeaa00000; /* b,l .+8,%r21 ; %r21 <- pc+8 */
646
- tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21 ; mask priv bits */
647
- tramp[2] = 0x4aa10028; /* ldw 20(%r21),%r1 ; load plabel */
648
- tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21 ; get closure addr */
649
- tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22 ; address of handler */
650
- tramp[5] = 0xeac0c000; /* bv%r0(%r22) ; branch to handler */
651
- tramp[6] = 0x0c281093; /* ldw 4(%r1),%r19 ; GP of handler */
652
- tramp[7] = ((UINT32)(ffi_closure_pa32) & ~2);
653
-
654
- /* Flush d/icache -- have to flush up 2 two lines because of
655
- alignment. */
656
- __asm__ volatile(
657
- "fdc 0(%0)\n\t"
658
- "fdc %1(%0)\n\t"
659
- "fic 0(%%sr4, %0)\n\t"
660
- "fic %1(%%sr4, %0)\n\t"
661
- "sync\n\t"
662
- "nop\n\t"
663
- "nop\n\t"
664
- "nop\n\t"
665
- "nop\n\t"
666
- "nop\n\t"
667
- "nop\n\t"
668
- "nop\n"
669
- :
670
- : "r"((unsigned long)tramp & ~31),
671
- "r"(32 /* stride */)
672
- : "memory");
673
- #endif
659
+ /* Get function descriptor address for ffi_closure_pa32. */
660
+ fd = (struct pa32_fd *)((UINT32)ffi_closure_pa32 & ~3);
674
661
 
675
- #ifdef PA_HPUX
676
- tramp[0] = 0xeaa00000; /* b,l .+8,%r21 ; %r21 <- pc+8 */
677
- tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21 ; mask priv bits */
678
- tramp[2] = 0x4aa10038; /* ldw 28(%r21),%r1 ; load plabel */
679
- tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21 ; get closure addr */
680
- tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22 ; address of handler */
681
- tramp[5] = 0x02c010b4; /* ldsid (%r22),%r20 ; load space id */
682
- tramp[6] = 0x00141820; /* mtsp %r20,%sr0 ; into %sr0 */
683
- tramp[7] = 0xe2c00000; /* be 0(%sr0,%r22) ; branch to handler */
684
- tramp[8] = 0x0c281093; /* ldw 4(%r1),%r19 ; GP of handler */
685
- tramp[9] = ((UINT32)(ffi_closure_pa32) & ~2);
686
-
687
- /* Flush d/icache -- have to flush three lines because of alignment. */
688
- __asm__ volatile(
689
- "copy %1,%0\n\t"
690
- "fdc,m %2(%0)\n\t"
691
- "fdc,m %2(%0)\n\t"
692
- "fdc,m %2(%0)\n\t"
693
- "ldsid (%1),%0\n\t"
694
- "mtsp %0,%%sr0\n\t"
695
- "copy %1,%0\n\t"
696
- "fic,m %2(%%sr0,%0)\n\t"
697
- "fic,m %2(%%sr0,%0)\n\t"
698
- "fic,m %2(%%sr0,%0)\n\t"
699
- "sync\n\t"
700
- "nop\n\t"
701
- "nop\n\t"
702
- "nop\n\t"
703
- "nop\n\t"
704
- "nop\n\t"
705
- "nop\n\t"
706
- "nop\n"
707
- : "=&r" ((unsigned long)tmp)
708
- : "r" ((unsigned long)tramp & ~31),
709
- "r" (32/* stride */)
710
- : "memory");
711
- #endif
662
+ /* Setup trampoline. */
663
+ tramp = (struct ffi_pa32_trampoline_struct *)c->tramp;
664
+ tramp->code_pointer = fd->code_pointer;
665
+ tramp->fake_gp = (UINT32)codeloc & ~3;
666
+ tramp->real_gp = fd->gp;
712
667
 
713
- closure->cif = cif;
714
- closure->user_data = user_data;
715
- closure->fun = fun;
668
+ c->cif = cif;
669
+ c->user_data = user_data;
670
+ c->fun = fun;
716
671
 
717
672
  return FFI_OK;
718
673
  }
@@ -68,12 +68,7 @@ typedef enum ffi_abi {
68
68
 
69
69
  #define FFI_CLOSURES 1
70
70
  #define FFI_NATIVE_RAW_API 0
71
-
72
- #ifdef PA_LINUX
73
- #define FFI_TRAMPOLINE_SIZE 32
74
- #else
75
- #define FFI_TRAMPOLINE_SIZE 40
76
- #endif
71
+ #define FFI_TRAMPOLINE_SIZE 12
77
72
 
78
73
  #define FFI_TYPE_SMALL_STRUCT2 -1
79
74
  #define FFI_TYPE_SMALL_STRUCT3 -2
@@ -259,7 +259,7 @@ L$done
259
259
  L$FE1
260
260
 
261
261
  /* void ffi_closure_pa32(void);
262
- Called with closure argument in %r21 */
262
+ Called with closure argument in %r19 */
263
263
 
264
264
  .SPACE $TEXT$
265
265
  .SUBSPA $CODE$
@@ -285,7 +285,9 @@ L$CFI22
285
285
  stw %arg2, -44(%r3)
286
286
  stw %arg3, -48(%r3)
287
287
 
288
- copy %r21, %arg0
288
+ /* Retrieve closure pointer and real gp. */
289
+ copy %r19, %arg0
290
+ ldw 8(%r19), %r19
289
291
  bl ffi_closure_inner_pa32, %r2
290
292
  copy %r3, %arg1
291
293
  ldwm -64(%sp), %r3
@@ -252,7 +252,7 @@ ffi_call_pa32:
252
252
  .LFE1:
253
253
 
254
254
  /* void ffi_closure_pa32(void);
255
- Called with closure argument in %r21 */
255
+ Called with closure argument in %r19 */
256
256
  .export ffi_closure_pa32,code
257
257
  .import ffi_closure_inner_pa32,code
258
258
 
@@ -277,7 +277,9 @@ ffi_closure_pa32:
277
277
  stw %arg2, -44(%r3)
278
278
  stw %arg3, -48(%r3)
279
279
 
280
- copy %r21, %arg0
280
+ /* Retrieve closure pointer and real gp. */
281
+ copy %r19, %arg0
282
+ ldw 8(%r19), %r19
281
283
  bl ffi_closure_inner_pa32, %r2
282
284
  copy %r3, %arg1
283
285
 
@@ -104,17 +104,16 @@ ENTRY(ffi_call_SYSV)
104
104
  bctrl
105
105
 
106
106
  /* Now, deal with the return value. */
107
- mtcrf 0x01,%r31 /* cr7 */
107
+ mtcrf 0x03,%r31 /* cr6-cr7 */
108
108
  bt- 31,L(small_struct_return_value)
109
109
  bt- 30,L(done_return_value)
110
110
  #ifndef __NO_FPRS__
111
111
  bt- 29,L(fp_return_value)
112
112
  #endif
113
113
  stw %r3,0(%r30)
114
- bf+ 28,L(done_return_value)
114
+ bf+ 27,L(done_return_value)
115
115
  stw %r4,4(%r30)
116
- mtcrf 0x02,%r31 /* cr6 */
117
- bf 27,L(done_return_value)
116
+ bf 26,L(done_return_value)
118
117
  stw %r5,8(%r30)
119
118
  stw %r6,12(%r30)
120
119
  /* Fall through... */
@@ -145,10 +144,9 @@ L(done_return_value):
145
144
  #ifndef __NO_FPRS__
146
145
  L(fp_return_value):
147
146
  .cfi_restore_state
148
- bf 28,L(float_return_value)
147
+ bf 27,L(float_return_value)
149
148
  stfd %f1,0(%r30)
150
- mtcrf 0x02,%r31 /* cr6 */
151
- bf 27,L(done_return_value)
149
+ bf 26,L(done_return_value)
152
150
  stfd %f2,8(%r30)
153
151
  b L(done_return_value)
154
152
  L(float_return_value):
@@ -557,13 +557,16 @@ ffi_prep_closure_loc (ffi_closure* closure,
557
557
  return FFI_BAD_ABI;
558
558
  }
559
559
 
560
+ /* endbr32. */
561
+ *(UINT32 *) tramp = 0xfb1e0ff3;
562
+
560
563
  /* movl or pushl immediate. */
561
- tramp[0] = op;
562
- *(void **)(tramp + 1) = codeloc;
564
+ tramp[4] = op;
565
+ *(void **)(tramp + 5) = codeloc;
563
566
 
564
567
  /* jmp dest */
565
- tramp[5] = 0xe9;
566
- *(unsigned *)(tramp + 6) = (unsigned)dest - ((unsigned)codeloc + 10);
568
+ tramp[9] = 0xe9;
569
+ *(unsigned *)(tramp + 10) = (unsigned)dest - ((unsigned)codeloc + 14);
567
570
 
568
571
  closure->cif = cif;
569
572
  closure->fun = fun;
@@ -728,13 +728,15 @@ ffi_prep_closure_loc (ffi_closure* closure,
728
728
  void *user_data,
729
729
  void *codeloc)
730
730
  {
731
- static const unsigned char trampoline[16] = {
732
- /* leaq -0x7(%rip),%r10 # 0x0 */
733
- 0x4c, 0x8d, 0x15, 0xf9, 0xff, 0xff, 0xff,
734
- /* jmpq *0x3(%rip) # 0x10 */
735
- 0xff, 0x25, 0x03, 0x00, 0x00, 0x00,
736
- /* nopl (%rax) */
737
- 0x0f, 0x1f, 0x00
731
+ static const unsigned char trampoline[24] = {
732
+ /* endbr64 */
733
+ 0xf3, 0x0f, 0x1e, 0xfa,
734
+ /* leaq -0xb(%rip),%r10 # 0x0 */
735
+ 0x4c, 0x8d, 0x15, 0xf5, 0xff, 0xff, 0xff,
736
+ /* jmpq *0x7(%rip) # 0x18 */
737
+ 0xff, 0x25, 0x07, 0x00, 0x00, 0x00,
738
+ /* nopl 0(%rax) */
739
+ 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00
738
740
  };
739
741
  void (*dest)(void);
740
742
  char *tramp = closure->tramp;
@@ -752,7 +754,7 @@ ffi_prep_closure_loc (ffi_closure* closure,
752
754
  dest = ffi_closure_unix64;
753
755
 
754
756
  memcpy (tramp, trampoline, sizeof(trampoline));
755
- *(UINT64 *)(tramp + 16) = (uintptr_t)dest;
757
+ *(UINT64 *)(tramp + sizeof (trampoline)) = (uintptr_t)dest;
756
758
 
757
759
  closure->cif = cif;
758
760
  closure->fun = fun;
@@ -136,12 +136,25 @@ typedef enum ffi_abi {
136
136
 
137
137
  #if defined (X86_64) || defined(X86_WIN64) \
138
138
  || (defined (__x86_64__) && defined (X86_DARWIN))
139
- # define FFI_TRAMPOLINE_SIZE 24
139
+ /* 4 bytes of ENDBR64 + 7 bytes of LEA + 6 bytes of JMP + 7 bytes of NOP
140
+ + 8 bytes of pointer. */
141
+ # define FFI_TRAMPOLINE_SIZE 32
140
142
  # define FFI_NATIVE_RAW_API 0
141
143
  #else
142
- # define FFI_TRAMPOLINE_SIZE 12
144
+ /* 4 bytes of ENDBR32 + 5 bytes of MOV + 5 bytes of JMP + 2 unused
145
+ bytes. */
146
+ # define FFI_TRAMPOLINE_SIZE 16
143
147
  # define FFI_NATIVE_RAW_API 1 /* x86 has native raw api support */
144
148
  #endif
145
149
 
150
+ #if !defined(GENERATE_LIBFFI_MAP) && defined(__ASSEMBLER__) \
151
+ && defined(__CET__)
152
+ # include <cet.h>
153
+ # define _CET_NOTRACK notrack
154
+ #else
155
+ # define _CET_ENDBR
156
+ # define _CET_NOTRACK
157
+ #endif
158
+
146
159
  #endif
147
160