aorecomp

Anarchy Online classic in the browser

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


tools/client/audit_game.py (4565B)


      1 """Audit the complete generated game against its inputs and compile records."""
      2 
      3 import hashlib
      4 import json
      5 import pathlib
      6 import re
      7 import subprocess
      8 
      9 import pefile
     10 
     11 
     12 root = pathlib.Path(__file__).resolve().parents[2]
     13 work = root / 'local/client/game-14.9.1'
     14 upstream = root / 'local/references/reconstruction-reference/pcrecomp'
     15 compiler = root / 'toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe'
     16 scope = json.loads((work / 'scope.json').read_text())
     17 inventory = json.loads((work / 'inventory.json').read_text())['modules']
     18 
     19 # Account for every inventoried PE and recheck the immutable originals.
     20 expected = {module['path'].lower() for module in inventory}
     21 actual = {module['name'].lower() for module in scope['game']}
     22 assert len(actual) == 34
     23 assert actual | (set(scope['vendors']) & expected) | {name.lower() for name in scope['excluded_executables']} == expected
     24 for module in inventory:
     25     assert hashlib.sha256((root / 'anarchy-vanilla-14.9.1' / module['path']).read_bytes()).hexdigest() == module['sha256']
     26 
     27 # Match the generator fingerprint and every generated source to its compile stamp.
     28 fingerprint = hashlib.sha256((root / 'tools/client/build_game.py').read_bytes())
     29 fingerprint.update((root / 'tools/client/game_eh.py').read_bytes())
     30 for directory in ('tools/lift', 'tools/disasm', 'tools/pe', 'runtime/recomp32'):
     31     for source in sorted((upstream / directory).iterdir()):
     32         if source.suffix in ('.py', '.h'):
     33             fingerprint.update(source.read_bytes())
     34 fingerprint.update(str(compiler.stat().st_mtime_ns).encode())
     35 pipeline = fingerprint.hexdigest()
     36 entries = []
     37 objects = []
     38 reports = []
     39 for module in scope['game']:
     40     directory = work / module['name']
     41     stamp = json.loads((directory / 'completed.json').read_text())
     42     assert stamp['pipeline'] == pipeline, module['name']
     43     assert stamp['input'] == module['sha256']
     44     assert stamp['report']['unsupported'] == 0
     45     functions = json.loads((directory / 'functions.json').read_text())
     46     addresses = {address for address, name in functions}
     47     assert module['entry'] in addresses
     48     image = pefile.PE(str(work / 'images' / module['name']))
     49     if image.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size:
     50         for export in image.DIRECTORY_ENTRY_EXPORT.symbols:
     51             section = image.get_section_by_rva(export.address)
     52             if section is not None and section.Characteristics & 0x20000000:
     53                 assert module['base'] + export.address in addresses
     54     for object_name in stamp['objects']:
     55         obj = pathlib.Path(object_name)
     56         source = obj.with_suffix('.c')
     57         command = [str(compiler), '-O2', '-fno-strict-aliasing', '-fms-extensions',
     58                    '-I', str(upstream / 'runtime/recomp32'), '-c', str(source), '-o', str(obj)]
     59         fingerprint = hashlib.sha256((source.read_text() + repr(command) + str(compiler.stat().st_mtime_ns)).encode())
     60         for header in sorted((upstream / 'runtime/recomp32').glob('*.h')):
     61             fingerprint.update(header.read_bytes())
     62         assert obj.is_file()
     63         assert fingerprint.hexdigest() == source.with_suffix('.sha256').read_text(), str(source)
     64     entries.extend(functions)
     65     objects.extend(stamp['objects'])
     66     reports.append(stamp['report'])
     67     print('Audited', module['name'], flush=True)
     68 
     69 # The final dispatch and link response must contain the entire audited set.
     70 dispatch = re.findall(r'\{0x([0-9A-F]+)u, (sub_[0-9A-F]+)\}', (work / 'dispatch.c').read_text())
     71 assert sorted((int(address, 16), name) for address, name in dispatch) == sorted(tuple(entry) for entry in entries)
     72 linked_objects = [line.strip('"').replace('/', '\\') for line in (work / 'objects.rsp').read_text().splitlines()]
     73 assert sorted(linked_objects) == sorted(objects)
     74 assert len({address for address, name in entries}) == len(entries)
     75 assert len(set(objects)) == len(objects)
     76 result = {'original_modules_unchanged': len(inventory), 'game_modules': len(actual),
     77           'functions': len(entries), 'objects': len(objects), 'unsupported': 0,
     78           'pipeline': pipeline,
     79           'native_runtime_sha256': hashlib.sha256((work.parent / 'native32/native32.c').read_bytes()).hexdigest(),
     80           'executable_sha256': hashlib.sha256((work / 'game.exe').read_bytes()).hexdigest(),
     81           'upstream_commit': subprocess.check_output(['git', '-c', f'safe.directory={upstream.as_posix()}',
     82               '-C', str(upstream), 'rev-parse', 'HEAD'], text=True).strip()}
     83 (work / 'completion-audit.json').write_text(json.dumps(result, indent=2))
     84 print(json.dumps(result, indent=2))