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>
340 lines
25 KiB
Python
340 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
|
|
|
|
EVIDENCE_DIR = None
|
|
|
|
SUPERVISOR = "\nimport base64,bz2,ctypes,hashlib,json,os,signal,subprocess,sys,time,selectors\ncfg=json.loads(sys.argv[1]); start=time.monotonic(); errors=[]; tracked={}; signals={}; waits={}\nleader=None; leader_start=None; output=b''; diagnostic=b''; timed_out=False; drained=False\nselector=selectors.DefaultSelector(); buffers={'stdout':bytearray(),'stderr':bytearray()}; adopted=[]; pending=b''; offset=0\nreaped=False; leader_rc=None; remaining=[]; pg_absent=False\n\ndef fault(op, error):\n errors.append([op,type(error).__name__,str(error)])\n\ndef exact(pid):\n try:\n with open('/proc/%d/stat'%pid,'rb') as stream: data=stream.read()\n words=data[data.rfind(b')')+2:].split()\n return {'pid':pid,'state':words[0].decode('ascii'),'ppid':int(words[1]),'pg':int(words[2]),'start':int(words[19])}\n except (FileNotFoundError,ProcessLookupError): return None\n\ndef table_now():\n result={}\n for name in os.listdir('/proc'):\n if name.isascii() and name.isdigit():\n value=exact(int(name))\n if value is not None: result[value['pid']]=value\n return result\n\ndef closure(table, supervisor, remembered):\n roots={supervisor}\n for pid,old in remembered.items():\n current=table.get(pid)\n if current and current['start']==old['start'] and current['state'] not in ('Z','X'):\n roots.add(pid)\n found={}\n changed=True\n while changed:\n changed=False\n for pid,current in table.items():\n if pid!=supervisor and pid not in found and (current['ppid'] in roots or pid in roots):\n found[pid]=current; roots.add(pid); changed=True\n return found\n\ndef discover():\n table=table_now(); found=closure(table,os.getpid(),tracked)\n for pid,current in found.items():\n if pid not in tracked or tracked[pid]['start']!=current['start']:\n tracked[pid]=dict(current)\n return table,found\n\ndef send_fresh(pid, token):\n current=exact(pid)\n if current is None or current['start']!=token['start'] or current['state'] in ('Z','X'): return\n os.kill(pid,signal.SIGKILL)\n signals[(pid,token['start'])]=signals.get((pid,token['start']),0)+1\n\ndef own_wait():\n global reaped,leader_rc\n if leader is not None:\n value=leader.poll()\n if value is not None:\n leader_rc=leader.wait(timeout=0)\n reaped=True\n\ndef adopted_wait():\n if leader is not None and not reaped: return\n while True:\n try: pid,status=os.waitpid(-1,os.WNOHANG)\n except ChildProcessError: break\n if pid==0: break\n adopted.append([pid,None,status,os.waitstatus_to_exitcode(status)])\n\ndef pump(timeout):\n global offset,drained,output,diagnostic\n for key,mask in selector.select(timeout):\n stream=key.fileobj; kind=key.data\n if kind=='stdin':\n try: written=os.write(stream.fileno(),pending[offset:offset+65536]); offset+=written\n except BrokenPipeError: offset=len(pending)\n except BlockingIOError: continue\n if offset==len(pending): selector.unregister(stream); stream.close()\n else:\n try: data=os.read(stream.fileno(),65536)\n except BlockingIOError: continue\n if data: buffers[kind].extend(data)\n else: selector.unregister(stream); stream.close()\n drained=not any(key.data in ('stdout','stderr') for key in selector.get_map().values())\n\nsupervisor_start=exact(os.getpid())['start']\ntry:\n libc=ctypes.CDLL(None,use_errno=True)\n if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')\n pending=sys.stdin.buffer.read()\n leader=subprocess.Popen(cfg['argv'],cwd=cfg['cwd'],stdin=subprocess.PIPE,\n stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)\n info=exact(leader.pid)\n if info is None: raise RuntimeError('leader identity disappeared before capture')\n leader_start=info['start']; tracked[leader.pid]=info\n for stream,kind in ((leader.stdout,'stdout'),(leader.stderr,'stderr')):\n os.set_blocking(stream.fileno(),False); selector.register(stream,selectors.EVENT_READ,kind)\n if pending:\n os.set_blocking(leader.stdin.fileno(),False); selector.register(leader.stdin,selectors.EVENT_WRITE,'stdin')\n else: leader.stdin.close()\n while True:\n pump(0.02); own_wait(); discover()\n if reaped and drained: break\n if time.monotonic()-start>=cfg['deadline']:\n timed_out=True; break\nexcept BaseException as error:\n fault('execute',error)\nfinally:\n recovery_end=time.monotonic()+cfg['recovery']\n while True:\n try: own_wait()\n except BaseException as error: fault('own_wait',error)\n found={}\n try: _,found=discover()\n except BaseException as error: fault('discover',error)\n for pid,token in list(found.items()):\n try: send_fresh(pid,token)\n except (FileNotFoundError,ProcessLookupError): pass\n except BaseException as error: fault('signal',error)\n try: own_wait()\n except BaseException as error: fault('own_wait',error)\n try: adopted_wait()\n except BaseException as error: fault('adopted_wait',error)\n if leader is not None and not drained:\n try: pump(0.02); own_wait()\n except BaseException as error: fault('drain',error)\n try:\n table,found=discover()\n remaining=[[p,v['start'],v['state']] for p,v in found.items()]\n pg_absent=leader is not None and not any(v['pg']==leader.pid for v in table.values())\n except BaseException as error:\n fault('absence',error); remaining=[['unknown']]; pg_absent=False\n if reaped and drained and not remaining and pg_absent: break\n if time.monotonic()>=recovery_end:\n fault('recovery',RuntimeError('deadline with unconfirmed closure')); break\n time.sleep(0.01)\n\noutput=bytes(buffers['stdout']); diagnostic=bytes(buffers['stderr'])\nmeta={'phase':cfg['phase'],'nonce':cfg['nonce'],'argv':cfg['argv'],\n 'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),\n 'supervisor_pid':os.getpid(),'supervisor_start':supervisor_start,\n 'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,\n 'leader_rc':leader_rc,'leader_reaped':reaped,'timeout':timed_out,\n 'controlled_rc':124 if timed_out else leader_rc,'elapsed':time.monotonic()-start,\n 'deadline':cfg['deadline'],'recovery':cfg['recovery'],\n 'pg_absent':pg_absent,'remaining':remaining,'exceptions':errors,\n 'adopted':adopted,\n 'descendants':[[p,t['start'],signals.get((p,t['start']),0)]\n for p,t in tracked.items() if leader is None or p!=leader.pid],\n 'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),\n 'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}\nwire={'meta':meta,'stdout':base64.b64encode(bz2.compress(output)).decode('ascii'),\n 'stderr':base64.b64encode(bz2.compress(diagnostic)).decode('ascii')}\nsys.stdout.write(json.dumps(wire,separators=(',',':'))+'\\n')\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 type(row[2]) is not int or type(row[3]) is not int or os.waitstatus_to_exitcode(row[2]) != row[3]:
|
|
raise ValueError('invalid adopted wait fact')
|
|
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())
|
|
evidence.append(entry)
|
|
if EVIDENCE_DIR is not None:
|
|
target=Path(EVIDENCE_DIR) / (phase+'.'+nonce)
|
|
target.with_suffix(target.suffix+'.wire').write_bytes(raw.stdout)
|
|
target.with_suffix(target.suffix+'.wrapper-stderr').write_bytes(raw.stderr)
|
|
target.with_suffix(target.suffix+'.input').write_bytes(payload)
|
|
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 = {'observer.c': '#define _POSIX_C_SOURCE 200809L\n#include "profile.h"\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nint main(void)\n{\n Bank bank;\n char *line = NULL;\n size_t capacity = 0;\n ssize_t length;\n bank_init(&bank);\n while ((length = getline(&line, &capacity, stdin)) >= 0) {\n CommandResult result;\n Summary summary;\n if (length > 0 && line[length - 1] == \'\\n\') {\n line[length - 1] = \'\\0\';\n }\n bank_command(&bank, line, &result);\n bank_summary(&bank, &summary);\n printf("%d %" PRIu64 " %zu %" PRId64 " %" PRId64 " %" PRId64,\n result.rc, bank.epoch, summary.count, summary.x_total,\n summary.y_total, summary.limit_total);\n printf(" %zu %zu %" PRIu64, summary.negative_gains,\n summary.zero_limits, summary.digest);\n printf(" %" PRId64 " %" PRId64 " %" PRId64 " %d",\n result.measurement.raw, result.measurement.value,\n result.measurement.distance, result.measurement.clipped);\n for (int64_t id = 0; id < BANK_CAPACITY; ++id) {\n Profile profile = {0};\n uint64_t revision = 0;\n int rc = bank_get(&bank, id, &profile, &revision);\n printf(" %d %" PRIu64, rc, revision);\n printf(" %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,\n profile.x, profile.y, profile.gain, profile.bias, profile.limit);\n }\n putchar(\'\\n\');\n }\n free(line);\n return ferror(stdin) || ferror(stdout) ? 2 : 0;\n}\n'}
|
|
CANDIDATES = ['include/layout.h', 'include/profile.h', 'src/store.c', 'src/projection.c', 'src/command.c', 'Makefile']
|
|
|
|
import copy
|
|
import re
|
|
|
|
COMPILE_SECONDS = 20
|
|
CANDIDATE_SECONDS = 8
|
|
CLEANUP_SECONDS = 5
|
|
ABSENCE_SECONDS = 5
|
|
|
|
|
|
def integer_token(token):
|
|
if not re.fullmatch(rb'[+-]?[0-9]+', token):
|
|
raise ValueError('non-ASCII decimal token')
|
|
negative = token.startswith(b'-')
|
|
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
|
|
if len(digits) > 20:
|
|
raise ValueError('integer beyond all published observable ranges')
|
|
value = int(digits)
|
|
return -value if negative else value
|
|
|
|
|
|
def commands():
|
|
lines = ['RESET','SUMMARY','RESTORE 0','GET 8','GET 0',
|
|
'PUT 0 3 4 2 1 1000','MEASURE 0 8 9',
|
|
'PATCH 0 1 0 999 999 999 999','MEASURE 0 8 9',
|
|
'SAVE 0','PUT 1 -3 6 -4 -2 0','SAVE 1','SWAP 0 1',
|
|
'RESTORE 0','CLONE 0 7','PATCH 7 30 999 0 0 0 0',
|
|
'MEASURE 7 -10000 10000','RESTORE 1','SUMMARY']
|
|
for field in range(5):
|
|
for value in [0,-1,1,-1000,1000,-16,16,100000]:
|
|
args = [9223372036854775807]*5
|
|
args[field] = value
|
|
lines += ['PUT 2 7 -5 -3 9 20', 'PATCH 2 %d %s' % (1<<field,' '.join(map(str,args))),
|
|
'MEASURE 2 10000 -10000']
|
|
lines += ['RESET','SAVE 0','PUT 3 -1000 1000 -16 -1000 100000',
|
|
'CLONE 3 3','SWAP 3 3','PATCH 3 0 9999 9999 9999 9999 9999',
|
|
'PUT 3 1001 0 0 0 0','PATCH 3 31 1 2 17 4 5',
|
|
'DELETE 3','DELETE 3','RESTORE 0','RESTORE 2','SAVE -1',
|
|
'CLONE 8 0','SWAP 0 8','MEASURE 0 10001 0',
|
|
'PUT 0 -1000 1000 16 1000 0','MEASURE 0 -1000 1000',
|
|
'MEASURE 0 10000 -10000','PUT 7 1000 -1000 -16 -1000 100000',
|
|
'MEASURE 7 -10000 10000','SAVE 1','RESET','RESTORE 1']
|
|
lines += ['', 'PUT 1_0 0 0 0 0 0','GET +','GET 9223372036854775808',
|
|
'GET -9223372036854775809','GET 1\v','PUT 0 1 2 3 4',
|
|
'SUMMARY 0','UNKNOWN','PUT 0 0 0 0 0 0 extra',
|
|
'GET '+('0'*5000), 'PUT +0000 -000 +000 000 -000 +000',
|
|
'\tGET\t+000\r ', 'GET -0', 'PUT 0 -9223372036854775808 0 0 0 0']
|
|
rng = random.Random(163)
|
|
for step in range(360):
|
|
kind = rng.randrange(11)
|
|
a,b = rng.randrange(-1,9),rng.randrange(-1,9)
|
|
fields = [rng.randint(-1000,1000),rng.randint(-1000,1000),rng.randint(-16,16),rng.randint(-1000,1000),rng.choice([0,1,17,100000])]
|
|
if step%5==0:
|
|
fields[rng.randrange(5)] = 0
|
|
if kind == 0:
|
|
line = 'PUT %d %s' % (a,' '.join(map(str,fields)))
|
|
elif kind == 1:
|
|
line = 'PATCH %d %d %s' % (a,rng.randrange(-1,33),' '.join(map(str,fields)))
|
|
elif kind == 2:
|
|
line = 'DELETE %d' % a
|
|
elif kind == 3:
|
|
line = 'CLONE %d %d' % (a,b)
|
|
elif kind == 4:
|
|
line = 'SWAP %d %d' % (a,b)
|
|
elif kind == 5:
|
|
line = 'SAVE %d' % rng.randrange(-1,3)
|
|
elif kind == 6:
|
|
line = 'RESTORE %d' % rng.randrange(-1,3)
|
|
elif kind == 7:
|
|
line = 'GET %d' % a
|
|
elif kind == 8:
|
|
line = 'MEASURE %d %d %d' % (a,rng.choice([-10001,-10000,0,10000,10001]),rng.randint(-10000,10000))
|
|
elif kind == 9:
|
|
line = 'SUMMARY'
|
|
else:
|
|
line = 'RESET' if step%31==0 else 'PATCH 0 31 0 0 0 0 0'
|
|
lines.append(line)
|
|
return lines
|
|
|
|
|
|
def expected_rows(lines):
|
|
slots = [None]*8
|
|
saved = [None,None]
|
|
epoch = 0
|
|
output = []
|
|
limits = [(-1000,1000),(-1000,1000),(-16,16),(-1000,1000),(0,100000)]
|
|
arities = dict(RESET=0,SUMMARY=0,PUT=6,PATCH=7,DELETE=1,CLONE=2,SWAP=2,SAVE=1,RESTORE=1,GET=1,MEASURE=3)
|
|
for line in lines:
|
|
rc = 0
|
|
measure = [0,0,0,0]
|
|
parts = re.split(rb'[ \t\r]+',line.encode('ascii').strip(b' \t\r'))
|
|
command = parts[0].decode('ascii') if parts else ''
|
|
args = []
|
|
try:
|
|
if len(line)>16384 or command not in arities or len(parts)!=arities[command]+1:
|
|
raise ValueError('syntax')
|
|
args = [integer_token(word) for word in parts[1:]]
|
|
if any(not -(1<<63)<=v<(1<<63) for v in args):
|
|
raise ValueError('int64')
|
|
except ValueError:
|
|
rc = -4
|
|
if rc == 0:
|
|
ids = args[:2] if command in ('CLONE','SWAP') else args[:1] if command in ('PUT','PATCH','DELETE','GET','MEASURE') else []
|
|
if any(not 0<=v<8 for v in ids):
|
|
rc = -1
|
|
elif command == 'PATCH' and not 0<=args[1]<=31:
|
|
rc = -1
|
|
elif command == 'MEASURE' and any(not -10000<=v<=10000 for v in args[1:]):
|
|
rc = -1
|
|
elif command in ('SAVE','RESTORE') and not 0<=args[0]<2:
|
|
rc = -1
|
|
elif command in ('PATCH','DELETE','GET','MEASURE','CLONE') and slots[args[0]] is None:
|
|
rc = -2
|
|
elif command == 'SWAP' and any(slots[i] is None for i in args[:2]):
|
|
rc = -2
|
|
elif command == 'RESTORE' and saved[args[0]] is None:
|
|
rc = -3
|
|
if rc == 0:
|
|
if command == 'RESET':
|
|
slots=[None]*8; saved=[None,None]; epoch=0
|
|
elif command in ('PUT','PATCH'):
|
|
if command == 'PUT':
|
|
value=args[1:]
|
|
else:
|
|
value=[args[2+f] if args[1]&(1<<f) else slots[args[0]][0][f] for f in range(5)]
|
|
if any(not low<=v<=high for v,(low,high) in zip(value,limits)):
|
|
rc=-1
|
|
else:
|
|
epoch+=1; slots[args[0]]=(list(value),epoch)
|
|
elif command == 'DELETE':
|
|
epoch+=1; slots[args[0]]=None
|
|
elif command == 'CLONE':
|
|
value=list(slots[args[0]][0]); epoch+=1; slots[args[1]]=(value,epoch)
|
|
elif command == 'SWAP':
|
|
left,right=args; a,b=slots[left][0],slots[right][0];epoch+=1
|
|
slots[left]=(list(b),epoch);slots[right]=(list(a),epoch)
|
|
elif command == 'SAVE':
|
|
saved[args[0]]=copy.deepcopy(slots)
|
|
elif command == 'RESTORE':
|
|
epoch+=1; slots=[None if entry is None else (list(entry[0]),epoch) for entry in saved[args[0]]]
|
|
elif command == 'MEASURE':
|
|
x,y,gain,bias,limit=slots[args[0]][0]
|
|
dx,dy=args[1]-x,args[2]-y
|
|
raw=dx*gain+dy+bias; value=max(-limit,min(limit,raw))
|
|
measure=[raw,value,abs(dx)+abs(dy),int(raw!=value)]
|
|
existing=[entry for entry in slots if entry is not None]
|
|
h=17
|
|
for index,entry in enumerate(slots):
|
|
words=[index,int(entry is not None)] + ([] if entry is None else [entry[1],*entry[0]])
|
|
for word in words:
|
|
h=(h*257+word)%1000003
|
|
row=[rc,epoch,len(existing),sum(e[0][0] for e in existing),sum(e[0][1] for e in existing),sum(e[0][4] for e in existing),sum(e[0][2]<0 for e in existing),sum(e[0][4]==0 for e in existing),h,*measure]
|
|
for entry in slots:
|
|
row += [-2,0,0,0,0,0,0] if entry is None else [0,entry[1],*entry[0]]
|
|
output.append(row)
|
|
return output
|
|
|
|
|
|
def parse_observations(data):
|
|
if data.endswith(b'\n'):
|
|
data=data[:-1]
|
|
rows=[]
|
|
for line in data.split(b'\n'):
|
|
tokens=re.split(rb'[ \t\r]+',line.strip(b' \t\r'))
|
|
rows.append([integer_token(token) for token in tokens])
|
|
return rows
|
|
|
|
|
|
def main():
|
|
evidence=[]; failures=[]; scratch=None; checked=0
|
|
try:
|
|
lines=commands(); expected=expected_rows(lines)
|
|
payload=('\n'.join(lines)+'\n').encode('ascii')
|
|
candidate=Path(sys.argv[1])
|
|
scratch=Path(tempfile.mkdtemp(prefix='layout-fields-'))
|
|
work=scratch/'work';work.mkdir(mode=0o777)
|
|
for name in CANDIDATES:
|
|
path=work/name;path.parent.mkdir(parents=True,exist_ok=True)
|
|
path.write_bytes((candidate/name).read_bytes())
|
|
for name,data in SUPPORT.items():
|
|
path=work/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(data)
|
|
# All support and inputs exist before the first candidate-identity child.
|
|
for path in [work,work/'src',work/'include']:
|
|
path.chmod(0o777)
|
|
for layout in range(4):
|
|
argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-Werror','-Iinclude',
|
|
'-DPROFILE_LAYOUT='+str(layout),'src/store.c','src/projection.c','src/command.c','observer.c','-o','bank'+str(layout)]
|
|
execute_stage('compiler'+str(layout),argv,work,COMPILE_SECONDS,b'',evidence)
|
|
for layout in range(4):
|
|
out,err=execute_stage('candidate'+str(layout),[str(work/('bank'+str(layout)))],work,CANDIDATE_SECONDS,payload,evidence)
|
|
actual=parse_observations(out)
|
|
if len(actual)!=len(expected):
|
|
raise ValueError('raw row count differs layout '+str(layout))
|
|
for index,(observed,wanted) in enumerate(zip(actual,expected)):
|
|
if observed!=wanted:
|
|
raise ValueError('raw observation differs layout %d row %d'%(layout,index))
|
|
checked+=1
|
|
except BaseException as error:
|
|
failures.append(['main',type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
for phase,program,deadline in [('cleanup',CLEANUP,CLEANUP_SECONDS),('absence',ABSENCE,ABSENCE_SECONDS)]:
|
|
try:
|
|
execute_stage(phase,[sys.executable,'-I','-c',program,str(scratch)],'/tmp',deadline,b'',evidence)
|
|
except BaseException as error:
|
|
failures.append([phase,type(error).__name__,str(error)])
|
|
else:
|
|
failures.append(['preparation','RuntimeError','scratch was not created'])
|
|
record=dict(stages=evidence,exceptions=failures,scratch=None if scratch is None else str(scratch),checked_rows=checked,
|
|
original_wire='protected' if EVIDENCE_DIR else 'not stored; actual lengths and SHA256 only')
|
|
marker=base64.b85encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode('ascii')
|
|
sys.stderr.write('LAYOUT_META85 '+marker+'\n')
|
|
if failures or not checked:
|
|
print('FAIL: '+('; '.join(row[0]+':'+row[1]+':'+row[2] for row in failures) or 'no behavior checks'))
|
|
return 1
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|