From e427fdff0af907faec8771b98e9cabeaadd05f12 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 22 Jan 2021 10:43:26 -0800 Subject: 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: # (catch: FALSE) local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) [ 1] x@0 0000 getlocal_WC_0 x@0 ( 2)[LiCa] 0002 putobject_INT2FIX_1_ 0003 opt_lt 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` --- common.mk | 2 ++ configure.ac | 11 +++++++++++ inits.c | 1 + misc/ujit_disasm.rb | 35 ----------------------------------- ujit.rb | 31 +++++++++++++++++++++++++++++++ ujit_iface.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 35 deletions(-) delete mode 100644 misc/ujit_disasm.rb create mode 100644 ujit.rb diff --git a/common.mk b/common.mk index 9cab62b60a..73f4c57473 100644 --- a/common.mk +++ b/common.mk @@ -1063,6 +1063,7 @@ BUILTIN_RB_SRCS = \ $(srcdir)/nilclass.rb \ $(srcdir)/prelude.rb \ $(srcdir)/gem_prelude.rb \ + $(srcdir)/ujit.rb \ $(empty) BUILTIN_RB_INCS = $(BUILTIN_RB_SRCS:.rb=.rbinc) @@ -15641,6 +15642,7 @@ ujit_core.$(OBJEXT): {$(VPATH)}vm_core.h ujit_core.$(OBJEXT): {$(VPATH)}vm_debug.h ujit_core.$(OBJEXT): {$(VPATH)}vm_opts.h ujit_core.$(OBJEXT): {$(VPATH)}vm_sync.h +ujit_iface.$(OBJEXT): {$(VPATH)}ujit.rbinc ujit_iface.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h ujit_iface.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h ujit_iface.$(OBJEXT): $(CCAN_DIR)/list/list.h diff --git a/configure.ac b/configure.ac index fa844eb7cd..b34004da1e 100644 --- a/configure.ac +++ b/configure.ac @@ -1229,6 +1229,17 @@ AC_CHECK_LIB(dl, dlopen) # Dynamic linking for SunOS/Solaris and SYSV AC_CHECK_LIB(dld, shl_load) # Dynamic linking for HP-UX AC_CHECK_LIB(socket, shutdown) # SunOS/Solaris +if pkg-config --exists capstone; then + CAPSTONE_CFLAGS=`pkg-config --cflags capstone` + CAPSTONE_LIB_L=`pkg-config --libs-only-L capstone` + LDFLAGS="$LDFLAGS $CAPSTONE_LIB_L" + CFLAGS="$CFLAGS $CAPSTONE_CFLAGS" +else + AC_MSG_ERROR(Please install capstone and pkg-config) +fi + +AC_CHECK_LIB(capstone, cs_open) # Capstone + dnl Checks for header files. AC_HEADER_DIRENT dnl AC_HEADER_STDC has been checked in AC_USE_SYSTEM_EXTENSIONS diff --git a/inits.c b/inits.c index f69ee73a89..059055246a 100644 --- a/inits.c +++ b/inits.c @@ -99,6 +99,7 @@ rb_call_builtin_inits(void) BUILTIN(timev); BUILTIN(nilclass); BUILTIN(marshal); + BUILTIN(ujit); Init_builtin_prelude(); } #undef CALL diff --git a/misc/ujit_disasm.rb b/misc/ujit_disasm.rb deleted file mode 100644 index 38eb139c33..0000000000 --- a/misc/ujit_disasm.rb +++ /dev/null @@ -1,35 +0,0 @@ -begin - require "crabstone" - require "stringio" -rescue LoadError => e - puts "Please install crabstone, which is needed by the disassembler:" - puts " $ brew install capstone" - puts " $ gem install capstone" - raise e -end - -module UJIT - def self.disasm(iseq) - blocks = UJIT.blocks_for(iseq) - return if blocks.empty? - - io = StringIO.new - - cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64) - - io.puts iseq.disasm - - blocks.sort_by(&:address).reverse.each do |block| - io.puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=") - cs.disasm(block.code, 0).each do |i| - io.printf( - "\t0x%
x:\t%s\t%
s\n", - address: i.address, - instruction: i.mnemonic, - details: i.op_str - ) - end - end - io.string - end -end diff --git a/ujit.rb b/ujit.rb new file mode 100644 index 0000000000..cdfb886795 --- /dev/null +++ b/ujit.rb @@ -0,0 +1,31 @@ +module UJIT + def omg + end + + def self.disasm(iseq) + blocks = UJIT.blocks_for(iseq) + return if blocks.empty? + + str = "" + + cs = UJIT::Disasm.open(UJIT::Disasm::ARCH_X86, UJIT::Disasm::MODE_64) + + str << iseq.disasm + str << "\n" + + blocks.sort_by(&:address).reverse.each do |block| + str << "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=") + str << "\n" + + cs.disasm(block.code, 0).each do |i| + str << sprintf( + "\t0x%
x:\t%s\t%
s\n", + address: i.address, + instruction: i.mnemonic, + details: i.op_str + ) + end + end + str + end +end diff --git a/ujit_iface.c b/ujit_iface.c index fac401a7b2..349aa40b1b 100644 --- a/ujit_iface.c +++ b/ujit_iface.c @@ -13,8 +13,12 @@ #include "ujit_codegen.h" #include "ujit_core.h" #include "ujit_hooks.inc" +#include "ujit.rbinc" +#include VALUE cUjitBlock; +VALUE cUjitDisasm; +VALUE cUjitDisasmInsn; extern st_table * version_tbl; extern codeblock_t *cb; @@ -25,6 +29,12 @@ static const rb_data_type_t ujit_block_type = { 0, 0, RUBY_TYPED_FREE_IMMEDIATELY }; +static const rb_data_type_t ujit_disasm_type = { + "UJIT/Disasm", + {0, (void(*)(void *))cs_close, 0, }, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY +}; + bool rb_ujit_enabled; // Hash table of encoded instructions @@ -366,6 +376,36 @@ iseq_end_index(VALUE self) return INT2NUM(block->end_idx); } +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; +} + void rb_ujit_init(void) { @@ -389,6 +429,14 @@ rb_ujit_init(void) rb_define_method(cUjitBlock, "iseq_start_index", iseq_start_index, 0); rb_define_method(cUjitBlock, "iseq_end_index", iseq_end_index, 0); + 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); + // Initialize the GC hooks method_lookup_dependency = st_init_numtable(); struct ujit_root_struct *root; -- cgit v1.2.3