One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
497 lines
25 KiB
Python
497 lines
25 KiB
Python
import base64
|
|
|
|
import bz2
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import math
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
import random
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
RECOVERY_SECONDS = 3
|
|
|
|
RECORD_WIRE = False
|
|
|
|
SUPERVISOR = r'''
|
|
import base64,bz2,ctypes,hashlib,json,os,signal,subprocess,sys,time
|
|
cfg=json.loads(sys.argv[1]); start=time.monotonic(); errors=[]; tracked={}; signals={}; waits=[]
|
|
leader=None; leader_start=None; output=b''; diagnostic=b''; timed_out=False; drained=False
|
|
reaped=False; leader_rc=None; remaining=[]; pg_absent=False
|
|
|
|
def fault(op, error):
|
|
errors.append([op,type(error).__name__,str(error)])
|
|
|
|
def exact(pid):
|
|
try:
|
|
with open('/proc/%d/stat'%pid,'rb') as stream: data=stream.read()
|
|
words=data[data.rfind(b')')+2:].split()
|
|
return {'pid':pid,'state':words[0].decode('ascii'),'ppid':int(words[1]),'pg':int(words[2]),'start':int(words[19])}
|
|
except (FileNotFoundError,ProcessLookupError): return None
|
|
|
|
def table_now():
|
|
result={}
|
|
for name in os.listdir('/proc'):
|
|
if name.isascii() and name.isdigit():
|
|
value=exact(int(name))
|
|
if value is not None: result[value['pid']]=value
|
|
return result
|
|
|
|
def closure(table, supervisor, remembered):
|
|
roots={supervisor}
|
|
for pid,old in remembered.items():
|
|
current=table.get(pid)
|
|
if current and current['start']==old['start'] and current['state'] not in ('Z','X'):
|
|
roots.add(pid)
|
|
found={}
|
|
changed=True
|
|
while changed:
|
|
changed=False
|
|
for pid,current in table.items():
|
|
if pid!=supervisor and pid not in found and (current['ppid'] in roots or pid in roots):
|
|
found[pid]=current; roots.add(pid); changed=True
|
|
return found
|
|
|
|
def discover():
|
|
table=table_now(); found=closure(table,os.getpid(),tracked)
|
|
for pid,current in found.items():
|
|
if pid not in tracked or tracked[pid]['start']!=current['start']:
|
|
tracked[pid]=dict(current)
|
|
return table,found
|
|
|
|
def send_fresh(pid, token):
|
|
current=exact(pid)
|
|
if current is None or current['start']!=token['start'] or current['state'] in ('Z','X'): return
|
|
os.kill(pid,signal.SIGKILL)
|
|
signals[(pid,token['start'])]=signals.get((pid,token['start']),0)+1
|
|
|
|
def own_wait():
|
|
global reaped,leader_rc
|
|
if leader is not None:
|
|
value=leader.poll()
|
|
if value is not None:
|
|
leader_rc=leader.wait(timeout=0)
|
|
reaped=True
|
|
|
|
def adopted_wait():
|
|
if leader is not None and not reaped: return
|
|
while True:
|
|
try: pid,status=os.waitpid(-1,os.WNOHANG)
|
|
except ChildProcessError: break
|
|
if pid==0: break
|
|
waits.append([pid,None,status,os.waitstatus_to_exitcode(status)])
|
|
|
|
supervisor_start=exact(os.getpid())['start']
|
|
try:
|
|
libc=ctypes.CDLL(None,use_errno=True)
|
|
if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')
|
|
leader=subprocess.Popen(cfg['argv'],cwd=cfg['cwd'],stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
|
|
info=exact(leader.pid)
|
|
if info is None: raise RuntimeError('leader identity disappeared before capture')
|
|
leader_start=info['start']; tracked[leader.pid]=info
|
|
payload=sys.stdin.buffer.read()
|
|
first=True
|
|
while True:
|
|
try:
|
|
output,diagnostic=leader.communicate(input=payload if first else None,timeout=0.04)
|
|
drained=True; own_wait(); break
|
|
except subprocess.TimeoutExpired:
|
|
first=False
|
|
own_wait()
|
|
discover()
|
|
if time.monotonic()-start>=cfg['deadline']:
|
|
timed_out=True; break
|
|
except BaseException as error:
|
|
fault('execute',error)
|
|
finally:
|
|
recovery_end=time.monotonic()+cfg['recovery']
|
|
while True:
|
|
try: own_wait()
|
|
except BaseException as error: fault('own_wait',error)
|
|
found={}
|
|
try: _,found=discover()
|
|
except BaseException as error: fault('discover',error)
|
|
for pid,token in list(found.items()):
|
|
try: send_fresh(pid,token)
|
|
except (FileNotFoundError,ProcessLookupError): pass
|
|
except BaseException as error: fault('signal',error)
|
|
try: own_wait()
|
|
except BaseException as error: fault('own_wait',error)
|
|
try: adopted_wait()
|
|
except BaseException as error: fault('adopted_wait',error)
|
|
if leader is not None and not drained:
|
|
try:
|
|
output,diagnostic=leader.communicate(timeout=0.03)
|
|
drained=True; own_wait()
|
|
except subprocess.TimeoutExpired: pass
|
|
except BaseException as error: fault('drain',error)
|
|
try:
|
|
table,found=discover()
|
|
remaining=[[p,v['start'],v['state']] for p,v in found.items()]
|
|
pg_absent=leader is not None and not any(v['pg']==leader.pid for v in table.values())
|
|
except BaseException as error:
|
|
fault('absence',error); remaining=[['unknown']]; pg_absent=False
|
|
if reaped and drained and not remaining and pg_absent: break
|
|
if time.monotonic()>=recovery_end:
|
|
fault('recovery',RuntimeError('deadline with unconfirmed closure')); break
|
|
time.sleep(0.01)
|
|
|
|
meta={'phase':cfg['phase'],'nonce':cfg['nonce'],'argv':cfg['argv'],
|
|
'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
|
|
'supervisor_pid':os.getpid(),'supervisor_start':supervisor_start,
|
|
'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,
|
|
'leader_rc':leader_rc,'leader_reaped':reaped,'timeout':timed_out,
|
|
'controlled_rc':124 if timed_out else leader_rc,'elapsed':time.monotonic()-start,
|
|
'deadline':cfg['deadline'],'recovery':cfg['recovery'],
|
|
'pg_absent':pg_absent,'remaining':remaining,'exceptions':errors,
|
|
'adopted':waits,
|
|
'descendants':[[p,t['start'],signals.get((p,t['start']),0)]
|
|
for p,t in tracked.items() if leader is None or p!=leader.pid],
|
|
'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),
|
|
'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}
|
|
wire={'meta':meta,'stdout':base64.b64encode(bz2.compress(output)).decode('ascii'),
|
|
'stderr':base64.b64encode(bz2.compress(diagnostic)).decode('ascii')}
|
|
sys.stdout.write(json.dumps(wire,separators=(',',':'))+'\n')
|
|
'''
|
|
|
|
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
|
|
|
|
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)"
|
|
|
|
def identity_int(value, minimum=0):
|
|
return type(value) is int and value >= minimum
|
|
|
|
def validate_meta(meta, config, out, err):
|
|
required = {'phase','nonce','argv','uid','gid','groups','supervisor_pid','supervisor_start',
|
|
'leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc',
|
|
'elapsed','deadline','recovery','pg_absent','remaining','exceptions','descendants',
|
|
'stdout_len','stdout_sha256','stderr_len','stderr_sha256','adopted'}
|
|
if type(meta) is not dict or not required <= meta.keys():
|
|
raise ValueError('missing stage metadata')
|
|
if meta['phase'] != config['phase'] or meta['nonce'] != config['nonce'] or meta['argv'] != config['argv']:
|
|
raise ValueError('stage binding conflict')
|
|
for name in ('uid','gid','supervisor_pid','supervisor_start','leader_pid','leader_start'):
|
|
if not identity_int(meta[name], 1 if name not in ('uid','gid') else 0):
|
|
raise ValueError('invalid identity '+name)
|
|
if meta['supervisor_pid'] == meta['leader_pid']:
|
|
raise ValueError('supervisor and leader identical')
|
|
isolated = os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534'
|
|
expected_identity = (65534,65534,[]) if isolated else (os.getuid(),os.getgid(),os.getgroups())
|
|
if type(meta['groups']) is not list or any(type(x) is not int or x < 0 for x in meta['groups']):
|
|
raise ValueError('invalid groups')
|
|
if (meta['uid'],meta['gid'],meta['groups']) != expected_identity:
|
|
raise ValueError('execution identity conflict')
|
|
if type(meta['deadline']) is not int or meta['deadline'] != config['deadline']:
|
|
raise ValueError('deadline conflict')
|
|
if type(meta['recovery']) is not int or meta['recovery'] != RECOVERY_SECONDS:
|
|
raise ValueError('recovery conflict')
|
|
elapsed = meta['elapsed']
|
|
if type(elapsed) not in (int,float) or not math.isfinite(elapsed) or not 0 <= elapsed <= config['deadline']+RECOVERY_SECONDS+2:
|
|
raise ValueError('invalid elapsed')
|
|
for name in ('timeout','leader_reaped','pg_absent'):
|
|
if type(meta[name]) is not bool:
|
|
raise ValueError('invalid boolean '+name)
|
|
for name in ('leader_rc','controlled_rc'):
|
|
if type(meta[name]) is not int:
|
|
raise ValueError('invalid return code '+name)
|
|
if meta['controlled_rc'] != (124 if meta['timeout'] else meta['leader_rc']):
|
|
raise ValueError('return code conflict')
|
|
for name in ('remaining','exceptions','descendants','adopted'):
|
|
if type(meta[name]) is not list:
|
|
raise ValueError('invalid list '+name)
|
|
for row in meta['descendants']:
|
|
if type(row) is not list or len(row) != 3 or not all(identity_int(x,1) for x in row[:2]) or not identity_int(row[2]):
|
|
raise ValueError('invalid descendant record')
|
|
for row in meta['adopted']:
|
|
if (type(row) is not list or len(row) != 4 or not identity_int(row[0],1)
|
|
or row[1] is not None or not identity_int(row[2]) or type(row[3]) is not int
|
|
or os.waitstatus_to_exitcode(row[2]) != row[3]):
|
|
raise ValueError('invalid adopted wait record')
|
|
for name,data in (('stdout',out),('stderr',err)):
|
|
if not identity_int(meta[name+'_len']) or meta[name+'_len'] != len(data):
|
|
raise ValueError('invalid IO length')
|
|
if type(meta[name+'_sha256']) is not str or meta[name+'_sha256'] != hashlib.sha256(data).hexdigest():
|
|
raise ValueError('invalid IO digest')
|
|
if meta['exceptions'] or meta['remaining'] or not meta['leader_reaped'] or not meta['pg_absent']:
|
|
raise ValueError('stage recovery or exception failure')
|
|
if meta['timeout'] or meta['leader_rc'] != 0 or meta['controlled_rc'] != 0:
|
|
raise ValueError('controlled timeout' if meta['timeout'] else 'leader nonzero exit')
|
|
|
|
def execute_stage(phase, argv, cwd, deadline, payload, evidence):
|
|
nonce = os.urandom(8).hex()
|
|
config = dict(phase=phase,argv=argv,cwd=str(cwd),deadline=deadline,recovery=RECOVERY_SECONDS,nonce=nonce)
|
|
wrapper_argv = [sys.executable,'-I','-c',SUPERVISOR,json.dumps(config,separators=(',',':'))]
|
|
raw = subprocess.run(wrapper_argv,input=payload,capture_output=True,timeout=deadline+RECOVERY_SECONDS+5)
|
|
# Preserve actual wrapper facts before touching or trusting its wire protocol.
|
|
entry = dict(phase=phase,nonce=nonce,wrapper_rc=raw.returncode,
|
|
wire_len=len(raw.stdout),wire_sha256=hashlib.sha256(raw.stdout).hexdigest(),
|
|
wrapper_stderr_len=len(raw.stderr),wrapper_stderr_sha256=hashlib.sha256(raw.stderr).hexdigest())
|
|
entry['original_wire_persisted'] = False
|
|
entry['original_io_persisted'] = False
|
|
evidence.append(entry)
|
|
if raw.returncode != 0:
|
|
raise ValueError(phase+' supervisor nonzero')
|
|
wire = json.loads(raw.stdout)
|
|
out = bz2.decompress(base64.b64decode(wire['stdout'],validate=True))
|
|
err = bz2.decompress(base64.b64decode(wire['stderr'],validate=True))
|
|
meta = wire['meta']
|
|
entry['meta'] = meta
|
|
validate_meta(meta,config,out,err)
|
|
return out,err
|
|
|
|
SUPPORT = {'Makefile': 'CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Werror -Iinclude\nSOURCES = src/mist.c src/command.c support/observer.c\n.PHONY: all demo recovery clean\nall: build/mist-replay\nbuild/mist-replay: $(SOURCES) include/mist.h\n\tmkdir -p build\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\ndemo: all\n\t./build/mist-replay < examples/demo.txt\nrecovery: all\n\t./build/mist-replay < examples/recovery.txt\nclean:\n\trm -rf build\n', 'support/observer.c': '#define _POSIX_C_SOURCE 200809L\n#include "mist.h"\n#include <stdio.h>\n#include <stdlib.h>\n\nstatic void output(const MistBank *bank, int code)\n{\n MistSummary summary;\n MistEvent events[MIST_HISTORY];\n size_t count;\n size_t position;\n int index;\n mist_summary(bank, &summary);\n printf("[%d,%d,%d,%u,%u,%u", code, summary.count, summary.cursor,\n summary.revision, summary.accepted, summary.rejected);\n for (index = 0; index < 4; index++) {\n printf(",%d", summary.mode_counts[index]);\n }\n printf(",%d,%d,%d,%d,%u", summary.unknown_modes, summary.total_quota,\n summary.total_delivered, summary.pending, summary.changes);\n printf(",[");\n for (index = 0; index < bank->count; index++) {\n MistBay bay;\n if (index) {\n putchar(\',\');\n }\n if (mist_view(bank, index, &bay) != 0) {\n exit(72);\n }\n printf("[%d,%d,%d,%u,\\"%s\\"]", (int)bay.mode, bay.quota,\n bay.delivered, bay.changes, mist_label(bay.mode));\n }\n printf("],[");\n count = mist_events(bank, events, MIST_HISTORY);\n for (position = 0; position < count; position++) {\n const MistEvent *event = &events[position];\n if (position) {\n putchar(\',\');\n }\n printf("[%u,%d,%d,%d,%d,%d]", event->revision, event->kind,\n event->index, event->before, event->after, event->amount);\n }\n puts("]]");\n}\n\nint main(void)\n{\n MistBank bank;\n MistCommand command;\n char *line = NULL;\n size_t capacity = 0;\n ssize_t size;\n mist_init(&bank, 3);\n while ((size = getline(&line, &capacity, stdin)) >= 0) {\n int code;\n (void)size;\n if (mist_decode(line, &command)) {\n code = mist_execute(&bank, &command);\n } else {\n bank.rejected++;\n code = -4;\n }\n output(&bank, code);\n }\n free(line);\n return ferror(stdin) ? 73 : 0;\n}\n'}
|
|
|
|
CANDIDATES = ['include/mist.h','src/mist.c','src/command.c']
|
|
COMPILER_DEADLINE = 20
|
|
CANDIDATE_DEADLINE = 8
|
|
CLEANUP_DEADLINE = 5
|
|
ABSENCE_DEADLINE = 5
|
|
|
|
LABELS = {0:'idle',3:'soak',8:'drain',12:'service'}
|
|
|
|
def parse_command(text):
|
|
import re
|
|
text = text.removesuffix('\n')
|
|
text = text.strip(' \t\r')
|
|
if not text or text[0] not in 'BSATMXRQ':
|
|
return None
|
|
if len(text)>1 and text[1] not in ' \t\r':
|
|
return None
|
|
parts = re.split('[ \t\r]+', text)
|
|
values=[]
|
|
for token in parts[1:]:
|
|
if re.fullmatch('[+-]?[0-9]+',token,flags=re.ASCII) is None:
|
|
return None
|
|
negative=token.startswith('-')
|
|
significant=token.lstrip('+-').lstrip('0') or '0'
|
|
if len(significant)>10:
|
|
return None
|
|
value=int(significant)*(-1 if negative else 1)
|
|
if not -2147483648<=value<=2147483647:
|
|
return None
|
|
values.append(value)
|
|
op=parts[0]
|
|
lengths={'B':1,'S':3,'A':2,'T':1,'M':3,'X':1,'Q':0}
|
|
if (op=='R' and len(values)>18) or (op!='R' and len(values)!=lengths[op]):
|
|
return None
|
|
return op,values
|
|
|
|
|
|
def model(lines):
|
|
from collections import deque
|
|
# Immutable tuples represent each committed bay. History is independent of storage layout.
|
|
bays=[(0,0,0,0)]*3
|
|
cursor=revision=accepted=rejected=0
|
|
events=deque(maxlen=8)
|
|
rows=[]
|
|
for text in lines:
|
|
parsed=parse_command(text)
|
|
code=0; event=None
|
|
if parsed is None:
|
|
code=-4
|
|
else:
|
|
op,v=parsed
|
|
n=len(bays)
|
|
valid=lambda index: 0<=index<n
|
|
amount=lambda value: 0<=value<=1000000
|
|
if op=='B':
|
|
bays=[(0,0,0,0)]*max(1,min(6,v[0]))
|
|
cursor=revision=accepted=rejected=0
|
|
events.clear()
|
|
elif op=='S':
|
|
index,raw,quota=v
|
|
if not valid(index): code=-1
|
|
elif raw not in LABELS: code=-2
|
|
elif not amount(quota) or quota<bays[index][2]: code=-3
|
|
else:
|
|
old,q,used,changes=bays[index]
|
|
bays[index]=(raw,quota,used,changes+(old!=raw))
|
|
event=(1,index,old,raw,quota)
|
|
elif op=='A':
|
|
index,units=v
|
|
if not valid(index): code=-1
|
|
elif not amount(units) or units>bays[index][1]-bays[index][2]: code=-3
|
|
else:
|
|
mode,quota,used,changes=bays[index]
|
|
bays[index]=(mode,quota,used+units,changes)
|
|
event=(2,index,used,used+units,units)
|
|
elif op=='T':
|
|
units=v[0]
|
|
if not amount(units): code=-3
|
|
else:
|
|
before=cursor; consumed=0
|
|
for unused in range(units):
|
|
eligible=next((i for i in list(range(cursor,n))+list(range(cursor))
|
|
if bays[i][0]==3 and bays[i][2]<bays[i][1]),None)
|
|
if eligible is None: break
|
|
mode,quota,used,changes=bays[eligible]
|
|
bays[eligible]=(mode,quota,used+1,changes)
|
|
cursor=(eligible+1)%n
|
|
consumed+=1
|
|
code=consumed
|
|
event=(3,-1,before,cursor,consumed)
|
|
elif op=='M':
|
|
source,target,units=v
|
|
if not valid(source) or not valid(target): code=-1
|
|
elif not amount(units) or units>bays[source][1]-bays[source][2]: code=-3
|
|
elif source!=target and bays[target][1]+units>1000000: code=-3
|
|
else:
|
|
if source!=target:
|
|
mode,quota,used,changes=bays[source]
|
|
bays[source]=(mode,quota-units,used,changes)
|
|
mode,quota,used,changes=bays[target]
|
|
bays[target]=(mode,quota+units,used,changes)
|
|
event=(4,source,source,target,units)
|
|
elif op=='X':
|
|
index=v[0]
|
|
if not valid(index): code=-1
|
|
else:
|
|
old,quota,used,changes=bays[index]
|
|
bays[index]=(0,0,0,changes+(old!=0))
|
|
event=(5,index,old,0,0)
|
|
elif op=='R':
|
|
if len(v)!=3*n: code=-4
|
|
else:
|
|
staged=[]
|
|
for index in range(n):
|
|
raw,quota,used=v[3*index:3*index+3]
|
|
if raw not in LABELS: code=-2; break
|
|
if not amount(quota) or not amount(used) or used>quota: code=-3; break
|
|
staged.append((raw,quota,used,bays[index][3]+(bays[index][0]!=raw)))
|
|
if not code:
|
|
event=(6,-1,sum(x[2] for x in bays),sum(x[2] for x in staged),n)
|
|
bays=staged
|
|
if code<0:
|
|
rejected+=1
|
|
if event is not None:
|
|
revision+=1; accepted+=1
|
|
events.append([revision,*event])
|
|
counts=[sum(x[0]==mode for x in bays) for mode in LABELS]
|
|
total_quota=sum(x[1] for x in bays)
|
|
total_used=sum(x[2] for x in bays)
|
|
rows.append([code,len(bays),cursor,revision,accepted,rejected,*counts,
|
|
sum(x[0] not in LABELS for x in bays),total_quota,total_used,
|
|
total_quota-total_used,sum(x[3] for x in bays),
|
|
[[*bay,LABELS.get(bay[0],'unknown')] for bay in bays],list(events)])
|
|
return rows
|
|
|
|
|
|
def scenarios():
|
|
lines=['Q','B 0','S 0 3 17','T 5','S 0 7 17','Q','S 0 8 16',
|
|
'S 0 -2147483648 20','S 0 2147483647 20','A 0 3','X 0',
|
|
'B 7','R','Q','Z 3','S 0 1_0 2','S 0 3 2147483648','S\v0 3 4',
|
|
' S\t+0000\r+0003 00020\r','T 0','Q',
|
|
'B 2','S 0 3 20','A 0 4','S 1 12 7','R 8 12 3 7 10 2','Q']
|
|
for mode in range(-2,16):
|
|
lines.extend(['S 0 3 50','S 0 %d 50'%mode,'Q',
|
|
'R 0 12 2 %d 15 4'%mode,'Q'])
|
|
for n in range(1,7):
|
|
lines.append('B %d'%n)
|
|
for i in range(n): lines.append('S %d %d %d'%(i,list(LABELS)[i%4],20+i))
|
|
lines += ['T 13','M 0 0 0','A 0 0','X -1','S -1 7 -1','S 0 8 -1']
|
|
valid=[str(v) for i in range(n) for v in (list(LABELS)[(i+1)%4],30+i,i)]
|
|
lines += ['R '+' '.join(valid),'T 22','Q']
|
|
for index in range(n):
|
|
bad=valid[:]; bad[index*3]='11'
|
|
lines += ['R '+' '.join(bad),'Q']
|
|
bad=valid[:]; bad[index*3+2]='1000001'
|
|
lines += ['R '+' '.join(bad)]
|
|
rng=random.Random(0x534D5631)
|
|
for block in range(12):
|
|
n=rng.randrange(1,7); lines.append('B %d'%n)
|
|
for step in range(28):
|
|
op=rng.choice('SSAATMMXRRQ')
|
|
index=rng.randrange(-1,n+1)
|
|
if op=='S': values=[index,rng.choice([0,3,8,12,1,2,7,11,13,-1]),rng.choice([-1,0,1,20,100,1000000,1000001])]
|
|
elif op=='A': values=[index,rng.choice([-1,0,1,3,25,1000001])]
|
|
elif op=='T': values=[rng.choice([-1,0,1,3,25,1000001])]
|
|
elif op=='M': values=[index,rng.randrange(-1,n+1),rng.choice([-1,0,1,10,1000001])]
|
|
elif op=='X': values=[index]
|
|
elif op=='R':
|
|
values=[v for i in range(n) for v in (rng.choice([0,3,8,12,7]),rng.randrange(0,60),rng.randrange(0,45))]
|
|
else: values=[]
|
|
lines.append(op+(' '+' '.join(map(str,values)) if values else ''))
|
|
lines += ['B 2','S 0 3 1000000','S 1 3 1000000','T 1000000','M 0 1 500000',
|
|
'T 1','Q','B '+('0'*5000)+'6',
|
|
'S '+('+'+'0'*5000)+'0 '+('+'+'0'*5000)+'3 '+('0'*5000)+'10',
|
|
'A 0 -'+('0'*5000),'Q']
|
|
return [line+'\n' for line in lines]
|
|
|
|
|
|
def exact_json(value):
|
|
if type(value) is list:
|
|
return all(exact_json(x) for x in value)
|
|
return type(value) in (int,str)
|
|
|
|
|
|
def main():
|
|
evidence=[]; failures=[]
|
|
scratch=None; business_ok=False; cleanup_ok=False; absence_ok=False
|
|
lines=scenarios()
|
|
expected=model(lines)
|
|
payload=''.join(lines).encode('ascii')
|
|
sources={name:(Path(sys.argv[1])/name).read_bytes() for name in CANDIDATES}
|
|
try:
|
|
scratch=Path(tempfile.mkdtemp(prefix='mist-state-'))
|
|
# All file preparation finishes before the first candidate-identity child.
|
|
for name,data in sources.items():
|
|
path=scratch/name; path.parent.mkdir(parents=True,exist_ok=True); path.write_bytes(data)
|
|
for name,text in SUPPORT.items():
|
|
path=scratch/name; path.parent.mkdir(parents=True,exist_ok=True); path.write_text(text)
|
|
for path in scratch.rglob('*'):
|
|
if path.is_dir(): os.chmod(path,0o777)
|
|
execute_stage('compiler',['make','-f','Makefile','all'],scratch,COMPILER_DEADLINE,b'',evidence)
|
|
output,diagnostic=execute_stage('candidate',[str(scratch/'build/mist-replay')],scratch,CANDIDATE_DEADLINE,payload,evidence)
|
|
actual=[]
|
|
for rawline in output.splitlines():
|
|
item=json.loads(rawline)
|
|
if not exact_json(item): raise ValueError('raw JSON type mismatch')
|
|
actual.append(item)
|
|
if actual!=expected:
|
|
first=next((i for i,(a,b) in enumerate(zip(actual,expected)) if a!=b),min(len(actual),len(expected)))
|
|
raise ValueError('normal raw observation mismatch row '+str(first))
|
|
business_ok=True
|
|
except BaseException as error:
|
|
failures.append(['business',type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/',CLEANUP_DEADLINE,b'',evidence)
|
|
cleanup_ok=True
|
|
except BaseException as error:
|
|
failures.append(['cleanup',type(error).__name__,str(error)])
|
|
try:
|
|
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/',ABSENCE_DEADLINE,b'',evidence)
|
|
absence_ok=True
|
|
except BaseException as error:
|
|
failures.append(['absence',type(error).__name__,str(error)])
|
|
record={'stages':evidence,'failures':failures,'business_ok':business_ok,
|
|
'cleanup_ok':cleanup_ok,'absence_ok':absence_ok,'rows':len(expected),
|
|
'input_len':len(payload),'input_sha256':hashlib.sha256(payload).hexdigest(),
|
|
'expected_len':len(json.dumps(expected,separators=(',',':')).encode()),
|
|
'scratch':str(scratch)}
|
|
marker=base64.b85encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode('ascii')
|
|
sys.stderr.write('MIST_EXEC:'+marker+'\n')
|
|
if business_ok and cleanup_ok and absence_ok and not failures:
|
|
print('PASS')
|
|
return 0
|
|
print('FAIL '+('; '.join(row[0]+':'+row[1]+':'+row[2] for row in failures) or 'incomplete flags'))
|
|
return 1
|
|
|
|
if __name__=='__main__':
|
|
sys.exit(main())
|