Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions pyvex/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,44 @@ 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 = {}
invalid_vals = (0xFFFFFFFF, -1)

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):
"""
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyvex/lifting/lift_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
137 changes: 137 additions & 0 deletions tests/test_irsb_extend.py
Original file line number Diff line number Diff line change
@@ -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("<i", 0x1000 - (jz_addr + 6)) # jz 0x1000
fallthrough = addr + len(code)

irsb = pyvex.lift(code, addr, pyvex.ARCH_AMD64, collect_data_refs=True)

assert irsb.size == len(code)
assert irsb.jumpkind == "Ijk_Boring"
imarks = [stmt for stmt in irsb.statements if type(stmt) is pyvex.stmt.IMark]
assert irsb.instructions == len(imarks)
assert irsb.instruction_addresses == tuple(imark.addr + imark.delta for imark in imarks)
assert {0x400000, 0x400007, 0x40000A, jz_addr} <= set(irsb.instruction_addresses)

assert irsb.default_exit_target == fallthrough
assert irsb.constant_jump_targets == {0x1000, fallthrough}
assert _exit_targets(irsb) == [(jz_addr, 0x1000)]

refs = {(ref.data_addr, ref.ins_addr) for ref in irsb.data_refs or []}
assert (0x400107, 0x400000) in refs
assert (0x400211, 0x40000A) in refs
for ref in irsb.data_refs or []:
assert _ins_addr_of_stmt(irsb, ref.stmt_idx) == ref.ins_addr

def test_lift_through_spotter_keeps_const_vals(self):
# An IAT call after an instruction lifted by AMD64Spotter: the folded call target must survive the join
addr = 0x1_4000_1000
slot_addr = 0x1_4000_1100
target = 0x1_40F0_0020
code = bytes.fromhex("488b0500010000") + bytes.fromhex("0f01f8") # mov rax, [rip + 0x100]; swapgs
call_addr = addr + len(code)
code += b"\xff\x15" + struct.pack("<i", slot_addr - (call_addr + 6)) # call qword ptr [slot_addr]

region = struct.pack("<Q", target)
buf = pyvex.ffi.from_buffer(region)
assert pyvex.pvc.register_readonly_region(slot_addr, len(region), buf)
try:
irsb = pyvex.lift(
code,
addr,
pyvex.ARCH_AMD64,
load_from_ro_regions=True,
const_prop=True,
collect_data_refs=True,
)
finally:
pyvex.pvc.deregister_all_readonly_regions()

assert irsb.jumpkind == "Ijk_Call"
assert isinstance(irsb.next, pyvex.expr.RdTmp)
folded = {cv.tmp: cv.value for cv in irsb.const_vals or []}
assert folded.get(irsb.next.tmp) == target
for cv in irsb.const_vals or []:
stmt = irsb.statements[cv.stmt_idx]
assert type(stmt) is pyvex.stmt.WrTmp and stmt.tmp == cv.tmp
assert (slot_addr, call_addr) in {(ref.data_addr, ref.ins_addr) for ref in irsb.data_refs or []}


if __name__ == "__main__":
unittest.main()
Loading