"""Unit tests for x64 .xdata UNWIND_INFO / UNWIND_CODE decoding in tools/recomp_census.py. Builds synthetic PE32+ images byte-by-byte with hand-constructed .pdata and .xdata records (no game files, standard library only). python -I tests/test_recomp_census_unwind.py """ import os import struct import sys import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if ROOT not in sys.path: sys.path.insert(0, ROOT) TOOLS_DIR = os.path.join(ROOT, "tools") if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) import pe_inspect # noqa: E402 import recomp_census as rc # noqa: E402 TEXT_RVA = 0x1000 TEXT_RAW = 0x200 TEXT_SIZE = 0x4000 IMAGE_BASE = 0x140000000 def unwind_header(version, flags, prolog_size, code_count, frame_reg=0, frame_off_scaled=0): b0 = (version & 0x7) | ((flags & 0x1F) << 3) b3 = (frame_reg & 0xF) | ((frame_off_scaled & 0xF) << 4) return bytes([b0, prolog_size & 0xFF, code_count & 0xFF, b3]) def ucode(code_offset, op, info=0): return bytes([code_offset & 0xFF, (op & 0xF) | ((info & 0xF) << 4)]) def u16(val): return struct.pack(" padded to 16 slots = 32 bytes before handler RVA): # 1: PUSH_NONVOL rbx (1 slot) -> +8 # 2: ALLOC_SMALL info=3 -> 32 bytes (1 slot) # 3: ALLOC_LARGE info=0, 0x20 -> 0x100 bytes (2 slots) # 4: ALLOC_LARGE info=1, 0x0002_0040 (3 slots) # 5: SET_FPREG rbp, FrameOffset=2*16=32 (1 slot) # 6: SAVE_NONVOL rsi, offset=4*8=32 (2 slots) # 7: SAVE_NONVOL_FAR r12, offset=0x0001_0020 (3 slots) # 8: SAVE_XMM128 xmm6, offset=3*16=48 (2 slots) codes = b"".join([ ucode(0x02, 0, 3), # PUSH_NONVOL rbx (1) ucode(0x06, 2, 3), # ALLOC_SMALL 32 (1) ucode(0x0D, 1, 0) + u16(0x20), # ALLOC_LARGE 256 (2) ucode(0x14, 1, 1) + u32(0x00020040), # ALLOC_LARGE 0x20040 (3) ucode(0x18, 3, 0), # SET_FPREG rbp (1) ucode(0x1D, 4, 6) + u16(4), # SAVE_NONVOL rsi @ 32 (2) ucode(0x24, 5, 12) + u32(0x00010020), # SAVE_NONVOL_FAR r12 @ 0x10020 (3) ucode(0x2C, 8, 6) + u16(3), # SAVE_XMM128 xmm6 @ 48 (2) ]) self.assertEqual(len(codes), 15 * 2) handler_rva = 0x1900 # 15 slots -> 2 bytes padding before handler_rva blob = unwind_header(1, rc.UNW_FLAG_EHANDLER, 0x30, 15, frame_reg=5, frame_off_scaled=2) + codes + b"\x00\x00" + u32(handler_rva) img = pe_inspect.PeImage(build_pe_with_xdata([], {0x3000: blob})) info = rc.parse_unwind_info(img, 0x3000) self.assertIsNone(info["error"]) self.assertEqual(info["version"], 1) self.assertEqual(info["flags"], ["EHANDLER"]) self.assertEqual(info["prolog_size"], 0x30) self.assertEqual(info["code_count"], 15) self.assertEqual(info["frame_register"], "rbp") self.assertEqual(info["frame_offset"], 32) self.assertEqual(info["stack_alloc"], 8 + 32 + 256 + 0x20040) self.assertEqual(info["saved_gprs"], ["rbx", "rsi", "r12"]) self.assertEqual(info["saved_xmms"], ["xmm6"]) self.assertEqual(info["handler_rva"], handler_rva) self.assertEqual(len(info["ops"]), 8) self.assertEqual(info["ops"][5], {"op": "UWOP_SAVE_NONVOL", "code_offset": 0x1D, "reg": "rsi", "offset": 32}) self.assertEqual(info["ops"][6], {"op": "UWOP_SAVE_NONVOL_FAR", "code_offset": 0x24, "reg": "r12", "offset": 0x10020}) self.assertEqual(info["ops"][7], {"op": "UWOP_SAVE_XMM128", "code_offset": 0x2C, "reg": "xmm6", "offset": 48}) def test_v2_epilog_spare_xmm_far_and_machframe(self): # 10 slots (even -> 0 padding bytes before UHANDLER): # 2 UWOP_EPILOG (1st at_end=True len=5; 2nd offset=0x120) -> 2 slots # 1 UWOP_EPILOG padding (offset=0) -> 1 slot # 1 UWOP_SPARE_CODE -> 3 slots # 1 UWOP_SAVE_XMM128_FAR xmm15 @ 0x30000 -> 3 slots # 1 UWOP_PUSH_MACHFRAME errcode=1 -> 1 slot codes = b"".join([ ucode(0x05, 6, 1), # EPILOG at_end=True, length=5 (1) ucode(0x20, 6, 1), # EPILOG offset=0x120 (1) ucode(0x00, 6, 0), # EPILOG padding (1) ucode(0x00, 7, 0) + u16(0) + u16(0), # SPARE_CODE (3) ucode(0x10, 9, 15) + u32(0x30000), # SAVE_XMM128_FAR xmm15 @ 0x30000 (3) ucode(0x00, 10, 1), # PUSH_MACHFRAME errcode=yes (1) ]) self.assertEqual(len(codes), 10 * 2) blob = unwind_header(2, rc.UNW_FLAG_UHANDLER, 0x14, 10) + codes + u32(0x1A00) img = pe_inspect.PeImage(build_pe_with_xdata([], {0x3000: blob})) info = rc.parse_unwind_info(img, 0x3000) self.assertIsNone(info["error"]) self.assertEqual(info["version"], 2) self.assertEqual(info["epilog_count"], 2) # at_end + offset 0x120; padding slot excluded self.assertTrue(info["ops"][0]["at_end"]) self.assertEqual(info["ops"][0]["length"], 5) self.assertEqual(info["ops"][1]["epilog_offset"], 0x120) self.assertTrue(info["ops"][2].get("padding")) self.assertEqual(info["ops"][3]["op"], "UWOP_SPARE_CODE") self.assertEqual(info["ops"][4], {"op": "UWOP_SAVE_XMM128_FAR", "code_offset": 0x10, "reg": "xmm15", "offset": 0x30000}) self.assertEqual(info["saved_xmms"], ["xmm15"]) self.assertTrue(info["has_machframe"]) self.assertTrue(info["ops"][5]["has_error_code"]) self.assertEqual(info["handler_rva"], 0x1A00) def test_multi_hop_chained_unwind_aggregates_frame_and_handler(self): # Secondary (cold) entry @ 0x3000 -> chains to middle @ 0x3040 -> chains to primary @ 0x3080 (with EHANDLER) # Cold: 1 odd slot (ALLOC_SMALL 16B) + 2B pad + chained RUNTIME_FUNCTION -> 0x3040 cold = (unwind_header(1, rc.UNW_FLAG_CHAININFO, 0x04, 1) + ucode(0x04, 2, 1) + b"\x00\x00" + struct.pack(" 0x3080 mid = (unwind_header(1, rc.UNW_FLAG_CHAININFO, 0x04, 2) + ucode(0x02, 0, 7) + ucode(0x01, 0, 3) + struct.pack(" truncated codes info = self.parse_single({0x4FFC: unwind_header(1, 0, 4, 2)}, rva=0x4FFC) self.assertIn("truncated unwind codes", info["error"]) # Place EHANDLER header at 0x4FFC with 0 codes -> handler RVA at 0x5000 is outside section info2 = self.parse_single({0x4FFC: unwind_header(1, rc.UNW_FLAG_EHANDLER, 0, 0)}, rva=0x4FFC) self.assertIn("truncated exception handler RVA", info2["error"]) # Place CHAINED header at 0x4FF8 with 0 codes -> chained RUNTIME_FUNCTION needs 12B, only 4B left info3 = self.parse_single({0x4FF8: unwind_header(1, rc.UNW_FLAG_CHAININFO, 0, 0)}, rva=0x4FF8) self.assertIn("truncated chained RUNTIME_FUNCTION", info3["error"]) def test_chained_cycle_detected(self): # 0x3000 -> 0x3020 -> 0x3000 a = unwind_header(1, rc.UNW_FLAG_CHAININFO, 0, 0) + struct.pack(" u1 (inherits EHANDLER 0x1800, FP=rbp, xmm6, adds ALLOC_SMALL 16B) u2 = (unwind_header(1, rc.UNW_FLAG_CHAININFO, 0x04, 1) + ucode(0x04, 2, 1) + b"\x00\x00" + struct.pack("