aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_msvcp_stdio_data.py (2506B)


      1 """Compare guest FILE data and translated streambuf aliases with Windows x86."""
      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/MSVCP60.DLL"
     11 assert hashlib.sha256(vendor.read_bytes()).hexdigest() == (
     12     "2b6b93c2d66969eb00258e2b5ad6172decebada096e3b1b077a3380c80e4a072"
     13 )
     14 work = root / "local/client/stdio-data"
     15 work.mkdir(parents=True, exist_ok=True)
     16 
     17 # Observe actual system MSVCRT data and the retained vendor's buffer methods.
     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/msvcp_stdio_data.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 # Compile complete generated chunks; discard only unused functions at link time.
     28 declarations = re.findall(r"^void (sub_[0-9A-F]+)\(void\);$", (decompiled / "tests/msvcp_stdio_data.c").read_text(), re.M)
     29 sources = []
     30 found = set()
     31 for source in sorted((decompiled / "vendor/msvcp60/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 # Keep test outputs separate from the active full Linux build.
     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/msvcp_stdio_data.c", "runtime/guest_crt.c", "runtime/guest_memory.c",
     47     "runtime/recomp_state.c", "vendor/msvcp60/source/data.c",
     48 ] + sources + ["-Wl,--gc-sections", "-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 MSVCRT/MSVCP == Linux x86-64: " + actual.strip())