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

738 lines
31 KiB
Python

"""Independent byte oracle and bounded candidate-identity execution for this task."""
import base64
import hashlib
import json
import math
import os
from pathlib import Path
import random
import stat
import subprocess
import sys
import tempfile
import time
import zlib
CANDIDATES = ("src/rr.c", "src/rr_math.c", "src/rr_plan.c")
SOURCE = ("include/rr.h", "include/sim_port.h", "src/demo.c",
"src/sim_port.c", "src/batch_observer.c", "Makefile")
MASK = (1 << 64) - 1
COMPILER_DEADLINE = 20.0
EXECUTE_DEADLINE = 8.0
CLEANUP_DEADLINE = 5.0
ABSENCE_DEADLINE = 5.0
# This code is passed to Python -c as a child under the candidate identity.
# It executes the requested argv, forwards raw IO, then recovers its own
# process tree. It knows no expected business result.
SUPERVISOR = r'''
import base64, ctypes, hashlib, json, os, selectors, signal, subprocess, sys, time
MISSING = (FileNotFoundError, ProcessLookupError)
DEAD = ('Z', 'X', 'x')
def proc_stat(pid):
try:
raw = open('/proc/%d/stat' % pid, 'rb').read()
except MISSING:
return None
end = raw.rfind(b')')
if end < 0:
raise RuntimeError('malformed proc stat')
fields = raw[end+2:].split()
if len(fields) < 20:
raise RuntimeError('short proc stat')
return dict(pid=pid, state=fields[0].decode('ascii'), ppid=int(fields[1]),
pgid=int(fields[2]), sid=int(fields[3]), start=int(fields[19]))
def process_table(errors):
table = {}
for name in os.listdir('/proc'):
if not name.isdigit():
continue
pid = int(name)
try:
item = proc_stat(pid)
if item is not None:
table[pid] = item
except MISSING:
continue
except Exception as exc:
errors.append('proc:%d:%s:%s' % (pid, type(exc).__name__, exc))
return table
def live(item):
return item is not None and item['state'] not in DEAD
def live_group(table, pgid):
return [item for item in table.values()
if pgid is not None and live(item) and item['pgid'] == pgid]
def owned_closure(table, self_token, tracked):
own = table.get(self_token[0])
if not live(own) or own['start'] != self_token[1]:
return {}
found = {self_token[0]: own}
changed = True
while changed:
changed = False
for pid, item in table.items():
if pid in found or not live(item):
continue
if item['ppid'] in found:
found[pid] = item
changed = True
for pid, start in list(tracked.items()):
item = table.get(pid)
if not live(item) or item['start'] != start:
continue
if pid not in found:
found[pid] = item
changed = True
while changed:
changed = False
for pid, item in table.items():
if pid in found or not live(item):
continue
if item['ppid'] in found:
found[pid] = item
changed = True
found.pop(self_token[0], None)
return found
def fresh_signal(pid, start, errors):
try:
item = proc_stat(pid)
if item is None or item['start'] != start or not live(item):
return
os.kill(pid, signal.SIGKILL)
except MISSING:
return
except Exception as exc:
errors.append('signal:%d:%s:%s' % (pid, type(exc).__name__, exc))
def reap_adopted(table, tokens, leader_pid, self_pid, adopted, errors):
for pid, item in table.items():
if pid != leader_pid and item['ppid'] == self_pid:
tokens[pid] = item['start']
for pid, start in list(tokens.items()):
if pid == leader_pid:
continue
item = table.get(pid)
if item is None or item['start'] != start or item['ppid'] != self_pid:
continue
try:
waited, status = os.waitpid(pid, os.WNOHANG)
if waited == pid:
adopted.append(dict(pid=pid, start=start, raw_status=status))
except ChildProcessError:
pass
except Exception as exc:
errors.append('reap:%d:%s:%s' % (pid, type(exc).__name__, exc))
def close_stream(selector, fileobj, errors):
try:
selector.unregister(fileobj)
except Exception as exc:
errors.append('unregister:%s:%s' % (type(exc).__name__, exc))
try:
fileobj.close()
except Exception as exc:
errors.append('closepipe:%s:%s' % (type(exc).__name__, exc))
def drain(selector, streams, errors, interval):
try:
ready = selector.select(interval)
except Exception as exc:
errors.append('select:%s:%s' % (type(exc).__name__, exc))
return
for key, _ in ready:
fileobj = key.fileobj
try:
chunk = os.read(fileobj.fileno(), 65536)
except BlockingIOError:
continue
except Exception as exc:
errors.append('readpipe:%s:%s' % (type(exc).__name__, exc))
close_stream(selector, fileobj, errors)
continue
if not chunk:
close_stream(selector, fileobj, errors)
else:
streams[key.data].extend(chunk)
def run(request):
started = time.monotonic()
errors = []
streams = {'stdout': bytearray(), 'stderr': bytearray()}
proc = None
selector = selectors.DefaultSelector()
adopted = []
tracked = {}
leader_start = None
leader_pgid = None
leader_rc = None
timed_out = False
self_item = proc_stat(os.getpid())
if self_item is None or not live(self_item):
raise RuntimeError('self proc absent')
self_token = (os.getpid(), self_item['start'])
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')
payload = base64.b64decode(request['stdin_b64'], validate=True)
try:
proc = subprocess.Popen(request['argv'], cwd=request['cwd'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, start_new_session=True,
env={'PATH':'/usr/bin:/bin','LANG':'C.UTF-8'})
leader = proc_stat(proc.pid)
if leader is not None:
leader_start = leader['start']
leader_pgid = leader['pgid']
tracked[proc.pid] = leader_start
if proc.stdin is not None:
try:
if payload:
proc.stdin.write(payload)
proc.stdin.flush()
finally:
proc.stdin.close()
for name, pipe in [('stdout', proc.stdout), ('stderr', proc.stderr)]:
os.set_blocking(pipe.fileno(), False)
selector.register(pipe, selectors.EVENT_READ, name)
end = started + request['deadline']
while True:
table = process_table(errors)
closure = owned_closure(table, self_token, tracked)
for pid, item in closure.items():
tracked[pid] = item['start']
leader_rc = proc.poll()
if leader_rc is not None:
proc.wait()
reap_adopted(table, tracked, proc.pid, self_token[0], adopted, errors)
active = [item for item in closure.values() if live(item)]
pg_active = live_group(table, leader_pgid)
if leader_rc is not None and not selector.get_map():
for item in active:
fresh_signal(item['pid'], item['start'], errors)
if not active and not pg_active:
break
now = time.monotonic()
if now >= end:
timed_out = True
break
drain(selector, streams, errors, min(0.05, end-now))
except Exception as exc:
errors.append('execute:%s:%s' % (type(exc).__name__, exc))
finally:
recovery_end = time.monotonic() + 3.0
while True:
try:
table = process_table(errors)
closure = owned_closure(table, self_token, tracked)
for pid, item in closure.items():
tracked[pid] = item['start']
if proc is not None:
leader_rc = proc.poll()
if leader_rc is not None:
proc.wait()
reap_adopted(table, tracked, proc.pid, self_token[0], adopted, errors)
for item in closure.values():
fresh_signal(item['pid'], item['start'], errors)
if selector.get_map():
drain(selector, streams, errors, 0.02)
current = process_table(errors)
remaining = owned_closure(current, self_token, tracked)
pg_live = live_group(current, leader_pgid)
if proc is not None and proc.poll() is not None:
leader_rc = proc.wait()
if not remaining and not pg_live and not selector.get_map() and (
proc is None or leader_rc is not None):
break
except Exception as exc:
errors.append('recovery:%s:%s' % (type(exc).__name__, exc))
if time.monotonic() >= recovery_end:
errors.append('recovery_deadline')
break
time.sleep(0.01)
for key in list(selector.get_map().values()):
close_stream(selector, key.fileobj, errors)
selector.close()
final_table = process_table(errors)
remaining = owned_closure(final_table, self_token, tracked)
pg_live = live_group(final_table, leader_pgid)
stdout = bytes(streams['stdout'])
stderr = bytes(streams['stderr'])
meta = dict(nonce=request['nonce'], stage=request['stage'],
request_sha256=request['_wire_sha256'],
argv=request['argv'], cwd=request['cwd'],
deadline=request['deadline'],
stdin_len=len(payload),
stdin_sha256=hashlib.sha256(payload).hexdigest(),
uid=os.getuid(), gid=os.getgid(), groups=os.getgroups(),
supervisor_pid=self_token[0], supervisor_start=self_token[1],
leader_pid=(proc.pid if proc is not None else None),
leader_start=leader_start, leader_pgid=leader_pgid,
leader_rc=leader_rc,
leader_reaped=(proc is not None and proc.returncode is not None),
timeout=timed_out,
controlled_rc=(124 if timed_out else
leader_rc if leader_rc is not None else 125),
elapsed=time.monotonic()-started,
descendants_absent=not bool(remaining),
pg_absent=not bool(pg_live), adopted=adopted,
seen=[dict(pid=pid,start=start) for pid,start in sorted(tracked.items())],
exceptions=errors,
stdout_len=len(stdout), stderr_len=len(stderr),
stdout_sha256=hashlib.sha256(stdout).hexdigest(),
stderr_sha256=hashlib.sha256(stderr).hexdigest())
return dict(meta=meta, stdout_b64=base64.b64encode(stdout).decode('ascii'),
stderr_b64=base64.b64encode(stderr).decode('ascii'))
if __name__ == '__main__':
try:
request_raw = sys.stdin.buffer.read()
request = json.loads(request_raw)
request['_wire_sha256'] = hashlib.sha256(request_raw).hexdigest()
result = run(request)
sys.stdout.write(json.dumps(result, separators=(',',':'))+'\n')
except BaseException as exc:
sys.stderr.write('supervisor:%s:%s\n' % (type(exc).__name__, exc))
sys.exit(125)
'''
def sha(data):
return hashlib.sha256(data).hexdigest()
def safe_candidate(root, relative):
"""Read each candidate path through no-follow directory descriptors."""
parts = relative.split("/")
if not parts or any(part in ("", ".", "..") for part in parts):
raise ValueError("unsafe candidate path")
directory = os.open(root, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
for part in parts[:-1]:
new_dir = os.open(part, os.O_RDONLY | os.O_DIRECTORY |
os.O_NOFOLLOW, dir_fd=directory)
os.close(directory)
directory = new_dir
descriptor = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW |
os.O_NONBLOCK, dir_fd=directory)
try:
info = os.fstat(descriptor)
if not stat.S_ISREG(info.st_mode) or info.st_size > 1048576:
raise ValueError("candidate file must be regular and <=1 MiB")
chunks = []
total = 0
while True:
chunk = os.read(descriptor, min(65536, 1048577-total))
if not chunk:
break
chunks.append(chunk)
total += len(chunk)
if total > 1048576:
raise ValueError("candidate file grew beyond 1 MiB")
if os.fstat(descriptor).st_size > 1048576:
raise ValueError("candidate file grew beyond 1 MiB")
return b"".join(chunks)
finally:
os.close(descriptor)
finally:
os.close(directory)
def scenarios():
cases = [
(4, 7, 17, 3, 8, 0, 0, 0),
(0, 9, 0, 1, 1, 0, 0, 0),
(1, 0, 0, 1, 1, 0, 0, 0),
(2, 0, 1, 1, 64, 0, 0, 0),
(4, 7, 17, 3, 8, 1, 0, 2),
(5, 43, 22, 7, 1, 3, 4, 2),
(5, 43, 22, 7, 64, 3, 4, 1),
(3, 17, 10, 2, 9, 0, 0, 1),
(3, 17, 10, 2, 9, 2, 100, 2),
(6, 123, 31, 1, 64, 0, 0, 0),
(64, 257, 7, 3, 5, 18, 2, 1),
(7, 255, 256, 64, 64, 4, 0, 2),
(8, 256, 63, 2, 2, 7, 30, 1),
(2, 9, 9, 3, 3, 0, 0, 2),
(2, 9, 9, 3, 3, 1, 9, 2),
(2, 4294967295, 256, 7, 64, 1, 100, 1),
]
rng = random.Random(26026)
for _ in range(15):
rounds = rng.randrange(1, 18)
seed = rng.randrange(0, 1000000)
max_length = rng.choice([0, 1, 2, 3, 17, 63, 127])
read_cap = rng.choice([1, 2, 3, 7, 64])
hint = rng.choice([1, 2, 8, 64])
error_round = rng.randrange(rounds)
length = (seed + 17 * error_round + error_round**2) % (max_length+1)
offset = rng.choice([0, length//2, length, length+1])
error = rng.choice([0, 1, 2])
cases.append((rounds, seed, max_length, read_cap, hint,
error_round, offset, error))
cases.extend([
(65, 1, 1, 1, 1, 0, 0, 0),
(1, 1, 1, 1, 0, 0, 0, 0),
])
cases = [case + (4294967295, 4294967295) for case in cases]
cases.extend([
(4, 7, 17, 3, 8, 0, 0, 0, 0, 4294967295),
(4, 7, 17, 3, 8, 0, 0, 0, 2, 4294967295),
(4, 7, 17, 3, 8, 0, 0, 0, 4294967295, 0),
(4, 7, 17, 3, 8, 1, 0, 2, 4294967295, 1),
])
return cases
def expected_one(case):
rounds, seed, maximum, _cap, hint, eround, eoffset, kind, open_fail, close_fail = case
fields = dict(returned=0, status=0, rounds_done=0, bytes=0,
even_bytes=0, high_bytes=0, rising_pairs=0,
checksum=0, digest_xor=0, longest=0, empty_rounds=0,
range_sum=0, open_attempts=0, open_successes=0,
close_attempts=0, close_successes=0, delivered_bytes=0,
eof_reads=0, live=0, peak=0, interrupts=0,
io_errors=0, invalid_calls=0)
if rounds > 64 or not 1 <= hint <= 64:
fields["returned"] = fields["status"] = 1
return fields
for round_index in range(rounds):
fields["open_attempts"] += 1
if round_index == open_fail:
fields["returned"] = fields["status"] = 2
break
length = (seed + 17*round_index + round_index*round_index) % (maximum+1)
data = [((seed ^ (round_index*37)) ^
(offset*19 + offset*round_index)) & 255
for offset in range(length)]
hit = (kind != 0 and round_index == eround and eoffset <= length)
consumed = min(eoffset, length) if hit and kind == 2 else length
fields["open_successes"] += 1
fields["close_attempts"] += 1
fields["delivered_bytes"] += consumed
fields["peak"] = 1
if hit and kind == 1:
fields["interrupts"] += 1
if hit and kind == 2:
fields["io_errors"] += 1
else:
fields["eof_reads"] += 1
if round_index == close_fail:
fields["returned"] = fields["status"] = 4
break
fields["close_successes"] += 1
if hit and kind == 2:
fields["returned"] = fields["status"] = 3
break
digest = 1469598103934665603
for value in data:
digest = ((digest ^ value) * 1099511628211) & MASK
shifted = round_index & 63
rotated = (digest if shifted == 0 else
((digest << shifted) | (digest >> (64-shifted))) & MASK)
fields["rounds_done"] += 1
fields["bytes"] += len(data)
fields["even_bytes"] += sum(value % 2 == 0 for value in data)
fields["high_bytes"] += sum(value >= 128 for value in data)
fields["rising_pairs"] += sum(b > a for a, b in zip(data, data[1:]))
fields["checksum"] = (fields["checksum"] +
(round_index+1)*digest) & MASK
fields["digest_xor"] ^= rotated
fields["longest"] = max(fields["longest"], len(data))
fields["empty_rounds"] += (len(data) == 0)
fields["range_sum"] += abs(data[0]-data[-1]) if data else 0
return fields
def exact_int(value):
return type(value) is int
def valid_sha(value):
return (type(value) is str and len(value) == 64 and
all(char in "0123456789abcdef" for char in value))
def stage_request(stage, argv, cwd, deadline, stdin):
nonce = hashlib.sha256(os.urandom(32)).hexdigest()
request = dict(nonce=nonce, stage=stage, argv=list(argv), cwd=str(cwd),
deadline=deadline,
stdin_b64=base64.b64encode(stdin).decode("ascii"))
raw = json.dumps(request, sort_keys=True, separators=(",", ":")).encode()
return request, raw
def run_stage(stage, argv, cwd, deadline, stdin=b""):
request, raw = stage_request(stage, argv, cwd, deadline, stdin)
record = dict(stage=stage, nonce=request["nonce"], argv=list(argv),
cwd=str(cwd), deadline=deadline, request_sha256=sha(raw),
stdin_len=len(stdin), stdin_sha256=sha(stdin))
try:
wrapper = subprocess.run([sys.executable, "-I", "-c", SUPERVISOR],
input=raw, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd="/tmp",
timeout=deadline+5.0, check=False)
record.update(wrapper_rc=wrapper.returncode,
wire_len=len(wrapper.stdout),
wire_sha256=sha(wrapper.stdout),
wrapper_stderr_len=len(wrapper.stderr),
wrapper_stderr_sha256=sha(wrapper.stderr))
except subprocess.TimeoutExpired as exc:
record.update(wrapper_timeout=True,
wrapper_stdout_len=len(exc.stdout or b""),
wrapper_stdout_sha256=sha(exc.stdout or b""),
wrapper_stderr_len=len(exc.stderr or b""),
wrapper_stderr_sha256=sha(exc.stderr or b""))
return record, None, None, None
except Exception as exc:
record["wrapper_error"] = type(exc).__name__ + ":" + str(exc)
return record, None, None, None
if wrapper.returncode != 0:
return record, None, None, None
try:
wire = json.loads(wrapper.stdout)
if type(wire) is not dict or set(wire) != {
"meta", "stdout_b64", "stderr_b64"}:
raise ValueError("wire shape")
if type(wire["meta"]) is not dict:
raise ValueError("meta shape")
stdout = base64.b64decode(wire["stdout_b64"], validate=True)
stderr = base64.b64decode(wire["stderr_b64"], validate=True)
record["meta"] = wire["meta"]
return record, wire["meta"], stdout, stderr
except Exception as exc:
record["wire_error"] = type(exc).__name__ + ":" + str(exc)
return record, None, None, None
def validate_stage(record, meta, stdout, stderr):
errors = []
if meta is None or stdout is None or stderr is None:
return ["missing_stage_wire"]
required = ("nonce", "stage", "request_sha256", "argv", "cwd",
"deadline", "stdin_len", "stdin_sha256", "uid", "gid",
"groups", "supervisor_pid", "supervisor_start", "leader_pid",
"leader_start", "leader_pgid", "leader_rc", "leader_reaped",
"timeout", "controlled_rc", "elapsed", "descendants_absent",
"pg_absent", "adopted", "seen", "exceptions", "stdout_len",
"stderr_len", "stdout_sha256", "stderr_sha256")
if any(key not in meta for key in required):
return ["missing_meta_fields:" +
",".join(key for key in required if key not in meta)]
for field in ("nonce", "stage", "request_sha256", "argv", "cwd",
"deadline", "stdin_len", "stdin_sha256"):
if meta[field] != record[field] or type(meta[field]) is not type(record[field]):
errors.append("binding:" + field)
uid = 65534 if os.environ.get("ROLLOUT_GRADE_ISOLATION") == "uid65534" else os.getuid()
gid = 65534 if os.environ.get("ROLLOUT_GRADE_ISOLATION") == "uid65534" else os.getgid()
groups = [] if os.environ.get("ROLLOUT_GRADE_ISOLATION") == "uid65534" else os.getgroups()
if not exact_int(meta["uid"]) or meta["uid"] != uid:
errors.append("uid")
if not exact_int(meta["gid"]) or meta["gid"] != gid:
errors.append("gid")
if type(meta["groups"]) is not list or any(
not exact_int(item) for item in meta["groups"]) or meta["groups"] != groups:
errors.append("groups")
for field in ("supervisor_pid", "supervisor_start", "leader_pid",
"leader_start", "leader_pgid"):
if not exact_int(meta[field]) or meta[field] <= 0:
errors.append(field)
if (exact_int(meta["leader_pid"]) and exact_int(meta["supervisor_pid"]) and
meta["leader_pid"] == meta["supervisor_pid"]):
errors.append("pid_collision")
if type(meta["leader_reaped"]) is not bool or not meta["leader_reaped"]:
errors.append("leader_reaped")
if type(meta["timeout"]) is not bool:
errors.append("timeout_type")
if not exact_int(meta["leader_rc"]):
errors.append("leader_rc")
if not exact_int(meta["controlled_rc"]):
errors.append("controlled_rc")
elif type(meta["timeout"]) is bool and exact_int(meta["leader_rc"]):
if meta["controlled_rc"] != (124 if meta["timeout"] else meta["leader_rc"]):
errors.append("controlled_consistency")
elapsed = meta["elapsed"]
if (type(elapsed) not in (int, float) or not math.isfinite(elapsed) or
elapsed < 0 or elapsed > record["deadline"] + 3.5):
errors.append("elapsed")
for field in ("descendants_absent", "pg_absent"):
if type(meta[field]) is not bool or not meta[field]:
errors.append(field)
if type(meta["exceptions"]) is not list or meta["exceptions"]:
errors.append("exceptions")
for field in ("adopted", "seen"):
if type(meta[field]) is not list:
errors.append(field)
continue
for token in meta[field]:
if type(token) is not dict or not exact_int(token.get("pid")) or (
not exact_int(token.get("start"))) or token["pid"] <= 0 or token["start"] <= 0:
errors.append(field + "_token")
break
if field == "adopted" and not exact_int(token.get("raw_status")):
errors.append("adopted_status")
break
for field, data in (("stdout", stdout), ("stderr", stderr)):
length = meta[field + "_len"]
digest = meta[field + "_sha256"]
if not exact_int(length) or length < 0 or length != len(data):
errors.append(field + "_len")
if not valid_sha(digest) or digest != sha(data):
errors.append(field + "_sha256")
if not valid_sha(meta["stdin_sha256"]) or not valid_sha(meta["request_sha256"]):
errors.append("request_hash")
return errors
def compare_output(raw, nonce, cases):
try:
text = raw.decode("utf-8")
lines = text.splitlines()
if len(lines) != len(cases)+1:
return "line_count"
header = json.loads(lines[0])
if type(header) is not dict or header != {"nonce": nonce}:
return "nonce"
for number, (line, case) in enumerate(zip(lines[1:], cases), 1):
row = json.loads(line)
expected = expected_one(case)
if type(row) is not dict or set(row) != set(expected) | {"read_calls"}:
return "row_%d_shape" % number
for key, wanted in expected.items():
if not exact_int(row[key]) or row[key] != wanted:
return "row_%d_%s" % (number, key)
count = row["read_calls"]
if not exact_int(count) or count < expected["open_successes"]:
return "row_%d_read_calls" % number
return None
except (UnicodeError, ValueError, TypeError, KeyError) as exc:
return "output_parse:" + type(exc).__name__
def prepare(candidate, root):
all_files = {}
for name in CANDIDATES:
all_files[name] = safe_candidate(candidate, name)
support = json.loads(Path(__file__).with_name("support.json").read_bytes())
if type(support) is not dict or set(support) != set(SOURCE):
raise ValueError("support files mismatch")
for name in SOURCE:
all_files[name] = base64.b64decode(support[name], validate=True)
for name, data in all_files.items():
parts = name.split("/")
place = Path(root).joinpath(*parts)
place.parent.mkdir(parents=True, exist_ok=True)
place.write_bytes(data)
os.chown(place, 65534, 65534) if os.geteuid() == 0 else None
for directory in (Path(root)/"include", Path(root)/"src"):
os.chown(directory, 65534, 65534) if os.geteuid() == 0 else None
return all_files
def emit_evidence(stages, failures):
compact = []
for stage in stages:
meta = stage.get("meta")
compact.append(dict(stage=stage["stage"], nonce=stage["nonce"],
argv=stage["argv"], cwd=stage["cwd"],
deadline=stage["deadline"],
request_sha256=stage["request_sha256"],
stdin_len=stage["stdin_len"],
stdin_sha256=stage["stdin_sha256"],
wrapper_rc=stage.get("wrapper_rc"),
wire_len=stage.get("wire_len"),
wire_sha256=stage.get("wire_sha256"),
wrapper_error=stage.get("wrapper_error"),
wire_error=stage.get("wire_error"),
meta=meta))
raw = json.dumps(dict(stages=compact, failures=failures),
separators=(",", ":"), sort_keys=True).encode()
token = base64.b85encode(zlib.compress(raw, 9)).decode("ascii")
sys.stderr.write("EVIDENCE_Z85:" + token + "\n")
sys.stderr.flush()
def main(candidate):
failures = []
stage_records = []
root = None
cases = scenarios()
try:
root = tempfile.mkdtemp(prefix="rr-grade-")
prepare(candidate, root)
compiler = ["gcc", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", "-pedantic", "-Iinclude", "src/batch_observer.c",
"src/sim_port.c", "src/rr.c", "src/rr_math.c",
"src/rr_plan.c", "-o", "observer"]
record, meta, stdout, stderr = run_stage(
"compiler", compiler, root, COMPILER_DEADLINE)
stage_records.append(record)
failures.extend("compiler_" + error for error in
validate_stage(record, meta, stdout, stderr))
if meta is None or meta.get("controlled_rc") != 0 or meta.get("timeout"):
failures.append("compiler_execution")
else:
nonce = hashlib.sha256(os.urandom(32)).hexdigest()
lines = [nonce + " " + str(len(cases))]
lines.extend(" ".join(str(item) for item in case) for case in cases)
payload = ("\n".join(lines) + "\n").encode()
record, meta, stdout, stderr = run_stage(
"candidate", [str(Path(root)/"observer")], root,
EXECUTE_DEADLINE, payload)
stage_records.append(record)
failures.extend("candidate_" + error for error in
validate_stage(record, meta, stdout, stderr))
if meta is None or meta.get("controlled_rc") != 0 or meta.get("timeout"):
failures.append("candidate_execution")
elif stdout is not None:
difference = compare_output(stdout, nonce, cases)
if difference:
failures.append("behavior:" + difference)
except Exception as exc:
failures.append("preparation_or_business:" + type(exc).__name__ + ":" + str(exc))
finally:
if root is not None:
try:
record, meta, stdout, stderr = run_stage(
"cleanup", ["/bin/rm", "-rf", "--", root], "/tmp",
CLEANUP_DEADLINE)
stage_records.append(record)
failures.extend("cleanup_" + error for error in
validate_stage(record, meta, stdout, stderr))
if meta is None or meta.get("controlled_rc") != 0 or meta.get("timeout"):
failures.append("cleanup_execution")
except Exception as exc:
failures.append("cleanup_parent:" + type(exc).__name__ + ":" + str(exc))
try:
absence_code = ("import os,sys\n"
"try: os.lstat(sys.argv[1])\n"
"except FileNotFoundError: print(0)\n"
"except OSError: print(2); sys.exit(2)\n"
"else: print(1)\n")
record, meta, stdout, stderr = run_stage(
"absence", [sys.executable, "-I", "-c", absence_code, root],
"/tmp", ABSENCE_DEADLINE)
stage_records.append(record)
failures.extend("absence_" + error for error in
validate_stage(record, meta, stdout, stderr))
if (meta is None or meta.get("controlled_rc") != 0 or
meta.get("timeout") or stdout != b"0\n"):
failures.append("absence_execution")
except Exception as exc:
failures.append("absence_parent:" + type(exc).__name__ + ":" + str(exc))
emit_evidence(stage_records, failures)
if failures:
print("FAIL " + failures[0])
return 1
print("PASS")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1]))