Skip to content

Commit

Permalink
Address Brandt feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorusso committed Nov 8, 2024
1 parent 24719d7 commit 507e662
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions Python/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 507e662

Please sign in to comment.