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>
484 lines
36 KiB
Python
484 lines
36 KiB
Python
import base64
|
|
import bz2
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import lzma
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import secrets
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
CANDIDATES = ['store.h', 'store.c', 'wire.h', 'wire.c', 'stream.h', 'stream.c', 'batch.h', 'batch.c']
|
|
COMPILER_DEADLINE = 20
|
|
CANDIDATE_DEADLINE = 12
|
|
CLEANUP_DEADLINE = 8
|
|
ABSENCE_DEADLINE = 8
|
|
CLEANUP_CODE = 'import shutil,sys; shutil.rmtree(sys.argv[1])'
|
|
ABSENCE_CODE = '''import os,sys
|
|
try:
|
|
os.lstat(sys.argv[1])
|
|
except FileNotFoundError:
|
|
raise SystemExit(0)
|
|
raise SystemExit(1)
|
|
'''
|
|
EXCHANGES = []
|
|
|
|
|
|
def digest(raw):
|
|
return hashlib.sha256(raw).hexdigest()
|
|
|
|
|
|
def evidence():
|
|
raw = json.dumps(EXCHANGES, separators=(',', ':')).encode()
|
|
compressed = bz2.compress(raw)
|
|
codec = 'B'
|
|
if len(raw) < 1024 * 1024:
|
|
alternative = lzma.compress(raw)
|
|
if len(alternative) < len(compressed):
|
|
compressed, codec = alternative, 'L'
|
|
sys.stderr.write('E:' + codec + base64.b85encode(compressed).decode() + '\n')
|
|
sys.stderr.flush()
|
|
|
|
|
|
def exact_int(value, minimum=None):
|
|
return type(value) is int and (minimum is None or value >= minimum)
|
|
|
|
|
|
def run_phase(phase, argv, cwd, input_bytes, deadline):
|
|
request = dict(phase=phase, nonce=secrets.token_hex(12), argv=argv, cwd=cwd,
|
|
input_hex=input_bytes.hex(), deadline=deadline)
|
|
raw_request = json.dumps(request, separators=(',', ':')).encode()
|
|
outer_started = __import__('time').monotonic()
|
|
process = subprocess.Popen([sys.executable, '-I', '-c', SUPERVISOR],
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
with open('/proc/%d/stat' % process.pid, 'rb') as source:
|
|
wrapper_start = int(source.read().rsplit(b')', 1)[1].split()[19])
|
|
outer_timeout = False
|
|
try:
|
|
wire, wrapper_stderr = process.communicate(input=raw_request, timeout=deadline + 8)
|
|
except subprocess.TimeoutExpired as error:
|
|
# Root lacks CAP_KILL for UID65534. Never enter subprocess.run's implicit kill-and-wait.
|
|
wire, wrapper_stderr = error.output or b'', error.stderr or b''
|
|
outer_timeout = True
|
|
for stream in (process.stdin, process.stdout, process.stderr):
|
|
if stream is not None and not stream.closed:
|
|
stream.close()
|
|
wrapper_rc = process.poll()
|
|
EXCHANGES.append(dict(phase=phase, wrapper_rc=wrapper_rc, wrapper_pid=process.pid,
|
|
wrapper_start=wrapper_start, outer_timeout=outer_timeout,
|
|
outer_elapsed=__import__('time').monotonic() - outer_started,
|
|
request_raw=raw_request.decode('latin1'),
|
|
request_len=len(raw_request), request_sha256=digest(raw_request),
|
|
wire_raw=wire.decode('latin1'), wire_len=len(wire), wire_sha256=digest(wire),
|
|
wrapper_stderr_hex=wrapper_stderr.hex()))
|
|
# This durable stderr record precedes parsing and rejects missing metadata honestly.
|
|
evidence()
|
|
if outer_timeout or type(wrapper_rc) is not int or wrapper_rc != 0:
|
|
raise ValueError(phase + ': supervisor outer timeout/nonzero rc=' + str(wrapper_rc))
|
|
result = json.loads(wire)
|
|
if type(result) is not dict:
|
|
raise ValueError(phase + ': metadata object')
|
|
for key in ('phase', 'nonce', 'argv', 'cwd', 'deadline'):
|
|
if result[key] != request[key] or type(result[key]) is not type(request[key]):
|
|
raise ValueError(phase + ': request binding ' + key)
|
|
if result['request_len'] != len(raw_request) or not exact_int(result['request_len'], 0) or result['request_sha256'] != digest(raw_request):
|
|
raise ValueError(phase + ': raw request binding')
|
|
wanted_uid = 65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else os.getuid()
|
|
wanted_gid = 65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else os.getgid()
|
|
wanted_groups = [] if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else os.getgroups()
|
|
for key, wanted in (('uid', wanted_uid), ('euid', wanted_uid), ('gid', wanted_gid), ('egid', wanted_gid)):
|
|
if not exact_int(result[key], 0) or result[key] != wanted:
|
|
raise ValueError(phase + ': identity ' + key)
|
|
if type(result['groups']) is not list or any(not exact_int(x, 0) for x in result['groups']) or result['groups'] != wanted_groups:
|
|
raise ValueError(phase + ': groups identity')
|
|
for key in ('supervisor_pid', 'supervisor_start', 'leader_pid', 'leader_start', 'leader_pgid'):
|
|
if not exact_int(result[key], 1):
|
|
raise ValueError(phase + ': process binding ' + key)
|
|
if result['supervisor_pid'] != process.pid or result['supervisor_start'] != wrapper_start:
|
|
raise ValueError(phase + ': wrapper identity binding')
|
|
if result['supervisor_pid'] == result['leader_pid'] or result['leader_pgid'] != result['leader_pid']:
|
|
raise ValueError(phase + ': process identities')
|
|
if type(result['elapsed']) not in (int, float) or not math.isfinite(result['elapsed']) or not 0 <= result['elapsed'] <= deadline + 4:
|
|
raise ValueError(phase + ': elapsed range')
|
|
if type(result['grace']) is not float or result['grace'] != 3.0:
|
|
raise ValueError(phase + ': grace')
|
|
if type(result['timeout']) is not bool or type(result['leader_reaped']) is not bool or type(result['descendants_absent']) is not bool or type(result['pg_absent']) is not bool:
|
|
raise ValueError(phase + ': boolean metadata')
|
|
if not exact_int(result['leader_rc']) or not exact_int(result['controlled_rc']):
|
|
raise ValueError(phase + ': rc metadata')
|
|
if result['controlled_rc'] != (124 if result['timeout'] else result['leader_rc']):
|
|
raise ValueError(phase + ': controlled rc inconsistency')
|
|
if type(result['remaining']) is not dict or type(result['pg_remaining']) is not dict or type(result['adopted']) is not list or type(result['exceptions']) is not list:
|
|
raise ValueError(phase + ': descendant metadata')
|
|
for row in result['adopted']:
|
|
if type(row) is not list or len(row) != 2 or not exact_int(row[0], 1) or not exact_int(row[1], 0):
|
|
raise ValueError(phase + ': adopted metadata')
|
|
if result['pg_absent'] != (not result['pg_remaining']):
|
|
raise ValueError(phase + ': PG absence contradiction')
|
|
if result['descendants_absent'] != (not result['remaining']):
|
|
raise ValueError(phase + ': absence contradiction')
|
|
if not exact_int(result['input_len'], 0) or result['input_len'] != len(input_bytes) or result['input_sha256'] != digest(input_bytes):
|
|
raise ValueError(phase + ': input binding')
|
|
streams = []
|
|
for label in ('stdout', 'stderr'):
|
|
raw = bytes.fromhex(result['stdout_hex']) if label == 'stdout' else bz2.decompress(bytes.fromhex(result['stderr_bz2_hex']))
|
|
if not exact_int(result[label + '_len'], 0) or result[label + '_len'] != len(raw) or result[label + '_sha256'] != digest(raw):
|
|
raise ValueError(phase + ': IO binding ' + label)
|
|
streams.append(raw)
|
|
if result['exceptions'] or not result['leader_reaped'] or not result['descendants_absent'] or result['remaining'] or not result['pg_absent'] or result['pg_remaining']:
|
|
raise ValueError(phase + ': incomplete reclaim ' + repr(result['exceptions']))
|
|
if result['timeout'] or result['controlled_rc'] != 0 or result['leader_rc'] != 0:
|
|
raise ValueError(phase + ': controlled rejection rc=' + str(result['controlled_rc']) + ' leader=' + str(result['leader_rc']) + ' timeout=' + str(result['timeout']))
|
|
return streams[0]
|
|
|
|
|
|
def read_candidate(root, name):
|
|
root_fd = os.open(root, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
try:
|
|
fd = os.open(name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=root_fd)
|
|
try:
|
|
info = os.fstat(fd)
|
|
if not stat.S_ISREG(info.st_mode) or info.st_size > 1024 * 1024:
|
|
raise ValueError('candidate is not a bounded regular file: ' + name)
|
|
with os.fdopen(fd, 'rb', closefd=False) as source:
|
|
raw = source.read(1024 * 1024 + 1)
|
|
if len(raw) > 1024 * 1024:
|
|
raise ValueError('candidate grew')
|
|
return raw
|
|
finally:
|
|
os.close(fd)
|
|
finally:
|
|
os.close(root_fd)
|
|
|
|
|
|
def decimal(word):
|
|
if not re.fullmatch(rb'[+-]?[0-9]+', word):
|
|
raise ValueError('non-ASCII decimal token')
|
|
negative = word[:1] == b'-'
|
|
digits = word.lstrip(b'+-').lstrip(b'0') or b'0'
|
|
if len(digits) > 20:
|
|
raise ValueError('observable integer out of range')
|
|
value = int(digits)
|
|
return -value if negative else value
|
|
|
|
|
|
def parse_observation(raw):
|
|
words = re.split(rb'[ \t\r]+', raw.strip(b' \t\r'))
|
|
if len(words) != 8:
|
|
raise ValueError('observer field count')
|
|
result, aux = decimal(words[0]), decimal(words[1])
|
|
def unhex(word):
|
|
if word == b'-':
|
|
return b''
|
|
if not re.fullmatch(rb'(?:[0-9a-f]{2})+', word):
|
|
raise ValueError('hex protocol')
|
|
return bytes.fromhex(word.decode('ascii'))
|
|
slots = []
|
|
for word in words[3:7]:
|
|
fields = word.split(b':')
|
|
if len(fields) != 3:
|
|
raise ValueError('span protocol')
|
|
slots.append((decimal(fields[0]), decimal(fields[1]), unhex(fields[2])))
|
|
stream = words[7].split(b':')
|
|
if len(stream) != 5:
|
|
raise ValueError('stream protocol')
|
|
return result, aux, unhex(words[2]), slots, tuple(map(decimal, stream))
|
|
|
|
SUPERVISOR = 'import base64\nimport bz2\nimport ctypes\nimport hashlib\nimport json\nimport os\nimport selectors\nimport signal\nimport subprocess\nimport sys\nimport time\n\n\ndef stat_pid(pid):\n try:\n with open(\'/proc/%d/stat\' % pid, \'rb\') as source:\n fields = source.read().rsplit(b\')\', 1)[1].split()\n return (fields[0].decode(\'ascii\'), int(fields[1]), int(fields[2]), int(fields[19]))\n except (FileNotFoundError, ProcessLookupError):\n return None\n\n\ndef closure(table, supervisor, tracked):\n roots = {supervisor}\n for pid, start in tracked.items():\n entry = table.get(pid)\n if entry is not None and entry[3] == start and entry[0] not in (\'Z\', \'X\'):\n roots.add(pid)\n found = set()\n changed = True\n while changed:\n changed = False\n for pid, entry in table.items():\n if pid != supervisor and (entry[1] in roots or pid in roots):\n found.add(pid)\n if pid not in roots:\n roots.add(pid)\n changed = True\n return {pid: table[pid][3] for pid in found}\n\n\ndef table_now():\n table = {}\n for word in os.listdir(\'/proc\'):\n if word.isascii() and word.isdigit():\n pid = int(word)\n entry = stat_pid(pid)\n if entry is not None:\n table[pid] = entry\n return table\n\n\ndef pg_members(table, pgid):\n return {pid: entry[3] for pid, entry in table.items() if entry[2] == pgid}\n\n\ndef fresh_kill(tokens):\n for pid, start in tokens.items():\n entry = stat_pid(pid)\n if entry is not None and entry[3] == start and entry[0] not in (\'Z\', \'X\'):\n try:\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n\n\ndef execute(request):\n started = time.monotonic()\n deadline = request[\'deadline\']\n grace = 3.0\n supervisor = os.getpid()\n supervisor_start = stat_pid(supervisor)[3]\n errors = []\n tracked = {}\n adopted = []\n stdout = bytearray()\n stderr = bytearray()\n child = None\n leader = None\n leader_start = None\n leader_pgid = None\n leader_rc = None\n timed_out = False\n remaining = {}\n selector = selectors.DefaultSelector()\n input_bytes = bytes.fromhex(request[\'input_hex\'])\n input_offset = 0\n\n def discover():\n nonlocal remaining\n table = table_now()\n found = closure(table, supervisor, tracked)\n tracked.update(found)\n remaining = {pid: start for pid, start in tracked.items()\n if pid in table and table[pid][3] == start}\n return found\n\n def poll_reap():\n nonlocal leader_rc\n if child is not None:\n leader_rc = child.poll()\n for pid in list(tracked):\n if pid == leader:\n continue\n entry = stat_pid(pid)\n if entry is not None and entry[3] == tracked[pid] and entry[1] == supervisor and entry[0] == \'Z\':\n try:\n reaped, status = os.waitpid(pid, os.WNOHANG)\n except ChildProcessError:\n continue\n if reaped:\n adopted.append([reaped, status])\n\n def drain(wait):\n nonlocal input_offset\n for key, mask in selector.select(wait):\n stream = key.fileobj\n if key.data == \'input\':\n try:\n sent = os.write(stream.fileno(), input_bytes[input_offset:input_offset + 65536])\n input_offset += sent\n except BrokenPipeError:\n input_offset = len(input_bytes)\n except BlockingIOError:\n continue\n if input_offset == len(input_bytes):\n selector.unregister(stream)\n stream.close()\n else:\n try:\n chunk = os.read(stream.fileno(), 65536)\n except BlockingIOError:\n continue\n if chunk:\n (stdout if key.data == \'stdout\' else stderr).extend(chunk)\n else:\n selector.unregister(stream)\n stream.close()\n\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 child = subprocess.Popen(request[\'argv\'], cwd=request[\'cwd\'],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n leader = child.pid\n entry = stat_pid(leader)\n if entry is None:\n raise RuntimeError(\'leader stat disappeared before binding\')\n leader_start, leader_pgid = entry[3], entry[2]\n tracked[leader] = leader_start\n for stream, name in ((child.stdout, \'stdout\'), (child.stderr, \'stderr\')):\n os.set_blocking(stream.fileno(), False)\n selector.register(stream, selectors.EVENT_READ, name)\n if input_bytes:\n os.set_blocking(child.stdin.fileno(), False)\n selector.register(child.stdin, selectors.EVENT_WRITE, \'input\')\n else:\n child.stdin.close()\n while True:\n discover()\n poll_reap()\n now = time.monotonic()\n if now - started >= deadline:\n timed_out = True\n if timed_out or leader_rc is not None:\n fresh_kill(discover())\n drain(0.005)\n discover()\n if leader_rc is not None and not remaining and not selector.get_map():\n break\n if now - started >= deadline + grace:\n errors.append(\'execution reclaim deadline\')\n break\n except BaseException as error:\n errors.append(type(error).__name__ + \':\' + str(error))\n finally:\n reclaim_end = min(started + deadline + grace, time.monotonic() + grace)\n while child is not None and time.monotonic() < reclaim_end:\n try:\n fresh_kill(discover())\n poll_reap()\n drain(0.005)\n discover()\n if leader_rc is not None and not remaining and not selector.get_map():\n break\n except BaseException as error:\n errors.append(type(error).__name__ + \':\' + str(error))\n time.sleep(0.005)\n try:\n discover()\n poll_reap()\n discover()\n except BaseException as error:\n errors.append(type(error).__name__ + \':\' + str(error))\n for key in list(selector.get_map().values()):\n key.fileobj.close()\n selector.close()\n elapsed = time.monotonic() - started\n out, err = bytes(stdout), bytes(stderr)\n try:\n pg_remaining = pg_members(table_now(), leader_pgid) if leader_pgid is not None else {0: 0}\n except BaseException as error:\n errors.append(type(error).__name__ + ":" + str(error))\n pg_remaining = {0: 0}\n return dict(phase=request[\'phase\'], nonce=request[\'nonce\'], argv=request[\'argv\'],\n cwd=request[\'cwd\'], deadline=deadline, grace=grace,\n uid=os.getuid(), euid=os.geteuid(), gid=os.getgid(), egid=os.getegid(),\n groups=os.getgroups(), supervisor_pid=supervisor, supervisor_start=supervisor_start, leader_pid=leader,\n leader_start=leader_start, leader_pgid=leader_pgid, leader_rc=leader_rc,\n leader_reaped=leader_rc is not None, controlled_rc=124 if timed_out else leader_rc,\n timeout=timed_out, elapsed=elapsed, remaining=remaining,\n descendants_absent=not remaining, pg_absent=not pg_remaining, pg_remaining=pg_remaining, adopted=adopted, exceptions=errors,\n input_len=len(input_bytes), input_sha256=hashlib.sha256(input_bytes).hexdigest(),\n stdout_hex=out.hex(), stdout_len=len(out),\n stdout_sha256=hashlib.sha256(out).hexdigest(),\n stderr_bz2_hex=bz2.compress(err).hex(), stderr_len=len(err),\n stderr_sha256=hashlib.sha256(err).hexdigest())\n\n\nif __name__ == \'__main__\':\n raw = sys.stdin.buffer.read()\n request = json.loads(raw)\n result = execute(request)\n result[\'request_len\'] = len(raw)\n result[\'request_sha256\'] = hashlib.sha256(raw).hexdigest()\n sys.stdout.write(json.dumps(result, separators=(\',\', \':\')))\n'
|
|
OBSERVER = '#include "store.h"\n#include "wire.h"\n#include "stream.h"\n#include "batch.h"\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nstatic int digit(char character)\n{\n if (character >= \'0\' && character <= \'9\') return character - \'0\';\n if (character >= \'a\' && character <= \'f\') return character - \'a\' + 10;\n return -1;\n}\n\nstatic size_t unhex(const char *word, unsigned char *output, size_t capacity)\n{\n size_t index, count;\n if (word == NULL || (word[0] == \'-\' && word[1] == 0)) return 0;\n count = strlen(word) / 2;\n if (count > capacity) return 0;\n for (index = 0; index < count; ++index) {\n output[index] = (unsigned char)((digit(word[index * 2]) << 4) |\n digit(word[index * 2 + 1]));\n }\n return count;\n}\n\nstatic void hexout(const unsigned char *data, size_t count)\n{\n static const char symbols[] = "0123456789abcdef";\n size_t index;\n if (count == 0) {\n putchar(\'-\');\n return;\n }\n for (index = 0; index < count; ++index) {\n putchar(symbols[data[index] >> 4]);\n putchar(symbols[data[index] & 15]);\n }\n}\n\nstatic char *next(void)\n{\n return strtok(NULL, " \\t\\r\\n");\n}\n\nstatic int number(void)\n{\n const char *word = next();\n return word ? (int)strtol(word, NULL, 10) : 0;\n}\n\nstatic size_t amount(void)\n{\n const char *word = next();\n return word ? (size_t)strtoull(word, NULL, 10) : 0;\n}\n\nstatic void state(int result, int aux, const unsigned char *bytes, size_t count,\n const stream_decoder *decoder)\n{\n int slot;\n printf("%d %d ", result, aux);\n hexout(bytes, count);\n for (slot = 0; slot < STORE_SLOTS; ++slot) {\n unsigned char *data = store_span(slot);\n size_t length = store_length(slot);\n printf(" %lu:%lu:", (unsigned long)((uintptr_t)data % 32u),\n (unsigned long)length);\n hexout(data, length);\n }\n printf(" %lu:%lu:%lu:%lu:%d\\n", stream_accepted(decoder),\n stream_rejected(decoder), stream_discarded(decoder),\n (unsigned long)stream_pending(decoder), stream_last_slot(decoder));\n fflush(stdout);\n}\n\nstatic void read_operation(batch_operation *op)\n{\n const char *word;\n memset(op, 0, sizeof(*op));\n op->kind = number();\n op->slot = number();\n op->peer = number();\n op->offset = amount();\n op->length = amount();\n op->value = (unsigned char)number();\n word = next();\n unhex(word, op->data, sizeof(op->data));\n}\n\nint main(void)\n{\n char line[4096];\n unsigned char input[2048];\n unsigned char output[2048];\n stream_decoder decoder;\n store_reset();\n stream_init(&decoder);\n while (fgets(line, sizeof(line), stdin) != NULL) {\n char *command = strtok(line, " \\t\\r\\n");\n const char *word;\n int result = 0, aux = 0, first, second;\n size_t length, count = 0, offset, written = 0;\n if (command == NULL) continue;\n switch (command[0]) {\n case \'R\':\n store_reset();\n stream_init(&decoder);\n break;\n case \'W\':\n first = number(); word = next(); length = unhex(word, input, sizeof(input));\n result = store_write(first, input, length);\n break;\n case \'A\':\n first = number(); word = next(); length = unhex(word, input, sizeof(input));\n result = store_append(first, input, length);\n break;\n case \'P\':\n first = number(); offset = amount(); word = next();\n length = unhex(word, input, sizeof(input));\n result = store_patch(first, offset, input, length);\n break;\n case \'F\':\n first = number(); second = number(); length = amount();\n result = store_fill(first, (unsigned char)second, length);\n break;\n case \'C\':\n first = number(); second = number(); result = store_copy(first, second);\n break;\n case \'E\':\n first = number(); offset = amount(); length = amount();\n result = store_erase(first, offset, length);\n break;\n case \'V\': result = store_reverse(number()); break;\n case \'O\': first = number(); offset = amount(); result = store_rotate(first, offset); break;\n case \'X\': first = number(); second = number(); result = store_xor(first, (unsigned char)second); break;\n case \'Q\': first = number(); second = number(); result = store_find(first, (unsigned char)second); break;\n case \'M\': first = number(); second = number(); result = store_compare(first, second); break;\n case \'K\': first = number(); result = store_checksum(first); break;\n case \'Z\':\n first = number(); length = amount();\n result = wire_pack(first, output, length, &written);\n if (result == 0) count = written;\n break;\n case \'U\':\n word = next(); length = unhex(word, input, sizeof(input));\n result = wire_unpack(input, length, &aux);\n break;\n case \'T\':\n word = next(); length = unhex(word, input, sizeof(input));\n result = stream_feed(&decoder, input, length);\n break;\n case \'L\': result = stream_flush(&decoder); break;\n case \'B\': {\n batch_operation op;\n size_t failed;\n read_operation(&op);\n result = batch_apply(&op, 1, &failed);\n aux = (int)failed;\n break;\n }\n case \'J\': {\n batch_operation ops[2];\n size_t failed;\n read_operation(&ops[0]);\n read_operation(&ops[1]);\n result = batch_apply(ops, 2, &failed);\n aux = (int)failed;\n break;\n }\n case \'H\': result = (int)batch_fingerprint(); break;\n case \'D\':\n first = number(); length = amount();\n result = batch_export(first, output, length, &written);\n if (result == 0) count = written;\n break;\n case \'I\':\n first = number(); word = next(); length = unhex(word, input, sizeof(input));\n result = batch_import(first, input, length);\n break;\n case \'S\':\n first = number(); offset = amount(); length = amount(); second = number();\n result = batch_slice(first, offset, length, output, (size_t)second, &written);\n if (result == 0) count = written;\n break;\n default: return 3;\n }\n state(result, aux, output, count, &decoder);\n }\n return ferror(stdin) ? 2 : 0;\n}\n'
|
|
|
|
def crc(data):
|
|
value = 65535
|
|
for byte in data:
|
|
value ^= byte << 8
|
|
for bit in range(8):
|
|
value = ((value << 1) ^ (0x1021 if value & 32768 else 0)) & 65535
|
|
return value
|
|
|
|
|
|
def frame(slot, data):
|
|
body = bytes([167, slot, len(data)]) + data
|
|
return body + crc(body).to_bytes(2, 'big')
|
|
|
|
|
|
def hx(raw):
|
|
return raw.hex() or '-'
|
|
|
|
|
|
class Model:
|
|
def __init__(self):
|
|
self.slots = [b''] * 4
|
|
self.pending = b''
|
|
self.accepted = self.rejected = self.discarded = 0
|
|
self.last = -1
|
|
|
|
def store(self, kind, slot, peer=0, offset=0, length=0, value=0, data=b''):
|
|
if kind not in range(1, 11):
|
|
return -3
|
|
if kind in (1, 2) and length > 128:
|
|
return -2
|
|
if kind == 3 and length > 128:
|
|
return -3
|
|
if slot not in range(4) or kind in (5, 10) and peer not in range(4):
|
|
return -1
|
|
old = self.slots[slot]
|
|
if kind == 1:
|
|
self.slots[slot] = data[:length]
|
|
elif kind == 2:
|
|
if length > 128 - len(old):
|
|
return -2
|
|
self.slots[slot] = old + data[:length]
|
|
elif kind == 3:
|
|
if offset > len(old) or length > len(old) - offset:
|
|
return -3
|
|
self.slots[slot] = old[:offset] + data[:length] + old[offset + length:]
|
|
elif kind == 4:
|
|
if offset > len(old) or length > len(old) - offset:
|
|
return -3
|
|
self.slots[slot] = old[:offset] + old[offset + length:]
|
|
elif kind == 5:
|
|
self.slots[slot] = self.slots[peer]
|
|
elif kind == 6:
|
|
self.slots[slot] = old[::-1]
|
|
elif kind == 7:
|
|
distance = offset % len(old) if old else 0
|
|
self.slots[slot] = old[distance:] + old[:distance]
|
|
elif kind == 8:
|
|
self.slots[slot] = bytes(x ^ value for x in old)
|
|
elif kind == 9:
|
|
if length > 128:
|
|
return -2
|
|
self.slots[slot] = bytes([value]) * length
|
|
else:
|
|
self.slots[slot], self.slots[peer] = self.slots[peer], old
|
|
return 0
|
|
|
|
def unpack(self, data):
|
|
if len(data) < 5:
|
|
return -11, 0
|
|
if data[0] != 167 or data[1] >= 4 or data[2] > 128 or len(data) != data[2] + 5:
|
|
return -12, 0
|
|
if crc(data[:-2]) != int.from_bytes(data[-2:], 'big'):
|
|
return -13, 0
|
|
self.slots[data[1]] = data[3:-2]
|
|
return 0, data[1]
|
|
|
|
def process(self):
|
|
while self.pending:
|
|
while self.pending and self.pending[0] != 167:
|
|
self.pending = self.pending[1:]
|
|
self.discarded += 1
|
|
if len(self.pending) < 3:
|
|
return
|
|
if self.pending[1] >= 4 or self.pending[2] > 128:
|
|
self.rejected += 1
|
|
self.discarded += 1
|
|
self.pending = self.pending[1:]
|
|
continue
|
|
total = self.pending[2] + 5
|
|
if len(self.pending) < total:
|
|
return
|
|
result, slot = self.unpack(self.pending[:total])
|
|
if result == 0:
|
|
self.accepted += 1
|
|
self.last = slot
|
|
self.pending = self.pending[total:]
|
|
else:
|
|
self.rejected += 1
|
|
self.discarded += 1
|
|
self.pending = self.pending[1:]
|
|
|
|
def command(self, command):
|
|
words = command.split(' ')
|
|
kind = words[0]
|
|
result = aux = 0
|
|
output = b''
|
|
n = lambda index: int(words[index])
|
|
h = lambda index: b'' if words[index] == '-' else bytes.fromhex(words[index])
|
|
if kind == 'R':
|
|
self.__init__()
|
|
elif kind in ('W', 'A', 'P'):
|
|
data = h(3 if kind == 'P' else 2)
|
|
slot = n(1)
|
|
if slot not in range(4):
|
|
result = -1
|
|
elif kind == 'W' and len(data) > 128:
|
|
result = -2
|
|
else:
|
|
result = self.store({'W': 1, 'A': 2, 'P': 3}[kind], slot,
|
|
offset=n(2) if kind == 'P' else 0,
|
|
length=len(data), data=data)
|
|
elif kind in ('F', 'C', 'E', 'V', 'O', 'X'):
|
|
result = self.store({'F': 9, 'C': 5, 'E': 4, 'V': 6, 'O': 7, 'X': 8}[kind], n(1),
|
|
peer=n(2) if kind == 'C' else 0,
|
|
offset=n(2) if kind in ('E', 'O') else 0,
|
|
length=n(3) if kind in ('E', 'F') else 0,
|
|
value=n(2) if kind in ('F', 'X') else 0)
|
|
elif kind == 'Q':
|
|
result = self.slots[n(1)].find(bytes([n(2)])) if n(1) in range(4) else -1
|
|
elif kind == 'M':
|
|
if n(1) not in range(4) or n(2) not in range(4):
|
|
result = -1
|
|
else:
|
|
left, right = self.slots[n(1)], self.slots[n(2)]
|
|
result = (left > right) - (left < right)
|
|
elif kind == 'K':
|
|
if n(1) in range(4):
|
|
for byte in self.slots[n(1)]:
|
|
result = (((result << 5) | (result >> 11)) + byte) & 65535
|
|
elif kind == 'Z':
|
|
if n(1) not in range(4):
|
|
result = -10
|
|
else:
|
|
packed = frame(n(1), self.slots[n(1)])
|
|
if n(2) < len(packed):
|
|
result = -14
|
|
else:
|
|
output = packed
|
|
elif kind == 'U':
|
|
result, aux = self.unpack(h(1))
|
|
if result:
|
|
aux = None
|
|
elif kind == 'T':
|
|
for byte in h(1):
|
|
self.pending += bytes([byte])
|
|
self.process()
|
|
elif kind == 'L':
|
|
self.process()
|
|
self.discarded += len(self.pending)
|
|
self.pending = b''
|
|
elif kind in ('B', 'J'):
|
|
saved = self.slots[:]
|
|
count = 1 if kind == 'B' else 2
|
|
aux = count
|
|
for index in range(count):
|
|
start = 1 + index * 7
|
|
fields = [int(x) for x in words[start:start + 6]]
|
|
raw = words[start + 6]
|
|
data = b'' if raw == '-' else bytes.fromhex(raw)
|
|
result = self.store(*fields, data=data)
|
|
if result:
|
|
self.slots = saved
|
|
aux = index
|
|
break
|
|
elif kind == 'H':
|
|
value = 2166136261
|
|
for slot, data in enumerate(self.slots):
|
|
for byte in bytes([slot, len(data)]) + data:
|
|
value = ((value ^ byte) * 16777619) & 0xffffffff
|
|
result = value if value < 0x80000000 else value - 0x100000000
|
|
elif kind == 'D':
|
|
if n(1) not in range(4):
|
|
result = -1
|
|
elif n(2) < 1 + len(self.slots[n(1)]):
|
|
result = -2
|
|
else:
|
|
output = bytes([len(self.slots[n(1)])]) + self.slots[n(1)]
|
|
elif kind == 'I':
|
|
raw = h(2)
|
|
if not raw or raw[0] > 128 or len(raw) != raw[0] + 1:
|
|
result = -3
|
|
elif n(1) not in range(4):
|
|
result = -1
|
|
else:
|
|
self.slots[n(1)] = raw[1:]
|
|
elif kind == 'S':
|
|
if n(1) not in range(4):
|
|
result = -1
|
|
elif n(2) > len(self.slots[n(1)]) or n(3) > len(self.slots[n(1)]) - n(2):
|
|
result = -3
|
|
elif n(4) < n(3):
|
|
result = -2
|
|
else:
|
|
output = self.slots[n(1)][n(2):n(2) + n(3)]
|
|
else:
|
|
raise ValueError('model command')
|
|
slots = [(0, len(data), data) for data in self.slots]
|
|
stream = (self.accepted, self.rejected, self.discarded, len(self.pending), self.last)
|
|
return result, aux, output, slots, stream
|
|
|
|
|
|
def suite():
|
|
commands = ['R', 'W 0 010203', 'A 0 04', 'Z 0 133',
|
|
'W 1 050607', 'C 2 1', 'W 3 0809', 'M 0 1', 'K 1', 'Q 0 3',
|
|
'F 0 65 127', 'A 0 4243', 'Z 0 133', 'A 0 44', 'A 0 45',
|
|
'F 1 33 128', 'F 2 99 129', 'W 3 -', 'W -1 ff',
|
|
'P 0 127 ff', 'P 0 128 ff', 'E 0 128 0', 'E 0 129 0',
|
|
'E 0 18446744073709551615 1', 'V 0', 'O 0 129', 'X 0 17', 'K 0',
|
|
'Z 0 132', 'Z -1 133', 'D 1 128', 'D 1 129', 'S 0 124 4 4',
|
|
'S 0 124 5 10', 'S 0 124 4 3', 'I 3 03010203', 'I 3 04010203',
|
|
'H', 'B 10 0 1 0 0 0 -', 'H',
|
|
'J 9 2 0 0 5 19 - 2 1 0 0 1 0 ff',
|
|
'J 9 2 0 0 5 19 - 1 3 0 0 2 0 4142', 'H']
|
|
for raw in (b'', b'\xa7', b'\x00\x00\x00\x00\x00', frame(2, b'xy'),
|
|
frame(3, b'')[:-1] + b'\x00', frame(3, b'')):
|
|
commands.append('U ' + hx(raw))
|
|
commands += ['R', 'T 1122a7ff', 'T 01a70081', 'L']
|
|
stream = frame(1, b'abc') + frame(2, b'\x00\xff') + b'\x23' + frame(3, b'')
|
|
for start, end in ((0, 2), (2, 5), (5, 9), (9, len(stream))):
|
|
commands.append('T ' + hx(stream[start:end]))
|
|
commands += ['T ' + hx(frame(0, b'z')[:-1] + b'\x00'), 'T a700', 'L']
|
|
rng = random.Random(42004)
|
|
for index in range(24):
|
|
slot = rng.randrange(4)
|
|
data = bytes(rng.randrange(16) for _ in range(rng.randrange(7)))
|
|
commands.append('W %d %s' % (slot, hx(data)))
|
|
commands.append(rng.choice(['V %d', 'O %d 3', 'X %d 19', 'K %d']) % slot)
|
|
model = Model()
|
|
expected = [model.command(command) for command in commands]
|
|
return ('\n'.join(commands) + '\n').encode('ascii'), expected
|
|
|
|
|
|
def main():
|
|
scratch = None
|
|
failures = []
|
|
expected = []
|
|
try:
|
|
scratch = tempfile.mkdtemp(prefix='static-alignment-')
|
|
# No chmod of the scratch root: the wrapper already gave it to the candidate UID.
|
|
raw_files = {name: read_candidate(sys.argv[1], name) for name in CANDIDATES}
|
|
for name, raw in raw_files.items():
|
|
(Path(scratch) / name).write_bytes(raw)
|
|
(Path(scratch) / 'observe.c').write_text(OBSERVER)
|
|
input_bytes, expected = suite()
|
|
compiler_argv = ['gcc', '-std=c11', '-O2', '-Wall', '-Wextra', '-Wpedantic',
|
|
'observe.c', 'store.c', 'wire.c', 'stream.c', 'batch.c', '-o', 'observe']
|
|
run_phase('compiler', compiler_argv, scratch, b'', COMPILER_DEADLINE)
|
|
raw_stdout = run_phase('candidate', [scratch + '/observe'], scratch, input_bytes, CANDIDATE_DEADLINE)
|
|
lines = raw_stdout.split(b'\n')
|
|
if lines[-1:] != [b''] or len(lines) != len(expected) + 1:
|
|
raise ValueError('stdout line count')
|
|
for index, (line, wanted) in enumerate(zip(lines[:-1], expected)):
|
|
observed = parse_observation(line)
|
|
if wanted[1] is None:
|
|
observed = observed[0], None, *observed[2:]
|
|
if observed != wanted:
|
|
raise ValueError('normal observation mismatch row %d: %r != %r' % (index, observed, wanted))
|
|
except BaseException as error:
|
|
failures.append(type(error).__name__ + ':' + str(error))
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
run_phase('cleanup', [sys.executable, '-I', '-c', CLEANUP_CODE, scratch], '/', b'', CLEANUP_DEADLINE)
|
|
except BaseException as error:
|
|
failures.append(type(error).__name__ + ':' + str(error))
|
|
try:
|
|
run_phase('absence', [sys.executable, '-I', '-c', ABSENCE_CODE, scratch], '/', b'', ABSENCE_DEADLINE)
|
|
except BaseException as error:
|
|
failures.append(type(error).__name__ + ':' + str(error))
|
|
if failures:
|
|
print('FAIL ' + ' | '.join(failures)[:1000])
|
|
return 1
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|