Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gymrat disasm #367

Merged
merged 10 commits into from
Nov 30, 2023
4 changes: 2 additions & 2 deletions pyvex/lifting/gym/arm_spotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,12 @@ def __init__(self, *args):
super().__init__(*args)
self.thumb: bool = False

def _lift(self, disassemble=False, dump_irsb=False):
def _lift(self):
if self.irsb.addr & 1:
# Thumb!
self.instrs = self.thumb_instrs
self.thumb = True
else:
self.instrs = self.arm_instrs
self.thumb = False
super()._lift(disassemble, dump_irsb)
super()._lift()
8 changes: 8 additions & 0 deletions pyvex/lifting/lifter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Lifter:
"addr",
"cross_insn_opt",
"load_from_ro_regions",
"disasm",
"dump_irsb",
)

"""
Expand Down Expand Up @@ -60,6 +62,8 @@ def lift(
collect_data_refs=False,
cross_insn_opt=True,
load_from_ro_regions=False,
disasm=False,
dump_irsb=False,
):
"""
Wrapper around the `_lift` method on Lifters. Should not be overridden in child classes.
Expand All @@ -80,6 +84,8 @@ def lift(
:param skip_stmts: Should the lifter skip transferring IRStmts from C to Python.
:param collect_data_refs: Should the LibVEX lifter collect data references in C.
:param cross_insn_opt: If cross-instruction-boundary optimizations are allowed or not.
:param disasm: Should the GymratLifter generate disassembly during lifting.
:param dump_irsb: Should the GymratLifter log the lifted IRSB.
"""
irsb = IRSB.empty_block(self.arch, self.addr)
self.data = data
Expand All @@ -95,6 +101,8 @@ def lift(
self.irsb = irsb
self.cross_insn_opt = cross_insn_opt
self.load_from_ro_regions = load_from_ro_regions
self.disasm = disasm
self.dump_irsb = dump_irsb
self._lift()
return self.irsb

Expand Down
16 changes: 10 additions & 6 deletions pyvex/lifting/util/lifter_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GymratLifter(Lifter):
"bitstrm",
"errors",
"thedata",
"disassembly",
)

REQUIRE_DATA_PY = True
Expand All @@ -48,6 +49,7 @@ def __init__(self, *args):
self.bitstrm = None
self.errors = None
self.thedata = None
self.disassembly = None

def create_bitstrm(self):
self.bitstrm = bitstring.ConstBitStream(bytes=self.thedata)
Expand Down Expand Up @@ -97,7 +99,7 @@ def decode(self):
log.exception(f"Error decoding block at offset {bytepos:#x} (address {addr:#x}):")
raise

def _lift(self, disassemble=False, dump_irsb=False):
def _lift(self):
self.thedata = (
self.data[: self.max_bytes]
if isinstance(self.data, (bytes, bytearray, memoryview))
Expand All @@ -106,8 +108,8 @@ def _lift(self, disassemble=False, dump_irsb=False):
log.debug(repr(self.thedata))
instructions = self.decode()

if disassemble:
return [instr.disassemble() for instr in instructions]
if self.disasm:
self.disassembly = [instr.disassemble() for instr in instructions]
self.irsb.jumpkind = JumpKind.Invalid
irsb_c = IRSBCustomizer(self.irsb)
log.debug("Decoding complete.")
Expand All @@ -127,7 +129,7 @@ def _lift(self, disassemble=False, dump_irsb=False):
dst_ty = vex_int_class(irsb_c.irsb.arch.bits).type
irsb_c.irsb.next = irsb_c.mkconst(dst, dst_ty)
log.debug(self.irsb._pp_str())
if dump_irsb:
if self.dump_irsb:
self.irsb.pp()
return self.irsb

Expand All @@ -136,11 +138,13 @@ def pp_disas(self):
insts = self.disassemble()
for addr, name, args in insts:
args_str = ",".join(str(a) for a in args)
disasstr += f"{addr:0#8x}:\t{name} {args_str}\n"
disasstr += f"{addr:#08x}:\t{name} {args_str}\n"
print(disasstr)

def error(self):
return self.errors

def disassemble(self):
return self.lift(disassemble=True)
if self.disassembly is None:
self.lift(self.data, disasm=True)
return self.disassembly
xxr0ss marked this conversation as resolved.
Show resolved Hide resolved