Recover counted MAC-format loops in ATERM association request
This commit is contained in:
@@ -1139,6 +1139,38 @@ int ATERMParsePacket(AtermPacket* packet, u32* setupType) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline s32 atermFormatHexByte(char* output, u8 byte) {
|
||||
s32 nibbles[2];
|
||||
s32 index;
|
||||
char* encoded = output;
|
||||
nibbles[0] = (byte & 0xF0) >> 4;
|
||||
nibbles[1] = byte & 0xF;
|
||||
for (index = 0; index < 2; index++) {
|
||||
s32 nibble = nibbles[index];
|
||||
if (nibble <= 9) {
|
||||
*encoded++ = nibble + '0';
|
||||
} else {
|
||||
*encoded++ = nibble + '7';
|
||||
}
|
||||
}
|
||||
*encoded = 0;
|
||||
return encoded - output;
|
||||
}
|
||||
|
||||
static inline void atermFormatMacAddress(char* text, const u8* address) {
|
||||
const u8* input = address;
|
||||
char* output = text;
|
||||
s32 index;
|
||||
for (index = 0; index < 6; index++) {
|
||||
u8 byte = *input++;
|
||||
output += atermFormatHexByte(output, byte);
|
||||
if (index < 5) {
|
||||
*output++ = ':';
|
||||
}
|
||||
}
|
||||
*output = 0;
|
||||
}
|
||||
|
||||
int ATERMBuildAssociationRequest(AtermAssociationRequest* request) {
|
||||
u8 scanAddress[8];
|
||||
u8 interfaceMacAddress[8];
|
||||
@@ -1160,66 +1192,8 @@ int ATERMBuildAssociationRequest(AtermAssociationRequest* request) {
|
||||
memcpy(request->secondAddress, interfaceMacAddress, 6);
|
||||
}
|
||||
if (gAtermUseSharedAddress != 0) {
|
||||
{
|
||||
const u8* addressCursor = interfaceMacAddress;
|
||||
const u8* addressEnd = interfaceMacAddress + 6;
|
||||
int addressIndex;
|
||||
char* textCursor = interfaceMacText;
|
||||
addressIndex = 0;
|
||||
do {
|
||||
u8 addressByte = *addressCursor++;
|
||||
char* encoded = textCursor;
|
||||
s32 highNibble = (addressByte & 0xF0) >> 4;
|
||||
s32 lowNibble = addressByte & 0xF;
|
||||
if (highNibble <= 9) {
|
||||
*encoded++ = highNibble + '0';
|
||||
} else {
|
||||
*encoded++ = highNibble + '7';
|
||||
}
|
||||
if (lowNibble <= 9) {
|
||||
*encoded++ = lowNibble + '0';
|
||||
} else {
|
||||
*encoded++ = lowNibble + '7';
|
||||
}
|
||||
*encoded = 0;
|
||||
textCursor += encoded - textCursor;
|
||||
if (addressIndex < 5) {
|
||||
*textCursor++ = ':';
|
||||
}
|
||||
addressIndex++;
|
||||
} while (addressCursor < addressEnd);
|
||||
*textCursor = '\0';
|
||||
}
|
||||
{
|
||||
const u8* addressCursor = scanAddress;
|
||||
const u8* addressEnd = scanAddress + 6;
|
||||
int addressIndex;
|
||||
char* textCursor = scanAddressText;
|
||||
addressIndex = 0;
|
||||
do {
|
||||
u8 addressByte = *addressCursor++;
|
||||
char* encoded = textCursor;
|
||||
s32 highNibble = (addressByte & 0xF0) >> 4;
|
||||
s32 lowNibble = addressByte & 0xF;
|
||||
if (highNibble <= 9) {
|
||||
*encoded++ = highNibble + '0';
|
||||
} else {
|
||||
*encoded++ = highNibble + '7';
|
||||
}
|
||||
if (lowNibble <= 9) {
|
||||
*encoded++ = lowNibble + '0';
|
||||
} else {
|
||||
*encoded++ = lowNibble + '7';
|
||||
}
|
||||
*encoded = 0;
|
||||
textCursor += encoded - textCursor;
|
||||
if (addressIndex < 5) {
|
||||
*textCursor++ = ':';
|
||||
}
|
||||
addressIndex++;
|
||||
} while (addressCursor < addressEnd);
|
||||
*textCursor = '\0';
|
||||
}
|
||||
atermFormatMacAddress(interfaceMacText, interfaceMacAddress);
|
||||
atermFormatMacAddress(scanAddressText, scanAddress);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
185
tools/decomp-assist/aterm-association-recovery-20261004.md
Normal file
185
tools/decomp-assist/aterm-association-recovery-20261004.md
Normal file
@@ -0,0 +1,185 @@
|
||||
# ATERM association formatting recovery
|
||||
|
||||
Base main `9aa4a0d88748808c5b1e3a9e62518641a5624a72`, tree
|
||||
`125f757de23bda7b0b8cad78472508a5f94a0506`. One authorized recovered source
|
||||
form in `src/scene/setting/ATERM.c`, function `ATERMBuildAssociationRequest`.
|
||||
No discovery/protocol/AES, header, compiler/configuration or linking changes.
|
||||
|
||||
## Recovered provenance and reason for restoration
|
||||
|
||||
`h2b.attempts.md:240–353` preserves the complete trial61 diff. Applying it to
|
||||
historical34858f3f reproduces whole-source SHA256
|
||||
`6fa4cc5bdf9ec05f63b18be1e148ccdfa3b61438f40c3e87342ae09c33047998`,
|
||||
exactly matching its logged compiled-source identifier6fa4cc5bdf9e at line88.
|
||||
This is exact recovered source provenance, rather than an inferred spelling.
|
||||
The old candidate object and raw sibling hashes are unavailable.
|
||||
|
||||
The log records official98.7594%,133/133 instructions,28 raw differences,
|
||||
unchanged19 exact functions/11584 code/18584 data bytes, empty identical pool
|
||||
and regressions[]. Lines144–174 explicitly restore every nonexact association
|
||||
trial; only exact StartNetworkStack was retained. No separate source-quality
|
||||
or data rejection is recorded for61. The nearby AOSS trial12's data loss and
|
||||
uninitialized guard experiments were not imported. Its final clean gate did
|
||||
not independently accept this discarded association candidate.
|
||||
|
||||
Current association body is byte-identical to the historical baseline. The
|
||||
subsequent real network-global split and exact StartNetworkStack remain intact.
|
||||
Only the archived61 helpers and two formatting uses are recovered. This is a
|
||||
previously tested family, with no claim of new source-form novelty.
|
||||
|
||||
Canonical current patch SHA256:
|
||||
`72757f427659688e0a58a05e6b2efd1e02e2341b1be7754b60bb6d0df85edd73`.
|
||||
Selected whole source SHA256:
|
||||
`e0eafa41540c9424bac5bad7cd0b5d2118473e14bc4830c6215e723d3e00dc12`.
|
||||
|
||||
## Actual target discrepancy and source contract
|
||||
|
||||
Original function814031DC has count6 at+0xC0 and+0x15C, mtctr at+0xD8 and
|
||||
+0x174, and bdnz at+0x154 and+0x1F0. Baseline uses end-pointer cmplw/blt.
|
||||
The recovered nested hex-byte/MAC encoders restore both counted-loop primitives.
|
||||
All133 candidate/target mnemonics, immediates, branches and calls correspond.
|
||||
Exactly28 remaining raw operand differences follow these independently checked
|
||||
region dataflow maps:
|
||||
|
||||
- +0xC0..+0x158: candidate r9→target r5 and candidate r5→target r9
|
||||
- +0x158..+0x1FC: candidate r9→r5, r5→r6, r4→r9 and r6→r4
|
||||
|
||||
Unlisted registers remain identical. These maps explain residual operands;
|
||||
they do not change objects, compiler choices or official scoring. Both pre-
|
||||
format call/control blocks and the epilogue retain identical raw words.
|
||||
OriginalDOL independently verifies120 nonrelocated words and all9 direct-call
|
||||
destinations. No register forcing is used; the function remains nonexact.
|
||||
|
||||
The two nibble entries are initialized and actually read by a bounded encoder
|
||||
loop. No dummy/unused state, artificial guard or initializer bypass is added.
|
||||
For every u8 input0..255, both nibbles are0..15 and produce ASCII48..57 or65..70.
|
||||
Indices0..2/0..6, pointer difference2 and output length17 fit their signed types.
|
||||
The two six-byte inputs remain inside the existing8-byte buffers; the maximum
|
||||
formatted write index17 remains inside each existing32-byte text buffer.
|
||||
|
||||
Each byte load is followed by high digit, low digit and NUL stores. The first
|
||||
five colons overwrite that NUL; the sixth leaves it, followed by finalNUL.
|
||||
Both addresses and every existing pre-format call, argument, request store,
|
||||
metadata read and global-flag guard keep their original order. The arrays
|
||||
remain private; no helper introduces callbacks, escaped addresses or loads
|
||||
from live caller state. The text arrays have no later C consumer, but both
|
||||
already exist and the original actually performs their stores. No new dead
|
||||
output storage is introduced to mimic those stores.
|
||||
|
||||
## Fresh independent build and official measurements
|
||||
|
||||
Reused a clean independent43U leaf/cache with only permitted prior documentation
|
||||
commits differing from main. All1027 cached objects and full raw report were
|
||||
verified against a newly frozen main reference before a fresh ordinary baseline
|
||||
compile. The compile reproduced every object/report byte. Cached tool/wrapper,
|
||||
compiler/configuration/header/source/log context639 pins were verified; tools
|
||||
were not downloaded or modified.
|
||||
|
||||
Exactly3 actual ordinary source compiles: one baseline, one candidate and one
|
||||
selected compile. There were no alternatives, proxy scores, deduplication or
|
||||
adaptive searches. Every compile has immutable source/object bytes, hashes,
|
||||
timestamps, full argv, output and actual compiler evidence.
|
||||
|
||||
- Function official:93.44361→98.7594, a strict+5.31579-point gain
|
||||
- Unit official:97.42845→97.575714
|
||||
- Global official:99.73871→99.73965
|
||||
- Baseline/candidate/selected/target:133 instructions,532 bytes, frame0x60
|
||||
- Exact functions20/26, code12200/19204, data18584/18864: unchanged
|
||||
- The existing280-byte ATERMRun switch-table imperfection remains unchanged
|
||||
- Selected whole object:
|
||||
`feacf87770e385fe70dca985be22c1b93bfd557dea6855d9d60977f506ad31c2`
|
||||
|
||||
OnlyATERM changes among all1027 source objects. Full-report changes are five
|
||||
fuzzy increases; all other fields and1026 complete unit reports are identical.
|
||||
All25 sibling bodies, addresses, extents and full function reports are identical.
|
||||
All.text bytes outside the owned extent, including padding, are identical.
|
||||
All20 existing exact functions retain zero strict instruction differences.
|
||||
|
||||
## Exact metadata and relocation accounting
|
||||
|
||||
ELF header, section order and every section header are identical. All145 raw
|
||||
symbol-table entries retain their exact type, binding, section, value, size and
|
||||
other fields. All raw relocation-table bytes are identical. Allocated nontext
|
||||
bytes are identical. Only owned.text bytes and.strtab spelling bytes change.
|
||||
|
||||
Four concrete compiler-local object names change at fixed symbol indices:
|
||||
|
||||
- 22:@2600→@2652, .data+0, extent40
|
||||
- 23:@2652→@2704, .data+40, extent132
|
||||
- 24:@1043→@1036, .sdata+0, extent7
|
||||
- 25:@2792→@2844, .data+172, extent44
|
||||
|
||||
Each entry and its entire allocated raw extent is independently byte-verified.
|
||||
No blanket prefix stripping or broad name normalization is used. The initial
|
||||
conservative name-sensitive relocation gate flagged three siblings; its failed
|
||||
record remains preserved. Exact four-pair/stable-index/raw-table proof resolves
|
||||
those descriptor-name warnings without weakening the actual relocation gate.
|
||||
|
||||
## Fresh bounded actual-PPC/reference proof
|
||||
|
||||
6656 distinct cases execute the whole actual baseline, candidate and original
|
||||
functions:19968 actual executions per run, plus6656 independent sequential
|
||||
source-reference executions. Worker initial and two fresh standalone-reproduction runs
|
||||
all pass:59904 worker actual executions total. Parent separately reproduced
|
||||
the whole proof from saved objects in its own validation outputs.
|
||||
|
||||
Coverage is133/133 instructions for each of the three objects. Return values,
|
||||
call arguments/order, every memory read/write trace and complete allocated
|
||||
memory state agree with the sequential reference. Stack/LR and all nonvolatile
|
||||
registers are preserved. Candidate's high/low/NUL/colon store ordering agrees
|
||||
exactly with original and baseline.
|
||||
|
||||
Cases include every raw input-byte value independently at every one of six
|
||||
positions of both MAC arrays,64 structured MAC pairs and512 deterministic
|
||||
random pairs. The selected BSSID's first-byte bit1 remains cleared as before.
|
||||
Four valid request layouts cover disjoint storage and contained aliases with
|
||||
addressBuffer, selectedBssid and the shared-address flag. Four NCD callback
|
||||
profiles mutate valid request/global fields or flags after producing six valid
|
||||
bytes. NCD return probes include-5,-1,0,1; all output bytes remain initialized.
|
||||
All memcpy ranges are allocated and nonoverlapping.
|
||||
|
||||
Two preexisting original/source global spellings are explicitly paired at exact
|
||||
relocation sites: gAtermProductName/lbl_81697248 at+8, .sdata+48, and
|
||||
gAtermUseSharedAddress/lbl_81697244 at+0xB4, .sdata+44. Section/value/addend/type
|
||||
and all other relocation properties agree. These are not new trial renames.
|
||||
|
||||
Limits: this is a bounded actual-instruction interpreter, not hardware or formal
|
||||
proof. memcpy/memcmp/NCD are modeled to valid-buffer contracts, not full SDK
|
||||
implementations. Concrete ELF relocations are rebased to private valid model
|
||||
allocations. Four alias/callback layouts do not exhaust arbitrary callers or
|
||||
2^96 MAC-pair inputs. An NCD failure leaving unwritten bytes is neither exploited
|
||||
nor newly defined. No malformed pointer/out-of-bounds callback claim is made.
|
||||
|
||||
The first proof-loader assertion compared preexisting global spellings literally
|
||||
and stopped before cases. It was replaced by the two exact site/type/section/
|
||||
value pairs above. The failed initial script is preserved; no source compile
|
||||
was repeated. This does not weaken baseline/candidate raw relocation identity.
|
||||
|
||||
## Final gates and repeatable proof
|
||||
|
||||
Selected ordinary compile reproduces the candidate whole object exactly.
|
||||
Default43U build, forced build/43U/ok, all1027 report/object accounting and
|
||||
originalDOL preservation pass. DOL SHA1:
|
||||
`26116613f624061ba99c8d1a299aaa6efa85670d`.
|
||||
Pool0/0 identical. Literal advisory analyzes24 functions/7 arguments, with no
|
||||
candidates/errors; only existing unequal-extent Discovery/RunConfig are skipped.
|
||||
Source diff-check passes. No new exact or complete-linking claim is made.
|
||||
|
||||
Private workspace evidence is `aterm-association-recovery-evidence/`, including
|
||||
`validate_saved.py`, actual compile records, saved reports/objects, exact
|
||||
metadata accounting and bounded model inputs/results. Earlier card and IDCT/JPEG
|
||||
trial evidence remains untouched. This one association recovery is closed with
|
||||
a retained gain. Discovery is a separate reserved scope, not part of this trial.
|
||||
|
||||
Repeat the standalone proof against saved authorized local artifacts; output
|
||||
directory must be fresh:
|
||||
|
||||
```sh
|
||||
.venv/bin/python tools/decomp-assist/aterm_association_recovery_audit.py \
|
||||
--baseline /path/to/baseline.o --candidate /path/to/selected.o \
|
||||
--original /path/to/original.o --dol /path/to/original-main.app \
|
||||
--symbols config/43U/symbols.txt --output-dir /path/to/fresh-proof
|
||||
```
|
||||
|
||||
The script only reads those inputs and writes its proof outputs. It does not
|
||||
compile, score, modify a repository or include proprietary binary artifacts.
|
||||
224
tools/decomp-assist/aterm_association_recovery_audit.py
Normal file
224
tools/decomp-assist/aterm_association_recovery_audit.py
Normal file
@@ -0,0 +1,224 @@
|
||||
"""Bounded actual PPC association-function executions versus sequential source reference.
|
||||
No SDK implementation, hardware, formal or exhaustive MAC Cartesian proof.
|
||||
"""
|
||||
import sys
|
||||
sys.dont_write_bytecode=True
|
||||
from pathlib import Path
|
||||
import hashlib,json,random,re,struct,itertools,time,os
|
||||
from elftools.elf.elffile import ELFFile
|
||||
from capstone import Cs,CS_ARCH_PPC,CS_MODE_32,CS_MODE_BIG_ENDIAN
|
||||
import argparse
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
for name in ['baseline','candidate','original','dol','symbols','output-dir']:parser.add_argument('--'+name,required=True,type=Path)
|
||||
args=parser.parse_args()
|
||||
NAME='ATERMBuildAssociationRequest'
|
||||
|
||||
OUT=args.output_dir.resolve();OUT.mkdir(parents=True,exist_ok=False)
|
||||
def write(n,v):(OUT/n).write_text(json.dumps(v,indent=2)+'\n')
|
||||
|
||||
def plain(x):
|
||||
if hasattr(x,'items'):return {k:plain(v) for k,v in x.items()}
|
||||
if isinstance(x,(list,tuple)):return [plain(v) for v in x]
|
||||
return x
|
||||
|
||||
def metadata(path):
|
||||
with path.open('rb') as f:
|
||||
obj=ELFFile(f);sections=[dict(name=z.name,header=plain(z.header),data=z.data()) for z in obj.iter_sections()];symbols=[dict(name=z.name,entry=plain(z.entry)) for z in obj.get_section_by_name('.symtab').iter_symbols()];functions={}
|
||||
for z in obj.get_section_by_name('.symtab').iter_symbols():
|
||||
if z['st_info']['type']=='STT_FUNC' and isinstance(z['st_shndx'],int) and z['st_size']:
|
||||
sec=obj.get_section(z['st_shndx']);a=z['st_value'];n=z['st_size'];functions[z.name]=dict(section=sec.name,address=a,size=n,body=sec.data()[a:a+n])
|
||||
return dict(header=plain(obj.header),sections=sections,symbols=symbols,functions=functions)
|
||||
mb=metadata(args.baseline);mc=metadata(args.candidate);assert mb['header']==mc['header'];assert len(mb['sections'])==len(mc['sections']);assert len(mb['symbols'])==len(mc['symbols'])==145;sectionchanges=[];pairs=[]
|
||||
for b,c in zip(mb['sections'],mc['sections']):
|
||||
assert b['name']==c['name'] and b['header']==c['header']
|
||||
if b['data']!=c['data']:
|
||||
assert b['name'] in ['.text','.strtab'];sectionchanges.append(b['name'])
|
||||
for i,(b,c) in enumerate(zip(mb['symbols'],mc['symbols'])):
|
||||
assert b['entry']==c['entry']
|
||||
if b['name']!=c['name']:
|
||||
ent=b['entry'];sec=mb['sections'][ent['st_shndx']];assert ent['st_info']==dict(bind='STB_LOCAL',type='STT_OBJECT');assert sec['data']==mc['sections'][ent['st_shndx']]['data'];extent=sec['data'][ent['st_value']:ent['st_value']+ent['st_size']];pairs.append(dict(index=i,old=b['name'],new=c['name'],entry=ent,section=sec['name'],extent_sha256=hashlib.sha256(extent).hexdigest()))
|
||||
assert [(x['index'],x['old'],x['new']) for x in pairs]==[(22,'@2600','@2652'),(23,'@2652','@2704'),(24,'@1043','@1036'),(25,'@2792','@2844')]
|
||||
assert mb['functions'].keys()==mc['functions'].keys();siblings=[]
|
||||
for name,fn in mb['functions'].items():
|
||||
if name!=NAME:assert fn==mc['functions'][name];siblings.append(name)
|
||||
assert len(siblings)==25
|
||||
begin=mb['functions'][NAME]['address'];end=begin+mb['functions'][NAME]['size'];bt=next(z['data'] for z in mb['sections'] if z['name']=='.text');ct=next(z['data'] for z in mc['sections'] if z['name']=='.text');assert bt[:begin]==ct[:begin] and bt[end:]==ct[end:]
|
||||
write('exact-metadata-audit.json',dict(pass_all=True,all_section_headers_symbol_entries_raw_relocation_tables_and_allocated_nontext_identical=True,all25_sibling_bodies_addresses_extents_identical=True,exact_four_local_name_pairs=pairs,section_data_changes=sectionchanges,no_broad_name_normalization=True))
|
||||
MASK=0xffffffff;s32=lambda x:x-0x100000000 if x&0x80000000 else x
|
||||
SD=0x20000000;SB=0x20010000;REQ=0x30000000;SP=0x60000100;FRAME=SP-0x60;RET=0x81234560
|
||||
BASES={'.sdata':SD,'.sbss':SB}
|
||||
DEC=Cs(CS_ARCH_PPC,CS_MODE_32|CS_MODE_BIG_ENDIAN)
|
||||
def load(p):
|
||||
with open(p,'rb') as f:
|
||||
obj=ELFFile(f);st=obj.get_section_by_name('.symtab');fn=st.get_symbol_by_name(NAME)[0];a=fn['st_value'];n=fn['st_size'];body=obj.get_section(fn['st_shndx']).data()[a:a+n];rels={}
|
||||
for sec in obj.iter_sections():
|
||||
if sec['sh_type']!='SHT_RELA' or sec['sh_info']!=fn['st_shndx']:continue
|
||||
sy=obj.get_section(sec['sh_link'])
|
||||
for r in sec.iter_relocations():
|
||||
if a<=r['r_offset']<a+n:
|
||||
sm=sy.get_symbol(r['r_info_sym']);sid=sm['st_shndx'];at=r['r_offset']-a
|
||||
rels[at]=dict(type=r['r_info_type'],name=sm.name,addend=r['r_addend'],section=obj.get_section(sid).name if isinstance(sid,int) else sid,value=sm['st_value'])
|
||||
return dict(raw=body,ins={i.address:(i.mnemonic,i.op_str) for i in DEC.disasm(body,0)},rels=rels)
|
||||
B=load(args.baseline);C=load(args.candidate);T=load(args.original)
|
||||
assert len(B['ins'])==len(C['ins'])==len(T['ins'])==133
|
||||
assert B['raw'][:0xc0]==C['raw'][:0xc0]==T['raw'][:0xc0]
|
||||
assert B['raw'][0x1fc:]==C['raw'][0x1fc:]==T['raw'][0x1fc:]
|
||||
assert B['rels']==C['rels']
|
||||
for pc,z in B['rels'].items():
|
||||
target=T['rels'][pc];assert {k:v for k,v in z.items() if k!='name'}=={k:v for k,v in target.items() if k!='name'}
|
||||
if z['name']!=target['name']:
|
||||
assert (pc,z['name'],target['name'],z['section'],z['value']) in [(8,'gAtermProductName','lbl_81697248','.sdata',48),(0xb4,'gAtermUseSharedAddress','lbl_81697244','.sdata',44)]
|
||||
# The candidate establishes both real six-iteration CTR loops at the target locations.
|
||||
for pc in [0xc0,0xd8,0x154,0x15c,0x174,0x1f0]:assert C['ins'][pc]==T['ins'][pc]
|
||||
assert B['ins'][0x14c][0]==B['ins'][0x1e8][0]=='cmplw'
|
||||
assert B['ins'][0x154][0]==B['ins'][0x1f0][0]=='blt'
|
||||
assert C['ins'][0x154][0]==C['ins'][0x1f0][0]=='bdnz'
|
||||
# Exact dataflow-correspondence maps for the two inlined formatting regions.
|
||||
# These explain residual operands only; they never alter scoring or compiled objects.
|
||||
register_region_maps=[dict(begin=0xc0,end=0x158,mapping={9:5,5:9}),dict(begin=0x158,end=0x1fc,mapping={9:5,5:6,4:9,6:4})]
|
||||
raw_operand_differences=[]
|
||||
for pc,(mn,op) in C['ins'].items():
|
||||
target=T['ins'][pc];normalized=op
|
||||
for region in register_region_maps:
|
||||
if region['begin']<=pc<region['end']:
|
||||
normalized=re.sub(r'\br(\d+)\b',lambda mt:'r'+str(region['mapping'].get(int(mt[1]),int(mt[1]))),op)
|
||||
assert (mn,normalized)==target,(hex(pc),(mn,op),target)
|
||||
if (mn,op)!=target:raw_operand_differences.append(dict(offset=pc,candidate=[mn,op],target=list(target)))
|
||||
assert len(raw_operand_differences)==28
|
||||
write('actual-primitive-correspondence.json',dict(all133_mnemonics_immediates_branches_and_calls_correspond=True,remaining28_raw_differences=raw_operand_differences,exact_region_register_dataflow_maps=register_region_maps,no_scoring_normalization=True))
|
||||
# Target words/direct-call addresses are independently verified against protected DOL.
|
||||
dol=(args.dol).read_bytes();assert hashlib.sha1(dol).hexdigest()=='26116613f624061ba99c8d1a299aaa6efa85670d'
|
||||
words=lambda o:struct.unpack_from('>18I',dol,o);sects=list(zip(words(0),words(0x48),words(0x90)))
|
||||
def at(a,n):
|
||||
for fo,va,size in sects:
|
||||
if va<=a and a+n<=va+size:return dol[fo+a-va:fo+a-va+n]
|
||||
raise AssertionError(('DOL address',a,n))
|
||||
syms={n:int(a,16) for n,a in re.findall(r'^([^\s=]+) = \.[^\s:]+:0x([0-9A-Fa-f]+);',(args.symbols).read_text(),re.M)}
|
||||
raw=0;calls=[]
|
||||
for pc in T['ins']:
|
||||
if pc not in T['rels']:assert at(syms[NAME]+pc,4)==T['raw'][pc:pc+4];raw+=1
|
||||
for pc,rel in T['rels'].items():
|
||||
if rel['type']!=10:continue
|
||||
w=int.from_bytes(at(syms[NAME]+pc,4),'big');d=w&0x3fffffc;d-=0x4000000 if d&0x2000000 else 0;assert syms[NAME]+pc+d==syms[rel['name']]+rel['addend'];calls.append(dict(offset=pc,name=rel['name'],destination=syms[rel['name']]+rel['addend']))
|
||||
class Memory:
|
||||
def __init__(self):self.regions=[];self.trace=[]
|
||||
def add(self,a,n,seed):self.regions.append((a,bytearray((seed+i*31)&255 for i in range(n))))
|
||||
def locate(self,a,n):
|
||||
for base,buf in self.regions:
|
||||
if base<=a and a+n<=base+len(buf):return buf,a-base
|
||||
raise AssertionError(('invalid allocation',hex(a),n))
|
||||
def read(self,a,n,trace=True):
|
||||
b,i=self.locate(a,n);v=bytes(b[i:i+n])
|
||||
if trace:self.trace.append(('read',a,n,v.hex()))
|
||||
return v
|
||||
def write(self,a,v,trace=True):
|
||||
b,i=self.locate(a,len(v));b[i:i+len(v)]=v
|
||||
if trace:self.trace.append(('write',a,len(v),v.hex()))
|
||||
def get(self,a,n=4,trace=True):return int.from_bytes(self.read(a,n,trace),'big')
|
||||
def put(self,a,v,n=4,trace=True):self.write(a,(v&((1<<(8*n))-1)).to_bytes(n,'big'),trace)
|
||||
def state(self):return [(a,bytes(b)) for a,b in self.regions]
|
||||
def fixture(case):
|
||||
m=Memory();seed=case['seed'];m.add(SD,52,seed);m.add(SB,80,seed+71);m.add(REQ,32,seed+97);m.add(SP-0x100,0x180,seed+151)
|
||||
m.write(SD+48,b'WARP',False);m.put(SD+44,case['flag'],trace=False);m.write(SB+68,bytes.fromhex(case['scan']),False)
|
||||
request=[REQ,SB+48,SB+64,SD+32][case['alias']];m.locate(request,16)
|
||||
caller=[((0x91000000+i*0x12345)^seed)&MASK for i in range(32)];caller[1]=SP;caller[3]=request
|
||||
return m,request,caller
|
||||
class Hooks:
|
||||
def __init__(self,m,req,case):self.m=m;self.req=req;self.case=case;self.trace=[];self.count=0;self.cmp_sign=None
|
||||
def call(self,name,args):
|
||||
self.count+=1;self.trace.append((name,tuple(args)));self.m.trace.append(('call',name,tuple(args)));m=self.m;c=self.case
|
||||
if name=='memcpy':
|
||||
dst,src,n=args;assert dst+n<=src or src+n<=dst,('overlapping memcpy forbidden',args);v=m.read(src,n);m.write(dst,v);return dst
|
||||
if name=='memcmp':
|
||||
aa,bb,n=args;assert n==6
|
||||
for i in range(n):
|
||||
a=m.get(aa+i,1);b=m.get(bb+i,1)
|
||||
if a!=b:self.cmp_sign=(a>b)-(a<b);return (a-b)&MASK
|
||||
self.cmp_sign=0;return 0
|
||||
if name=='NCDGetWirelessMacAddress':
|
||||
assert args==[FRAME+8];m.write(args[0],bytes.fromhex(c['interface']));profile=c['callback']
|
||||
if profile==1:
|
||||
m.write(SB+68,bytes((c['seed']+i*19)&255 for i in range(6)));m.write(SD+48,b'NEC!');m.put(SD+44,0)
|
||||
elif profile==2:
|
||||
m.write(self.req,bytes((c['seed']+i*29)&255 for i in range(16)));m.write(SB+68,bytes((c['seed']+i*23)&255 for i in range(6)));m.put(SD+44,0xffffffff)
|
||||
elif profile==3:
|
||||
value=m.get(SD+44);m.put(SD+44,value^1);m.write(SB+56,b'MUTATE');m.put(self.req+2,(c['seed']^0x95)&255,1)
|
||||
return c['ncd_return']&MASK
|
||||
raise AssertionError(('unknown call',name,args))
|
||||
def mask(mb,me):return sum(1<<(31-i) for i in (range(mb,me+1) if mb<=me else list(range(mb,32))+list(range(0,me+1))))
|
||||
def execute(obj,case):
|
||||
m,req,caller=fixture(case);hooks=Hooks(m,req,case);r=caller.copy();pc=0;cr=0;ctr=case['seed']&MASK;lr=RET;coverage=set();steps=0
|
||||
def val(x):return r[int(x[1:])]
|
||||
def reladdr(pc):
|
||||
z=obj['rels'][pc];assert z['type']==109 and z['section'] in BASES;return BASES[z['section']]+z['value']+z['addend']
|
||||
def ea(op,pc):
|
||||
if pc in obj['rels']:return reladdr(pc)
|
||||
mt=re.fullmatch(r'(-?(?:0x[0-9a-f]+|\d+))\((r\d+|0)\)',op);assert mt,op;return ((0 if mt[2]=='0' else val(mt[2]))+int(mt[1],0))&MASK
|
||||
while True:
|
||||
steps+=1;assert steps<1000;coverage.add(pc);mn,op=obj['ins'][pc];pp=op.split(', ');nxt=pc+4
|
||||
if mn in ['lwz','lbz']:r[int(pp[0][1:])]=m.get(ea(pp[1],pc),4 if mn=='lwz' else 1)
|
||||
elif mn in ['stw','stb']:m.put(ea(pp[1],pc),val(pp[0]),4 if mn=='stw' else 1)
|
||||
elif mn=='stwu':addr=ea(pp[1],pc);m.put(addr,val(pp[0]));r[1]=addr
|
||||
elif mn=='mflr':r[int(pp[0][1:])]=lr
|
||||
elif mn=='mtlr':lr=val(pp[0])
|
||||
elif mn=='mtctr':ctr=val(pp[0])
|
||||
elif mn=='mr':r[int(pp[0][1:])]=val(pp[1])
|
||||
elif mn=='li':r[int(pp[0][1:])]=reladdr(pc) if pc in obj['rels'] else int(pp[1],0)&MASK
|
||||
elif mn=='addi':r[int(pp[0][1:])]=((0 if pp[1]=='0' else val(pp[1]))+int(pp[2],0))&MASK
|
||||
elif mn=='add':r[int(pp[0][1:])]=(val(pp[1])+val(pp[2]))&MASK
|
||||
elif mn=='subf':r[int(pp[0][1:])]=(val(pp[2])-val(pp[1]))&MASK
|
||||
elif mn=='andi.':r[int(pp[0][1:])]=val(pp[1])&int(pp[2],0);v=r[int(pp[0][1:])];cr=(s32(v)>0)-(s32(v)<0)
|
||||
elif mn=='rlwinm':sh,mb,me=map(lambda x:int(x,0),pp[2:]);x=val(pp[1]);r[int(pp[0][1:])]=(((x<<sh)|(x>>(32-sh)))&mask(mb,me))&MASK
|
||||
elif mn=='clrlwi':r[int(pp[0][1:])]=val(pp[1])&((1<<(32-int(pp[2],0)))-1)
|
||||
elif mn=='cmpwi':x=s32(val(pp[0]));y=int(pp[1],0);cr=(x>y)-(x<y)
|
||||
elif mn=='cmplw':x=val(pp[0]);y=val(pp[1]);cr=(x>y)-(x<y)
|
||||
elif mn in ['b','beq','bgt','bge','blt']:
|
||||
take={'b':True,'beq':cr==0,'bgt':cr>0,'bge':cr>=0,'blt':cr<0}[mn]
|
||||
if take:nxt=int(pp[-1],0)
|
||||
elif mn=='bdnz':ctr=(ctr-1)&MASK;nxt=int(pp[0],0) if ctr else nxt
|
||||
elif mn=='bl':
|
||||
rel=obj['rels'][pc];assert rel['type']==10 and rel['addend']==0;name=rel['name'];arity=1 if name=='NCDGetWirelessMacAddress' else 3;result=hooks.call(name,r[3:3+arity]);lr=nxt
|
||||
for k in [0,*range(3,13)]:r[k]=(0xb0000000+hooks.count*0x100+k+case['seed'])&MASK
|
||||
ctr=(0xc0000000+hooks.count+case['seed'])&MASK;cr=1;r[3]=result
|
||||
elif mn=='blr':
|
||||
assert lr==RET and r[1]==SP and r[14:32]==caller[14:32];return dict(result=r[3],calls=hooks.trace,trace=m.trace,memory=m.state(),coverage=coverage,steps=steps,cmp_sign=hooks.cmp_sign)
|
||||
else:raise AssertionError(('unsupported actual instruction',hex(pc),mn,op))
|
||||
pc=nxt
|
||||
|
||||
def reference(case):
|
||||
m,req,caller=fixture(case);hooks=Hooks(m,req,case);m.put(FRAME,SP);m.put(FRAME+0x64,RET);m.put(FRAME+0x5c,caller[31]);hooks.call('memcpy',[req+12,SD+48,4]);hooks.call('memcpy',[FRAME+0x10,SB+68,6]);v=m.get(FRAME+0x10,1);m.put(FRAME+0x10,v&0xfd,1);hooks.call('NCDGetWirelessMacAddress',[FRAME+8]);hooks.call('memcpy',[SB+56,FRAME+8,6]);order=s32(hooks.call('memcmp',[FRAME+0x10,FRAME+8,6]))
|
||||
if order<=0:hooks.call('memcpy',[req,FRAME+8,6]);hooks.call('memcpy',[req+6,FRAME+0x10,6])
|
||||
else:hooks.call('memcpy',[req,FRAME+0x10,6]);hooks.call('memcpy',[req+6,FRAME+8,6])
|
||||
flag=m.get(SD+44)
|
||||
if flag:
|
||||
for inp,out in [(FRAME+8,FRAME+0x38),(FRAME+0x10,FRAME+0x18)]:
|
||||
cursor=out
|
||||
for i in range(6):
|
||||
byte=m.get(inp+i,1);hi=byte//16;lo=byte%16;m.put(cursor,hi+(48 if hi<=9 else 55),1);m.put(cursor+1,lo+(48 if lo<=9 else 55),1);m.put(cursor+2,0,1);cursor+=2
|
||||
if i<5:m.put(cursor,58,1);cursor+=1
|
||||
m.put(cursor,0,1)
|
||||
m.get(FRAME+0x5c);m.get(FRAME+0x64);return dict(result=1,calls=hooks.trace,trace=m.trace,memory=m.state(),cmp_sign=hooks.cmp_sign,formatting=bool(flag))
|
||||
|
||||
def case(scan,interface,flag=1,alias=0,callback=0,seed=0,ncd_return=0):return dict(scan=bytes(scan).hex(),interface=bytes(interface).hex(),flag=flag,alias=alias,callback=callback,seed=seed,ncd_return=ncd_return)
|
||||
cases=[]
|
||||
# Exhaust all256 raw input byte values independently at all six positions of both actual source arrays.
|
||||
for value,pos,which in itertools.product(range(256),range(6),range(2)):
|
||||
scan=[0x5a]*6;interface=[0xa5]*6
|
||||
(scan if which==0 else interface)[pos]=value;cases.append(case(scan,interface,seed=len(cases)))
|
||||
patterns=[bytes(6),bytes([255]*6),bytes(range(6)),bytes(range(5,-1,-1)),bytes([9,10,0x99,0x9a,0xa0,0xfa]),bytes([0x10,0xef,0xfe,0xa9,0x90,0x0f]),bytes([0,1,2,0xff,0x7f,0x80]),bytes([0x80,0x7f,0xff,2,1,0])]
|
||||
for a,b,alias,cb,flag in itertools.product(patterns,patterns,range(4),range(4),[0,1,0xffffffff]):cases.append(case(a,b,flag,alias,cb,len(cases),[-5,0,1,-1][cb]))
|
||||
rng=random.Random(202610040007)
|
||||
for i in range(512):cases.append(case(rng.randbytes(6),rng.randbytes(6),rng.choice([0,1,0xffffffff,0x80000000]),i%4,i%4,len(cases),rng.choice([-5,-1,0,1])))
|
||||
assert len(cases)==6656;write('ppc-path-inputs.json',cases)
|
||||
coverage={n:set() for n in ['baseline','candidate','target']};digest=hashlib.sha256();counts={};start=time.time();maxsteps=0
|
||||
for number,x in enumerate(cases):
|
||||
rr=reference(x)
|
||||
for n,obj in [('baseline',B),('candidate',C),('target',T)]:
|
||||
got=execute(obj,x)
|
||||
for k in ['result','calls','trace','memory','cmp_sign']:assert got[k]==rr[k],('bounded mismatch',number,n,k,x,got[k] if k in ['result','cmp_sign'] else 'trace/state')
|
||||
coverage[n].update(got['coverage']);maxsteps=max(maxsteps,got['steps'])
|
||||
counts[str((x['alias'],x['callback'],rr['formatting'],rr['cmp_sign']))]=counts.get(str((x['alias'],x['callback'],rr['formatting'],rr['cmp_sign'])),0)+1
|
||||
digest.update(json.dumps(x,sort_keys=True).encode());digest.update(repr(rr['trace']).encode());digest.update(repr(rr['memory']).encode())
|
||||
for n,obj in [('baseline',B),('candidate',C),('target',T)]:assert coverage[n]==set(obj['ins']),('coverage',n,set(obj['ins'])-coverage[n])
|
||||
summary=dict(pass_all=True,cases=len(cases),actual_ppc_executions=len(cases)*3,independent_sequential_reference_executions=len(cases),all256_raw_input_byte_values_at_every_six_positions_of_both_input_arrays=True,selected_bssid_first_byte_bit1_is_cleared_as_existing_source_requires=True,actual_return_calls_ordered_full_memory_read_write_trace_and_all_allocated_state_equal=True,all_nonvolatile_registers_stack_and_return_address_preserved=True,coverage={n:dict(covered=len(v),total=133,offsets=sorted(v)) for n,v in coverage.items()},caller_alias_layouts=['disjoint request','request inside sbss overlapping addressBuffer','request inside sbss overlapping selectedBssid','request inside sdata overlapping shared-address flag'],callback_profiles=['valid MAC output','also mutate selected/product/flagzero','also mutate request/selected/flagffffffff','also mutate request/addressBuffer/toggleflag'],ncd_returns=[-5,-1,0,1],all_memcpy_ranges_allocated_and_nonoverlapping=True,actual_cfg_loop_primitives_fixed=True,target_dol_nonrelocated_words_verified=raw,target_direct_calls_verified=calls,max_actual_steps=maxsteps,result_digest=digest.hexdigest(),case_classes=counts,elapsed_seconds=time.time()-start,limits=['Bounded whole-function actual-object PPC interpreter and independent sequential reference; not hardware or formal proof','memcpy/memcmp/NCD external routines modeled to their stated valid-buffer contracts rather than executing all SDK internals','All ELF relocations retain their actual section/symbol/addend semantics, rebased to private allocated model regions','Private caller frame and concrete four valid request/global contained-alias layouts, four NCD callback profiles; not arbitrary invalid pointers or out-of-bounds callback writes','Every byte value separately at every input position plus structured MAC pairs and512 deterministic random pairs; not exhaustive2^96 pair domain','NCD error-return probes still provide six initialized output bytes; source behavior after an NCD failure leaving bytes unwritten is not exploited or newly defined','Formatting writes remain in existing private arrays even though no C consumer follows; no new dummy output storage'],no_formal_or_hardware_claim=True)
|
||||
write('ppc-path-audit.json',summary);print('PPC REFERENCE PASS',len(cases),'cases',len(cases)*3,'actualexec full133/133 allthree;exactcalls/readwrites/allocatedstate',flush=True)
|
||||
Reference in New Issue
Block a user