2020-12-08 16:54:41 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include "insns.inc"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "vm_core.h"
|
|
|
|
#include "vm_sync.h"
|
|
|
|
#include "vm_callinfo.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "internal/compile.h"
|
|
|
|
#include "internal/class.h"
|
|
|
|
#include "insns_info.inc"
|
|
|
|
#include "ujit.h"
|
|
|
|
#include "ujit_iface.h"
|
|
|
|
#include "ujit_codegen.h"
|
|
|
|
#include "ujit_core.h"
|
|
|
|
#include "ujit_hooks.inc"
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
#include "ujit.rbinc"
|
|
|
|
#include <capstone/capstone.h>
|
2020-12-08 16:54:41 -05:00
|
|
|
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
VALUE cUjitBlock;
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
VALUE cUjitDisasm;
|
|
|
|
VALUE cUjitDisasmInsn;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
|
|
|
extern st_table * version_tbl;
|
|
|
|
extern codeblock_t *cb;
|
|
|
|
|
|
|
|
static const rb_data_type_t ujit_block_type = {
|
|
|
|
"UJIT/Block",
|
|
|
|
{0, 0, 0, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
static const rb_data_type_t ujit_disasm_type = {
|
|
|
|
"UJIT/Disasm",
|
|
|
|
{0, (void(*)(void *))cs_close, 0, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
bool rb_ujit_enabled;
|
|
|
|
|
|
|
|
// Hash table of encoded instructions
|
|
|
|
extern st_table *rb_encoded_insn_data;
|
|
|
|
|
2020-12-08 17:19:28 -05:00
|
|
|
// Write the uJIT entry point pre-call bytes
|
|
|
|
void
|
|
|
|
cb_write_pre_call_bytes(codeblock_t* cb)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < sizeof(ujit_with_ec_pre_call_bytes); ++i)
|
|
|
|
cb_write_byte(cb, ujit_with_ec_pre_call_bytes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the uJIT exit post-call bytes
|
|
|
|
void
|
|
|
|
cb_write_post_call_bytes(codeblock_t* cb)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < sizeof(ujit_with_ec_post_call_bytes); ++i)
|
|
|
|
cb_write_byte(cb, ujit_with_ec_post_call_bytes[i]);
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// Keep track of mapping from instructions to generated code
|
|
|
|
// See comment for rb_encoded_insn_data in iseq.c
|
|
|
|
void
|
|
|
|
map_addr2insn(void *code_ptr, int insn)
|
|
|
|
{
|
|
|
|
const void * const *table = rb_vm_get_insns_address_table();
|
|
|
|
const void * const translated_address = table[insn];
|
|
|
|
st_data_t encoded_insn_data;
|
|
|
|
if (st_lookup(rb_encoded_insn_data, (st_data_t)translated_address, &encoded_insn_data)) {
|
|
|
|
st_insert(rb_encoded_insn_data, (st_data_t)code_ptr, encoded_insn_data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_bug("ujit: failed to find info for original instruction while dealing with addr2insn");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
opcode_at_pc(const rb_iseq_t *iseq, const VALUE *pc)
|
|
|
|
{
|
|
|
|
const VALUE at_pc = *pc;
|
|
|
|
if (FL_TEST_RAW((VALUE)iseq, ISEQ_TRANSLATED)) {
|
|
|
|
return rb_vm_insn_addr2opcode((const void *)at_pc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (int)at_pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:10:52 -05:00
|
|
|
// Verify that calling with cd on receiver goes to callee
|
|
|
|
void
|
|
|
|
check_cfunc_dispatch(VALUE receiver, struct rb_call_data *cd, void *callee, rb_callable_method_entry_t *compile_time_cme)
|
|
|
|
{
|
|
|
|
if (METHOD_ENTRY_INVALIDATED(compile_time_cme)) {
|
|
|
|
rb_bug("ujit: output code uses invalidated cme %p", (void *)compile_time_cme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool callee_correct = false;
|
|
|
|
const rb_callable_method_entry_t *cme = rb_callable_method_entry(CLASS_OF(receiver), vm_ci_mid(cd->ci));
|
|
|
|
if (cme->def->type == VM_METHOD_TYPE_CFUNC) {
|
|
|
|
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(cme->def, body.cfunc);
|
|
|
|
if ((void *)cfunc->func == callee) {
|
|
|
|
callee_correct = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!callee_correct) {
|
|
|
|
rb_bug("ujit: output code calls wrong method cd->cc->klass: %p", (void *)cd->cc->klass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MJIT_FUNC_EXPORTED VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
|
|
|
|
|
|
|
bool
|
|
|
|
cfunc_needs_frame(const rb_method_cfunc_t *cfunc)
|
|
|
|
{
|
|
|
|
void* fptr = (void*)cfunc->func;
|
|
|
|
|
|
|
|
// Leaf C functions do not need a stack frame
|
|
|
|
// or a stack overflow check
|
|
|
|
return !(
|
|
|
|
// Hash#key?
|
|
|
|
fptr == (void*)rb_hash_has_key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// GC root for interacting with the GC
|
|
|
|
struct ujit_root_struct {};
|
|
|
|
|
|
|
|
// Map cme_or_cc => [[iseq, offset]]. An entry in the map means compiled code at iseq[offset]
|
|
|
|
// is only valid when cme_or_cc is valid
|
|
|
|
static st_table *method_lookup_dependency;
|
|
|
|
|
|
|
|
struct compiled_region_array {
|
|
|
|
int32_t size;
|
|
|
|
int32_t capa;
|
2021-01-14 16:58:20 -05:00
|
|
|
block_t* data[];
|
2020-12-08 16:54:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add an element to a region array, or allocate a new region array.
|
|
|
|
static struct compiled_region_array *
|
2021-01-14 16:58:20 -05:00
|
|
|
add_compiled_region(struct compiled_region_array *array, block_t* block)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
|
|
|
if (!array) {
|
|
|
|
// Allocate a brand new array with space for one
|
2021-01-14 16:58:20 -05:00
|
|
|
array = malloc(sizeof(*array) + sizeof(block_t*));
|
2020-12-08 16:54:41 -05:00
|
|
|
if (!array) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
array->size = 0;
|
|
|
|
array->capa = 1;
|
|
|
|
}
|
|
|
|
if (array->size == INT32_MAX) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Check if the region is already present
|
|
|
|
for (int32_t i = 0; i < array->size; i++) {
|
2021-01-14 16:58:20 -05:00
|
|
|
if (array->data[i] == block) {
|
2020-12-08 16:54:41 -05:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (array->size + 1 > array->capa) {
|
|
|
|
// Double the array's capacity.
|
|
|
|
int64_t double_capa = ((int64_t)array->capa) * 2;
|
|
|
|
int32_t new_capa = (int32_t)double_capa;
|
|
|
|
if (new_capa != double_capa) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-01-14 16:58:20 -05:00
|
|
|
array = realloc(array, sizeof(*array) + new_capa * sizeof(block_t*));
|
2020-12-08 16:54:41 -05:00
|
|
|
if (array == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
array->capa = new_capa;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t size = array->size;
|
2021-01-14 16:58:20 -05:00
|
|
|
array->data[size] = block;
|
2020-12-08 16:54:41 -05:00
|
|
|
array->size++;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_lookup_dependency_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
|
|
|
|
{
|
2021-01-14 16:58:20 -05:00
|
|
|
block_t *block = (block_t *)data;
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
struct compiled_region_array *regions = NULL;
|
|
|
|
if (existing) {
|
|
|
|
regions = (struct compiled_region_array *)*value;
|
|
|
|
}
|
2021-01-14 16:58:20 -05:00
|
|
|
regions = add_compiled_region(regions, block);
|
2020-12-08 16:54:41 -05:00
|
|
|
if (!regions) {
|
|
|
|
rb_bug("ujit: failed to add method lookup dependency"); // TODO: we could bail out of compiling instead
|
|
|
|
}
|
|
|
|
*value = (st_data_t)regions;
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember that the currently compiling region is only valid while cme and cc are valid
|
|
|
|
void
|
2021-01-14 16:58:20 -05:00
|
|
|
assume_method_lookup_stable(const struct rb_callcache *cc, const rb_callable_method_entry_t *cme, block_t* block)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
2021-01-22 13:29:09 -05:00
|
|
|
RUBY_ASSERT(block != NULL);
|
2021-01-14 16:58:20 -05:00
|
|
|
st_update(method_lookup_dependency, (st_data_t)cme, add_lookup_dependency_i, (st_data_t)block);
|
|
|
|
st_update(method_lookup_dependency, (st_data_t)cc, add_lookup_dependency_i, (st_data_t)block);
|
2020-12-08 16:54:41 -05:00
|
|
|
// FIXME: This is a leak! When either the cme or the cc become invalid, the other also needs to go
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ujit_root_mark_i(st_data_t k, st_data_t v, st_data_t ignore)
|
|
|
|
{
|
|
|
|
// FIXME: This leaks everything that end up in the dependency table!
|
|
|
|
// One way to deal with this is with weak references...
|
|
|
|
rb_gc_mark((VALUE)k);
|
|
|
|
struct compiled_region_array *regions = (void *)v;
|
|
|
|
for (int32_t i = 0; i < regions->size; i++) {
|
2021-01-14 16:58:20 -05:00
|
|
|
rb_gc_mark((VALUE)regions->data[i]->blockid.iseq);
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC callback during mark phase
|
|
|
|
static void
|
|
|
|
ujit_root_mark(void *ptr)
|
|
|
|
{
|
|
|
|
if (method_lookup_dependency) {
|
|
|
|
st_foreach(method_lookup_dependency, ujit_root_mark_i, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ujit_root_free(void *ptr)
|
|
|
|
{
|
|
|
|
// Do nothing. The root lives as long as the process.
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
ujit_root_memsize(const void *ptr)
|
|
|
|
{
|
|
|
|
// Count off-gc-heap allocation size of the dependency table
|
|
|
|
return st_memsize(method_lookup_dependency); // TODO: more accurate accounting
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom type for interacting with the GC
|
|
|
|
// TODO: compaction support
|
|
|
|
// TODO: make this write barrier protected
|
|
|
|
static const rb_data_type_t ujit_root_type = {
|
|
|
|
"ujit_root",
|
|
|
|
{ujit_root_mark, ujit_root_free, ujit_root_memsize, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
|
|
|
// Callback when cme or cc become invalid
|
|
|
|
void
|
|
|
|
rb_ujit_method_lookup_change(VALUE cme_or_cc)
|
|
|
|
{
|
2021-01-14 16:58:20 -05:00
|
|
|
if (!method_lookup_dependency)
|
|
|
|
return;
|
2020-12-08 16:54:41 -05:00
|
|
|
|
|
|
|
RUBY_ASSERT(IMEMO_TYPE_P(cme_or_cc, imemo_ment) || IMEMO_TYPE_P(cme_or_cc, imemo_callcache));
|
|
|
|
|
|
|
|
st_data_t image;
|
|
|
|
if (st_lookup(method_lookup_dependency, (st_data_t)cme_or_cc, &image)) {
|
|
|
|
struct compiled_region_array *array = (void *)image;
|
2021-01-14 16:58:20 -05:00
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// Invalidate all regions that depend on the cme or cc
|
|
|
|
for (int32_t i = 0; i < array->size; i++) {
|
2021-01-14 16:58:20 -05:00
|
|
|
block_t* block = array->data[i];
|
|
|
|
invalidate(block);
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
array->size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ujit_compile_iseq(const rb_iseq_t *iseq)
|
|
|
|
{
|
|
|
|
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
VALUE *encoded = (VALUE *)iseq->body->iseq_encoded;
|
|
|
|
|
2020-12-10 00:06:10 -05:00
|
|
|
// Compile a block version starting at the first instruction
|
2021-01-13 14:14:16 -05:00
|
|
|
uint8_t* code_ptr = gen_entry_point(iseq, 0);
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-01-13 14:14:16 -05:00
|
|
|
if (code_ptr)
|
|
|
|
{
|
|
|
|
// Map the code address to the corresponding opcode
|
|
|
|
int first_opcode = opcode_at_pc(iseq, &encoded[0]);
|
|
|
|
map_addr2insn(code_ptr, first_opcode);
|
|
|
|
encoded[0] = (VALUE)code_ptr;
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
2020-12-10 00:06:10 -05:00
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
struct ujit_block_itr {
|
|
|
|
const rb_iseq_t *iseq;
|
|
|
|
VALUE list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
iseqw_ujit_collect_blocks(st_data_t key, st_data_t value, st_data_t argp)
|
|
|
|
{
|
|
|
|
block_t * block = (block_t *)value;
|
|
|
|
struct ujit_block_itr * itr = (struct ujit_block_itr *)argp;
|
|
|
|
|
|
|
|
if (block->blockid.iseq == itr->iseq) {
|
|
|
|
VALUE rb_block = TypedData_Wrap_Struct(cUjitBlock, &ujit_block_type, block);
|
|
|
|
rb_ary_push(itr->list, rb_block);
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a list of the UJIT blocks associated with `rb_iseq` */
|
|
|
|
static VALUE
|
|
|
|
ujit_blocks_for(VALUE mod, VALUE rb_iseq)
|
|
|
|
{
|
|
|
|
const rb_iseq_t *iseq = rb_iseqw_to_iseq(rb_iseq);
|
|
|
|
st_table * vt = (st_table *)version_tbl;
|
|
|
|
struct ujit_block_itr itr;
|
|
|
|
itr.iseq = iseq;
|
|
|
|
itr.list = rb_ary_new();
|
|
|
|
|
|
|
|
rb_st_foreach(vt, iseqw_ujit_collect_blocks, (st_data_t)&itr);
|
|
|
|
|
|
|
|
return itr.list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2021-01-22 12:22:34 -05:00
|
|
|
ujit_install_entry(VALUE mod, VALUE iseq)
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
{
|
|
|
|
rb_ujit_compile_iseq(rb_iseqw_to_iseq(iseq));
|
|
|
|
return iseq;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the address of the UJIT::Block */
|
|
|
|
static VALUE
|
|
|
|
block_address(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
|
|
|
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
|
|
|
|
return LONG2NUM((intptr_t)block);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the machine code for UJIT::Block as a binary string */
|
|
|
|
static VALUE
|
|
|
|
block_code(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
|
|
|
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
|
|
|
|
|
2021-01-22 12:22:34 -05:00
|
|
|
return (VALUE)rb_str_new(
|
|
|
|
(const char*)cb->mem_block + block->start_pos,
|
|
|
|
block->end_pos - block->start_pos
|
|
|
|
);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the start index in the Instruction Sequence that corresponds to this
|
|
|
|
* UJIT::Block */
|
|
|
|
static VALUE
|
|
|
|
iseq_start_index(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
|
|
|
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
|
|
|
|
|
|
|
|
return INT2NUM(block->blockid.idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the end index in the Instruction Sequence that corresponds to this
|
|
|
|
* UJIT::Block */
|
|
|
|
static VALUE
|
|
|
|
iseq_end_index(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
|
|
|
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
|
|
|
|
|
|
|
|
return INT2NUM(block->end_idx);
|
|
|
|
}
|
|
|
|
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
static VALUE
|
|
|
|
ujit_disasm_open(VALUE mod, VALUE arch, VALUE mode)
|
|
|
|
{
|
|
|
|
csh * handle;
|
|
|
|
VALUE disasm = TypedData_Make_Struct(cUjitDisasm, csh, &ujit_disasm_type, handle);
|
|
|
|
cs_open(NUM2INT(arch), NUM2INT(mode), handle);
|
|
|
|
return disasm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ujit_disasm(VALUE self, VALUE code, VALUE from)
|
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
csh * handle;
|
|
|
|
cs_insn *insns;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(self, csh, &ujit_disasm_type, handle);
|
|
|
|
count = cs_disasm(*handle, StringValuePtr(code), RSTRING_LEN(code), NUM2INT(from), 0, &insns);
|
|
|
|
VALUE insn_list = rb_ary_new_capa(count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
VALUE vals = rb_ary_new_from_args(3, LONG2NUM(insns[i].address),
|
|
|
|
rb_str_new2(insns[i].mnemonic),
|
|
|
|
rb_str_new2(insns[i].op_str));
|
|
|
|
rb_ary_push(insn_list, rb_struct_alloc(cUjitDisasmInsn, vals));
|
|
|
|
}
|
|
|
|
cs_free(insns, count);
|
|
|
|
return insn_list;
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
void
|
|
|
|
rb_ujit_init(void)
|
|
|
|
{
|
|
|
|
if (!ujit_scrape_successful || !PLATFORM_SUPPORTED_P)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_ujit_enabled = true;
|
|
|
|
|
2020-12-10 16:59:13 -05:00
|
|
|
ujit_init_core();
|
2020-12-08 16:54:41 -05:00
|
|
|
ujit_init_codegen();
|
|
|
|
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
VALUE mUjit = rb_define_module("UJIT");
|
2021-01-22 12:22:34 -05:00
|
|
|
rb_define_module_function(mUjit, "install_entry", ujit_install_entry, 1);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
rb_define_module_function(mUjit, "blocks_for", ujit_blocks_for, 1);
|
|
|
|
|
|
|
|
cUjitBlock = rb_define_class_under(mUjit, "Block", rb_cObject);
|
|
|
|
rb_define_method(cUjitBlock, "address", block_address, 0);
|
|
|
|
rb_define_method(cUjitBlock, "code", block_code, 0);
|
|
|
|
rb_define_method(cUjitBlock, "iseq_start_index", iseq_start_index, 0);
|
|
|
|
rb_define_method(cUjitBlock, "iseq_end_index", iseq_end_index, 0);
|
|
|
|
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
cUjitDisasm = rb_define_class_under(mUjit, "Disasm", rb_cObject);
|
|
|
|
rb_define_const(cUjitDisasm, "ARCH_X86", INT2NUM(CS_ARCH_X86));
|
|
|
|
rb_define_const(cUjitDisasm, "MODE_64", INT2NUM(CS_MODE_64));
|
|
|
|
rb_define_module_function(cUjitDisasm, "open", ujit_disasm_open, 2);
|
|
|
|
rb_define_method(cUjitDisasm, "disasm", ujit_disasm, 2);
|
|
|
|
|
|
|
|
cUjitDisasmInsn = rb_struct_define_under(cUjitDisasm, "Insn", "address", "mnemonic", "op_str", NULL);
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// Initialize the GC hooks
|
|
|
|
method_lookup_dependency = st_init_numtable();
|
|
|
|
struct ujit_root_struct *root;
|
|
|
|
VALUE ujit_root = TypedData_Make_Struct(0, struct ujit_root_struct, &ujit_root_type, root);
|
|
|
|
rb_gc_register_mark_object(ujit_root);
|
|
|
|
}
|