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

400 lines
19 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import errno
import json
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
CANDIDATES = ['descriptor.h', 'fd_ops.c', 'bank.c', 'stream.c', 'wire.c', 'console.c']
LIBRARY = ['fd_ops.c', 'bank.c', 'stream.c', 'wire.c']
LIMIT = 4096
def failed(error):
return {'rc': -1, 'err': error}
def ok(result=0):
return {'rc': result, 'err': 0}
def i32(text):
if not re.fullmatch(r'[+-]?[0-9]+', text, flags=re.ASCII):
raise ValueError('decimal')
negative = text.startswith('-')
digits = text.lstrip('+-').lstrip('0') or '0'
if len(digits) > 10:
raise ValueError('range')
value = int(digits) * (-1 if negative else 1)
if not -(2 ** 31) <= value <= 2 ** 31 - 1:
raise ValueError('range')
return value
def hexbytes(text):
if text == '-':
return b''
if not text or len(text) % 2 or not re.fullmatch('[0-9a-fA-F]+', text):
raise ValueError('hex')
return bytes.fromhex(text)
def token(data):
return data.hex() if data else '-'
class Bank:
def __init__(self):
self.slots = [None] * 8
def action(self, line):
words = re.split(r'[ \t\r\n]+', line.strip(' \t\r\n'))
command = words[0]
counts = {'reset': 1, 'seed': 4, 'bind': 3, 'read': 3, 'write': 3,
'seek': 3, 'flags': 3, 'info': 2, 'drop': 2,
'readnull': 3, 'writenull': 3}
if command not in counts or len(words) != counts[command]:
return failed(errno.EINVAL)
if command == 'reset':
self.slots = [None] * 8
return ok()
try:
index = i32(words[1])
value = i32(words[2]) if command in ('bind', 'read', 'seek', 'flags', 'readnull', 'writenull') else None
data = hexbytes(words[2]) if command in ('seed', 'write') else None
except ValueError:
return failed(errno.EINVAL)
# Stream span validation precedes bank/descriptor validation.
if command == 'read' and (value < 0 or value > LIMIT):
return dict(failed(errno.EINVAL), data='')
if command in ('readnull', 'writenull') and value != 0:
return failed(errno.EINVAL)
if command == 'write' and len(data) > LIMIT:
return failed(errno.EINVAL)
if command == 'seek' and not 0 <= value <= LIMIT:
return failed(errno.EINVAL)
if not 0 <= index < 8:
result = failed(errno.EINVAL)
return dict(result, data='') if command == 'read' else result
if command == 'seed':
name = words[3]
if len(data) > LIMIT or not re.fullmatch('[a-zA-Z0-9_-]{1,31}', name):
return failed(errno.EINVAL)
self.slots[index] = {'description': {'data': bytearray(data), 'pos': 0},
'name': name, 'flag': 0, 'reads': 0, 'writes': 0}
return ok()
entry = self.slots[index]
if command == 'bind':
if not 0 <= value < 8:
return failed(errno.EINVAL)
if entry is None:
return failed(errno.EBADF)
if index != value:
self.slots[value] = {'description': entry['description'],
'name': entry['name'], 'flag': 0, 'reads': 0, 'writes': 0}
return ok()
if command == 'flags' and value not in (0, 1):
return failed(errno.EINVAL)
if entry is None:
result = failed(errno.EBADF)
return dict(result, data='') if command == 'read' else result
description = entry['description']
if command == 'info':
return dict(ok(), cursor=description['pos'], size=len(description['data']),
cloexec=entry['flag'], read_bytes=entry['reads'],
written_bytes=entry['writes'], name=entry['name'])
if command == 'drop':
self.slots[index] = None
return ok()
if command == 'flags':
entry['flag'] = value
return ok()
if command == 'seek':
description['pos'] = value
return ok(value)
if command in ('readnull', 'writenull'):
return ok()
if command == 'read':
position = description['pos']
result = bytes(description['data'][position:position + value])
description['pos'] += len(result)
entry['reads'] += len(result)
return dict(ok(len(result)), data=result.hex())
if command == 'write':
position = description['pos']
if not data:
return ok()
if position + len(data) > LIMIT:
return failed(errno.EFBIG)
if position + len(data) > len(description['data']):
description['data'].extend(b'\x00' * (position + len(data) - len(description['data'])))
description['data'][position:position + len(data)] = data
description['pos'] += len(data)
entry['writes'] += len(data)
return ok(len(data))
raise AssertionError(command)
def bank_cases():
lines = [f'info {i}' for i in range(8)]
lines += ['reset', 'reset', 'seed 0 6162636465666768696a field', 'bind 0 1',
'read 0 3', 'read 1 3', 'info 0', 'info 1', 'flags 0 1',
'bind 0 0', 'info 0', 'flags 1 1', 'bind 0 1', 'info 1',
'seek 1 1', 'write 0 5a00ff', 'info 0', 'info 1', 'seek 0 0',
'read 1 20', 'read 0 0', 'read 1 1', 'drop 0', 'info 1',
'seek 1 0', 'read 1 10', 'seed 2 3132333435 other', 'flags 2 1',
'read 2 1', 'bind 0 2', 'info 2', 'read 2 2', 'bind 1 2',
'info 2', 'seed 1 78 new', 'info 2', 'seek 2 0', 'read 2 20',
'read 1 2', 'drop 2', 'drop 2', 'bind 7 7', 'info 7',
'seed 7 - empty', 'read 7 1', 'flags 7 1', 'bind 7 7', 'info 7',
'seek 7 4096', 'write 7 -', 'write 7 00', 'info 7',
'seek 7 4095', 'write 7 ff', 'seek 7 4092', 'read 7 5']
for index in (-2147483648, -1, 8, 2147483647):
lines += [f'info {index}', f'seed {index} 00 a', f'bind {index} 1',
f'bind 1 {index}', f'read {index} 1', f'write {index} 00',
f'seek {index} 0', f'flags {index} 0', f'drop {index}']
lines += ['flags 1 -1', 'flags 1 2', 'flags 0 2', 'seek 1 -1',
'seek 1 4097', 'read 1 -1', 'read 1 4097', 'read 0 0',
'flags 0 0', 'write 0 -', 'seed 1 00 bad.name', 'info 1',
'seed 1 00 ' + 'n' * 32, 'info 1', 'seed 6 - ' + 'N' * 31,
'info 6', 'reset']
randomizer = random.Random(20261027)
payload = bytes(randomizer.randrange(256) for _ in range(LIMIT))
lines += ['seed 0 ' + payload.hex() + ' full', 'bind 0 7', 'read 7 4096',
'seek 0 0', 'write 7 ' + payload[::-1].hex(), 'seek 7 0',
'read 0 4096', 'seed 0 ' + (payload + b'x').hex() + ' over', 'write 0 ' + (payload + b'x').hex(), 'info 0', 'seed 3 - e', 'seek 3 6', 'write 3 000aff',
'seek 3 0', 'read 3 20', 'reset']
for iteration in range(180):
slot = randomizer.randrange(8)
choice = randomizer.randrange(10)
if choice == 0:
data = bytes(randomizer.randrange(256) for _ in range(randomizer.randrange(35)))
line = f'seed {slot} {token(data)} stream_{iteration}'
elif choice == 1:
line = f'bind {slot} {randomizer.randrange(8)}'
elif choice == 2:
line = f'read {slot} {randomizer.randrange(12)}'
elif choice == 3:
line = f'write {slot} {token(bytes([randomizer.randrange(256)] * randomizer.randrange(8)))}'
elif choice == 4:
line = f'seek {slot} {randomizer.choice([0, 1, 7, 4095, 4096])}'
elif choice == 5:
line = f'flags {slot} {randomizer.randrange(2)}'
elif choice == 6:
line = f'drop {slot}'
else:
line = f'info {slot}'
lines.append(line)
lines += ['reset'] + [f'info {i}' for i in range(8)]
return lines
def multi_bank_cases():
return ['use 0', 'seed 0 6162636465666768 first', 'flags 0 1', 'bind 0 1',
'read 0 2', 'info 1', 'use 1', 'seed 0 30313233343536373839 second',
'flags 0 1', 'bind 0 1', 'flags 1 1', 'read 0 1', 'read 1 2',
'info 0', 'info 1', 'use 0', 'reset', 'use 1', 'info 0', 'info 1',
'read 1 2', 'info 0', 'info 1', 'use 0', 'seed 0 78 reused',
'flags 0 1', 'drop 0', 'use 1', 'info 0', 'read 0 1', 'info 1',
'use 0', 'reset', 'use 1', 'drop 0', 'read 1 2', 'info 1',
'reset', 'use 0']
def equal(actual, expected):
if not isinstance(actual, dict):
return False
for key, wanted in expected.items():
if key == '_hex_text_chars':
continue
if key not in actual:
return False
value = actual[key]
if isinstance(wanted, (int, float)):
if isinstance(value, bool) or not isinstance(value, (int, float)) or value != wanted:
return False
elif key == 'buffer' and '_hex_text_chars' in expected:
if not isinstance(value, str) or not re.fullmatch('[0-9a-fA-F]*', value) or len(value) % 2:
return False
encoded = bytes.fromhex(value)
size = expected['_hex_text_chars']
if b'\0' not in encoded or encoded.index(0) != size:
return False
prefix = encoded[:size]
wanted_prefix = bytes.fromhex(wanted)[:size]
if not re.fullmatch(b'[0-9a-fA-F]*', prefix):
return False
if bytes.fromhex(prefix.decode('ascii')) != bytes.fromhex(wanted_prefix.decode('ascii')):
return False
elif key in ('data', 'buffer', 'a', 'b'):
if not isinstance(value, str) or value.lower() != wanted.lower():
return False
elif value != wanted:
return False
return True
def rows(result, label):
if result.returncode:
raise ValueError(label + ' exited ' + str(result.returncode) + ': ' + result.stderr[-500:].decode('utf-8', 'replace'))
try:
return [json.loads(line) for line in result.stdout.decode('utf-8').splitlines() if line.strip()]
except (ValueError, UnicodeDecodeError) as error:
raise ValueError(label + ' did not emit JSON records') from error
def compare(actual, expected, label):
if len(actual) != len(expected):
raise ValueError(label + ' record count differs')
for index, (found, wanted) in enumerate(zip(actual, expected)):
if not equal(found, wanted):
raise ValueError(label + ' observation differs at ' + str(index))
def helper_cases():
lines, expected = [], []
texts = ['', '0', '-0', '+0', '1', '-1', '2147483647', '-2147483648',
'2147483648', '-2147483649', '+', '-', ' 1', '1 ', '1x', '\v1', '\f1',
'1\n', '12', '--1', '++1', '+-1', '0' * 3000 + '7', '-00000000002']
for text in texts:
lines.append('parse ' + token(text.encode('utf-8')))
try:
value = i32(text)
expected.append(dict(ok(), value=value))
except ValueError:
expected.append(dict(failed(errno.EINVAL), value=1234567))
for text, capacity, null in [('-', 0, 0), ('-', 0, 1), ('00', 0, 0),
('00', 1, 0), ('00FfA1', 16, 0), ('00', 1, 1),
('a', 16, 0), ('-0', 16, 0), ('0g', 16, 0),
('', 16, 0), ('00 11', 16, 0), ('f' * 32, 16, 0),
('f' * 34, 16, 0)]:
lines.append(f'decode {capacity} {null} {token(text.encode())}')
buffer = bytearray([0xa5] * 16)
try:
data = hexbytes(text)
if len(data) > capacity or data and null:
raise ValueError('capacity')
if not null:
buffer[:len(data)] = data
expected.append(dict(ok(), length=len(data), buffer=buffer.hex()))
except ValueError:
expected.append(dict(failed(errno.EINVAL), length=777, buffer=buffer.hex()))
for data, capacity, null in [(b'', 0, 0), (b'', 1, 0), (b'', 1, 1),
(b'\x00\xff\xa1', 6, 0), (b'\x00\xff\xa1', 7, 0),
(b'abc', 24, 1), (bytes(range(8)), 24, 0)]:
lines.append(f'encode {capacity} {null} {token(data)}')
buffer = bytearray([0xa5] * 24)
if capacity < 2 * len(data) + 1 or data and null:
expected.append(dict(failed(errno.EINVAL), buffer=buffer.hex()))
else:
encoded = data.hex().encode() + b'\0'
buffer[:len(encoded)] = encoded
expected.append(dict(ok(), buffer=buffer.hex(), _hex_text_chars=2 * len(data)))
large = bytes(range(256)) * 16
for capacity in (4095, 4096):
lines.append(f'decodebig {large.hex()} {capacity}')
buffer = bytearray([0xa5] * 4097)
if capacity < len(large):
expected.append(dict(failed(errno.EINVAL), length=777, buffer=buffer.hex()))
else:
buffer[:len(large)] = large
expected.append(dict(ok(), length=len(large), buffer=buffer.hex()))
for capacity in (8192, 8193):
lines.append(f'encodebig {large.hex()} {capacity}')
buffer = bytearray([0xa5] * 8194)
if capacity < 2 * len(large) + 1:
expected.append(dict(failed(errno.EINVAL), buffer=buffer.hex()))
else:
encoded = large.hex().encode() + b'\0'
buffer[:len(encoded)] = encoded
expected.append(dict(ok(), buffer=buffer.hex(), _hex_text_chars=2 * len(large)))
for name in ['init', 'reset', 'seed_bank', 'seed_data', 'seed_name', 'bind',
'drop', 'take_bank', 'emit_bank', 'seek', 'flags', 'inspect_bank',
'inspect_view', 'parse_text', 'parse_value', 'decode_text',
'decode_length', 'encode_output', 'encode_data', 'encode_overflow', 'seed_empty']:
lines.append('null ' + name)
expected.append(ok() if name in ('init', 'seed_empty') else failed(errno.EINVAL))
return lines, expected
def main():
candidate = Path(sys.argv[1])
scratch = Path(tempfile.mkdtemp(prefix='descriptor-check-', dir=Path.cwd()))
for name in CANDIDATES:
(scratch / name).write_bytes((candidate / name).read_bytes())
(scratch / 'observer.c').write_bytes(Path(__file__).with_name('observer.c').read_bytes())
# All reads and scratch source writes precede the first untrusted child.
commands = bank_cases()
bank = Bank()
expected = [bank.action(line) for line in commands]
extra = ['seed 0 6162 nullcheck', 'readnull 0 0', 'readnull 0 1',
'writenull 0 0', 'writenull 0 1', 'readnull 7 0', 'writenull 7 0',
'info 0', 'reset']
expected += [bank.action(line) for line in extra]
multi_lines = multi_bank_cases()
banks, active = [bank, Bank()], 0
for line in multi_lines:
if line.startswith('use '):
active = int(line.split()[1])
expected.append(ok())
else:
expected.append(banks[active].action(line))
helper_lines, helper_expected = helper_cases()
raw_payloads = [b'', b'z', bytes(range(1, 18)), bytes(range(256))]
observer_input = commands + extra + multi_lines + helper_lines + ['raw ' + token(data) for data in raw_payloads]
console_commands = commands + ['seed +0000001 00aaFf lead',
'seek 0001 +0000000', 'read +01 0000003',
'flags 1 1', 'bind 0001 +0001', 'info 1',
'seed 0000000000000000000000000000000000000000000000002 - zeros',
'info ' + '0' * 12000 + '2',
'seek 1 1x', 'read 1 2147483648', 'flags 1 +',
'seek 1 \v0', 'seek 1 \f0', 'write 1 0g',
'seed 1 a broken', 'wat', 'read 1', 'read 1 1 extra',
'reset', 'info 1']
console_bank = Bank()
console_expected = [console_bank.action(line) for line in console_commands]
compile_base = ['gcc', '-std=c11', '-D_POSIX_C_SOURCE=200809L', '-O2', '-I', str(scratch)]
sources = [str(scratch / name) for name in LIBRARY]
for name, main_file in [('observe', 'observer.c'), ('console', 'console.c')]:
result = subprocess.run(compile_base + sources + [str(scratch / main_file), '-o', str(scratch / name)],
cwd=scratch, capture_output=True, timeout=30)
if result.returncode:
raise ValueError('build failed: ' + result.stderr[-1000:].decode('utf-8', 'replace'))
result = subprocess.run([str(scratch / 'observe')], input=('\n'.join(observer_input) + '\n').encode(),
cwd=scratch, capture_output=True, timeout=15)
observed = rows(result, 'API')
compare(observed[:len(expected) + len(helper_expected)], expected + helper_expected, 'API')
raw_rows = observed[len(expected) + len(helper_expected):]
if len(raw_rows) != 2 * len(raw_payloads):
raise ValueError('raw descriptor observation count differs')
for actual, vacant_row, payload in zip(raw_rows[::2], raw_rows[1::2], raw_payloads):
source, target = actual.get('source'), actual.get('target')
if type(source) is not int or type(target) is not int or source < 0 or target < 0 or source == target:
raise ValueError('raw descriptor identity missing')
first, second = payload[:2], payload[2:5]
position = len(first) + len(second)
wanted = dict(flag_rc=0, flag_err=0, before=1, self_rc=source, self_err=0, after=1,
bind_rc=target, bind_err=0, target_flags=0, na=len(first), nb=len(second),
a=first.hex(), b=second.hex(), pos=position, bad_rc=-1, bad_err=errno.EBADF,
preserved=position, negative_rc=-1, negative_err=errno.EBADF,
closed_rc=-1, closed_err=errno.EBADF, invalid_rc=-1, invalid_err=errno.EINVAL,
invalid_fd_rc=-1, invalid_fd_err=errno.EBADF, clear_rc=0, clear_err=0, clear_flags=0)
vacant = vacant_row.get('vacant')
if type(vacant) is not int or vacant < 0 or not equal(vacant_row, dict(rc=vacant, err=0, flags=0)):
raise ValueError('unopened target observation differs')
if not equal(actual, wanted):
raise ValueError('raw descriptor observation differs')
result = subprocess.run([str(scratch / 'console')], input=('\n'.join(console_commands) + '\n').encode(),
cwd=scratch, capture_output=True, timeout=15)
compare(rows(result, 'console'), console_expected, 'console')
print('PASS')
if __name__ == '__main__':
try:
main()
except (ValueError, subprocess.TimeoutExpired) as error:
print('FAIL: ' + str(error))
raise SystemExit(1)