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

384 lines
32 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]=[pid,None,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_waits':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_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) != 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:
entry['wire_ascii'] = 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 = {'include/scan.h': '#ifndef SCAN_H\n#define SCAN_H\n#include <stddef.h>\n#include <stdint.h>\n#define SCAN_CAPACITY 32u\n#define SCAN_EVENT_CAPACITY 64u\n#define SCAN_LIMIT 1000000u\n#define SCAN_HASH_MOD 1000000007u\n\ntypedef enum {\n SCAN_EXECUTE = 0,\n SCAN_PREVIEW = 1\n} ScanMode;\ntypedef enum {\n SCAN_ACCEPT = 0,\n SCAN_INACTIVE = 1,\n SCAN_FUTURE = 2,\n SCAN_PRIORITY = 3,\n SCAN_BUDGET = 4,\n SCAN_CREDIT = 5,\n SCAN_OUTSIDE = 6\n} ScanDecision;\ntypedef enum {\n EVENT_VISIT = 1,\n EVENT_COMMIT = 2\n} EventKind;\ntypedef struct {\n uint32_t id;\n uint32_t units;\n uint32_t ready;\n uint32_t priority;\n uint32_t active;\n} ScanJob;\ntypedef struct {\n ScanJob jobs[SCAN_CAPACITY];\n size_t count;\n uint32_t now;\n uint64_t credits;\n uint64_t revision;\n uint64_t delivered;\n} ScanStore;\ntypedef struct {\n uint32_t kind;\n uint32_t slot;\n uint32_t id;\n uint32_t units;\n uint32_t decision;\n} ScanEvent;\ntypedef struct {\n ScanEvent events[SCAN_EVENT_CAPACITY];\n size_t count;\n int overflow;\n} ScanPort;\ntypedef struct {\n uint32_t begin;\n uint32_t end;\n uint32_t minimum_priority;\n uint64_t budget;\n uint32_t mode;\n} ScanRequest;\ntypedef struct {\n uint32_t visits;\n uint32_t selected;\n uint64_t units;\n uint64_t digest;\n} ScanResult;\n\nvoid store_reset(ScanStore *store);\nint store_configure(ScanStore *store, size_t count, uint64_t credits, uint32_t now);\nint store_install(ScanStore *store, size_t slot, const ScanJob *job);\nint store_set_clock(ScanStore *store, uint32_t now, uint64_t credits);\nconst ScanJob *store_job(const ScanStore *store, size_t slot);\nint store_commit(ScanStore *store, size_t slot);\nuint64_t store_active_mask(const ScanStore *store);\nuint64_t store_fingerprint(const ScanStore *store);\nint store_valid(const ScanStore *store);\nvoid port_reset(ScanPort *port);\nvoid port_visit(ScanPort *port, size_t slot, const ScanJob *job, ScanDecision decision);\nvoid port_commit(ScanPort *port, size_t slot, const ScanJob *job);\nint request_valid(const ScanStore *store, const ScanRequest *request);\nScanDecision scan_decision(const ScanStore *store, const ScanJob *job,\n uint32_t floor, uint64_t budget, uint64_t credit);\nuint64_t scan_digest(uint64_t previous, const ScanJob *job, size_t slot);\nint scan_run(ScanStore *store, ScanPort *port,\n const ScanRequest *request, ScanResult *result);\n#endif\n', 'src/store.c': '#include "scan.h"\n#include <string.h>\n\nvoid store_reset(ScanStore *store)\n{\n memset(store, 0, sizeof(*store));\n}\nint store_configure(ScanStore *store, size_t count, uint64_t credits, uint32_t now)\n{\n if (count > SCAN_CAPACITY || credits > SCAN_LIMIT || now > SCAN_LIMIT) {\n return -1;\n }\n store_reset(store);\n store->count = count;\n store->credits = credits;\n store->now = now;\n return 0;\n}\nstatic int job_valid(const ScanJob *job)\n{\n if (job->id == 0 || job->id > SCAN_LIMIT) {\n return 0;\n }\n if (job->units > 1000 || job->ready > SCAN_LIMIT) {\n return 0;\n }\n if (job->priority > 9 || job->active > 1) {\n return 0;\n }\n return 1;\n}\nint store_install(ScanStore *store, size_t slot, const ScanJob *job)\n{\n size_t index;\n if (slot >= store->count || !job_valid(job)) {\n return -1;\n }\n for (index = 0; index < store->count; ++index) {\n if (index != slot && store->jobs[index].id == job->id) {\n return -1;\n }\n }\n store->jobs[slot] = *job;\n return 0;\n}\nint store_set_clock(ScanStore *store, uint32_t now, uint64_t credits)\n{\n if (now > SCAN_LIMIT || credits > SCAN_LIMIT) {\n return -1;\n }\n store->now = now;\n store->credits = credits;\n return 0;\n}\nconst ScanJob *store_job(const ScanStore *store, size_t slot)\n{\n if (slot >= store->count) {\n return NULL;\n }\n return &store->jobs[slot];\n}\nint store_commit(ScanStore *store, size_t slot)\n{\n ScanJob *job;\n if (slot >= store->count) {\n return -1;\n }\n job = &store->jobs[slot];\n if (job->active == 0 || store->credits < job->units) {\n return -1;\n }\n store->credits -= job->units;\n store->delivered += job->units;\n store->revision += 1;\n job->active = 0;\n return 0;\n}\nuint64_t store_active_mask(const ScanStore *store)\n{\n uint64_t result = 0;\n size_t index;\n for (index = 0; index < store->count; ++index) {\n if (store->jobs[index].active) {\n result |= UINT64_C(1) << index;\n }\n }\n return result;\n}\nstatic uint64_t fold(uint64_t previous, uint64_t value)\n{\n return (previous * 131 + value) % SCAN_HASH_MOD;\n}\nuint64_t store_fingerprint(const ScanStore *store)\n{\n uint64_t result = 0;\n size_t index;\n result = fold(result, store->count);\n result = fold(result, store->now);\n result = fold(result, store->credits);\n result = fold(result, store->revision);\n result = fold(result, store->delivered);\n for (index = 0; index < store->count; ++index) {\n const ScanJob *job = &store->jobs[index];\n result = fold(result, job->id);\n result = fold(result, job->units);\n result = fold(result, job->ready);\n result = fold(result, job->priority);\n result = fold(result, job->active);\n }\n return result;\n}\nint store_valid(const ScanStore *store)\n{\n size_t left;\n size_t right;\n if (store->count > SCAN_CAPACITY || store->now > SCAN_LIMIT) {\n return 0;\n }\n if (store->credits > SCAN_LIMIT) {\n return 0;\n }\n for (left = 0; left < store->count; ++left) {\n if (!job_valid(&store->jobs[left])) {\n return 0;\n }\n for (right = left + 1; right < store->count; ++right) {\n if (store->jobs[left].id == store->jobs[right].id) {\n return 0;\n }\n }\n }\n return 1;\n}\n', 'src/port.c': '#include "scan.h"\n#include <string.h>\n\nvoid port_reset(ScanPort *port)\n{\n memset(port, 0, sizeof(*port));\n}\nstatic void port_append(ScanPort *port, EventKind kind, size_t slot,\n const ScanJob *job, ScanDecision decision)\n{\n ScanEvent *event;\n if (port->count == SCAN_EVENT_CAPACITY) {\n port->overflow = 1;\n return;\n }\n event = &port->events[port->count];\n event->kind = kind;\n event->slot = (uint32_t)slot;\n event->id = job == NULL ? 0 : job->id;\n event->units = job == NULL ? 0 : job->units;\n event->decision = decision;\n port->count += 1;\n}\nvoid port_visit(ScanPort *port, size_t slot, const ScanJob *job, ScanDecision decision)\n{\n port_append(port, EVENT_VISIT, slot, job, decision);\n}\nvoid port_commit(ScanPort *port, size_t slot, const ScanJob *job)\n{\n port_append(port, EVENT_COMMIT, slot, job, SCAN_ACCEPT);\n}\nint request_valid(const ScanStore *store, const ScanRequest *request)\n{\n if (!store_valid(store)) {\n return 0;\n }\n if (request->begin > request->end || request->end > store->count) {\n return 0;\n }\n if (request->minimum_priority > 9 || request->budget > SCAN_LIMIT) {\n return 0;\n }\n if (request->mode != SCAN_EXECUTE && request->mode != SCAN_PREVIEW) {\n return 0;\n }\n return 1;\n}\nScanDecision scan_decision(const ScanStore *store, const ScanJob *job,\n uint32_t floor, uint64_t budget, uint64_t credit)\n{\n if (job == NULL) {\n return SCAN_OUTSIDE;\n }\n if (!job->active) {\n return SCAN_INACTIVE;\n }\n if (job->ready > store->now) {\n return SCAN_FUTURE;\n }\n if (job->priority < floor) {\n return SCAN_PRIORITY;\n }\n if (job->units > budget) {\n return SCAN_BUDGET;\n }\n if (job->units > credit) {\n return SCAN_CREDIT;\n }\n return SCAN_ACCEPT;\n}\nuint64_t scan_digest(uint64_t previous, const ScanJob *job, size_t slot)\n{\n uint64_t identity = (uint64_t)job->id * 17 + slot * 3 + job->units;\n return (previous * 257 + identity) % SCAN_HASH_MOD;\n}\n', 'src/observer.c': '#include "scan.h"\n#include <stdio.h>\n#include <inttypes.h>\n#include <limits.h>\n#include <string.h>\n\nstatic int is_separator(int value)\n{\n return value == \' \' || value == \'\\t\' || value == \'\\r\' || value == \'\\n\';\n}\nstatic int read_word(char *text, size_t capacity)\n{\n size_t used = 0;\n int value;\n do {\n value = getchar();\n } while (is_separator(value));\n if (value == EOF) {\n return 0;\n }\n while (value != EOF && !is_separator(value)) {\n if (value < \'A\' || value > \'Z\' || used + 1 >= capacity) {\n return -1;\n }\n text[used++] = (char)value;\n value = getchar();\n }\n text[used] = \'\\0\';\n return 1;\n}\nstatic int read_number(uint64_t *number)\n{\n int value;\n uint64_t result = 0;\n int digits = 0;\n int overflow = 0;\n do {\n value = getchar();\n } while (is_separator(value));\n if (value == \'+\') {\n value = getchar();\n }\n while (value != EOF && !is_separator(value)) {\n if (value < \'0\' || value > \'9\') {\n return -1;\n }\n digits = 1;\n if (result > (UINT64_MAX - (uint64_t)(value - \'0\')) / 10) {\n overflow = 1;\n } else if (!overflow) {\n result = result * 10 + (uint64_t)(value - \'0\');\n }\n value = getchar();\n }\n if (!digits || overflow) {\n return -1;\n }\n *number = result;\n return 0;\n}\nstatic int values(uint64_t *numbers, size_t count)\n{\n size_t index;\n for (index = 0; index < count; ++index) {\n if (read_number(&numbers[index]) != 0) {\n return -1;\n }\n }\n return 0;\n}\nstatic int initialize(ScanStore *store)\n{\n uint64_t numbers[5];\n size_t slot;\n if (values(numbers, 3) != 0 || numbers[0] > SCAN_CAPACITY ||\n numbers[1] > SCAN_LIMIT || numbers[2] > SCAN_LIMIT) {\n return -1;\n }\n if (store_configure(store, (size_t)numbers[0], numbers[1], (uint32_t)numbers[2]) != 0) {\n return -1;\n }\n for (slot = 0; slot < store->count; ++slot) {\n ScanJob job;\n if (values(numbers, 5) != 0 || numbers[0] > SCAN_LIMIT ||\n numbers[1] > 1000 || numbers[2] > SCAN_LIMIT ||\n numbers[3] > 9 || numbers[4] > 1) {\n return -1;\n }\n job.id = (uint32_t)numbers[0];\n job.units = (uint32_t)numbers[1];\n job.ready = (uint32_t)numbers[2];\n job.priority = (uint32_t)numbers[3];\n job.active = (uint32_t)numbers[4];\n if (store_install(store, slot, &job) != 0) {\n return -1;\n }\n }\n return store_valid(store) ? 0 : -1;\n}\nstatic int read_request(ScanRequest *request)\n{\n uint64_t numbers[5];\n if (values(numbers, 5) != 0) {\n return -1;\n }\n if (numbers[0] > UINT32_MAX || numbers[1] > UINT32_MAX ||\n numbers[2] > UINT32_MAX || numbers[4] > UINT32_MAX) {\n return -1;\n }\n request->begin = (uint32_t)numbers[0];\n request->end = (uint32_t)numbers[1];\n request->minimum_priority = (uint32_t)numbers[2];\n request->budget = numbers[3];\n request->mode = (uint32_t)numbers[4];\n return 0;\n}\nstatic void print_event(const ScanEvent *event)\n{\n printf("[%u,%u,%u,%u,%u]", event->kind, event->slot,\n event->id, event->units, event->decision);\n}\nstatic void observe(int rc, const ScanStore *store, const ScanPort *port,\n const ScanResult *result)\n{\n size_t index;\n printf("[%d,%u,%u,%" PRIu64 ",%" PRIu64 ",", rc, result->visits,\n result->selected, result->units, result->digest);\n printf("%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%zu,[",\n store->credits, store->revision, store->delivered,\n store_active_mask(store), store_fingerprint(store), port->count);\n for (index = 0; index < port->count; ++index) {\n if (index != 0) {\n putchar(\',\');\n }\n print_event(&port->events[index]);\n }\n puts("]]");\n}\nint main(void)\n{\n ScanStore store;\n ScanPort port;\n ScanResult result;\n char command[16];\n int found;\n int configured = 0;\n store_reset(&store);\n while ((found = read_word(command, sizeof(command))) == 1) {\n int rc = 0;\n port_reset(&port);\n memset(&result, 0, sizeof(result));\n if (strcmp(command, "INIT") == 0) {\n if (initialize(&store) != 0) {\n return 2;\n }\n configured = 1;\n } else if (!configured) {\n return 2;\n } else if (strcmp(command, "SCAN") == 0) {\n ScanRequest request;\n if (read_request(&request) != 0) {\n return 2;\n }\n rc = scan_run(&store, &port, &request, &result);\n } else if (strcmp(command, "SET") == 0) {\n uint64_t numbers[2];\n if (values(numbers, 2) != 0 || numbers[0] > SCAN_LIMIT ||\n numbers[1] > SCAN_LIMIT) {\n return 2;\n }\n rc = store_set_clock(&store, (uint32_t)numbers[0], numbers[1]);\n } else if (strcmp(command, "SNAP") != 0) {\n return 2;\n }\n observe(rc, &store, &port, &result);\n }\n if (found < 0 || ferror(stdin) || ferror(stdout)) {\n return 2;\n }\n return 0;\n}\n'}
COMPILER_SECONDS = 20
CANDIDATE_SECONDS = 8
CLEANUP_SECONDS = 5
ABSENCE_SECONDS = 5
def scenarios():
rng = random.Random(730241)
commands = []
def init(jobs, credit, now):
commands.append(('INIT',jobs,credit,now))
def scan(a,b,floor=0,budget=1000000,mode=0):
commands.append(('SCAN',a,b,floor,budget,mode))
init([[101,4,0,5,1],[102,7,12,8,1],[103,0,2,4,1],[104,5,0,3,1]],30,10)
scan(0,0,0,20);scan(0,4,4,20);scan(4,4,0,0)
commands.append(('SNAP',))
init([[201,4,0,5,1],[202,7,2,8,1],[203,5,0,3,1]],20,10)
scan(0,3,4,20,1);scan(0,3,4,20,1);scan(0,3,4,20,0)
for count in [0,1,2,8,32]:
jobs = [[700+i,0 if i%5==0 else i*7%39,i%4, i%10, int(i%3!=2)] for i in range(count)]
init(jobs,55,2)
for point in range(count+1): scan(point,point,0,40,point%2)
scan(0,count,0,0,1);scan(0,count,0,0)
scan(0,count,4,55,1);scan(0,count,4,55)
scan(0,count,0,1000000,1)
commands.append(('SET',1000000,1000000))
scan(0,count,0,1000000)
scan(count+1,count+1);scan(1,0);scan(0,count+1)
scan(0,count,10);scan(0,count,0,1000001);scan(0,count,0,4,2)
commands.append(('SNAP',))
for batch in range(18):
count=rng.randrange(1,33)
jobs=[[10000+batch*40+i,rng.randrange(0,1001),rng.randrange(0,21),rng.randrange(10),rng.randrange(2)] for i in range(count)]
init(jobs,rng.randrange(0,4001),rng.randrange(0,21))
for step in range(13):
lo=rng.randrange(count+1);hi=rng.randrange(lo,count+1)
scan(lo,hi,rng.randrange(10),rng.randrange(0,4001),step%3==0 and 1 or 0)
if step%4==3: commands.append(('SET',rng.randrange(0,21),rng.randrange(0,4001)))
return commands
def model(commands):
rows=[]; jobs=[];credits=now=revision=delivered=0
for cmd in commands:
result=[0,0,0,0,0];events=[]
if cmd[0]=='INIT':
jobs=[list(j) for j in cmd[1]];credits=cmd[2];now=cmd[3];revision=delivered=0
elif cmd[0]=='SET': now,credits=cmd[1:]
elif cmd[0]=='SCAN':
lo,hi,floor,budget,mode=cmd[1:]
if not (0<=lo<=hi<=len(jobs) and floor<=9 and budget<=1000000 and mode in (0,1)):
result[0]=-1
else:
virtual_credit=credits
for slot in range(lo,hi):
ident,units,ready,priority,active=jobs[slot]
conditions=[not active,ready>now,priority<floor,units>budget,units>virtual_credit]
decision=next((i+1 for i,x in enumerate(conditions) if x),0)
events.append([1,slot,ident,units,decision]);result[1]+=1
if decision==0:
budget-=units;virtual_credit-=units
result[2]+=1;result[3]+=units
result[4]=(result[4]*257+ident*17+slot*3+units)%1000000007
if mode==0:
jobs[slot][4]=0;credits-=units;revision+=1;delivered+=units
events.append([2,slot,ident,units,0])
mask=sum(1<<i for i,j in enumerate(jobs) if j[4])
fingerprint=0
state=[len(jobs),now,credits,revision,delivered]+[v for j in jobs for v in j]
for value in state: fingerprint=(fingerprint*131+value)%1000000007
rows.append(result+[credits,revision,delivered,mask,fingerprint,len(events),events])
return rows
def encode(commands):
lines=[]
for cmd in commands:
if cmd[0]=='INIT':
lines.append('INIT %d %d %d'%(len(cmd[1]),cmd[2],cmd[3]))
lines.extend(' '.join(map(str,j)) for j in cmd[1])
else: lines.append(' '.join(map(str,cmd)))
return ('\n'.join(lines)+'\n').encode('ascii')
def observations(raw):
def reject_float(value): raise ValueError('noninteger JSON number')
rows=[json.loads(line,parse_float=reject_float,parse_constant=reject_float) for line in raw.decode('utf-8').splitlines()]
def ints(value):
return type(value) is int or type(value) is list and all(ints(x) for x in value)
if not all(type(row) is list and ints(row) for row in rows): raise ValueError('invalid JSON observation types')
return rows
def main():
evidence=[];errors=[];scratch=None;success=False;cleanup_ok=False;absence_ok=False
try:
scratch=Path(tempfile.mkdtemp(prefix='empty-scan-'))
for name,data in SUPPORT.items():
path=scratch/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(data)
source=Path(sys.argv[1])/'src/scan.c'
(scratch/'src/scan.c').write_bytes(source.read_bytes())
for folder in ('src','include'): os.chmod(scratch/folder,0o777)
for name in [*SUPPORT,'src/scan.c']: os.chmod(scratch/name,0o644)
commands=scenarios();payload=encode(commands);expected=model(commands)
os.environ['SCAN_PRIVATE_CHECK']=str(Path(__file__).resolve())
compile_argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude','src/store.c','src/port.c','src/scan.c','src/observer.c','-o','program']
execute_stage('compiler',compile_argv,scratch,COMPILER_SECONDS,b'',evidence)
out,err=execute_stage('candidate',[str(scratch/'program')],scratch,CANDIDATE_SECONDS,payload,evidence)
actual=observations(out)
if actual!=expected:
mismatch=next((i for i,(a,b) in enumerate(zip(actual,expected)) if a!=b),min(len(actual),len(expected)))
raise ValueError('behavior difference row=%d actual=%s expected=%s'%(mismatch,str(actual[mismatch:mismatch+1])[:500],str(expected[mismatch:mismatch+1])[:500]))
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)],'/tmp',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)],'/tmp',ABSENCE_SECONDS,b'',evidence)
absence_ok=True
except BaseException as error: errors.append(['absence',type(error).__name__,str(error)])
record={'stages':evidence,'errors':errors,'success':success,'cleanup_ok':cleanup_ok,'absence_ok':absence_ok}
packed=base64.b64encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode()
print('EXEC_BZ2 '+packed,file=sys.stderr)
if success and cleanup_ok and absence_ok and not errors:
print('PASS');return 0
print('FAIL '+repr(errors)[:600]);return 1
if __name__=='__main__': sys.exit(main())