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>
221 lines
25 KiB
Python
221 lines
25 KiB
Python
HEADER = '#ifndef WORKER_BUNDLE_H\n#define WORKER_BUNDLE_H\n#include <stddef.h>\n#include <stdint.h>\n#define WB_MAX_JOBS 32\n#define WB_MAX_SAMPLES 24\n#define WB_MAX_WIDTH 8\n#define WB_RT_BLOCKS 256\n#define WB_RT_THREADS 32\n#define WB_NOTRUN 0\n#define WB_OK 1\n#define WB_NOMEM 2\n#define WB_BADRESULT 3\n#define WB_START_ERROR 4\n#define WB_INVALID 5\nstruct wb_job {\n int seed;\n int samples;\n int scale;\n int offset;\n int threshold;\n};\nstruct wb_request {\n int count;\n int width;\n int fail_start;\n int fail_alloc;\n struct wb_job jobs[WB_MAX_JOBS];\n};\nstruct wb_row {\n int status;\n int samples;\n int minimum;\n int maximum;\n int below;\n int equal;\n int above;\n int64_t sum;\n int64_t energy;\n uint32_t fingerprint;\n};\nstruct wb_report {\n int status;\n int completed;\n int failed;\n int total_samples;\n int winning_job;\n int64_t sum;\n int64_t energy;\n uint32_t fingerprint;\n struct wb_row rows[WB_MAX_JOBS];\n};\nstruct wb_block {\n void *pointer;\n size_t size;\n};\nstruct wb_thread {\n int used;\n int joined;\n int token;\n void *result;\n};\nstruct wb_runtime {\n struct wb_block blocks[WB_RT_BLOCKS];\n struct wb_thread threads[WB_RT_THREADS];\n int next_token;\n int fail_start;\n int fail_alloc;\n int starts;\n int launched;\n int joined;\n int active;\n int attempts;\n int failure_hit;\n int live;\n int bytes;\n int errors;\n};\ntypedef void *(*wb_worker_fn)(void *);\nvoid wb_runtime_init(struct wb_runtime *, int, int);\nvoid wb_runtime_reset(struct wb_runtime *);\nvoid *wb_alloc(struct wb_runtime *, size_t);\nvoid wb_free(struct wb_runtime *, void *);\nint wb_start(struct wb_runtime *, wb_worker_fn, void *, int *);\nint wb_join(struct wb_runtime *, int, void **);\nint wb_execute(struct wb_runtime *, const struct wb_request *, struct wb_report *);\n#endif\n'
|
|
RUNTIME = '#include "worker_bundle.h"\n#include <stdlib.h>\n#include <string.h>\nstatic int free_block_slot(const struct wb_runtime *rt)\n{\n int i;\n for (i = 0; i < WB_RT_BLOCKS; ++i) {\n if (rt->blocks[i].pointer == NULL) {\n return i;\n }\n }\n return -1;\n}\nstatic int pointer_slot(const struct wb_runtime *rt, const void *pointer)\n{\n int i;\n for (i = 0; i < WB_RT_BLOCKS; ++i) {\n if (rt->blocks[i].pointer == pointer) {\n return i;\n }\n }\n return -1;\n}\nstatic int thread_slot(const struct wb_runtime *rt)\n{\n int i;\n for (i = 0; i < WB_RT_THREADS; ++i) {\n if (!rt->threads[i].used || rt->threads[i].joined) {\n return i;\n }\n }\n return -1;\n}\nstatic int token_slot(const struct wb_runtime *rt, int token)\n{\n int i;\n for (i = 0; i < WB_RT_THREADS; ++i) {\n if (rt->threads[i].used && rt->threads[i].token == token) {\n return i;\n }\n }\n return -1;\n}\nvoid wb_runtime_init(struct wb_runtime *rt, int fail_start, int fail_alloc)\n{\n memset(rt, 0, sizeof(*rt));\n rt->next_token = 1;\n rt->fail_start = fail_start;\n rt->fail_alloc = fail_alloc;\n}\nvoid wb_runtime_reset(struct wb_runtime *rt)\n{\n int i;\n for (i = 0; i < WB_RT_BLOCKS; ++i) {\n if (rt->blocks[i].pointer != NULL) {\n free(rt->blocks[i].pointer);\n rt->blocks[i].pointer = NULL;\n }\n }\n memset(rt, 0, sizeof(*rt));\n}\nvoid *wb_alloc(struct wb_runtime *rt, size_t size)\n{\n int slot;\n void *pointer;\n ++rt->attempts;\n if (rt->fail_alloc > 0 && rt->attempts == rt->fail_alloc) {\n ++rt->failure_hit;\n return NULL;\n }\n if (size == 0 || size > 4096) {\n ++rt->errors;\n return NULL;\n }\n slot = free_block_slot(rt);\n if (slot < 0) {\n ++rt->errors;\n return NULL;\n }\n pointer = calloc(1, size);\n if (pointer == NULL) {\n ++rt->errors;\n return NULL;\n }\n rt->blocks[slot].pointer = pointer;\n rt->blocks[slot].size = size;\n ++rt->live;\n rt->bytes += (int)size;\n return pointer;\n}\nvoid wb_free(struct wb_runtime *rt, void *pointer)\n{\n int slot;\n if (pointer == NULL) {\n return;\n }\n slot = pointer_slot(rt, pointer);\n if (slot < 0) {\n ++rt->errors;\n return;\n }\n free(rt->blocks[slot].pointer);\n rt->bytes -= (int)rt->blocks[slot].size;\n rt->blocks[slot].pointer = NULL;\n rt->blocks[slot].size = 0;\n --rt->live;\n}\nint wb_start(struct wb_runtime *rt, wb_worker_fn worker, void *argument, int *token)\n{\n int slot;\n struct wb_thread *thread;\n ++rt->starts;\n if (rt->fail_start > 0 && rt->starts == rt->fail_start) {\n return -1;\n }\n slot = thread_slot(rt);\n if (slot < 0 || worker == NULL || token == NULL) {\n ++rt->errors;\n return -1;\n }\n thread = &rt->threads[slot];\n thread->used = 1;\n thread->joined = 0;\n thread->token = rt->next_token++;\n thread->result = worker(argument);\n *token = thread->token;\n ++rt->launched;\n ++rt->active;\n return 0;\n}\nint wb_join(struct wb_runtime *rt, int token, void **result)\n{\n int slot = token_slot(rt, token);\n struct wb_thread *thread;\n if (slot < 0 || result == NULL) {\n ++rt->errors;\n return -1;\n }\n thread = &rt->threads[slot];\n if (thread->joined) {\n ++rt->errors;\n return -1;\n }\n *result = thread->result;\n thread->result = NULL;\n thread->joined = 1;\n ++rt->joined;\n --rt->active;\n return 0;\n}\n'
|
|
OBSERVER = '#include "worker_bundle.h"\n#include <stdio.h>\n#include <inttypes.h>\nint main(void)\n{\n int cases, c;\n if (scanf("%d", &cases) != 1) { return 2; }\n for (c = 0; c < cases; ++c) {\n struct wb_request request = {0};\n struct wb_report report;\n struct wb_runtime runtime;\n int i, result;\n if (scanf("%d%d%d%d", &request.count, &request.width,\n &request.fail_start, &request.fail_alloc) != 4) { return 2; }\n for (i = 0; i < request.count && i < WB_MAX_JOBS; ++i) {\n struct wb_job *job = &request.jobs[i];\n if (scanf("%d%d%d%d%d", &job->seed, &job->samples,\n &job->scale, &job->offset, &job->threshold) != 5) { return 2; }\n }\n wb_runtime_init(&runtime, request.fail_start, request.fail_alloc);\n result = wb_execute(&runtime, &request, &report);\n printf("B %d %d %d %d %d %d %d %" PRId64 " %" PRId64 " %" PRIu32 "\\n",\n c, result, report.status, report.completed, report.failed,\n report.total_samples, report.winning_job, report.sum,\n report.energy, report.fingerprint);\n for (i = 0; i < request.count && i < WB_MAX_JOBS; ++i) {\n struct wb_row *row = &report.rows[i];\n printf("R %d %d %d %d %d %d %d %d %d %" PRId64 " %" PRId64 " %" PRIu32 "\\n",\n c, i, row->status, row->samples, row->minimum, row->maximum,\n row->below, row->equal, row->above, row->sum,\n row->energy, row->fingerprint);\n }\n int blocks = 0, block_bytes = 0, handles = 0;\n for (i = 0; i < WB_RT_BLOCKS; ++i) {\n if (runtime.blocks[i].pointer != NULL) {\n ++blocks;\n block_bytes += (int)runtime.blocks[i].size;\n }\n }\n for (i = 0; i < WB_RT_THREADS; ++i) {\n handles += runtime.threads[i].used && !runtime.threads[i].joined;\n }\n printf("T %d %d %d %d %d %d %d %d %d %d %d %d %d\\n", c,\n runtime.starts, runtime.launched, runtime.joined, runtime.active,\n runtime.attempts, runtime.failure_hit, runtime.live,\n runtime.bytes, runtime.errors, blocks, block_bytes, handles);\n wb_runtime_reset(&runtime);\n }\n return 0;\n}\n'
|
|
SUPERVISOR = "import base64, ctypes, hashlib, json, os, selectors, signal, subprocess, sys, time\nphase, deadline_text, nonce, argv_json = sys.argv[1:]\nargv = json.loads(argv_json)\ndeadline = float(deadline_text)\nstarted = time.monotonic()\nerrors = []\ntracked = {}\nreaped = []\ndef snapshot():\n result = {}\n for name in os.listdir('/proc'):\n if not name.isdecimal():\n continue\n try:\n raw = open('/proc/' + name + '/stat', 'rb').read()\n fields = raw[raw.rfind(b')')+2:].split()\n result[int(name)] = (int(fields[1]), int(fields[2]), int(fields[19]), fields[0].decode())\n except (FileNotFoundError, ProcessLookupError):\n continue\n return result\nself_pid = os.getpid()\ntry:\n if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0:\n raise OSError(ctypes.get_errno(), 'prctl subreaper')\n self_start = snapshot()[self_pid][2]\nexcept BaseException as exc:\n errors.append('subreaper:' + repr(exc))\n self_start = -1\nleader = None\nleader_id = None\nout = bytearray()\nerr = bytearray()\ntimed_out = False\nleader_rc = None\nleader_reaped = False\nselector = selectors.DefaultSelector()\ndef discover():\n table = snapshot()\n root = table.get(self_pid)\n if root is None or root[2] != self_start:\n raise RuntimeError('supervisor identity changed')\n # Only live PID/start matches can supply ancestry. Dead/reused tracked PIDs are never roots.\n live_roots = {self_pid}\n for pid, birth in tracked.items():\n current = table.get(pid)\n if current is not None and current[2] == birth and current[3] not in ('Z', 'X'):\n live_roots.add(pid)\n changed = True\n while changed:\n changed = False\n for pid, item in table.items():\n if pid != self_pid and item[0] in live_roots and pid not in live_roots:\n tracked[pid] = item[2]\n live_roots.add(pid)\n changed = True\n return table\n\ndef own_poll():\n global leader_rc, leader_reaped\n if leader is not None:\n code = leader.poll()\n if code is not None:\n leader_rc = code\n leader_reaped = True\n\ndef adopted_reap():\n # Never steal own Popen's wait status, including a just-exited leader.\n own_poll()\n if leader is not None and not leader_reaped:\n return\n while True:\n try:\n pid, status = os.waitpid(-1, os.WNOHANG)\n except ChildProcessError:\n break\n if pid == 0:\n break\n reaped.append([pid, status])\n\ndef kill_known(table):\n for pid, birth in list(tracked.items()):\n current = table.get(pid)\n if current is None or current[2] != birth:\n continue\n try:\n # Fresh stat immediately before kill, rather than trusting an old closure.\n fresh = snapshot().get(pid)\n if fresh is not None and fresh[2] == birth:\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n except BaseException as exc:\n errors.append('kill:' + repr(exc))\n\ntry:\n leader = subprocess.Popen(argv, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE,\n start_new_session=True)\n first = snapshot().get(leader.pid)\n if first is None:\n raise RuntimeError('leader not observable')\n leader_id = [leader.pid, first[2]]\n tracked[leader.pid] = first[2]\n for stream, label in ((leader.stdout, 'out'), (leader.stderr, 'err')):\n os.set_blocking(stream.fileno(), False)\n selector.register(stream, selectors.EVENT_READ, label)\n while True:\n table = discover()\n own_poll()\n adopted_reap()\n live = [pid for pid, birth in tracked.items()\n if pid in table and table[pid][2] == birth]\n if time.monotonic() - started >= deadline and (not leader_reaped or selector.get_map()):\n timed_out = True\n # Reclaim descendants even after a successful leader closes all pipes.\n if timed_out or leader_reaped:\n kill_known(table)\n for key, mask in selector.select(0.01):\n chunk = os.read(key.fileobj.fileno(), 65536)\n if chunk:\n (out if key.data == 'out' else err).extend(chunk)\n else:\n selector.unregister(key.fileobj)\n key.fileobj.close()\n if leader_reaped and not live and not selector.get_map():\n break\n if time.monotonic() - started > deadline + 3:\n errors.append('reclamation-deadline')\n break\n own_poll()\n adopted_reap()\nexcept BaseException as exc:\n errors.append('executor:' + repr(exc))\nfinally:\n try:\n table = discover()\n kill_known(table)\n if leader is not None:\n if leader.poll() is None:\n try:\n leader.wait(timeout=1)\n except BaseException as exc:\n errors.append('leader-wait:' + repr(exc))\n own_poll()\n adopted_reap()\n table = discover()\n survivors = [[pid, birth] for pid, birth in tracked.items()\n if pid in table and table[pid][2] == birth]\n # Independent process-group absence is evaluated from the current snapshot.\n pg_survivors = [] if leader_id is None else [[pid, entry[2]] for pid, entry in table.items()\n if entry[1] == leader_id[0] and tracked.get(pid) == entry[2]]\n except BaseException as exc:\n errors.append('final:' + repr(exc))\n survivors = None\n pg_survivors = None\nmeta = dict(phase=phase, nonce=nonce, argv=argv, deadline=deadline,\n identity=[os.getuid(), os.getgid(), os.getgroups()],\n supervisor=[self_pid, self_start], leader=leader_id,\n leader_rc=leader_rc, leader_reaped=leader_reaped, timed_out=timed_out,\n descendants_absent=survivors == [], pg_absent=pg_survivors == [],\n survivors=survivors, pg_survivors=pg_survivors, errors=errors,\n reaped=reaped, tracked=sorted([[p,b] for p,b in tracked.items()]),\n elapsed=round(time.monotonic()-started, 6),\n stdout_len=len(out), stdout_sha=hashlib.sha256(out).hexdigest(),\n stderr_len=len(err), stderr_sha=hashlib.sha256(err).hexdigest())\nwire = dict(meta=meta, stdout=base64.b64encode(out).decode(), stderr=base64.b64encode(err).decode())\nsys.stdout.buffer.write(json.dumps(wire, separators=(',', ':')).encode())\nsys.stdout.buffer.flush()\nsys.exit(125 if errors else (124 if timed_out else 0))\n"
|
|
ABSENCE = 'import os,sys\ntry:\n os.lstat(sys.argv[1])\nexcept FileNotFoundError:\n sys.exit(0)\nsys.exit(1)\n'
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import uuid
|
|
import zlib
|
|
|
|
# Public support snapshots, supervisor and all input are prepared before the first child.
|
|
PHASES = {'compiler': 25, 'candidate': 15, 'cleanup': 10, 'absence': 5}
|
|
OBSERVATIONS = []
|
|
EXCEPTIONS = []
|
|
|
|
def mix(state, word):
|
|
return ((state ^ (word & 0xffffffff)) * 16777619) & 0xffffffff
|
|
|
|
def row_model(job):
|
|
seed, n, scale, offset, threshold = job
|
|
values = [max(-200, min(200, (((seed * 17 + k * 13) % 101) - 50) * scale + offset)) for k in range(n)]
|
|
fp = 2166136261
|
|
for value in values:
|
|
fp = mix(fp, value)
|
|
return [1, n, min(values, default=0), max(values, default=0),
|
|
sum(v < threshold for v in values), sum(v == threshold for v in values),
|
|
sum(v > threshold for v in values), sum(values), sum(v*v for v in values), fp]
|
|
|
|
def valid_case(case):
|
|
n, width, fs, fa, jobs = case
|
|
return (0 <= n <= 32 and 1 <= width <= 8 and 0 <= fs <= 33 and 0 <= fa <= 65 and
|
|
all(0 <= s <= 10000 and 0 <= c <= 24 and -8 <= m <= 8 and -300 <= a <= 300 and -200 <= t <= 200
|
|
for s,c,m,a,t in jobs))
|
|
|
|
def expected_case(index, case):
|
|
n, width, fs, fa, jobs = case
|
|
empty = [0,0,0,0,0,0,0,0,0,2166136261]
|
|
rows = [empty[:] for _ in range(max(0,n))]
|
|
starts = launched = attempts = hit = 0
|
|
batch_status = 1
|
|
if not valid_case(case):
|
|
batch_status = 5
|
|
else:
|
|
for i, job in enumerate(jobs):
|
|
starts += 1
|
|
if starts == fs:
|
|
rows[i][0] = 4
|
|
batch_status = 4
|
|
break
|
|
launched += 1
|
|
failed = False
|
|
for _ in range(1 + (job[1] > 0)):
|
|
attempts += 1
|
|
if attempts == fa:
|
|
hit += 1
|
|
failed = True
|
|
break
|
|
if failed:
|
|
rows[i][0] = 2
|
|
else:
|
|
rows[i] = row_model(job)
|
|
if batch_status != 4 and any(row[0] == 2 for row in rows):
|
|
batch_status = 2
|
|
successes = [i for i,row in enumerate(rows) if row[0] == 1]
|
|
winner = min(successes, key=lambda i:(-rows[i][7], rows[i][8], i), default=-1)
|
|
fp = 2166136261
|
|
if batch_status != 5:
|
|
for i,row in enumerate(rows):
|
|
fp = mix(mix(fp,i), row[0])
|
|
if row[0] == 1:
|
|
fp = mix(fp,row[9])
|
|
lines = ['B '+ ' '.join(map(str,[index,batch_status,batch_status,len(successes),
|
|
sum(row[0] in (2,3,4) for row in rows),sum(rows[i][1] for i in successes),winner,
|
|
sum(rows[i][7] for i in successes),sum(rows[i][8] for i in successes),fp]))]
|
|
lines += ['R '+ ' '.join(map(str,[index,i,*row])) for i,row in enumerate(rows)]
|
|
lines += ['T '+ ' '.join(map(str,[index,starts,launched,launched,0,attempts,hit,0,0,0,0,0,0]))]
|
|
return lines
|
|
|
|
def make_cases():
|
|
rng = random.Random(9173107)
|
|
cases = []
|
|
demo = [(7,5,2,-3,0),(42,3,1,10,20)]
|
|
cases.append((2,2,0,0,demo))
|
|
for width in (1,2,4,8):
|
|
for n in (0,1,3,8,17,32):
|
|
jobs = [(rng.randrange(10001),rng.randrange(25),rng.randrange(-8,9),rng.randrange(-300,301),rng.randrange(-200,201)) for _ in range(n)]
|
|
cases.append((n,width,0,0,jobs))
|
|
jobs = [(i*31, i%5, (i%7)-3, 40-i*7, (i%3)*100-100) for i in range(12)]
|
|
for width in (1,2,3,8):
|
|
for fs in (1,2,3,7,12,13):
|
|
cases.append((12,width,fs,0,jobs))
|
|
for fa in range(1,24):
|
|
cases.append((12,width,0,fa,jobs))
|
|
for _ in range(45):
|
|
n = rng.randrange(1,33)
|
|
js = [(rng.randrange(10001),rng.randrange(25),rng.randrange(-8,9),rng.randrange(-300,301),rng.randrange(-200,201)) for _ in range(n)]
|
|
cases.append((n,rng.randrange(1,9),rng.randrange(n+2),rng.randrange(2*n+2),js))
|
|
for changed in ((0,0,0,0,[]),(0,9,0,0,[]),(0,1,-1,0,[]),(0,1,34,0,[]),(0,1,0,66,[])):
|
|
cases.append(changed)
|
|
for field, bad in ((0,-1),(0,10001),(1,-1),(1,25),(2,-9),(2,9),(3,-301),(3,301),(4,-201),(4,201)):
|
|
job = [0,1,1,0,0]
|
|
job[field] = bad
|
|
cases.append((2,2,0,0,[(7,2,1,0,0),tuple(job)]))
|
|
return cases
|
|
|
|
def digest(raw):
|
|
return hashlib.sha256(raw).hexdigest()
|
|
|
|
def execute(phase, argv, stdin=b''):
|
|
nonce = uuid.uuid4().hex
|
|
deadline = PHASES[phase]
|
|
completed = subprocess.run([sys.executable,'-I','-c',SUPERVISOR,phase,str(deadline),nonce,json.dumps(argv)],
|
|
input=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
# Persist actual wrapper facts in the terminal record before attempting wire parsing.
|
|
record = {'phase':phase,'nonce':nonce,'deadline':deadline,'argv':argv,
|
|
'wrapper_rc':completed.returncode,'wire_len':len(completed.stdout),
|
|
'wire_sha':digest(completed.stdout),'wrapper_stderr_len':len(completed.stderr),
|
|
'wrapper_stderr_sha':digest(completed.stderr)}
|
|
OBSERVATIONS.append(record)
|
|
wire = json.loads(completed.stdout)
|
|
meta = wire['meta']
|
|
record['meta'] = meta
|
|
out = base64.b64decode(wire['stdout'],validate=True)
|
|
err = base64.b64decode(wire['stderr'],validate=True)
|
|
required = {'phase','nonce','argv','deadline','identity','supervisor','leader','leader_rc',
|
|
'leader_reaped','timed_out','descendants_absent','pg_absent','survivors',
|
|
'pg_survivors','errors','reaped','tracked','elapsed','stdout_len','stdout_sha',
|
|
'stderr_len','stderr_sha'}
|
|
if not required.issubset(meta):
|
|
raise ValueError('missing-metadata-fields')
|
|
expected_identity = [65534,65534,[]] if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else [os.getuid(),os.getgid(),os.getgroups()]
|
|
consistent = (completed.returncode == 0 and completed.stderr == b'' and
|
|
meta['phase'] == phase and meta['nonce'] == nonce and meta['argv'] == argv and
|
|
meta['deadline'] == deadline and meta['identity'] == expected_identity and
|
|
meta['leader_reaped'] is True and meta['timed_out'] is False and
|
|
meta['descendants_absent'] is True and meta['pg_absent'] is True and
|
|
meta['survivors'] == [] and meta['pg_survivors'] == [] and meta['errors'] == [] and
|
|
type(meta['leader_rc']) is int and meta['leader_rc'] == 0 and
|
|
isinstance(meta['leader'],list) and len(meta['leader']) == 2 and
|
|
all(type(v) is int and v > 0 for v in meta['leader']) and
|
|
isinstance(meta['supervisor'],list) and len(meta['supervisor']) == 2 and
|
|
all(type(v) is int and v > 0 for v in meta['supervisor']) and
|
|
meta['supervisor'][0] != meta['leader'][0] and
|
|
isinstance(meta['tracked'],list) and meta['leader'] in meta['tracked'] and
|
|
isinstance(meta['reaped'],list) and type(meta['elapsed']) in (float,int) and
|
|
math.isfinite(meta['elapsed']) and 0 <= meta['elapsed'] <= deadline + 4 and
|
|
type(meta['stdout_len']) is int and meta['stdout_len'] >= 0 and
|
|
type(meta['stderr_len']) is int and meta['stderr_len'] >= 0 and
|
|
meta['stdout_len'] == len(out) and meta['stdout_sha'] == digest(out) and
|
|
meta['stderr_len'] == len(err) and meta['stderr_sha'] == digest(err))
|
|
if not consistent:
|
|
raise ValueError('phase-rejected:'+phase)
|
|
return out
|
|
|
|
def main():
|
|
scratch = None
|
|
success = False
|
|
cleaned = False
|
|
absent = False
|
|
failure = ''
|
|
cases = make_cases()
|
|
expected = [line for i,case in enumerate(cases) for line in expected_case(i,case)]
|
|
stdin = (str(len(cases))+'\n'+''.join(' '.join(map(str,case[:4]))+'\n'+''.join(' '.join(map(str,j))+'\n' for j in case[4]) for case in cases)).encode()
|
|
try:
|
|
scratch = Path(tempfile.mkdtemp(prefix='rwb-grade-'))
|
|
source = Path(sys.argv[1])/'src/bundle.c'
|
|
# All source/support/input preparation is complete before first child.
|
|
(scratch/'bundle.c').write_bytes(source.read_bytes())
|
|
(scratch/'worker_bundle.h').write_text(HEADER)
|
|
(scratch/'runtime.c').write_text(RUNTIME)
|
|
(scratch/'observe.c').write_text(OBSERVER)
|
|
for file in ('bundle.c','worker_bundle.h','runtime.c','observe.c'):
|
|
os.chmod(scratch/file,0o644)
|
|
binary = str(scratch/'observe')
|
|
compile_argv = ['gcc','-std=c11','-O2','-Wall','-Wextra','-I'+str(scratch),str(scratch/'bundle.c'),str(scratch/'runtime.c'),str(scratch/'observe.c'),'-o',binary]
|
|
execute('compiler',compile_argv)
|
|
raw = execute('candidate',[binary],stdin)
|
|
# stdout is the public ASCII decimal protocol; stderr is arbitrary bytes and ignored.
|
|
actual = raw.decode('ascii').splitlines()
|
|
if actual != expected:
|
|
first = next((i for i,(left,right) in enumerate(zip(actual,expected)) if left != right), min(len(actual),len(expected)))
|
|
left = actual[first] if first < len(actual) else '<missing>'
|
|
right = expected[first] if first < len(expected) else '<end>'
|
|
failure = 'observation '+str(first)+': actual='+repr(left)+' expected='+repr(right)
|
|
else:
|
|
success = True
|
|
except BaseException as exc:
|
|
EXCEPTIONS.append(['main',repr(exc)])
|
|
failure = 'execution:'+repr(exc)
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
execute('cleanup',[sys.executable,'-I','-c','import shutil,sys;shutil.rmtree(sys.argv[1])',str(scratch)])
|
|
cleaned = True
|
|
except BaseException as exc:
|
|
EXCEPTIONS.append(['cleanup',repr(exc)])
|
|
try:
|
|
execute('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)])
|
|
absent = True
|
|
except BaseException as exc:
|
|
EXCEPTIONS.append(['absence',repr(exc)])
|
|
terminal = {'scratch':str(scratch),'success':success,'cleaned':cleaned,'absent':absent,
|
|
'exceptions':EXCEPTIONS,'phases':OBSERVATIONS,'rows':len(expected),
|
|
'input_len':len(stdin),'input_sha':digest(stdin)}
|
|
encoded = base64.b64encode(zlib.compress(json.dumps(terminal,separators=(',',':')).encode(),9)).decode()
|
|
sys.stderr.write('RWB_Z '+encoded+'\n')
|
|
okay = success and cleaned and absent and not EXCEPTIONS
|
|
print('PASS' if okay else 'FAIL '+(failure or repr(EXCEPTIONS)))
|
|
return 0 if okay else 1
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|