8000 use a macro so we do not leak globals · ruby/ruby@fec7f83 · GitHub
[go: up one dir, main page]

Skip to content

Commit fec7f83

Browse files
committed
use a macro so we do not leak globals
1 parent 0e60bdb commit fec7f83

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

yjit_asm.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,6 @@ static uint8_t *align_ptr(uint8_t *ptr, uint32_t multiple)
146146
return ptr + pad;
147147
}
148148

149-
void cb_mark_writeable(codeblock_t *cb)
150-
{
151-
if (mprotect(cb->mem_block, cb->mem_size, PROT_READ | PROT_WRITE)) {
152-
rb_bug("Couldn't make JIT page (%p) writeable, errno: %s", (void *)cb->mem_block, strerror(errno));
153-
}
154-
}
155-
156-
void cb_mark_executable(codeblock_t *cb)
157-
{
158-
if (mprotect(cb->mem_block, cb->mem_size, PROT_READ | PROT_EXEC)) {
159-
rb_bug("Couldn't make JIT page (%p) executable, errno: %s", (void *)cb->mem_block, strerror(errno));
160-
}
161-
}
162-
163149
// Allocate a block of executable memory
164150
uint8_t *alloc_exec_mem(uint32_t mem_size)
165151
{
@@ -227,11 +213,13 @@ uint8_t *alloc_exec_mem(uint32_t mem_size)
227213
codeblock_t block;
228214
block.mem_block = mem_block;
229215
block.mem_size = mem_size;
216+
217+
codeblock_t * cb = █
230218
// Fill the executable memory with INT3 (0xCC) so that
231219
// executing uninitialized memory will fault
232-
cb_mark_writeable(&block);
220+
CB_MARK_WRITEABLE(cb);
233221
memset(mem_block, 0xCC, mem_size);
234-
cb_mark_executable(&block);
222+
CB_MARK_EXECUTABLE(cb);
235223

236224
return mem_block;
237225
#else

yjit_asm.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,18 @@ static inline uint32_t cb_new_label(codeblock_t *cb, const char *name);
283283
static inline void cb_write_label(codeblock_t *cb, uint32_t label_idx);
284284
static inline void cb_label_ref(codeblock_t *cb, uint32_t label_idx);
285285
static inline void cb_link_labels(codeblock_t *cb);
286-
void cb_mark_writeable(codeblock_t *cb);
287-
void cb_mark_executable(codeblock_t *cb);
286+
287+
#define CB_MARK_WRITEABLE(cb) do { \
288+
if (mprotect(cb->mem_block, cb->mem_size, PROT_READ | PROT_WRITE)) { \
289+
rb_bug("Couldn't make JIT page (%p) writeable, errno: %s", (void *)cb->mem_block, strerror(errno)); \
290+
} \
291+
} while(0);
292+
293+
#define CB_MARK_EXECUTABLE(cb) do { \
294+
if (mprotect(cb->mem_block, cb->mem_size, PROT_READ | PROT_EXEC)) { \
295+
rb_bug("Couldn't make JIT page (%p) writeable, errno: %s", (void *)cb->mem_block, strerror(errno)); \
296+
} \
297+
} while(0);
288298

289299
// Encode individual instructions into a code block
290300
static inline void add(codeblock_t *cb, x86opnd_t opnd0, x86opnd_t opnd1);

yjit_codegen.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ gen_full_cfunc_return(void)
475475
codeblock_t *cb = ocb;
476476
outline_full_cfunc_return_pos = ocb->write_pos;
477477

478-
cb_mark_writeable(cb);
478+
CB_MARK_WRITEABLE(cb);
479479

480480
// This chunk of code expect REG_EC to be filled properly and
481481
// RAX to contain the return value of the C method.
@@ -495,7 +495,7 @@ gen_full_cfunc_return(void)
495495

496496
mov(cb, RAX, imm_opnd(Qundef));
497497
ret(cb);
498-
cb_mark_executable(cb);
498+
CB_MARK_EXECUTABLE(cb);
499499
}
500500

501501
/*
@@ -4641,22 +4641,22 @@ yjit_init_codegen(void)
46414641
uint8_t *mem_block = alloc_exec_mem(mem_size);
46424642

46434643
cb = █
4644-
cb_mark_writeable(cb);
4644+
CB_MARK_WRITEABLE(cb);
46454645
cb_init(cb, mem_block, mem_size/2);
4646-
cb_mark_executable(cb);
4646+
CB_MARK_EXECUTABLE(cb);
46474647

46484648
ocb = &outline_block;
4649-
cb_mark_writeable(ocb);
4649+
CB_MARK_WRITEABLE(ocb);
46504650
cb_init(ocb, mem_block + mem_size/2, mem_size/2);
4651-
cb_mark_executable(ocb);
4651+
CB_MARK_EXECUTABLE(ocb);
46524652

46534653
// Generate the interpreter exit code for leave
4654-
cb_mark_writeable(cb);
4654+
CB_MARK_WRITEABLE(cb);
46554655
leave_exit_code = yjit_gen_leave_exit(cb);
46564656

46574657
// Generate full exit code for C func
46584658
gen_full_cfunc_return();
4659-
cb_mark_writeable(cb);
4659+
CB_MARK_WRITEABLE(cb);
46604660

46614661
// Map YARV opcodes to the corresponding codegen functions
46624662
yjit_reg_op(BIN(nop), gen_nop);

yjit_core.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -739,16 +739,16 @@ gen_entry_point(const rb_iseq_t *iseq, uint32_t insn_idx, rb_execution_context_t
739739
// The entry context makes no assumptions about types
740740
blockid_t blockid = { iseq, insn_idx };
741741

742-
cb_mark_writeable(cb);
743-
cb_mark_writeable(ocb);
742+
CB_MARK_WRITEABLE(cb);
743+
CB_MARK_WRITEABLE(ocb);
744744
// Write the interpreter entry prologue
745745
uint8_t *code_ptr = yjit_entry_prologue(cb, iseq);
746746

747747
// Try to generate code for the entry block
748748
block_t *block = gen_block_version(blockid, &DEFAULT_CTX, ec);
749749

750-
cb_mark_executable(ocb);
751-
cb_mark_executable(cb);
750+
CB_MARK_EXECUTABLE(ocb);
751+
CB_MARK_EXECUTABLE(cb);
752752

753753
// If we couldn't generate any code
754754
if (block->end_idx == insn_idx)
@@ -783,8 +783,8 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex
783783
}
784784
else
785785
{
786-
cb_mark_writeable(cb);
787-
cb_mark_writeable(ocb);
786+
CB_MARK_WRITEABLE(cb);
787+
CB_MARK_WRITEABLE(ocb);
788788
//fprintf(stderr, "\nstub hit, branch: %p, target idx: %d\n", branch, target_idx);
789789
//fprintf(stderr, "blockid.iseq=%p, blockid.idx=%d\n", target.iseq, target.idx);
790790
//fprintf(stderr, "chain_depth=%d\n", target_ctx->chain_depth);
@@ -852,8 +852,8 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex
852852
// Restore interpreter sp, since the code hitting the stub expects the original.
853853
ec->cfp->sp = original_interp_sp;
854854

855-
cb_mark_executable(ocb);
856-
cb_mark_executable(cb);
855+
CB_MARK_EXECUTABLE(ocb);
856+
CB_MARK_EXECUTABLE(cb);
857857
}
858858

859859
RB_VM_LOCK_LEAVE();
@@ -1093,8 +1093,8 @@ static void
10931093
invalidate_block_version(block_t *block)
10941094
{
10951095
ASSERT_vm_locking();
1096-
cb_mark_writeable(cb);
1097-
cb_mark_writeable(ocb);
1096+
CB_MARK_WRITEABLE(cb);
1097+
CB_MARK_WRITEABLE(ocb);
10981098

10991099
// TODO: want to assert that all other ractors are stopped here. Can't patch
11001100
// machine code that some other thread is running.
@@ -1189,8 +1189,8 @@ invalidate_block_version(block_t *block)
11891189
yjit_runtime_counters.invalidation_count++;
11901190
#endif
11911191

1192-
cb_mark_executable(ocb);
1193-
cb_mark_executable(cb);
1192+
CB_MARK_EXECUTABLE(ocb);
1193+
CB_MARK_EXECUTABLE(cb);
11941194

11951195
// fprintf(stderr, "invalidation done\n");
11961196
}

0 commit comments

Comments
 (0)
0