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>
474 lines
29 KiB
Python
474 lines
29 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]=[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,
|
|
'descendants':[[p,t['start'],signals.get((p,t['start']),0),None]
|
|
for p,t in tracked.items() if leader is None or p!=leader.pid],
|
|
'adopted_waits':[[p,None,s[0],s[1]] for p,s 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_waits'}
|
|
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_waits'):
|
|
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) != 4 or not all(identity_int(x,1) for x in row[:2]) or not identity_int(row[2]) or row[3] is not None:
|
|
raise ValueError('invalid descendant record')
|
|
for row in meta['adopted_waits']:
|
|
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())
|
|
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 = {'render_api.h': '#ifndef RENDER_API_H\n#define RENDER_API_H\n#include <stddef.h>\n#include <stdint.h>\n#define LABEL_LIMIT 48\n#define TEXT_LIMIT 512\n#define SESSION_LIMIT 64\n#define PORT_SLOTS 16\nstruct record {\n int32_t identifier;\n unsigned level;\n int32_t value;\n unsigned char label[LABEL_LIMIT];\n size_t label_length;\n};\nstruct command {\n unsigned sequence;\n size_t capacity;\n int allocation_failure;\n int sink_failure;\n struct record record;\n};\nstruct render_result {\n int status;\n size_t required;\n size_t emitted;\n};\nstruct session {\n unsigned accepted;\n unsigned rejected;\n unsigned last_sequence;\n unsigned successful;\n unsigned failed;\n uint32_t digest;\n};\nenum render_status {\n RENDER_OK = 0,\n RENDER_TRUNCATED = 1,\n RENDER_ALLOC = 2,\n RENDER_SINK = 3,\n RENDER_INVALID = 4\n};\nvoid session_init(struct session *session);\nint session_parse(const char *line, struct command *command);\nstruct render_result session_apply(struct session *session,\n const struct command *command);\nstruct render_result render_record(const struct command *command);\nsize_t codec_record_size(const struct record *record);\nsize_t codec_record_write(const struct record *record, char *target,\n size_t capacity);\nint codec_hex_decode(const char *text, unsigned char *out, size_t *length);\nuint32_t codec_digest(uint32_t previous, const unsigned char *data,\n size_t length);\n#endif\n', 'buffer_port.h': '#ifndef BUFFER_PORT_H\n#define BUFFER_PORT_H\n#include <stddef.h>\n#include <stdint.h>\nstruct port_observation {\n unsigned live;\n unsigned allocated;\n unsigned released;\n unsigned published;\n unsigned invalid;\n unsigned allocation_attempts;\n unsigned sink_attempts;\n uint32_t live_digest;\n size_t output_length;\n unsigned char output[512];\n};\nvoid port_begin(int allocation_failure, int sink_failure);\nchar *port_allocate(size_t capacity);\nint port_release(char *buffer);\nint port_publish(const char *buffer, size_t length);\nstruct port_observation port_observe(void);\n#endif\n', 'buffer_port.c': '#include "buffer_port.h"\n#include "render_api.h"\n#include <string.h>\nstruct allocation {\n int live;\n size_t capacity;\n unsigned char bytes[TEXT_LIMIT];\n};\nstatic struct allocation allocations[PORT_SLOTS];\nstatic struct port_observation counters;\nstatic int fail_allocate;\nstatic int fail_sink;\nstatic uint32_t append_digest(uint32_t value, unsigned char byte)\n{\n return value * UINT32_C(33) + byte + UINT32_C(1);\n}\nvoid port_begin(int allocation_failure, int sink_failure)\n{\n fail_allocate = allocation_failure;\n fail_sink = sink_failure;\n counters.output_length = 0;\n memset(counters.output, 0, sizeof(counters.output));\n}\nchar *port_allocate(size_t capacity)\n{\n counters.allocation_attempts++;\n if (fail_allocate || capacity == 0 || capacity > TEXT_LIMIT) {\n return NULL;\n }\n for (unsigned slot = 0; slot < PORT_SLOTS; slot++) {\n if (!allocations[slot].live) {\n allocations[slot].live = 1;\n allocations[slot].capacity = capacity;\n memset(allocations[slot].bytes, 0xa5, TEXT_LIMIT);\n counters.allocated++;\n return (char *)allocations[slot].bytes;\n }\n }\n return NULL;\n}\nint port_release(char *buffer)\n{\n for (unsigned slot = 0; slot < PORT_SLOTS; slot++) {\n if (buffer == (char *)allocations[slot].bytes) {\n if (!allocations[slot].live) {\n counters.invalid++;\n return 0;\n }\n allocations[slot].live = 0;\n memset(allocations[slot].bytes, 0xdd, TEXT_LIMIT);\n counters.released++;\n return 1;\n }\n }\n counters.invalid++;\n return 0;\n}\nint port_publish(const char *buffer, size_t length)\n{\n counters.sink_attempts++;\n for (unsigned slot = 0; slot < PORT_SLOTS; slot++) {\n struct allocation *allocation = &allocations[slot];\n if (buffer != (const char *)allocation->bytes) {\n continue;\n }\n if (!allocation->live || length >= allocation->capacity ||\n allocation->bytes[length] != 0) {\n counters.invalid++;\n return 0;\n }\n if (fail_sink) {\n return 0;\n }\n counters.published++;\n counters.output_length = length;\n memcpy(counters.output, allocation->bytes, length);\n return 1;\n }\n counters.invalid++;\n return 0;\n}\nstruct port_observation port_observe(void)\n{\n struct port_observation observation = counters;\n observation.live = 0;\n observation.live_digest = 0;\n for (unsigned slot = 0; slot < PORT_SLOTS; slot++) {\n const struct allocation *allocation = &allocations[slot];\n if (allocation->live) {\n observation.live++;\n observation.live_digest = append_digest(observation.live_digest,\n (unsigned char)slot);\n observation.live_digest = append_digest(observation.live_digest,\n (unsigned char)(allocation->capacity & 255U));\n observation.live_digest = append_digest(observation.live_digest,\n (unsigned char)(allocation->capacity >> 8));\n for (size_t index = 0; index < allocation->capacity; index++) {\n observation.live_digest = append_digest(observation.live_digest,\n allocation->bytes[index]);\n }\n }\n }\n return observation;\n}\n', 'observer.c': '#include "render_api.h"\n#include "buffer_port.h"\n#include <stdio.h>\n#include <string.h>\nstatic void output_hex(const unsigned char *data, size_t length)\n{\n if (length == 0) {\n putchar(\'-\');\n return;\n }\n for (size_t index = 0; index < length; index++) {\n printf("%02x", (unsigned)data[index]);\n }\n}\nstatic void print_observation(unsigned row, int parsed,\n const struct render_result *result,\n const struct session *session)\n{\n struct port_observation observed = port_observe();\n printf("%u %d %d %zu %zu ", row, parsed, result->status,\n result->required, result->emitted);\n printf("%u %u %u %u %u %u %u %u ", observed.live,\n observed.allocated, observed.released, observed.published,\n observed.invalid, observed.allocation_attempts,\n observed.sink_attempts, observed.live_digest);\n printf("%u %u %u %u %u %u %zu ", session->accepted,\n session->rejected, session->last_sequence, session->successful,\n session->failed, session->digest, observed.output_length);\n output_hex(observed.output, observed.output_length);\n putchar(\'\\n\');\n}\nint main(void)\n{\n char line[1024];\n unsigned row = 0;\n struct session session;\n session_init(&session);\n while (fgets(line, sizeof(line), stdin) != NULL) {\n struct command command;\n struct render_result result;\n int parsed;\n if (strchr(line, \'\\n\') == NULL && !feof(stdin)) {\n return 65;\n }\n row++;\n parsed = session_parse(line, &command);\n result.status = RENDER_INVALID;\n result.required = 0;\n result.emitted = 0;\n port_begin(parsed ? command.allocation_failure : 0,\n parsed ? command.sink_failure : 0);\n if (parsed) {\n result = session_apply(&session, &command);\n } else {\n session.rejected++;\n }\n print_observation(row, parsed, &result, &session);\n }\n return ferror(stdin) ? 66 : 0;\n}\n', 'Makefile': 'CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Werror -pedantic\nSOURCES = codec.c render.c session.c buffer_port.c observer.c\nHEADERS = render_api.h buffer_port.h\n.PHONY: all clean demo capacity\nall: render-demo\nrender-demo: $(SOURCES) $(HEADERS)\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\ndemo: render-demo\n\t./render-demo < examples/default.txt\ncapacity: render-demo\n\t./render-demo < examples/capacity.txt\nclean:\n\trm -f render-demo\n'}
|
|
|
|
COMPILE_DEADLINE = 20
|
|
CANDIDATE_DEADLINE = 8
|
|
CLEANUP_DEADLINE = 5
|
|
ABSENCE_DEADLINE = 5
|
|
CANDIDATES = ('codec.c','render.c','session.c')
|
|
|
|
|
|
def canonical(identifier, level, value, label):
|
|
escaped = bytearray()
|
|
for byte in label:
|
|
if byte in (34,92):
|
|
escaped.extend((92,byte))
|
|
elif 32 <= byte <= 126:
|
|
escaped.append(byte)
|
|
else:
|
|
escaped.extend(('\\u00%02x' % byte).encode('ascii'))
|
|
return (b'{"id":'+str(identifier).encode()+b',"level":'+str(level).encode()
|
|
+b',"value":'+str(value).encode()+b',"label":"'+escaped+b'"}')
|
|
|
|
|
|
def digest(previous, data):
|
|
for byte in data:
|
|
previous = (33*previous+byte+1) & 0xffffffff
|
|
return previous
|
|
|
|
|
|
def input_number(text, lower, upper):
|
|
sign = 1
|
|
if text[:1] in (b'+',b'-'):
|
|
sign = -1 if text[:1] == b'-' else 1
|
|
text = text[1:]
|
|
if not text or any(byte < 48 or byte > 57 for byte in text):
|
|
raise ValueError('decimal syntax')
|
|
significant = text.lstrip(b'0') or b'0'
|
|
if len(significant) > 10:
|
|
raise ValueError('decimal range')
|
|
value = sign * int(significant)
|
|
if not lower <= value <= upper:
|
|
raise ValueError('decimal range')
|
|
return value
|
|
|
|
|
|
def parse_input(line):
|
|
# Only the documented horizontal separators participate; LF is physical framing.
|
|
if not line.endswith(b'\n'):
|
|
raise ValueError('missing LF')
|
|
text = line[:-1]
|
|
fields = text.replace(b'\t',b' ').replace(b'\r',b' ').split(b' ')
|
|
fields = [field for field in fields if field]
|
|
if len(fields) != 9 or fields[0] != b'R':
|
|
raise ValueError('line shape')
|
|
domains = [(1,64),(-2**31,2**31-1),(0,7),(-2**31,2**31-1),(0,512),(0,1),(0,1)]
|
|
numbers = [input_number(field,*domain) for field,domain in zip(fields[1:8],domains)]
|
|
label = fields[8]
|
|
if label == b'-':
|
|
label = b''
|
|
else:
|
|
if not 2 <= len(label) <= 96 or len(label)%2 or any(x not in b'0123456789abcdefABCDEF' for x in label):
|
|
raise ValueError('hex syntax')
|
|
label = bytes.fromhex(label.decode('ascii'))
|
|
return numbers+[label]
|
|
|
|
|
|
def build_cases():
|
|
rng = random.Random(531042)
|
|
lines = []
|
|
def add(seq, identifier, level, value, cap, alloc=0, sink=0, label=b''):
|
|
text = 'R %d %d %d %d %d %d %d %s\n' % (seq,identifier,level,value,cap,alloc,sink,label.hex() or '-')
|
|
lines.append(text.encode('ascii'))
|
|
# Default replay never contains an exact-capacity input.
|
|
add(1,42,2,-17,128,label=b'sensor')
|
|
add(2,43,0,99,128,label=b'relay')
|
|
sequence = 3
|
|
for label in [b'',b'"\\',bytes([0,9,10,31,127,255]),bytes(range(48)),b'x'*48]:
|
|
identifier = rng.choice([-2**31,-1,0,7,2**31-1])
|
|
value = rng.choice([-2**31,-1,0,2**31-1])
|
|
length = len(canonical(identifier,7,value,label))
|
|
for cap in [0,1,length-1,length,length+1,512]:
|
|
add(sequence,identifier,7,value,cap,label=label)
|
|
sequence += 1
|
|
for alloc,sink in [(1,0),(0,1),(1,1),(0,0)]:
|
|
add(sequence,-77,3,100,512,alloc,sink,b'failure\x00path')
|
|
sequence += 1
|
|
add(sequence,5,1,6,128,label=b'accepted')
|
|
add(sequence,7,2,8,128,label=b'duplicate')
|
|
add(1,9,3,10,128,label=b'old')
|
|
sequence += 1
|
|
for _ in range(64-sequence+1):
|
|
label = bytes(rng.randrange(256) for _ in range(rng.randrange(49)))
|
|
length = len(canonical(-sequence,sequence%8,sequence*3,label))
|
|
add(sequence,-sequence,sequence%8,sequence*3,rng.choice([length,length+1,512]),label=label)
|
|
sequence += 1
|
|
lines += [b'\n',b'R 65 1 0 0 128 0 0 -\n',b'R 1 2147483648 0 0 128 0 0 -\n',
|
|
b'R 1 1 8 0 128 0 0 -\n',b'R 1 1 0 0 513 0 0 -\n',b'R 1 1 0 0 128 2 0 -\n',
|
|
b'R 1 1 0 0 128 0 0 0\n',b'R 1 1 0 0 128 0 0 gg\n',b'R 1 1 0 0 128 0 0 - extra\n',
|
|
b'R 1_0 1 0 0 128 0 0 -\n',b'R\v1 1 0 0 128 0 0 -\n',b'R\f1 1 0 0 128 0 0 -\n']
|
|
# Invalid lexical rows followed by a valid high-zero token are independent parser regressions.
|
|
lines.append(b' \tR +'+b'0'*600+b'64 -0 000 -000 000512 0 0 - \r\n')
|
|
return b''.join(lines)
|
|
|
|
|
|
def expected_rows(payload):
|
|
rows=[]
|
|
accepted=rejected=last=successful=failed=session_digest=0
|
|
allocated=released=published=attempts=sink_attempts=0
|
|
physical=payload.split(b'\n')[:-1]
|
|
for row,body in enumerate(physical,1):
|
|
parsed=1;status=4;required=emitted=0;output=b''
|
|
try:
|
|
sequence,identifier,level,value,cap,alloc,sink,label=parse_input(body+b'\n')
|
|
except ValueError:
|
|
parsed=0;rejected+=1
|
|
else:
|
|
if sequence<=last:
|
|
rejected+=1
|
|
else:
|
|
accepted+=1;last=sequence
|
|
text=canonical(identifier,level,value,label)
|
|
required=len(text);attempts+=1
|
|
if alloc or cap==0:
|
|
status=2
|
|
else:
|
|
allocated+=1;released+=1
|
|
if cap<=required:
|
|
status=1
|
|
else:
|
|
sink_attempts+=1
|
|
if sink:
|
|
status=3
|
|
else:
|
|
status=0;emitted=required;output=text;published+=1
|
|
if status==0:
|
|
successful+=1;session_digest=digest(session_digest,text)
|
|
else:
|
|
failed+=1
|
|
rows.append([row,parsed,status,required,emitted,0,allocated,released,published,0,
|
|
attempts,sink_attempts,0,accepted,rejected,last,successful,failed,session_digest,len(output),output])
|
|
return rows
|
|
|
|
|
|
def stdout_rows(data):
|
|
if not data.endswith(b'\n'):
|
|
raise ValueError('stdout missing LF')
|
|
rows=[]
|
|
for line in data.split(b'\n')[:-1]:
|
|
fields=[x for x in line.replace(b'\t',b' ').replace(b'\r',b' ').split(b' ') if x]
|
|
if len(fields)!=21:
|
|
raise ValueError('stdout field count')
|
|
numbers=[input_number(x,0,0xffffffff) for x in fields[:20]]
|
|
text=fields[20]
|
|
if text==b'-':
|
|
output=b''
|
|
else:
|
|
if len(text)%2 or any(x not in b'0123456789abcdef' for x in text):
|
|
raise ValueError('stdout hex syntax')
|
|
output=bytes.fromhex(text.decode('ascii'))
|
|
rows.append(numbers+[output])
|
|
return rows
|
|
|
|
|
|
def main():
|
|
evidence=[];failures=[]
|
|
business_ok=cleanup_ok=absence_ok=False
|
|
scratch=None
|
|
try:
|
|
candidate=Path(sys.argv[1])
|
|
contents={name:(candidate/name).read_bytes() for name in CANDIDATES}
|
|
payload=build_cases()
|
|
expected=expected_rows(payload)
|
|
scratch=Path(tempfile.mkdtemp(prefix='render-buffer-'))
|
|
build=scratch/'build'
|
|
build.mkdir(mode=0o777)
|
|
# Parent owns only this newly-created child, never chmod the UID-owned scratch root.
|
|
os.chmod(build,0o777)
|
|
for name,text in SUPPORT.items():
|
|
(build/name).write_text(text)
|
|
for name,data in contents.items():
|
|
(build/name).write_bytes(data)
|
|
# All candidate-controlled files are prepared before the first subprocess.
|
|
argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-Werror','-pedantic',
|
|
'codec.c','render.c','session.c','buffer_port.c','observer.c','-o','render-demo']
|
|
execute_stage('compiler',argv,build,COMPILE_DEADLINE,b'',evidence)
|
|
out,err=execute_stage('candidate',[str(build/'render-demo')],build,CANDIDATE_DEADLINE,payload,evidence)
|
|
observed=stdout_rows(out)
|
|
if observed!=expected:
|
|
first=next((i for i,(a,b) in enumerate(zip(observed,expected),1) if a!=b),min(len(observed),len(expected))+1)
|
|
raise ValueError('raw observation differs at row %d; observed rows=%d expected rows=%d' % (first,len(observed),len(expected)))
|
|
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,'exceptions':failures,'business_ok':business_ok,
|
|
'cleanup_ok':cleanup_ok,'absence_ok':absence_ok,'scratch':None if scratch is None else str(scratch),
|
|
'input_len':len(payload) if 'payload' in locals() else None,
|
|
'input_sha256':hashlib.sha256(payload).hexdigest() if 'payload' in locals() else None,
|
|
'original_wire_io_preserved':False}
|
|
marker=base64.b64encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode()
|
|
sys.stderr.write('RENDER_EVIDENCE_BZ2='+marker+'\n')
|
|
if failures or not (business_ok and cleanup_ok and absence_ok):
|
|
print('FAIL '+repr(failures))
|
|
return 1
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__=='__main__':
|
|
sys.exit(main())
|