diff --git a/pyvex/block.py b/pyvex/block.py index 6ba11c59..0a65e798 100644 --- a/pyvex/block.py +++ b/pyvex/block.py @@ -212,13 +212,31 @@ def copy(self) -> "IRSB": def extend(self, extendwith) -> None: """ - Appends an irsb to the current irsb. The irsb that is appended is invalidated. The appended irsb's jumpkind and - default exit are used. + Appends an irsb to the current irsb. The appended irsb's jumpkind and default exit are used. + + The appended irsb is not modified, so it remains usable and may be appended to more than one irsb. + :param extendwith: The IRSB to append to this IRSB :vartype extendwith: :class:`IRSB` """ + self._extend(extendwith, copy_statements=True) + + def _extend(self, extendwith, copy_statements: bool) -> None: + """ + Appends `extendwith` to this IRSB, see :meth:`extend`. + + :param extendwith: The IRSB to append to this IRSB + :param copy_statements: Whether to copy the statements of `extendwith` before renumbering their tmps. Only + pass False if `extendwith` is discarded afterwards, as it is left invalid. + """ if self.stmts_used == 0: self._from_py(extendwith) + # Later calls append to the statement list and the type environment, so they must not be shared + self.statements = list(self.statements) + self.tyenv = copy.deepcopy(self.tyenv) + self.data_refs = copy.copy(extendwith.data_refs) + self.const_vals = copy.copy(extendwith.const_vals) + self.is_noop_block = extendwith.is_noop_block return conversion_dict = {} @@ -226,7 +244,12 @@ def extend(self, extendwith) -> None: new_size = self.size + extendwith.size new_instructions = self.instructions + extendwith.instructions + new_instruction_addresses = self.instruction_addresses + extendwith.instruction_addresses new_direct_next = extendwith.direct_next + stmt_idx_offset = len(self.statements) + # Tmps in the appended statements are renumbered in place below. Unless they are copied first, the appended IRSB + # is left referring to tmps that do not exist in its own type environment. + statements = copy.deepcopy(extendwith.statements) if copy_statements else extendwith.statements def convert_tmp(tmp): """ @@ -250,7 +273,7 @@ def convert_expr(expr_): return RdTmp.get_instance(convert_tmp(expr_.tmp)) return expr_ - for stmt_ in extendwith.statements: + for stmt_ in statements: stmttype = type(stmt_) if stmttype is WrTmp: stmt_.tmp = convert_tmp(stmt_.tmp) @@ -261,8 +284,6 @@ def convert_expr(expr_): elif stmttype is Dirty: if stmt_.tmp not in invalid_vals: stmt_.tmp = convert_tmp(stmt_.tmp) - for e in stmt_.args: - convert_expr(e) elif stmttype is CAS: if stmt_.oldLo not in invalid_vals: stmt_.oldLo = convert_tmp(stmt_.oldLo) @@ -277,14 +298,27 @@ def convert_expr(expr_): stmt_.replace_expression(to_replace) # Add the converted statement to self.statements self.statements.append(stmt_) - extendwith.next = convert_expr(extendwith.next) - self.next = extendwith.next - self.jumpkind = extendwith.jumpkind + + if extendwith.data_refs: + self.data_refs = list(self.data_refs or ()) + [ + DataRef(ref.data_addr, ref.data_size, ref.data_type, ref.stmt_idx + stmt_idx_offset, ref.ins_addr) + for ref in extendwith.data_refs + ] + if extendwith.const_vals: + self.const_vals = list(self.const_vals or ()) + [ + ConstVal(convert_tmp(cv.tmp), cv.value, cv.stmt_idx + stmt_idx_offset) for cv in extendwith.const_vals + ] + self._size = new_size self._instructions = new_instructions + self._instruction_addresses = new_instruction_addresses + # Recomputed from the statements on the next access + self._exit_statements = None + self.next = convert_expr(extendwith.next) + self.jumpkind = extendwith.jumpkind self._direct_next = new_direct_next - - # TODO: Change exit_statements, data_references, etc. + self.default_exit_target = extendwith.default_exit_target + self.is_noop_block = self.is_noop_block and extendwith.is_noop_block def invalidate_direct_next(self) -> None: self._direct_next = None diff --git a/pyvex/lifting/lift_function.py b/pyvex/lifting/lift_function.py index f4241f24..54931d70 100644 --- a/pyvex/lifting/lift_function.py +++ b/pyvex/lifting/lift_function.py @@ -265,8 +265,8 @@ def lift( vex_archinfo=vex_archinfo, ) if more_irsb.size: - # Successfully decoded more bytes - final_irsb.extend(more_irsb) + # Successfully decoded more bytes. more_irsb is discarded afterwards, so its statements need no copy. + final_irsb._extend(more_irsb, copy_statements=False) # pylint: disable=protected-access elif max_bytes == 0: # We have no more bytes left. Mark the jumpkind of the IRSB as Ijk_Boring if final_irsb.size > 0 and final_irsb.jumpkind == "Ijk_NoDecode": diff --git a/tests/test_irsb_extend.py b/tests/test_irsb_extend.py new file mode 100644 index 00000000..6efb664a --- /dev/null +++ b/tests/test_irsb_extend.py @@ -0,0 +1,137 @@ +"""Tests for IRSB.extend(). + +``lift()`` joins the blocks produced by different lifters the same way, for example when libVEX stops at an instruction +that only a gym spotter can lift. The joined block must describe all of its instructions, and the appended block must +stay intact. +""" + +# pylint: disable=missing-class-docstring,no-self-use +import struct +import unittest + +import pyvex + + +def _ins_addr_of_stmt(irsb: pyvex.IRSB, stmt_idx: int) -> int: + for stmt in reversed(irsb.statements[: stmt_idx + 1]): + if type(stmt) is pyvex.stmt.IMark: + return stmt.addr + stmt.delta + raise AssertionError(f"Statement {stmt_idx} is not preceded by an IMark") + + +def _exit_targets(irsb: pyvex.IRSB) -> list[tuple[int, int]]: + targets = [] + for ins_addr, stmt_idx, stmt in irsb.exit_statements: + assert isinstance(stmt, pyvex.stmt.Exit) + assert irsb.statements[stmt_idx] is stmt + targets.append((ins_addr, stmt.dst.value)) + return targets + + +class TestIRSBExtend(unittest.TestCase): + def test_extend_does_not_modify_the_appended_irsb(self): + # Two blocks at the same address, so that each of them is contiguous with the appended block + first = pyvex.lift(b"\x50", 0x1000, pyvex.ARCH_X86) # push eax + second = pyvex.lift(b"\x51", 0x1000, pyvex.ARCH_X86) # push ecx + appended = pyvex.lift(b"\x52", 0x1001, pyvex.ARCH_X86) # push edx + appended_str = str(appended) + + for irsb in (first, second): + irsb.extend(appended) + assert str(appended) == appended_str + assert irsb.typecheck() + assert irsb.size == 2 + assert irsb.instruction_addresses == (0x1000, 0x1001) + + assert appended.typecheck() + + def test_extend_empty_irsb_does_not_alias_the_appended_irsb(self): + block = pyvex.lift(b"\x50", 0x1000, pyvex.ARCH_X86) # push eax + block_str = str(block) + + irsb = pyvex.IRSB(None, 0x1000, pyvex.ARCH_X86) + irsb.extend(block) + irsb.extend(pyvex.lift(b"\x51", 0x1001, pyvex.ARCH_X86)) # push ecx + + assert str(block) == block_str + assert block.typecheck() + assert irsb.typecheck() + assert irsb.size == 2 + assert irsb.instruction_addresses == (0x1000, 0x1001) + + def test_extend_refreshes_exit_statements(self): + irsb = pyvex.lift(b"\x50", 0x1000, pyvex.ARCH_X86) # push eax + assert irsb.exit_statements == () # computes and caches the exits + irsb.extend(pyvex.lift(b"\x74\x02", 0x1001, pyvex.ARCH_X86)) # je 0x1005 + + assert _exit_targets(irsb) == [(0x1001, 0x1005)] + assert irsb.default_exit_target == 0x1003 + assert irsb.constant_jump_targets == {0x1003, 0x1005} + + def test_lift_through_spotter_keeps_derived_state(self): + # swapgs is lifted by AMD64Spotter, so lift() joins the blocks around it with extend() + addr = 0x400000 + code = ( + bytes.fromhex("488b0500010000") # mov rax, [rip + 0x100] + + bytes.fromhex("0f01f8") # swapgs + + bytes.fromhex("488b1d00020000") # mov rbx, [rip + 0x200] + ) + jz_addr = addr + len(code) + code += b"\x0f\x84" + struct.pack("