1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/**********************************************************************
mjit_c.c - C helpers for MJIT
Copyright (C) 2017 Takashi Kokubun <[email protected]>.
**********************************************************************/
#include "ruby/internal/config.h" // defines USE_MJIT
#if USE_MJIT
#include "mjit.h"
#include "mjit_c.h"
#include "internal.h"
#include "internal/compile.h"
#include "internal/hash.h"
#include "yjit.h"
#include "vm_insnhelper.h"
#include "insns.inc"
#include "insns_info.inc"
#include "mjit_sp_inc.inc"
#if SIZEOF_LONG == SIZEOF_VOIDP
#define NUM2PTR(x) NUM2ULONG(x)
#define PTR2NUM(x) ULONG2NUM(x)
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
#define NUM2PTR(x) NUM2ULL(x)
#define PTR2NUM(x) ULL2NUM(x)
#endif
// An offsetof implementation that works for unnamed struct and union.
// Multiplying 8 for compatibility with libclang's offsetof.
#define OFFSETOF(ptr, member) RB_SIZE2NUM(((char *)&ptr.member - (char*)&ptr) * 8)
#define SIZEOF(type) RB_SIZE2NUM(sizeof(type))
#define SIGNED_TYPE_P(type) RBOOL((type)(-1) < (type)(1))
// macOS: brew install capstone
// Ubuntu/Debian: apt-get install libcapstone-dev
// Fedora: dnf -y install capstone-devel
//#ifdef HAVE_LIBCAPSTONE
#if 1
#include <capstone/capstone.h>
#define CODE "\x55\x48\x8b\x05\xb8\x13\x00\x00"
// Return an array of [address, mnemonic, op_str]
static VALUE
dump_disasm(rb_execution_context_t *ec, VALUE self, VALUE from, VALUE to)
{
// Prepare for calling cs_disasm
static csh handle;
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) {
rb_raise(rb_eRuntimeError, "failed to make Capstone handle");
}
size_t from_addr = NUM2SIZET(from);
size_t to_addr = NUM2SIZET(to);
// Call cs_disasm and convert results to a Ruby array
cs_insn *insns;
size_t count = cs_disasm(handle, (const uint8_t *)from_addr, to_addr - from_addr, from_addr, 0, &insns);
VALUE result = 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(result, vals);
}
// Free memory used by capstone
cs_free(insns, count);
cs_close(&handle);
return result;
}
#else
static VALUE
mjit_disasm(VALUE self, VALUE from, VALUE to)
{
return rb_ary_new();
}
#endif
#include "mjit_c.rbinc"
#endif // USE_MJIT
|