diff options
Diffstat (limited to 'miasm2')
| -rw-r--r-- | miasm2/core/asmblock.py | 39 | ||||
| -rw-r--r-- | miasm2/jitter/jitcore.py | 42 |
2 files changed, 54 insertions, 27 deletions
diff --git a/miasm2/core/asmblock.py b/miasm2/core/asmblock.py index 8740aeb7..f763d85f 100644 --- a/miasm2/core/asmblock.py +++ b/miasm2/core/asmblock.py @@ -297,13 +297,23 @@ class AsmBlockBad(AsmBlock): """Stand for a *bad* ASM block (malformed, unreachable, not disassembled, ...)""" - ERROR_TYPES = {-1: "Unknown error", - 0: "Unable to disassemble", - 1: "Null starting block", - 2: "Address forbidden by dont_dis", - } - def __init__(self, label=None, alignment=1, errno=-1, *args, **kwargs): + ERROR_UNKNOWN = -1 + ERROR_CANNOT_DISASM = 0 + ERROR_NULL_STARTING_BLOCK = 1 + ERROR_FORBIDDEN = 2 + ERROR_IO = 3 + + + ERROR_TYPES = { + ERROR_UNKNOWN: "Unknown error", + ERROR_CANNOT_DISASM: "Unable to disassemble", + ERROR_NULL_STARTING_BLOCK: "Null starting block", + ERROR_FORBIDDEN: "Address forbidden by dont_dis", + ERROR_IO: "IOError", + } + + def __init__(self, label=None, alignment=1, errno=ERROR_UNKNOWN, *args, **kwargs): """Instanciate an AsmBlock_bad. @label, @alignement: same as AsmBlock.__init__ @errno: (optional) specify a error type associated with the block @@ -311,6 +321,8 @@ class AsmBlockBad(AsmBlock): super(AsmBlockBad, self).__init__(label, alignment, *args, **kwargs) self._errno = errno + errno = property(lambda self: self._errno) + def __str__(self): error_txt = self.ERROR_TYPES.get(self._errno, self._errno) return "\n".join([str(self.label), @@ -1450,7 +1462,7 @@ class disasmEngine(object): if not cur_block.lines: job_done.add(offset) # Block is empty -> bad block - cur_block = AsmBlockBad(label, errno=2) + cur_block = AsmBlockBad(label, errno=AsmBlockBad.ERROR_FORBIDDEN) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block @@ -1475,18 +1487,25 @@ class disasmEngine(object): break off_i = offset + error = None try: instr = self.arch.dis(self.bin_stream, self.attrib, offset) - except (Disasm_Exception, IOError), e: + except Disasm_Exception as e: + log_asmblock.warning(e) + instr = None + error = AsmBlockBad.ERROR_CANNOT_DISASM + except IOError as e: log_asmblock.warning(e) instr = None + error = AsmBlockBad.ERROR_IO + if instr is None: log_asmblock.warning("cannot disasm at %X", int(off_i)) if not cur_block.lines: job_done.add(offset) # Block is empty -> bad block - cur_block = AsmBlockBad(label, errno=0) + cur_block = AsmBlockBad(label, errno=error) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block @@ -1499,7 +1518,7 @@ class disasmEngine(object): log_asmblock.warning("reach nul instr at %X", int(off_i)) if not cur_block.lines: # Block is empty -> bad block - cur_block = AsmBlockBad(label, errno=1) + cur_block = AsmBlockBad(label, errno=AsmBlockBad.ERROR_NULL_STARTING_BLOCK) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block diff --git a/miasm2/jitter/jitcore.py b/miasm2/jitter/jitcore.py index f2b1375d..4402ef49 100644 --- a/miasm2/jitter/jitcore.py +++ b/miasm2/jitter/jitcore.py @@ -17,7 +17,7 @@ # from hashlib import md5 -from miasm2.core import asmblock +from miasm2.core.asmblock import disasmEngine, AsmLabel, AsmBlockBad from miasm2.core.interval import interval from miasm2.core.utils import BoundedDict from miasm2.jitter.csts import * @@ -57,13 +57,15 @@ class JitCore(object): "max_exec_per_call": 0 # 0 means no limit } - self.mdis = asmblock.disasmEngine(ir_arch.arch, ir_arch.attrib, bs, - lines_wd=self.options["jit_maxline"], - symbol_pool=ir_arch.symbol_pool, - follow_call=False, - dontdis_retcall=False, - split_dis=self.split_dis, - dis_block_callback=self.disasm_cb) + self.mdis = disasmEngine( + ir_arch.arch, ir_arch.attrib, bs, + lines_wd=self.options["jit_maxline"], + symbol_pool=ir_arch.symbol_pool, + follow_call=False, + dontdis_retcall=False, + split_dis=self.split_dis, + dis_block_callback=self.disasm_cb + ) def set_options(self, **kwargs): @@ -135,7 +137,7 @@ class JitCore(object): """ # Get the block - if isinstance(addr, asmblock.AsmLabel): + if isinstance(addr, AsmLabel): addr = addr.offset # Prepare disassembler @@ -143,13 +145,9 @@ class JitCore(object): self.mdis.dis_block_callback = self.disasm_cb # Disassemble it - try: - cur_block = self.mdis.dis_block(addr) - except IOError: - # vm_exception_flag is set - label = self.ir_arch.symbol_pool.getby_offset_create(addr) - cur_block = asmblock.AsmBlockBad(label) - + cur_block = self.mdis.dis_block(addr) + if isinstance(cur_block, AsmBlockBad): + return cur_block # Logging if self.log_newbloc: print cur_block @@ -165,6 +163,7 @@ class JitCore(object): # Update jitcode mem range self.add_bloc_to_mem_interval(vm, cur_block) + return cur_block def runbloc(self, cpu, lbl, breakpoints): """Run the block starting at lbl. @@ -177,7 +176,16 @@ class JitCore(object): if not lbl in self.lbl2jitbloc: # Need to JiT the block - self.disbloc(lbl, cpu.vmmngr) + cur_block = self.disbloc(lbl, cpu.vmmngr) + if isinstance(cur_block, AsmBlockBad): + errno = cur_block.errno + if errno == AsmBlockBad.ERROR_IO: + cpu.vmmngr.set_exception(EXCEPT_ACCESS_VIOL) + elif errno == AsmBlockBad.ERROR_CANNOT_DISASM: + cpu.set_exception(EXCEPT_UNK_MNEMO) + else: + raise RuntimeError("Unhandled disasm result %r" % errno) + return lbl # Run the block and update cpu/vmmngr state return self.exec_wrapper(lbl, cpu, self.lbl2jitbloc.data, breakpoints, |