Trace VR04 physical registration and generated helper lifetimes
This commit is contained in:
1 parent
3794439d4e
commit
346d350176
17 files changed
+718
-3
No files matched your search
@@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read-only VR04 serialized registration inventory and copied-code loop probe."""
|
||||
import argparse
|
||||
from collections import Counter
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from checkpoint_script_state import EXE_SHA256, find_case, read_va, rel_call_target
|
||||
from pe_inspect import PE
|
||||
from rtti_map import rtti_map
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]/'levels'))
|
||||
from gam import parse
|
||||
|
||||
REA_ID = 'ev_0d64cc58fa8f5573fc8e61e0f2c20e36288099212bd2257d3a3121361fd6c904'
|
||||
# Own registration sites, not counts of nested helpers or live runtime nodes.
|
||||
# PostLoad sites dispatch virtual2C; constructor sites directly call the bridge.
|
||||
CLASSES = (
|
||||
('3JIM','C3DJimmy',0x422160,0x4a373c,0xc0,'postload',0x4238b4,None),
|
||||
('3ROC','C3DRocketShip',0x43d840,0x4b25ac,0xc0,'postload',0x43db22,None),
|
||||
('STRT','C3DStartPoint',0x4422d0,0x4b7654,0xc8,'constructor',0x44249d,0x464210),
|
||||
('3PHO','C3DPhoneBooth',0x4355a0,0x4adf6c,0xc0,'constructor',0x4356a0,0x4623d0),
|
||||
('3PAT','C3DPatrolPoint',0x434b30,0x4acee4,0xc8,'constructor',0x434d44,0x464210),
|
||||
('3PIC','C3DPickupItem',0x4358b0,0x4ae548,0xc8,'constructor',0x435a37,0x464210),
|
||||
('3SOL','C3DYokianSoldier',0x44bee0,0x4c19c4,0xc0,'constructor',0x44bfa4,0x4623d0),
|
||||
('3TRO','C3DVRTrophy',0x448c60,0x4be404,0xc0,'constructor',0x448d33,0x4623d0),
|
||||
('3SM1','C3DSmoke',0x440530,0x4b4f08,0xc8,'no_own_call_in_inspected_chain',None,None),
|
||||
('3CAM','C3DCutSceneCamera',0x415a00,0x497bfc,0xc8,'no_own_call_in_inspected_chain',None,None),
|
||||
('3AIT','C3DAITrigger',0x40ba70,0x48f8c0,0xc8,'constructor',0x40be5b,0x464210),
|
||||
('3CHK','C3DCheckPoint',0x414190,0x4959a8,0xc8,'constructor',0x414323,0x464210),
|
||||
('LOAD','CLoadLevel',0x457b10,0x4d025c,0xc8,'constructor',0x457cca,0x464210),
|
||||
)
|
||||
RANGES = (
|
||||
(0x4623d0,0x46246e,'51906b2746390b64d79865a4ea0cddd03733aa5625d562bbf57264b7fa9b3d63'),
|
||||
(0x464210,0x46429f,'a99be3694789b6b6284b7e171eec0f2f26b59eef6a366265646bf6c8cbd92000'),
|
||||
(0x458910,0x45892b,'9631a8979eaae01bbf611aa19759788fb03daa8888f753dc64a58b7b7982070e'),
|
||||
(0x479a40,0x479aba,'cd3225de887694ae93f8ae226eeecf71322038438c7b587a81c14a504b10ae98'),
|
||||
(0x42ab50,0x42ab5f,'faf97e04eb625de47a2541a07db3a483ca05cd9b9fee870c7ca33d272e6e7e7a'),
|
||||
(0x46e570,0x46e69f,'ef42fea8b45653bbd21064e5d2da15011ddadc1790a7356997c2a7ba363d3c9c'),
|
||||
)
|
||||
TARGETS = (0x48e5b4,0x695d3c,0x695d40,0x50999c)
|
||||
|
||||
|
||||
def collect(original):
|
||||
original = Path(original)
|
||||
pe = PE(find_case(original,'Neutron.exe'))
|
||||
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
|
||||
raise ValueError('Unsupported executable')
|
||||
classes = {}
|
||||
for kind,name,ctor,table,displacement,phase,site,target in CLASSES:
|
||||
tables = rtti_map(pe,name)['vtables']
|
||||
if not any(int(t['va'],16)==table and int(t['object_displacement'],16)==displacement for t in tables):
|
||||
raise ValueError('RTTI/table identity mismatch')
|
||||
if site:
|
||||
if target and rel_call_target(pe,site)!=target:
|
||||
raise ValueError('Registration bridge call changed')
|
||||
if target is None and read_va(pe,site,3)!=b'\xff\x50\x2c':
|
||||
raise ValueError('PostLoad InitPhysics dispatch changed')
|
||||
methods = {hex(slot):hex(struct.unpack('<I',read_va(pe,table+slot,4))[0])
|
||||
for slot in (0x28,0x2c,0x178,0x210,0x220,0x40c)}
|
||||
classes[kind] = dict(name=name,constructor=hex(ctor),game_table=hex(table),
|
||||
displacement=hex(displacement),own_registration_phase=phase,
|
||||
registration_site=hex(site) if site else None,
|
||||
registration_file_offset=hex(pe.offset(site-pe.base)) if site else None,methods=methods)
|
||||
ranges, relocations = [], []
|
||||
for start,end,digest in RANGES:
|
||||
data = read_va(pe,start,end-start)
|
||||
if hashlib.sha256(data).hexdigest()!=digest:
|
||||
raise ValueError('Source range changed')
|
||||
ranges.append(dict(va=hex(start),file_offset=hex(pe.offset(start-pe.base)),length=end-start,sha256=digest))
|
||||
for target in TARGETS:
|
||||
for match in re.finditer(re.escape(struct.pack('<I',target)),data):
|
||||
relocations.append(dict(va=hex(start+match.start()),target=hex(target)))
|
||||
path = find_case(original,'gam/VR04.gam')
|
||||
records = []
|
||||
for ordinal,entity in enumerate(parse(path.read_bytes()).entities):
|
||||
c = classes[entity.kind];values=entity.values()
|
||||
records.append(dict(ordinal=ordinal,kind=entity.kind,tag=values.get('ObjectTag'),
|
||||
file_offset=hex(entity.offset),own_registration_phase=c['own_registration_phase'],
|
||||
game_table=c['game_table'],task=values.get('TaskName'),
|
||||
authored={k:values[k] for k in ('HasCollision','RequiredLevel','ExactLevel','RemoveLevel','CanMove') if k in values},
|
||||
runtime_membership='unobserved; reuse, helpers, constructor success and owner calls remain separate'))
|
||||
return dict(source_sha256=EXE_SHA256,classes=classes,ranges=ranges,relocations=relocations,
|
||||
gam_sha256=hashlib.sha256(path.read_bytes()).hexdigest(),records=records,
|
||||
serialized_phase_counts=dict(Counter(r['own_registration_phase'] for r in records)),
|
||||
scope='All72 serialized records/13 class interfaces; conditional source own-registration sites, not a complete runtime physical-list count.')
|
||||
|
||||
|
||||
def generate(original,output,rea):
|
||||
original,output=Path(original).resolve(),Path(output).resolve()
|
||||
if output.is_relative_to(original) or original.is_relative_to(output):
|
||||
raise ValueError('Separate generated directory required')
|
||||
if output.exists() and any(p.is_symlink() for p in output.rglob('*')):
|
||||
raise ValueError('Generated output contains symlinks')
|
||||
report=collect(original)
|
||||
evidence=next(r for r in json.loads(Path(rea).read_text())['records'] if r['evidence_id']==REA_ID)
|
||||
if evidence['subject']['digest']['sha256']!=EXE_SHA256:
|
||||
raise ValueError('REA source identity mismatch')
|
||||
rows=[]
|
||||
for line in evidence['raw_result']['stdout'].splitlines():
|
||||
match=re.match(r'\s*([0-9a-f]+):',line)
|
||||
if match:rows.append((int(match[1],16),line))
|
||||
for item,(a,b,_) in zip(report['ranges'],RANGES):
|
||||
item['rea_extract']='\n'.join(line for va,line in rows if a<=va<b)
|
||||
if not item['rea_extract']:raise ValueError('Missing REA instructions')
|
||||
report['rea_evidence_id']=REA_ID
|
||||
output.mkdir(parents=True,exist_ok=True)
|
||||
text=Path(__file__).with_name('physical_registration_probe.c').read_text()
|
||||
text=text.replace('/* RELOCATIONS */',','.join('{%s,%s}'%(r['va'],r['target']) for r in report['relocations']))
|
||||
(output/'physical_registration_probe.c').write_text(text)
|
||||
(output/'grappler_scalar_probe.c').write_text(Path(__file__).with_name('grappler_scalar_probe.c').read_text())
|
||||
(output/'source.json').write_text(json.dumps(report,indent=2)+'\n')
|
||||
return dict(records=len(report['records']),classes=len(report['classes']),ranges=len(report['ranges']),
|
||||
relocated_operands=len(report['relocations']),serialized_phase_counts=report['serialized_phase_counts'])
|
||||
|
||||
|
||||
def capture(output,directory):
|
||||
output,directory=Path(output),Path(directory)
|
||||
metadata=json.loads((directory/'capture.json').read_text());log=(directory/'wine.log').read_text()
|
||||
if metadata['captures'][-1]['wineExit']!=0 or 'PHYSICAL_REGISTRATION_COMPLETE' not in log:
|
||||
raise ValueError('Incomplete original component capture')
|
||||
source=json.loads((output/'source.json').read_text())
|
||||
digest=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
|
||||
if digest(find_case(directory/'game','Neutron.exe'))!=source['source_sha256']:
|
||||
raise ValueError('Copied source identity mismatch')
|
||||
if json.loads((directory/'probe-identity.json').read_text())['sha256']!=digest(output/'probe.exe'):
|
||||
raise ValueError('Observed probe identity mismatch')
|
||||
cases=[json.loads(line[4:]) for line in log.splitlines() if line.startswith('REG ')]
|
||||
expected=[('polygon_first','P0;R0;Z0;I0;',[0],[1,0,0],[1,0,0]),
|
||||
('polygon_repeat','P0;R0;Z0;I0;',[0,0],[1,0,0],[2,0,0]),
|
||||
('guard_already_registered','',[0,0],[1,0,0],[2,0,0]),
|
||||
('clear_then_guard','P0;R0;Z0;I0;',[0,0,0],[1,0,0],[3,0,0]),
|
||||
('sprite_first','P1;R1;Z1;I1;',[1],[0,1,0],[0,1,0]),
|
||||
('sprite_repeat','P1;R1;Z1;I1;',[1,1],[0,1,0],[0,2,0]),
|
||||
('nested_init_registration','P0;R0;Z0;I0;P1;R1;Z1;I1;',[1,0],[1,1,0],[1,1,0]),
|
||||
('register_disabled_body','P0;R0;Z0;I0;',[0],[1,0,0],[1,0,0]),
|
||||
('loader_three','I0;L0;I1;L1;I2;L2;C;T;',[],[0,0,0],[1,1,1]),
|
||||
('loader_append_on_init','I0;L0;I1;L1;I2;L2;C;T;',[],[0,0,0],[1,1,1]),
|
||||
('loader_append_on_post','I0;L0;I1;L1;I2;L2;C;T;',[],[0,0,0],[1,1,1]),
|
||||
('loader_empty','C;T;',[],[0,0,0],[0,0,0])]
|
||||
if len(cases)!=len(expected):raise ValueError('Missing original observations')
|
||||
for row,(name,events,order,flags,inits) in zip(cases,expected):
|
||||
if (row['name'],row['events'],row['order'],row['count'],row['flags'],row['inits'])!=(name,events,order,len(order),flags,inits):
|
||||
raise ValueError(f'Original registration mismatch: {name}')
|
||||
for i in range(3):
|
||||
registered=i in order
|
||||
if row['positions'][i] != ([1.25+i,-2.5-i,3.75+i] if registered else [0,0,0]):
|
||||
raise ValueError('Transform publication mismatch')
|
||||
if row['rotations'][i] != ([22.5,-45,90] if registered else [0,0,0]):
|
||||
raise ValueError('Angle bridge mismatch')
|
||||
files=[directory/'wine.log',directory/'capture.json',directory/'probe-identity.json',
|
||||
output/'source.json',output/'physical_registration_probe.c',output/'probe.exe']
|
||||
report=dict(cases=cases,provenance={str(p):digest(p) for p in files},
|
||||
scope='12 isolated copied-code registration/loader cases with synthetic Init/PostLoad/transform/log/timer dependencies; no real scene construction or native Windows test.')
|
||||
(output/'observations.json').write_text(json.dumps(report,indent=2)+'\n')
|
||||
return dict(cases=len(cases),scope=report['scope'])
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
cli=argparse.ArgumentParser(description=__doc__);cli.add_argument('original',type=Path)
|
||||
cli.add_argument('--output',type=Path);cli.add_argument('--rea',type=Path);cli.add_argument('--capture',type=Path)
|
||||
args=cli.parse_args()
|
||||
if args.capture and not args.output or args.output and not(args.rea or args.capture):
|
||||
cli.error('Generation needs --output/--rea; capture needs --output/--capture')
|
||||
result=capture(args.output,args.capture) if args.capture else generate(args.original,args.output,args.rea) if args.output else collect(args.original)
|
||||
print(json.dumps(result,indent=2))
|
||||
Reference in new issue
Block a user