amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

359 lines
15 KiB
Python

"""Independent byte-prefix oracle; submitted C is executed only in children."""
import errno
import json
import random
import re
import subprocess
import sys
import tempfile
from pathlib import Path
CANDIDATES = ["include/vector.h", "src/cursor.c", "src/job.c", "src/ledger.c",
"src/submit.c", "src/device.c", "src/console.c"]
E = errno
def fresh():
return dict(ready=0, spans=[], done=0, calls=0, error=0,
book=[0] * 5, script=[], pos=0, received=b"", dcalls=0)
def position(s):
prefix = 0
for i, data in enumerate(s["spans"]):
if s["done"] < prefix + len(data):
return i, s["done"] - prefix
prefix += len(data)
return len(s["spans"]), 0
def window(s, budget, width):
result = []
begin = s["done"]
origin = 0
for data in s["spans"]:
end = origin + len(data)
if end > begin and budget and len(result) < width:
part = data[max(begin - origin, 0):][:budget]
if part:
result.append(part)
budget -= len(part)
origin = end
return result
def snapshot(s):
index, offset = position(s)
nums = [s["ready"], len(s["spans"]), index, offset, s["done"],
sum(map(len, s["spans"])), s["calls"], s["error"],
*s["book"], s["dcalls"], s["pos"], len(s["received"])]
return ("STATE", tuple(nums), s["received"])
def finish(s, accepted, error, paused, completed):
increments = [accepted, 1, completed, int(error != 0), paused]
s["book"] = [x + y for x, y in zip(s["book"], increments)]
return accepted if accepted else -error
def oracle(events, console=False):
s = fresh()
output = []
for step, event in enumerate(events, 1):
op, *args = event
result = 0
if op == "R":
s = fresh()
elif op == "O":
spans, nulls, null_array, null_job = args
invalid = (len(spans) > 12 or null_job or null_array and bool(spans)
or any(len(v) > 256 or n and bool(v) for v, n in zip(spans, nulls)))
if invalid:
result = -E.EINVAL
else:
s.update(ready=1, spans=spans[:], done=0, calls=0, error=0)
elif op == "P":
values = args[0]
if console and (len(values) > 64 or any(v < 0 and v not in
[-E.EINTR, -E.EAGAIN, -E.EIO, -E.ENOSPC] or v > 3073 for v in values)):
result = -E.EINVAL
else:
s.update(script=values[:], pos=0)
elif op == "S":
quota, width, flags = args
if not s["ready"] or flags & 7 or not 1 <= quota <= 3072 or not 1 <= width <= 4:
result = -E.EINVAL
else:
accepted = 0
error = paused = completed = 0
s["error"] = 0
total = sum(map(len, s["spans"]))
target = min(s["done"] + quota, total)
while s["done"] < target:
pieces = window(s, target - s["done"], width)
offered = sum(map(len, pieces))
if s["pos"] < len(s["script"]):
action = s["script"][s["pos"]]
s["pos"] += 1
else:
action = offered
s["dcalls"] += 1
s["calls"] += 1
output.append(("CALL", tuple(pieces), action))
if action == -E.EINTR:
continue
if action < 0 or action > offered:
error = -action if action < 0 else E.EPROTO
break
if action == 0:
paused = 1
break
s["received"] += b"".join(pieces)[:action]
accepted += action
s["done"] += action
completed = int(s["done"] == total)
s["error"] = error
result = finish(s, accepted, error, paused, completed)
elif op == "B":
s["book"] = [0] * 5
elif op == "F":
result = finish(s, *args)
elif op == "A":
s["done"] += args[0]
elif op == "C":
s["done"] = 0
elif op == "W":
output.append(("WINDOW", tuple(window(s, *args))))
elif op == "D":
received = b"\x00"
output += [("DEVICE", (0, 0, 0, 0), b""), ("DEVICE", (0, 0, 0, 0), b""),
("DEVICE", (1, 1, 1, 1), received)]
output += [("DEVICE", (-E.EINVAL, 1, 1, 1), received)] * 3
output += [("DEVICE", (-E.EIO, 2, 2, 1), received),
("DEVICE", (0, 2, 0, 1), received),
("DEVICE", (3, 3, 0, 4), b"\x00\x00\xff\x05"),
("HEX", b""), ("HEX", b"\x00\xff\x05"),
("DEVICE", (0, 0, 0, 0), b"")]
elif op == "I":
output += [("IOSTATUS", 2), ("IOSTATUS", 2)]
elif op == "BAD":
result = -E.EINVAL
output.append(("RESULT", step, result))
output.append(snapshot(s))
return output
def opened(*spans):
return ["O", list(spans), [0] * len(spans), 0, 0]
def raw_events():
rng = random.Random(0x1756A2)
events = [["R"], ["D"], ["I"], ["S", 5, 2, 0], opened(b"abc", b"", b"def", b"gh"),
["P", [4, 2]], ["S", 6, 4, 0], ["S", 8, 4, 0],
["R"], opened(b"abcd", b"efgh"), ["P", [2, -E.EIO]],
["S", 8, 1, 0], ["S", 8, 4, 0],
["P", [-E.EAGAIN]], ["S", 8, 2, 0],
opened(b"", b"x", b"", b"yz", b""), ["W", 0, 4],
["W", 2, 4], ["A", 2], ["W", 1, 4], ["A", 0],
["A", 1], ["S", 4, 1, 0], ["C"], ["S", 1, 1, 8],
["P", [-E.EINTR, -E.EINTR, 0]], ["S", 2, 2, 0],
["P", [1, -E.ENOSPC]], ["S", 2, 2, 0], ["S", 2, 2, 0]]
for spans, nulls, na, nj in [([b"keep"], [0], 0, 0),
([b"z"], [1], 0, 0), ([b"x"], [0], 1, 0),
([b"a"] * 13, [0] * 13, 0, 0), ([bytes(257)], [0], 0, 0),
([b"b"], [0], 0, 1), ([], [], 1, 0), ([b"", b""], [1, 1], 0, 0)]:
events += [["O", spans, nulls, na, nj], ["S", 1, 1, 0]]
events += [opened(b"valid"), ["P", [0]], ["S", 0, 1, 0],
["S", 3073, 1, 0], ["S", 1, 0, 0], ["S", 1, 5, 0]]
events += [["S", 1, 1, flag] for flag in [1, 2, 4]]
events += [["S", 1, 1, 0], ["B"], ["F", 0, E.EIO, 0, 0],
["F", 7, E.EAGAIN, 0, 0], ["F", 0, 0, 1, 0],
["F", 13, 0, 0, 1], ["F", 2, 0, 0, 0]]
# Every width and quota endpoint, 12 descriptors, all byte values and trailing empties.
for width in range(1, 5):
events += [["R"], opened(*[bytes(range(256)) for _ in range(12)]),
["P", [-E.EINTR, 1, 255]], ["S", 3072, width, 0],
["S", 3072, width, 0], ["S", 1, width, 0]]
for action in [-E.EIO, -E.EAGAIN, -E.ENOSPC, 0, 3073]:
for prefix in [[], [1], [3], [4]]:
events += [["R"], opened(b"abc", b"", b"defghi", b""),
["P", prefix + [action]], ["S", 9, 4, 0], ["S", 9, 4, 0]]
for _ in range(90):
spans = [bytes(rng.randrange(256) for _ in range(rng.choice([0, 0, 1, 2, 3, 17, 64, 256])))
for _ in range(rng.randrange(13))]
events += [["R"], opened(*spans)]
for _ in range(7):
script = [rng.choice([-E.EINTR, -E.EAGAIN, -E.EIO, -E.ENOSPC, 0, 1, 2, 4, 17, 3073])
for _ in range(rng.randrange(7))]
events += [["P", script], ["S", rng.choice([1, 2, 3, 7, 31, 256, 3072]), rng.randrange(1, 5), 0]]
events += [["P", []], ["S", 3072, 4, 0], ["S", 1, 1, 0]]
return events
def raw_input(events):
rows = []
for op, *args in events:
if op == "O":
spans, nulls, na, nj = args
fields = [op, len(spans), na, nj]
for span, null in zip(spans, nulls):
fields += [len(span), null, *span]
elif op == "P":
fields = [op, len(args[0]), *args[0]]
else:
fields = [op, *args]
rows.append(" ".join(map(str, fields)))
return ("\n".join(rows) + "\n").encode("ascii")
def console_events():
events = [["R"], opened(b"abc", b"", b"def", b"gh"), ["P", [4, 2]],
["S", 6, 4, 0], ["S", 8, 4, 0], ["Q"]]
events += [["BAD", text] for text in ["", "Z", "R 1", "Q 0", "O", "O 2 00", "O 0 00",
"O 1 a", "O 1 gg", "O -1", "P", "P 2 1", "P 1 -1", "P 1 3074",
"S 1", "S 1 2 3", "S -1 1", "S 1 1x", "S 18446744073709551616 1"]]
events += [["BAD", text] for text in ["S \x0b1 1", "S \x0c1 1", "S +\x0b1 1", "S 1 \x0c1",
"O \x0b1 ff", "O \x0c1 ff", "P \x0b1 0", "P 1 \x0b1", "P 1 \x0c1",
"P 1 +\x0b1", "P 1 -\x0c4", "S -0 1", "S 1 -0", "O -0", "P -0",
"S + 1", "S ++1 1", "P 1 -", "P 1 +-1", "P 1 1junk", "O 1junk ff",
"P 1 \x0b-4", "P 1 \x0c-4", "S 1\x0b 1", "P 1 0\x0c"]]
events += [opened(b"stable"), ["P", [1, -E.EIO]], ["S", 6, 1, 0], ["Q"],
["BAD", "O 1 " + "aa" * 257], ["S", 6, 4, 0],
["P", [0] * 65], ["S", 1, 1, 0], ["P", []]]
events += [["R"], opened(b"x"), ["P", [0] * 64], ["S", 1, 1, 0],
["P", [3073]], ["S", 1, 1, 0], ["P", [-E.EAGAIN]], ["S", 1, 1, 0],
["P", [-E.EIO]], ["S", 1, 1, 0], ["P", []], ["S", 1, 1, 0],
["P", [-E.EINTR]], ["S", 1, 1, 0], ["Q"]]
rng = random.Random(0xAFE094)
for _ in range(30):
spans = [bytes(rng.randrange(256) for _ in range(rng.choice([0, 1, 3, 19, 256])))
for _ in range(rng.randrange(13))]
events += [["R"], opened(*spans), ["P", [-E.EINTR, 1, -E.ENOSPC]],
["S", 3072, 4, 0], ["S", 3072, 1, 0], ["Q"],
["S", 0, 1, 0], ["S", 1, 5, 0]]
return events
def console_input(events):
rows = []
for index, (op, *args) in enumerate(events):
if op == "BAD":
rows.append(args[0])
continue
if op == "O":
spans = args[0]
fields = ["O", len(spans), *(v.hex().upper() if index % 2 else v.hex() for v in spans)]
fields = [x if x != "" else "-" for x in fields]
elif op == "P":
fields = [op, len(args[0]), *args[0]]
elif op == "S":
fields = [op, *args[:2]]
else:
fields = [op]
# Whitespace, positive signs and leading zeroes are public syntax.
text = "\t".join("+0" + str(v) if type(v) is int and v >= 0 and index % 3 == 0 else str(v) for v in fields)
rows.append(text)
return ("\r\n".join(rows) + "\r\n").encode("ascii")
def observations(data):
words = iter(data.decode("ascii").split())
result = []
def number():
value = next(words)
if not re.fullmatch(r"[+-]?[0-9]+", value):
raise ValueError("noninteger observation")
return int(value)
def binary(word):
return b"" if word == "-" else bytes.fromhex(word)
for marker in words:
if marker == "RESULT":
result.append((marker, number(), number()))
elif marker == "STATE":
values = tuple(number() for _ in range(16))
result.append((marker, values, binary(next(words))))
elif marker == "DEVICE":
values = tuple(number() for _ in range(4))
result.append((marker, values, binary(next(words))))
elif marker == "HEX":
result.append((marker, binary(next(words))))
elif marker == "IOSTATUS":
result.append((marker, number()))
elif marker in ["CALL", "WINDOW"]:
count = number()
if not 0 <= count <= 4:
raise ValueError("invalid window size")
spans = []
for _ in range(count):
length, text = next(words).split(":", 1)
value = binary(text)
if int(length) != len(value):
raise ValueError("window byte length mismatch")
spans.append(value)
if marker == "CALL":
if next(words) != "=>":
raise ValueError("missing call result")
result.append((marker, tuple(spans), number()))
else:
result.append((marker, tuple(spans)))
else:
raise ValueError("unexpected observation marker")
return result
def run(argv, scratch, stdin=None, timeout=25):
result = subprocess.run(argv, cwd=scratch, input=stdin, capture_output=True, timeout=timeout)
if result.stderr:
print(result.stderr[-2500:].decode("utf-8", "replace"), file=sys.stderr)
return result
def main():
root = Path(__file__).resolve().parent
context = json.loads((root / "context.json").read_text())
harness = (root / "harness.c").read_bytes()
sources = {name: (Path(sys.argv[1]) / name).read_bytes() for name in CANDIDATES}
raw, console = raw_events(), console_events()
cases = [("api", raw_input(raw), oracle(raw)),
("replay", console_input(console), oracle(console, True))]
extra = [["R"], opened(b"\xff"), ["P", [0]], ["S", 1, 1, 0],
["P", [-E.EINTR]], ["S", 1, 1, 0], ["Q"]]
long_open = "O +" + "0" * 8184 + "1 ff\n"
long_script = "P +1 -" + "0" * 8184 + "4\n"
assert len(long_open.rstrip("\n").encode()) == 8191 and len(long_script.rstrip("\n").encode()) == 8191
cases += [("replay", b"", []),
("replay", ("R\n" + long_open + "P +0001 -0000\nS +0001 +01\n" +
long_script + "S +0001 +01\nQ").encode(), oracle(extra, True))]
scratch = Path(tempfile.mkdtemp(prefix="io-vector-progress-", dir=Path.cwd()))
for name, content in {**context, **sources, "harness.c": harness}.items():
target = scratch / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(content.encode() if isinstance(content, str) else content)
# All writes and candidate reads are finished before the first child starts.
cc = ["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-pedantic", "-Iinclude"]
libraries = [name for name in CANDIDATES if name.endswith(".c")]
for entry, program in [("harness.c", "api"), ("src/main.c", "replay")]:
built = run(cc + libraries + [entry, "-o", program], scratch, timeout=35)
if built.returncode:
print("build failed"); return 1
for program, data, expected in cases:
completed = run([str(scratch / program)], scratch, data)
if completed.returncode:
print("candidate process failed: " + str(completed.returncode)); return 1
try:
actual = observations(completed.stdout)
except (ValueError, UnicodeError, StopIteration) as error:
print("invalid observation: " + str(error)); return 1
if actual != expected:
mismatch = next((i for i, pair in enumerate(zip(actual, expected)) if pair[0] != pair[1]), min(len(actual), len(expected)))
print("%s observation %d mismatch (%d/%d records)" % (program, mismatch + 1, len(actual), len(expected)))
print("got " + repr(actual[mismatch:mismatch + 1])[:600], file=sys.stderr)
print("expected " + repr(expected[mismatch:mismatch + 1])[:600], file=sys.stderr)
return 1
print("PASS"); return 0
if __name__ == "__main__":
raise SystemExit(main())