miasm
Reverse engineering framework
JitCore.h
Go to the documentation of this file.
1 #ifndef JITCORE_H
2 #define JITCORE_H
3 
4 #if _WIN32
5 #define _MIASM_EXPORT __declspec(dllexport)
6 
7 #ifndef SSIZE_MAX
8 #ifdef _WIN64
9 #define SSIZE_MAX _I64_MAX
10 #else
11 #define SSIZE_MAX INT_MAX
12 #endif
13 #endif
14 
15 #else
16 #define _MIASM_EXPORT
17 #endif
18 
19 #define RAISE(errtype, msg) {PyObject* p; p = PyErr_Format( errtype, msg ); return p;}
20 #define RAISE_ret0(errtype, msg) {PyObject* p; p = PyErr_Format( errtype, msg ); return 0;}
21 
22 
23 #if PY_MAJOR_VERSION >= 3
24 #define getset_reg_bn(regname, size) \
25  static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
26  { \
27  bn_t bn; \
28  PyObject* py_long; \
29  bn = (self->cpu)->regname; \
30  bn = bignum_mask(bn, (size)); \
31  py_long = bn_to_PyLong(bn); \
32  return py_long; \
33  } \
34  \
35  static PyObject *JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
36  { \
37  bn_t bn; \
38  PyObject* py_long = value; \
39  if (PyLong_Check(py_long)){ \
40  Py_INCREF(py_long); \
41  } else { \
42  RAISE(PyExc_TypeError,"arg must be int"); \
43  } \
44  \
45  bn = PyLong_to_bn(py_long); \
46  \
47  (self->cpu)->regname = bignum_mask(bn, (size)); \
48  return 0; \
49  }
50 
51 
52 #else
53 #define getset_reg_bn(regname, size) \
54  static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
55  { \
56  bn_t bn; \
57  PyObject* py_long; \
58  bn = (self->cpu)->regname; \
59  bn = bignum_mask(bn, (size)); \
60  py_long = bn_to_PyLong(bn); \
61  return py_long; \
62  } \
63  \
64  static PyObject *JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
65  { \
66  bn_t bn; \
67  PyObject* py_long = value; \
68  uint64_t tmp; \
69  \
70  if (PyInt_Check(py_long)){ \
71  tmp = (uint64_t)PyInt_AsLong(py_long); \
72  py_long = PyLong_FromLong((long)tmp); \
73  } else if (PyLong_Check(py_long)){ \
74  Py_INCREF(py_long); \
75  } \
76  else{ \
77  RAISE(PyExc_TypeError,"arg must be int"); \
78  } \
79  \
80  bn = PyLong_to_bn(py_long); \
81  \
82  self->cpu->regname = bignum_mask(bn, (size)); \
83  return 0; \
84  }
85 #endif
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 #define getset_reg_u64(regname) \
98  static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
99  { \
100  return PyLong_FromUnsignedLongLong(self->cpu->regname); \
101  } \
102  static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
103  { \
104  uint64_t val; \
105  PyGetInt_uint64_t_retneg(value, val); \
106  self->cpu->regname = val; \
107  return 0; \
108  }
109 
110 #define getset_reg_u32(regname) \
111  static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
112  { \
113  return PyLong_FromUnsignedLongLong(self->cpu->regname); \
114  } \
115  static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
116  { \
117  uint32_t val; \
118  PyGetInt_uint32_t_retneg(value, val); \
119  self->cpu->regname = val; \
120  return 0; \
121  }
122 
123 
124 #define getset_reg_u16(regname) \
125  static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
126  { \
127  return PyLong_FromUnsignedLongLong(self->cpu->regname); \
128  } \
129  static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
130  { \
131  uint16_t val; \
132  PyGetInt_uint16_t_retneg(value, val); \
133  self->cpu->regname = val; \
134  return 0; \
135  }
136 
137 
138 #define getset_reg_u8(regname) \
139  static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \
140  { \
141  return PyLong_FromUnsignedLongLong(self->cpu->regname); \
142  } \
143  static int JitCpu_set_ ## regname (JitCpu *self, PyObject *value, void *closure) \
144  { \
145  uint8_t val; \
146  PyGetInt_uint8_t_retneg(value, val); \
147  self->cpu->regname = val; \
148  return 0; \
149  }
150 
151 
152 #define get_reg(reg) do { \
153  o = PyLong_FromUnsignedLongLong((uint64_t)self->cpu->reg); \
154  PyDict_SetItemString(dict, #reg, o); \
155  Py_DECREF(o); \
156  } while(0);
157 
158 
159 #define get_reg_bn(reg, size) do { \
160  bn_t bn; \
161  PyObject* py_long; \
162  bn = self->cpu->reg; \
163  bn = bignum_mask(bn, size); \
164  py_long = bn_to_PyLong(bn); \
165  PyDict_SetItemString(dict, #reg, py_long); \
166  Py_DECREF(py_long); \
167  } while(0);
168 
169 
170 #define get_reg_off(reg) do { \
171  o = PyLong_FromUnsignedLongLong((uint64_t)offsetof(struct vm_cpu, reg)); \
172  PyDict_SetItemString(dict, #reg, o); \
173  Py_DECREF(o); \
174  } while(0);
175 
176 
177 
178 
179 typedef struct {
180  uint8_t is_local;
181  uint64_t address;
182 } block_id;
183 
184 struct vm_cpu;
185 
186 typedef struct {
187  PyObject_HEAD
189  PyObject *jitter;
190  struct vm_cpu *cpu;
191 } JitCpu;
192 
193 
194 typedef struct _reg_dict{
195  char* name;
196  size_t offset;
197  size_t size;
199 
200 
201 
202 void JitCpu_dealloc(JitCpu* self);
203 PyObject * JitCpu_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
204 PyObject * JitCpu_get_vmmngr(JitCpu *self, void *closure);
205 PyObject * JitCpu_set_vmmngr(JitCpu *self, PyObject *value, void *closure);
206 PyObject * JitCpu_get_vmcpu(JitCpu *self, void *closure);
207 PyObject * JitCpu_set_vmcpu(JitCpu *self, PyObject *value, void *closure);
208 PyObject * JitCpu_get_jitter(JitCpu *self, void *closure);
209 PyObject * JitCpu_set_jitter(JitCpu *self, PyObject *value, void *closure);
210 void Resolve_dst(block_id* BlockDst, uint64_t addr, uint64_t is_local);
211 
212 #define Resolve_dst(b, arg_addr, arg_is_local) do {(b)->address = (arg_addr); (b)->is_local = (arg_is_local);} while(0)
213 
214 
215 
216 _MIASM_EXPORT uint8_t MEM_LOOKUP_08(JitCpu* jitcpu, uint64_t addr);
217 _MIASM_EXPORT uint16_t MEM_LOOKUP_16(JitCpu* jitcpu, uint64_t addr);
218 _MIASM_EXPORT uint32_t MEM_LOOKUP_32(JitCpu* jitcpu, uint64_t addr);
219 _MIASM_EXPORT uint64_t MEM_LOOKUP_64(JitCpu* jitcpu, uint64_t addr);
220 
222 _MIASM_EXPORT bn_t MEM_LOOKUP_INT_BN(JitCpu* jitcpu, int size, uint64_t addr);
223 
224 _MIASM_EXPORT uint64_t MEM_LOOKUP_BN_INT(JitCpu* jitcpu, int size, bn_t addr);
225 
226 _MIASM_EXPORT void MEM_WRITE_BN_BN(JitCpu* jitcpu, int size, bn_t addr, bn_t src);
227 _MIASM_EXPORT void MEM_WRITE_BN_INT(JitCpu* jitcpu, int size, bn_t addr, uint64_t src);
228 _MIASM_EXPORT void MEM_WRITE_INT_BN(JitCpu* jitcpu, int size, uint64_t addr, bn_t src);
229 
230 
231 _MIASM_EXPORT void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr);
232 _MIASM_EXPORT void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr);
233 
234 
235 
236 #define VM_exception_flag (jitcpu->pyvm->vm_mngr.exception_flags)
237 #define CPU_exception_flag (((struct vm_cpu*)jitcpu->cpu)->exception_flags)
238 #define CPU_exception_flag_at_instr ((CPU_exception_flag) && ((CPU_exception_flag) > EXCEPT_NUM_UPDT_EIP))
239 #define JIT_RET_EXCEPTION 1
240 #define JIT_RET_NO_EXCEPTION 0
241 
242 #endif
MEM_LOOKUP_BN_BN
_MIASM_EXPORT bn_t MEM_LOOKUP_BN_BN(JitCpu *jitcpu, int size, bn_t addr)
Definition: JitCore.c:99
MEM_LOOKUP_08
uint8_t MEM_LOOKUP_08(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:79
MEM_LOOKUP_32
uint32_t MEM_LOOKUP_32(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:89
bn.h
MEM_LOOKUP_INT_BN_TO_PTR
_MIASM_EXPORT void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu *jitcpu, int size, uint64_t addr, char *ptr)
Definition: JitCore.c:166
bignum_rshift
bn_t bignum_rshift(bn_t a, int nbits)
Definition: bn.c:416
_reg_dict
Definition: JitCore.h:194
vm_mngr.h
MEM_LOOKUP_08
_MIASM_EXPORT uint8_t MEM_LOOKUP_08(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:79
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.mem_read
def mem_read(self, expr_mem)
Definition: emulatedsymbexec.py:70
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.update_cpu_from_engine
def update_cpu_from_engine(self)
Definition: emulatedsymbexec.py:114
bn
Definition: bn.h:97
JitCpu_new
PyObject * JitCpu_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Definition: JitCore.c:20
MEM_WRITE_INT_BN
void MEM_WRITE_INT_BN(JitCpu *jitcpu, int size, uint64_t addr, bn_t src)
Definition: JitCore.c:221
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.reset_regs
def reset_regs(self)
Definition: emulatedsymbexec.py:64
vm_MEM_WRITE_32
void vm_MEM_WRITE_32(vm_mngr_t *vm_mngr, uint64_t addr, unsigned int src)
Definition: vm_mngr.c:473
MEM_WRITE_BN_INT
void MEM_WRITE_BN_INT(JitCpu *jitcpu, int size, bn_t addr, uint64_t src)
Definition: JitCore.c:196
miasm.expression.expression
Definition: expression.py:1
Resolve_dst
#define Resolve_dst(b, arg_addr, arg_is_local)
Definition: JitCore.h:212
miasm.ir.symbexec.SymbolicExecutionEngine
Definition: symbexec.py:757
bignum_or
bn_t bignum_or(bn_t a, bn_t b)
Definition: bn.c:531
miasm.jitter.emulatedsymbexec.EmulatedSymbExec._simp_handle_x86_cpuid
def _simp_handle_x86_cpuid(self, e_s, expr)
Definition: emulatedsymbexec.py:152
JitCpu_get_vmmngr
PyObject * JitCpu_get_vmmngr(JitCpu *self, void *closure)
Definition: JitCore.c:28
MEM_LOOKUP_BN_INT
uint64_t MEM_LOOKUP_BN_INT(JitCpu *jitcpu, int size, bn_t addr)
Definition: JitCore.c:119
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.__init__
def __init__(self, cpu, vm, *args, **kwargs)
Definition: emulatedsymbexec.py:55
modint.i
i
Definition: modint.py:70
miasm.core.utils
Definition: utils.py:1
JitCpu_get_jitter
PyObject * JitCpu_get_jitter(JitCpu *self, void *closure)
Definition: JitCore.c:63
VmMngr::vm_mngr
vm_mngr_t vm_mngr
Definition: vm_mngr_py.h:11
MEM_WRITE_BN_BN
void MEM_WRITE_BN_BN(JitCpu *jitcpu, int size, bn_t addr, bn_t src)
Definition: JitCore.c:180
MEM_LOOKUP_BN_BN
bn_t MEM_LOOKUP_BN_BN(JitCpu *jitcpu, int size, bn_t addr)
Definition: JitCore.c:99
MEM_LOOKUP_BN_INT
_MIASM_EXPORT uint64_t MEM_LOOKUP_BN_INT(JitCpu *jitcpu, int size, bn_t addr)
Definition: JitCore.c:119
miasm.ir.symbexec.SymbolicExecutionEngine.expr_simp
expr_simp
Definition: symbexec.py:828
vm_mngr_py.h
vm_MEM_WRITE_64
void vm_MEM_WRITE_64(vm_mngr_t *vm_mngr, uint64_t addr, uint64_t src)
Definition: vm_mngr.c:478
JitCpu_set_vmmngr
PyObject * JitCpu_set_vmmngr(JitCpu *self, PyObject *value, void *closure)
Definition: JitCore.c:38
miasm.arch.aarch64.sem.ret
def ret(arg1)
Definition: sem.py:1796
vm_MEM_LOOKUP_08
unsigned char vm_MEM_LOOKUP_08(vm_mngr_t *vm_mngr, uint64_t addr)
Definition: vm_mngr.c:484
add_mem_read
void add_mem_read(vm_mngr_t *vm_mngr, uint64_t addr, uint64_t size)
Definition: vm_mngr.c:365
export_llvm.value
value
Definition: export_llvm.py:69
_reg_dict::name
char * name
Definition: JitCore.h:195
MEM_WRITE_INT_BN_FROM_PTR
_MIASM_EXPORT void MEM_WRITE_INT_BN_FROM_PTR(JitCpu *jitcpu, int size, uint64_t addr, char *ptr)
Definition: JitCore.c:235
JitCpu_set_jitter
PyObject * JitCpu_set_jitter(JitCpu *self, PyObject *value, void *closure)
Definition: JitCore.c:73
vm_MEM_LOOKUP_16
unsigned short vm_MEM_LOOKUP_16(vm_mngr_t *vm_mngr, uint64_t addr)
Definition: vm_mngr.c:491
compat_py23.h
vm_MEM_LOOKUP_64
uint64_t vm_MEM_LOOKUP_64(vm_mngr_t *vm_mngr, uint64_t addr)
Definition: vm_mngr.c:505
JitCpu_dealloc
void JitCpu_dealloc(JitCpu *self)
Definition: JitCore.c:14
full.type
type
Definition: full.py:35
JitCpu_get_vmcpu
PyObject * JitCpu_get_vmcpu(JitCpu *self, void *closure)
Definition: JitCore.c:46
miasm.arch.x86.arch
Definition: arch.py:1
VmMngr
Definition: vm_mngr_py.h:8
vm_MEM_WRITE_08
void vm_MEM_WRITE_08(vm_mngr_t *vm_mngr, uint64_t addr, unsigned char src)
Definition: vm_mngr.c:462
miasm.jitter.emulatedsymbexec.EmulatedSymbExec
Definition: emulatedsymbexec.py:7
MEM_LOOKUP_32
_MIASM_EXPORT uint32_t MEM_LOOKUP_32(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:89
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.cpu
cpu
Definition: emulatedsymbexec.py:61
block_id::is_local
uint8_t is_local
Definition: JitCore.h:180
JitCpu::jitter
PyObject * jitter
Definition: JitCore.h:189
JitCpu_get_jitter
PyObject * JitCpu_get_jitter(JitCpu *self, void *closure)
Definition: JitCore.c:63
MEM_WRITE_INT_BN_FROM_PTR
void MEM_WRITE_INT_BN_FROM_PTR(JitCpu *jitcpu, int size, uint64_t addr, char *ptr)
Definition: JitCore.c:235
bignum_from_int
bn_t bignum_from_int(DTYPE_TMP i)
Definition: bn.c:54
expr_c.ptr
ptr
Definition: expr_c.py:44
queue.h
MEM_LOOKUP_64
uint64_t MEM_LOOKUP_64(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:94
vm_MEM_WRITE_16
void vm_MEM_WRITE_16(vm_mngr_t *vm_mngr, uint64_t addr, unsigned short src)
Definition: vm_mngr.c:468
dse_crackme.stderr
stderr
Definition: dse_crackme.py:320
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.update_engine_from_cpu
def update_engine_from_cpu(self)
Definition: emulatedsymbexec.py:129
JitCpu_set_vmmngr
PyObject * JitCpu_set_vmmngr(JitCpu *self, PyObject *value, void *closure)
Definition: JitCore.c:38
miasm.core.utils.encode_hex
def encode_hex(value)
Definition: utils.py:133
MEM_WRITE_BN_BN
_MIASM_EXPORT void MEM_WRITE_BN_BN(JitCpu *jitcpu, int size, bn_t addr, bn_t src)
Definition: JitCore.c:180
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.x86_cpuid
dictionary x86_cpuid
Definition: emulatedsymbexec.py:10
bignum_lshift
bn_t bignum_lshift(bn_t a, int nbits)
Definition: bn.c:387
shellcode.args
args
Definition: shellcode.py:26
vm_MEM_LOOKUP_32
unsigned int vm_MEM_LOOKUP_32(vm_mngr_t *vm_mngr, uint64_t addr)
Definition: vm_mngr.c:498
_reg_dict::offset
size_t offset
Definition: JitCore.h:196
JitCpu::pyvm
PyObject_HEAD VmMngr * pyvm
Definition: JitCore.h:188
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.enable_emulated_simplifications
def enable_emulated_simplifications(self)
Definition: emulatedsymbexec.py:164
block_id::address
uint64_t address
Definition: JitCore.h:181
miasm.ir.symbexec.SymbolicExecutionEngine.ir_arch
ir_arch
Definition: symbexec.py:827
JitCore.h
miasm.core.utils.decode_hex
def decode_hex(value)
Definition: utils.py:130
_reg_dict::size
size_t size
Definition: JitCore.h:197
JitCpu_dealloc
void JitCpu_dealloc(JitCpu *self)
Definition: JitCore.c:14
bignum_to_uint64
uint64_t bignum_to_uint64(bn_t n)
Definition: bn.c:134
MEM_WRITE_INT_BN
_MIASM_EXPORT void MEM_WRITE_INT_BN(JitCpu *jitcpu, int size, uint64_t addr, bn_t src)
Definition: JitCore.c:221
MEM_LOOKUP_16
uint16_t MEM_LOOKUP_16(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:84
MEM_LOOKUP_INT_BN
bn_t MEM_LOOKUP_INT_BN(JitCpu *jitcpu, int size, uint64_t addr)
Definition: JitCore.c:150
_MIASM_EXPORT
#define _MIASM_EXPORT
Definition: JitCore.h:16
JitCpu_set_vmcpu
PyObject * JitCpu_set_vmcpu(JitCpu *self, PyObject *value, void *closure)
Definition: JitCore.c:55
MEM_LOOKUP_INT_BN_TO_PTR
void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu *jitcpu, int size, uint64_t addr, char *ptr)
Definition: JitCore.c:166
dis_binary.addr
addr
Definition: dis_binary.py:24
JitCpu_new
PyObject * JitCpu_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Definition: JitCore.c:20
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.mem_write
def mem_write(self, dest, data)
Definition: emulatedsymbexec.py:89
JitCpu_set_vmcpu
PyObject * JitCpu_set_vmcpu(JitCpu *self, PyObject *value, void *closure)
Definition: JitCore.c:55
JitCpu_get_vmmngr
PyObject * JitCpu_get_vmmngr(JitCpu *self, void *closure)
Definition: JitCore.c:28
miasm.jitter.emulatedsymbexec.EmulatedSymbExec.vm
vm
Definition: emulatedsymbexec.py:62
miasm.arch.x86.arch.is_op_segm
def is_op_segm(expr)
Definition: arch.py:190
example_types.val
val
Definition: example_types.py:218
miasm.ir.symbexec.SymbolicExecutionEngine.symbols
symbols
Definition: symbexec.py:822
JitCpu_set_jitter
PyObject * JitCpu_set_jitter(JitCpu *self, PyObject *value, void *closure)
Definition: JitCore.c:73
miasm.ir.symbexec
Definition: symbexec.py:1
reg_dict
struct _reg_dict reg_dict
JitCpu::cpu
struct vm_cpu * cpu
Definition: JitCore.h:190
block_id
Definition: JitCore.h:179
shellcode.size
size
Definition: shellcode.py:32
JitCpu
Definition: JitCore.h:186
vm_cpu
Definition: JitCore_aarch64.h:2
MEM_LOOKUP_INT_BN
_MIASM_EXPORT bn_t MEM_LOOKUP_INT_BN(JitCpu *jitcpu, int size, uint64_t addr)
Definition: JitCore.c:150
JitCpu_get_vmcpu
PyObject * JitCpu_get_vmcpu(JitCpu *self, void *closure)
Definition: JitCore.c:46
MEM_LOOKUP_64
_MIASM_EXPORT uint64_t MEM_LOOKUP_64(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:94
MEM_WRITE_BN_INT
_MIASM_EXPORT void MEM_WRITE_BN_INT(JitCpu *jitcpu, int size, bn_t addr, uint64_t src)
Definition: JitCore.c:196
MEM_LOOKUP_16
_MIASM_EXPORT uint16_t MEM_LOOKUP_16(JitCpu *jitcpu, uint64_t addr)
Definition: JitCore.c:84
miasm.jitter.emulatedsymbexec.EmulatedSymbExec._simp_handle_segm
def _simp_handle_segm(self, e_s, expr)
Definition: emulatedsymbexec.py:142