diff --git a/Python/jit.c b/Python/jit.c index c3e6aea4037f58..dbaef80049c0d6 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -56,15 +56,13 @@ jit_alloc(size_t size) int flags = MEM_COMMIT | MEM_RESERVE; unsigned char *memory = VirtualAlloc(NULL, size, flags, PAGE_READWRITE); int failed = memory == NULL; -#elif defined(__APPLE__) && defined(__aarch64__) - int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT; - int prot = PROT_READ | PROT_WRITE | PROT_EXEC; - unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); - int failed = memory == MAP_FAILED; - pthread_jit_write_protect_np(0); #else int flags = MAP_ANONYMOUS | MAP_PRIVATE; int prot = PROT_READ | PROT_WRITE; +# ifdef MAP_JIT + flags |= MAP_JIT; + prot |= PROT_EXEC; +# endif unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); int failed = memory == MAP_FAILED; #endif @@ -108,13 +106,12 @@ mark_executable(unsigned char *memory, size_t size) } int old; int failed = !VirtualProtect(memory, size, PAGE_EXECUTE_READ, &old); -#elif defined(__APPLE__) && defined(__aarch64__) - int failed = 0; - __builtin___clear_cache((char *)memory, (char *)memory + size); - pthread_jit_write_protect_np(1); #else + int failed = 0; __builtin___clear_cache((char *)memory, (char *)memory + size); - int failed = mprotect(memory, size, PROT_EXEC | PROT_READ); +# ifndef MAP_JIT + failed = mprotect(memory, size, PROT_EXEC | PROT_READ); +# endif #endif if (failed) { jit_error("unable to protect executable memory"); @@ -510,6 +507,9 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz if (memory == NULL) { return -1; } +#ifdef MAP_JIT + pthread_jit_write_protect_np(0); +#endif // Update the offsets of each instruction: for (size_t i = 0; i < length; i++) { state.instruction_starts[i] += (uintptr_t)memory; @@ -540,7 +540,11 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz data += group->data_size; assert(code == memory + code_size); assert(data == memory + code_size + data_size); - if (mark_executable(memory, total_size)) { + int status = mark_executable(memory, total_size); +#ifdef MAP_JIT + pthread_jit_write_protect_np(1); +#endif + if (status) { jit_free(memory, total_size); return -1; }