diff options
Diffstat (limited to 'miasm2/jitter')
| -rw-r--r-- | miasm2/jitter/codegen.py | 20 | ||||
| -rw-r--r-- | miasm2/jitter/llvmconvert.py | 43 | ||||
| -rw-r--r-- | miasm2/jitter/vm_mngr.h | 80 |
3 files changed, 56 insertions, 87 deletions
diff --git a/miasm2/jitter/codegen.py b/miasm2/jitter/codegen.py index b2398dd2..15f172ee 100644 --- a/miasm2/jitter/codegen.py +++ b/miasm2/jitter/codegen.py @@ -25,7 +25,6 @@ class Attributes(object): self.mem_read = False self.mem_write = False self.set_exception = False - self.op_set_exception = False self.log_mn = log_mn self.log_regs = log_regs self.instr = None @@ -36,8 +35,6 @@ class CGen(object): Helper to generate C code for a given AsmBlock """ - IMPLICIT_EXCEPTION_OP = set(['umod', 'udiv']) - """ Translate native assembly block to C """ @@ -325,7 +322,7 @@ class CGen(object): out = [] if attrib.mem_read | attrib.mem_write: out += (self.CODE_VM_EXCEPTION_POST_INSTR % (self.C_PC)).split('\n') - if attrib.set_exception or attrib.op_set_exception: + if attrib.set_exception: out += (self.CODE_CPU_EXCEPTION_POST_INSTR % (self.C_PC)).split('\n') if attrib.mem_read | attrib.mem_write: @@ -437,10 +434,6 @@ class CGen(object): if c_prefetch: out += self.gen_check_memory_exception(attrib.instr.offset) - # Check if operator raised exception flags - if attrib.op_set_exception: - out += self.gen_check_cpu_exception(attrib.instr.offset) - out.append("// Mem updt") out += c_mem @@ -462,12 +455,6 @@ class CGen(object): return out - def is_exception_operator(self, operator): - """Return True if the @op operator can raise a runtime exception""" - - return any(operator.startswith(except_op) - for except_op in self.IMPLICIT_EXCEPTION_OP) - def get_caracteristics(self, assignblk, attrib): """ Set the carateristics in @attrib according to the @assignblk @@ -479,10 +466,6 @@ class CGen(object): attrib.set_exception = self.ir_arch.arch.regs.exception_flags in assignblk element_read = assignblk.get_r(mem_read=True) - # Check implicit exception raising - attrib.op_set_exception = any(self.is_exception_operator(operator) - for elem in assignblk.values() - for operator in m2_expr.get_expr_ops(elem)) # Check mem read attrib.mem_read = any(isinstance(expr, m2_expr.ExprMem) for expr in element_read) @@ -513,7 +496,6 @@ class CGen(object): attrib.instr = instr instr_attrib.mem_read |= attrib.mem_read instr_attrib.mem_write |= attrib.mem_write - instr_attrib.op_set_exception |= attrib.op_set_exception instr_attrib.set_exception |= attrib.set_exception return instr_attrib, irblocks_attributes diff --git a/miasm2/jitter/llvmconvert.py b/miasm2/jitter/llvmconvert.py index e7a1d6bc..ae018c18 100644 --- a/miasm2/jitter/llvmconvert.py +++ b/miasm2/jitter/llvmconvert.py @@ -242,23 +242,6 @@ class LLVMContext_JIT(LLVMContext): "args": [LLVMType.IntType(k), LLVMType.IntType(k)]}}) - for k in [16, 32, 64]: - self.add_fc({"imod%s" % k: {"ret": LLVMType.IntType(k), - "args": [p8, - LLVMType.IntType(k), - LLVMType.IntType(k)]}}) - self.add_fc({"idiv%s" % k: {"ret": LLVMType.IntType(k), - "args": [p8, - LLVMType.IntType(k), - LLVMType.IntType(k)]}}) - self.add_fc({"umod%s" % k: {"ret": LLVMType.IntType(k), - "args": [p8, - LLVMType.IntType(k), - LLVMType.IntType(k)]}}) - self.add_fc({"udiv%s" % k: {"ret": LLVMType.IntType(k), - "args": [p8, - LLVMType.IntType(k), - LLVMType.IntType(k)]}}) def add_log_functions(self): "Add functions for state logging" @@ -745,11 +728,21 @@ class LLVMFunction(): return ret if op in ["imod", "idiv", "umod", "udiv"]: - fc_ptr = self.mod.get_global( - "%s%s" % (op, expr.args[0].size)) - args_casted = [self.add_ir(arg) for arg in expr.args] - args = [self.local_vars["vmcpu"]] + args_casted - ret = builder.call(fc_ptr, args) + assert len(expr.args) == 2 + + arg_b = self.add_ir(expr.args[1]) + arg_a = self.add_ir(expr.args[0]) + + if op == "imod": + callback = builder.srem + elif op == "idiv": + callback = builder.sdiv + elif op == "umod": + callback = builder.urem + elif op == "udiv": + callback = builder.udiv + + ret = callback(arg_a, arg_b) self.update_cache(expr, ret) return ret @@ -1020,8 +1013,6 @@ class LLVMFunction(): fc_ptr = self.mod.get_global("check_invalid_code_blocs") self.builder.call(fc_ptr, [self.local_vars["vmmngr"]]) self.check_memory_exception(next_instr, restricted_exception=False) - if attrib.set_exception or attrib.op_set_exception: - self.check_cpu_exception(next_instr, restricted_exception=False) if attrib.mem_read | attrib.mem_write: fc_ptr = self.mod.get_global("reset_memory_access") @@ -1146,10 +1137,6 @@ class LLVMFunction(): self.check_memory_exception(instr.offset, restricted_exception=True) - # Check operation exception - if attributes[index].op_set_exception: - self.check_cpu_exception(instr.offset, restricted_exception=True) - # Update the memory for dst, src in values.iteritems(): if isinstance(dst, m2_expr.ExprMem): diff --git a/miasm2/jitter/vm_mngr.h b/miasm2/jitter/vm_mngr.h index 912b105e..74ad49ad 100644 --- a/miasm2/jitter/vm_mngr.h +++ b/miasm2/jitter/vm_mngr.h @@ -224,55 +224,55 @@ unsigned int rcr_rez_op(unsigned int size, unsigned int a, unsigned int b, unsig #define UDIV(sizeA) \ - uint ## sizeA ## _t udiv ## sizeA (vm_cpu_t* vmcpu, uint ## sizeA ## _t a, uint ## sizeA ## _t b) \ - { \ - uint ## sizeA ## _t r; \ - if (b == 0) { \ - vmcpu->exception_flags |= EXCEPT_INT_DIV_BY_ZERO; \ - return 0; \ - } \ - r = a/b; \ - return r; \ - } + uint ## sizeA ## _t udiv ## sizeA (vm_cpu_t* vmcpu, uint ## sizeA ## _t a, uint ## sizeA ## _t b) \ + { \ + uint ## sizeA ## _t r; \ + if (b == 0) { \ + fprintf(stderr, "Should not happen\n"); \ + exit(0); \ + } \ + r = a/b; \ + return r; \ + } #define UMOD(sizeA) \ - uint ## sizeA ## _t umod ## sizeA (vm_cpu_t* vmcpu, uint ## sizeA ## _t a, uint ## sizeA ## _t b) \ - { \ - uint ## sizeA ## _t r; \ - if (b == 0) { \ - vmcpu->exception_flags |= EXCEPT_INT_DIV_BY_ZERO; \ - return 0; \ - } \ - r = a%b; \ - return r; \ - } + uint ## sizeA ## _t umod ## sizeA (vm_cpu_t* vmcpu, uint ## sizeA ## _t a, uint ## sizeA ## _t b) \ + { \ + uint ## sizeA ## _t r; \ + if (b == 0) { \ + fprintf(stderr, "Should not happen\n"); \ + exit(0); \ + } \ + r = a%b; \ + return r; \ + } #define IDIV(sizeA) \ - int ## sizeA ## _t idiv ## sizeA (vm_cpu_t* vmcpu, int ## sizeA ## _t a, int ## sizeA ## _t b) \ - { \ - int ## sizeA ## _t r; \ - if (b == 0) { \ - vmcpu->exception_flags |= EXCEPT_INT_DIV_BY_ZERO; \ - return 0; \ - } \ - r = a/b; \ - return r; \ - } + int ## sizeA ## _t idiv ## sizeA (vm_cpu_t* vmcpu, int ## sizeA ## _t a, int ## sizeA ## _t b) \ + { \ + int ## sizeA ## _t r; \ + if (b == 0) { \ + fprintf(stderr, "Should not happen\n"); \ + exit(0); \ + } \ + r = a/b; \ + return r; \ + } #define IMOD(sizeA) \ - int ## sizeA ## _t imod ## sizeA (vm_cpu_t* vmcpu, int ## sizeA ## _t a, int ## sizeA ## _t b) \ - { \ - int ## sizeA ## _t r; \ - if (b == 0) { \ - vmcpu->exception_flags |= EXCEPT_INT_DIV_BY_ZERO; \ - return 0; \ - } \ - r = a%b; \ - return r; \ - } + int ## sizeA ## _t imod ## sizeA (vm_cpu_t* vmcpu, int ## sizeA ## _t a, int ## sizeA ## _t b) \ + { \ + int ## sizeA ## _t r; \ + if (b == 0) { \ + fprintf(stderr, "Should not happen\n"); \ + exit(0); \ + } \ + r = a%b; \ + return r; \ + } void memory_access_list_init(struct memory_access_list * access); |