tools/client/test_portable_eh.py (4886B)
1 """Validate generated catch-only continuations against production Windows/Linux EH. 2 3 Checks catch-all dispatch, metadata lookup, longjmp continuation, return value, 4 stack/FS restoration, preserved registers and an x87 catch store across repeated 5 calls. Does not cover typed catches, object binding, destructor unwind, rethrow, 6 nested exceptions, uncaught exceptions or concurrent guest execution. 7 """ 8 import pathlib 9 import struct 10 import subprocess 11 import sys 12 13 14 root = pathlib.Path(__file__).resolve().parents[2] 15 upstream = root / "local/references/reconstruction-reference/pcrecomp" 16 sys.path.insert(0, str(upstream)) 17 from tools.lift import translator, generate 18 from tools.pe.pe_analyze import Section 19 import game_eh 20 21 22 def generate_fixture(): 23 # Synthetic x86 is generator input only; no code bytes enter runtime memory. 24 data = bytearray(b"\xcc" * 0x4000) 25 prologue = bytes.fromhex("558bec6a00680020004264a1000000005064892500000000" 26 "83ec048965f068503000426a00e8d60f0000cc") 27 data[0x1000:0x1000 + len(prologue)] = prologue 28 data[0x1080:0x108a] = bytes.fromhex("b82a000000e906000000") 29 data[0x1090:0x109e] = bytes.fromhex("8b4df464890d000000008be55dc3") 30 data[0x1100:0x1111] = bytes.fromhex("d9e8b880100042dd5de8c3b8a0100042c3") 31 data[0x10a0:0x10a6] = bytes.fromhex("b839050000c3") 32 data[0x2000:0x200a] = bytes.fromhex("b800300042e900000000") 33 data[0x3000:0x3014] = struct.pack("<5I", 0x19930520, 1, 0x42003100, 1, 0x42003200) 34 data[0x3100:0x3108] = struct.pack("<2I", 0xffffffff, 0) 35 data[0x3200:0x3214] = struct.pack("<5I", 0, 0, 1, 1, 0x42003300) 36 data[0x3300:0x3310] = struct.pack("<4I", 0, 0, 0, 0x42001100) 37 disassembler = translator.Disassembler(bytes(data), 0x42000000, 38 [Section(".text", 0, len(data), 0, len(data), 0x60000020)]) 39 function = disassembler.disassemble_function(0x42001000) 40 catch = disassembler.disassemble_function(0x42001100) 41 functions = {function.address: function, catch.address: catch} 42 instructions = {i.address: i for block in function.blocks.values() for i in block.instructions} 43 assert 0x42001080 not in instructions 44 instructions[0x42000ff0] = next(disassembler.disassemble_at(0x42000ff0)) 45 leaders = set(function.blocks) | {0x42000ff0} 46 47 resumes = game_eh.add_exception_resumes(disassembler, function.address, functions, instructions, leaders) 48 code = generate.lift_function_linear(translator.Lifter(iat_map={}, lifted=set()), 49 "sub_42001000", [instructions[a] for a in sorted(instructions)], leaders, 50 function.address, eh_resume=resumes) 51 catch_instructions = {i.address: i for block in catch.blocks.values() for i in block.instructions} 52 code += "\n" + generate.lift_function_linear(translator.Lifter(iat_map={}, lifted=set()), 53 "sub_42001100", [catch_instructions[a] for a in sorted(catch_instructions)], 54 set(catch.blocks), catch.address) 55 56 assert resumes == {0x42001080} 57 assert 0x42001080 in instructions and 0x420010a0 not in instructions 58 assert "RECOMP_EH_ENTER();" in code and "L_42001080:" in code 59 return ('/* Generated by tools/client/test_portable_eh.py. */\n' 60 '#define RECOMP_GENERATED_CODE\n#include "recomp_types.h"\n' + code) 61 62 63 def main(): 64 decompiled = root / "client" 65 work = root / "local/client/portable-eh" 66 work.mkdir(parents=True, exist_ok=True) 67 generated = generate_fixture() 68 (work / "lifted.c").write_text(generated, encoding="ascii") 69 assert (decompiled / "tests/guest_eh_lifted.c").read_text() == generated 70 71 # Use production eh32.c on both hosts, without native32 or an image loader. 72 reference = work / "reference.exe" 73 subprocess.run([ 74 str(root / "toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), 75 "-O2", "-fms-extensions", "-Iruntime", "-Idata", "tests/guest_eh.c", "tests/guest_eh_lifted.c", 76 "tests/reference_memory.c", "runtime/recomp_state.c", "runtime/eh32.c", 77 "-o", str(reference), 78 ], cwd=decompiled, check=True) 79 expected = subprocess.check_output([str(reference)], text=True, timeout=30) 80 81 linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] 82 linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" 83 wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] 84 subprocess.run(wsl + [ 85 "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-Idata", "-fno-strict-aliasing", 86 "tests/guest_eh.c", "tests/guest_eh_lifted.c", "runtime/guest_memory.c", 87 "runtime/recomp_state.c", "runtime/eh32.c", "-o", linux_output, 88 ], check=True) 89 actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30) 90 assert actual == expected, (expected, actual) 91 print("Windows x86 == Linux x86-64: " + actual.strip()) 92 93 94 if __name__ == "__main__": 95 main()