[前][次][番号順一覧][スレッド一覧]

ruby-changes:70003

From: Aaron <ko1@a...>
Date: Thu, 2 Dec 2021 05:46:12 +0900 (JST)
Subject: [ruby-changes:70003] 157095b3a4 (master): Mark JIT code as writeable / executable depending on the situation

https://git.ruby-lang.org/ruby.git/commit/?id=157095b3a4

From 157095b3a44d8b0130a532a0b7be3f5ac197111c Mon Sep 17 00:00:00 2001
From: Aaron Patterson <tenderlove@r...>
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 <john@h...>
---
 misc/yjit_asm_tests.c |  2 +-
 yjit_asm.c            | 48 +++++++++++++++++++++++++++++++++++++++++++++---
 yjit_asm.h            |  9 +++++++++
 yjit_codegen.c        |  3 +++
 yjit_core.c           | 13 +++++++++++++
 yjit_iface.c          | 11 ++++++++++-
 6 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/misc/yjit_asm_tests.c b/misc/yjit_asm_tests.c
index 5548af07f53..b37d483ecf1 100644
--- a/misc/yjit_asm_tests.c
+++ b/misc/yjit_asm_tests.c
@@ -401,7 +401,7 @@ void run_runtime_tests(void) https://github.com/ruby/ruby/blob/trunk/misc/yjit_asm_tests.c#L401
     int (*function)(void);
     function = (int (*)(void))mem_block;
 
-    #define TEST(BODY) cb_set_pos(cb, 0); BODY ret(cb); assert_equal(7, function());
+    #define TEST(BODY) cb_set_pos(cb, 0); BODY ret(cb); cb_mark_all_executable(cb); assert_equal(7, function());
 
     // add
     TEST({ mov(cb, RAX, imm_opnd(0)); add(cb, RAX, imm_opnd(7)); })
diff --git a/yjit_asm.c b/yjit_asm.c
index 2ae50295a9b..d093b2edde8 100644
--- a/yjit_asm.c
+++ b/yjit_asm.c
@@ -163,7 +163,7 @@ static uint8_t *alloc_exec_mem(uint32_t mem_size) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L163
             mem_block = (uint8_t*)mmap(
                 (void*)req_addr,
                 mem_size,
-                PROT_READ | PROT_WRITE | PROT_EXEC,
+                PROT_READ | PROT_EXEC,
                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
                 -1,
                 0
@@ -184,7 +184,7 @@ static uint8_t *alloc_exec_mem(uint32_t mem_size) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L184
         mem_block = (uint8_t*)mmap(
             (void*)alloc_exec_mem,
             mem_size,
-            PROT_READ | PROT_WRITE | PROT_EXEC,
+            PROT_READ | PROT_EXEC,
             MAP_PRIVATE | MAP_ANONYMOUS,
             -1,
             0
@@ -197,7 +197,7 @@ static uint8_t *alloc_exec_mem(uint32_t mem_size) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L197
         mem_block = (uint8_t*)mmap(
             NULL,
             mem_size,
-            PROT_READ | PROT_WRITE | PROT_EXEC,
+            PROT_READ | PROT_EXEC,
             MAP_PRIVATE | MAP_ANONYMOUS,
             -1,
             0
@@ -210,9 +210,17 @@ static uint8_t *alloc_exec_mem(uint32_t mem_size) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L210
         exit(-1);
     }
 
+    codeblock_t block;
+    block.current_aligned_write_pos = ALIGNED_WRITE_POSITION_NONE;
+    block.mem_block = mem_block;
+    block.mem_size = mem_size;
+
+    codeblock_t * cb = &block;
     // Fill the executable memory with INT3 (0xCC) so that
     // executing uninitialized memory will fault
+    cb_mark_all_writeable(cb);
     memset(mem_block, 0xCC, mem_size);
+    cb_mark_all_executable(cb);
 
     return mem_block;
 #else
@@ -230,6 +238,7 @@ void cb_init(codeblock_t *cb, uint8_t *mem_block, uint32_t mem_size) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L238
     cb->write_pos = 0;
     cb->num_labels = 0;
     cb->num_refs = 0;
+    cb->current_aligned_write_pos = ALIGNED_WRITE_POSITION_NONE;
 }
 
 // Align the current write position to a multiple of bytes
@@ -277,6 +286,7 @@ void cb_write_byte(codeblock_t *cb, uint8_t byte) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L286
 {
     assert (cb->mem_block);
     assert (cb->write_pos + 1 <= cb->mem_size);
+    cb_mark_position_writeable(cb, cb->write_pos);
     cb->mem_block[cb->write_pos++] = byte;
 }
 
@@ -1771,3 +1781,35 @@ void cb_write_lock_prefix(codeblock_t *cb) https://github.com/ruby/ruby/blob/trunk/yjit_asm.c#L1781
 {
     cb_write_byte(cb, 0xF0);
 }
+
+void cb_mark_all_writeable(codeblock_t * cb)
+{
+    if (mprotect(cb->mem_block, cb->mem_size, PROT_READ | PROT_WRITE)) {
+        fprintf(stderr, "Couldn't make JIT page (%p) writeable, errno: %s", (void *)cb->mem_block, strerror(errno));
+        abort();
+    }
+}
+
+void cb_mark_position_writeable(codeblock_t * cb, uint32_t write_pos)
+{
+    uint32_t pagesize = (uint32_t)sysconf(_SC_PAGESIZE);
+    uint32_t aligned_position = (write_pos / pagesize) * pagesize;
+
+    if (cb->current_aligned_write_pos != aligned_position) {
+        cb->current_aligned_write_pos = aligned_position;
+        if (mprotect(cb->mem_block + aligned_position, pagesize, PROT_READ | PROT_WRITE)) {
+            fprintf(stderr, "Couldn't make JIT page (%p) writeable, errno: %s", (void *)(cb->mem_block + aligned_position), strerror(errno));
+            abort();
+        }
+    }
+}
+
+void cb_mark_all_executable(codeblock_t * cb)
+{
+    cb->current_aligned_write_pos = ALIGNED_WRITE_POSITION_NONE;
+    if (mprotect(cb->mem_block, cb->mem_size, PROT_READ | PROT_EXEC)) {
+        fprintf(stderr, "Couldn't make JIT page (%p) executable, errno: %s", (void *)cb->mem_block, strerror(errno));
+        abort();
+    }
+}
+
diff --git a/yjit_asm.h b/yjit_asm.h
index b1b2baae2e5..ad032d01392 100644
--- a/yjit_asm.h
+++ b/yjit_asm.h
@@ -55,8 +55,14 @@ typedef struct CodeBlock https://github.com/ruby/ruby/blob/trunk/yjit_asm.h#L55
     // Flag to enable or disable comments
     bool has_asm;
 
+    // Keep track of the current aligned write position.
+    // Used for changing protection when writing to the JIT buffer
+    uint32_t current_aligned_write_pos;
 } codeblock_t;
 
+// 1 is not aligned so this won't match any pages
+#define ALIGNED_WRITE_POSITION_NONE 1
+
 enum OpndType
 {
     OPND_NONE,
@@ -261,6 +267,9 @@ static inline uint32_t cb_new_label(codeblock_t *cb, const char *name); https://github.com/ruby/ruby/blob/trunk/yjit_asm.h#L267
 static inline void cb_write_label(codeblock_t *cb, uint32_t label_idx);
 static inline void cb_label_ref(codeblock_t *cb, uint32_t label_idx);
 static inline void cb_link_labels(codeblock_t *cb);
+static inline void cb_mark_all_writeable(codeblock_t *cb);
+static inline void cb_mark_position_writeable(codeblock_t *cb, uint32_t write_pos);
+static inline void cb_mark_all_executable(codeblock_t *cb);
 
 // Encode individual instructions into a code block
 static inline void add(codeblock_t *cb, x86opnd_t opnd0, x86opnd_t opnd1);
diff --git a/yjit_codegen.c b/yjit_codegen.c
index 7b44874af86..96f895b9346 100644
--- a/yjit_codegen.c
+++ b/yjit_codegen.c
@@ -4876,6 +4876,8 @@ rb_yjit_tracing_invalidate_all(void) https://github.com/ruby/ruby/blob/trunk/yjit_codegen.c#L4876
     RUBY_ASSERT_ALWAYS(yjit_codepage_frozen_bytes <= old_pos && "frozen bytes should increase monotonically");
     yjit_codepage_frozen_bytes = old_pos;
 
+    cb_mark_all_executable(ocb);
+    cb_mark_all_executable(cb);
     RB_VM_LOCK_LEAVE();
 }
 
@@ -4957,6 +4959,7 @@ yjit_init_codegen(void) https://github.com/ruby/ruby/blob/trunk/yjit_codegen.c#L4959
 
     // Generate full exit code for C func
     gen_full_cfunc_return();
+    cb_mark_all_executable(cb);
 
     // Map YARV opcodes to the corresponding codegen functions
     yjit_reg_op(BIN(nop), gen_nop);
diff --git a/yjit_core.c b/yjit_core.c
index f19b83c5ff5..00905e7f249 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 https://github.com/ruby/ruby/blob/trunk/yjit_core.c#L833
     // 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 https://github.com/ruby/ruby/blob/trunk/yjit_core.c#L876
         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 https://github.com/ruby/ruby/blob/trunk/yjit_core.c#L958
             // 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 https://github.com/ruby/ruby/blob/trunk/yjit_core.c#L1210
 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) https://github.com/ruby/ruby/blob/trunk/yj (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]