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>
478 lines
33 KiB
Python
478 lines
33 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
|
|
|
|
PROTECTED_RECORD_DIR = None
|
|
|
|
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,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)]
|
|
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) != 4 or not identity_int(row[0],1)
|
|
or row[1] is not None or type(row[2]) is not int or row[2] < 0
|
|
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 PROTECTED_RECORD_DIR is not None:
|
|
target = Path(PROTECTED_RECORD_DIR)
|
|
(target / (nonce+'.wire')).write_bytes(raw.stdout)
|
|
(target / (nonce+'.wrapper-stderr')).write_bytes(raw.stderr)
|
|
(target / (nonce+'.input')).write_bytes(payload)
|
|
(target / (nonce+'.wrapper.json')).write_text(json.dumps(entry,sort_keys=True))
|
|
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 = {'README.md': '# Protocol lane replay\n\nRun `make`, then `make demo`. For a separate optional boundary session run `make edge`.\nThe complete public contract is [docs/protocol.md](docs/protocol.md).\nThe command line executable accepts stdin or a file: `./width-replay examples/demo.txt`.\nThe numeric transcript is a raw view of the lane state and recent audit journal.\n', 'examples/demo.txt': '314159\n1 0 3 1 0\n1 0 -1 1 0\n4 1 0 0 0\n2 1 2 5 2\n5 1 0 0 0\n7 2 1 0 0\n8 2 3 0 0\n', 'examples/high-edge.txt': '271828\n2 0 31 31 1\n4 1 0 0 0\n2 1 0 31 0\n5 1 0 0 0\n2 7 12 31 2\n', 'tools/observe.c': '#define _POSIX_C_SOURCE 200809L\n#include "width.h"\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic void print_event(const PWEvent *event)\n{\n printf(" %" PRIu32 " %" PRId32 " %" PRId32 " %" PRId32,\n event->sequence, event->request.op, event->request.lane, event->request.a);\n printf(" %" PRId32 " %" PRId32 " %" PRId32 " %" PRIu32 " %" PRIu32,\n event->request.b, event->request.c, event->result, event->before, event->after);\n}\nstatic void print_row(const PWContext *context, int result)\n{\n PWSnapshot snapshot;\n pw_snapshot(context, &snapshot);\n size_t count = pw_audit_count(&context->audit);\n printf("%d %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %zu",\n result, snapshot.sequence, snapshot.ok, snapshot.bad,\n snapshot.audit, snapshot.invalid_requests, count);\n for (int i = 0; i < PW_LANES; ++i) {\n const PWLane *lane = &snapshot.lanes[i];\n printf(" %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32,\n lane->live, lane->pending, lane->active, lane->dirty);\n printf(" %" PRIu32 " %" PRIu32 " %" PRIu32,\n lane->generation, lane->accepted, lane->rejected);\n }\n for (size_t i = 0; i < count; ++i) {\n PWEvent event;\n memset(&event, 0, sizeof(event));\n int rc = pw_audit_at(&context->audit, i, &event);\n if (rc != 0) {\n printf(" %d", rc);\n } else {\n print_event(&event);\n }\n }\n putchar(\'\\n\');\n}\nstatic int blank(const char *line, size_t length)\n{\n for (size_t i = 0; i < length; ++i) {\n if (line[i] != \' \' && line[i] != \'\\t\' && line[i] != \'\\r\') {\n return 0;\n }\n }\n return 1;\n}\nint main(int argc, char **argv)\n{\n if (argc > 2) {\n return 2;\n }\n FILE *input = stdin;\n if (argc == 2) {\n input = fopen(argv[1], "rb");\n if (input == NULL) {\n return 2;\n }\n }\n char *line = NULL;\n size_t capacity = 0;\n ssize_t received = getline(&line, &capacity, input);\n int32_t seed;\n int status = 0;\n PWContext context;\n if (received < 0) {\n status = 2;\n } else {\n size_t length = (size_t)received;\n if (length && line[length - 1] == \'\\n\') {\n --length;\n }\n if (pw_parse_seed(line, length, &seed) != 0) {\n status = 2;\n } else {\n pw_init(&context, seed);\n print_row(&context, 0);\n }\n }\n while (status == 0 && (received = getline(&line, &capacity, input)) >= 0) {\n size_t length = (size_t)received;\n if (length && line[length - 1] == \'\\n\') {\n --length;\n }\n if (blank(line, length)) {\n continue;\n }\n PWRequest request;\n if (pw_parse_request(line, length, &request) != 0) {\n status = 2;\n break;\n }\n int result = pw_apply(&context, &request);\n print_row(&context, result);\n }\n if (ferror(input)) {\n status = 2;\n }\n free(line);\n if (input != stdin && fclose(input) != 0) {\n status = 2;\n }\n return status;\n}\n', 'docs/protocol.md': "# Width replay contract v1\n\n1. This is a deterministic software model of eight 32-bit protocol lanes. It uses C11, gcc and make, without hardware or external services. Build with `make`; `make demo` runs only `examples/demo.txt`. The separate optional command `make edge` runs `examples/high-edge.txt`; it is not loaded by the default demo.\n2. `width-replay [file]` reads the named file, or stdin with no file. The first line contains one signed 32-bit decimal seed. Each subsequent line contains exactly five signed 32-bit decimal integers: `op lane a b c`. Decimal tokens allow an optional leading + or -, arbitrary leading zeroes and digits 0..9 only. Separators within a line are space, TAB or CR; lines end in LF, and a final unterminated line is allowed. Empty request lines are skipped. Invalid lexical input exits 2; valid request streams exit 0 even when a request is rejected. stderr may contain arbitrary diagnostic bytes and does not affect the result.\n3. The public API is `pw_init`, `pw_apply`, `pw_effective`, `pw_snapshot`, and the audit query helpers in `include/width.h`. `pw_apply` returns 0 for success, 1 for invalid op/lane/argument, 2 for an incompatible transaction state. All input arguments fit int32_t. All mathematical word, sequence, generation, count and audit arithmetic wraps modulo 2^32.\n4. Initialization assigns lane i (0..7) the word `uint32(seed) XOR (0x9e3779b9 * (i+1) mod 2^32)`. Each lane starts inactive, not dirty, with generation/accepted/rejected zero. Global sequence/ok/bad are zero; audit starts at 2166136261. Port invalid_requests starts zero.\n5. Every request, including a rejected one, increments global sequence. A successful request increments global ok and the addressed lane's accepted counter. A rejection increments global bad and, if lane is 0..7, that lane's rejected counter. There is no reset command. Unknown op or invalid lane returns 1 before other argument or state checks. All reserved arguments stated below must be zero, otherwise return 1 before state checks. Rejected requests leave words, active/dirty flags and generations unchanged.\n6. `op=1 BIT`: a is a bit position in [0,31], b is 0 (clear) or 1 (set), c is reserved zero. On success set/clear that bit of the effective word. Reject any other a or b with 1.\n7. `op=2 RANGE`: a and b are inclusive low/high positions with 0<=a<=b<32; c is 0 (clear), 1 (set), or 2 (toggle). Affect exactly those positions and no others. Reject any other range or mode with 1.\n8. `op=3 WRITE`: use the 32-bit unsigned representation of a as the new effective word. b and c are reserved zero.\n9. `op=4 BEGIN`: a,b,c are reserved zero. Return 2 if already active; otherwise capture the live word as pending, set active=1 and dirty=0. No generation change.\n10. `op=5 COMMIT`: a,b,c are reserved zero. Return 2 if inactive. Otherwise publish pending as live, increment generation once if dirty=1, then clear active and dirty.\n11. `op=6 ABORT`: a,b,c are reserved zero. Return 2 if inactive; otherwise discard pending, clear active and dirty; no generation change.\n12. `op=7 COPY`: a is a source lane in [0,7]; b,c are reserved zero. Read that lane's effective word and assign it to the addressed lane's effective word. Self copy is valid; source counters do not change.\n13. `op=8 SWAP`: a is a peer lane in [0,7]; b,c are reserved zero. Return 2 if either lane is active. Otherwise exchange their live words. Increment each generation iff that lane's word changed. For self swap no generation changes. Only the addressed lane's accepted/rejected counter changes.\n14. `op=9 RESTORE`: a,b,c are reserved zero. Assign the addressed lane's initialization word to its effective word.\n15. An inactive lane's effective word is live. An active lane's effective word is pending. Every successful data assignment (BIT/RANGE/WRITE/COPY/RESTORE) updates the effective word; while active, dirty becomes 1 if it ever changed during that transaction and stays 1 until COMMIT/ABORT. While inactive, increment generation exactly when that assignment changes live. A no-change successful assignment still counts as accepted. BEGIN/COMMIT/ABORT and SWAP follow their own generation rules above.\n16. The port in `support/port.c` is a controlled replacement for a shift instruction: `pw_port_shift(port,value,exponent)` returns `(value << exponent) mod 2^32` for 0<=exponent<32. Other exponents increment invalid_requests and return zero without executing an invalid shift. Correct processing never requests an invalid exponent, including rejected commands. Direct arithmetic or other correct algorithms are allowed; the number of valid port requests is unconstrained.\n17. Audit appends one event after each request. Before is the addressed lane's effective word before processing, or 0 for an invalid lane. After is its effective word afterwards, or 0 for invalid lane. Fold the eight unsigned 32-bit values `[new_sequence, uint32(op),uint32(lane),uint32(a),uint32(b),uint32(c),uint32(result),before]`, followed by after, into the current audit: `digest = (digest XOR value) * 16777619 mod 2^32` for each value. Keep the newest 16 events, oldest first. Each event stores sequence, op,lane,a,b,c,result,before,after. `pw_audit_at` returns 0 plus that event for a valid index, otherwise 1 without touching its output. `pw_audit_count` and `pw_audit_digest` report count and digest.\n18. `pw_snapshot` copies all eight lane records and reports sequence/ok/bad/audit and invalid_requests without modifying anything. `pw_effective` returns 0 for an invalid lane. The observer prints one numeric row after initialization, then after every request. A row is `[result,sequence,ok,bad,audit,invalid_requests,audit_count]`, followed by seven numbers per lane `[live,pending,active,dirty,generation,accepted,rejected]`, then newest audit events oldest-first (nine numbers per event). Initial result is 0. All printed values are unsigned except result and the event op/lane/a/b/c/result, which are signed. Each row ends LF; arbitrary space/TAB/CR horizontal spacing, optional leading +, and leading zeroes are allowed. Inactive pending words are unspecified and are ignored; only the active prefix of the audit buffer is defined. No other stdout text is permitted. The observer contains no expected answers or assertions.\n19. Submission consists only of `include/width.h`, `src/engine.c`, `src/audit.c`, and `src/format.c`. The public Makefile, support, observer and examples are fixed support and are restored unchanged for formal builds; edits to them are not submitted. Formal builds use gcc with `-std=c11 -O2 -Wall -Wextra -Iinclude -Isupport` on these C files plus `support/port.c` and `tools/observe.c`; no additional libraries. Keep this interface compatible with the fixed public build and satisfy all clauses above; implementation and data organization may vary.\n", 'support/port.h': '#ifndef PW_PORT_H\n#define PW_PORT_H\n#include <stdint.h>\ntypedef struct {\n uint32_t invalid_requests;\n} PWPort;\nvoid pw_port_init(PWPort *port);\nuint32_t pw_port_shift(PWPort *port, uint32_t value, int64_t exponent);\n#endif\n', 'support/port.c': '#include "port.h"\nvoid pw_port_init(PWPort *port)\n{\n port->invalid_requests = 0;\n}\nuint32_t pw_port_shift(PWPort *port, uint32_t value, int64_t exponent)\n{\n if (exponent < 0 || exponent >= 32) {\n port->invalid_requests++;\n return 0;\n }\n return value << (unsigned)exponent;\n}\n', 'Makefile': 'CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra\nCPPFLAGS = -Iinclude -Isupport\nSOURCES = src/engine.c src/audit.c src/format.c support/port.c tools/observe.c\nHEADERS = include/width.h support/port.h\n.PHONY: all demo edge clean\nall: width-replay\nwidth-replay: $(SOURCES) $(HEADERS)\n\t$(CC) $(CFLAGS) $(CPPFLAGS) $(SOURCES) -o $@\ndemo: width-replay\n\t./width-replay examples/demo.txt\nedge: width-replay\n\t./width-replay examples/high-edge.txt\nclean:\n\trm -f width-replay\n'}
|
|
|
|
CANDIDATES = ['include/width.h', 'src/engine.c', 'src/audit.c', 'src/format.c']
|
|
|
|
COMPILE_DEADLINE = 20
|
|
CANDIDATE_DEADLINE = 8
|
|
CLEANUP_DEADLINE = 5
|
|
ABSENCE_DEADLINE = 5
|
|
MASK = (1 << 32) - 1
|
|
|
|
def model(seed, requests):
|
|
initial = [(seed & MASK) ^ ((0x9e3779b9 * (i+1)) & MASK) for i in range(8)]
|
|
lanes = [dict(live=x,pending=x,active=0,dirty=0,generation=0,accepted=0,rejected=0) for x in initial]
|
|
journal = []
|
|
sequence = ok = bad = 0
|
|
digest = 2166136261
|
|
def effective(i):
|
|
return lanes[i]['pending'] if lanes[i]['active'] else lanes[i]['live']
|
|
def row(result):
|
|
values = [result,sequence,ok,bad,digest,0,len(journal)]
|
|
for lane in lanes:
|
|
values += [lane['live'],lane['pending'] if lane['active'] else None,lane['active'],lane['dirty'],lane['generation'],lane['accepted'],lane['rejected']]
|
|
for event in journal:
|
|
values += list(event)
|
|
return values
|
|
rows = [row(0)]
|
|
for op,lane,a,b,c in requests:
|
|
before = effective(lane) if 0 <= lane < 8 else 0
|
|
result = 0
|
|
assigned = None
|
|
if not 1 <= op <= 9 or not 0 <= lane < 8:
|
|
result = 1
|
|
elif op == 1:
|
|
if not 0 <= a < 32 or b not in (0,1) or c != 0:
|
|
result = 1
|
|
else:
|
|
mask = 2 ** a
|
|
assigned = before | mask if b else before & (MASK ^ mask)
|
|
elif op == 2:
|
|
if not 0 <= a <= b < 32 or c not in (0,1,2):
|
|
result = 1
|
|
else:
|
|
mask = (2 ** (b-a+1) - 1) * 2 ** a
|
|
assigned = (before & (MASK ^ mask), before | mask, before ^ mask)[c]
|
|
elif op == 3:
|
|
if b or c:
|
|
result = 1
|
|
else:
|
|
assigned = a & MASK
|
|
elif op in (4,5,6,9):
|
|
if a or b or c:
|
|
result = 1
|
|
elif op == 9:
|
|
assigned = initial[lane]
|
|
elif (op == 4) == bool(lanes[lane]['active']):
|
|
result = 2
|
|
elif op == 4:
|
|
lanes[lane].update(active=1,dirty=0,pending=before)
|
|
elif op == 5:
|
|
state = lanes[lane]
|
|
state['live'] = state['pending']
|
|
state['generation'] = (state['generation'] + state['dirty']) & MASK
|
|
state.update(active=0,dirty=0)
|
|
else:
|
|
lanes[lane].update(active=0,dirty=0)
|
|
elif op in (7,8):
|
|
if not 0 <= a < 8 or b or c:
|
|
result = 1
|
|
elif op == 7:
|
|
assigned = effective(a)
|
|
elif lanes[lane]['active'] or lanes[a]['active']:
|
|
result = 2
|
|
elif lane != a and before != effective(a):
|
|
first,second = lanes[lane],lanes[a]
|
|
first['live'],second['live'] = second['live'],first['live']
|
|
for state in (first,second):
|
|
state['generation'] = (state['generation']+1) & MASK
|
|
if assigned is not None:
|
|
state = lanes[lane]
|
|
if state['active']:
|
|
state['dirty'] |= int(state['pending'] != assigned)
|
|
state['pending'] = assigned
|
|
else:
|
|
state['generation'] = (state['generation'] + int(state['live'] != assigned)) & MASK
|
|
state['live'] = assigned
|
|
sequence = (sequence+1) & MASK
|
|
if result:
|
|
bad = (bad+1) & MASK
|
|
if 0 <= lane < 8:
|
|
lanes[lane]['rejected'] = (lanes[lane]['rejected']+1) & MASK
|
|
else:
|
|
ok = (ok+1) & MASK
|
|
lanes[lane]['accepted'] = (lanes[lane]['accepted']+1) & MASK
|
|
after = effective(lane) if 0 <= lane < 8 else 0
|
|
event = (sequence,op,lane,a,b,c,result,before,after)
|
|
journal = (journal+[event])[-16:]
|
|
for value in event:
|
|
digest = ((digest ^ (value & MASK)) * 16777619) & MASK
|
|
rows.append(row(result))
|
|
return rows
|
|
|
|
def cases():
|
|
actions = [(1,0,3,1,0),(1,0,-1,1,0),(4,1,0,0,0),(2,1,2,5,2),(5,1,0,0,0),(7,2,1,0,0),(8,2,3,0,0)]
|
|
actions += [(2,0,31,31,1),(4,1,0,0,0),(2,1,0,31,0),(5,1,0,0,0),(2,7,12,31,2)]
|
|
for lane in range(8):
|
|
for position in (-2147483648,-33,-1,0,1,30,31,32,33,2147483647):
|
|
actions.append((1,lane,position,(position+lane) % 2,0))
|
|
for low,high in ((0,0),(0,31),(31,31),(1,30),(30,31),(-1,0),(0,32),(10,9),(32,32)):
|
|
for mode in range(3): actions.append((2,lane,low,high,mode))
|
|
actions += [(4,lane,0,0,0),(4,lane,0,0,0),(3,lane,-1,0,0),(3,lane,0,0,0),(3,lane,-1,0,0),(5,lane,0,0,0),(6,lane,0,0,0),(4,lane,0,0,0),(9,lane,0,0,0),(6,lane,0,0,0)]
|
|
rng = random.Random(17432026)
|
|
edges = [-2147483648,-2,-1,0,1,2,7,8,15,30,31,32,2147483647]
|
|
for i in range(700):
|
|
op = rng.randrange(-1,12)
|
|
lane = rng.choice([-1,0,1,2,3,4,5,6,7,8])
|
|
if op == 1:
|
|
a,b,c = rng.choice(edges),rng.choice([-1,0,1,2]),rng.choice([0,0,0,1])
|
|
elif op == 2:
|
|
a,b,c = rng.choice(edges),rng.choice(edges),rng.randrange(-1,4)
|
|
elif op == 3:
|
|
a,b,c = rng.randint(-2147483648,2147483647),0,rng.choice([0,0,1])
|
|
elif op in (7,8):
|
|
a,b,c = rng.randrange(-1,9),0,rng.choice([0,0,0,1])
|
|
else:
|
|
a,b,c = rng.choice([0,0,0,1]),0,0
|
|
actions.append((op,lane,a,b,c))
|
|
# Explicit recovery and dirty history, source-active copy and equal-word swap.
|
|
for lane in range(8):
|
|
actions += [(6,lane,0,0,0),(3,lane,0,0,0),(4,lane,0,0,0),(1,lane,31,1,0),(1,lane,31,0,0),(7,(lane+1)%8,lane,0,0),(5,lane,0,0,0),(8,lane,lane,0,0),(9,lane,0,0,0)]
|
|
seed = -17032026
|
|
payload = (str(seed)+'\n'+'\n'.join(' '.join(map(str,x)) for x in actions)+'\n').encode('ascii')
|
|
return seed,actions,payload
|
|
|
|
def decimal(token):
|
|
sign = 1
|
|
if token[:1] in (b'+',b'-'):
|
|
if token[:1] == b'-': sign = -1
|
|
token = token[1:]
|
|
if not token or any(c < 48 or c > 57 for c in token):
|
|
raise ValueError('nondecimal stdout token')
|
|
token = token.lstrip(b'0') or b'0'
|
|
if len(token) > 10:
|
|
raise ValueError('stdout integer outside observable range')
|
|
return sign*int(token)
|
|
|
|
def observations(output):
|
|
if not output.endswith(b'\n'):
|
|
raise ValueError('missing stdout LF')
|
|
result = []
|
|
for line in output[:-1].split(b'\n'):
|
|
if any(ch not in b'0123456789+- \t\r' for ch in line):
|
|
raise ValueError('stdout lexical violation')
|
|
tokens = line.replace(b'\t',b' ').replace(b'\r',b' ').split(b' ')
|
|
result.append([decimal(x) for x in tokens if x])
|
|
return result
|
|
|
|
def compare(output, expected):
|
|
actual = observations(output)
|
|
if len(actual) != len(expected):
|
|
raise ValueError('row count actual=%d expected=%d'%(len(actual),len(expected)))
|
|
for index,(seen,wanted) in enumerate(zip(actual,expected)):
|
|
if len(seen) != len(wanted):
|
|
raise ValueError('raw row width at %d actual=%d expected=%d'%(index,len(seen),len(wanted)))
|
|
for column,(got,need) in enumerate(zip(seen,wanted)):
|
|
if need is not None and got != need:
|
|
raise ValueError('raw observation at row=%d column=%d actual=%d expected=%d'%(index,column,got,need))
|
|
|
|
def main(candidate):
|
|
evidence = []
|
|
failures = []
|
|
scratch = None
|
|
success = False
|
|
cleanup_ok = False
|
|
absence_ok = False
|
|
try:
|
|
scratch = Path(tempfile.mkdtemp(prefix='width-check-'))
|
|
# Everything in the candidate tree is prepared before any candidate identity child starts.
|
|
for name in CANDIDATES:
|
|
target = scratch/name
|
|
target.parent.mkdir(parents=True,exist_ok=True)
|
|
target.write_bytes((candidate/name).read_bytes())
|
|
for name,data in SUPPORT.items():
|
|
target = scratch/name
|
|
target.parent.mkdir(parents=True,exist_ok=True)
|
|
target.write_text(data)
|
|
if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534':
|
|
for path in scratch.rglob('*'):
|
|
os.chown(path,65534,65534)
|
|
seed,requests,payload = cases()
|
|
expected = model(seed,requests)
|
|
argv = ['gcc','-std=c11','-O2','-Wall','-Wextra','-Iinclude','-Isupport','src/engine.c','src/audit.c','src/format.c','support/port.c','tools/observe.c','-o','width-replay']
|
|
execute_stage('compiler',argv,scratch,COMPILE_DEADLINE,b'',evidence)
|
|
output,diagnostic = execute_stage('candidate',[str(scratch/'width-replay')],scratch,CANDIDATE_DEADLINE,payload,evidence)
|
|
compare(output,expected)
|
|
success = True
|
|
except BaseException as error:
|
|
failures.append(['execution',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)
|
|
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)],'/tmp',ABSENCE_DEADLINE,b'',evidence)
|
|
absence_ok = True
|
|
except BaseException as error:
|
|
failures.append(['absence',type(error).__name__,str(error)])
|
|
record = dict(stages=evidence,failures=failures,scratch=str(scratch),success=success,cleanup_ok=cleanup_ok,absence_ok=absence_ok)
|
|
marker = base64.b85encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode('ascii')
|
|
print('EVIDENCE85 '+marker,file=sys.stderr)
|
|
passed = success and cleanup_ok and absence_ok and not failures
|
|
print('PASS' if passed else 'FAIL '+repr(failures)[:500])
|
|
return 0 if passed else 1
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main(Path(sys.argv[1])))
|