amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
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>
2026-10-27 01:55:56 +08:00

436 lines
30 KiB
Python

import base64
import bz2
import hashlib
import json
import math
import os
from pathlib import Path
import random
import subprocess
import sys
import tempfile
import time
RECOVERY_SECONDS = 3
RECORD_WIRE = False
SUPERVISOR = r'''
import base64,bz2,ctypes,hashlib,json,os,signal,subprocess,sys,time
cfg=json.loads(sys.argv[1]); start=time.monotonic(); errors=[]; tracked={}; signals={}; waits=[]
leader=None; leader_start=None; output=b''; diagnostic=b''; timed_out=False; drained=False
reaped=False; leader_rc=None; remaining=[]; pg_absent=False
def fault(op, error):
errors.append([op,type(error).__name__,str(error)])
def exact(pid):
try:
with open('/proc/%d/stat'%pid,'rb') as stream: data=stream.read()
words=data[data.rfind(b')')+2:].split()
return {'pid':pid,'state':words[0].decode('ascii'),'ppid':int(words[1]),'pg':int(words[2]),'start':int(words[19])}
except (FileNotFoundError,ProcessLookupError): return None
def table_now():
result={}
for name in os.listdir('/proc'):
if name.isascii() and name.isdigit():
value=exact(int(name))
if value is not None: result[value['pid']]=value
return result
def closure(table, supervisor, remembered):
roots={supervisor}
for pid,old in remembered.items():
current=table.get(pid)
if current and current['start']==old['start'] and current['state'] not in ('Z','X'):
roots.add(pid)
found={}
changed=True
while changed:
changed=False
for pid,current in table.items():
if pid!=supervisor and pid not in found and (current['ppid'] in roots or pid in roots):
found[pid]=current; roots.add(pid); changed=True
return found
def discover():
table=table_now(); found=closure(table,os.getpid(),tracked)
for pid,current in found.items():
if pid not in tracked or tracked[pid]['start']!=current['start']:
tracked[pid]=dict(current)
return table,found
def send_fresh(pid, token):
current=exact(pid)
if current is None or current['start']!=token['start'] or current['state'] in ('Z','X'): return
os.kill(pid,signal.SIGKILL)
signals[(pid,token['start'])]=signals.get((pid,token['start']),0)+1
def own_wait():
global reaped,leader_rc
if leader is not None:
value=leader.poll()
if value is not None:
leader_rc=leader.wait(timeout=0)
reaped=True
def adopted_wait():
if leader is not None and not reaped: return
while True:
try: pid,status=os.waitpid(-1,os.WNOHANG)
except ChildProcessError: break
if pid==0: break
waits.append([pid,None,status,os.waitstatus_to_exitcode(status)])
supervisor_start=exact(os.getpid())['start']
try:
libc=ctypes.CDLL(None,use_errno=True)
if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')
leader=subprocess.Popen(cfg['argv'],cwd=cfg['cwd'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
info=exact(leader.pid)
if info is None: raise RuntimeError('leader identity disappeared before capture')
leader_start=info['start']; tracked[leader.pid]=info
payload=sys.stdin.buffer.read()
first=True
while True:
try:
output,diagnostic=leader.communicate(input=payload if first else None,timeout=0.04)
drained=True; own_wait(); break
except subprocess.TimeoutExpired:
first=False
own_wait()
discover()
if time.monotonic()-start>=cfg['deadline']:
timed_out=True; break
except BaseException as error:
fault('execute',error)
finally:
recovery_end=time.monotonic()+cfg['recovery']
while True:
try: own_wait()
except BaseException as error: fault('own_wait',error)
found={}
try: _,found=discover()
except BaseException as error: fault('discover',error)
for pid,token in list(found.items()):
try: send_fresh(pid,token)
except (FileNotFoundError,ProcessLookupError): pass
except BaseException as error: fault('signal',error)
try: own_wait()
except BaseException as error: fault('own_wait',error)
try: adopted_wait()
except BaseException as error: fault('adopted_wait',error)
if leader is not None and not drained:
try:
output,diagnostic=leader.communicate(timeout=0.03)
drained=True; own_wait()
except subprocess.TimeoutExpired: pass
except BaseException as error: fault('drain',error)
try:
table,found=discover()
remaining=[[p,v['start'],v['state']] for p,v in found.items()]
pg_absent=leader is not None and not any(v['pg']==leader.pid for v in table.values())
except BaseException as error:
fault('absence',error); remaining=[['unknown']]; pg_absent=False
if reaped and drained and not remaining and pg_absent: break
if time.monotonic()>=recovery_end:
fault('recovery',RuntimeError('deadline with unconfirmed closure')); break
time.sleep(0.01)
meta={'phase':cfg['phase'],'nonce':cfg['nonce'],'argv':cfg['argv'],
'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
'supervisor_pid':os.getpid(),'supervisor_start':supervisor_start,
'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,
'leader_rc':leader_rc,'leader_reaped':reaped,'timeout':timed_out,
'controlled_rc':124 if timed_out else leader_rc,'elapsed':time.monotonic()-start,
'deadline':cfg['deadline'],'recovery':cfg['recovery'],
'pg_absent':pg_absent,'remaining':remaining,'exceptions':errors,
'adopted_waits':waits,
'descendants':[[p,t['start'],signals.get((p,t['start']),0)]
for p,t in tracked.items() if leader is None or p!=leader.pid],
'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),
'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}
wire={'meta':meta,'stdout':base64.b64encode(bz2.compress(output)).decode('ascii'),
'stderr':base64.b64encode(bz2.compress(diagnostic)).decode('ascii')}
sys.stdout.write(json.dumps(wire,separators=(',',':'))+'\n')
'''
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)"
def identity_int(value, minimum=0):
return type(value) is int and value >= minimum
def validate_meta(meta, config, out, err):
required = {'phase','nonce','argv','uid','gid','groups','supervisor_pid','supervisor_start',
'leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc',
'elapsed','deadline','recovery','pg_absent','remaining','exceptions','descendants',
'stdout_len','stdout_sha256','stderr_len','stderr_sha256','adopted_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) != 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_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 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 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:
entry['wire_bz2'] = base64.b64encode(bz2.compress(raw.stdout)).decode('ascii')
entry['wrapper_stderr_bz2'] = base64.b64encode(bz2.compress(raw.stderr)).decode('ascii')
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']
if not RECORD_WIRE: entry['meta'] = meta
validate_meta(meta,config,out,err)
return out,err
SUPPORT = {'ticket.h': '#ifndef TICKET_H\n#define TICKET_H\n#include <stddef.h>\n#include <stdint.h>\n#define TICKET_SLOTS 8\n#define TICKET_MAX_ID UINT64_C(1000000000)\n#define TICKET_MAX_OWNER UINT32_C(1000000)\n#define TICKET_MAX_COUNT UINT32_C(64)\ntypedef struct {\n uint64_t epoch;\n uint64_t owner;\n uint64_t first;\n uint64_t last;\n uint64_t count;\n uint64_t sum;\n} TicketGrant;\ntypedef struct {\n uint64_t open;\n uint64_t epoch;\n uint64_t first;\n uint64_t limit;\n uint64_t next;\n uint64_t allocated;\n uint64_t committed;\n uint64_t canceled;\n uint64_t pending_owners;\n uint64_t pending_units;\n uint64_t committed_sum;\n} TicketStats;\ntypedef struct {\n int used;\n TicketGrant grant;\n} TicketEntry;\ntypedef struct {\n uint64_t open;\n uint64_t epoch;\n uint64_t first;\n uint64_t limit;\n uint64_t next;\n uint64_t allocated;\n uint64_t committed;\n uint64_t canceled;\n uint64_t committed_sum;\n TicketEntry entries[TICKET_SLOTS];\n} TicketBook;\nint ticket_reset(TicketBook *book, uint64_t first, uint64_t limit);\nint ticket_reserve(TicketBook *book, uint32_t owner, uint32_t count, TicketGrant *out);\nint ticket_commit(TicketBook *book, uint32_t owner, TicketGrant *out);\nint ticket_cancel(TicketBook *book, uint32_t owner, TicketGrant *out);\nint ticket_peek(TicketBook *book, uint32_t owner, TicketGrant *out);\nint ticket_stats(TicketBook *book, TicketStats *out);\nint ticket_list(TicketBook *book, TicketGrant *out, size_t capacity, size_t *written);\n#endif\n', 'input.h': '#ifndef INPUT_H\n#define INPUT_H\n#include <stdint.h>\n#include <stdio.h>\ntypedef struct {\n int opcode;\n unsigned book;\n uint64_t a;\n uint64_t b;\n} Command;\nint command_read(FILE *stream, Command *out, unsigned *line_number);\nconst char *command_name(int opcode);\n#endif\n', 'input.c': '#include "input.h"\n#include <string.h>\n#include <stddef.h>\n\nstatic int horizontal(char value)\n{\n return value == \' \' || value == \'\\t\';\n}\n\nstatic size_t words(char *line, char **tokens, size_t capacity)\n{\n size_t count = 0;\n char *cursor = line;\n while (*cursor != \'\\0\') {\n while (horizontal(*cursor)) {\n ++cursor;\n }\n if (*cursor == \'\\0\') {\n break;\n }\n if (count == capacity) {\n return capacity + 1;\n }\n tokens[count++] = cursor;\n while (*cursor != \'\\0\' && !horizontal(*cursor)) {\n ++cursor;\n }\n if (*cursor != \'\\0\') {\n *cursor++ = \'\\0\';\n }\n }\n return count;\n}\n\nstatic int decimal(const char *text, uint64_t *value)\n{\n uint64_t result = 0;\n if (*text == \'+\') {\n ++text;\n }\n if (*text == \'\\0\') {\n return 0;\n }\n while (*text != \'\\0\') {\n if (*text < \'0\' || *text > \'9\') {\n return 0;\n }\n unsigned digit = (unsigned)(*text++ - \'0\');\n if (result > (UINT64_MAX - digit) / 10) {\n return 0;\n }\n result = result * 10 + digit;\n }\n *value = result;\n return 1;\n}\n\nconst char *command_name(int opcode)\n{\n const char *names[] = {"INVALID", "RESET", "RESERVE", "COMMIT", "CANCEL", "PEEK", "STATS", "LIST", "NULL"};\n if (opcode < 1 || opcode > 8) {\n return names[0];\n }\n return names[opcode];\n}\n\nstatic int parse(char *line, Command *out)\n{\n char *tokens[5];\n size_t count = words(line, tokens, 5);\n if (count < 2 || count > 4) {\n return -1;\n }\n int opcode = 0;\n for (int i = 1; i <= 8; ++i) {\n if (strcmp(tokens[0], command_name(i)) == 0) {\n opcode = i;\n break;\n }\n }\n size_t expected = 3;\n if (opcode == 1 || opcode == 2) {\n expected = 4;\n }\n if (opcode == 6) {\n expected = 2;\n }\n if (opcode == 0 || count != expected) {\n return -1;\n }\n uint64_t numbers[3] = {0};\n for (size_t i = 1; i < count; ++i) {\n if (!decimal(tokens[i], &numbers[i - 1])) {\n return -1;\n }\n }\n if (numbers[0] > 3 || (opcode == 7 && numbers[1] > 8) || (opcode == 8 && numbers[1] > 9)) {\n return -1;\n }\n out->opcode = opcode;\n out->book = (unsigned)numbers[0];\n out->a = numbers[1];\n out->b = numbers[2];\n return 1;\n}\n\nint command_read(FILE *stream, Command *out, unsigned *line_number)\n{\n char line[4097];\n while (fgets(line, sizeof(line), stream) != NULL) {\n ++*line_number;\n size_t length = strlen(line);\n if (length > 4095 || (length > 0 && line[length - 1] != \'\\n\' && !feof(stream))) {\n return -1;\n }\n while (length > 0 && (line[length - 1] == \'\\n\' || line[length - 1] == \'\\r\')) {\n line[--length] = \'\\0\';\n }\n char *cursor = line;\n while (horizontal(*cursor)) {\n ++cursor;\n }\n if (*cursor == \'\\0\' || *cursor == \'#\') {\n continue;\n }\n return parse(cursor, out);\n }\n return ferror(stream) ? -1 : 0;\n}\n', 'observer.c': '#include "ticket.h"\n#include "input.h"\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdint.h>\n\nstatic TicketGrant sentinel(void)\n{\n TicketGrant grant;\n grant.epoch = UINT64_C(9000000001);\n grant.owner = UINT64_C(9000000002);\n grant.first = UINT64_C(9000000003);\n grant.last = UINT64_C(9000000004);\n grant.count = UINT64_C(9000000005);\n grant.sum = UINT64_C(9000000006);\n return grant;\n}\n\nstatic void print_grant(const TicketGrant *grant)\n{\n printf(" %" PRIu64, grant->epoch);\n printf(" %" PRIu64, grant->owner);\n printf(" %" PRIu64, grant->first);\n printf(" %" PRIu64, grant->last);\n printf(" %" PRIu64, grant->count);\n printf(" %" PRIu64, grant->sum);\n}\n\nstatic void print_stats(const TicketStats *stats)\n{\n printf(" %" PRIu64, stats->open);\n printf(" %" PRIu64, stats->epoch);\n printf(" %" PRIu64, stats->first);\n printf(" %" PRIu64, stats->limit);\n printf(" %" PRIu64, stats->next);\n printf(" %" PRIu64, stats->allocated);\n printf(" %" PRIu64, stats->committed);\n printf(" %" PRIu64, stats->canceled);\n printf(" %" PRIu64, stats->pending_owners);\n printf(" %" PRIu64, stats->pending_units);\n printf(" %" PRIu64, stats->committed_sum);\n}\n\nstatic int null_call(TicketBook *book, uint64_t kind, TicketGrant *out, uint64_t *aux)\n{\n size_t written = UINT64_C(9000000007);\n TicketGrant list[TICKET_SLOTS];\n switch (kind) {\n case 0: return ticket_reserve(NULL, 1, 1, out);\n case 1: return ticket_reserve(book, 1, 1, NULL);\n case 2: return ticket_reset(NULL, 1, 10);\n case 3: return ticket_commit(book, 1, NULL);\n case 4: return ticket_cancel(NULL, 1, out);\n case 5: return ticket_peek(book, 1, NULL);\n case 6: return ticket_stats(book, NULL);\n case 7: return ticket_list(book, list, TICKET_SLOTS, NULL);\n case 8: return ticket_list(NULL, list, TICKET_SLOTS, &written);\n case 9: {\n int rc = ticket_list(book, NULL, TICKET_SLOTS, &written);\n *aux = written;\n return rc;\n }\n default: return -1;\n }\n}\n\nstatic int apply(TicketBook *book, const Command *command, TicketGrant *out, uint64_t *aux)\n{\n if (command->opcode == 1) {\n return ticket_reset(book, command->a, command->b);\n }\n if (command->opcode == 6) {\n TicketStats stats;\n return ticket_stats(book, &stats);\n }\n if (command->opcode == 7) {\n TicketGrant list[TICKET_SLOTS];\n for (size_t i = 0; i < TICKET_SLOTS; ++i) { list[i] = sentinel(); }\n size_t written = UINT64_C(9000000007);\n int rc = ticket_list(book, list, (size_t)command->a, &written);\n *aux = written;\n if (rc != 0 || (written > 0 && written <= TICKET_SLOTS)) {\n *out = list[0];\n }\n return rc;\n }\n if (command->opcode == 8) {\n return null_call(book, command->a, out, aux);\n }\n if (command->a > UINT32_MAX || command->b > UINT32_MAX) {\n return -1;\n }\n uint32_t owner = (uint32_t)command->a;\n uint32_t count = (uint32_t)command->b;\n switch (command->opcode) {\n case 2: return ticket_reserve(book, owner, count, out);\n case 3: return ticket_commit(book, owner, out);\n case 4: return ticket_cancel(book, owner, out);\n case 5: return ticket_peek(book, owner, out);\n default: return -1;\n }\n}\n\nstatic void observe(unsigned sequence, const Command *command, int rc, const TicketGrant *grant, uint64_t aux, TicketBook *book)\n{\n TicketStats stats = {0};\n TicketGrant list[TICKET_SLOTS];\n size_t written = UINT64_C(9000000007);\n int stats_rc = ticket_stats(book, &stats);\n int list_rc = ticket_list(book, list, TICKET_SLOTS, &written);\n printf("%u %d %u %d", sequence, command->opcode, command->book, rc);\n print_grant(grant);\n printf(" %" PRIu64, aux);\n print_stats(&stats);\n printf(" %d %zu", list_rc, written);\n for (size_t i = 0; list_rc == 0 && i < written && i < TICKET_SLOTS; ++i) {\n print_grant(&list[i]);\n }\n printf("\\n");\n (void)stats_rc;\n}\n\nint main(void)\n{\n TicketBook books[4] = {0};\n unsigned line_number = 0;\n unsigned sequence = 0;\n Command command;\n int status;\n while ((status = command_read(stdin, &command, &line_number)) > 0) {\n TicketGrant grant = sentinel();\n uint64_t aux = UINT64_C(9000000007);\n int rc = apply(&books[command.book], &command, &grant, &aux);\n observe(++sequence, &command, rc, &grant, aux, &books[command.book]);\n }\n if (status < 0) {\n fprintf(stderr, "invalid command at line %u\\n", line_number);\n return 64;\n }\n return ferror(stdout) ? 74 : 0;\n}\n'}
COMPILER_DEADLINE = 15
CANDIDATE_DEADLINE = 8
CLEANUP_DEADLINE = 5
ABSENCE_DEADLINE = 5
SENTINEL = list(range(9000000001,9000000007))
AUX = 9000000007
def commands():
rows=[]
def add(*items): rows.append(tuple(items))
for b in range(4):
add('STATS',b); add('LIST',b,0); add('RESERVE',b,1,1)
for kind in range(10): add('NULL',b,kind)
add('RESET',b,100+20*b,999+20*b)
for count in (1,2,3,64):
add('RESERVE',0,100+count,count)
add('PEEK',0,100+count); add('COMMIT',0,100+count)
for owner in (8,3,11,2,19,1,9,7): add('RESERVE',1,owner,2)
add('RESERVE',1,8,1); add('RESERVE',1,99,1)
for capacity in range(9): add('LIST',1,capacity)
for owner in (2,11,8): add('CANCEL',1,owner)
add('RESERVE',1,22,3); add('COMMIT',1,22)
add('RESET',1,999999998,1000000000)
add('PEEK',1,3); add('RESERVE',1,3,2); add('RESERVE',1,4,2)
add('RESERVE',1,4,1); add('COMMIT',1,3); add('CANCEL',1,4)
add('RESERVE',1,5,1); add('STATS',1)
for first,limit in ((0,10),(2,1),(1,1000000001),(1,1),(1000000000,1000000000)):
add('RESET',2,first,limit); add('RESERVE',2,42,1)
add('RESERVE',2,43,1); add('CANCEL',2,42)
for owner,count in ((0,1),(1000001,1),(1,0),(1,65),(4294967296,1),(1,4294967296)):
add('RESERVE',3,owner,count)
for op in ('PEEK','COMMIT','CANCEL'):
for owner in (0,1000001,123,4294967296): add(op,3,owner)
rng=random.Random(150103)
for n in range(280):
b=rng.randrange(4); op=rng.choice(['RESERVE']*5+['COMMIT','CANCEL','PEEK','LIST','STATS','RESET','NULL'])
if op=='RESET':
first=rng.choice([1,5,100000,999999900]); add(op,b,first,first+rng.randrange(100))
elif op=='RESERVE': add(op,b,rng.choice([0,1,2,3,7,13,1000000,1000001]),rng.choice([0,1,2,3,8,64,65]))
elif op in ('COMMIT','CANCEL','PEEK'): add(op,b,rng.choice([0,1,2,3,7,13,1000000,1000001]))
elif op=='LIST': add(op,b,rng.randrange(9))
elif op=='NULL': add(op,b,rng.randrange(10))
else: add(op,b)
for b in range(4): add('STATS',b); add('LIST',b,8)
return rows
class Journal:
def __init__(self):
self.epoch=0; self.first=0; self.limit=0; self.events=[]
def live(self):
return sorted((e[0] for e in self.events if e[1]=='pending'),key=lambda x:x[1])
def stats(self):
if not self.epoch: return [0]*11
allocated=sum(e[0][4] for e in self.events)
committed=sum(e[0][4] for e in self.events if e[1]=='commit')
canceled=sum(e[0][4] for e in self.events if e[1]=='cancel')
pending=self.live()
sums=sum(e[0][5] for e in self.events if e[1]=='commit')
return [1,self.epoch,self.first,self.limit,self.first+allocated,allocated,committed,canceled,len(pending),sum(g[4] for g in pending),sums]
def apply(self,row):
op=row[0]; a=row[2] if len(row)>2 else 0; b=row[3] if len(row)>3 else 0
value=SENTINEL[:]; aux=AUX; rc=0
if op=='NULL':
if a==9:
rc=-1 if self.live() else 0
aux=AUX if rc else 0
else: rc=-1
elif op=='RESET':
if not (1<=a<=b<=1000000000): rc=-1
else: self.epoch+=1; self.first=a; self.limit=b; self.events=[]
elif op=='LIST':
live=self.live()
if a<len(live): rc=-1
else:
aux=len(live)
if live: value=list(live[0])
elif op=='RESERVE':
live=self.live(); owners={g[1] for g in live}; cursor=self.stats()[4]
if not (1<=a<=1000000 and 1<=b<=64): rc=-1
elif not self.epoch: rc=-2
elif a in owners: rc=-4
elif len(live)==8: rc=-5
elif cursor+b-1>self.limit: rc=-3
else:
ids=range(cursor,cursor+b)
value=[self.epoch,a,cursor,cursor+b-1,b,sum(ids)]
self.events.append([tuple(value),'pending'])
elif op in ('COMMIT','CANCEL','PEEK'):
found=next((e for e in self.events if e[1]=='pending' and e[0][1]==a),None)
if not 1<=a<=1000000: rc=-1
elif not self.epoch: rc=-2
elif found is None: rc=-6
else:
value=list(found[0])
if op!='PEEK': found[1]=op.lower()
return rc,value,aux
def oracle(rows):
books=[Journal() for _ in range(4)]; result=[]
names=['RESET','RESERVE','COMMIT','CANCEL','PEEK','STATS','LIST','NULL']
for index,row in enumerate(rows,1):
book=books[row[1]]; rc,value,aux=book.apply(row); live=book.live()
result.append([index,names.index(row[0])+1,row[1],rc,*value,aux,*book.stats(),0,len(live),*(x for grant in live for x in grant)])
return result
def payload_for(rows):
return ('\n'.join(' '.join(map(str,row)) for row in rows)+'\n').encode('ascii')
def decimal_token(token):
if not token: raise ValueError('empty decimal token')
sign=-1 if token[:1]==b'-' else 1
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 stdout token')
effective=digits.lstrip(b'0') or b'0'
if len(effective)>20: raise ValueError('observation outside uint64 range')
value=int(effective)*sign
if not -6<=value<=18446744073709551615: raise ValueError('observation outside defined range')
return value
def observations(data):
parts=data.split(b'\n')
if parts[-1]==b'': parts.pop()
result=[]
for line in parts:
if line.endswith(b'\r'): line=line[:-1]
line=line.strip(b' \t')
if not line: raise ValueError('empty stdout row')
tokens=[]; current=bytearray()
for c in line:
if c in (32,9):
if current: tokens.append(bytes(current)); current.clear()
else: current.append(c)
if current: tokens.append(bytes(current))
result.append([decimal_token(token) for token in tokens])
return result
def business_compare(data,expected):
actual=observations(data)
if len(actual)!=len(expected): raise ValueError('stdout row count %d expected %d'%(len(actual),len(expected)))
for i,(got,wanted) in enumerate(zip(actual,expected),1):
if got!=wanted:
for column,(x,y) in enumerate(zip(got,wanted)):
if x!=y: raise ValueError('raw observation difference row=%d column=%d got=%d expected=%d'%(i,column,x,y))
raise ValueError('raw observation width difference row=%d'%i)
def main():
evidence=[]; failures=[]; scratch=None; checks=0
rows=commands(); payload=payload_for(rows); expected=oracle(rows)
try:
scratch=Path(tempfile.mkdtemp(prefix='ticket-sequence-'))
project=scratch/'project'; project.mkdir(); project.chmod(0o777)
sources=dict(SUPPORT); sources['ticket.c']=(Path(sys.argv[1])/'ticket.c').read_text()
for name,source in sources.items():
path=project/name; path.write_text(source); path.chmod(0o644)
# All candidate-controlled paths and public inputs are prepared before the first child.
compiler=['gcc','-std=c11','-O2','-Wall','-Wextra','-Wpedantic','ticket.c','input.c','observer.c','-o','ticket-demo']
execute_stage('compiler',compiler,project,COMPILER_DEADLINE,b'',evidence)
output,diagnostic=execute_stage('candidate',[str(project/'ticket-demo')],project,CANDIDATE_DEADLINE,payload,evidence)
business_compare(output,expected); checks=len(expected)
except BaseException as error:
failures.append([type(error).__name__,str(error)])
finally:
if scratch is not None:
try:
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/tmp',CLEANUP_DEADLINE,b'',evidence)
except BaseException as error: failures.append(['cleanup',type(error).__name__,str(error)])
try:
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/tmp',ABSENCE_DEADLINE,b'',evidence)
except BaseException as error: failures.append(['absence',type(error).__name__,str(error)])
record={'stages':evidence,'failures':failures,'checks':checks,'input_len':len(payload),'input_sha256':hashlib.sha256(payload).hexdigest()}
encoded=base64.b64encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode()
sys.stderr.write('TICKET_EVIDENCE '+encoded+'\n')
print('FAIL '+repr(failures) if failures or checks!=len(expected) else 'PASS')
return 1 if failures or checks!=len(expected) else 0
if __name__=='__main__':
sys.exit(main())