Skip to content

Commit 507e662

Browse files
committed
Address Brandt feedback
1 parent 24719d7 commit 507e662

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Python/jit.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ jit_alloc(size_t size)
5656
int flags = MEM_COMMIT | MEM_RESERVE;
5757
unsigned char *memory = VirtualAlloc(NULL, size, flags, PAGE_READWRITE);
5858
int failed = memory == NULL;
59-
#elif defined(__APPLE__) && defined(__aarch64__)
60-
int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT;
61-
int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
62-
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0);
63-
int failed = memory == MAP_FAILED;
64-
pthread_jit_write_protect_np(0);
6559
#else
6660
int flags = MAP_ANONYMOUS | MAP_PRIVATE;
6761
int prot = PROT_READ | PROT_WRITE;
62+
# ifdef MAP_JIT
63+
flags |= MAP_JIT;
64+
prot |= PROT_EXEC;
65+
# endif
6866
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0);
6967
int failed = memory == MAP_FAILED;
7068
#endif
@@ -108,13 +106,12 @@ mark_executable(unsigned char *memory, size_t size)
108106
}
109107
int old;
110108
int failed = !VirtualProtect(memory, size, PAGE_EXECUTE_READ, &old);
111-
#elif defined(__APPLE__) && defined(__aarch64__)
112-
int failed = 0;
113-
__builtin___clear_cache((char *)memory, (char *)memory + size);
114-
pthread_jit_write_protect_np(1);
115109
#else
110+
int failed = 0;
116111
__builtin___clear_cache((char *)memory, (char *)memory + size);
117-
int failed = mprotect(memory, size, PROT_EXEC | PROT_READ);
112+
# ifndef MAP_JIT
113+
failed = mprotect(memory, size, PROT_EXEC | PROT_READ);
114+
# endif
118115
#endif
119116
if (failed) {
120117
jit_error("unable to protect executable memory");
@@ -510,6 +507,9 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz
510507
if (memory == NULL) {
511508
return -1;
512509
}
510+
#ifdef MAP_JIT
511+
pthread_jit_write_protect_np(0);
512+
#endif
513513
// Update the offsets of each instruction:
514514
for (size_t i = 0; i < length; i++) {
515515
state.instruction_starts[i] += (uintptr_t)memory;
@@ -540,7 +540,11 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz
540540
data += group->data_size;
541541
assert(code == memory + code_size);
542542
assert(data == memory + code_size + data_size);
543-
if (mark_executable(memory, total_size)) {
543+
int status = mark_executable(memory, total_size);
544+
#ifdef MAP_JIT
545+
pthread_jit_write_protect_np(1);
546+
#endif
547+
if (status) {
544548
jit_free(memory, total_size);
545549
return -1;
546550
}

0 commit comments

Comments
 (0)