Codex 69521e54e0 Add 15 embedded repair tasks and correct grader-read controls
Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
2026-10-16 14:23:48 +08:00

789 lines
32 KiB
Python

import errno
import json
from pathlib import Path
import random
import select
import subprocess
import struct
import sys
import tempfile
import termios
import zlib
def pty_cases(seed):
rng = random.Random(seed)
commands, expected = [], []
faults = [[0, 0] for _ in range(4)]
input_flags = sum(getattr(termios, name) for name in
('IGNBRK', 'BRKINT', 'PARMRK', 'ISTRIP', 'INLCR', 'IGNCR',
'ICRNL', 'IXON', 'IXOFF', 'IXANY'))
local_flags = sum(getattr(termios, name) for name in
('ECHO', 'ECHONL', 'ICANON', 'ISIG', 'IEXTEN', 'NOFLSH'))
def initial(value):
cc = bytearray((value + index) % 251 + 1 for index in range(termios.NCCS))
cc[termios.VMIN], cc[termios.VTIME] = 7, 9
# Linux n_tty exposes 19 kernel cc slots; libc reserves the remainder.
cc[19:] = bytes(len(cc) - 19)
return [input_flags, local_flags, termios.OPOST | termios.ONLCR,
1, 0, 0, 1, 1, 38400, 38400, (value >> 1) & 1, value & 1, cc]
banks = [dict(active=0, attrs=initial(0)), dict(active=0, attrs=initial(0))]
selected = 0
def snapshot(attrs):
return attrs[:-1] + [attrs[-1][:]]
def operation(index):
error, remaining = faults[index]
if remaining and error == errno.EINTR:
faults[index][1] = 0
return 0
if remaining:
faults[index][1] -= 1
return -error
return 0
def configure(attrs, baud, minimum, deciseconds, nonblocking):
value = snapshot(attrs)
value[0], value[1], value[2] = termios.IXANY, termios.NOFLSH, termios.ONLCR
value[5] = 1
value[8], value[9], value[10] = baud, baud, nonblocking
value[-1][termios.VMIN], value[-1][termios.VTIME] = minimum, deciseconds
return value
def command(name, *args):
nonlocal selected
commands.append(' '.join(map(str, (name,) + args)))
state = banks[selected]
if name == 'A':
selected = args[0]
state = banks[selected]
elif name == 'Z':
state['attrs'] = initial(args[0])
elif name == 'F':
faults[args[0]] = [args[1], args[2]]
elif name in ('B', 'U'):
baud, minimum, deciseconds, nonblocking = args
result = 0
if name == 'B' and state['active']:
result = -errno.EALREADY
elif name == 'U' and not state['active']:
result = -errno.ENODEV
elif baud not in (1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200) or minimum > 255 or deciseconds > 255 or nonblocking not in (0, 1):
result = -errno.EINVAL
else:
original = snapshot(state['attrs'])
for index in (0, 2, 1):
result = operation(index)
if result:
break
if not result:
prepared = configure(original, *args)
# Setting attributes and flags are separate transactions.
state['attrs'] = prepared[:-3] + original[-3:-1] + [prepared[-1]]
result = operation(3)
if result:
operation(1)
state['attrs'] = original
else:
state['attrs'] = prepared
if name == 'B':
state.update(active=1, saved=original)
expected.append('R %d' % result)
elif name == 'E':
result = 0
if state['active']:
result = operation(1)
if not result:
before = state['attrs']
saved = snapshot(state['saved'])
state['attrs'] = saved[:-3] + before[-3:-1] + [saved[-1]]
result = operation(3)
if not result:
state['attrs'] = saved
state['active'] = 0
expected.append('R %d' % result)
elif name == 'D':
expected.append('D ' + args[0])
attrs = state['attrs']
expected.append('S %d %s %s' %
(state['active'], ' '.join(map(str, attrs[:-1])), attrs[-1].hex()))
command('E')
command('U', 9600, 1, 0, 1)
for bad in [(12345, 1, 0, 0), (9600, 256, 0, 0), (9600, 0, 256, 0), (9600, 1, 0, 2)]:
command('B', *bad)
# Normal begin/update/end must restore the first snapshot, including other cc bytes.
for index in range(14):
command('Z', rng.randrange(251))
command('B', rng.choice((1200, 9600, 115200)), rng.randrange(256), rng.randrange(256), index & 1)
command('B', 4800, 1, 0, 1)
command('U', rng.choice((2400, 38400, 57600)), rng.randrange(256), rng.randrange(256), (index + 1) & 1)
command('U', 12345, 1, 0, 1)
command('E')
command('E')
# Each stage can be interrupted. Permanent failures preserve transaction state.
for operation_id in range(4):
command('Z', rng.randrange(251))
command('F', operation_id, errno.EINTR, 3)
command('B', 19200, 0, 1, 1)
command('F', operation_id, errno.EINTR, 2)
command('U', 115200, 1, 0, 0)
command('F', operation_id, errno.EINTR, 0)
command('F', 1, errno.EINTR, 3)
command('F', 3, errno.EINTR, 2)
command('E')
command('Z', rng.randrange(251))
command('F', operation_id, errno.EIO, 1)
command('B', 4800, 1, 0, 1)
command('B', 9600, 1, 0, 1)
command('F', operation_id, errno.EIO, 1)
command('U', 57600, 0, 1, 0)
command('F', operation_id, errno.EIO, 0)
command('E')
for operation_id in (1, 3):
command('Z', 3)
command('B', 9600, 1, 0, 0)
command('F', operation_id, errno.EIO, 1)
command('E')
command('E')
# Independent sessions remain active while the other one is reconfigured.
command('A', 0)
command('Z', 91)
command('B', 9600, 1, 0, 1)
command('A', 1)
command('Z', 47)
command('B', 57600, 1, 0, 0)
command('U', 115200, 1, 0, 1)
command('A', 0)
command('U', 38400, 1, 0, 1)
command('E')
command('A', 1)
command('E')
# This final test crosses an actual kernel PTY; no scripted termios substitute.
command('Z', 17)
command('B', 115200, 1, 0, 1)
command('D', '-')
command('D', bytes([0x80, 0xff, 0x0d, 0x0a, 0x11, 0x13, 0x03, 0x7f, 0, 0x42]).hex())
command('D', bytes(rng.randrange(256) for _ in range(37)).hex())
command('D', bytes(rng.randrange(256) for _ in range(512)).hex())
command('E')
return commands, expected
def event_cases(seed):
rng = random.Random(seed)
commands, expected = [], []
def fresh():
return dict(fragment=bytearray(), keys=0, pending=0, dx=0, dy=0, repeats=0,
dropping=0, frames=0, lost=0, callback=True)
banks = [dict(reader=fresh(), queue=bytearray(), chunk=24, read_error=0,
read_count=0, snapshot_error=0, snapshot_count=0, snapshots=0,
snapshot=0, eof=0) for _ in range(2)]
selected = 0
def clear(reader):
reader.update(pending=reader['keys'], dx=0, dy=0, repeats=0)
def report(reader, recovered):
reader['frames'] += 1
if reader['callback']:
expected.append('D %d %d %d %d %d %d %d' %
(selected, recovered, reader['keys'], reader['dx'], reader['dy'],
reader['repeats'], reader['frames']))
def event(state, raw):
reader = state['reader']
_, _, kind, code, value = struct.unpack('@llHHi', raw)
if kind == 0 and code == 3:
if not reader['dropping']:
reader['lost'] += 1
clear(reader)
reader['dropping'] = 1
elif reader['dropping']:
if kind == 0 and code == 0:
while True:
state['snapshots'] += 1
if state['snapshot_count']:
state['snapshot_count'] -= 1
if state['snapshot_error'] == errno.EINTR:
continue
return -state['snapshot_error']
break
reader['keys'] = state['snapshot']
clear(reader)
reader['dropping'] = 0
report(reader, 1)
elif kind == 0 and code == 0:
reader['keys'] = reader['pending']
report(reader, 0)
clear(reader)
elif kind == 1 and code < 32:
if value == 0: reader['pending'] &= ~(1 << code)
elif value == 1: reader['pending'] |= 1 << code
elif value == 2: reader['repeats'] += 1
elif kind == 2 and code in (0, 1):
reader['dx' if code == 0 else 'dy'] += value
return 0
def pump(state):
reader = state['reader']
while True:
if state['read_count']:
state['read_count'] -= 1
if state['read_error'] == errno.EINTR:
continue
if state['read_error'] in (errno.EAGAIN, errno.EWOULDBLOCK):
return 0
return -state['read_error']
if not state['queue']:
if not state['eof']:
return 0
if reader['fragment']:
reader['fragment'].clear()
clear(reader)
return -errno.EPROTO
return 1
count = min(len(state['queue']), state['chunk'], 24 - len(reader['fragment']))
reader['fragment'].extend(state['queue'][:count])
del state['queue'][:count]
if len(reader['fragment']) == 24:
raw = bytes(reader['fragment'])
reader['fragment'].clear()
result = event(state, raw)
if result:
return result
def command(name, *args):
nonlocal selected
commands.append(' '.join(map(str, (name,) + args)))
if name == 'A': selected = args[0]
state = banks[selected]
reader = state['reader']
if name == 'E': state['queue'].extend(struct.pack('@llHHi', 0, 0, *args))
elif name == 'Q': state['queue'].extend(bytes.fromhex(args[0]) if args[0] != '-' else b'')
elif name == 'C': state['chunk'] = args[0]
elif name == 'K': state['snapshot'] = args[0]
elif name == 'F':
prefix = 'snapshot' if args[0] else 'read'
state[prefix + '_error'], state[prefix + '_count'] = args[1:]
elif name == 'X': state['eof'] = args[0]
elif name in ('I', 'N'):
state['reader'] = reader = fresh()
reader['callback'] = name == 'I'
elif name == 'T':
reader['fragment'].clear()
clear(reader)
elif name == 'P': expected.append('R %d' % pump(state))
expected.append('S %d %d %d %d %d %d %d %d %d %d %d' %
(len(reader['fragment']), reader['dropping'], reader['keys'], reader['pending'],
reader['dx'], reader['dy'], reader['repeats'], reader['frames'], reader['lost'],
state['snapshots'], len(state['queue'])))
command('P')
command('E', 1, 7, 1)
command('P')
command('E', 2, 0, 100)
command('E', 2, 1, -50)
command('E', 1, 7, 2)
command('E', 0, 0, 0)
command('P')
# All byte splits across separate EAGAIN-bounded pump calls, not just read sizes.
for at in range(1, 24):
command('I')
raw = struct.pack('@llHHi', rng.randrange(100000), 123, 1, at, 1)
command('Q', raw[:at].hex())
command('P')
command('F', 0, errno.EINTR, 2)
command('Q', raw[at:].hex())
command('E', 0, 0, 0)
command('C', at)
command('P')
for index in range(35):
for _ in range(rng.randrange(1, 9)):
command('E', rng.choice((1, 1, 2, 3)), rng.randrange(36), rng.choice((-1, 0, 1, 2, 3, 2147483647, -2147483648)))
command('E', 2, index & 1, rng.randrange(-100000, 100000))
command('E', 0, 0, 0)
command('C', rng.randrange(1, 49))
command('P')
# Loss invalidates uncommitted state; a snapshot owns recovery, repeat isn't down.
for fail_error in (0, errno.EINTR, errno.EIO):
command('E', 1, 31, 1)
command('E', 2, 1, 500)
command('P')
command('E', 0, 3, 0)
command('E', 1, 5, 1)
command('E', 0, 3, 0)
command('K', rng.getrandbits(32))
if fail_error: command('F', 1, fail_error, 2 if fail_error == errno.EINTR else 1)
command('E', 0, 0, 0)
command('E', 1, 1, 2)
command('E', 0, 0, 0)
command('P')
command('P')
command('F', 0, errno.EIO, 1)
command('E', 1, 0, 1)
command('E', 0, 0, 0)
command('P')
command('P')
command('Q', '0001020304')
command('P')
command('X', 1)
command('P')
command('P')
command('X', 0)
command('A', 1)
command('Q', struct.pack('@llHHi', 0, 0, 1, 31, 1)[:11].hex())
command('P')
command('A', 0)
command('N')
command('E', 1, 2, 1)
command('E', 0, 0, 0)
command('P')
command('A', 1)
command('Q', struct.pack('@llHHi', 0, 0, 1, 31, 1)[11:].hex())
command('E', 0, 0, 0)
command('P')
command('Q', 'aabbcc')
command('P')
command('T')
command('E', 1, 1, 1)
command('E', 0, 0, 0)
command('P')
return commands, expected
def hash_bytes(data):
value = 2166136261
for byte in data:
value = ((value ^ byte) * 16777619) & 0xffffffff
return value
def transfer_cases(seed):
rng = random.Random(seed)
commands, expected = [], []
def fresh():
return dict(request=b'', response_size=0, sent=0, response=bytearray(), done=1,
now=0, wire=bytearray(), writes=[], reads=[], polls=[])
banks = [fresh(), fresh()]
selected = 0
def drive(state, budget):
if state['done']:
return 0
expires = state['now'] + budget
def remaining():
return max(0, expires - state['now'])
while not state['done']:
if not remaining():
return -errno.ETIMEDOUT
sending = state['sent'] < len(state['request'])
if sending:
capacity = len(state['request']) - state['sent']
amount, error, cost = state['writes'].pop(0) if state['writes'] else (512, 0, 1)
state['now'] += cost
if error:
expected.append('W %d -1 %d -' % (capacity, error))
result = -error
else:
amount = min(amount, capacity)
raw = state['request'][state['sent']:state['sent'] + amount]
state['wire'].extend(raw)
expected.append('W %d %d 0 %s' % (capacity, amount, raw.hex() or '-'))
result = 0 if amount else -errno.EIO
state['sent'] += amount
else:
capacity = state['response_size'] - len(state['response'])
if state['reads']:
raw, error, cost = state['reads'][0]
state['now'] += cost
if error:
state['reads'].pop(0)
expected.append('R %d -1 %d -' % (capacity, error))
result = -error
else:
piece = raw[:capacity]
if len(raw) > capacity: state['reads'][0] = (raw[capacity:], error, cost)
else: state['reads'].pop(0)
state['response'].extend(piece)
expected.append('R %d %d 0 %s' % (capacity, len(piece), piece.hex() or '-'))
result = 0 if piece else -errno.EPIPE
else:
state['now'] += 1
expected.append('R %d -1 %d -' % (capacity, errno.EAGAIN))
result = -errno.EAGAIN
if result == -errno.EINTR:
continue
if result in (-errno.EAGAIN, -errno.EWOULDBLOCK):
events = select.POLLOUT if sending else select.POLLIN
while True:
timeout = remaining()
if not timeout:
return -errno.ETIMEDOUT
if state['polls']:
observed_result, observed, error, cost = state['polls'].pop(0)
state['now'] += cost
else:
observed_result, observed, error = 0, 0, 0
state['now'] += timeout
expected.append('P %d %d %d %d %d' % (events, timeout, observed_result, observed, error))
if observed_result < 0:
if error == errno.EINTR: continue
return -error
if observed_result == 0: return -errno.ETIMEDOUT
if observed & select.POLLNVAL: return -errno.EBADF
if sending and observed & (select.POLLHUP | select.POLLERR): return -errno.EPIPE
if not sending and observed & select.POLLERR and not observed & (select.POLLIN | select.POLLHUP): return -errno.EIO
if observed & events or not sending and observed & select.POLLHUP:
break
continue
if result:
return result
state['done'] = int(state['sent'] == len(state['request']) and len(state['response']) == state['response_size'])
return 0
def command(name, *args):
nonlocal selected
commands.append(' '.join(map(str, (name,) + args)))
if name == 'A': selected = args[0]
if name == 'Z': banks[selected] = fresh()
state = banks[selected]
if name == 'I':
raw = bytes.fromhex(args[0]) if args[0] != '-' else b''
good = len(raw) <= 512 and args[1] <= 512
if good: state.update(request=raw, response_size=args[1], sent=0, response=bytearray(), done=int(not raw and not args[1]))
expected.append('I %d %d %d' % (0 if good else -errno.EINVAL, hash_bytes(raw), hash_bytes(raw)))
elif name == 'C': state['now'] = args[0]
elif name == 'W': state['writes'].append(args)
elif name == 'R': state['reads'].append((bytes.fromhex(args[0]) if args[0] != '-' else b'', *args[1:]))
elif name == 'P': state['polls'].append(args)
elif name == 'D': expected.append('D %d' % drive(state, args[0]))
expected.append('S %d %d %d %d %s %s %d %d %d' %
(state['sent'], len(state['response']), state['done'], state['now'],
state['response'].hex() or '-', state['wire'].hex() or '-',
len(state['writes']), len(state['reads']), len(state['polls'])))
command('D', 0)
for length in (0, 1, 2, 31, 255, 512):
command('Z')
request = bytes(rng.randrange(256) for _ in range(length))
response = bytes(rng.randrange(256) for _ in range(513 - length)) if length else b''
command('I', request.hex() or '-', len(response))
if len(response) > 512:
continue
for _ in range(max(1, length // 10)):
command('W', rng.randrange(1, 11), 0, 1)
if response: command('R', response.hex(), 0, 1)
command('D', 1000)
command('D', 0)
# Partial send, poll EINTR, timeout, and resume must never duplicate bytes.
for index in range(18):
command('Z')
request = bytes(rng.randrange(256) for _ in range(30 + index))
response = bytes(rng.randrange(256) for _ in range(17 + index))
command('I', request.hex(), len(response))
command('W', 3, 0, 2)
command('W', 0, errno.EINTR, 2)
command('W', 0, errno.EAGAIN, 1)
command('P', -1, 0, errno.EINTR, 3)
command('P', 1, select.POLLOUT, 0, 2)
command('D', 10)
command('R', response[:7].hex(), 0, 1)
command('R', '-', errno.EAGAIN, 1)
command('P', 1, select.POLLIN | select.POLLHUP, 0, 2)
command('R', '-', errno.EINTR, 1)
command('R', response[7:].hex(), 0, 1)
command('D', 20)
for events in (select.POLLNVAL, select.POLLERR, select.POLLHUP, select.POLLIN, select.POLLOUT,
select.POLLOUT | select.POLLERR, select.POLLOUT | select.POLLHUP,
select.POLLOUT | select.POLLNVAL, select.POLLIN | select.POLLERR,
select.POLLIN | select.POLLHUP | select.POLLERR,
select.POLLIN | select.POLLNVAL):
for sending in (False, True):
command('Z')
command('I', 'abcd' if sending else '-', 0 if sending else 3)
command('W' if sending else 'R', 0 if sending else '-', errno.EAGAIN, 1)
command('P', 1, events, 0, 2)
if not sending: command('R', '010203', 0, 1)
command('D', 9)
for error in (errno.EIO, errno.EPIPE, errno.EBADF):
command('Z')
command('I', '11223344', 0)
command('W', 1, 0, 1)
command('W', 0, error, 1)
command('D', 20)
command('D', 20)
command('Z')
command('I', '-', 4)
command('R', '0102', 0, 1)
command('R', '-', 0, 1)
command('D', 20)
command('R', '0304', 0, 1)
command('D', 20)
command('Z')
command('I', '01020304', 2)
command('D', 0)
command('W', 0, 0, 1)
command('D', 20)
command('I', '01' * 513, 0)
command('I', '-', 513)
command('A', 1)
command('I', '04030201', 2)
command('R', '1213', 0, 1)
command('D', 3)
command('A', 0)
command('R', '1415', 0, 1)
command('D', 10)
command('A', 1)
command('D', 3)
return commands, expected
def journal_record(sequence, payload):
prefix = b'LOG1' + struct.pack('<HHI', len(payload), 0, sequence)
return prefix + struct.pack('<I', zlib.crc32(prefix + payload)) + payload + struct.pack('<I', sequence ^ 0xffffffff)
def journal_cases(seed):
rng = random.Random(seed)
commands, expected = [], []
file = bytearray()
state = dict(opened=0, end=0, sequence=1, dirty=0)
faults = [[0, 0] for _ in range(4)]
def operation(index):
error, count = faults[index]
if count:
if error == errno.EINTR:
faults[index][1] = 0
return 0
faults[index][1] -= 1
return -error
return 0
def recover():
position, sequence = 0, 1
while position < len(file):
result = operation(0)
if result: return result
if len(file) - position < 16: break
magic = bytes(file[position:position + 4])
size, flags, current, checksum = struct.unpack('<HHII', file[position + 4:position + 16])
if magic != b'LOG1' or size > 240 or flags or current != sequence: break
result = operation(0)
if result: return result
end = position + 20 + size
if end > len(file): break
body = bytes(file[position + 16:end - 4])
if zlib.crc32(bytes(file[position:position + 12]) + body) != checksum: break
if int.from_bytes(file[end - 4:end], 'little') != (sequence ^ 0xffffffff): break
position, sequence = end, sequence + 1
state.update(opened=1, end=position, sequence=sequence, dirty=int(position != len(file)))
return 0
def append(payload):
if not state['opened']: return -errno.ENODEV
if len(payload) > 240: return -errno.EINVAL
state['dirty'] = 1
result = operation(2)
if result: return result
del file[state['end']:]
record = journal_record(state['sequence'], payload)
result = operation(1)
if result: return result
file.extend(record)
result = operation(3)
if result: return result
state.update(end=len(file), sequence=state['sequence'] + 1, dirty=0)
return 0
def command(name, *args):
commands.append(' '.join(map(str, (name,) + args)))
if name == 'Z':
file[:] = bytes.fromhex(args[0]) if args[0] != '-' else b''
state.update(opened=0, end=0, sequence=1, dirty=0)
elif name == 'F': faults[args[0]] = [args[1], args[2]]
elif name == 'O': expected.append('O %d' % recover())
elif name == 'A':
payload = bytes.fromhex(args[0]) if args[0] != '-' else b''
expected.append('A %d %d %d' % (append(payload), hash_bytes(payload), hash_bytes(payload)))
expected.append('S %d %d %d %d %s' %
(state['opened'], state['end'], state['sequence'], state['dirty'], file.hex() or '-'))
command('A', '00')
command('Z', '-')
command('C', 1, 1)
command('O')
for size in (0, 1, 2, 16, 239, 240):
command('A', bytes(rng.randrange(256) for _ in range(size)).hex() or '-')
command('A', 'ab' * 241)
command('O')
first = journal_record(1, b'boot')
second = journal_record(2, bytes(range(41)))
third = journal_record(3, b'next')
for at in range(len(second)):
command('Z', (first + second[:at]).hex())
command('C', 1 + at % 11, 1 + at % 13)
command('O')
command('A', bytes([at, 0, 255]).hex())
command('O')
for offset in range(len(second)):
damaged = bytearray(second)
damaged[offset] ^= 1 << (offset % 8)
command('Z', (first + damaged + third).hex())
command('O')
command('A', '010203')
for operation_id in range(4):
for error in (errno.EINTR, errno.EIO):
command('Z', (first + b'partial').hex())
command('O')
command('F', operation_id, error, 2 if error == errno.EINTR else 1)
command('O' if operation_id == 0 else 'A', *(() if operation_id == 0 else ('aabbccdd',)))
command('A', '12345678')
command('O')
command('Z', (journal_record(2, b'wrong-initial') + first).hex())
command('O')
command('A', '99')
return commands, expected
def signal_cases(seed):
rng = random.Random(seed)
commands, expected = [], []
state = dict(active=0, pending=0, counts=[0, 0, 0], wake=0, blocked=0, deferred=0,
old=[0, 0, 0], pipe=0)
faults = [[0, 0] for _ in range(3)]
def operation(index):
error, count = faults[index]
if count:
if error == errno.EINTR:
faults[index][1] = 0
return 0
faults[index][1] -= 1
return -error
return 0
def note(index):
if not state['active']: return
state['pending'] |= 1 << index
state['counts'][index] += 1
result = operation(1)
if result and result not in (-errno.EAGAIN, -errno.EWOULDBLOCK): state['wake'] = -result
if not result: state['pipe'] += 1
def deliver(index):
if state['active']: note(index)
else: state['old'][index] += 1
def command(name, *args):
commands.append(' '.join(map(str, (name,) + args)))
if name == 'I':
result = -errno.EALREADY if state['active'] else 0
if not result: state.update(active=1, pending=0, counts=[0, 0, 0], wake=0, pipe=0)
expected.append('I %d' % result)
elif name == 'E':
state.update(active=0, pending=0, counts=[0, 0, 0], wake=0, pipe=0)
expected.append('E 0')
elif name == 'F': faults[args[0]] = [args[1], args[2]]
elif name == 'R':
index = args[0]
if state['blocked'] & (1 << index): state['deferred'] |= 1 << index
else: deliver(index)
expected.append('R 0')
elif name == 'H':
note(args[0])
expected.append('H %d' % args[1])
elif name == 'B':
state['blocked'] = args[0]
for index in range(3):
if state['deferred'] & (1 << index) and not state['blocked'] & (1 << index):
state['deferred'] &= ~(1 << index)
deliver(index)
expected.append('B 0')
elif name == 'C':
nonce = args[0]
observed = [nonce] * 5
result = -errno.ENODEV if not state['active'] else operation(2)
if not result:
result = operation(0)
if result in (-errno.EAGAIN, -errno.EWOULDBLOCK):
result = 0
if not result:
observed = [state['pending'], *state['counts'], state['wake']]
state.update(pending=0, counts=[0, 0, 0], wake=0, pipe=0)
expected.append('C %d %s' % (result, ' '.join(map(str, observed))))
expected.append('S %d %d %s %d %d %d %s' %
(state['active'], state['pending'], ' '.join(map(str, state['counts'])),
state['wake'], state['blocked'], state['deferred'], ' '.join(map(str, state['old']))))
command('C', 19)
command('I')
command('I')
for _ in range(17): command('R', rng.randrange(3))
command('C', 71)
command('C', 83)
for operation_id in range(3):
for error in (errno.EINTR, errno.EAGAIN, errno.EIO):
command('F', operation_id, error, 2 if error == errno.EINTR else 1)
command('H', operation_id, 123)
command('C', 91)
command('C', 92)
for mask in range(8):
command('B', mask)
for index in range(3):
command('R', index)
command('R', index)
command('C', 17)
command('B', 0)
command('C', 23)
command('B', 7)
command('R', 0)
command('R', 2)
command('E')
command('B', 0)
command('R', 1)
command('I')
command('R', 2)
command('C', 29)
command('E')
return commands, expected
def main():
private = Path(__file__).resolve().parent
data = json.loads((private / 'context.json').read_text())
cases = {'pty': pty_cases, 'event': event_cases, 'transfer': transfer_cases,
'journal': journal_cases, 'signal': signal_cases}
commands, expected = cases[data['kind']](data['seed'])
scratch = Path(tempfile.mkdtemp(prefix='embedded2-abi-' + data['kind'] + '-', dir='/tmp'))
for name, source in data['context'].items():
(scratch / name).write_text(source)
for name in data['candidates']:
(scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
# All filesystem work completes before candidate execution. The console contains
# only parsing, real/syscall-stub IO, and raw observations; the oracle stays here.
build = subprocess.run(['gcc', '-std=c11', '-O2', '-Wall', '-Wextra', '-pedantic',
*data['sources'], '-o', 'observe'], cwd=scratch,
capture_output=True, text=True, timeout=20)
if build.returncode:
print('build failed: ' + build.stderr[-1000:].replace('\n', ' '))
return 1
result = subprocess.run([str(scratch / 'observe')], cwd=scratch,
input='\n'.join(commands) + '\n',
capture_output=True, text=True, timeout=15)
# shortcut: scratch is intentionally retained until the owning controller or
# trusted-local batch owner removes its exact task prefix after all runs finish.
# Never touch the shared scratch filesystem after the candidate has executed.
sys.stderr.write(result.stderr[-3000:])
if result.returncode:
print('candidate exit %d' % result.returncode)
return 1
actual = result.stdout.splitlines()
if actual != expected:
index = next((index for index in range(min(len(actual), len(expected)))
if actual[index] != expected[index]), min(len(actual), len(expected)))
got = actual[index] if index < len(actual) else '<missing>'
wanted = expected[index] if index < len(expected) else '<end>'
print('observation %d got %r expected %r' % (index, got[:220], wanted[:220]))
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())