HARD: Mynewt FCB media results and SPIFFS append truncate space (batch 28 47298b7, after a top-up); TinyUSB CH34x control completion, TLV320 output gain encoding and ArduinoJSON decimal carry (batch 26 f20f11c); NetX Duo PPP buffered burst (batch 28 7f01d64). MEDIUM: RT-Thread ringbuffer DMA peek (batch 28 7f01d64). Task digests match the measured ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
11 KiB
Python
136 lines
11 KiB
Python
"""Trusted parser/model. Inputs and every expanded expected value are frozen first."""
|
|
import json
|
|
|
|
def judge(case,returncode,stdout,stderr):
|
|
errors=[]
|
|
try:rows=[json.loads(line) for line in stdout.splitlines()]
|
|
except (ValueError,TypeError) as e:return ['malformed observations: '+str(e)],[]
|
|
if case['diagnostic']:return [],rows
|
|
if returncode:errors.append('candidate exit '+str(returncode))
|
|
identity=[r for r in rows if r.get('event')=='identity']
|
|
if identity!=[dict(event='identity',uid=65534,gid=65534,groups=0)]:errors.append('identity')
|
|
if not any(r.get('event')=='init' and r.get('ok')==1 for r in rows):errors.append('real tuh_init')
|
|
active_addr=None
|
|
for e in case['expected']:
|
|
rs=[r for r in rows if r.get('id')==e['id']]
|
|
def event(name):return [r for r in rs if r.get('event')==name]
|
|
def cache(where):return [r for r in event('cache') if r.get('where')==where]
|
|
def require(ok,what):
|
|
if not ok:errors.append(f"{e['id']} {e['kind']} {what}")
|
|
require(all(r.get('ok')==1 for r in event('cache')),'local cache query success')
|
|
if e['kind']=='attach':
|
|
require(len(event('mount'))==1,'mount callback')
|
|
require(len(event('hostmount'))==1,'host mounted')
|
|
active_addr=event('hostmount')[0].get('addr') if len(event('hostmount'))==1 else None
|
|
require(active_addr is not None and active_addr>0 and len(event('mount'))==1 and event('mount')[0].get('addr')==active_addr,'CDC and host device address')
|
|
require(len(event('attached'))==1 and event('attached')[0]['mounted']==1 and event('attached')[0]['hostmounted']==1,'public mounted')
|
|
require(len(cache('mount'))==1 and cache('mount')[0]['coding']==e['coding'] and cache('mount')[0]['state']==e['state'],'initial coding/state')
|
|
wires=event('wire');require(any(w['phase']=='setup' and w['hex'][2:4]=='06' for w in wires),'descriptor enumeration')
|
|
elif e['kind']=='detach':
|
|
require(len(event('unmount'))==1 and len(event('hostunmount'))==1,'unmount callbacks')
|
|
require(len(event('detached'))==1 and event('detached')[0]['mounted']==0,'unmounted public state')
|
|
require(any(w.get('addr',0)>0 for w in event('close')),'device endpoint close')
|
|
elif e['kind']=='control':
|
|
ret=event('return');cb=event('callback');done=event('done');async_=e['mode'] in (1,3)
|
|
require(len(ret)==1 and ret[0]['ret']==(1 if async_ else int(e['result']==0)),'return bool')
|
|
if ret:
|
|
require(ret[0]['guard']==[0x12345678,0x87654321],'result canaries')
|
|
require(ret[0]['callbacks']==0 if async_ else True,'callback before return')
|
|
require(ret[0]['result']==(4 if e['mode'] in (1,2,3) else e['result']),'result pointer')
|
|
require(len(done)==1 and done[0]['callbacks']==int(async_),'completion count')
|
|
if async_:
|
|
require(len(cb)==1 and cb[0]['result']==e['result'] and cb[0]['userdata']==e['userdata'] and cb[0]['addr']==active_addr and cb[0]['ep']==0,'callback result/userdata/address')
|
|
require(len(cache('return'))==1 and cache('return')[0]['coding'] in e.get('before_options',[e['before']]) and cache('return')[0]['state']==e['state_before'],'async result only after completion')
|
|
require(len(cache('callback'))==1 and cache('callback')[0]['coding'] in e['after'] and cache('callback')[0]['state']==e['state_after'],'callback sees completed cache')
|
|
require(len(cache('after'))==1 and cache('after')[0]['coding'] in e['after'] and cache('after')[0]['state']==e['state_after'],'successful fields cache; failed fields unchanged')
|
|
wire=event('wire');require(any(r['phase']=='setup' for r in wire),'physical setup')
|
|
require(any(r['phase']=='status' and r['result']==e['result'] for r in wire),'physical status result')
|
|
if e['injection']:require(len(done)==1 and done[0]['injected']==1,'physical error injected')
|
|
elif e['kind']=='tx':
|
|
require(event('write_zero')==[dict(event='write_zero',id=e['id'],n=0)],'zero write')
|
|
data=''.join(r['hex'] for r in event('wire') if r['phase']=='tx')
|
|
require(data==e['hex'],'wire byte sequence')
|
|
require(len(event('tx_done'))==1 and event('tx_done')[0]['accepted']==e['n'],'accepted byte count')
|
|
require(all(0<=r['n']<=r['ask'] for r in event('write')),'accept lengths')
|
|
# USB also permits termination at the host's known requested length.
|
|
# Keep all raw ZLP observations; byte integrity does not mandate one ending policy.
|
|
elif e['kind']=='rx':
|
|
require(event('read_zero')==[dict(event='read_zero',id=e['id'],n=0)],'zero read')
|
|
require(''.join(r['hex'] for r in event('read'))==e['hex'],'read byte sequence')
|
|
require(len(event('rx_done'))==1 and event('rx_done')[0]['read']==e['n'] and event('rx_done')[0]['submitted']==e['n'],'received byte count')
|
|
require(all(r['n']<=e['chunk'] for r in event('read')),'read length')
|
|
return errors,rows
|
|
|
|
import base64,hashlib,os,resource,subprocess,sys,tempfile,zlib
|
|
from pathlib import Path
|
|
GRADER=Path(__file__).resolve().parent
|
|
CASES=json.loads(zlib.decompress((GRADER/'cases.data').read_bytes()))
|
|
SUPPORT={k:base64.b64decode(v) for k,v in json.loads(zlib.decompress((GRADER/'support.data').read_bytes())).items()}
|
|
CANDIDATE=(Path(sys.argv[1])/'src/class/cdc/cdc_host.c').read_bytes()
|
|
SOURCE=dict(SUPPORT);SOURCE['src/class/cdc/cdc_host.c']=CANDIDATE
|
|
scratch=Path(tempfile.mkdtemp(prefix='ch34x-check-'))
|
|
for name,data in SOURCE.items():
|
|
p=scratch/name;p.parent.mkdir(parents=True,exist_ok=True);p.write_bytes(data)
|
|
for p in [*scratch.rglob('*'),scratch]:os.chown(p,65534,65534)
|
|
rootstr=str(scratch)
|
|
common=['gcc','-std=c11','-O0','-g0','-I'+rootstr+'/port','-I'+rootstr+'/src',rootstr+'/src/tusb.c',rootstr+'/src/host/usbh.c',rootstr+'/src/common/tusb_fifo.c',rootstr+'/src/class/cdc/cdc_host.c',rootstr+'/app/observe.c','-o']
|
|
builds={state:common+[rootstr+'/observe'+str(state),'-DENUM_STATE='+str(state)] for state in (0,3)}
|
|
runs={state:[rootstr+'/observe'+str(state)] for state in (0,3)}
|
|
# Optional author helper evidence is another private bind, never a candidate path.
|
|
if 'CH34X_EVIDENCE' in os.environ:
|
|
evidence=Path(os.environ['CH34X_EVIDENCE'])
|
|
else:
|
|
evidence=Path(tempfile.mkdtemp(prefix='ch34x-private-'))
|
|
# bos_verify hands general mkdtemp roots to candidate UID; restore this private output root.
|
|
os.chown(evidence,0,0)
|
|
st=evidence.stat()
|
|
if st.st_uid==65534 or st.st_mode&0o077:raise RuntimeError('private evidence directory permissions')
|
|
probe_paths=[str(GRADER),str(GRADER/'check.py'),str(GRADER/'cases.data'),str(GRADER/'support.data'),str(evidence),str(evidence/'expanded-expected.json'),'/proc/self/root'+str(evidence),'/proc/self/root'+str(evidence/'expanded-expected.json'),f'/proc/{os.getpid()}/root'+str(evidence/'expanded-expected.json')]
|
|
probe_dirs=[str(GRADER),str(evidence),'/proc/self/root'+str(evidence)]
|
|
probe="import os,json,pathlib\nr=[]\ndirs="+repr(probe_dirs)+"\nfor p in "+repr(probe_paths)+":\n try:\n q=pathlib.Path(p)\n (list(q.iterdir()) if p in dirs else q.read_bytes())\n r.append([p,'READABLE'])\n except PermissionError:r.append([p,'PermissionError'])\n except Exception as e:r.append([p,type(e).__name__])\nprint(json.dumps({'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),'paths':r}))\n"
|
|
snapshot={'source_hashes':{k:hashlib.sha256(v).hexdigest() for k,v in SOURCE.items()},'candidate_sha256':hashlib.sha256(CANDIDATE).hexdigest(),'cases_sha256':hashlib.sha256((GRADER/'cases.data').read_bytes()).hexdigest(),'check_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),'build_argv':builds,'run_argv':runs,'probe_argv':['python3','-I','-c',probe],'scratch':rootstr,'scratch_mode':'mkdtemp0700; chown65534 only; parent no filesystem access after first child','case_count':len(CASES)}
|
|
(evidence/'candidate.c.data').write_bytes(CANDIDATE)
|
|
(evidence/'check.py.data').write_bytes(Path(__file__).read_bytes())
|
|
(evidence/'support.data.snapshot').write_bytes((GRADER/'support.data').read_bytes())
|
|
(evidence/'cases.data.snapshot').write_bytes((GRADER/'cases.data').read_bytes())
|
|
(evidence/'expanded-expected.json').write_text(json.dumps(CASES,indent=2)+'\n')
|
|
(evidence/'frozen-manifest.json').write_text(json.dumps(snapshot,indent=2)+'\n')
|
|
(evidence/'prechild-manifest.json').write_text(json.dumps({p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in evidence.iterdir() if p.is_file() and p.name!='prechild-manifest.json'},indent=2)+'\n')
|
|
# All candidate/support/harness/inputs/expected/argv/probe/private snapshots are now frozen.
|
|
# No parent code below touches scratch, including on compiler failure or timeout.
|
|
def restrict():
|
|
resource.setrlimit(resource.RLIMIT_CORE,(0,0))
|
|
resource.setrlimit(resource.RLIMIT_STACK,(1024*1024,1024*1024))
|
|
def run(argv,inp='',timeout=2):
|
|
try:
|
|
p=subprocess.run(argv,input=inp,text=True,capture_output=True,timeout=timeout,user=65534,group=65534,extra_groups=[],preexec_fn=restrict,cwd='/',env={'PATH':'/usr/local/bin:/usr/bin:/bin','LANG':'C','HOME':'/nonexistent'})
|
|
return dict(argv=argv,exit=p.returncode,stdout=p.stdout,stderr=p.stderr,timeout=False)
|
|
except subprocess.TimeoutExpired as p:
|
|
return dict(argv=argv,exit=None,stdout=p.stdout.decode() if isinstance(p.stdout,bytes) else p.stdout or '',stderr=p.stderr.decode() if isinstance(p.stderr,bytes) else p.stderr or '',timeout=True)
|
|
probe_result=run(snapshot['probe_argv'],timeout=10)
|
|
(evidence/'identity-probe.json').write_text(json.dumps(probe_result,indent=2)+'\n')
|
|
try:observed=json.loads(probe_result['stdout'])
|
|
except ValueError:observed={}
|
|
if probe_result['exit']!=0 or observed.get('uid')!=65534 or observed.get('gid')!=65534 or observed.get('groups')!=[] or len(observed.get('paths',[]))!=len(probe_paths) or any(r[1]!='PermissionError' for r in observed.get('paths',[])):
|
|
print('isolation probe could not confirm denial',file=sys.stderr);raise SystemExit(86)
|
|
for state,argv in builds.items():
|
|
p=run(argv,timeout=40);(evidence/('build-'+str(state)+'.json')).write_text(json.dumps(p,indent=2)+'\n')
|
|
if p['exit']!=0:
|
|
print('candidate compilation failed',file=sys.stderr);print('FAIL');raise SystemExit(1)
|
|
failures=[];summary=[]
|
|
with (evidence/'raw-observations.jsonl').open('w') as raw:
|
|
for c in CASES:
|
|
p=run(runs[c['enumstate']],c['input'],timeout=1 if c['diagnostic'] else 2)
|
|
errors,rows=judge(c,p['exit'],p['stdout'],p['stderr'])
|
|
raw.write(json.dumps(dict(label=c['label'],diagnostic=c['diagnostic'],input=c['input'],**p,errors=errors),separators=(',',':'))+'\n');raw.flush()
|
|
summary.append(dict(label=c['label'],exit=p['exit'],timeout=p['timeout'],diagnostic=c['diagnostic'],errors=errors))
|
|
if errors:
|
|
failures.append(dict(label=c['label'],errors=errors))
|
|
if os.environ.get('CH34X_ALL_CASES')!='1':break
|
|
receipt=dict(status='FAIL' if failures else 'PASS',candidate_sha256=snapshot['candidate_sha256'],check_sha256=snapshot['check_sha256'],case_count=len(CASES),executed=len(summary),failed=len(failures),probe='PASS',scratch_parent_postchild_fs_accesses=0,results=summary)
|
|
(evidence/'checker-receipt.json').write_text(json.dumps(receipt,indent=2)+'\n')
|
|
print(json.dumps({k:v for k,v in receipt.items() if k!='results'}),file=sys.stderr)
|
|
if failures:
|
|
print(json.dumps(failures[:2]),file=sys.stderr);print('FAIL');raise SystemExit(1)
|
|
print('PASS')
|