Files
Neutron/tools/binary/retained_companion.py
T

158 lines
7.7 KiB
Python

#!/usr/bin/env python3
"""Read-only Goddard/shadow retained-load callback evidence; no source execution."""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
import struct
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent))
from checkpoint_script_state import EXE_SHA256, find_case, read_va
from pe_inspect import PE
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'levels'))
import gam
SITES = (
('goddard_complete_table', 0x41c866, 'c70620f94900'),
('goddard_interface_table', 0x41c883, 'c70780f44900'),
('goddard_retention', 0x41c99b, '538bcfe89d450500'),
('goddard_reuse', 0x41c9c0, 'c686c900000001'),
('goddard_init_bridge', 0x41e6a0, 'e90b98feff'),
('actor_init_base', 0x407eb3, 'e838090500'),
('actor_init_active', 0x407eb8, 'c6460a01'),
('actor_init_state', 0x407ebc, '8b867c080000898608060000'),
('actor_init_waypoint', 0x407ec8, 'c7860008000000000000'),
('goddard_postload_base', 0x41e593, 'e8e8aefeff'),
('actor_postload_base', 0x409486, 'e825530000'),
('target_tag_lookup', 0x40948b, '8d86ac06000050e8d9ab0600'),
('target_pointer_store', 0x40949a, '898600060000'),
('target_id_store', 0x4094ae, '898e10060000'),
('target_root_x', 0x4094c9, '898e28080000'),
('target_root_y', 0x4094e5, '898e2c080000'),
('target_root_z', 0x4094fd, '898e30080000'),
('state_snapshot', 0x409503, '8b96080600008d8e40ffffff89967c080000'),
('state_virtual_dispatch', 0x409515, '8b96080600008b0152ff9024010000'),
('state_previous_store', 0x4089f8, '898ecc0600008986c8060000'),
('state_same_gate', 0x408a18, '3bc10f846e010000'),
('authored_rotation_get', 0x40952d, 'ff9070010000'),
('logical_rotation_set', 0x409552, 'ff922c030000'),
('rotation_cache_start', 0x409569, '81c6740200008916'),
('rotation_cache_end', 0x409580, '89460c5e83c410c3'),
('normal_range', 0x41e598, 'c786180900000080bb44'),
('hide_lv4a', 0x41e5ad, '3d4134564c'),
('hide_lv1d', 0x41e5ba, '3d4431564c'),
('hide_lv1c', 0x41e5c1, '3d4331564c'),
('hide_lev6', 0x41e5cc, '3d3656454c'),
('hide_lev7', 0x41e5d3, '3d3756454c'),
('hide_lv6a', 0x41e5e0, '3d4136564c'),
('special_vr06', 0x41e5e7, '3d36305256'),
('tutorial6_range', 0x41e5ee, 'c7861809000000c05a45'),
('normal_enable', 0x41e618, '6a018bceff90d0000000'),
('normal_collision_enable', 0x41e624, '6a018bceff9214020000'),
('normal_submode_zero', 0x41e637, '6a008bcfff927c010000'),
('normal_radius130', 0x41e64a, '68000002438bceff9010010000'),
('normal_flag928', 0x41e657, 'c6862809000000'),
('hidden_disable', 0x41e674, '6a008bceff90d0000000'),
('hidden_collision_disable', 0x41e680, '6a008bceff9214020000'),
('hidden_flag928', 0x41e68a, 'c6862809000001'),
('companion_pointer_branch', 0x423c6a, '8b855c09000085c07560'),
('companion_ctor', 0x423c98, 'e8738bffff'),
('companion_reused_zero_rotation', 0x423cd4, '8d88c00000006a006a006a008b01ff9074010000'),
('rotation_set_authored_y', 0x4716df, '898e00010000'),
('rotation_set_authored_z', 0x4716e7, '899604010000'),
('rotation_set_authored_x', 0x4716f6, '8986fc000000'),
('rotation_set_logical', 0x471702, 'ff9030030000'),
('rotation_render_sync', 0x47170c, 'ff92e0030000'),
('logical_euler_set_x', 0x472428, '898124020000'),
('render_sync_bridge', 0x4624f0, 'e94b100100'),
('submode_zero_vector_x', 0x41cc73, 'a100824f008981d4060000'),
('submode_zero_vector_y', 0x41cc7e, 'a104824f008981d8060000'),
('submode_zero_vector_z', 0x41cc89, 'a108824f008981dc060000'),
('submode_zero_state', 0x41cc94, '668991a0090000'),
('shadow_complete_table', 0x43f6a0, 'c7031c3c4b00'),
('shadow_interface_table', 0x43f6ad, 'c74500b8374b00'),
('shadow_postload_base', 0x43f814, 'e8a75cffff'),
('shadow_authored_size', 0x43f819, '0fbf86220500000fbf9620050000'),
('shadow_runtime_size', 0x43f83e, 'd999ac000000d999b0000000'),
('shadow_render_flags', 0x43f84a, 'c781b400000002000000c781b800000000000000'),
('shadow_authored_db', 0x4354c3, '8d86bc04000050e8f1570300'),
('shadow_resource_attach', 0x4354f5, 'ff92c8000000'),
('shadow_resource_flags', 0x435503, 'b8010000008d4e24894660894664'),
('shadow_fallback_db', 0x435518, '68c0024f00e89e570300'),
('shadow_fallback_attach', 0x43553f, '6a0350ff92c8000000'),
)
VTABLES = (
('Goddard Init', 0x49f480, 0x28, 0x41e6a0),
('Goddard PostLoad', 0x49f480, 0x40c, 0x41e590),
('Goddard authored Euler getter', 0x49f480, 0x170, 0x4716a0),
('Goddard authored Euler setter', 0x49f480, 0x174, 0x4716d0),
('Goddard logical Euler vector setter', 0x49f480, 0x32c, 0x472450),
('Goddard logical Euler xyz setter', 0x49f480, 0x330, 0x472420),
('Goddard rotation sync', 0x49f480, 0x3e0, 0x4624f0),
('Goddard state dispatch', 0x49f920, 0x124, 0x4089b0),
('Goddard submode dispatch', 0x49f920, 0x17c, 0x41cc30),
('Goddard radius setter', 0x49f480, 0x110, 0x471240),
('Goddard live radius setter', 0x49f480, 0x264, 0x471b40),
('Goddard collision flag', 0x49f480, 0x214, 0x471a00),
('Shadow Init', 0x4b37b8, 0x28, 0x4587f0),
('Shadow PostLoad', 0x4b37b8, 0x40c, 0x43f810),
('Shadow resource attach', 0x4b3c1c, 0xc8, 0x464430),
)
HIDDEN_CODES = frozenset(('LEV6', 'LEV7', 'LV1C', 'LV1D', 'LV4A', 'LV6A'))
def level_policy(code: str):
"""Bounded interpretation of41E5A2..41E692, not execution of original code.
Hidden path does not assign a new radius/submode; None explicitly means
untouched by this local branch, not zero or a preservation proof elsewhere.
"""
if len(code) != 4 or not code.isascii():
raise ValueError('Original level codes have four ASCII characters')
hidden = code in HIDDEN_CODES
return dict(hidden=hidden, range=3500 if code == 'VR06' else 1500,
collision_enabled=not hidden, radius=None if hidden else 130,
submode=None if hidden else 0, flag928=int(hidden))
def collect(root: Path):
pe = PE(find_case(root, 'Neutron.exe'))
digest = hashlib.sha256(pe.data).hexdigest()
sites = []
for name, va, expected in SITES:
actual = read_va(pe, va, len(bytes.fromhex(expected))).hex()
sites.append(dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)),
expected=expected, actual=actual, matches=actual == expected))
tables = []
for name, table, slot, expected in VTABLES:
actual = struct.unpack('<I', read_va(pe, table + slot, 4))[0]
tables.append(dict(name=name, table=hex(table), slot=hex(slot),
file_offset=hex(pe.offset(table+slot-pe.base)),
target=hex(actual), matches=actual == expected))
route = []
for name in ('level1b.gam', 'VR04.gam', 'level1.gam'):
path = find_case(root, 'gam/' + name)
raw = path.read_bytes()
level = gam.parse(raw)
route.append(dict(file=name, code=level.code, sha256=hashlib.sha256(raw).hexdigest(),
dog_records=[hex(e.offset) for e in level.entities if e.kind == '3DOG'],
shadow_records=[hex(e.offset) for e in level.entities if e.kind == '3SHA'],
policy=level_policy(level.code)))
return dict(known_binary=digest == EXE_SHA256, sha256=digest,
sites=sites, vtables=tables, route=route,
scope='Static source bytes/data and bounded interpretation only; no original execution.')
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
result = collect(cli.parse_args().original)
print(json.dumps(result, indent=2))
if not result['known_binary'] or not all(x['matches'] for k in ('sites', 'vtables') for x in result[k]):
raise SystemExit(1)