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>
631 lines
28 KiB
Python
631 lines
28 KiB
Python
"""Independent lock-stack oracle and candidate-identity execution supervisor."""
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import zlib
|
|
|
|
CANDIDATES = ('src/manager.cpp', 'src/port.cpp')
|
|
CONTEXT = Path(__file__).with_name('context.json')
|
|
LEVELS = (10, 20, 35, 50, 80, 120)
|
|
PRODUCTION = {'compile': 18.0, 'candidate': 14.0, 'cleanup': 8.0, 'absence': 8.0}
|
|
|
|
# The supervisor only executes argv and forwards raw bytes. It knows no oracle outcomes.
|
|
SUPERVISOR = r'''
|
|
import base64, ctypes, hashlib, json, math, os, selectors, signal, subprocess, sys, time
|
|
|
|
def identity(pid):
|
|
try:
|
|
raw = open('/proc/%d/stat' % pid, 'rb').read()
|
|
except (FileNotFoundError, ProcessLookupError):
|
|
return None
|
|
tail = raw[raw.rfind(b')') + 2:].split()
|
|
return (tail[0].decode('ascii'), int(tail[1]), int(tail[2]), int(tail[19]))
|
|
|
|
def closure_from_table(table, root_pid, root_start):
|
|
root = table.get(root_pid)
|
|
if root is None or root[3] != root_start or root[0] in ('Z','X'):
|
|
return {}
|
|
owned = {root_pid}
|
|
changed = True
|
|
while changed:
|
|
changed = False
|
|
for pid, item in table.items():
|
|
if (pid not in owned and item[0] not in ('Z','X') and
|
|
item[1] in owned):
|
|
owned.add(pid)
|
|
changed = True
|
|
return {pid: table[pid] for pid in owned if pid != root_pid}
|
|
|
|
def related(root_pid, root_start):
|
|
table = {}
|
|
for name in os.listdir('/proc'):
|
|
if not name.isascii() or not name.isdigit():
|
|
continue
|
|
pid = int(name)
|
|
try:
|
|
item = identity(pid)
|
|
except PermissionError:
|
|
continue
|
|
if item is not None:
|
|
table[pid] = item
|
|
return closure_from_table(table, root_pid, root_start)
|
|
|
|
def owned_group_present(groups):
|
|
for group, start in groups.items():
|
|
leader = identity(group)
|
|
if leader is not None and leader[3] != start:
|
|
continue
|
|
for name in os.listdir('/proc'):
|
|
if not name.isascii() or not name.isdigit():
|
|
continue
|
|
try:
|
|
item = identity(int(name))
|
|
except PermissionError:
|
|
continue
|
|
if item is not None and item[0] not in ('Z','X') and item[2] == group:
|
|
return True
|
|
return False
|
|
|
|
def fresh_kill(pid, start):
|
|
item = identity(pid)
|
|
if item is None or item[3] != start or item[0] in ('Z', 'X'):
|
|
return False
|
|
try:
|
|
os.kill(pid, signal.SIGKILL)
|
|
return True
|
|
except ProcessLookupError:
|
|
return False
|
|
|
|
def fresh_group_kill(group, pid, start):
|
|
item = identity(pid)
|
|
if item is None or item[3] != start or item[0] in ('Z','X') or item[2] != group:
|
|
return False
|
|
if group == os.getpgrp():
|
|
return False
|
|
try:
|
|
os.killpg(group, signal.SIGKILL)
|
|
return True
|
|
except ProcessLookupError:
|
|
return False
|
|
|
|
def digest(data):
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
phase, deadline_text, input64, *argv = sys.argv[1:]
|
|
deadline = float(deadline_text)
|
|
input_data = base64.b64decode(input64)
|
|
start = time.monotonic()
|
|
self_pid = os.getpid()
|
|
self_item = identity(self_pid)
|
|
errors = []
|
|
reaped = []
|
|
groups_seen = {}
|
|
groups_killed = []
|
|
leader = None
|
|
leader_start = None
|
|
leader_rc = None
|
|
timed_out = False
|
|
sent = 0
|
|
out = bytearray()
|
|
err = bytearray()
|
|
sel = selectors.DefaultSelector()
|
|
try:
|
|
if ctypes.CDLL(None).prctl(36, 1, 0, 0, 0) != 0: # PR_SET_CHILD_SUBREAPER
|
|
raise OSError('subreaper prctl failed')
|
|
hold_read, hold_write = os.pipe()
|
|
boot = 'import os,sys; os.read(int(sys.argv[1]),1); os.execvp(sys.argv[2],sys.argv[2:])'
|
|
leader = subprocess.Popen([sys.executable,'-I','-c',boot,str(hold_read),*argv],
|
|
pass_fds=(hold_read,), stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
start_new_session=True)
|
|
os.close(hold_read)
|
|
leader_item = identity(leader.pid)
|
|
if leader_item is None:
|
|
raise RuntimeError('leader start identity missing')
|
|
leader_start = leader_item[3]
|
|
groups_seen[leader.pid] = leader_start
|
|
os.write(hold_write, b'1')
|
|
os.close(hold_write)
|
|
os.set_blocking(leader.stdin.fileno(), False)
|
|
pending = memoryview(input_data)
|
|
sent = 0
|
|
if pending:
|
|
sel.register(leader.stdin, selectors.EVENT_WRITE, None)
|
|
else:
|
|
leader.stdin.close()
|
|
for stream, target in ((leader.stdout, out), (leader.stderr, err)):
|
|
os.set_blocking(stream.fileno(), False)
|
|
sel.register(stream, selectors.EVENT_READ, target)
|
|
while True:
|
|
if leader_rc is None:
|
|
leader_rc = leader.poll()
|
|
for pid, item in related(self_pid, self_item[3]).items():
|
|
group = item[2]
|
|
if group not in groups_seen:
|
|
leader_of_group = identity(group)
|
|
if leader_of_group is not None:
|
|
groups_seen[group] = leader_of_group[3]
|
|
if leader_rc is not None and not sel.get_map():
|
|
break
|
|
if time.monotonic() - start >= deadline:
|
|
timed_out = True
|
|
break
|
|
for key, _ in sel.select(0.04):
|
|
if key.fileobj is leader.stdin:
|
|
try:
|
|
sent += os.write(key.fileobj.fileno(), pending[sent:sent + 65536])
|
|
if sent == len(pending):
|
|
sel.unregister(key.fileobj)
|
|
key.fileobj.close()
|
|
except BrokenPipeError:
|
|
sel.unregister(key.fileobj)
|
|
key.fileobj.close()
|
|
continue
|
|
chunk = os.read(key.fileobj.fileno(), 65536)
|
|
if chunk:
|
|
key.data.extend(chunk)
|
|
else:
|
|
sel.unregister(key.fileobj)
|
|
# A leader may leave a closed-stdio setsid child. Reap it before claiming absence.
|
|
except BaseException as exc:
|
|
errors.append(type(exc).__name__ + ':' + str(exc)[:160])
|
|
finally:
|
|
recovery_end = time.monotonic() + 2.0
|
|
try:
|
|
while time.monotonic() < recovery_end:
|
|
descendants = related(self_pid, self_item[3])
|
|
if leader is not None and leader_rc is None:
|
|
leader_rc = leader.poll()
|
|
for pid, item in descendants.items():
|
|
group = item[2]
|
|
if group not in groups_seen:
|
|
leader_of_group = identity(group)
|
|
if leader_of_group is not None:
|
|
groups_seen[group] = leader_of_group[3]
|
|
if fresh_group_kill(group, pid, item[3]):
|
|
groups_killed.append(group)
|
|
fresh_kill(pid, item[3])
|
|
if leader is not None and leader_rc is None and leader_start is not None:
|
|
fresh_kill(leader.pid, leader_start)
|
|
leader_rc = leader.poll()
|
|
if leader_rc is not None:
|
|
while True:
|
|
try:
|
|
pid, status = os.waitpid(-1, os.WNOHANG)
|
|
except ChildProcessError:
|
|
break
|
|
if pid == 0:
|
|
break
|
|
reaped.append([pid, status])
|
|
if leader_rc is not None and not related(self_pid, self_item[3]) and not owned_group_present(groups_seen):
|
|
break
|
|
time.sleep(0.02)
|
|
if leader is not None and leader_rc is None:
|
|
try:
|
|
leader_rc = leader.wait(timeout=0.1)
|
|
except subprocess.TimeoutExpired:
|
|
errors.append('leader_wait_timeout')
|
|
for key in list(sel.get_map().values()):
|
|
if leader is not None and key.fileobj is leader.stdin:
|
|
sel.unregister(key.fileobj)
|
|
key.fileobj.close()
|
|
continue
|
|
try:
|
|
while True:
|
|
chunk = os.read(key.fileobj.fileno(), 65536)
|
|
if not chunk:
|
|
break
|
|
key.data.extend(chunk)
|
|
except BlockingIOError:
|
|
pass
|
|
sel.unregister(key.fileobj)
|
|
remaining = related(self_pid, self_item[3])
|
|
pg_absent = not owned_group_present(groups_seen)
|
|
except BaseException as exc:
|
|
errors.append('recovery_' + type(exc).__name__ + ':' + str(exc)[:160])
|
|
remaining = {self_pid: ('?', 0, 0, 0)}
|
|
pg_absent = False
|
|
elapsed = time.monotonic() - start
|
|
rc = 124 if timed_out else leader_rc
|
|
wire = {
|
|
'phase': phase, 'deadline': deadline, 'elapsed': elapsed,
|
|
'supervisor_pid': self_pid, 'supervisor_start': self_item[3],
|
|
'leader_pid': leader.pid if leader else None, 'leader_start': leader_start,
|
|
'uid': os.getuid(), 'gid': os.getgid(), 'groups': os.getgroups(),
|
|
'argv': argv, 'timeout': timed_out, 'rc': rc, 'leader_rc': leader_rc,
|
|
'leader_reaped': leader_rc is not None,
|
|
'descendants_absent': not remaining, 'pg_absent': pg_absent,
|
|
'pg_groups': [[group, start] for group, start in sorted(groups_seen.items())],
|
|
'pg_killed': sorted(set(groups_killed)), 'remaining': sorted(remaining),
|
|
'reaped': reaped, 'exceptions': errors,
|
|
'stdin_len': len(input_data), 'stdin_sha256': digest(input_data),
|
|
'stdin_sent': sent,
|
|
'stdout_len': len(out), 'stdout_sha256': digest(out),
|
|
'stderr_len': len(err), 'stderr_sha256': digest(err),
|
|
'stdout_b64': base64.b64encode(out).decode('ascii'),
|
|
'stderr_b64': base64.b64encode(err).decode('ascii')
|
|
}
|
|
sys.stdout.write(json.dumps(wire, separators=(',', ':')) + '\n')
|
|
sys.stdout.flush()
|
|
'''
|
|
|
|
|
|
def sha(data):
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
def exact_int(value, positive=False):
|
|
return type(value) is int and (not positive or value > 0)
|
|
|
|
|
|
def exact_number(value, low, high):
|
|
return type(value) in (int, float) and math.isfinite(value) and low <= value <= high
|
|
|
|
|
|
def inspect_wire(raw, phase, deadline, expected_uid, expected_gid, expected_groups):
|
|
wire = json.loads(raw)
|
|
if not isinstance(wire, dict):
|
|
raise ValueError('wire object')
|
|
mandatory = ('phase','deadline','elapsed','supervisor_pid','supervisor_start',
|
|
'leader_pid','leader_start','uid','gid','groups','argv','timeout',
|
|
'rc','leader_rc','leader_reaped','descendants_absent','remaining',
|
|
'pg_absent','pg_groups','pg_killed','reaped','exceptions',
|
|
'stdin_len','stdin_sha256','stdin_sent','stdout_len',
|
|
'stdout_sha256','stderr_len','stderr_sha256','stdout_b64','stderr_b64')
|
|
if any(field not in wire for field in mandatory):
|
|
raise ValueError('wire missing fields')
|
|
if wire['phase'] != phase or not exact_number(wire['deadline'], deadline, deadline):
|
|
raise ValueError('wire phase/deadline')
|
|
if not exact_number(wire['elapsed'], 0, deadline + 3):
|
|
raise ValueError('wire elapsed')
|
|
for key in ('supervisor_pid','supervisor_start','leader_pid','leader_start'):
|
|
if not exact_int(wire[key], True):
|
|
raise ValueError('wire pid/start')
|
|
if wire['supervisor_pid'] == wire['leader_pid']:
|
|
raise ValueError('wire self leader')
|
|
if not exact_int(wire['uid']) or not exact_int(wire['gid']):
|
|
raise ValueError('wire identity type')
|
|
groups = wire['groups']
|
|
if type(groups) is not list or any(type(x) is not int for x in groups):
|
|
raise ValueError('wire group type')
|
|
if (wire['uid'],wire['gid'],groups) != (expected_uid,expected_gid,expected_groups):
|
|
raise ValueError('wire identity mismatch')
|
|
if type(wire['argv']) is not list or not wire['argv'] or any(type(x) is not str for x in wire['argv']):
|
|
raise ValueError('wire argv')
|
|
if (type(wire['timeout']) is not bool or type(wire['leader_reaped']) is not bool or
|
|
type(wire['descendants_absent']) is not bool or type(wire['pg_absent']) is not bool):
|
|
raise ValueError('wire flags')
|
|
if not exact_int(wire['rc']) or not exact_int(wire['leader_rc']):
|
|
raise ValueError('wire rc')
|
|
if (wire['timeout'] and wire['rc'] != 124) or (not wire['timeout'] and wire['rc'] != wire['leader_rc']):
|
|
raise ValueError('wire rc/timeout')
|
|
if (not wire['leader_reaped'] or not wire['descendants_absent'] or
|
|
not wire['pg_absent'] or type(wire['remaining']) is not list or wire['remaining']):
|
|
raise ValueError('wire unreaped descendant')
|
|
if (type(wire['reaped']) is not list or any(type(x) is not list or len(x) != 2 or
|
|
any(type(y) is not int for y in x) for x in wire['reaped']) or
|
|
type(wire['exceptions']) is not list or wire['exceptions']):
|
|
raise ValueError('wire exceptions')
|
|
groups_seen = wire['pg_groups']
|
|
if (type(groups_seen) is not list or not groups_seen or
|
|
any(type(x) is not list or len(x) != 2 or any(not exact_int(y, True) for y in x)
|
|
for x in groups_seen) or
|
|
[wire['leader_pid'],wire['leader_start']] not in groups_seen):
|
|
raise ValueError('wire pg groups')
|
|
if (type(wire['pg_killed']) is not list or
|
|
any(not exact_int(x, True) or x not in [p[0] for p in groups_seen]
|
|
for x in wire['pg_killed'])):
|
|
raise ValueError('wire pg killed')
|
|
if (not exact_int(wire['stdin_len']) or wire['stdin_len'] < 0 or
|
|
not exact_int(wire['stdin_sent']) or not 0 <= wire['stdin_sent'] <= wire['stdin_len'] or
|
|
type(wire['stdin_sha256']) is not str or len(wire['stdin_sha256']) != 64):
|
|
raise ValueError('wire stdin')
|
|
for side in ('stdout','stderr'):
|
|
count = wire[side + '_len']
|
|
if not exact_int(count) or count < 0:
|
|
raise ValueError('wire io length')
|
|
value = base64.b64decode(wire[side + '_b64'], validate=True)
|
|
if len(value) != count or sha(value) != wire[side + '_sha256']:
|
|
raise ValueError('wire io digest')
|
|
wire[side + '_bytes'] = value
|
|
return wire
|
|
|
|
|
|
def read_candidate(root, name):
|
|
fd = os.open(root, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
try:
|
|
bits = name.split('/')
|
|
for part in bits[:-1]:
|
|
next_fd = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=fd)
|
|
os.close(fd)
|
|
fd = next_fd
|
|
src = os.open(bits[-1], os.O_RDONLY | os.O_NOFOLLOW, dir_fd=fd)
|
|
try:
|
|
if not stat.S_ISREG(os.fstat(src).st_mode):
|
|
raise ValueError('candidate not regular')
|
|
with os.fdopen(src, 'rb', closefd=False) as stream:
|
|
return stream.read(1024 * 1024 + 1)
|
|
finally:
|
|
os.close(src)
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
class Model:
|
|
def __init__(self):
|
|
self.stacks = {owner: [] for owner in range(1, 5)}
|
|
self.port = [0] * 6
|
|
self.attempts = self.successes = self.failures = self.reads = self.resets = 0
|
|
|
|
def take(self, owner, lock):
|
|
if owner not in self.stacks or lock not in range(6): return -22
|
|
stack = self.stacks[owner]
|
|
if stack and LEVELS[lock] <= LEVELS[stack[-1]]: return -35
|
|
if self.port[lock]: return -16
|
|
stack.append(lock); self.port[lock] = owner
|
|
return 0
|
|
|
|
def drop(self, owner, lock):
|
|
if owner not in self.stacks or lock not in range(6): return -22
|
|
stack = self.stacks[owner]
|
|
if lock in stack[:-1]: return -35
|
|
if not self.port[lock]: return -2
|
|
if self.port[lock] != owner: return -1
|
|
stack.pop(); self.port[lock] = 0
|
|
return 0
|
|
|
|
def chain(self, owner, locks):
|
|
if owner not in self.stacks or not 1 <= len(locks) <= 6 or any(x not in range(6) for x in locks): return -22
|
|
old_stack = list(self.stacks[owner]); old_port = list(self.port)
|
|
for lock in locks:
|
|
status = self.take(owner, lock)
|
|
if status:
|
|
self.stacks[owner] = old_stack; self.port = old_port
|
|
return status
|
|
return 0
|
|
|
|
def apply(self, command):
|
|
parts = command.split(); op = parts[0]; args = list(map(int, parts[1:]))
|
|
if op in ('T','D','B','A'):
|
|
if op == 'T': status = self.take(*args)
|
|
elif op == 'D': status = self.drop(*args)
|
|
elif op == 'B': status = self.chain(args[0], args[1:])
|
|
else:
|
|
owner = args[0]
|
|
status = -22 if owner not in self.stacks else 0
|
|
if status == 0:
|
|
for lock in reversed(self.stacks[owner][:]):
|
|
status = self.drop(owner, lock)
|
|
if status: break
|
|
self.attempts += 1
|
|
if status == 0: self.successes += 1
|
|
else: self.failures += 1
|
|
return ('R', status)
|
|
if op == 'Z':
|
|
self.stacks = {owner: [] for owner in range(1, 5)}; self.port = [0] * 6
|
|
self.resets += 1; return ('Z', 0)
|
|
if op == 'J': return ('J', self.attempts, self.successes, self.failures, self.reads, self.resets)
|
|
self.reads += 1
|
|
if op == 'V':
|
|
owner = args[0]; stack = self.stacks.get(owner, [])
|
|
top = -22 if owner not in self.stacks else LEVELS[stack[-1]] if stack else 0
|
|
return ('V', owner, top, len(stack), *stack)
|
|
if op == 'P':
|
|
lock = args[0]; return ('P', lock, self.port[lock] if lock in range(6) else -22)
|
|
if op == 'C': return ('C', sum(bool(x) for x in self.port))
|
|
if op == 'S':
|
|
fields = [sum(bool(x) for x in self.port), *self.port]
|
|
for owner in range(1, 5):
|
|
stack = self.stacks[owner]
|
|
fields += [LEVELS[stack[-1]] if stack else 0, len(stack), *stack]
|
|
return ('S', *fields)
|
|
if op == 'I':
|
|
masks = [sum(1 << lock for lock in self.stacks[owner]) for owner in range(1, 5)]
|
|
return ('I', sum(bool(x) for x in self.port), 0, 0, 0, *masks)
|
|
if op in ('F','G','E'):
|
|
owner = args[0]
|
|
if op == 'E':
|
|
results = []
|
|
for lock in range(6):
|
|
if owner not in self.stacks: result = -22
|
|
elif lock in self.stacks[owner][:-1]: result = -35
|
|
elif self.stacks[owner] and self.stacks[owner][-1] == lock: result = 0
|
|
elif not self.port[lock]: result = -2
|
|
else: result = -1
|
|
results.append(result)
|
|
return ('E', owner, *results)
|
|
results = []
|
|
for lock in range(6):
|
|
if owner not in self.stacks: status = -22
|
|
elif self.stacks[owner] and LEVELS[lock] <= LEVELS[self.stacks[owner][-1]]: status = -35
|
|
elif self.port[lock]: status = -16
|
|
else: status = 0
|
|
results.append(status)
|
|
if op == 'F': return ('F', owner, *results)
|
|
eligible = [i for i, status in enumerate(results) if status == 0]
|
|
return ('G', owner, len(eligible), eligible[0] if eligible else -1)
|
|
raise ValueError(op)
|
|
|
|
|
|
def cases():
|
|
segments = [
|
|
['T 1 0','T 1 3','V 1','P 3','I','F 2','T 2 1','S','D 1 3','V 1','T 1 1','S'],
|
|
['Z','T 1 0','T 1 4','D 1 4','T 1 1','D 1 1','D 1 0','C','I'],
|
|
['Z','B 1 0 2 5','S','B 2 1 3','S','B 2 4 5','S','A 1','B 2 4 5','S'],
|
|
['Z','T 2 2','T 1 2','D 1 2','E 2','D 2 2','P 2','I'],
|
|
['Z','T 1 0','T 1 3','D 1 0','S','D 1 3','D 1 0','J','I'],
|
|
['Z','F 0','G 0','E 0','V 0','P 6','A 0','B 0 1','T 5 0','D 5 0','J'],
|
|
]
|
|
rng = random.Random(13013)
|
|
for _ in range(3):
|
|
commands = ['Z']
|
|
for index in range(110):
|
|
owner = rng.randrange(1, 5); lock = rng.randrange(0, 6)
|
|
action = rng.randrange(10)
|
|
if action < 3: commands.append(f'T {owner} {lock}')
|
|
elif action < 5: commands.append(f'D {owner} {lock}')
|
|
elif action == 5: commands.append(f'B {owner} {lock} {rng.randrange(6)}')
|
|
elif action == 6: commands.append(f'A {owner}')
|
|
elif action == 7: commands.append(f'V {owner}')
|
|
elif action == 8: commands.append(f'F {owner}')
|
|
else: commands.append(f'G {owner}')
|
|
if index % 7 == 0: commands.extend(['S','I','C','J'])
|
|
segments.append(commands)
|
|
return segments
|
|
|
|
|
|
def parse_stdout(data):
|
|
if not data.endswith(b'\n'): raise ValueError('missing final LF')
|
|
lines = data[:-1].split(b'\n')
|
|
parsed = []
|
|
for line in lines:
|
|
parts = [x for x in re.split(rb'[ \t\r]+', line.strip(b' \t\r')) if x]
|
|
if not parts or len(parts[0]) != 1 or parts[0] not in (b'R',b'Z',b'V',b'P',b'C',b'S',b'I',b'F',b'G',b'E',b'J'):
|
|
raise ValueError('output tag')
|
|
values = []
|
|
for token in parts[1:]:
|
|
if re.fullmatch(rb'[+-]?[0-9]+', token) is None:
|
|
raise ValueError('output decimal')
|
|
sign = -1 if token.startswith(b'-') else 1
|
|
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
|
|
if len(digits) > 10: raise ValueError('output range')
|
|
value = sign * int(digits)
|
|
if not -2147483648 <= value <= 2147483647: raise ValueError('output range')
|
|
values.append(value)
|
|
parsed.append((parts[0].decode('ascii'), *values))
|
|
return parsed
|
|
|
|
|
|
def stage(phase, argv, input_bytes, expected_uid, expected_gid, expected_groups):
|
|
deadline = PRODUCTION[phase]
|
|
command = [sys.executable, '-I', '-c', SUPERVISOR, phase, str(deadline),
|
|
base64.b64encode(input_bytes).decode('ascii'), *argv]
|
|
completed = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
timeout=deadline + 8)
|
|
raw = completed.stdout
|
|
# Capture the actual wrapper return and raw wire before parsing, even on malformed wire.
|
|
record = {'phase':phase,'wrapper_rc':completed.returncode,'wire_len':len(raw),
|
|
'wire_sha256':sha(raw),'wrapper_stderr_len':len(completed.stderr),
|
|
'wrapper_stderr_sha256':sha(completed.stderr)}
|
|
try:
|
|
if completed.returncode != 0: raise ValueError('supervisor nonzero')
|
|
wire = inspect_wire(raw, phase, deadline, expected_uid, expected_gid, expected_groups)
|
|
if wire['argv'] != argv: raise ValueError('wire argv mismatch')
|
|
if wire['stdin_len'] != len(input_bytes) or wire['stdin_sha256'] != sha(input_bytes):
|
|
raise ValueError('wire stdin mismatch')
|
|
if not wire['timeout'] and wire['rc'] == 0 and wire['stdin_sent'] != len(input_bytes):
|
|
raise ValueError('wire stdin incomplete')
|
|
record.update({key:wire[key] for key in ('rc','leader_rc','timeout','leader_reaped',
|
|
'descendants_absent','pg_absent','pg_groups','pg_killed','elapsed',
|
|
'deadline','supervisor_pid','supervisor_start',
|
|
'leader_pid','leader_start','uid','gid','groups','stdout_len',
|
|
'stdout_sha256','stderr_len','stderr_sha256','stdin_len',
|
|
'stdin_sha256','stdin_sent','exceptions')})
|
|
return wire, record
|
|
except Exception as error:
|
|
record['error'] = type(error).__name__ + ':' + str(error)
|
|
return None, record
|
|
|
|
|
|
def main(candidate):
|
|
expected_uid = 65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else os.getuid()
|
|
expected_gid = 65534 if expected_uid == 65534 else os.getgid()
|
|
expected_groups = [] if expected_uid == 65534 else os.getgroups()
|
|
scratch = None
|
|
records = []
|
|
faults = []
|
|
observed = 0
|
|
behavior_ok = False
|
|
compile_ok = False
|
|
try:
|
|
context = json.loads(CONTEXT.read_text())
|
|
source = {name: read_candidate(candidate, name) for name in CANDIDATES}
|
|
if any(len(data) > 1024 * 1024 for data in source.values()):
|
|
raise ValueError('candidate oversize')
|
|
traffic = [command for segment in cases() for command in segment]
|
|
model = Model()
|
|
expected = [model.apply(command) for command in traffic]
|
|
traffic_bytes = ('\n'.join(traffic) + '\n').encode('ascii')
|
|
scratch = Path(tempfile.mkdtemp(prefix='cc-lo-013-'))
|
|
for name, data in {**{k:v.encode() for k,v in context.items()}, **source}.items():
|
|
target = scratch / name
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_bytes(data)
|
|
target.chmod(0o644)
|
|
if expected_uid != os.getuid():
|
|
os.chown(target.parent, expected_uid, expected_gid)
|
|
os.chown(target, expected_uid, expected_gid)
|
|
sources = [str(scratch / name) for name in ('src/main.cpp','src/script.cpp',
|
|
'src/trace.cpp','src/audit.cpp','src/manager.cpp','src/port.cpp')]
|
|
binary = str(scratch / 'level-demo')
|
|
compile_argv = ['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic',
|
|
'-I'+str(scratch/'include'), *sources, '-o',binary]
|
|
wire, record = stage('compile', compile_argv, b'', expected_uid, expected_gid, expected_groups)
|
|
records.append(record)
|
|
compile_ok = wire is not None and wire['rc'] == 0 and not wire['timeout']
|
|
if not compile_ok:
|
|
faults.append('compile_failure')
|
|
else:
|
|
wire, record = stage('candidate', [binary], traffic_bytes,
|
|
expected_uid, expected_gid, expected_groups)
|
|
records.append(record)
|
|
if wire is None or wire['rc'] != 0 or wire['timeout']:
|
|
faults.append('candidate_execution')
|
|
else:
|
|
try:
|
|
got = parse_stdout(wire['stdout_bytes'])
|
|
observed = len(got)
|
|
if got != expected:
|
|
offset = next((i for i,(a,b) in enumerate(zip(got,expected)) if a != b), min(len(got),len(expected)))
|
|
faults.append('behavior_row_%d' % offset)
|
|
else:
|
|
behavior_ok = True
|
|
except Exception as error:
|
|
faults.append('output_' + type(error).__name__)
|
|
except BaseException as error:
|
|
faults.append('prepare_or_check_' + type(error).__name__ + ':' + str(error)[:100])
|
|
finally:
|
|
if scratch is not None:
|
|
cleanup_code = 'import shutil,sys; shutil.rmtree(sys.argv[1])'
|
|
absence_code = ('import os,sys\ntry: os.lstat(sys.argv[1])\n'
|
|
'except FileNotFoundError: raise SystemExit(0)\n'
|
|
'raise SystemExit(1)')
|
|
try:
|
|
wire, record = stage('cleanup', [sys.executable,'-I','-c',cleanup_code,str(scratch)],
|
|
b'', expected_uid, expected_gid, expected_groups)
|
|
records.append(record)
|
|
if wire is None or wire['rc'] != 0 or wire['timeout']:
|
|
faults.append('cleanup_failure')
|
|
except BaseException as error:
|
|
faults.append('cleanup_exception_' + type(error).__name__)
|
|
try:
|
|
wire, record = stage('absence', [sys.executable,'-I','-c',absence_code,str(scratch)],
|
|
b'', expected_uid, expected_gid, expected_groups)
|
|
records.append(record)
|
|
if wire is None or wire['rc'] != 0 or wire['timeout']:
|
|
faults.append('absence_failure')
|
|
except BaseException as error:
|
|
faults.append('absence_exception_' + type(error).__name__)
|
|
summary = {'records':records,'faults':faults,'observations':observed,
|
|
'compile_ok':compile_ok,'behavior_ok':behavior_ok}
|
|
packed = zlib.compress(json.dumps(summary,separators=(',',':')).encode('utf-8'), 9)
|
|
sys.stderr.write('CC13META:' + base64.b64encode(packed).decode('ascii') + '\n')
|
|
if not faults and compile_ok and behavior_ok and scratch is not None and len(records) >= 3:
|
|
print('PASS')
|
|
return 0
|
|
print(faults[0] if faults else 'incomplete')
|
|
return 1
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main(sys.argv[1]))
|