#!/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('