From 157095b3a44d8b0130a532a0b7be3f5ac197111c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 26 Oct 2021 16:57:30 -0700 Subject: Mark JIT code as writeable / executable depending on the situation Some platforms don't want memory to be marked as writeable and executable at the same time. When we write to the code block, we calculate the OS page that the buffer position maps to. Then we call `mprotect` to allow writes on that particular page. As an optimization, we cache the "last written" aligned page which allows us to amortize the cost of the `mprotect` call. In other words, sequential writes to the same page will only call `mprotect` on the page once. When we're done writing, we call `mprotect` on the entire JIT buffer. This means we don't need to keep track of which pages were marked as writeable, we let the OS take care of that. Co-authored-by: John Hawthorn --- yjit_core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'yjit_core.c') diff --git a/yjit_core.c b/yjit_core.c index f19b83c5ff..00905e7f24 100644 --- a/yjit_core.c +++ b/yjit_core.c @@ -833,12 +833,16 @@ gen_entry_point(const rb_iseq_t *iseq, uint32_t insn_idx, rb_execution_context_t // The entry context makes no assumptions about types blockid_t blockid = { iseq, insn_idx }; + rb_vm_barrier(); // Write the interpreter entry prologue. Might be NULL when out of memory. uint8_t *code_ptr = yjit_entry_prologue(cb, iseq); // Try to generate code for the entry block block_t *block = gen_block_version(blockid, &DEFAULT_CTX, ec); + cb_mark_all_executable(ocb); + cb_mark_all_executable(cb); + // If we couldn't generate any code if (!block || block->end_idx == insn_idx) { return NULL; @@ -872,6 +876,8 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex dst_addr = branch->dst_addrs[target_idx]; } else { + rb_vm_barrier(); + // :stub-sp-flush: // Generated code do stack operations without modifying cfp->sp, while the // cfp->sp tells the GC what values on the stack to root. Generated code @@ -952,6 +958,9 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex // frame. We do that in code_for_exit_from_stub. dst_addr = code_for_exit_from_stub; } + + cb_mark_all_executable(ocb); + cb_mark_all_executable(cb); } const ptrdiff_t new_branch_size = branch_code_size(branch); @@ -1201,6 +1210,7 @@ static void invalidate_block_version(block_t *block) { ASSERT_vm_locking(); + // TODO: want to assert that all other ractors are stopped here. Can't patch // machine code that some other thread is running. @@ -1324,6 +1334,9 @@ invalidate_block_version(block_t *block) yjit_runtime_counters.invalidation_count++; #endif + cb_mark_all_executable(ocb); + cb_mark_all_executable(cb); + // fprintf(stderr, "invalidation done\n"); } -- cgit v1.2.3