tools/client/test_ace_token.py (2505B)
1 """Compare actual x86 ACE token methods with generated C on native Linux.""" 2 import hashlib 3 import pathlib 4 import re 5 import subprocess 6 7 8 root = pathlib.Path(__file__).resolve().parents[2] 9 decompiled = root / "client" 10 vendor = root / "anarchy-runtime-14.9.1/ace.dll" 11 assert hashlib.sha256(vendor.read_bytes()).hexdigest() == ( 12 "a1a0d96a5ff605f6611b8584204dad855914b1cbeedc0389f3a68279e015c22d" 13 ) 14 work = root / "local/client/ace-token" 15 work.mkdir(parents=True, exist_ok=True) 16 17 # Compile the native x86 reference and load the exact retained vendor DLL. 18 reference = work / "reference.exe" 19 subprocess.run([ 20 str(root / "toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), 21 "-O2", "-Iruntime", "tests/ace_token.c", "tests/reference_memory.c", 22 "runtime/recomp_state.c", "-o", str(reference), 23 ], cwd=decompiled, check=True) 24 expected = subprocess.check_output([str(reference), str(vendor)], text=True, timeout=30) 25 print("Windows reference: " + expected.strip(), flush=True) 26 27 # Select complete generated chunks containing the real exercised functions. 28 declarations = re.findall(r"^void (sub_[0-9A-F]+)\(void\);$", (decompiled / "tests/ace_token.c").read_text(), re.M) 29 sources = [] 30 found = set() 31 for source in sorted((decompiled / "vendor/ace/source").glob("lifted-*.c")): 32 functions = set(re.findall(r"^void (sub_[0-9A-F]+)\(void\) \{$", source.read_text(), re.M)) 33 selected = functions.intersection(declarations) 34 if selected: 35 sources.append(source.relative_to(decompiled).as_posix()) 36 found.update(selected) 37 assert found == set(declarations), set(declarations) - found 38 39 # Build an isolated ELF with production platform adapters and no PE loader. 40 linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] 41 linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" 42 wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] 43 subprocess.run(wsl + [ 44 "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", 45 "-ffunction-sections", "-fdata-sections", "-fno-strict-aliasing", 46 "tests/ace_token.c", "runtime/linux_path.c", "runtime/linux_platform.c", "runtime/guest_memory.c", 47 "runtime/recomp_state.c", "vendor/ace/source/data.c", 48 ] + sources + ["-Wl,--gc-sections", "-pthread", "-lm", "-o", linux_output], check=True) 49 actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30) 50 assert actual == expected, (expected, actual) 51 print("Windows ACE == Linux x86-64: " + actual.strip())