Recover ordered physical pair collision lifecycle
This commit is contained in:
1 parent
27d91ad697
commit
924ad2f67b
17 files changed
+815
-1
No files matched your search
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Guard the original ordered pair pass; generate a copied-code contact probe."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import struct
|
||||
|
||||
from checkpoint_script_state import EXE_SHA256, find_case, read_va
|
||||
from pe_inspect import PE
|
||||
import sys
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'levels'))
|
||||
from gam import parse
|
||||
|
||||
REA_ID = 'ev_0d64cc58fa8f5573fc8e61e0f2c20e36288099212bd2257d3a3121361fd6c904'
|
||||
RANGES = (
|
||||
(0x47a430, 0x47a5de, '68ff890cd3f3526f8d4dd936b72748fdeb549cc97b56068ccc75c23df2c81ae6'),
|
||||
(0x47b3e0, 0x47b4a1, '008cc7fa18ca1b59567f4388928045dcdfc9ac85d25dce277d2270378b18f5df'),
|
||||
(0x47ca90, 0x47caba, '1d5618a99b80fec594c14a36b837e10456790bc44cac4306f8f7e66fd3a757b2'),
|
||||
(0x470a60, 0x470a88, '6fd5f554e28bd9d733f561a1e755689f11d86997c2fda2ccefdbeab6946fe9f3'),
|
||||
(0x470a90, 0x470b19, 'bfc56f226eb97077379bbb4f273ae47f0c0c1f97a1ec335996435a5c4f650038'),
|
||||
(0x470b20, 0x470bb8, 'a84cc049930fd1301bb89a6a84255ebc4fe9515e26ce1d7be75c743574e31202'),
|
||||
(0x470bc0, 0x470bea, '09bfd61be8128cdbf9c1f1aed7a416cd5ae3c735abf588106d17d744a233951a'),
|
||||
(0x470bf0, 0x470c44, '5211f48735716c7a7426486f0b44ae9e93388546d825437edc6ef3f787e2aa8d'),
|
||||
)
|
||||
TARGETS = (0x4f69e4, 0x695d3c, 0x695d47, 0x4f6b08, 0x48d104, 0x509948)
|
||||
TABLES = {'3JIM': 0x4a373c, '3ROC': 0x4b25ac, '3SOL': 0x4c19c4, '3TRO': 0x4be404}
|
||||
COMMON = {0x178: 0x471720, 0x210: 0x4719f0, 0x220: 0x471a30,
|
||||
0x3a0: 0x472f80, 0x4c: 0x470bf0}
|
||||
|
||||
|
||||
def collect(original):
|
||||
original = Path(original)
|
||||
pe = PE(find_case(original, 'Neutron.exe'))
|
||||
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
|
||||
raise ValueError('Original identity mismatch')
|
||||
ranges, relocations, bindings = [], [], []
|
||||
for start, end, digest in RANGES:
|
||||
code = read_va(pe, start, end-start)
|
||||
if hashlib.sha256(code).hexdigest() != digest:
|
||||
raise ValueError(f'Changed source body {start:#x}')
|
||||
ranges.append(dict(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(code)-3):
|
||||
if code[index:index+4] == encoded:
|
||||
relocations.append(dict(va=hex(start+index), target=hex(target)))
|
||||
for kind, table in TABLES.items():
|
||||
for slot, expected in COMMON.items():
|
||||
if struct.unpack('<I', read_va(pe, table+slot, 4))[0] != expected:
|
||||
raise ValueError('Changed actor binding')
|
||||
bindings.append(dict(kind=kind, table=hex(table), slot=hex(slot), target=hex(expected),
|
||||
file_offset=hex(pe.offset(table+slot-pe.base))))
|
||||
gam_path = find_case(original, 'gam/VR04.gam')
|
||||
candidates = []
|
||||
for entity in parse(gam_path.read_bytes()).entities:
|
||||
if entity.kind in TABLES:
|
||||
values = entity.values()
|
||||
candidates.append(dict(kind=entity.kind, tag=values['ObjectTag'], file_offset=hex(entity.offset),
|
||||
has_collision=values['HasCollision'], can_move=values['CanMove'],
|
||||
task=values['TaskName'], required_level=values['RequiredLevel'],
|
||||
table=hex(TABLES[entity.kind])))
|
||||
return dict(source_sha256=EXE_SHA256,
|
||||
omt_sha256=hashlib.sha256(find_case(original, 'OMT2.dll').read_bytes()).hexdigest(),
|
||||
ranges=ranges, relocations=relocations,
|
||||
bindings=bindings, vr04_sha256=hashlib.sha256(gam_path.read_bytes()).hexdigest(),
|
||||
vr04_candidates=candidates)
|
||||
|
||||
|
||||
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)
|
||||
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 mismatch')
|
||||
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))
|
||||
for entry, (start, end, _) in zip(report['ranges'], RANGES):
|
||||
excerpt = '\n'.join(line for va, line in rows if start <= va < end)
|
||||
if not excerpt:
|
||||
raise ValueError('Missing retained REA instructions')
|
||||
entry['rea_extract'] = excerpt
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
probe = Path(__file__).with_name('pair_lifecycle_probe.c').read_text()
|
||||
probe = probe.replace('/* RELOCATIONS */', ','.join('{%s,%s}' % (row['va'], row['target']) for row in report['relocations']))
|
||||
(output/'pair_lifecycle_probe.c').write_text(probe)
|
||||
(output/'grappler_scalar_probe.c').write_text(Path(__file__).with_name('grappler_scalar_probe.c').read_text())
|
||||
report.update(rea_evidence_id=REA_ID,
|
||||
scope='Copied complete pair/sphere/exclusion/base-contact routines; synthetic actors/permissions/response and box adapters, not whole original gameplay.')
|
||||
(output/'source.json').write_text(json.dumps(report, indent=2)+'\n')
|
||||
return dict(source_sha256=EXE_SHA256, guarded_ranges=len(report['ranges']),
|
||||
relocated_operands=len(report['relocations']), guarded_bindings=len(report['bindings']))
|
||||
|
||||
|
||||
def capture(output, directory):
|
||||
directory, output = Path(directory), Path(output)
|
||||
metadata = json.loads((directory/'capture.json').read_text())
|
||||
log = (directory/'wine.log').read_text()
|
||||
if metadata['captures'][-1]['wineExit'] != 0 or 'PAIR_LIFECYCLE_COMPLETE' not in log:
|
||||
raise ValueError('Incomplete probe')
|
||||
cases = [json.loads(line[5:]) for line in log.splitlines() if line.startswith('PAIR ')]
|
||||
if len(cases) != 28 or len({c['name'] for c in cases}) != 28:
|
||||
raise ValueError('Missing or duplicate case')
|
||||
source = json.loads((output/'source.json').read_text())
|
||||
for filename, field in (('Neutron.exe', 'source_sha256'), ('OMT2.dll', 'omt_sha256')):
|
||||
if hashlib.sha256(find_case(directory/'game', filename).read_bytes()).hexdigest() != source[field]:
|
||||
raise ValueError('Copied original identity mismatch')
|
||||
identity = json.loads((directory/'probe-identity.json').read_text())
|
||||
if identity['sha256'] != hashlib.sha256((output/'probe.exe').read_bytes()).hexdigest():
|
||||
raise ValueError('Observed probe binary identity mismatch')
|
||||
files = [directory/'wine.log', directory/'capture.json', directory/'probe-identity.json',
|
||||
output/'source.json', output/'pair_lifecycle_probe.c', output/'probe.exe']
|
||||
report = dict(cases=cases, provenance={str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in files},
|
||||
scope='Isolated original component execution under Wine32; no original entrypoint, constructor or real class callback.')
|
||||
(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.capture or args.rea):
|
||||
cli.error('--output needs --rea for generation, or --capture for verification')
|
||||
print(json.dumps(capture(args.output, args.capture) if args.capture else
|
||||
generate(args.original, args.output, args.rea) if args.output else collect(args.original), indent=2))
|
||||
@@ -0,0 +1,126 @@
|
||||
/* Complete original pair pass, sphere query, exclusions and base contact list.
|
||||
Synthetic actors/permission/response callbacks and box-result adapter. */
|
||||
#define main unused_scalar_main
|
||||
#include "grappler_scalar_probe.c"
|
||||
#undef main
|
||||
typedef struct {float x,y,z,w;} Vec;
|
||||
typedef struct Node {struct Node *next,*previous;void *value;} Node;
|
||||
typedef struct Actor {
|
||||
unsigned char raw[0x500];uintptr_t table[0x440/4];
|
||||
Node contacts,exclusions;Vec root;float radius;int id,enabled,mode,allowed,integration,authored,inhibit;
|
||||
} Actor;
|
||||
static Actor actors[3];static Node head,nodes[4];
|
||||
static char events[4096];static unsigned used;static int mutation,box_hit=1;
|
||||
typedef void (TC *Contact)(Actor*,Actor*);
|
||||
static void event(char kind,Actor*a,Actor*b){
|
||||
require(used+20<sizeof(events),"event buffer");
|
||||
used+=(unsigned)snprintf(events+used,sizeof(events)-used,"%c%d%d;",kind,a->id,b->id);
|
||||
}
|
||||
static void list_init(Node*n){n->next=n->previous=n;n->value=NULL;}
|
||||
static void append(Node*list,Actor*a){
|
||||
Node*n=calloc(1,sizeof(*n));require(n!=NULL,"node");n->value=a;
|
||||
n->next=list;n->previous=list->previous;list->previous->next=n;list->previous=n;
|
||||
}
|
||||
static void clear(Node*n){while(n->next!=n){Node*p=n->next;n->next=p->next;free(p);}list_init(n);}
|
||||
static int TC enabled(Actor*a){return a->enabled;}
|
||||
static int TC mode(Actor*a){return a->mode;}
|
||||
static int TC allows(Actor*a,Actor*b){(void)b;return a->allowed;}
|
||||
static float TC radius(Actor*a){return a->radius;}
|
||||
static Vec*TC root(Actor*a,Vec*out){*out=a->root;return out;}
|
||||
static int TC integration(Actor*a){return a->integration;}
|
||||
static int TC authored(Actor*a){return a->authored;}
|
||||
static void TC set_integration(Actor*a,int v){a->integration=v;}
|
||||
static int TC inhibit(Actor*a){return a->inhibit;}
|
||||
static int TC no(Actor*a){(void)a;return 0;}
|
||||
static void TC response(Actor*a,Actor*b,float distance){
|
||||
require(distance>=0,"distance");event('P',a,b);
|
||||
if(mutation==1&&a->id==0)a->root.x=1000;
|
||||
}
|
||||
static void TC overlap(Actor*a,Actor*b,float distance){
|
||||
event('R',a,b);((void(TC*)(Actor*,Actor*,float))AT(0x470bf0))(a,b,distance);
|
||||
}
|
||||
static void TC enter(Actor*a,Actor*b){
|
||||
event('E',a,b);((Contact)AT(0x470a90))(a,b);
|
||||
if(a->id==0&&b->id==1){
|
||||
if(mutation==2)a->enabled=0;
|
||||
if(mutation==3)actors[2].enabled=0;
|
||||
if(mutation==4)append(&b->contacts,a);
|
||||
}
|
||||
}
|
||||
static void TC leave(Actor*a,Actor*b){event('L',a,b);((Contact)AT(0x470b20))(a,b);}
|
||||
static void TC stay(Actor*a,Actor*b){event('S',a,b);((Contact)AT(0x470bc0))(a,b);}
|
||||
static int box(Actor*a,Actor*b){event('B',a,b);return box_hit;}
|
||||
static void*allocate(size_t size){require(size==12,"source contact allocation");return malloc(size);}
|
||||
static void release(void*p){free(p);}
|
||||
static void reset(void){
|
||||
for(int i=0;i<3;i++){
|
||||
if(actors[i].contacts.next){clear(&actors[i].contacts);clear(&actors[i].exclusions);}
|
||||
Actor*a=&actors[i];memset(a,0,sizeof(*a));a->id=i;a->enabled=a->allowed=a->integration=1;a->radius=10;
|
||||
list_init(&a->contacts);list_init(&a->exclusions);
|
||||
*(void**)a=a->table;*(void**)(a->raw+0x260)=&a->contacts;*(void**)(a->raw+0x254)=&a->exclusions;
|
||||
#define SLOT(s,f) a->table[(s)/4]=(uintptr_t)(f)
|
||||
SLOT(0x220,enabled);SLOT(0x178,mode);SLOT(0x3a0,allows);SLOT(0x260,radius);SLOT(0x310,root);
|
||||
SLOT(0x3c,AT(0x470a60));SLOT(0x40,enter);SLOT(0x44,leave);SLOT(0x48,stay);SLOT(0x4c,overlap);
|
||||
SLOT(0x210,integration);SLOT(0xb4,authored);SLOT(0x214,set_integration);SLOT(0x1e0,inhibit);
|
||||
SLOT(0x1f8,no);SLOT(0x414,response);
|
||||
#undef SLOT
|
||||
}
|
||||
list_init(&head);mutation=0;box_hit=1;*(unsigned char*)AT(0x4f69e4)=1;
|
||||
}
|
||||
static void order(int a,int b,int c){
|
||||
list_init(&head);int ids[]={a,b,c};
|
||||
for(int i=0;i<3&&ids[i]>=0;i++){
|
||||
Node*n=&nodes[i];n->value=&actors[ids[i]];n->next=&head;n->previous=head.previous;
|
||||
head.previous->next=n;head.previous=n;
|
||||
}
|
||||
*(void**)AT(0x695d3c)=&head;
|
||||
}
|
||||
static void tick(const char*name){
|
||||
used=0;events[0]=0;((void(__cdecl*)(float))AT(0x47a430))(1.f/60);
|
||||
printf("PAIR {\"name\":\"%s\",\"events\":\"%s\",\"contacts\":[",name,events);
|
||||
for(int i=0;i<3;i++){
|
||||
unsigned mask=0;for(Node*n=actors[i].contacts.next;n!=&actors[i].contacts;n=n->next)mask|=1u<<((Actor*)n->value)->id;
|
||||
printf("%s%u",i?",":"",mask);
|
||||
}
|
||||
printf("],\"integration\":[%d,%d,%d],\"enabled\":[%d,%d,%d],\"rootX\":[%.9g,%.9g,%.9g]}\n",
|
||||
actors[0].integration,actors[1].integration,actors[2].integration,
|
||||
actors[0].enabled,actors[1].enabled,actors[2].enabled,actors[0].root.x,actors[1].root.x,actors[2].root.x);
|
||||
}
|
||||
int main(void){
|
||||
setbuf(stdout,NULL);map_source();
|
||||
static const struct{unsigned va,target;} relocations[]={/* RELOCATIONS */};
|
||||
for(unsigned i=0;i<sizeof(relocations)/sizeof(*relocations);i++){
|
||||
uint32_t value=(uintptr_t)AT(relocations[i].target);memcpy(AT(relocations[i].va),&value,4);
|
||||
}
|
||||
HMODULE omt=LoadLibraryA("Z:\\work\\game\\OMT2.dll");
|
||||
if(!omt)fprintf(stderr,"OMT loader error=%lu\n",GetLastError());
|
||||
require(omt!=NULL,"copied OMT");
|
||||
FARPROC quick=GetProcAddress(omt,"?quick_magnitude@OMedia3DVector@@QBEMXZ");require(quick!=NULL,"quick magnitude");
|
||||
*(void**)AT(0x48d104)=quick;jump(AT(0x478990),allocate);jump(AT(0x4789a0),release);jump(AT(0x47a5e0),box);
|
||||
reset();order(0,1,-1);tick("enter");tick("stay");actors[1].root.x=20;tick("tangent_leave");
|
||||
actors[0].integration=0;actors[0].authored=1;actors[1].root.x=19;tick("reenter_sleeping");
|
||||
actors[1].root.x=21;tick("leave_wakes_authored");
|
||||
reset();order(0,1,-1);append(&actors[0].exclusions,&actors[1]);tick("earlier_excludes");
|
||||
reset();order(0,1,-1);append(&actors[1].exclusions,&actors[0]);tick("later_excludes");
|
||||
order(1,0,-1);tick("reordered_exclusion_leaves");
|
||||
reset();order(0,1,-1);tick("before_disable");actors[1].enabled=0;tick("disabled_retains_contact");
|
||||
actors[1].enabled=1;actors[0].allowed=0;tick("permission_retains_contact");
|
||||
actors[0].allowed=1;*(unsigned char*)AT(0x4f69e4)=0;tick("global_off_retains_contact");
|
||||
reset();order(0,1,-1);actors[1].allowed=0;tick("later_permission_rejects");
|
||||
reset();order(0,1,-1);actors[1].root.x=19.999f;tick("inside");
|
||||
reset();order(0,1,-1);actors[1].root.x=20.001f;tick("outside");
|
||||
reset();order(0,1,2);tick("three_actor_order");
|
||||
reset();order(0,1,2);mutation=1;tick("response_moves_root");
|
||||
reset();order(0,1,2);mutation=2;tick("enter_disables_outer");
|
||||
reset();order(0,1,2);mutation=3;tick("enter_disables_later");
|
||||
reset();order(0,1,-1);mutation=4;tick("enter_changes_counterpart_membership");
|
||||
reset();order(0,0,1);tick("duplicate_body_identity");
|
||||
reset();order(0,1,-1);actors[1].inhibit=1;tick("other_inhibits_response");
|
||||
reset();order(0,1,-1);actors[0].integration=0;tick("sleeping_still_contacts");
|
||||
reset();order(0,1,-1);actors[0].mode=2;tick("box_first");
|
||||
reset();order(0,1,-1);actors[1].mode=2;tick("box_second");
|
||||
reset();order(0,1,-1);actors[0].mode=actors[1].mode=2;tick("both_box_keep_order");
|
||||
box_hit=0;tick("box_separation");
|
||||
reset();order(0,-1,-1);tick("single_actor");
|
||||
puts("PAIR_LIFECYCLE_COMPLETE");return 0;
|
||||
}
|
||||
Reference in new issue
Block a user