diff options
| -rw-r--r-- | miasm2/ir/ir2C.py | 62 | ||||
| -rw-r--r-- | miasm2/jitter/Jittcc.c | 10 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore.c | 8 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore.h | 2 | ||||
| -rw-r--r-- | miasm2/jitter/jitcore_tcc.py | 2 |
5 files changed, 40 insertions, 44 deletions
diff --git a/miasm2/ir/ir2C.py b/miasm2/ir/ir2C.py index 136f3e85..30e9fee0 100644 --- a/miasm2/ir/ir2C.py +++ b/miasm2/ir/ir2C.py @@ -209,7 +209,7 @@ pre_instr_test_exception = r""" // pre instruction test exception if (vm_mngr->exception_flags) { %s; - RETURN_PC; + return; } """ @@ -218,14 +218,14 @@ code_exception_fetch_mem_at_instr = r""" // except fetch mem at instr if (vm_mngr->exception_flags & EXCEPT_DO_NOT_UPDATE_PC) { %s; - RETURN_PC; + return; } """ code_exception_fetch_mem_post_instr = r""" // except fetch mem post instr if (vm_mngr->exception_flags) { %s; - RETURN_PC; + return; } """ @@ -234,14 +234,14 @@ code_exception_fetch_mem_at_instr_noautomod = r""" // except fetch mem at instr noauto if ((vm_mngr->exception_flags & ~EXCEPT_CODE_AUTOMOD) & EXCEPT_DO_NOT_UPDATE_PC) { %s; - RETURN_PC; + return; } """ code_exception_fetch_mem_post_instr_noautomod = r""" // except post instr noauto if (vm_mngr->exception_flags & ~EXCEPT_CODE_AUTOMOD) { %s; - RETURN_PC; + return; } """ @@ -250,7 +250,7 @@ code_exception_at_instr = r""" // except at instr if (vmcpu->exception_flags && vmcpu->exception_flags > EXCEPT_NUM_UPDT_EIP) { %s; - RETURN_PC; + return; } """ @@ -263,7 +263,7 @@ if (vmcpu->exception_flags) { else { %s; } - RETURN_PC; + return; } """ @@ -271,7 +271,7 @@ if (vmcpu->exception_flags) { code_exception_at_instr_noautomod = r""" if ((vmcpu->exception_flags & ~EXCEPT_CODE_AUTOMOD) && vmcpu->exception_flags > EXCEPT_NUM_UPDT_EIP) { %s; - RETURN_PC; + return; } """ @@ -283,13 +283,16 @@ if (vmcpu->exception_flags & ~EXCEPT_CODE_AUTOMOD) { else { %s; } - RETURN_PC; + return; } """ goto_local_code = r""" -if (BlockDst.is_local) { - goto *local_labels[BlockDst.address]; +if (BlockDst->is_local) { + goto *local_labels[BlockDst->address]; +} +else { + return; } """ @@ -311,23 +314,23 @@ def set_pc(ir_arch, src): def gen_resolve_int(ir_arch, e): - return 'Resolve_dst(%X, 0)'%(e) + return 'Resolve_dst(BlockDst, %X, 0)'%(e) def gen_resolve_id_lbl(ir_arch, e): if e.name.name.startswith("lbl_gen_"): # TODO XXX CLEAN - return 'Resolve_dst(0x%X, 1)'%(e.name.index) + return 'Resolve_dst(BlockDst, 0x%X, 1)'%(e.name.index) else: - return 'Resolve_dst(0x%X, 0)'%(e.name.offset) + return 'Resolve_dst(BlockDst, 0x%X, 0)'%(e.name.offset) def gen_resolve_id(ir_arch, e): - return 'Resolve_dst(%s, 0)'%(patch_c_id(ir_arch.arch, e).toC()) + return 'Resolve_dst(BlockDst, %s, 0)'%(patch_c_id(ir_arch.arch, e).toC()) def gen_resolve_mem(ir_arch, e): - return 'Resolve_dst(%s, 0)'%(patch_c_id(ir_arch.arch, e).toC()) + return 'Resolve_dst(BlockDst, %s, 0)'%(patch_c_id(ir_arch.arch, e).toC()) def gen_resolve_other(ir_arch, e): - return 'Resolve_dst(%s, 0)'%(patch_c_id(ir_arch.arch, e).toC()) + return 'Resolve_dst(BlockDst, %s, 0)'%(patch_c_id(ir_arch.arch, e).toC()) def gen_resolve_dst_simple(ir_arch, e): if isinstance(e, ExprInt): @@ -347,11 +350,11 @@ def gen_irdst(ir_arch, e): if isinstance(e, ExprCond): dst_cond_c = patch_c_id(ir_arch.arch, e.cond).toC() out.append("if (%s)"%dst_cond_c) - out.append(' BlockDst = %s;'%(gen_resolve_dst_simple(ir_arch, e.src1))) + out.append(' %s;'%(gen_resolve_dst_simple(ir_arch, e.src1))) out.append("else") - out.append(' BlockDst = %s;'%(gen_resolve_dst_simple(ir_arch, e.src2))) + out.append(' %s;'%(gen_resolve_dst_simple(ir_arch, e.src2))) else: - out.append('BlockDst = %s;'%(gen_resolve_dst_simple(ir_arch, e))) + out.append('%s;'%(gen_resolve_dst_simple(ir_arch, e))) return out def Expr2C(ir_arch, l, exprs, gen_exception_code=False): @@ -450,7 +453,7 @@ def Expr2C(ir_arch, l, exprs, gen_exception_code=False): if e.dst == ir_arch.arch.pc[ir_arch.attrib]: pc_is_dst = True - out_pc += ["RETURN_PC;"] + out_pc += ["return;"] # if len(id_to_update) != len(set(id_to_update)): # raise ValueError('Not implemented: multi dst to same id!', str([str(x) @@ -461,12 +464,12 @@ def Expr2C(ir_arch, l, exprs, gen_exception_code=False): if fetch_mem: e = set_pc(ir_arch, l.offset & mask_int) s1 = "%s" % patch_c_id(ir_arch.arch, e).toC() - s1 += ';\n BlockDst = Resolve_dst(0x%X, 0);\n'%(l.offset & mask_int) + s1 += ';\n Resolve_dst(BlockDst, 0x%X, 0)'%(l.offset & mask_int) out.append(code_exception_fetch_mem_at_instr_noautomod % s1) if set_exception_flags: e = set_pc(ir_arch, l.offset & mask_int) s1 = "%s" % patch_c_id(ir_arch.arch, e).toC() - s1 += ';\n BlockDst = Resolve_dst(0x%X, 0);\n'%(l.offset & mask_int) + s1 += ';\n Resolve_dst(BlockDst, 0x%X, 0)'%(l.offset & mask_int) out.append(code_exception_at_instr_noautomod % s1) for i in id_to_update: @@ -481,14 +484,14 @@ def Expr2C(ir_arch, l, exprs, gen_exception_code=False): if set_exception_flags: if pc_is_dst: post_instr.append("if (vm_mngr->exception_flags) { " + - "/*pc = 0x%X; */RETURN_PC; }" % (l.offset)) + "/*pc = 0x%X; */return; }" % (l.offset)) else: e = set_pc(ir_arch, l.offset & mask_int) s1 = "%s" % patch_c_id(ir_arch.arch, e).toC() - s1 += ';\n BlockDst = Resolve_dst(0x%X, 0);\n'%(l.offset & mask_int) + s1 += ';\n Resolve_dst(BlockDst, 0x%X, 0)'%(l.offset & mask_int) e = set_pc(ir_arch, (l.offset + l.l) & mask_int) s2 = "%s" % patch_c_id(ir_arch.arch, e).toC() - s2 += ';\n BlockDst = Resolve_dst(0x%X, 0);\n'%((l.offset + l.l) & mask_int) + s2 += ';\n Resolve_dst(BlockDst, 0x%X, 0)'%((l.offset + l.l) & mask_int) post_instr.append( code_exception_post_instr_noautomod % (s1, s2)) @@ -500,7 +503,7 @@ def Expr2C(ir_arch, l, exprs, gen_exception_code=False): e = set_pc(ir_arch, offset & mask_int) s1 = "%s" % patch_c_id(ir_arch.arch, e).toC() - s1 += ';\n BlockDst = Resolve_dst(0x%X, 0);\n'%(offset & mask_int) + s1 += ';\n Resolve_dst(BlockDst, 0x%X, 0)'%(offset & mask_int) post_instr.append( code_exception_fetch_mem_post_instr_noautomod % (s1)) @@ -540,7 +543,7 @@ def ir2C(ir_arch, irbloc, lbl_done, if l.offset not in lbl_done: e = set_pc(ir_arch, l.offset & mask_int) s1 = "%s" % patch_c_id(ir_arch.arch, e).toC() - s1 += ';\n BlockDst = Resolve_dst(0x%X, 0);\n'%(l.offset & mask_int) + s1 += ';\n Resolve_dst(BlockDst, 0x%X, 0)'%(l.offset & mask_int) out.append([pre_instr_test_exception % (s1)]) lbl_done.add(l.offset) @@ -561,7 +564,7 @@ def ir2C(ir_arch, irbloc, lbl_done, def irblocs2C(ir_arch, resolvers, label, irblocs, gen_exception_code=False, log_mn=False, log_regs=False): out = [] - out.append("block_id BlockDst = {0, 0};") + lbls = [b.label for b in irblocs] lbls_local = [] for l in lbls: @@ -598,7 +601,6 @@ def irblocs2C(ir_arch, resolvers, label, irblocs, out.append(l) dst = irbloc.dst out.append("") - out.append("return BlockDst;") return out diff --git a/miasm2/jitter/Jittcc.c b/miasm2/jitter/Jittcc.c index 8c5b2046..476b2048 100644 --- a/miasm2/jitter/Jittcc.c +++ b/miasm2/jitter/Jittcc.c @@ -53,7 +53,6 @@ TCCState * tcc_init_state(void) for (i=0;i<include_array_count; i++){ tcc_add_include_path(tcc_state, include_array[i]); } - return tcc_state; } @@ -75,7 +74,6 @@ PyObject* tcc_set_emul_lib_path(PyObject* self, PyObject* args) char* lib_arg; char* str1, * str2; - if (!PyArg_ParseTuple(args, "ss", &include_arg, &lib_arg)) @@ -129,17 +127,15 @@ typedef struct { PyObject* tcc_exec_bloc(PyObject* self, PyObject* args) { - //PyObject* (*func)(void*, void*); - block_id (*func)(void*, void*); + void (*func)(block_id*, void*, void*); uint64_t vm; uint64_t cpu; PyObject* ret; - block_id BlockDst; + block_id BlockDst = {0, 0}; if (!PyArg_ParseTuple(args, "KKK", &func, &cpu, &vm)) return NULL; - BlockDst = func((void*)cpu, (void*)vm); - + func(&BlockDst, (void*)cpu, (void*)vm); ret = PyTuple_New(2); if (ret == NULL) { fprintf(stderr, "Erreur alloc!\n"); diff --git a/miasm2/jitter/arch/JitCore.c b/miasm2/jitter/arch/JitCore.c index 739beb74..6d3b0df4 100644 --- a/miasm2/jitter/arch/JitCore.c +++ b/miasm2/jitter/arch/JitCore.c @@ -1,10 +1,8 @@ #include <Python.h> #include "JitCore.h" -block_id Resolve_dst(uint64_t addr, uint64_t is_local) +void Resolve_dst(block_id* b, uint64_t addr, uint64_t is_local) { - block_id b; - b.address = addr; - b.is_local = is_local; - return b; + b->address = addr; + b->is_local = is_local; } diff --git a/miasm2/jitter/arch/JitCore.h b/miasm2/jitter/arch/JitCore.h index 723a10cc..735cbd27 100644 --- a/miasm2/jitter/arch/JitCore.h +++ b/miasm2/jitter/arch/JitCore.h @@ -70,4 +70,4 @@ typedef struct { uint64_t address; } block_id; -block_id Resolve_dst(uint64_t addr, uint64_t is_local); +void Resolve_dst(block_id* BlockDst, uint64_t addr, uint64_t is_local); diff --git a/miasm2/jitter/jitcore_tcc.py b/miasm2/jitter/jitcore_tcc.py index a546253b..dd49f635 100644 --- a/miasm2/jitter/jitcore_tcc.py +++ b/miasm2/jitter/jitcore_tcc.py @@ -130,7 +130,7 @@ class JitCore_Tcc(jitcore.JitCore): def jitirblocs(self, label, irblocs): f_name = "bloc_%s" % label.name - f_declaration = 'block_id %s(vm_cpu_t* vmcpu, vm_mngr_t* vm_mngr)' % f_name + f_declaration = 'void %s(block_id * BlockDst, vm_cpu_t* vmcpu, vm_mngr_t* vm_mngr)' % f_name out = irblocs2C(self.ir_arch, self.resolver, label, irblocs, gen_exception_code=True, log_mn=self.log_mn, |