Skip to content

Commit

Permalink
Fix gymrat disasm (#367)
Browse files Browse the repository at this point in the history
* Fix gymrat disasm

* allow kwargs for _lifter()

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Revert "allow kwargs for _lifter()"

This reverts commit a583072.

* fix gymrat disasm by overriding lift

* Revert "fix gymrat disasm by overriding lift"

This reverts commit 6f83b80.

* add kwargs in Lifter for GymratLifter

* Return self.irsb instead of result of self._lift()

* Update ARMSpotter to proper semantics

* stick to the semantics of lift to return irsb

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Kevin Phoenix <[email protected]>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent 98a756d commit 63bc840
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
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

0 comments on commit 63bc840

Please sign in to comment.