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>
424 lines
28 KiB
Python
424 lines
28 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[pid]=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,
|
|
'descendants':[[p,t['start'],signals.get((p,t['start']),0)]
|
|
for p,t in tracked.items() if leader is None or p!=leader.pid],
|
|
'adopted':[[p,None,rc] for p,rc in waits.items()],
|
|
'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) != 3 or not identity_int(row[0],1) or row[1] is not None or type(row[2]) is not int:
|
|
raise ValueError('invalid adopted 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())
|
|
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 = {'observer.c': '#include "output.h"\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <string.h>\n#include <limits.h>\n\nstruct port_state {\n int direction;\n int value;\n unsigned directions;\n unsigned writes;\n unsigned ignored;\n unsigned replacements;\n};\nstruct command {\n char opcode;\n int first;\n int second;\n int count;\n};\nstruct token {\n const char *begin;\n const char *end;\n};\nstatic struct port_state ports[OUTPUT_PINS];\nstatic uint64_t audit;\nstatic unsigned calls;\nstatic unsigned bad_calls;\n\nstatic int pin_exists(int pin)\n{\n return pin >= 0 && pin < OUTPUT_PINS;\n}\n\nstatic void record_call(int operation, int pin, int argument)\n{\n uint64_t word = (uint64_t)(17 * operation + 13 * pin + 7 * argument + 1);\n audit = (audit * 257 + word) % UINT64_C(1000000007);\n calls += 1;\n}\n\nvoid port_direction(int pin)\n{\n if (!pin_exists(pin)) {\n bad_calls += 1;\n return;\n }\n record_call(1, pin, 1);\n ports[pin].directions += 1;\n ports[pin].direction = 1;\n}\n\nvoid port_value(int pin, int value)\n{\n if (!pin_exists(pin) || (value != 0 && value != 1)) {\n bad_calls += 1;\n return;\n }\n record_call(2, pin, value);\n ports[pin].writes += 1;\n if (ports[pin].direction == 0) {\n ports[pin].ignored += 1;\n return;\n }\n ports[pin].value = value;\n}\n\nvoid port_rebind(int pin, int value)\n{\n if (!pin_exists(pin) || (value != 0 && value != 1)) {\n bad_calls += 1;\n return;\n }\n record_call(3, pin, value);\n ports[pin].replacements += 1;\n ports[pin].direction = 0;\n ports[pin].value = value;\n}\n\nstatic void initialize_ports(void)\n{\n int pin;\n for (pin = 0; pin < OUTPUT_PINS; ++pin) {\n ports[pin].direction = 0;\n ports[pin].value = 0;\n ports[pin].directions = 0;\n ports[pin].writes = 0;\n ports[pin].ignored = 0;\n ports[pin].replacements = 0;\n }\n audit = 0;\n calls = 0;\n bad_calls = 0;\n}\n\nstatic int horizontal(int character)\n{\n return character == \' \' || character == \'\\t\' || character == \'\\r\';\n}\n\nstatic int next_token(const char **cursor, struct token *token)\n{\n const char *position = *cursor;\n while (horizontal((unsigned char)*position)) {\n position += 1;\n }\n if (*position == \'\\0\' || *position == \'\\n\') {\n *cursor = position;\n return 0;\n }\n token->begin = position;\n while (*position != \'\\0\' && *position != \'\\n\' &&\n !horizontal((unsigned char)*position)) {\n position += 1;\n }\n token->end = position;\n *cursor = position;\n return 1;\n}\n\nstatic int decimal_token(struct token token, int *result)\n{\n const char *position = token.begin;\n int negative = 0;\n unsigned magnitude = 0;\n unsigned limit;\n if (position < token.end && (*position == \'+\' || *position == \'-\')) {\n negative = *position == \'-\';\n position += 1;\n }\n if (position == token.end) {\n return 0;\n }\n limit = negative ? (unsigned)INT_MAX + 1U : (unsigned)INT_MAX;\n while (position < token.end) {\n unsigned digit;\n if (*position < \'0\' || *position > \'9\') {\n return 0;\n }\n digit = (unsigned)(*position - \'0\');\n if (magnitude > (limit - digit) / 10U) {\n return 0;\n }\n magnitude = magnitude * 10U + digit;\n position += 1;\n }\n if (negative) {\n *result = magnitude == (unsigned)INT_MAX + 1U ? INT_MIN : -(int)magnitude;\n } else {\n *result = (int)magnitude;\n }\n return 1;\n}\n\nstatic int argument_count(char opcode)\n{\n switch (opcode) {\n case \'S\':\n case \'M\':\n case \'R\':\n return 2;\n case \'A\':\n case \'C\':\n case \'Q\':\n return 1;\n default:\n return -1;\n }\n}\n\nstatic int decode_line(const char *line, struct command *command)\n{\n const char *cursor = line;\n struct token token;\n int count;\n int values[2] = {0, 0};\n int index;\n if (!next_token(&cursor, &token)) {\n return 0;\n }\n if (token.end - token.begin != 1) {\n return -1;\n }\n command->opcode = *token.begin;\n count = argument_count(command->opcode);\n if (count < 0) {\n return -1;\n }\n for (index = 0; index < count; ++index) {\n if (!next_token(&cursor, &token) || !decimal_token(token, &values[index])) {\n return -1;\n }\n }\n if (next_token(&cursor, &token)) {\n return -1;\n }\n command->first = values[0];\n command->second = values[1];\n command->count = count;\n return 1;\n}\n\nstatic int dispatch(struct output_control *control, const struct command *command)\n{\n struct output_view view;\n switch (command->opcode) {\n case \'S\':\n return output_stage(control, command->first, command->second);\n case \'M\':\n return output_stage_mask(control, command->first, command->second);\n case \'A\':\n return output_apply(control, command->first);\n case \'C\':\n return output_cancel(control, command->first);\n case \'R\':\n return output_rebind(control, command->first, command->second);\n case \'Q\':\n return output_query(control, command->first, &view);\n default:\n return -1;\n }\n}\n\nstatic void print_control(const struct output_control *control)\n{\n printf(" %u %u %u", control->staged, control->committed, control->canceled);\n}\n\nstatic void print_port(int pin)\n{\n const struct port_state *port = &ports[pin];\n printf(" %d %d %u %u %u %u", port->direction, port->value,\n port->directions, port->writes, port->ignored, port->replacements);\n}\n\nstatic void print_view(const struct output_control *control, int pin)\n{\n struct output_view view = {-9, -9, -9};\n int result = output_query(control, pin, &view);\n printf(" %d %d %d %d", result, view.desired, view.pending, view.ready);\n}\n\nstatic void print_observation(unsigned sequence, int result,\n const struct output_control *control)\n{\n int pin;\n printf("%u %d %u %u %llu", sequence, result, calls, bad_calls,\n (unsigned long long)audit);\n print_control(control);\n for (pin = 0; pin < OUTPUT_PINS; ++pin) {\n print_view(control, pin);\n print_port(pin);\n }\n putchar(\'\\n\');\n}\n\nstatic int append_character(char **line, size_t *capacity, size_t *length, int character)\n{\n char *replacement;\n size_t next;\n if (*length + 1 >= *capacity) {\n next = *capacity == 0 ? 128 : *capacity * 2;\n if (next < *capacity || next > (size_t)INT_MAX) {\n return 0;\n }\n replacement = realloc(*line, next);\n if (replacement == NULL) {\n return 0;\n }\n *line = replacement;\n *capacity = next;\n }\n (*line)[*length] = (char)character;\n *length += 1;\n return 1;\n}\n\nstatic int read_line(FILE *stream, char **line, size_t *capacity)\n{\n int character;\n size_t length = 0;\n while ((character = fgetc(stream)) != EOF) {\n if (character == 0 || !append_character(line, capacity, &length, character)) {\n return -1;\n }\n if (character == \'\\n\') {\n break;\n }\n }\n if (ferror(stream)) {\n return -1;\n }\n if (length == 0 && character == EOF) {\n return 0;\n }\n if (!append_character(line, capacity, &length, 0)) {\n return -1;\n }\n return 1;\n}\n\nstatic int run_stream(FILE *stream)\n{\n struct output_control control;\n char *line = NULL;\n size_t capacity = 0;\n unsigned sequence = 0;\n int status = 0;\n initialize_ports();\n output_init(&control);\n for (;;) {\n struct command command;\n int read_result = read_line(stream, &line, &capacity);\n int decoded;\n int result;\n if (read_result == 0) {\n break;\n }\n if (read_result < 0) {\n status = 2;\n break;\n }\n decoded = decode_line(line, &command);\n if (decoded == 0) {\n continue;\n }\n result = decoded < 0 ? -2 : dispatch(&control, &command);\n sequence += 1;\n print_observation(sequence, result, &control);\n }\n free(line);\n return status;\n}\n\nint main(int argc, char **argv)\n{\n FILE *stream = stdin;\n int result;\n if (argc > 2) {\n return 2;\n }\n if (argc == 2) {\n stream = fopen(argv[1], "rb");\n if (stream == NULL) {\n return 2;\n }\n }\n result = run_stream(stream);\n if (stream != stdin && fclose(stream) != 0) {\n result = 2;\n }\n if (ferror(stdout)) {\n result = 2;\n }\n return result;\n}\n', 'Makefile': 'CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Werror -pedantic\n.PHONY: all clean demo replacement\nall: output-demo\noutput-demo: output.c output.h observer.c\n\t$(CC) $(CFLAGS) output.c observer.c -o $@\ndemo: output-demo\n\t./output-demo examples/default.txt\nreplacement: output-demo\n\t./output-demo examples/replacement.txt\nclean:\n\trm -f output-demo\n'}
|
|
COMPILE_SECONDS = 20
|
|
CANDIDATE_SECONDS = 10
|
|
CLEANUP_SECONDS = 5
|
|
ABSENCE_SECONDS = 5
|
|
|
|
|
|
def decimal(data):
|
|
if not data or data[:1] in (b'+', b'-') and len(data) == 1:
|
|
raise ValueError('empty decimal')
|
|
sign = -1 if data[:1] == b'-' else 1
|
|
digits = data[1:] if data[:1] in (b'+', b'-') else data
|
|
if any(x < 48 or x > 57 for x in digits):
|
|
raise ValueError('nondecimal stdout')
|
|
significant = digits.lstrip(b'0') or b'0'
|
|
if len(significant) > 12:
|
|
raise ValueError('observed integer outside public range')
|
|
return sign * int(significant)
|
|
|
|
|
|
def parse_rows(data):
|
|
# Public stdout rows are LF records separated by space, TAB or CR only.
|
|
import re
|
|
lines = data.split(b'\n')
|
|
if lines and lines[-1] == b'': lines.pop()
|
|
rows = []
|
|
for line in lines:
|
|
words = re.split(br'[ \t\r]+', line.strip(b' \t\r'))
|
|
if len(words) != 88: raise ValueError('raw row width')
|
|
rows.append([decimal(word) for word in words])
|
|
return rows
|
|
|
|
|
|
def scenario():
|
|
# Independent input generation; never evaluates or executes a fixture.
|
|
lines = ['S 2 1','A 4','S 2 0','A 4','M 129 129','A 255','Q 2','C 0',
|
|
'M 10 10','A 10','R 1 0','S 1 1','A 2','R 3 1','M 10 2','A 10']
|
|
lines += ['A 0','C 255','M 0 255','M 255 0','A 255','A 255','C 0',
|
|
'S -1 1','S 8 1','S 0 2','M 256 0','M 1 -1','A -1','C 256',
|
|
'R 8 0','R 0 2','Q -1','Q 8','Q 0','S 1 0 extra','S 1','Z 1','M 1_0 0']
|
|
lines += ['\tS\t+'+'0'*5000+'7\t+'+'0'*5000+'1\r', 'A +000128',
|
|
'S -'+'0'*5000+' 0','A 000001',' \t\r', '']
|
|
rng = random.Random(714093)
|
|
for index in range(410):
|
|
op = rng.choice('SSMMAACRQ')
|
|
if op in 'SR': line = '%s %d %d' % (op,rng.randrange(8),rng.randrange(2))
|
|
elif op == 'M': line = 'M %d %d' % (rng.randrange(256),rng.randrange(256))
|
|
elif op in 'AC': line = '%s %d' % (op,rng.randrange(256))
|
|
else: line = 'Q %d' % rng.randrange(8)
|
|
lines.append(line)
|
|
lines += ['M 255 165','A 255','M 255 90','C 85','A 255']
|
|
return ('\n'.join(lines)+'\n').encode('ascii')
|
|
|
|
|
|
def commands(payload):
|
|
import re
|
|
result = []
|
|
for line in payload.split(b'\n'):
|
|
words = re.split(br'[ \t\r]+',line.strip(b' \t\r')) if line.strip(b' \t\r') else []
|
|
if not words: continue
|
|
op = words[0]
|
|
count = 2 if op in (b'S',b'M',b'R') else 1 if op in (b'A',b'C',b'Q') else -1
|
|
try:
|
|
if count < 0 or len(words) != count+1: raise ValueError('syntax')
|
|
args = [decimal(word) for word in words[1:]]
|
|
if any(x < -2147483648 or x > 2147483647 for x in args): raise ValueError('int32')
|
|
result.append((op.decode('ascii'),args))
|
|
except ValueError: result.append(('?',[]))
|
|
return result
|
|
|
|
|
|
def compare(payload, data):
|
|
sequence = commands(payload)
|
|
rows = parse_rows(data)
|
|
if len(rows) != len(sequence): raise ValueError('raw row count')
|
|
desired = [0]*8; pending = [0]*8; ready = [0]*8
|
|
direction = [0]*8; value = [0]*8; directs = [0]*8
|
|
writes = [0]*8; ignored = [0]*8; replaced = [0]*8
|
|
staged = committed = canceled = audit = calls = 0
|
|
def event(op,p,arg):
|
|
nonlocal audit,calls
|
|
audit = (audit*257+17*op+13*p+7*arg+1)%1000000007
|
|
calls += 1
|
|
for index,((op,args),row) in enumerate(zip(sequence,rows),1):
|
|
rc = 0
|
|
if op == '?': rc = -2
|
|
elif op in ('S','R') and not (0 <= args[0] < 8 and args[1] in (0,1)): rc = -1
|
|
elif op == 'M' and not all(0 <= x <= 255 for x in args): rc = -1
|
|
elif op in ('A','C') and not 0 <= args[0] <= 255: rc = -1
|
|
elif op == 'Q' and not 0 <= args[0] < 8: rc = -1
|
|
elif op == 'S':
|
|
p,v = args; desired[p] = v; pending[p] = 1; staged += 1
|
|
elif op == 'M':
|
|
mask,bits = args; rc = 0
|
|
for p in range(8):
|
|
if mask & (1<<p):
|
|
desired[p] = int(bool(bits & (1<<p))); pending[p] = 1; staged += 1; rc += 1
|
|
elif op == 'C':
|
|
rc = 0
|
|
for p in range(8):
|
|
if args[0] & (1<<p) and pending[p]: pending[p] = 0; canceled += 1; rc += 1
|
|
elif op == 'R':
|
|
p,v = args; direction[p] = ready[p] = 0; value[p] = v; replaced[p] += 1; event(3,p,v)
|
|
elif op == 'A':
|
|
rc = 0
|
|
for p in range(8):
|
|
if args[0] & (1<<p) and pending[p]:
|
|
# Reasserting output is permitted. Its actual per-pin
|
|
# counter delta chooses the allowed event, never a value.
|
|
actual_directions = row[8+p*10+6]
|
|
delta = actual_directions-directs[p]
|
|
if delta not in ((0,1) if ready[p] else (1,)):
|
|
raise ValueError('direction establishment row %d pin %d'%(index,p))
|
|
if delta:
|
|
directs[p] += 1; direction[p] = 1; event(1,p,1)
|
|
ready[p] = 1; value[p] = desired[p]; writes[p] += 1; event(2,p,desired[p])
|
|
pending[p] = 0; committed += 1; rc += 1
|
|
expected = [index,rc,calls,0,audit,staged,committed,canceled]
|
|
for p in range(8):
|
|
expected += [0,desired[p],pending[p],ready[p],direction[p],value[p],directs[p],writes[p],ignored[p],replaced[p]]
|
|
if row != expected:
|
|
column = next(i for i,(a,b) in enumerate(zip(row,expected)) if a != b)
|
|
raise ValueError('raw observation row %d field %d got %d expected %d'%(index,column,row[column],expected[column]))
|
|
return len(rows)
|
|
|
|
|
|
def main():
|
|
evidence = []
|
|
errors = []
|
|
scratch = None
|
|
business_ok = cleanup_ok = absence_ok = False
|
|
checks = 0
|
|
try:
|
|
scratch = Path(tempfile.mkdtemp(prefix='output-order-'))
|
|
# All candidate-controlled tree reads/writes precede the first child.
|
|
sources = {name:(Path(sys.argv[1])/name).read_bytes() for name in ('output.c','output.h')}
|
|
for name,content in SUPPORT.items():
|
|
(scratch/name).write_text(content)
|
|
for name,content in sources.items():
|
|
(scratch/name).write_bytes(content)
|
|
payload = scenario()
|
|
(scratch/'input.txt').write_bytes(payload)
|
|
compile_argv = ['gcc','-std=c11','-O2','-Wall','-Wextra','-Werror','-pedantic',
|
|
'output.c','observer.c','-o','output-demo']
|
|
execute_stage('compiler',compile_argv,scratch,COMPILE_SECONDS,b'',evidence)
|
|
data,diagnostic = execute_stage('candidate',[str(scratch/'output-demo')],scratch,CANDIDATE_SECONDS,payload,evidence)
|
|
checks = compare(payload,data)
|
|
business_ok = True
|
|
except BaseException as error:
|
|
errors.append([type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/',CLEANUP_SECONDS,b'',evidence)
|
|
cleanup_ok = True
|
|
except BaseException as error: errors.append(['cleanup',type(error).__name__,str(error)])
|
|
try:
|
|
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/',ABSENCE_SECONDS,b'',evidence)
|
|
absence_ok = True
|
|
except BaseException as error: errors.append(['absence',type(error).__name__,str(error)])
|
|
marker = {'stages':evidence,'errors':errors,'business':business_ok,'cleanup':cleanup_ok,'absence':absence_ok,'rows':checks}
|
|
packed = base64.b64encode(bz2.compress(json.dumps(marker,separators=(',',':')).encode())).decode()
|
|
sys.stderr.write('OUTPUT_EVIDENCE '+packed+'\n')
|
|
if business_ok and cleanup_ok and absence_ok and not errors:
|
|
print('PASS')
|
|
return 0
|
|
print('FAIL '+repr(errors)[:220])
|
|
return 1
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|