From 5389eb668d9c4bd040419753acf1943f58a7abe2 Mon Sep 17 00:00:00 2001 From: veritas501 Date: Mon, 26 Apr 2021 16:39:06 +0800 Subject: [PATCH] fix(emulate): let `emulate` works on unicorn-1.0.2rc1 ~ unicorn-1.0.2 --- pwndbg/disasm/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pwndbg/disasm/__init__.py b/pwndbg/disasm/__init__.py index 6fa498e9b..89552666c 100644 --- a/pwndbg/disasm/__init__.py +++ b/pwndbg/disasm/__init__.py @@ -214,16 +214,14 @@ def near(address, instructions=1, emulate=False, show_prev_insns=True): if address == pc and emulate: emu = pwndbg.emu.emulator.Emulator() - # For whatever reason, the first instruction is emulated twice. - # Skip the first one here. - emu.single_step() - # Now find all of the instructions moving forward. # # At this point, we've already added everything *BEFORE* the requested address, # and the instruction at 'address'. insn = current total_instructions = 1 + (2*instructions) + last_emu_target = None + target_candidate = address while insn and len(insns) < total_instructions: target = insn.target @@ -236,7 +234,14 @@ def near(address, instructions=1, emulate=False, show_prev_insns=True): # If we initialized the emulator and emulation is still enabled, we can use it # to figure out the next instruction. if emu: - target_candidate, size_candidate = emu.single_step() + # For whatever reason, the first instruction is emulated twice on + # unicorn-1.0.2rc1~unicorn-1.0.2rc3, but not on >= unicorn-1.0.2rc4. + # If the address is equal with the last one, skip it + last_emu_target = target_candidate + while last_emu_target == target_candidate: + target_candidate, size_candidate = emu.single_step() + if not target_candidate: + break if None not in (target_candidate, size_candidate): target = target_candidate