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>
374 lines
15 KiB
Python
374 lines
15 KiB
Python
"""Trusted receiver model; submitted C is executed only in child processes."""
|
|
import binascii
|
|
import json
|
|
from pathlib import Path
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
|
|
def frame(tag, payload=b'', flags=0):
|
|
header = b'\xc3\x5a\x01' + bytes([flags]) + tag.to_bytes(4, 'little') + bytes([len(payload)])
|
|
body = header + payload
|
|
return body + binascii.crc_hqx(body, 0xffff).to_bytes(2, 'little')
|
|
|
|
|
|
def unpack(data, tag):
|
|
if len(data) < 11 or len(data) > 107:
|
|
return None
|
|
if data[:3] != b'\xc3\x5a\x01' or data[3] > 3:
|
|
return None
|
|
if data[8] > 96 or len(data) != 11 + data[8]:
|
|
return None
|
|
if int.from_bytes(data[-2:], 'little') != binascii.crc_hqx(data[:-2], 0xffff):
|
|
return None
|
|
decoded_tag = int.from_bytes(data[4:8], 'little')
|
|
if decoded_tag != tag:
|
|
return None
|
|
return decoded_tag, data[3], data[8], data[9:-2]
|
|
|
|
|
|
def scenarios(seed):
|
|
rng = random.Random(seed)
|
|
demo_payload = b'window:0123456789abcdefghijklmnop'
|
|
demo = frame(305419896, demo_payload, 2)
|
|
events = [('R', 7), ('S', 0, 11, 96, 305419896), ('C', 0),
|
|
('D', 0, 1, demo), ('C', 0), ('C', 0), ('Z',),
|
|
('S', 1, 0, 128, 4294967295), ('D', 1, 2, b''), ('C', 1),
|
|
('S', 1, 15, 128, 0), ('D', 1, 1, frame(0, b'', 3)), ('C', 1), ('Z',)]
|
|
# Actual lengths exercise every residue, from empty payload through maximum.
|
|
for size in range(97):
|
|
offset = [0, 1, 15, 16, 31, 64, 128, 149][size % 8]
|
|
capacity = min(128, 256 - offset)
|
|
payload = bytes(rng.randrange(256) for _ in range(size))
|
|
tag = rng.getrandbits(32)
|
|
slot = size % 4
|
|
events += [('S', slot, offset, capacity, tag), ('C', slot),
|
|
('D', slot, 1, frame(tag, payload, size % 4)),
|
|
('C', slot), ('C', slot)]
|
|
# Explicit short, malformed, tag, and upper bank edge boundaries.
|
|
good = frame(8, b'edge')
|
|
malformed = [b'', b'\xc3', good[:-1], good + b'\0',
|
|
bytes([0]) + good[1:], good[:2] + b'\x02' + good[3:],
|
|
good[:-2] + b'\0\0', frame(9, b'tag'), frame(8, b'', 4),
|
|
b'\xc3\x5a\x01\x00\x08\x00\x00\x00\x61' + bytes(99)]
|
|
for index, raw in enumerate(malformed):
|
|
offset = 128 if len(raw) > 107 else 255 - max(1, len(raw))
|
|
events += [('S', index % 4, offset, min(128, 256-offset), 8),
|
|
('D', index % 4, 1, raw), ('C', index % 4)]
|
|
for slot, offset, capacity in [(4, 0, 1), (0, 256, 1), (0, 0, 0),
|
|
(0, 0, 129), (0, 255, 2), (3, 255, 1)]:
|
|
events.append(('S', slot, offset, capacity, 0))
|
|
events += [('D', 3, 1, b'\x11'), ('C', 3), ('C', 4), ('X', 4), ('X', 3)]
|
|
# Four simultaneous slots; terminal cancellation, pending cancellation, reset.
|
|
for round_index in range(35):
|
|
events.append(('R', rng.getrandbits(32)))
|
|
order = list(range(4))
|
|
rng.shuffle(order)
|
|
for slot in order:
|
|
offset = rng.randrange(150)
|
|
tag = rng.choice([0, 4294967295, rng.getrandbits(32)])
|
|
events += [('S', slot, offset, 107, tag), ('C', slot),
|
|
('S', slot, offset, 107, tag)]
|
|
if slot == round_index % 4:
|
|
continue
|
|
if rng.randrange(5) == 0:
|
|
raw, status = b'', 2
|
|
else:
|
|
payload = bytes(rng.randrange(256) for _ in range(rng.randrange(97)))
|
|
raw, status = frame(tag, payload, rng.randrange(4)), 1
|
|
if rng.randrange(7) == 0:
|
|
raw = raw[:-1] + bytes([raw[-1] ^ 0x80])
|
|
events.append(('D', slot, status, raw))
|
|
rng.shuffle(order)
|
|
for slot in order[:3]:
|
|
events.append((rng.choice(['C', 'X']), slot))
|
|
events.append(('Z',))
|
|
events.append(('S', 2, 128, 128, rng.getrandbits(32)))
|
|
events.append(('X', 2))
|
|
# Reuse without resetting the cache: each new completion must be refreshed.
|
|
events.append(('R', 19))
|
|
for index in range(48):
|
|
payload = bytes(rng.randrange(256) for _ in range(21 + index % 70))
|
|
tag = rng.getrandbits(32)
|
|
events += [('S', 0, index % 16, 128, tag),
|
|
('D', 0, 1, frame(tag, payload, index % 4)), ('C', 0)]
|
|
events += [('S', 0, 0, 11, 0), ('S', 1, 245, 11, 1),
|
|
('D', 1, 1, frame(1)), ('Z',), ('C', 1), ('X', 0)]
|
|
return events
|
|
|
|
|
|
def expected(events):
|
|
slots = {}
|
|
audit = [0] * 6 # acquired, returned, aborted, completed, live, violations
|
|
rows = []
|
|
|
|
def release(slot, cancel=False):
|
|
item = slots.pop(slot)
|
|
if cancel and item['state'] == 0:
|
|
audit[2] += 1
|
|
audit[1] += 1
|
|
audit[4] -= 1
|
|
|
|
for event in events:
|
|
op, *args = event
|
|
code = 0
|
|
result = None
|
|
read_bounds = (0, 0)
|
|
if op == 'R':
|
|
slots = {}
|
|
audit = [0] * 6
|
|
elif op == 'Z':
|
|
for slot in list(slots):
|
|
release(slot, True)
|
|
elif op == 'S':
|
|
slot, offset, capacity, tag = args
|
|
if not (0 <= slot < 4 and 0 <= offset < 256 and
|
|
1 <= capacity <= 128 and offset + capacity <= 256):
|
|
code = -1
|
|
elif slot in slots:
|
|
code = -2
|
|
else:
|
|
slots[slot] = dict(offset=offset, capacity=capacity, tag=tag, state=0, raw=b'')
|
|
audit[0] += 1
|
|
audit[4] += 1
|
|
elif op == 'D':
|
|
slot, state, raw = args
|
|
if not (0 <= slot < 4 and state in (1, 2)):
|
|
code = -1
|
|
elif slot not in slots or slots[slot]['state'] != 0:
|
|
code = -2
|
|
elif len(raw) > slots[slot]['capacity'] or (state == 2 and raw):
|
|
code = -1
|
|
else:
|
|
slots[slot]['state'] = state
|
|
slots[slot]['raw'] = raw
|
|
audit[3] += 1
|
|
elif op in ('C', 'X'):
|
|
slot, = args
|
|
if not 0 <= slot < 4:
|
|
code = -1
|
|
elif slot not in slots:
|
|
code = -2
|
|
elif op == 'X':
|
|
release(slot, True)
|
|
elif slots[slot]['state'] == 0:
|
|
code = 1
|
|
else:
|
|
item = slots[slot]
|
|
if item['state'] == 2:
|
|
code = -3
|
|
else:
|
|
result = unpack(item['raw'], item['tag'])
|
|
code = 0 if result is not None else -4
|
|
read_bounds = (len(item['raw']), len(item['raw'])) if result is not None else (0, len(item['raw']))
|
|
release(slot)
|
|
else:
|
|
raise ValueError('unmodelled command')
|
|
rows.append((op, code, result, [len(slots)] + audit[:], read_bounds))
|
|
return rows
|
|
|
|
|
|
def compare(stdout, answers):
|
|
lines = stdout.splitlines()
|
|
if len(lines) != 2 * len(answers):
|
|
return 'observation row count differs'
|
|
previous_reads = 0
|
|
for step, (op, code, result, audit, read_bounds) in enumerate(answers, 1):
|
|
observed = lines[2 * (step - 1)].split()
|
|
counters = lines[2 * (step - 1) + 1].split()
|
|
try:
|
|
if len(observed) != 8 or observed[:3] != ['O', str(step), op] or int(observed[3]) != code:
|
|
return 'command result differs at event %d' % step
|
|
if len(counters) != 10 or counters[:2] != ['A', str(step)] or list(map(int, counters[2:9])) != audit:
|
|
return 'resource observation differs at event %d' % step
|
|
current_reads = int(counters[9])
|
|
delta = current_reads if op == 'R' else current_reads - previous_reads
|
|
if not read_bounds[0] <= delta <= read_bounds[1]:
|
|
return 'CPU byte coverage differs at event %d' % step
|
|
previous_reads = current_reads
|
|
if result is not None:
|
|
tag, flags, size, payload = result
|
|
if list(map(int, observed[4:7])) != [tag, flags, size]:
|
|
return 'packet metadata differs at event %d' % step
|
|
actual = b'' if observed[7] == '-' else bytes.fromhex(observed[7])
|
|
if actual[:size] != payload or len(actual) < size:
|
|
return 'packet bytes differ at event %d' % step
|
|
except (ValueError, IndexError):
|
|
return 'malformed observation at event %d' % step
|
|
return None
|
|
|
|
|
|
|
|
# Pure execution only: this process has the same UID as its compiler/candidate.
|
|
# No task input generation, expected observations, or comparison runs here.
|
|
SUPERVISOR = r"""
|
|
import ctypes, errno, json, os, signal, subprocess, sys, time
|
|
phase, budget, encoded = sys.argv[1:]
|
|
limit = float(budget)
|
|
argv = json.loads(encoded)
|
|
identity = dict(uid=os.getuid(), gid=os.getgid(), groups=os.getgroups())
|
|
proc = None
|
|
timed_out = False
|
|
child_rc = None
|
|
reaped = 0
|
|
out = err = b''
|
|
|
|
def note(event, **fields):
|
|
fields.update(identity, event=event, phase=phase)
|
|
sys.stderr.buffer.write(('\n' + json.dumps(fields, sort_keys=True) + '\n').encode('ascii'))
|
|
sys.stderr.buffer.flush()
|
|
|
|
def kill_group():
|
|
if proc is not None:
|
|
try:
|
|
os.killpg(proc.pid, signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
|
|
def reap_children():
|
|
count = 0
|
|
until = time.monotonic() + 3
|
|
while True:
|
|
children = open('/proc/self/task/%d/children' % os.getpid()).read().split()
|
|
for pid in children:
|
|
try:
|
|
os.kill(int(pid), signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
try:
|
|
while True:
|
|
pid, status = os.waitpid(-1, os.WNOHANG)
|
|
if pid == 0:
|
|
break
|
|
count += 1
|
|
except ChildProcessError:
|
|
return count
|
|
if time.monotonic() >= until:
|
|
raise RuntimeError('owned descendants did not reap')
|
|
time.sleep(0.01)
|
|
|
|
try:
|
|
libc = ctypes.CDLL(None, use_errno=True)
|
|
if libc.prctl(36, 1, 0, 0, 0) != 0:
|
|
raise OSError(ctypes.get_errno(), 'PR_SET_CHILD_SUBREAPER')
|
|
data = sys.stdin.buffer.read()
|
|
note('execution_start', deadline_seconds=limit)
|
|
proc = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE, start_new_session=True)
|
|
try:
|
|
out, err = proc.communicate(data, timeout=limit)
|
|
except subprocess.TimeoutExpired:
|
|
timed_out = True
|
|
kill_group()
|
|
proc.wait(timeout=3)
|
|
reaped += reap_children()
|
|
out, err = proc.communicate(timeout=3)
|
|
child_rc = proc.returncode
|
|
kill_group()
|
|
reaped += reap_children()
|
|
sys.stdout.buffer.write(out)
|
|
sys.stdout.buffer.flush()
|
|
sys.stderr.buffer.write(err)
|
|
note('execution_end', child_returncode=child_rc, timed_out=timed_out,
|
|
descendants_reaped=reaped, direct_child_reaped=proc.returncode is not None)
|
|
if timed_out:
|
|
raise SystemExit(124)
|
|
raise SystemExit(child_rc if child_rc >= 0 else 128 - child_rc)
|
|
except SystemExit:
|
|
raise
|
|
except BaseException as error:
|
|
try:
|
|
kill_group()
|
|
if proc is not None:
|
|
proc.wait(timeout=3)
|
|
reaped += reap_children()
|
|
except BaseException as cleanup_error:
|
|
note('execution_reap_error', detail=type(cleanup_error).__name__)
|
|
note('execution_error', detail=type(error).__name__, descendants_reaped=reaped)
|
|
raise SystemExit(125)
|
|
"""
|
|
|
|
CLEANUP = r"""
|
|
import json, os, shutil, sys
|
|
path = sys.argv[1]
|
|
shutil.rmtree(path)
|
|
absent = not os.path.lexists(path)
|
|
print(json.dumps(dict(scratch=path, absent=absent, uid=os.getuid(), gid=os.getgid(), groups=os.getgroups())))
|
|
raise SystemExit(0 if absent else 1)
|
|
"""
|
|
|
|
|
|
def execute(argv, phase, deadline, cwd, data=b''):
|
|
result = subprocess.run([sys.executable, '-I', '-c', SUPERVISOR,
|
|
phase, str(deadline), json.dumps(argv)],
|
|
cwd=cwd, input=data, capture_output=True)
|
|
# stderr has no text protocol. Preserve execution metadata and replace only
|
|
# undecodable diagnostic bytes; candidate stdout is decoded separately.
|
|
if result.stderr:
|
|
print(result.stderr[-1800:].decode('utf-8', 'replace'), file=sys.stderr, end='')
|
|
print('execution result phase=%s rc=%d' % (phase, result.returncode), file=sys.stderr)
|
|
return result
|
|
|
|
|
|
def main():
|
|
config = json.loads(Path(__file__).with_name('context.json').read_text())
|
|
events = scenarios(config['seed'])
|
|
answers = expected(events)
|
|
commands = ('\n'.join(' '.join((value.hex() or '-') if isinstance(value, bytes)
|
|
else str(value) for value in event) for event in events) + '\n').encode('ascii')
|
|
scratch = None
|
|
failure = None
|
|
try:
|
|
scratch = Path(tempfile.mkdtemp(prefix='rx-window-'))
|
|
print('scratch prepared: ' + str(scratch), file=sys.stderr)
|
|
# All candidate-controlled material is prepared before the first child runs.
|
|
for name, content in config['context'].items():
|
|
(scratch / name).write_text(content)
|
|
(scratch / 'rx_window.c').write_bytes((Path(sys.argv[1]) / 'rx_window.c').read_bytes())
|
|
executable = str(scratch / 'rx-demo')
|
|
build = execute(['cc', '-std=c11', '-O2', '-Wall', '-Wextra', '-Wpedantic',
|
|
'rx_window.c', 'port.c', 'wire.c', 'demo.c', '-o', executable],
|
|
'compiler', 30, str(scratch))
|
|
if build.returncode != 0:
|
|
failure = 'candidate build timeout' if build.returncode == 124 else 'candidate build failed'
|
|
else:
|
|
run = execute([executable], 'candidate', 20, str(scratch), commands)
|
|
if run.returncode != 0:
|
|
failure = 'candidate timeout' if run.returncode == 124 else 'candidate execution failed'
|
|
else:
|
|
try:
|
|
failure = compare(run.stdout.decode('utf-8'), answers)
|
|
except UnicodeDecodeError:
|
|
failure = 'candidate stdout is not valid UTF-8 protocol'
|
|
except Exception as error:
|
|
failure = 'execution exception: ' + type(error).__name__
|
|
finally:
|
|
if scratch is not None:
|
|
# Only this candidate-identity child accesses the controlled tree.
|
|
# The trusted parent never touches it after compilation starts.
|
|
try:
|
|
cleanup = execute([sys.executable, '-I', '-c', CLEANUP, str(scratch)],
|
|
'cleanup', 15, '/')
|
|
if cleanup.returncode != 0:
|
|
failure = 'candidate-identity scratch cleanup failed'
|
|
else:
|
|
observation = json.loads(cleanup.stdout.decode('utf-8'))
|
|
print('scratch cleanup: ' + json.dumps(observation, sort_keys=True), file=sys.stderr)
|
|
if observation.get('absent') is not True:
|
|
failure = 'candidate-identity scratch cleanup unconfirmed'
|
|
except Exception as error:
|
|
failure = 'candidate-identity scratch cleanup exception: ' + type(error).__name__
|
|
print(failure, file=sys.stderr)
|
|
if failure is not None:
|
|
print(failure)
|
|
return 1
|
|
print('raw observation rows: %d' % (2 * len(events)), file=sys.stderr)
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|