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>
403 lines
29 KiB
Python
403 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,selectors
|
|
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
|
|
selector=selectors.DefaultSelector(); chunks={1:[],2:[]}; payload=b''; written=0
|
|
|
|
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]=[pid,None,os.waitstatus_to_exitcode(status)]
|
|
|
|
def pump(wait):
|
|
global written,drained
|
|
for key,events in selector.select(wait):
|
|
stream=key.fileobj; tag=key.data
|
|
if tag==0:
|
|
try: count=os.write(stream.fileno(),payload[written:written+65536])
|
|
except BrokenPipeError: count=0; written=len(payload)
|
|
written+=count
|
|
if written>=len(payload):
|
|
selector.unregister(stream); stream.close()
|
|
else:
|
|
data=os.read(stream.fileno(),65536)
|
|
if data: chunks[tag].append(data)
|
|
else: selector.unregister(stream); stream.close()
|
|
drained=not selector.get_map()
|
|
|
|
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')
|
|
payload=sys.stdin.buffer.read()
|
|
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
|
|
for stream,tag in ((leader.stdin,0),(leader.stdout,1),(leader.stderr,2)):
|
|
os.set_blocking(stream.fileno(),False)
|
|
if tag==0 and not payload: stream.close()
|
|
else: selector.register(stream,selectors.EVENT_WRITE if tag==0 else selectors.EVENT_READ,tag)
|
|
while True:
|
|
pump(0.04); own_wait()
|
|
discover()
|
|
if drained and reaped: break
|
|
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: pump(0.03); own_wait()
|
|
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)
|
|
|
|
output=b''.join(chunks[1]); diagnostic=b''.join(chunks[2])
|
|
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':list(waits.values()),
|
|
'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 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())
|
|
if RECORD_WIRE:
|
|
target=Path('/evidence')/(phase+'.wire')
|
|
target.write_bytes(raw.stdout)
|
|
(Path('/evidence')/(phase+'.wrapper-stderr')).write_bytes(raw.stderr)
|
|
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 = {'scanner.h': '#ifndef STEP_SCANNER_H\n#define STEP_SCANNER_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#define SP_MAX_ITEMS 64\n#define SP_MAX_BUDGET 256\n#define SP_DIGEST_MOD 1000003u\n\ntypedef enum {\n SP_UNCONFIGURED = 0,\n SP_READY = 1,\n SP_BUDGET = 2,\n SP_DONE = 3\n} sp_stop;\ntypedef enum {\n SP_FETCH = 0,\n SP_CLASSIFY = 1,\n SP_COMMIT = 2\n} sp_phase;\ntypedef struct {\n int count;\n int first;\n int last;\n int modulus;\n int residue;\n int values[SP_MAX_ITEMS];\n} sp_plan;\ntypedef struct {\n int kind;\n int index;\n int value;\n int selected;\n uint64_t visited;\n uint64_t kept;\n int64_t sum;\n uint32_t digest;\n} sp_event;\ntypedef struct {\n sp_event entries[SP_MAX_BUDGET];\n size_t count;\n} sp_journal;\ntypedef struct {\n sp_plan plan;\n int configured;\n int cursor;\n int direction;\n int value;\n int selected;\n sp_phase phase;\n sp_stop stop;\n uint64_t steps;\n uint64_t visited;\n uint64_t kept;\n int64_t sum;\n uint32_t digest;\n} sp_scanner;\ntypedef enum {\n SP_CMD_INVALID,\n SP_CMD_PLAN,\n SP_CMD_TICK,\n SP_CMD_SNAPSHOT,\n SP_CMD_RESTART\n} sp_command_kind;\ntypedef struct {\n sp_command_kind kind;\n sp_plan plan;\n int budget;\n} sp_command;\nvoid sp_init(sp_scanner *scanner);\nint sp_plan_valid(const sp_plan *plan);\nint sp_install(sp_scanner *scanner, const sp_plan *plan);\nint sp_restart(sp_scanner *scanner);\nint sp_tick(sp_scanner *scanner, int budget, sp_journal *journal);\nvoid sp_journal_clear(sp_journal *journal);\nvoid sp_journal_add(sp_journal *journal, const sp_scanner *scanner, int kind);\nuint32_t sp_digest_next(uint32_t previous, int index, int value);\nint sp_domain_length(const sp_plan *plan);\nint sp_selected(int value, int modulus, int residue);\nint sp_decode(const char *text, sp_command *command);\nint sp_read_line(FILE *input, char **buffer, size_t *capacity);\nvoid sp_print(FILE *output, int result, const sp_scanner *scanner, const sp_journal *journal);\nint sp_dispatch(sp_scanner *scanner, const sp_command *command, sp_journal *journal);\n#endif\n', 'command.c': '#include "scanner.h"\n#include <limits.h>\n#include <stdlib.h>\n#include <string.h>\n\ntypedef struct {\n const unsigned char *next;\n} sp_reader;\n\nstatic int horizontal(unsigned char character)\n{\n return character == \' \' || character == \'\\t\' || character == \'\\r\';\n}\n\nstatic void skip_space(sp_reader *reader)\n{\n while (horizontal(*reader->next)) {\n reader->next += 1;\n }\n}\n\nstatic int at_end(sp_reader *reader)\n{\n skip_space(reader);\n return *reader->next == 0;\n}\n\nstatic int read_integer(sp_reader *reader, int lower, int upper, int *value)\n{\n const unsigned char *cursor;\n unsigned int magnitude = 0;\n int negative = 0;\n int overflow = 0;\n int digit_seen = 0;\n skip_space(reader);\n cursor = reader->next;\n if (*cursor == \'+\' || *cursor == \'-\') {\n negative = *cursor == \'-\';\n cursor += 1;\n }\n while (*cursor >= \'0\' && *cursor <= \'9\') {\n unsigned int digit = (unsigned int)(*cursor - \'0\');\n digit_seen = 1;\n if (magnitude > 100000u) {\n overflow = 1;\n } else {\n magnitude = magnitude * 10u + digit;\n }\n cursor += 1;\n }\n if (!digit_seen || (*cursor && !horizontal(*cursor))) {\n return -1;\n }\n if (overflow || magnitude > INT_MAX) {\n return -1;\n }\n *value = negative ? -(int)magnitude : (int)magnitude;\n if (*value < lower || *value > upper) {\n return -1;\n }\n reader->next = cursor;\n return 0;\n}\n\nstatic int read_plan(sp_reader *reader, sp_command *command)\n{\n int index;\n sp_plan *plan = &command->plan;\n if (read_integer(reader, 0, SP_MAX_ITEMS, &plan->count)) {\n return -1;\n }\n if (read_integer(reader, 0, SP_MAX_ITEMS - 1, &plan->first)) {\n return -1;\n }\n if (read_integer(reader, 0, SP_MAX_ITEMS - 1, &plan->last)) {\n return -1;\n }\n if (read_integer(reader, 1, 17, &plan->modulus)) {\n return -1;\n }\n if (read_integer(reader, 0, 16, &plan->residue)) {\n return -1;\n }\n for (index = 0; index < plan->count; ++index) {\n if (read_integer(reader, -1000, 1000, &plan->values[index])) {\n return -1;\n }\n }\n if (!at_end(reader) || !sp_plan_valid(plan)) {\n return -1;\n }\n command->kind = SP_CMD_PLAN;\n return 0;\n}\n\nint sp_decode(const char *text, sp_command *command)\n{\n sp_reader reader;\n unsigned char operation;\n memset(command, 0, sizeof(*command));\n reader.next = (const unsigned char *)text;\n skip_space(&reader);\n operation = *reader.next;\n if (!operation) {\n return -1;\n }\n reader.next += 1;\n if (*reader.next && !horizontal(*reader.next)) {\n return -1;\n }\n if (operation == \'P\') {\n return read_plan(&reader, command);\n }\n if (operation == \'T\') {\n if (read_integer(&reader, 0, SP_MAX_BUDGET, &command->budget)) {\n return -1;\n }\n command->kind = SP_CMD_TICK;\n } else if (operation == \'S\') {\n command->kind = SP_CMD_SNAPSHOT;\n } else if (operation == \'R\') {\n command->kind = SP_CMD_RESTART;\n } else {\n return -1;\n }\n if (!at_end(&reader)) {\n command->kind = SP_CMD_INVALID;\n return -1;\n }\n return 0;\n}\n\nint sp_read_line(FILE *input, char **buffer, size_t *capacity)\n{\n size_t used = 0;\n int character;\n if (!*buffer) {\n *capacity = 128;\n *buffer = malloc(*capacity);\n if (!*buffer) {\n return -1;\n }\n }\n while ((character = fgetc(input)) != EOF && character != \'\\n\') {\n if (used + 1 >= *capacity) {\n size_t larger;\n char *replacement;\n if (*capacity > SIZE_MAX / 2) {\n return -1;\n }\n larger = *capacity * 2;\n replacement = realloc(*buffer, larger);\n if (!replacement) {\n return -1;\n }\n *buffer = replacement;\n *capacity = larger;\n }\n (*buffer)[used++] = (char)character;\n }\n if (character == EOF && used == 0) {\n return 0;\n }\n (*buffer)[used] = 0;\n return 1;\n}\n', 'journal.c': '#include "scanner.h"\n\nvoid sp_journal_clear(sp_journal *journal)\n{\n journal->count = 0;\n}\n\nvoid sp_journal_add(sp_journal *journal, const sp_scanner *scanner, int kind)\n{\n sp_event *event;\n if (journal->count == SP_MAX_BUDGET) {\n return;\n }\n event = &journal->entries[journal->count];\n event->kind = kind;\n event->index = scanner->cursor;\n event->value = scanner->value;\n event->selected = scanner->selected;\n event->visited = scanner->visited;\n event->kept = scanner->kept;\n event->sum = scanner->sum;\n event->digest = scanner->digest;\n journal->count += 1;\n}\n\nuint32_t sp_digest_next(uint32_t previous, int index, int value)\n{\n uint32_t position = (uint32_t)(index + 1);\n uint32_t normalized = (uint32_t)(value + 1000);\n uint32_t token = position * 17u + normalized;\n uint64_t extended = (uint64_t)previous * 131u + token;\n return (uint32_t)(extended % SP_DIGEST_MOD);\n}\n\nint sp_selected(int value, int modulus, int residue)\n{\n int remainder = value % modulus;\n if (remainder < 0) {\n remainder += modulus;\n }\n return remainder == residue;\n}\n\nint sp_domain_length(const sp_plan *plan)\n{\n int distance;\n if (plan->count == 0) {\n return 0;\n }\n distance = plan->last - plan->first;\n if (distance < 0) {\n distance = -distance;\n }\n return distance + 1;\n}\n', 'format.c': '#include "scanner.h"\n#include <inttypes.h>\n\nstatic void print_snapshot(FILE *output, int result, const sp_scanner *scanner)\n{\n fprintf(output, "%d %d %d %d", result, (int)scanner->stop,\n (int)scanner->phase, scanner->cursor);\n fprintf(output, " %" PRIu64, scanner->steps);\n fprintf(output, " %" PRIu64, scanner->visited);\n fprintf(output, " %" PRIu64, scanner->kept);\n fprintf(output, " %" PRId64, scanner->sum);\n fprintf(output, " %" PRIu32, scanner->digest);\n}\n\nstatic void print_event(FILE *output, const sp_event *event)\n{\n fprintf(output, " %d", event->kind);\n fprintf(output, " %d", event->index);\n fprintf(output, " %d", event->value);\n fprintf(output, " %d", event->selected);\n fprintf(output, " %" PRIu64, event->visited);\n fprintf(output, " %" PRIu64, event->kept);\n fprintf(output, " %" PRId64, event->sum);\n fprintf(output, " %" PRIu32, event->digest);\n}\n\nvoid sp_print(FILE *output, int result, const sp_scanner *scanner, const sp_journal *journal)\n{\n size_t index;\n print_snapshot(output, result, scanner);\n fprintf(output, " %zu", journal->count);\n for (index = 0; index < journal->count; ++index) {\n print_event(output, &journal->entries[index]);\n }\n fputc(\'\\n\', output);\n}\n\nint sp_dispatch(sp_scanner *scanner, const sp_command *command, sp_journal *journal)\n{\n sp_journal_clear(journal);\n switch (command->kind) {\n case SP_CMD_PLAN:\n return sp_install(scanner, &command->plan);\n case SP_CMD_TICK:\n return sp_tick(scanner, command->budget, journal);\n case SP_CMD_SNAPSHOT:\n return 0;\n case SP_CMD_RESTART:\n return sp_restart(scanner);\n default:\n return -1;\n }\n}\n', 'observer.c': '#include "scanner.h"\n#include <stdlib.h>\n\nint main(void)\n{\n sp_scanner scanner;\n sp_command command;\n sp_journal journal;\n char *buffer = NULL;\n size_t capacity = 0;\n int received;\n int result;\n sp_init(&scanner);\n while ((received = sp_read_line(stdin, &buffer, &capacity)) > 0) {\n sp_journal_clear(&journal);\n result = sp_decode(buffer, &command);\n if (result == 0) {\n result = sp_dispatch(&scanner, &command, &journal);\n }\n sp_print(stdout, result, &scanner, &journal);\n }\n free(buffer);\n if (received < 0) {\n return 2;\n }\n if (ferror(stdin) || ferror(stdout)) {\n return 3;\n }\n return 0;\n}\n'}
|
|
|
|
|
|
def integer(token):
|
|
if not token: raise ValueError('empty integer')
|
|
negative=token[:1]==b'-'
|
|
digits=token[1:] if token[:1] in (b'+',b'-') else token
|
|
if not digits or any(c<48 or c>57 for c in digits): raise ValueError('nondecimal integer')
|
|
significant=digits.lstrip(b'0') or b'0'
|
|
if len(significant)>19: raise ValueError('integer outside observable range')
|
|
value=int(significant)
|
|
return -value if negative else value
|
|
|
|
def tokens(line):
|
|
import re
|
|
return [word for word in re.split(rb'[ \t\r]+',line.strip(b' \t\r')) if word]
|
|
|
|
def decode(line):
|
|
words=tokens(line)
|
|
if not words: return None
|
|
op=words[0]
|
|
try: numbers=[integer(word) for word in words[1:]]
|
|
except ValueError: return None
|
|
if op in (b'S',b'R') and not numbers: return (op,)
|
|
if op==b'T' and len(numbers)==1 and 0<=numbers[0]<=256: return (op,numbers[0])
|
|
if op!=b'P' or len(numbers)<5: return None
|
|
n,first,last,mod,res=*numbers[:5],
|
|
values=numbers[5:]
|
|
if not 0<=n<=64 or len(values)!=n or not 1<=mod<=17 or not 0<=res<mod: return None
|
|
if any(not -1000<=v<=1000 for v in values): return None
|
|
if n==0:
|
|
if first!=0 or last!=0: return None
|
|
elif not (0<=first<n and 0<=last<n): return None
|
|
return (op,n,first,last,mod,res,values)
|
|
|
|
def oracle(payload):
|
|
installed=None; progress=0; stop=0
|
|
result=[]
|
|
def arrays(plan):
|
|
_,n,first,last,mod,res,values=plan
|
|
indices=[] if not n else list(range(first,last+(1 if first<=last else -1),1 if first<=last else -1))
|
|
kept=[0]; sums=[0]; digests=[0]
|
|
for index in indices:
|
|
value=values[index]; selected=int(value%mod==res)
|
|
kept.append(kept[-1]+selected)
|
|
sums.append(sums[-1]+(value if selected else 0))
|
|
digests.append((digests[-1]*131+(index+1)*17+value+1000)%1000003 if selected else digests[-1])
|
|
return indices,kept,sums,digests
|
|
lines=payload.split(b'\n')
|
|
if lines[-1:]==[b'']: lines=lines[:-1]
|
|
for line in lines:
|
|
command=decode(line); rc=0; events=[]
|
|
if command is None: rc=-1
|
|
elif command[0]==b'P':
|
|
installed=command; progress=0; stop=1 if command[1] else 3
|
|
elif command[0]==b'R':
|
|
if installed is None: rc=-1
|
|
else: progress=0; stop=1 if installed[1] else 3
|
|
elif command[0]==b'T':
|
|
if installed is None: rc=-1
|
|
elif stop!=3:
|
|
indices,kept,sums,digests=arrays(installed)
|
|
target=min(progress+command[1],len(indices)*3)
|
|
for tick in range(progress+1,target+1):
|
|
rank=(tick-1)//3; stage=(tick-1)%3+1; index=indices[rank]
|
|
value=installed[6][index]; selected=0 if stage==1 else int(value%installed[4]==installed[5])
|
|
completed=tick//3
|
|
events.extend([stage,index,value,selected,completed,kept[completed],sums[completed],digests[completed]])
|
|
progress=target; stop=3 if target==len(indices)*3 else 2
|
|
if installed is None: header=[rc,0,0,-1,0,0,0,0,0,len(events)//8]
|
|
else:
|
|
indices,kept,sums,digests=arrays(installed); completed=progress//3
|
|
cursor=-1 if not indices else indices[min(completed,len(indices)-1)]
|
|
header=[rc,stop,progress%3,cursor,progress,completed,kept[completed],sums[completed],digests[completed],len(events)//8]
|
|
result.append(header+events)
|
|
return result
|
|
|
|
def requests():
|
|
rng=random.Random(712931)
|
|
lines=['S','R','T 0','P 5 0 4 2 0 4 7 6 9 2','T 4','S','T 20','S',
|
|
'P 5 4 1 1 0 4 7 6 9 2','T 4','T 20','R','T 12',
|
|
'P 0 0 0 1 0','T 0','T 256','R','P 1 0 0 17 3 -1000','T 1','T 0','T 1','T 1','T 256',
|
|
'P 3 0 2 2 0 1 3 5','T 9','R','T 10','P 2 1 0 3 2 -1 1000','T 6',
|
|
'P 1 0 0 1 0 1','T 2','P 1 0 0 1 1 9','T 1','', 'T 1_0','T\v1','T 257','T -1','R extra','P 1 0 0 1 0','P 1 0 0 1 0 2 3',
|
|
'T +'+('0'*5000),'P +0001 -000 +0000 01 -000 -'+('0'*5000),'T 3']
|
|
for i in range(100):
|
|
n=rng.choice([0,1,2,3,7,16,64]); first=rng.randrange(n) if n else 0; last=rng.randrange(n) if n else 0
|
|
mod=rng.randrange(1,18); res=rng.randrange(mod); values=[rng.randrange(-1000,1001) for _ in range(n)]
|
|
lines.append('P '+' '.join(map(str,[n,first,last,mod,res,*values])))
|
|
for budget in [0,1,2,rng.randrange(1,20),256,1]: lines.append('T '+str(budget))
|
|
if i%3==0: lines.extend(['R','T 256','S'])
|
|
return ('\n'.join(lines)+'\n').encode('ascii')
|
|
|
|
def compare(output,expected):
|
|
lines=output.split(b'\n')
|
|
if lines[-1:]==[b'']: lines.pop()
|
|
if len(lines)!=len(expected): raise ValueError('observation line count '+str((len(lines),len(expected))))
|
|
for index,(line,wanted) in enumerate(zip(lines,expected)):
|
|
actual=[integer(word) for word in tokens(line)]
|
|
if actual!=wanted:
|
|
offset=next((i for i in range(min(len(actual),len(wanted))) if actual[i]!=wanted[i]),min(len(actual),len(wanted)))
|
|
raise ValueError('first raw difference row=%d field=%d actual=%r expected=%r'%(index,offset,actual[offset:offset+1],wanted[offset:offset+1]))
|
|
|
|
def main():
|
|
evidence=[]; errors=[]; scratch=None; success=False
|
|
try:
|
|
scratch=Path(tempfile.mkdtemp(prefix='step-progress-'))
|
|
# Prepare every source and all stdin bytes before the first child.
|
|
work=scratch/'work'; work.mkdir(mode=0o777); work.chmod(0o777)
|
|
for name,text in SUPPORT.items(): (work/name).write_text(text)
|
|
(work/'scanner.c').write_bytes((Path(sys.argv[1])/'scanner.c').read_bytes())
|
|
payload=requests(); expected=oracle(payload)
|
|
compiler=['gcc','-std=c11','-O2','-Wall','-Wextra','-Werror','-pedantic','scanner.c','command.c','journal.c','format.c','observer.c','-o','scan']
|
|
execute_stage('compiler',compiler,work,20,b'',evidence)
|
|
out,err=execute_stage('candidate',[str(work/'scan')],work,8,payload,evidence)
|
|
compare(out,expected)
|
|
success=True
|
|
except BaseException as error:
|
|
errors.append(['main',type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
try: execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/',5,b'',evidence)
|
|
except BaseException as error: errors.append(['cleanup',type(error).__name__,str(error)])
|
|
try: execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/',5,b'',evidence)
|
|
except BaseException as error: errors.append(['absence',type(error).__name__,str(error)])
|
|
record={'success':success,'exceptions':errors,'stages':evidence}
|
|
packed=base64.b85encode(bz2.compress(json.dumps(record,separators=(',',':')).encode('utf-8'))).decode('ascii')
|
|
sys.stderr.write('STEP_EVIDENCE_B85='+packed+'\n')
|
|
if success and not errors:
|
|
print('PASS'); return 0
|
|
print('FAIL '+repr(errors)); return 1
|
|
|
|
if __name__=='__main__': sys.exit(main())
|