Skip to content

Commit

Permalink
Add location hint to code block mmap call
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb authored and XrXr committed Oct 20, 2021
1 parent c20066b commit 304adba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
10 changes: 4 additions & 6 deletions ujit_asm.c
Expand Up @@ -117,7 +117,7 @@ void cb_init(codeblock_t* cb, size_t mem_size)
{
// Map the memory as executable
cb->mem_block = (uint8_t*)mmap(
NULL,
&cb_init,
mem_size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON,
Expand Down Expand Up @@ -1083,15 +1083,13 @@ void jmp_rm(codeblock_t* cb, x86opnd_t opnd)
cb_write_rm(cb, false, false, NO_OPND, opnd, 4, 1, 0xFF);
}

/*
/// Opcode for direct jump with relative 8-bit offset
const ubyte JMP_REL8_OPCODE = 0xEB;
*/

/*
/// jmp - Jump with relative 8-bit offset
void jmp8(CodeBlock cb, int8_t offset)
{
/// Opcode for direct jump with relative 8-bit offset
const ubyte JMP_REL8_OPCODE = 0xEB;
cb.writeASM("jmp", ((offset > 0)? "+":"-") ~ to!string(offset));
cb.writeByte(JMP_REL8_OPCODE);
cb.writeByte(offset);
Expand Down
30 changes: 28 additions & 2 deletions ujit_compile.c
Expand Up @@ -170,6 +170,7 @@ ujit_compile_insn(rb_iseq_t *iseq, unsigned int insn_idx, unsigned int* next_uji
st_data_t st_gen_fn;
if (!rb_st_lookup(gen_fns, opcode, &st_gen_fn))
{
//print_int(cb, imm_opnd(num_instrs));
//print_str(cb, insn_name(opcode));
break;
}
Expand Down Expand Up @@ -200,8 +201,6 @@ ujit_compile_insn(rb_iseq_t *iseq, unsigned int insn_idx, unsigned int* next_uji
return NULL;
}

//print_int(cb, imm_opnd(num_instrs));

// Write the adjusted SP back into the CFP
if (ctx.stack_diff != 0)
{
Expand All @@ -217,6 +216,33 @@ ujit_compile_insn(rb_iseq_t *iseq, unsigned int insn_idx, unsigned int* next_uji
// Write the post call bytes
ujit_instr_exit(cb);

/*
// Hack to patch a relative 32-bit jump to the instruction handler
int next_opcode = (int)*ctx.pc;
const void * const *table = rb_vm_get_insns_address_table();
VALUE encoded = (VALUE)table[next_opcode];
uint8_t* p_handler = (uint8_t*)encoded;
uint8_t* p_code = &cb->mem_block[cb->write_pos];
int64_t rel64 = ((int64_t)p_handler) - ((int64_t)p_code - 2 + 5);
//printf("p_handler: %lld\n", (int64_t)p_handler);
//printf("rel64: %lld\n", rel64);
uint8_t byte0 = cb->mem_block[cb->write_pos - 2];
uint8_t byte1 = cb->mem_block[cb->write_pos - 1];
//printf("cb_init: %lld\n", (int64_t)&cb_init);
//printf("%lld\n", rel64);
if (byte0 == 0xFF && byte1 == 0x20 && rel64 >= -2147483648 && rel64 <= 2147483647)
{
//printf("%02X %02X\n", (int)byte0, (int)byte1);
cb->write_pos -= 2;
jmp32(cb, (int32_t)rel64);
}
*/

addr2insn_bookkeeping(code_ptr, first_opcode);

return code_ptr;
Expand Down

0 comments on commit 304adba

Please sign in to comment.