Skip to content

Commit ef57d84

Browse files
committed
Fix JIT memprot and cache invalidation on Arm64
- Move pthread_jit_write_protect_np(false) before reading MAP_JIT memory in update_branch_imm since Apple Silicon requires write mode for both read and write operations on MAP_JIT memory - Add missing cache invalidation in resolve_jumps() for x86_64 after patching jump offsets
1 parent 9ba5553 commit ef57d84

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/jit.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ static void update_branch_imm(struct jit_state *state,
593593
assert((imm & 3) == 0);
594594
uint32_t insn;
595595
imm >>= 2;
596+
#if defined(__APPLE__) && defined(__aarch64__)
597+
/* Must be in write mode to read/write MAP_JIT memory on Apple ARM64 */
598+
pthread_jit_write_protect_np(false);
599+
#endif
596600
memcpy(&insn, state->buf + offset, sizeof(uint32_t));
597601
if ((insn & 0xfe000000U) == 0x54000000U /* Conditional branch immediate. */
598602
|| (insn & 0x7e000000U) ==
@@ -607,9 +611,6 @@ static void update_branch_imm(struct jit_state *state,
607611
assert(false);
608612
insn = BAD_OPCODE;
609613
}
610-
#if defined(__APPLE__) && defined(__aarch64__)
611-
pthread_jit_write_protect_np(false);
612-
#endif
613614
memcpy(state->buf + offset, &insn, sizeof(uint32_t));
614615
sys_icache_invalidate(state->buf + offset, sizeof(uint32_t));
615616
#if defined(__APPLE__) && defined(__aarch64__)
@@ -2231,6 +2232,7 @@ static void resolve_jumps(struct jit_state *state)
22312232

22322233
uint8_t *offset_ptr = &state->buf[jump.offset_loc];
22332234
memcpy(offset_ptr, &rel, sizeof(uint32_t));
2235+
sys_icache_invalidate(offset_ptr, sizeof(uint32_t));
22342236
#elif defined(__aarch64__)
22352237
int32_t rel = target_loc - jump.offset_loc;
22362238
update_branch_imm(state, jump.offset_loc, rel);

0 commit comments

Comments
 (0)