Files
Neutron/tools/binary/collision_response.py
T

78 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Guard original collision callgraph and emit explicit probe relocation operands.
No original code is executed here. Relocations apply only to generated in-memory
copies in collision_response_probe.c; original files remain unchanged.
"""
import argparse
import hashlib
import json
from pathlib import Path
import struct
from checkpoint_script_state import EXE_SHA256, find_case, read_va
from pe_inspect import PE
BODIES = (
('vector_copy', 0x409F60, 0x409F7F, '488dbddb9d91612e9d16686d3d0c8a7fbc851b9899b24fcd7433007989a8b2ad'),
('segment_build', 0x469530, 0x4695AA, '87676d756eda5ce058c2210556bd57fbd54dd72cbede346725e653c2bc5ad66c'),
('nearest_triangle_edge', 0x4695B0, 0x469880, '4b358d6ba956a9629bf2e814174c757a3930d891c0b4cbab9b089623dcef6a2a'),
('nearest_segment_point', 0x469880, 0x469A05, '040c9805125fe32871183be8c82b9c909e34ce165711405d97af57f3da711965'),
('triangle_ray', 0x469B70, 0x469E19, '41bca9254c9ba5c57882cf2bc975a350d266a3ac2b5b9965cad11e585bb4faa0'),
('sphere_response', 0x47BD80, 0x47C204, '8f0892a12197cdd142aecacce636ac7de6c7a061516ab16137909b5b17cff156'),
)
# Absolute memory operands in the six bounded functions, cross-checked against
# objdump and REA. Debug calls are disabled by695D44; no other imports are bound.
TARGETS = (0x48D104, 0x48D914, 0x48D924, 0x48D974, 0x49B1F8, 0x4A3148,
0x4D5E40, 0x4D5E44, 0x5CFBE0, 0x5CFBE8, 0x5CFBEC, 0x5CFBF0,
0x5CFBF4, 0x695D44, 0x695D5C, 0x695D74)
def collect(original):
pe = PE(find_case(Path(original), 'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
raise ValueError('Unsupported original EXE')
bodies, operands = [], []
for name, start, end, digest in BODIES:
body = read_va(pe, start, end-start)
if hashlib.sha256(body).hexdigest() != digest:
raise ValueError(f'Changed function {name}')
bodies.append(dict(name=name, va=hex(start), file_offset=hex(pe.offset(start-pe.base)),
length=end-start, sha256=digest))
for target in TARGETS:
encoded = struct.pack('<I', target)
for index in range(len(body)-3):
if body[index:index+4] == encoded:
operands.append(dict(va=start+index, target=target))
return dict(sha256=EXE_SHA256, bodies=bodies, probe_operands=sorted(operands, key=lambda x:x['va']),
constants={hex(va): struct.unpack('<f',read_va(pe,va,4))[0]
for va in (0x48D974,0x49B1F8,0x4A3148)},
scope='Static callgraph guard; generated probe relocations are synthetic fixture setup, not source loader behavior.')
def header(report):
lines = ['/* Generated guarded absolute operands; never patch on-disk originals. */',
'static const struct { unsigned va, target; } relocations[] = {']
lines += [f' {{0x{p["va"]:x}, 0x{p["target"]:x}}},' for p in report['probe_operands']]
return '\n'.join(lines+['};',''])
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original',type=Path)
cli.add_argument('--probe-header',type=Path)
cli.add_argument('--target-copy',type=Path)
args=cli.parse_args()
report=collect(args.original)
if args.target_copy:
target=args.target_copy.resolve(strict=True)
if target.is_relative_to(args.original.resolve()): cli.error('Probe target must be a generated copy')
if hashlib.sha256(target.read_bytes()).hexdigest()!=report['sha256']:
cli.error('Probe copy does not match authoritative EXE')
if args.probe_header:
output=args.probe_header.resolve()
if output.is_relative_to(args.original.resolve()): cli.error('Generated header must be outside originals')
output.write_text(header(report))
print(json.dumps(report,indent=2))