Also flush the code alias mapping when creating a closure. It seems that this is necessary on some aarch64 implementations. The existing code only flashes the writable mapping. diff -rup a/include/ffi_common.h b/include/ffi_common.h --- a/include/ffi_common.h 2014-04-25 13:45:13.000000000 -0400 +++ b/include/ffi_common.h 2019-06-14 14:12:04.387499160 -0400 @@ -82,6 +82,10 @@ ffi_status ffi_prep_cif_machdep(ffi_cif ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif, unsigned int nfixedargs, unsigned int ntotalargs); +/* Translate a data pointer to a code pointer. Needed for closures on + some targets. */ +void *ffi_data_to_code_pointer (void *data) FFI_HIDDEN; + /* Extended cif, used in callback from assembly routine */ typedef struct { Only in b/include: ffi_common.h.orig diff -rup a/src/aarch64/ffi.c b/src/aarch64/ffi.c --- a/src/aarch64/ffi.c 2019-06-14 14:11:03.485469505 -0400 +++ b/src/aarch64/ffi.c 2019-06-14 14:12:04.392499162 -0400 @@ -926,6 +926,10 @@ ffi_prep_closure_loc (ffi_closure* closu FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_SYSV, codeloc, cif->aarch64_flags); + /* Also clear the cache on the executable alias mapping. */ + unsigned char *code = ffi_data_to_code_pointer (&closure->tramp[0]); + ffi_clear_cache (code, code + FFI_TRAMPOLINE_SIZE); + closure->cif = cif; closure->user_data = user_data; closure->fun = fun; Only in b/src/aarch64: ffi.c.orig diff -rup a/src/closures.c b/src/closures.c --- a/src/closures.c 2014-05-11 09:54:19.000000000 -0400 +++ b/src/closures.c 2019-06-14 14:12:04.396499164 -0400 @@ -597,6 +597,13 @@ ffi_closure_alloc (size_t size, void **c return ptr; } +void * +ffi_data_to_code_pointer (void *data) +{ + msegmentptr seg = segment_holding (gm, data); + return add_segment_exec_offset (data, seg); +} + /* Release a chunk of memory allocated with ffi_closure_alloc. If FFI_CLOSURE_FREE_CODE is nonzero, the given address can be the writable or the executable address given. Otherwise, only the @@ -656,5 +663,11 @@ ffi_closure_free (void *ptr) free (ptr); } +void * +ffi_data_to_code_pointer (void *data) +{ + return data; +} + # endif /* ! FFI_MMAP_EXEC_WRIT */ #endif /* FFI_CLOSURES */