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>
268 lines
33 KiB
Python
268 lines
33 KiB
Python
SUPPORT = {'include/frame.hpp': '#ifndef CAPTURE_FRAME_HPP\n#define CAPTURE_FRAME_HPP\n#include <array>\n#include <cstddef>\n#include <cstdint>\n\nnamespace capture {\nconstexpr std::size_t Capacity = 16;\nenum class Status : int {\n Ok = 0,\n Empty = 1,\n BadShape = 2,\n BadScale = 3\n};\nenum class Domain : std::size_t {\n Frame = 0,\n Layout = 1\n};\nenum Register : std::size_t {\n Sequence = 0,\n Present = 1,\n Revision = 2,\n TickLow = 3,\n TickHigh = 4,\n Rows = 5,\n Columns = 6,\n Scale = 7,\n Tag = 8,\n Payload = 9,\n RegisterCount = Payload + Capacity\n};\nstruct Frame {\n std::uint32_t revision = 0;\n std::uint64_t tick = 0;\n std::uint32_t rows = 0;\n std::uint32_t columns = 0;\n std::int32_t scale = 0;\n std::uint32_t tag = 0;\n std::array<std::int32_t, Capacity> samples{};\n};\nstruct Summary {\n std::size_t count = 0;\n std::int64_t sum = 0;\n std::int32_t minimum = 0;\n std::int32_t maximum = 0;\n std::uint64_t checksum = 0;\n};\nclass Port {\npublic:\n virtual ~Port() = default;\n virtual std::uint32_t load(std::size_t word) = 0;\n virtual void store(std::size_t word, std::uint32_t value) = 0;\n virtual void lock(Domain domain) = 0;\n virtual void unlock(Domain domain) = 0;\n};\nclass Guard {\npublic:\n Guard(Port& port, Domain domain);\n ~Guard();\n Guard(const Guard&) = delete;\n Guard& operator=(const Guard&) = delete;\nprivate:\n Port& port_;\n Domain domain_;\n};\nStatus validate(const Frame& frame);\nstd::size_t active_count(const Frame& frame);\nSummary summarize(const Frame& frame);\nbool same_frame(const Frame& left, const Frame& right);\nclass FrameStore {\npublic:\n explicit FrameStore(Port& port);\n Status publish(const Frame& frame);\n Status snapshot(Frame& result);\nprivate:\n Port& port_;\n};\n}\n#endif\n', 'include/memory_port.hpp': '#ifndef CAPTURE_MEMORY_PORT_HPP\n#define CAPTURE_MEMORY_PORT_HPP\n#include "frame.hpp"\n#include <atomic>\n#include <functional>\n#include <mutex>\n\nnamespace capture {\nstruct Access {\n std::size_t word;\n bool write;\n std::uint32_t value;\n};\nclass MemoryPort final : public Port {\npublic:\n using AccessHook = std::function<void(Access)>;\n using LockHook = std::function<void(Domain, bool)>;\n MemoryPort();\n std::uint32_t load(std::size_t word) override;\n void store(std::size_t word, std::uint32_t value) override;\n void lock(Domain domain) override;\n void unlock(Domain domain) override;\n void access_hook(AccessHook hook);\n void lock_hook(LockHook hook);\nprivate:\n void notify(Access access);\n std::array<std::atomic<std::uint32_t>, RegisterCount> words_;\n std::array<std::mutex, 2> domains_;\n std::mutex hooks_mutex_;\n AccessHook access_;\n LockHook lock_;\n};\n}\n#endif\n', 'include/replay.hpp': '#ifndef CAPTURE_REPLAY_HPP\n#define CAPTURE_REPLAY_HPP\n#include "memory_port.hpp"\n#include <condition_variable>\n#include <exception>\n#include <thread>\n\nnamespace capture {\nclass ReadInterleave {\npublic:\n ReadInterleave(MemoryPort& port, FrameStore& store, Frame pending, std::size_t cut);\n ~ReadInterleave();\n Status finish();\n ReadInterleave(const ReadInterleave&) = delete;\n ReadInterleave& operator=(const ReadInterleave&) = delete;\nprivate:\n void on_access(Access access);\n void on_lock(Domain domain, bool blocked);\n void launch();\n void run();\n MemoryPort& port_;\n FrameStore& store_;\n Frame pending_;\n std::size_t cut_;\n std::size_t loads_ = 0;\n std::thread::id owner_;\n std::thread worker_;\n std::mutex state_mutex_;\n std::condition_variable state_changed_;\n bool started_ = false;\n bool attempted_ = false;\n bool blocked_ = false;\n bool done_ = false;\n bool finished_ = false;\n Status status_ = Status::Empty;\n std::exception_ptr failure_;\n};\n}\n#endif\n', 'src/cli.cpp': '#include "frame.hpp"\n#include "memory_port.hpp"\n#include "replay.hpp"\n#include <iostream>\n#include <limits>\n#include <sstream>\n#include <stdexcept>\n#include <string>\n#include <vector>\n\nnamespace {\nusing namespace capture;\nstd::string token(std::istream& input) {\n std::string result;\n if (!(input >> result)) {\n throw std::runtime_error("missing token");\n }\n return result;\n}\nstd::uint64_t unsigned_number(std::istream& input, std::uint64_t maximum) {\n const std::string text = token(input);\n if (text.empty() || text[0] == \'-\') {\n throw std::runtime_error("unsigned number");\n }\n std::size_t used = 0;\n const auto value = std::stoull(text, &used, 10);\n if (used != text.size() || value > maximum) {\n throw std::runtime_error("unsigned range");\n }\n return value;\n}\nstd::int32_t signed_number(std::istream& input) {\n const std::string text = token(input);\n std::size_t used = 0;\n const auto value = std::stoll(text, &used, 10);\n if (used != text.size() || value < INT32_MIN || value > INT32_MAX) {\n throw std::runtime_error("signed range");\n }\n return static_cast<std::int32_t>(value);\n}\nFrame read_frame(std::istream& input) {\n Frame frame;\n frame.revision = static_cast<std::uint32_t>(unsigned_number(input, UINT32_MAX));\n frame.tick = unsigned_number(input, UINT64_MAX);\n frame.rows = static_cast<std::uint32_t>(unsigned_number(input, UINT32_MAX));\n frame.columns = static_cast<std::uint32_t>(unsigned_number(input, UINT32_MAX));\n frame.scale = signed_number(input);\n frame.tag = static_cast<std::uint32_t>(unsigned_number(input, UINT32_MAX));\n std::size_t count = 0;\n if (frame.rows <= 4 && frame.columns <= 4) {\n count = static_cast<std::size_t>(frame.rows) * frame.columns;\n }\n for (std::size_t i = 0; i < count; ++i) {\n frame.samples[i] = signed_number(input);\n }\n return frame;\n}\nvoid end_of_line(std::istream& input) {\n std::string extra;\n if (input >> extra) {\n throw std::runtime_error("trailing token");\n }\n}\nvoid print_frame(Status status, const Frame& frame) {\n std::cout << "F " << static_cast<int>(status);\n if (status == Status::Ok) {\n std::cout << \' \' << frame.revision << \' \' << frame.tick;\n std::cout << \' \' << frame.rows << \' \' << frame.columns;\n std::cout << \' \' << frame.scale << \' \' << frame.tag;\n const auto count = active_count(frame);\n for (std::size_t i = 0; i < count; ++i) {\n std::cout << \' \' << frame.samples[i];\n }\n const auto summary = summarize(frame);\n std::cout << " | " << summary.count << \' \' << summary.sum;\n std::cout << \' \' << summary.minimum << \' \' << summary.maximum;\n std::cout << \' \' << summary.checksum;\n }\n std::cout << \'\\n\';\n}\nvoid print_publish(Status status) {\n std::cout << "P " << static_cast<int>(status) << \'\\n\';\n}\nvoid print_bank(MemoryPort& port) {\n const auto present = port.load(Present);\n const auto revision = port.load(Revision);\n const auto low = port.load(TickLow);\n const auto high = port.load(TickHigh);\n const auto rows = port.load(Rows);\n const auto columns = port.load(Columns);\n const auto scale = port.load(Scale);\n const auto tag = port.load(Tag);\n std::cout << "B " << present << \' \' << revision << \' \' << low << \' \' << high;\n std::cout << \' \' << rows << \' \' << columns << \' \' << scale << \' \' << tag;\n const auto count = rows <= 4 && columns <= 4 ? rows * columns : 0;\n for (std::size_t i = 0; i < count; ++i) {\n std::cout << \' \' << port.load(Payload + i);\n }\n std::cout << \'\\n\';\n}\nFrame marker() {\n Frame frame;\n frame.revision = 0x11223344;\n frame.tick = UINT64_C(0x1020304050607080);\n frame.rows = 4;\n frame.columns = 4;\n frame.scale = -33;\n frame.tag = 0x99887766;\n for (std::size_t i = 0; i < Capacity; ++i) {\n frame.samples[i] = static_cast<std::int32_t>(71 + i * 19);\n }\n return frame;\n}\nvoid serial(const std::vector<Frame>& frames) {\n MemoryPort port;\n FrameStore store(port);\n Frame output = marker();\n const Frame before = output;\n const auto empty = store.snapshot(output);\n std::cout << "E " << static_cast<int>(empty) << \' \';\n std::cout << (same_frame(output, before) ? 1 : 0);\n std::cout << \' \' << output.revision << \' \' << output.tick;\n std::cout << \' \' << output.rows << \' \' << output.columns;\n std::cout << \' \' << output.scale << \' \' << output.tag;\n for (const auto value : output.samples) {\n std::cout << \' \' << value;\n }\n std::cout << \'\\n\';\n for (const auto& frame : frames) {\n print_publish(store.publish(frame));\n print_bank(port);\n const auto status = store.snapshot(output);\n print_frame(status, output);\n }\n}\nvoid race(const Frame& before, const Frame& after, std::size_t cut) {\n MemoryPort port;\n FrameStore store(port);\n print_publish(store.publish(before));\n print_bank(port);\n ReadInterleave interleave(port, store, after, cut);\n Frame output = marker();\n const auto status = store.snapshot(output);\n print_frame(status, output);\n print_publish(interleave.finish());\n print_bank(port);\n const auto final_status = store.snapshot(output);\n print_frame(final_status, output);\n}\nvoid request(const std::string& line) {\n std::istringstream input(line);\n const auto command = token(input);\n if (command == "serial") {\n const auto count = unsigned_number(input, 128);\n std::vector<Frame> frames;\n frames.reserve(static_cast<std::size_t>(count));\n for (std::uint64_t i = 0; i < count; ++i) {\n frames.push_back(read_frame(input));\n }\n end_of_line(input);\n serial(frames);\n } else if (command == "race") {\n const auto cut = unsigned_number(input, 128);\n if (cut == 0) {\n throw std::runtime_error("zero cut");\n }\n const auto before = read_frame(input);\n const auto after = read_frame(input);\n end_of_line(input);\n if (validate(before) != Status::Ok || validate(after) != Status::Ok) {\n throw std::runtime_error("race frame");\n }\n race(before, after, static_cast<std::size_t>(cut));\n } else {\n throw std::runtime_error("command");\n }\n}\nint demo(const std::string& name) {\n Frame before;\n before.revision = 10;\n before.tick = 1000;\n before.rows = 2;\n before.columns = 2;\n before.scale = 1;\n before.tag = 7;\n before.samples = {10, 20, 30, 40};\n Frame after = before;\n after.revision = 11;\n after.tick = UINT64_C(4294967297);\n after.tag = 8;\n after.samples = {-1, -2, -3, -4};\n if (name == "resize") {\n after.rows = 1;\n after.columns = 3;\n } else if (name != "stable") {\n throw std::runtime_error("demo name");\n }\n race(before, after, 2);\n return 0;\n}\n}\nint main(int argc, char** argv) {\n try {\n if (argc == 3 && std::string(argv[1]) == "--demo") {\n return demo(argv[2]);\n }\n if (argc != 1) {\n throw std::runtime_error("arguments");\n }\n std::string line;\n while (std::getline(std::cin, line)) {\n if (line.size() > 8192) {\n throw std::runtime_error("line length");\n }\n const auto start = line.find_first_not_of(" \\t\\r");\n if (start == std::string::npos || line[start] == \'#\') {\n continue;\n }\n request(line);\n }\n if (std::cin.bad()) {\n throw std::runtime_error("input read");\n }\n return 0;\n } catch (const std::exception& error) {\n std::cerr << "input error: " << error.what() << \'\\n\';\n return 2;\n }\n}\n', 'src/memory_port.cpp': '#include "memory_port.hpp"\n#include <stdexcept>\n#include <utility>\n\nnamespace capture {\nMemoryPort::MemoryPort() {\n for (auto& word : words_) {\n word.store(0);\n }\n}\nvoid MemoryPort::notify(Access access) {\n AccessHook hook;\n {\n std::lock_guard<std::mutex> guard(hooks_mutex_);\n hook = access_;\n }\n if (hook) {\n hook(access);\n }\n}\nstd::uint32_t MemoryPort::load(std::size_t word) {\n if (word >= words_.size()) {\n throw std::out_of_range("register load");\n }\n const auto value = words_[word].load();\n notify({word, false, value});\n return value;\n}\nvoid MemoryPort::store(std::size_t word, std::uint32_t value) {\n if (word >= words_.size()) {\n throw std::out_of_range("register store");\n }\n words_[word].store(value);\n notify({word, true, value});\n}\nvoid MemoryPort::lock(Domain domain) {\n const auto index = static_cast<std::size_t>(domain);\n if (index >= domains_.size()) {\n throw std::out_of_range("mutex domain");\n }\n auto& mutex = domains_[index];\n const bool blocked = !mutex.try_lock();\n LockHook hook;\n {\n std::lock_guard<std::mutex> guard(hooks_mutex_);\n hook = lock_;\n }\n if (hook) {\n hook(domain, blocked);\n }\n if (blocked) {\n mutex.lock();\n }\n}\nvoid MemoryPort::unlock(Domain domain) {\n const auto index = static_cast<std::size_t>(domain);\n if (index >= domains_.size()) {\n throw std::out_of_range("mutex domain");\n }\n domains_[index].unlock();\n}\nvoid MemoryPort::access_hook(AccessHook hook) {\n std::lock_guard<std::mutex> guard(hooks_mutex_);\n access_ = std::move(hook);\n}\nvoid MemoryPort::lock_hook(LockHook hook) {\n std::lock_guard<std::mutex> guard(hooks_mutex_);\n lock_ = std::move(hook);\n}\n}\n', 'src/replay.cpp': '#include "replay.hpp"\n#include <utility>\n\nnamespace capture {\nReadInterleave::ReadInterleave(MemoryPort& port, FrameStore& store,\n Frame pending, std::size_t cut)\n : port_(port), store_(store), pending_(std::move(pending)), cut_(cut),\n owner_(std::this_thread::get_id()) {\n port_.access_hook([this](Access access) { on_access(access); });\n port_.lock_hook([this](Domain domain, bool blocked) { on_lock(domain, blocked); });\n}\nReadInterleave::~ReadInterleave() {\n if (!finished_) {\n try {\n finish();\n } catch (...) {\n }\n }\n}\nvoid ReadInterleave::launch() {\n if (!started_) {\n started_ = true;\n worker_ = std::thread([this] { run(); });\n }\n}\nvoid ReadInterleave::run() {\n try {\n const auto result = store_.publish(pending_);\n std::lock_guard<std::mutex> guard(state_mutex_);\n status_ = result;\n } catch (...) {\n std::lock_guard<std::mutex> guard(state_mutex_);\n failure_ = std::current_exception();\n }\n {\n std::lock_guard<std::mutex> guard(state_mutex_);\n done_ = true;\n }\n state_changed_.notify_all();\n}\nvoid ReadInterleave::on_lock(Domain, bool blocked) {\n if (std::this_thread::get_id() == owner_) {\n return;\n }\n {\n std::lock_guard<std::mutex> guard(state_mutex_);\n if (!attempted_) {\n attempted_ = true;\n blocked_ = blocked;\n }\n }\n state_changed_.notify_all();\n}\nvoid ReadInterleave::on_access(Access access) {\n if (std::this_thread::get_id() != owner_ || access.write || started_) {\n return;\n }\n ++loads_;\n if (loads_ != cut_) {\n return;\n }\n launch();\n std::unique_lock<std::mutex> guard(state_mutex_);\n state_changed_.wait(guard, [this] { return attempted_ || done_; });\n if (!blocked_) {\n state_changed_.wait(guard, [this] { return done_; });\n }\n}\nStatus ReadInterleave::finish() {\n if (!finished_) {\n launch();\n worker_.join();\n port_.access_hook({});\n port_.lock_hook({});\n finished_ = true;\n }\n if (failure_) {\n std::rethrow_exception(failure_);\n }\n return status_;\n}\n}\n'}
|
|
|
|
import json
|
|
import base64
|
|
import hashlib
|
|
import zlib
|
|
from types import SimpleNamespace
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
MASK32 = (1 << 32) - 1
|
|
MASK64 = (1 << 64) - 1
|
|
|
|
def count(frame):
|
|
return frame[2] * frame[3] if frame[2] <= 4 and frame[3] <= 4 else 0
|
|
|
|
def status(frame):
|
|
rows, columns, scale = frame[2:5]
|
|
if not ((rows == columns == 0) or (1 <= rows <= 4 and 1 <= columns <= 4)):
|
|
return 2
|
|
return 0 if -4096 <= scale <= 4096 else 3
|
|
|
|
def frame_text(frame):
|
|
return ' '.join(map(str, frame))
|
|
|
|
def raw(frame):
|
|
header, samples = frame[:6], frame[6:]
|
|
revision, tick, rows, columns, scale, tag = header
|
|
words = [revision, tick & MASK32, tick >> 32, rows, columns, scale, tag] + samples
|
|
checksum = 1469598103934665603
|
|
for word in words:
|
|
checksum = ((checksum ^ (word & MASK32)) * 1099511628211) & MASK64
|
|
summary = [len(samples), sum(samples), min(samples, default=0), max(samples, default=0), checksum]
|
|
return 'F 0 ' + frame_text(frame) + ' | ' + ' '.join(map(str, summary))
|
|
|
|
def bank(frame):
|
|
if frame is None:
|
|
return 'B 0 0 0 0 0 0 0 0'
|
|
revision, tick, rows, columns, scale, tag = frame[:6]
|
|
words = [1, revision, tick & MASK32, tick >> 32, rows, columns, scale & MASK32, tag]
|
|
words.extend(value & MASK32 for value in frame[6:])
|
|
return 'B ' + ' '.join(map(str, words))
|
|
|
|
def make_frame(rng, shape=None, serial=0):
|
|
rows, columns = shape if shape is not None else rng.choice(
|
|
[(0, 0), (1, 1), (1, 4), (4, 1), (2, 2), (3, 4), (4, 3), (4, 4)])
|
|
boundaries = [-2147483648, -4096, -1, 0, 1, 4096, 2147483647]
|
|
samples = [rng.choice(boundaries) if (i + serial) % 3 == 0 else rng.randint(-1000000000, 1000000000)
|
|
for i in range(rows * columns)]
|
|
return [rng.choice([0, MASK32, 11, rng.getrandbits(32)]),
|
|
rng.choice([0, MASK64, 1 << 32, rng.getrandbits(64)]),
|
|
rows, columns, rng.choice([-4096, -1, 0, 1, 4096]), rng.getrandbits(32)] + samples
|
|
|
|
def plan():
|
|
rng = random.Random(0x524541444652414D)
|
|
requests, expected, cases = [], [], []
|
|
def serial(frames, label):
|
|
requests.append('serial ' + str(len(frames)) + ''.join(' ' + frame_text(f) for f in frames))
|
|
marker = [0x11223344, 0x1020304050607080, 4, 4, -33, 0x99887766]
|
|
marker.extend(71 + i * 19 for i in range(16))
|
|
expected.append({'E 1 1 ' + ' '.join(map(str, marker))})
|
|
latest = None
|
|
for frame in frames:
|
|
result = status(frame)
|
|
expected.append({'P ' + str(result)})
|
|
if result == 0:
|
|
latest = frame
|
|
expected.append({bank(latest)})
|
|
expected.append({raw(latest)} if latest is not None else {'F 1'})
|
|
cases.append({'kind': label, 'frames': len(frames), 'lines': 1 + len(frames) * 3})
|
|
serial([], 'empty')
|
|
invalid = [
|
|
[0, 0, 1, 0, 0, 0], [1, 1, 0, 1, 0, 1],
|
|
[2, 2, 5, 1, 0, 2], [3, 3, MASK32, MASK32, 5000, 3],
|
|
[4, 4, 0, 0, -4097, 4], [5, 5, 1, 1, 4097, 5, 99],
|
|
]
|
|
serial(invalid, 'rejection-before-first-publish')
|
|
boundary = []
|
|
for i, shape in enumerate([(0, 0), (1, 1), (4, 4), (2, 2), (4, 1), (1, 4), (0, 0)]):
|
|
boundary.append(make_frame(rng, shape, i))
|
|
boundary.append(invalid[i % len(invalid)])
|
|
serial(boundary, 'boundary-and-rejection-preserves-state')
|
|
for trial in range(24):
|
|
frames = []
|
|
for i in range(10):
|
|
frames.append(make_frame(rng, serial=i + trial))
|
|
if i % 3 == 0:
|
|
frames.append(invalid[(i + trial) % len(invalid)])
|
|
serial(frames, 'random-serial')
|
|
shapes = [(0, 0), (1, 1), (2, 2), (1, 3), (4, 4), (4, 1), (3, 4)]
|
|
for trial in range(42):
|
|
first_shape = shapes[trial % len(shapes)]
|
|
second_shape = first_shape if trial < 14 else shapes[(trial + 3) % len(shapes)]
|
|
first = make_frame(rng, first_shape, trial)
|
|
second = make_frame(rng, second_shape, trial + 1)
|
|
# Also exercise repeated revision and tick; neither is a publication token.
|
|
if trial % 4 == 0:
|
|
second[0] = first[0]
|
|
if trial % 5 == 0:
|
|
second[1] = first[1]
|
|
for cut in [1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 16, 24, 32, 64, 128]:
|
|
requests.append('race ' + str(cut) + ' ' + frame_text(first) + ' ' + frame_text(second))
|
|
expected.extend([{'P 0'}, {bank(first)}, {raw(first), raw(second)},
|
|
{'P 0'}, {bank(second)}, {raw(second)}])
|
|
cases.append({'kind': 'same-layout-race' if first_shape == second_shape else 'resize-race',
|
|
'cut': cut, 'lines': 6})
|
|
assert all(len(line.encode()) <= 8192 for line in requests)
|
|
return '\n'.join(requests) + '\n', expected, cases
|
|
|
|
def emit(stage, **fields):
|
|
print('STAGE ' + json.dumps(dict(stage=stage, **fields), sort_keys=True), file=sys.stderr, flush=True)
|
|
|
|
|
|
EXECUTOR = "import base64\nimport ctypes\nimport errno\nimport json\nimport os\nimport signal\nimport subprocess\nimport sys\nimport time\n\nconfiguration = json.loads(sys.argv[1])\nstarted = time.monotonic()\nprocess = None\nout = err = b''\ngroups = set()\nreaped = []\nfault = None\ntimed_out = False\nleader_reaped = False\ntree_absent = False\ngroup_absent = False\nidentity = dict(uid=os.getuid(), gid=os.getgid(), groups=os.getgroups())\n\ndef error_record(error):\n return dict(type=type(error).__name__, message=str(error))\n\ndef children(pid):\n with open('/proc/' + str(pid) + '/task/' + str(pid) + '/children') as stream:\n return [int(word) for word in stream.read().split()]\n\ndef descendants():\n pending = children(os.getpid())\n found = set()\n while pending:\n pid = pending.pop()\n if pid in found:\n continue\n found.add(pid)\n try:\n pending.extend(children(pid))\n except FileNotFoundError:\n pass\n return found\n\ndef kill_family():\n pids = descendants()\n if process is not None and process.poll() is None:\n pids.add(process.pid)\n for pid in sorted(pids):\n try:\n group = os.getpgid(pid)\n if group != os.getpgrp():\n groups.add(group)\n try:\n os.killpg(group, signal.SIGKILL)\n except ProcessLookupError:\n pass\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n\ndef finish_family():\n global leader_reaped, tree_absent, group_absent\n kill_family()\n if process is not None:\n process.wait(timeout=2)\n leader_reaped = True\n deadline = time.monotonic() + 3\n while True:\n kill_family()\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(dict(pid=pid, returncode=os.waitstatus_to_exitcode(status)))\n remaining = descendants()\n if not remaining:\n tree_absent = True\n break\n if time.monotonic() >= deadline:\n raise RuntimeError('descendants remain after bounded teardown')\n time.sleep(.01)\n absent = []\n for group in sorted(groups):\n try:\n os.killpg(group, 0)\n except ProcessLookupError:\n absent.append(True)\n else:\n absent.append(False)\n group_absent = bool(process is not None) and all(absent)\n\ntry:\n libc = ctypes.CDLL(None, use_errno=True)\n if libc.prctl(36, 1, 0, 0, 0) != 0:\n number = ctypes.get_errno()\n raise OSError(number, os.strerror(number))\n payload = sys.stdin.buffer.read()\n process = subprocess.Popen(configuration['argv'], cwd=configuration.get('cwd'),\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n groups.add(process.pid)\n try:\n out, err = process.communicate(payload, timeout=configuration['deadline'])\n leader_reaped = True\n except subprocess.TimeoutExpired:\n timed_out = True\n kill_family()\n process.wait(timeout=2)\n leader_reaped = True\n out, err = process.communicate(timeout=2)\nexcept BaseException as error:\n fault = error_record(error)\nfinally:\n if process is not None:\n try:\n finish_family()\n except BaseException as error:\n if fault is None:\n fault = error_record(error)\n complete = (process is not None and fault is None and leader_reaped and\n tree_absent and group_absent)\n returncode = (125 if not complete else 124 if timed_out else process.returncode)\n metadata = dict(phase=configuration['phase'], deadline=configuration['deadline'],\n elapsed=round(time.monotonic() - started, 6),\n supervisor_pid=os.getpid(), identity=identity,\n leader_pid=process.pid if process is not None else None,\n leader_returncode=process.returncode if process is not None else None,\n returncode=returncode, timed_out=timed_out, error=fault,\n leader_reaped=leader_reaped, adopted_reaped=reaped,\n process_groups=sorted(groups), group_absent=group_absent,\n tree_absent=tree_absent, complete=complete)\n envelope = dict(meta=metadata, stdout=base64.b64encode(out).decode('ascii'),\n stderr=base64.b64encode(err).decode('ascii'))\n sys.stdout.write(json.dumps(envelope, separators=(',', ':')) + '\\n')\n sys.stdout.flush()\n"
|
|
CALLS = {}
|
|
|
|
def error_record(error):
|
|
return dict(type=type(error).__name__, message=str(error))
|
|
|
|
def _execute(phase, argv, deadline, cwd=None, payload=b''):
|
|
record = dict(deadline=deadline, supervisor_rc=None, meta=None, error=None)
|
|
CALLS[phase] = record
|
|
try:
|
|
configuration = dict(phase=phase, argv=argv, deadline=deadline, cwd=cwd)
|
|
completed = subprocess.run([sys.executable, '-I', '-c', EXECUTOR,
|
|
json.dumps(configuration, separators=(',', ':'))],
|
|
input=payload, capture_output=True)
|
|
record['supervisor_rc'] = completed.returncode
|
|
envelope = json.loads(completed.stdout.decode('utf-8', 'strict'))
|
|
metadata = envelope.get('meta')
|
|
record['meta'] = metadata
|
|
if completed.returncode != 0 or not isinstance(metadata, dict):
|
|
raise RuntimeError('missing terminal execution metadata')
|
|
if metadata.get('phase') != phase or metadata.get('deadline') != deadline:
|
|
raise RuntimeError('execution metadata identity mismatch')
|
|
out = base64.b64decode(envelope['stdout'], validate=True)
|
|
err = base64.b64decode(envelope['stderr'], validate=True)
|
|
record.update(stdout_bytes=len(out), stderr_bytes=len(err),
|
|
stdout_sha256=hashlib.sha256(out).hexdigest(),
|
|
stderr_sha256=hashlib.sha256(err).hexdigest())
|
|
if err and len(err) <= 512:
|
|
record['stderr_base64'] = base64.b64encode(err).decode('ascii')
|
|
if out and len(out) <= 512:
|
|
record['stdout_base64'] = base64.b64encode(out).decode('ascii')
|
|
result = SimpleNamespace(returncode=metadata['returncode'], metadata=metadata,
|
|
stdout=out.decode('utf-8', 'strict'),
|
|
stderr=err.decode('utf-8', 'replace'))
|
|
return result
|
|
except Exception as error:
|
|
record['error'] = error_record(error)
|
|
raise
|
|
|
|
def _ok(result):
|
|
return (result is not None and result.returncode == 0 and
|
|
result.metadata.get('complete') is True and result.metadata.get('error') is None and
|
|
result.metadata.get('leader_reaped') is True and
|
|
result.metadata.get('group_absent') is True and result.metadata.get('tree_absent') is True)
|
|
|
|
def main():
|
|
candidate = Path(sys.argv[1])
|
|
source = (candidate / 'src/frame.cpp').read_bytes()
|
|
payload, expected, cases = plan()
|
|
payload_bytes = payload.encode('utf-8')
|
|
scratch = tempfile.mkdtemp(prefix='concur-reader-frame-')
|
|
project = Path(scratch) / 'project'
|
|
success = False
|
|
reason = 'unexecuted'
|
|
primary_error = cleanup_error = absence_error = None
|
|
cleanup = absence = None
|
|
try:
|
|
project.mkdir(mode=0o777)
|
|
project.chmod(0o777)
|
|
for name, text in SUPPORT.items():
|
|
path = project / name
|
|
path.parent.mkdir(parents=True, exist_ok=True, mode=0o777)
|
|
path.write_text(text)
|
|
path.chmod(0o644)
|
|
(project / 'src/frame.cpp').write_bytes(source)
|
|
(project / 'src/frame.cpp').chmod(0o644)
|
|
for directory in [project / 'src', project / 'include']:
|
|
directory.chmod(0o777)
|
|
binary = str(project / 'frame-demo')
|
|
compile_argv = ['g++', '-std=c++17', '-O2', '-pthread', '-Wall', '-Wextra', '-Wpedantic',
|
|
'-I' + str(project / 'include'),
|
|
str(project / 'src/frame.cpp'), str(project / 'src/memory_port.cpp'),
|
|
str(project / 'src/replay.cpp'), str(project / 'src/cli.cpp'), '-o', binary]
|
|
identity_argv = [sys.executable, '-I', '-c',
|
|
'import json,os; print(json.dumps(dict(uid=os.getuid(),gid=os.getgid(),groups=os.getgroups())))']
|
|
emit('prepared', scratch=scratch, requests=len(cases), observations=len(expected))
|
|
identity = _execute('identity', identity_argv, 10)
|
|
emit('identity', returncode=identity.returncode, stdout=identity.stdout.strip())
|
|
if not _ok(identity):
|
|
reason = 'identity process failed'
|
|
else:
|
|
compiled = _execute('compile', compile_argv, 30, cwd=str(project))
|
|
emit('compile', returncode=compiled.returncode, stderr=compiled.stderr[-160:])
|
|
if not _ok(compiled):
|
|
reason = 'compile failed'
|
|
else:
|
|
run = _execute('candidate', [binary], 30, cwd=str(project), payload=payload_bytes)
|
|
emit('candidate', returncode=run.returncode, stderr=run.stderr[-160:],
|
|
controlled_rejection=run.returncode == 73)
|
|
if not _ok(run):
|
|
reason = 'candidate controlled rejection' if run.returncode == 73 else 'candidate execution failed'
|
|
else:
|
|
observed = run.stdout.splitlines()
|
|
if len(observed) != len(expected):
|
|
reason = 'observation count differs'
|
|
emit('difference', expected_lines=len(expected), observed_lines=len(observed))
|
|
else:
|
|
mismatch = next((i for i, (line, allowed) in enumerate(zip(observed, expected))
|
|
if line not in allowed), None)
|
|
if mismatch is not None:
|
|
reason = 'raw observation differs'
|
|
offset = 0
|
|
location = None
|
|
for case_index, case in enumerate(cases):
|
|
if offset <= mismatch < offset + case['lines']:
|
|
location = dict(request=case_index, observation=mismatch - offset, **case)
|
|
break
|
|
offset += case['lines']
|
|
emit('difference', index=mismatch, observed=observed[mismatch][:400],
|
|
allowed=sorted(expected[mismatch]), location=location)
|
|
else:
|
|
success = True
|
|
reason = 'all defined observations matched'
|
|
except Exception as error:
|
|
primary_error = error_record(error)
|
|
reason = 'checker execution error'
|
|
emit('error', **primary_error)
|
|
finally:
|
|
try:
|
|
cleanup = _execute('cleanup', ['/bin/rm', '-rf', '--', scratch], 15)
|
|
emit('cleanup', returncode=cleanup.returncode, stderr=cleanup.stderr[-160:])
|
|
except Exception as error:
|
|
cleanup_error = error_record(error)
|
|
emit('cleanup', returncode=None, error=cleanup_error)
|
|
try:
|
|
absence = _execute('absence', [sys.executable, '-I', '-c',
|
|
'import os,sys; raise SystemExit(19 if os.path.lexists(sys.argv[1]) else 0)',
|
|
scratch], 5)
|
|
emit('absence', returncode=absence.returncode)
|
|
except Exception as error:
|
|
absence_error = error_record(error)
|
|
emit('absence', returncode=None, error=absence_error)
|
|
terminal = dict(scratch=scratch, primary_error=primary_error, cleanup_error=cleanup_error,
|
|
absence_error=absence_error, phases=CALLS)
|
|
terminal_json = json.dumps(terminal, sort_keys=True, separators=(',', ':'))
|
|
if len(terminal_json.encode('utf-8')) > 3700:
|
|
terminal_line = 'TERMINAL_Z ' + base64.b64encode(zlib.compress(terminal_json.encode('utf-8'))).decode('ascii')
|
|
else:
|
|
terminal_line = 'TERMINAL ' + terminal_json
|
|
print(terminal_line, file=sys.stderr, flush=True)
|
|
if (success and primary_error is None and cleanup_error is None and absence_error is None and
|
|
_ok(cleanup) and _ok(absence)):
|
|
print('PASS')
|
|
return 0
|
|
print('FAIL: ' + (reason if _ok(cleanup) and _ok(absence) else 'cleanup not confirmed'))
|
|
return 1
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|