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>
407 lines
30 KiB
Python
407 lines
30 KiB
Python
"""Independent lifetime oracle and candidate-identity deadline supervisor."""
|
||
import base64
|
||
import hashlib
|
||
import json
|
||
import math
|
||
import os
|
||
from pathlib import Path
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
import time
|
||
import uuid
|
||
import zlib
|
||
|
||
SUPERVISOR = "import base64\nimport ctypes\nimport errno\nimport hashlib\nimport json\nimport os\nimport selectors\nimport signal\nimport subprocess\nimport sys\nimport time\n\n\ndef read_stat(pid, errors):\n try:\n with open('/proc/%d/stat' % pid, 'rb') as stream:\n raw = stream.read()\n fields = raw[raw.rindex(b')') + 2:].split()\n return (int(fields[1]), int(fields[19]), fields[0].decode('ascii'))\n except (FileNotFoundError, ProcessLookupError):\n return None\n except Exception as error:\n errors.append(['stat', pid, type(error).__name__, repr(error)])\n return None\n\n\ndef proc_table(errors):\n table = {}\n for entry in os.listdir('/proc'):\n if entry.isdigit():\n pid = int(entry)\n row = read_stat(pid, errors)\n if row is not None:\n table[pid] = row\n return table\n\n\ndef descendants(table, supervisor, tracked):\n roots = {supervisor}\n for pid, start in tracked:\n row = table.get(pid)\n if row is not None and row[1] == start and row[2] not in ('Z', 'X'):\n roots.add(pid)\n result = set()\n while True:\n additions = {pid for pid, row in table.items()\n if pid != supervisor and row[0] in roots and pid not in result}\n if not additions:\n break\n result.update(additions)\n roots.update(additions)\n for pid, start in tracked:\n row = table.get(pid)\n if row is not None and row[1] == start and row[2] not in ('Z', 'X'):\n result.add(pid)\n return {(pid, table[pid][1]) for pid in result}\n\n\ndef signal_fresh(token, errors, signals):\n pid, start = token\n fresh = read_stat(pid, errors)\n if fresh is None or fresh[1] != start or fresh[2] in ('Z', 'X'):\n return\n try:\n os.kill(pid, signal.SIGKILL)\n signals.append([pid, start])\n except (FileNotFoundError, ProcessLookupError):\n pass\n except Exception as error:\n errors.append(['signal', pid, start, type(error).__name__, repr(error)])\n\n\ndef run(config):\n started = time.monotonic()\n errors, signals, adopted = [], [], []\n supervisor = os.getpid()\n leader = None\n leader_token = None\n leader_rc = None\n leader_reaped = False\n timed_out = False\n output = bytearray()\n diagnostic = bytearray()\n tracked = set()\n selector = selectors.DefaultSelector()\n input_bytes = base64.b64decode(config['input'])\n input_position = 0\n remaining = set()\n deadline = config['deadline']\n stop = started + deadline\n recovery_stop = None\n try:\n libc = ctypes.CDLL(None, use_errno=True)\n if libc.prctl(36, 1, 0, 0, 0) != 0:\n raise OSError(ctypes.get_errno(), 'PR_SET_CHILD_SUBREAPER')\n leader = subprocess.Popen(config['argv'], cwd=config['cwd'], stdin=subprocess.PIPE,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)\n row = read_stat(leader.pid, errors)\n if row is None:\n raise RuntimeError('leader identity unavailable')\n leader_token = (leader.pid, row[1])\n tracked.add(leader_token)\n for stream, role in ((leader.stdout, 'out'), (leader.stderr, 'err'), (leader.stdin, 'in')):\n os.set_blocking(stream.fileno(), False)\n selector.register(stream, selectors.EVENT_WRITE if role == 'in' else selectors.EVENT_READ, role)\n while True:\n # Popen owns the leader's exit status, before any adopted-child wait.\n leader_rc = leader.poll()\n leader_reaped = leader_rc is not None\n table = proc_table(errors)\n remaining = descendants(table, supervisor, tracked)\n tracked.update(remaining)\n for pid, start in sorted(remaining):\n if pid == leader.pid:\n continue\n row = table.get(pid)\n if row is not None and row[1] == start and row[0] == supervisor:\n try:\n waited, status = os.waitpid(pid, os.WNOHANG)\n if waited:\n adopted.append([pid, start, status])\n except ChildProcessError:\n pass\n except Exception as error:\n errors.append(['reap', pid, start, type(error).__name__, repr(error)])\n now = time.monotonic()\n if not leader_reaped and now >= stop:\n timed_out = True\n recovering = timed_out or leader_reaped or bool(errors)\n if recovering:\n if recovery_stop is None:\n recovery_stop = now + 3.0\n for token in sorted(remaining):\n signal_fresh(token, errors, signals)\n if leader_reaped and not remaining and not selector.get_map():\n break\n if recovery_stop is not None and now >= recovery_stop:\n errors.append(['recovery_deadline', sorted(remaining)])\n break\n for key, events in selector.select(0.01):\n stream, role = key.fileobj, key.data\n try:\n if role == 'in':\n if input_position == len(input_bytes):\n selector.unregister(stream)\n stream.close()\n else:\n written = os.write(stream.fileno(), input_bytes[input_position:input_position+65536])\n input_position += written\n else:\n chunk = os.read(stream.fileno(), 65536)\n if chunk:\n (output if role == 'out' else diagnostic).extend(chunk)\n else:\n selector.unregister(stream)\n stream.close()\n except BlockingIOError:\n pass\n except BrokenPipeError:\n selector.unregister(stream)\n stream.close()\n except Exception as error:\n errors.append(['pipe', role, type(error).__name__, repr(error)])\n selector.unregister(stream)\n stream.close()\n except Exception as error:\n errors.append(['execution', type(error).__name__, repr(error)])\n finally:\n # Exceptions still receive bounded discovery, signal, poll/wait, adopted reap.\n recovery_limit = time.monotonic() + 3.0\n while leader is not None:\n try:\n leader_rc = leader.poll()\n leader_reaped = leader_rc is not None\n table = proc_table(errors)\n remaining = descendants(table, supervisor, tracked)\n tracked.update(remaining)\n if leader_reaped and not remaining:\n break\n for token in sorted(remaining):\n signal_fresh(token, errors, signals)\n for pid, start in sorted(remaining):\n if pid == leader.pid:\n continue\n row = table.get(pid)\n if row is not None and row[1] == start and row[0] == supervisor:\n try:\n waited, status = os.waitpid(pid, os.WNOHANG)\n if waited:\n adopted.append([pid, start, status])\n except ChildProcessError:\n pass\n except Exception as error:\n errors.append(['finally_reap', pid, start, type(error).__name__, repr(error)])\n except Exception as error:\n errors.append(['finally_iteration', type(error).__name__, repr(error)])\n if time.monotonic() >= recovery_limit:\n errors.append(['finally_deadline', sorted(remaining)])\n break\n time.sleep(0.01)\n for key in list(selector.get_map().values()):\n try:\n key.fileobj.close()\n except Exception as error:\n errors.append(['close', type(error).__name__, repr(error)])\n selector.close()\n elapsed = time.monotonic() - started\n stdout, stderr = bytes(output), bytes(diagnostic)\n meta = dict(phase=config['phase'], nonce=config['nonce'], argv=config['argv'], cwd=config['cwd'],\n uid=os.getuid(), gid=os.getgid(), groups=os.getgroups(), supervisor_pid=supervisor,\n leader_pid=leader.pid if leader else None, leader_start=leader_token[1] if leader_token else None,\n leader_rc=leader_rc, leader_reaped=leader_reaped, deadline=deadline, elapsed=elapsed,\n timed_out=timed_out, controlled_rc=124 if timed_out else leader_rc,\n remaining=sorted(remaining), exceptions=errors, signals=signals, adopted_reaped=adopted,\n stdout_len=len(stdout), stdout_sha256=hashlib.sha256(stdout).hexdigest(),\n stderr_len=len(stderr), stderr_sha256=hashlib.sha256(stderr).hexdigest())\n print(json.dumps(dict(meta=meta, stdout=base64.b64encode(stdout).decode('ascii'),\n stderr=base64.b64encode(stderr).decode('ascii')), separators=(',', ':')))\n\n\nif __name__ == '__main__':\n run(json.loads(sys.argv[1]))\n"
|
||
OBSERVER = '#include "cache.hpp"\n#include <iostream>\nint main() {\n tile::Service service;\n std::string line;\n while (std::getline(std::cin, line)) {\n const auto observation = service.execute(line);\n std::cout << \'[\';\n for (std::size_t index = 0; index < observation.size(); ++index) {\n if (index) std::cout << \',\';\n std::cout << observation[index];\n }\n std::cout << "]\\n";\n }\n return 0;\n}\n'
|
||
|
||
"""Independent declarative object/owner oracle; no candidate import or gold execution."""
|
||
import copy
|
||
import re
|
||
import random
|
||
|
||
|
||
class Oracle:
|
||
def __init__(self):
|
||
self.slots = [None] * 32
|
||
self.entries = {}
|
||
self.objects = {}
|
||
self.created = 0
|
||
self.deaths = []
|
||
|
||
def collect(self):
|
||
alive = set(self.slots) - {None}
|
||
for serial in list(self.objects):
|
||
if serial not in alive:
|
||
del self.objects[serial]
|
||
self.deaths.append(serial)
|
||
|
||
def put(self, slot, serial):
|
||
self.slots[slot] = serial
|
||
self.collect()
|
||
|
||
@staticmethod
|
||
def pattern(index, seed):
|
||
return (seed + 17*index + index*index % 251) % 256
|
||
|
||
def checksum(self, serial):
|
||
return sum((index+1)*value for index, value in enumerate(self.objects[serial])) % 1000000007
|
||
|
||
def execute(self, raw):
|
||
tokens = [x for x in re.split('[ \t\r]+', raw) if x]
|
||
if not tokens:
|
||
return [1]
|
||
command = tokens[0]
|
||
schemas = {'ACQUIRE': [31, 'name', 'name', 4096, 255], 'LOOKUP': [31, 'name', 'name'],
|
||
'COPY': [31, 31], 'MOVE': [31, 31], 'SWAP': [31, 31], 'COMPARE': [31, 31],
|
||
'DROP': [31], 'VIEW': [31], 'OWNERS': [31], 'DROP_ALL': [],
|
||
'WRITE': [31, 4096, 255], 'FILL': [31, 4096, 4096, 255],
|
||
'RESIZE': [31, 4096, 255], 'READ': [31, 4096, 4096],
|
||
'COPY_RANGE': [31, 4096, 31, 4096, 4096], 'ERASE': ['name', 'name'],
|
||
'RETIRE': ['name'], 'CLEAR': [], 'SWEEP': [], 'CACHE': [], 'STATS': [], 'DEATHS': []}
|
||
schema = schemas.get(command)
|
||
if schema is None or len(schema) != len(tokens)-1:
|
||
return [1]
|
||
values = []
|
||
for token, domain in zip(tokens[1:], schema):
|
||
if domain == 'name':
|
||
if not re.fullmatch('[A-Za-z0-9_.-]{1,32}', token):
|
||
return [1]
|
||
values.append(token)
|
||
else:
|
||
if not re.fullmatch('[+-]?[0-9]+', token):
|
||
return [1]
|
||
negative = token.startswith('-')
|
||
significant = token.lstrip('+-').lstrip('0') or '0'
|
||
if len(significant) > 4:
|
||
return [1]
|
||
number = int(significant)
|
||
if number > domain or (negative and number):
|
||
return [1]
|
||
values.append(number)
|
||
if command in ('ACQUIRE', 'LOOKUP'):
|
||
slot, tenant, name = values[:3]
|
||
key = tenant, name
|
||
serial = self.entries.get(key)
|
||
if serial not in self.objects:
|
||
if command == 'LOOKUP':
|
||
return [2]
|
||
self.created += 1
|
||
serial = self.created
|
||
length, seed = values[3:]
|
||
self.objects[serial] = [self.pattern(i, seed) for i in range(length)]
|
||
self.entries[key] = serial
|
||
self.put(slot, serial)
|
||
return [0, serial, self.checksum(serial)]
|
||
if command in ('COPY', 'MOVE'):
|
||
source, destination = values
|
||
serial = self.slots[source]
|
||
if serial is None:
|
||
return [2]
|
||
if command == 'MOVE' and source != destination:
|
||
# Transfer keeps serial alive throughout the atomic owner change.
|
||
self.slots[source] = None
|
||
self.put(destination, serial)
|
||
return [0, serial]
|
||
if command == 'SWAP':
|
||
left, right = values
|
||
self.slots[left], self.slots[right] = self.slots[right], self.slots[left]
|
||
return [0, self.slots[left] or 0, self.slots[right] or 0]
|
||
if command == 'DROP':
|
||
self.put(values[0], None)
|
||
return [0]
|
||
if command == 'DROP_ALL':
|
||
occupied = sum(x is not None for x in self.slots)
|
||
for slot in range(32):
|
||
self.put(slot, None)
|
||
return [0, occupied]
|
||
if command in ('VIEW', 'OWNERS', 'WRITE', 'FILL', 'RESIZE', 'READ'):
|
||
serial = self.slots[values[0]]
|
||
if serial is None:
|
||
return [2]
|
||
data = self.objects[serial]
|
||
if command == 'VIEW':
|
||
return [0, serial, len(data), self.checksum(serial)]
|
||
if command == 'OWNERS':
|
||
return [0, serial, self.slots.count(serial)]
|
||
if command == 'WRITE':
|
||
offset, value = values[1:]
|
||
if offset >= len(data):
|
||
return [1]
|
||
data[offset] = value
|
||
elif command in ('FILL', 'READ'):
|
||
offset, count = values[1:3]
|
||
if offset+count > len(data):
|
||
return [1]
|
||
if command == 'READ':
|
||
return [0, count] + data[offset:offset+count]
|
||
data[offset:offset+count] = [values[3]] * count
|
||
else:
|
||
length, seed = values[1:]
|
||
old_length = len(data)
|
||
del data[length:]
|
||
data.extend(self.pattern(i, seed) for i in range(old_length, length))
|
||
return [0, self.checksum(serial)]
|
||
if command == 'COPY_RANGE':
|
||
source, source_offset, destination, destination_offset, count = values
|
||
first, second = self.slots[source], self.slots[destination]
|
||
if first is None or second is None:
|
||
return [2]
|
||
a, b = self.objects[first], self.objects[second]
|
||
if source_offset+count > len(a) or destination_offset+count > len(b):
|
||
return [1]
|
||
b[destination_offset:destination_offset+count] = a[source_offset:source_offset+count]
|
||
return [0, self.checksum(second)]
|
||
if command == 'COMPARE':
|
||
a, b = [self.slots[i] for i in values]
|
||
if a is None or b is None:
|
||
return [2]
|
||
return [0, int(a == b), int(self.objects[a] == self.objects[b])]
|
||
if command == 'ERASE':
|
||
key = tuple(values)
|
||
present = key in self.entries
|
||
self.entries.pop(key, None)
|
||
return [0, int(present)]
|
||
if command in ('CLEAR', 'RETIRE', 'SWEEP'):
|
||
removed = [key for key, serial in self.entries.items() if command == 'CLEAR' or
|
||
command == 'RETIRE' and key[0] == values[0] or
|
||
command == 'SWEEP' and serial not in self.objects]
|
||
for key in removed:
|
||
del self.entries[key]
|
||
return [0, len(removed)]
|
||
if command == 'CACHE':
|
||
live = sum(serial in self.objects for serial in self.entries.values())
|
||
return [0, len(self.entries), live, len(self.entries)-live]
|
||
if command == 'STATS':
|
||
return [0, self.created, len(self.deaths), len(self.objects),
|
||
sum(len(x) for x in self.objects.values()),
|
||
sum(x is not None for x in self.slots), len(self.entries)]
|
||
if command == 'DEATHS':
|
||
return [0, len(self.deaths)] + self.deaths
|
||
raise AssertionError(command)
|
||
|
||
|
||
def make_matrix():
|
||
commands, expected, labels = [], [], []
|
||
oracle = Oracle()
|
||
def emit(command, label):
|
||
commands.append(command)
|
||
expected.append(copy.deepcopy(oracle.execute(command)))
|
||
labels.append(label)
|
||
def observe(label):
|
||
for command in ('STATS', 'CACHE', 'DEATHS'):
|
||
emit(command, label)
|
||
for line in ['ACQUIRE 0 demo tile 32 7', 'COPY 0 1', 'DROP 0', 'VIEW 1', 'DROP 1']:
|
||
emit(line, 'C04-default')
|
||
observe('C04-default')
|
||
emit('SWEEP', 'C07-default')
|
||
observe('C07-default')
|
||
for line in ['ACQUIRE 0 north shared 8 3', 'ACQUIRE 1 south shared 12 9', 'COMPARE 0 1',
|
||
'LOOKUP 2 north shared', 'LOOKUP 3 south shared', 'OWNERS 0', 'OWNERS 1',
|
||
'WRITE 2 1 255', 'VIEW 0', 'VIEW 1', 'RETIRE north', 'LOOKUP 4 south shared',
|
||
'LOOKUP 5 north shared', 'ACQUIRE 5 north shared 0 0', 'COMPARE 0 5', 'DROP_ALL', 'SWEEP']:
|
||
emit(line, 'C03-namespace')
|
||
observe('C03-namespace')
|
||
for line in ['ACQUIRE 0 a k 8 4', 'COPY 0 1', 'DROP 0', 'LOOKUP 2 a k', 'OWNERS 1',
|
||
'ERASE a k', 'ACQUIRE 3 a k 8 4', 'COMPARE 1 3', 'CLEAR', 'VIEW 1', 'VIEW 3',
|
||
'COPY 1 3', 'MOVE 3 1', 'MOVE 1 1', 'COPY 1 1', 'SWAP 1 31', 'VIEW 31',
|
||
'DROP_ALL', 'ACQUIRE 0 a k 0 9', 'DROP 0', 'ACQUIRE 0 a k 4 1', 'DROP_ALL', 'SWEEP']:
|
||
emit(line, 'C04-identity')
|
||
observe('C04-identity')
|
||
for line in ['ACQUIRE 0 bytes one 16 3', 'COPY 0 1', 'READ 0 0 16', 'COPY_RANGE 0 0 1 3 10',
|
||
'READ 1 0 16', 'COPY_RANGE 1 3 0 0 10', 'READ 0 0 16', 'FILL 1 16 0 255',
|
||
'RESIZE 0 32 17', 'READ 1 14 18', 'RESIZE 1 4 7', 'READ 0 0 4', 'RESIZE 1 0 0',
|
||
'FILL 0 0 0 0', 'READ 0 0 0', 'WRITE 0 0 0', 'RESIZE 1 4096 255', 'VIEW 0',
|
||
'WRITE 0 4095 7', 'FILL 0 4090 6 8', 'READ 1 4090 6', 'RESIZE 0 1 1']:
|
||
emit(line, 'C06-payload')
|
||
observe('C06-payload')
|
||
invalid=['', ' \t\r', 'wat', 'STATS x', 'DROP -1', 'DROP 32', 'DROP 1_0', 'DROP +',
|
||
'ACQUIRE 0 bad:name k 4 1', 'ACQUIRE 0 '+('a'*33)+' k 4 1', 'ACQUIRE 0 a k 4097 0',
|
||
'ACQUIRE 0 a k 4 256', 'FILL 0 0 2 256', 'WRITE 0 4096 0', 'FILL 0 1 1 0',
|
||
'COPY_RANGE 0 0 1 1 1', 'READ 0 2 0', 'DROP\v0', 'DROP\f0', 'DROP 0',
|
||
'VIEW 0 extra', 'ERASE a', 'RETIRE a b', 'SWAP 0 32', 'RESIZE 0 -1 0']
|
||
for line in invalid:
|
||
emit(line, 'C02-error')
|
||
emit('VIEW 0', 'C02-atomic')
|
||
for sign in ('', '+', '-'):
|
||
emit('VIEW '+sign+'0'*5000, 'C02-leading-zero')
|
||
for line in ['\tVIEW\t+0000\r ', 'DROP -000', 'LOOKUP 0 absent key', 'COPY 0 0', 'MOVE 0 0', 'COMPARE 0 1']:
|
||
emit(line, 'C02-legal')
|
||
observe('C02-recovery')
|
||
rng = random.Random(472031)
|
||
names = ['k', 'key', 'same', 'p.q', 'A-_1']
|
||
tenants = ['east', 'west', 'North', 'a', 'a-b']
|
||
for index in range(760):
|
||
slot, other = rng.randrange(32), rng.randrange(32)
|
||
tenant, name = rng.choice(tenants), rng.choice(names)
|
||
option = rng.randrange(22)
|
||
size, seed = rng.choice([0,1,3,16,31,64,127]), rng.randrange(256)
|
||
choices = [f'ACQUIRE {slot} {tenant} {name} {size} {seed}',
|
||
f'LOOKUP {slot} {tenant} {name}', f'COPY {slot} {other}', f'MOVE {slot} {other}',
|
||
f'DROP {slot}', f'VIEW {slot}', f'OWNERS {slot}', f'SWAP {slot} {other}',
|
||
f'COMPARE {slot} {other}', f'WRITE {slot} {rng.randrange(130)} {seed}',
|
||
f'FILL {slot} {rng.randrange(66)} {rng.randrange(66)} {seed}',
|
||
f'RESIZE {slot} {size} {seed}', f'READ {slot} 0 {rng.randrange(8)}',
|
||
f'COPY_RANGE {slot} 0 {other} 0 {rng.randrange(8)}', f'ERASE {tenant} {name}',
|
||
f'RETIRE {tenant}', 'SWEEP', 'CACHE', 'STATS', 'DEATHS', 'CLEAR', 'DROP_ALL']
|
||
emit(choices[option], 'C09-sequence')
|
||
if index % 7 == 0:
|
||
observe('C08-reconcile')
|
||
for slot in range(32):
|
||
emit(f'ACQUIRE {slot} full n{slot} 4096 {slot}', 'C08-full')
|
||
observe('C08-full')
|
||
emit('DROP_ALL','C08-drain')
|
||
observe('C08-drain')
|
||
emit('SWEEP','C08-drain')
|
||
observe('C08-drain')
|
||
return ('\n'.join(commands)+'\n').encode('utf-8'), expected, labels
|
||
|
||
|
||
CANDIDATES = ['cache.hpp', 'payload.cpp', 'registry.cpp', 'commands.cpp']
|
||
DEADLINES = {'compiler': 20.0, 'candidate': 8.0, 'cleanup': 5.0, 'absence': 5.0}
|
||
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
|
||
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nelse: sys.exit(1)"
|
||
TRACE = {'phases': [], 'exceptions': [], 'failures': []}
|
||
|
||
|
||
def exact_int(value):
|
||
return type(value) is int
|
||
|
||
|
||
def stage(phase, argv, cwd, input_bytes=b''):
|
||
config = dict(phase=phase, argv=argv, cwd=cwd, input=base64.b64encode(input_bytes).decode('ascii'),
|
||
nonce=uuid.uuid4().hex, deadline=DEADLINES[phase])
|
||
started = time.monotonic()
|
||
wrapper_argv = [sys.executable, '-I', '-c', SUPERVISOR, json.dumps(config, separators=(',', ':'))]
|
||
completed = subprocess.run(wrapper_argv, capture_output=True)
|
||
raw = completed.stdout
|
||
# Wire facts precede parsing and survive deliberately malformed metadata.
|
||
record = dict(phase=phase, nonce=config['nonce'], argv=argv, cwd=cwd, deadline=config['deadline'],
|
||
wrapper_rc=completed.returncode, wire_len=len(raw), wire_sha256=hashlib.sha256(raw).hexdigest(),
|
||
wrapper_stderr_len=len(completed.stderr),
|
||
wrapper_stderr_sha256=hashlib.sha256(completed.stderr).hexdigest(),
|
||
wrapper_source_sha256=hashlib.sha256(SUPERVISOR.encode()).hexdigest(),
|
||
parent_elapsed=time.monotonic()-started)
|
||
TRACE['phases'].append(record)
|
||
if completed.returncode != 0:
|
||
raise RuntimeError(phase+' supervisor nonzero '+str(completed.returncode))
|
||
value = json.loads(raw)
|
||
if type(value) is not dict or set(value) != {'meta', 'stdout', 'stderr'}:
|
||
raise ValueError(phase+' wire shape')
|
||
meta = value['meta']
|
||
record['meta'] = meta
|
||
stdout = base64.b64decode(value['stdout'], validate=True)
|
||
stderr = base64.b64decode(value['stderr'], validate=True)
|
||
required = {'phase','nonce','argv','cwd','uid','gid','groups','supervisor_pid','leader_pid','leader_start',
|
||
'leader_rc','leader_reaped','deadline','elapsed','timed_out','controlled_rc','remaining',
|
||
'exceptions','signals','adopted_reaped','stdout_len','stdout_sha256','stderr_len','stderr_sha256'}
|
||
if type(meta) is not dict or set(meta) != required:
|
||
raise ValueError(phase+' metadata fields')
|
||
for name in ('supervisor_pid', 'leader_pid', 'leader_start'):
|
||
if not exact_int(meta[name]) or meta[name] <= 0:
|
||
raise ValueError(phase+' positive integer '+name)
|
||
if meta['supervisor_pid'] == meta['leader_pid']:
|
||
raise ValueError(phase+' leader identity collision')
|
||
for name in ('uid','gid'):
|
||
if not exact_int(meta[name]) or meta[name] < 0:
|
||
raise ValueError(phase+' identity type')
|
||
if type(meta['groups']) is not list or any(not exact_int(x) or x < 0 for x in meta['groups']):
|
||
raise ValueError(phase+' groups type')
|
||
expected_identity = (65534,65534,[]) if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else (os.getuid(),os.getgid(),os.getgroups())
|
||
if (meta['uid'],meta['gid'],meta['groups']) != expected_identity:
|
||
raise ValueError(phase+' execution identity')
|
||
if meta['phase'] != phase or meta['nonce'] != config['nonce'] or meta['argv'] != argv or meta['cwd'] != cwd:
|
||
raise ValueError(phase+' binding')
|
||
if type(meta['deadline']) not in (int,float) or not math.isfinite(meta['deadline']) or meta['deadline'] != config['deadline']:
|
||
raise ValueError(phase+' deadline')
|
||
if type(meta['elapsed']) not in (int,float) or not math.isfinite(meta['elapsed']) or not 0 <= meta['elapsed'] <= config['deadline']+7.0:
|
||
raise ValueError(phase+' elapsed')
|
||
if type(meta['timed_out']) is not bool or type(meta['leader_reaped']) is not bool:
|
||
raise ValueError(phase+' boolean type')
|
||
if not exact_int(meta['leader_rc']) or not exact_int(meta['controlled_rc']):
|
||
raise ValueError(phase+' returncode type')
|
||
if meta['controlled_rc'] != (124 if meta['timed_out'] else meta['leader_rc']):
|
||
raise ValueError(phase+' controlled returncode conflict')
|
||
if meta['timed_out'] and meta['leader_rc'] >= 0:
|
||
raise ValueError(phase+' timeout leader conflict')
|
||
for name, width in (('signals',2), ('adopted_reaped',3), ('remaining',2)):
|
||
rows = meta[name]
|
||
if type(rows) is not list or any(type(row) is not list or len(row) != width or
|
||
any(not exact_int(x) for x in row) or row[0] <= 0 or row[1] <= 0 for row in rows):
|
||
raise ValueError(phase+' process list '+name)
|
||
if type(meta['exceptions']) is not list:
|
||
raise ValueError(phase+' exceptions type')
|
||
for prefix, data in (('stdout',stdout), ('stderr',stderr)):
|
||
if not exact_int(meta[prefix+'_len']) or meta[prefix+'_len'] < 0 or meta[prefix+'_len'] != len(data):
|
||
raise ValueError(phase+' IO length')
|
||
if meta[prefix+'_sha256'] != hashlib.sha256(data).hexdigest():
|
||
raise ValueError(phase+' IO digest')
|
||
if meta['exceptions'] or meta['remaining'] or not meta['leader_reaped']:
|
||
raise ValueError(phase+' incomplete recovery or exception')
|
||
if meta['timed_out'] or meta['controlled_rc'] != 0 or meta['leader_rc'] != 0:
|
||
raise RuntimeError(phase+' controlled failure rc='+str(meta['controlled_rc']))
|
||
return stdout, stderr
|
||
|
||
|
||
def check(candidate):
|
||
input_bytes, expected, labels = make_matrix()
|
||
frozen = {name:(candidate/name).read_bytes() for name in CANDIDATES}
|
||
TRACE['source_sha256'] = {name:hashlib.sha256(raw).hexdigest() for name,raw in frozen.items()}
|
||
TRACE['input_len'] = len(input_bytes)
|
||
TRACE['input_sha256'] = hashlib.sha256(input_bytes).hexdigest()
|
||
TRACE['evidence_rows'] = len(expected)
|
||
scratch = None
|
||
behavior_ok = False
|
||
cleanup_ok = False
|
||
absence_ok = False
|
||
try:
|
||
scratch = tempfile.mkdtemp(prefix='tile-weak-cache-')
|
||
TRACE['scratch'] = scratch
|
||
work = Path(scratch)/'work'
|
||
work.mkdir()
|
||
work.chmod(0o777)
|
||
for name, data in dict(frozen, **{'main.cpp':OBSERVER.encode()}).items():
|
||
path = work/name
|
||
path.write_bytes(data)
|
||
path.chmod(0o644)
|
||
compiler_argv = ['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic',
|
||
'payload.cpp','registry.cpp','commands.cpp','main.cpp','-o','cache_probe']
|
||
# No controller filesystem access below this point, including finally.
|
||
stage('compiler', compiler_argv, str(work))
|
||
stdout, stderr = stage('candidate', ['./cache_probe'], str(work), input_bytes)
|
||
lines = stdout.splitlines()
|
||
if len(lines) != len(expected):
|
||
TRACE['failures'].append(['row_count',len(lines),len(expected)])
|
||
else:
|
||
behavior_ok = True
|
||
for index,(raw,wanted) in enumerate(zip(lines,expected)):
|
||
observed = json.loads(raw)
|
||
if type(observed) is not list or any(not exact_int(x) for x in observed) or observed != wanted:
|
||
TRACE['failures'].append(['behavior',index,labels[index],observed,wanted])
|
||
behavior_ok = False
|
||
break
|
||
except Exception as error:
|
||
TRACE['exceptions'].append(['main',type(error).__name__,repr(error)])
|
||
finally:
|
||
if scratch is not None:
|
||
try:
|
||
stage('cleanup',[sys.executable,'-I','-c',CLEANUP,scratch],'/tmp')
|
||
cleanup_ok = True
|
||
except Exception as error:
|
||
TRACE['exceptions'].append(['cleanup',type(error).__name__,repr(error)])
|
||
try:
|
||
stage('absence',[sys.executable,'-I','-c',ABSENCE,scratch],'/tmp')
|
||
absence_ok = True
|
||
except Exception as error:
|
||
TRACE['exceptions'].append(['absence',type(error).__name__,repr(error)])
|
||
TRACE['success_flags'] = [behavior_ok, cleanup_ok, absence_ok]
|
||
passed = behavior_ok and cleanup_ok and absence_ok and not TRACE['exceptions'] and not TRACE['failures']
|
||
raw_trace = json.dumps(TRACE, separators=(',',':'),sort_keys=True).encode()
|
||
sys.stderr.write('TILETRACE '+base64.b64encode(zlib.compress(raw_trace,9)).decode()+'\n')
|
||
print('PASS' if passed else 'FAIL '+json.dumps(TRACE['failures'][:1] or TRACE['exceptions'][:1],separators=(',',':'))[:250])
|
||
return 0 if passed else 1
|
||
|
||
|
||
if __name__ == '__main__':
|
||
sys.exit(check(Path(sys.argv[1])))
|