154 lines
8.2 KiB
Python
154 lines
8.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Guard the original derived Jimmy frame and prepare a bounded caller probe."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import struct
|
|
import subprocess
|
|
|
|
from checkpoint_script_state import EXE_SHA256, find_case, read_va, rel_call_target
|
|
from pe_inspect import PE
|
|
|
|
REA_ID = 'ev_0d64cc58fa8f5573fc8e61e0f2c20e36288099212bd2257d3a3121361fd6c904'
|
|
RANGES = (
|
|
(0x424600, 0x425110), (0x425120, 0x42516f),
|
|
(0x425170, 0x425488), (0x425490, 0x4255b4),
|
|
(0x4255c0, 0x425862), (0x42aac0, 0x42aaf2),
|
|
(0x409f60, 0x409f80), (0x458960, 0x458985),
|
|
(0x471a10, 0x471a18), (0x471b40, 0x471b4d),
|
|
(0x471f10, 0x471f3a), (0x472300, 0x47232a),
|
|
(0x403a10, 0x403a18), (0x403700, 0x403723),
|
|
(0x406590, 0x406645),
|
|
)
|
|
SLOTS = (
|
|
(0x4a3bdc, 0x208, 0x424d10), (0x4a3bdc, 0x1a4, 0x425170),
|
|
(0x4a3bdc, 0x1a8, 0x4252e0), (0x4a3bdc, 0x194, 0x42aac0),
|
|
(0x4a3bdc, 0x1c8, 0x424e80), (0x4a3bdc, 0x1b4, 0x4255c0),
|
|
(0x4a3bdc, 0x1b0, 0x425490), (0x4a3bdc, 0x1e0, 0x425120),
|
|
(0x4a373c, 0x218, 0x458960), (0x4a373c, 0x264, 0x471b40),
|
|
(0x4a373c, 0x2bc, 0x471f10), (0x4a373c, 0x310, 0x472300),
|
|
(0x4a373c, 0x314, 0x472360), (0x4a373c, 0x318, 0x472330),
|
|
(0x4a373c, 0x2c0, 0x471f70), (0x4a3bdc, 0xfc, 0x40dd40),
|
|
(0x4a3bdc, 0x114, 0x40e4a0), (0x48d414, 0x4a8, 0x403a10),
|
|
(0x48d414, 0x4c4, 0x406590), (0x48d414, 0x488, 0x403700),
|
|
(0x48d414, 0x460, 0x4033f0),
|
|
)
|
|
SITES = (
|
|
(0x424721, 'a081814f00', 'menu lock load'),
|
|
(0x424732, '663d0200', 'menu state2 comparison'),
|
|
(0x424736, '0f85ca050000', 'locked other state returns'),
|
|
(0x424744, 'ff9060040000', 'locked state2 menu callback'),
|
|
(0x424767, 'ff9208020000', 'objective helper before controller'),
|
|
(0x424bfc, 'ff92a4010000', 'rising helper before controller'),
|
|
(0x424c08, 'ff90a8010000', 'shadow helper before controller'),
|
|
(0x424c2b, 'ff9094010000', 'airborne sound cleanup'),
|
|
(0x424c3f, 'ff92c8010000', 'post-controller Invisible helper'),
|
|
(0x424c88, 'ff92b4010000', 'post-controller impact helper'),
|
|
(0x424c94, 'ff90b0010000', 'post-controller root history helper'),
|
|
(0x425521, 'ff9214030000', 'history root setter'),
|
|
(0x425142, '668986c4070000', 'ceiling fall mode remains1'),
|
|
(0x424ea6, '0f8564020000', 'non-Invisible upkeep return'),
|
|
)
|
|
|
|
|
|
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('Output must be separate from originals')
|
|
pe = PE(find_case(original, 'Neutron.exe'))
|
|
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
|
|
raise ValueError('Unexpected source EXE')
|
|
record = next(r for r in json.loads(Path(rea).read_text())['records']
|
|
if r['evidence_id'] == REA_ID)
|
|
if record['subject']['digest']['sha256'] != EXE_SHA256:
|
|
raise ValueError('REA subject identity mismatch')
|
|
for table, slot, target in SLOTS:
|
|
if struct.unpack('<I', read_va(pe, table+slot, 4))[0] != target:
|
|
raise ValueError(f'Virtual binding {table:#x}+{slot:#x}')
|
|
for address, expected, name in SITES:
|
|
if read_va(pe, address, len(bytes.fromhex(expected))).hex() != expected:
|
|
raise ValueError(name)
|
|
if rel_call_target(pe, 0x424c34) != 0x437940:
|
|
raise ValueError('Controller direct call')
|
|
rows = []
|
|
for line in record['raw_result']['stdout'].splitlines():
|
|
match = re.match(r'\s*([0-9a-f]+):', line)
|
|
if match:
|
|
rows.append((int(match[1], 16), line))
|
|
targets, excerpts = set(), {}
|
|
for a, b in RANGES:
|
|
excerpts[hex(a)] = '\n'.join(line for va, line in rows if a <= va < b)+'\n'
|
|
if not excerpts[hex(a)].strip():
|
|
raise ValueError('Missing retained REA range')
|
|
asm = subprocess.run(['objdump', '-d', '-Mintel', f'--start-address={a}',
|
|
f'--stop-address={b}', str(pe.path)],
|
|
capture_output=True, text=True, check=True).stdout
|
|
targets.update(int(v, 16) for v in re.findall(r'ds:0x([0-9a-f]+)', asm))
|
|
targets.update(int(v, 16) for v in re.findall(r'(?:push|mov\s+\w+,)\s*0x([0-9a-f]+)', asm)
|
|
if 0x48d000 <= int(v, 16) < 0x699000)
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
source = Path(__file__).with_name('jimmy_derived_frame_probe.c').read_text()
|
|
source = source.replace('/* RELOCATIONS */', ','.join(hex(v) for v in sorted(targets)))
|
|
source = source.replace('/* RANGES */', ','.join('{'+hex(a)+','+hex(b)+'}' for a,b in RANGES))
|
|
(output/'jimmy_derived_frame_probe.c').write_text(source)
|
|
(output/'grappler_scalar_probe.c').write_text(Path(__file__).with_name('grappler_scalar_probe.c').read_text())
|
|
result = dict(source_sha256=EXE_SHA256, rea_evidence_id=REA_ID,
|
|
slots=[dict(table=hex(t), slot=hex(s), target=hex(v), file_offset=hex(pe.offset(t+s-pe.base)))
|
|
for t,s,v in SLOTS],
|
|
sites=[dict(va=hex(a), file_offset=hex(pe.offset(a-pe.base)), bytes=e, name=n) for a,e,n in SITES],
|
|
controller_call=dict(va='0x424c34', target='0x437940'),
|
|
ranges=[dict(va=hex(a), file_offset=hex(pe.offset(a-pe.base)), length=b-a,
|
|
sha256=hashlib.sha256(read_va(pe,a,b-a)).hexdigest()) for a,b in RANGES],
|
|
relocations=[hex(v) for v in sorted(targets)], rea_extracts=excerpts,
|
|
scope='Derived caller with recorded controller and no-hit queries; no whole physics/gameplay.')
|
|
(output/'source.json').write_text(json.dumps(result, indent=2)+'\n')
|
|
return result
|
|
|
|
|
|
def observe(output, capture):
|
|
output, capture = Path(output), Path(capture)
|
|
if json.loads((capture/'capture.json').read_text())['captures'][-1]['wineExit'] != 0:
|
|
raise ValueError('Probe exit was not zero')
|
|
log = (capture/'wine.log').read_text()
|
|
if 'JIMMY_DERIVED_FRAME_COMPLETE' not in log or 'DERIVED_FRAME_CASES 50' not in log:
|
|
raise ValueError('Missing full completion marker')
|
|
cases = [dict(word.split('=',1) for word in line.split()[1:])
|
|
for line in log.splitlines() if line.startswith('FRAME_CASE ')]
|
|
if len(cases) != 50:
|
|
raise ValueError('Incomplete frame matrix')
|
|
for row in cases:
|
|
ticks, lock, state, rising = (int(row[k]) for k in ('ticks','lock','state','rising'))
|
|
ran = 0 if lock else ticks
|
|
if int(row['controllers']) != ran or int(row['up']) != (ran if rising else 0) or int(row['down']) != ran:
|
|
raise ValueError('Controller/query dispatch')
|
|
if int(row['closed']) != (ticks if lock and state==2 else 0):
|
|
raise ValueError('Menu transition dispatch')
|
|
if row['mode'] != '1' or abs(float(row['turn'])-11.666668)>1e-6:
|
|
raise ValueError('Movement state changed outside controller')
|
|
if row['current'] != '13,155,17' or row['target'] != '-40,154,-12':
|
|
raise ValueError('Euler changed outside controller')
|
|
paths = [output/'probe.exe', output/'jimmy_derived_frame_probe.c', output/'source.json',
|
|
capture/'wine.log', capture/'capture.json', capture/'game/Neutron.exe']
|
|
if hashlib.sha256(paths[-1].read_bytes()).hexdigest() != EXE_SHA256:
|
|
raise ValueError('Executed mapped source copy identity')
|
|
result = dict(cases=cases, source_sha256=EXE_SHA256, rea_evidence_id=REA_ID,
|
|
provenance={str(p):hashlib.sha256(p.read_bytes()).hexdigest() for p in paths},
|
|
scope='50 derived-caller cases; recorded controller, no-hit world queries and UI adapters.')
|
|
(output.parent/'native-observations.json').write_text(json.dumps(result, indent=2)+'\n')
|
|
return dict(cases=len(cases), wine_exit=0)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('original', type=Path)
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
parser.add_argument('--rea', type=Path, required=True)
|
|
parser.add_argument('--capture', type=Path)
|
|
args = parser.parse_args()
|
|
result = generate(args.original, args.output, args.rea)
|
|
if args.capture:
|
|
result['observations'] = observe(args.output, args.capture)
|
|
print(json.dumps({k:v for k,v in result.items() if k != 'rea_extracts'}, indent=2))
|