Skip to content

Commit 1f14b3f

Browse files
authored
Ensure that VM_PROT_EXECUTE is set on the trampoline page. (java-native-access#718)
1 parent 000ecd0 commit 1f14b3f

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/closures.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ ffi_tramp_is_present (__attribute__((unused)) void *ptr)
134134
# define HAVE_MNTENT 1
135135
# endif
136136
# if defined(_WIN32) || defined(__OS2__)
137-
/* Windows systems may have Data Execution Protection (DEP) enabled,
137+
/* Windows systems may have Data Execution Protection (DEP) enabled,
138138
which requires the use of VirtualMalloc/VirtualFree to alloc/free
139139
executable memory. */
140140
# define FFI_MMAP_EXEC_WRIT 1
@@ -230,12 +230,24 @@ ffi_trampoline_table_alloc (void)
230230
kt = vm_remap (mach_task_self (), &trampoline_page, PAGE_MAX_SIZE, 0x0,
231231
VM_FLAGS_OVERWRITE, mach_task_self (), trampoline_page_template,
232232
FALSE, &cur_prot, &max_prot, VM_INHERIT_SHARE);
233-
if (kt != KERN_SUCCESS || !(cur_prot & VM_PROT_EXECUTE))
233+
if (kt != KERN_SUCCESS)
234234
{
235235
vm_deallocate (mach_task_self (), config_page, PAGE_MAX_SIZE * 2);
236236
return NULL;
237237
}
238238

239+
if (!(cur_prot & VM_PROT_EXECUTE))
240+
{
241+
/* If VM_PROT_EXECUTE isn't set on the remapped trampoline page, set it */
242+
kt = vm_protect (mach_task_self (), trampoline_page, PAGE_MAX_SIZE,
243+
FALSE, cur_prot | VM_PROT_EXECUTE);
244+
if (kt != KERN_SUCCESS)
245+
{
246+
vm_deallocate (mach_task_self (), config_page, PAGE_MAX_SIZE * 2);
247+
return NULL;
248+
}
249+
}
250+
239251
/* We have valid trampoline and config pages */
240252
table = calloc (1, sizeof (ffi_trampoline_table));
241253
table->free_count = FFI_TRAMPOLINE_COUNT;

0 commit comments

Comments
 (0)