Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
579 lines
20 KiB
Python
579 lines
20 KiB
Python
"""Frozen C contexts, subprocess observations, and independent Python peripheral models."""
|
|
import json
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
def i2c_cases(config):
|
|
rng = random.Random(config['seed'])
|
|
events = []
|
|
|
|
def q(identifier, tx, rx, address=51):
|
|
events.append(('Q', identifier, address, len(tx), rx, *tx))
|
|
|
|
def irq(flag, byte=0):
|
|
events.append(('I', flag, byte))
|
|
|
|
def finish(tx, rx):
|
|
irq(1)
|
|
if tx:
|
|
for _ in tx:
|
|
irq(2)
|
|
if rx:
|
|
irq(1)
|
|
for _ in range(rx):
|
|
irq(4, rng.randrange(256))
|
|
irq(8)
|
|
|
|
# Each normal path is independent of queue deletion and error handling.
|
|
for writes in (0, 1, 2, 16):
|
|
for reads in (0, 1, 2, 3, 16):
|
|
if writes + reads == 0:
|
|
continue
|
|
events.append(('R',))
|
|
data = [rng.randrange(256) for _ in range(writes)]
|
|
q(1, data, reads)
|
|
finish(data, reads)
|
|
irq(8)
|
|
irq(0)
|
|
# After a prior dequeue, queue slots are a ring, not zero based.
|
|
for cancel_offset in (0, 1, 2):
|
|
events.append(('R',))
|
|
q(1, [17], 0)
|
|
q(2, [18], 0)
|
|
finish([17], 0)
|
|
for identifier in (3, 4, 5):
|
|
q(identifier, [identifier * 13], 0)
|
|
events.append(('C', 3 + cancel_offset))
|
|
q(6, [78], 0)
|
|
for identifier in (2, 3, 4, 5, 6):
|
|
finish([identifier], 0)
|
|
events.append(('R',))
|
|
q(91, [17], 0)
|
|
q(92, [19], 0)
|
|
q(92, [39], 0)
|
|
finish([17], 0)
|
|
finish([19], 0)
|
|
# Admission, duplicate identifiers, full queue, unknown and repeated cancellation.
|
|
events.append(('R',))
|
|
for identifier, address, tx, rx in ((0, 10, [1], 0), (9, 128, [1], 0),
|
|
(10, 1, [], 0), (11, 1, [], 17)):
|
|
events.append(('Q', identifier, address, len(tx), rx, *tx))
|
|
for identifier in (1, 2, 3, 4, 5, 2):
|
|
q(identifier, [identifier], 1)
|
|
events.extend([('C', 99), ('C', 3), ('C', 3), ('C', 1), ('C', 1)])
|
|
irq(1 | 4 | 16)
|
|
irq(8)
|
|
q(1, [], 1)
|
|
for _ in range(6):
|
|
finish([0], 1)
|
|
# Flags are snapshot-consumed; one phase transition, errors dominate progress.
|
|
for flags in (1 | 2, 1 | 4, 2 | 4, 1 | 2 | 4 | 8, 16 | 1, 32 | 16,
|
|
32 | 8, 16 | 8, 63):
|
|
events.append(('R',))
|
|
q(1, [7, 9], 2)
|
|
q(2, [], 2)
|
|
irq(flags, 201)
|
|
for _ in range(4):
|
|
irq(1)
|
|
irq(2)
|
|
irq(4, rng.randrange(256))
|
|
irq(8)
|
|
# Cancellation at each stage, including an already pending successful STOP.
|
|
for prefix in ([], [1], [1, 2], [1, 2, 2], [1, 2, 2, 1], [1, 2, 2, 1, 4],
|
|
[1, 2, 2, 1, 4, 4]):
|
|
events.append(('R',))
|
|
q(1, [1, 2], 2)
|
|
q(2, [3], 0)
|
|
for flag in prefix:
|
|
irq(flag, 115)
|
|
events.append(('C', 1))
|
|
irq(16 | 4)
|
|
irq(8)
|
|
finish([3], 0)
|
|
# Reuse of ids, error recovery, queue churn, and randomized register snapshots.
|
|
for round_index in range(config['rounds']):
|
|
if round_index % 10 == 0:
|
|
events.append(('R',))
|
|
mode = rng.randrange(4)
|
|
if mode == 0:
|
|
q(rng.randrange(1, 13), [rng.randrange(256) for _ in range(rng.randrange(5))],
|
|
rng.randrange(5), rng.randrange(126, 130) if round_index % 9 == 0 else rng.randrange(128))
|
|
elif mode == 1:
|
|
events.append(('C', rng.randrange(1, 13)))
|
|
else:
|
|
irq(rng.choice((0, 1, 2, 4, 8, 16, 32, 3, 5, 12, 24, 48, 63)), rng.randrange(256))
|
|
# A final pending transaction is deliberately not completed by EOF.
|
|
events.append(('R',))
|
|
q(71, [17, 39], 3)
|
|
irq(1)
|
|
return events
|
|
|
|
|
|
def i2c_expected(events):
|
|
output = []
|
|
queue = []
|
|
active = None
|
|
phase = 0
|
|
position = [0, 0]
|
|
counts = [0] * 7 # submitted, completed, failed, cancelled, rejected, interrupts, ignored
|
|
writes = reads = pending = busy = 0
|
|
status = 0
|
|
operation = 0
|
|
|
|
def emit(name, a, b=0):
|
|
output.append('E %d %s %d %d' % (operation, name, a, b))
|
|
|
|
def begin(request):
|
|
nonlocal active, phase, status, position, busy
|
|
active = request
|
|
position = [0, 0]
|
|
status = 0
|
|
phase = 1 if request[2] else 3
|
|
busy = 1
|
|
emit('START', request[1], 0 if request[2] else 1)
|
|
|
|
def complete():
|
|
nonlocal active, phase, busy, position
|
|
counts[{0: 1, 1: 2, 2: 2, 3: 3}[status]] += 1
|
|
emit('DONE', active[0], status)
|
|
phase = 0
|
|
active = None
|
|
position = [0, 0]
|
|
busy = 0
|
|
if queue:
|
|
begin(queue.pop(0))
|
|
|
|
def stop(result):
|
|
nonlocal phase, status
|
|
status = result
|
|
phase = 5
|
|
emit('STOP', 0)
|
|
|
|
for operation, event in enumerate(events, 1):
|
|
kind = event[0]
|
|
if kind == 'R':
|
|
queue = []
|
|
active = None
|
|
phase = 0
|
|
position = [0, 0]
|
|
counts = [0] * 7
|
|
writes = reads = pending = busy = 0
|
|
status = 0
|
|
elif kind == 'Q':
|
|
identifier, address, txlen, rxlen = event[1:5]
|
|
data = list(event[5:])
|
|
request = (identifier, address, data, rxlen)
|
|
occupied = [x[0] for x in queue] + ([active[0]] if active else [])
|
|
if not identifier or address > 127 or rxlen > 16 or txlen + rxlen == 0 or identifier in occupied or (active and len(queue) == 3):
|
|
counts[4] += 1
|
|
emit('REJECT', identifier)
|
|
else:
|
|
counts[0] += 1
|
|
if active is None:
|
|
begin(request)
|
|
else:
|
|
queue.append(request)
|
|
elif kind == 'C':
|
|
identifier = event[1]
|
|
if active is not None and identifier == active[0]:
|
|
if phase != 5:
|
|
stop(3)
|
|
else:
|
|
for i, request in enumerate(queue):
|
|
if request[0] == identifier:
|
|
del queue[i]
|
|
counts[3] += 1
|
|
emit('DONE', identifier, 3)
|
|
break
|
|
else:
|
|
flags, byte = event[1:]
|
|
pending |= flags & 63
|
|
flags = pending
|
|
counts[5] += 1
|
|
if flags:
|
|
pending = 0
|
|
emit('CLEAR', flags, 0)
|
|
if not flags or not active:
|
|
counts[6] += 1
|
|
elif phase == 5:
|
|
if flags & 8:
|
|
complete()
|
|
else:
|
|
counts[6] += 1
|
|
elif flags & 32:
|
|
status = 2
|
|
complete()
|
|
elif flags & 16:
|
|
stop(1)
|
|
elif phase in (1, 3) and flags & 1:
|
|
if phase == 1:
|
|
phase = 2
|
|
writes += 1
|
|
emit('WRITE', active[2][position[0]], writes)
|
|
position[0] += 1
|
|
else:
|
|
phase = 4
|
|
emit('ACK', int(active[3] > 1))
|
|
elif phase == 2 and flags & 2:
|
|
if position[0] < len(active[2]):
|
|
writes += 1
|
|
emit('WRITE', active[2][position[0]], writes)
|
|
position[0] += 1
|
|
elif active[3]:
|
|
phase = 3
|
|
emit('START', active[1], 1)
|
|
else:
|
|
stop(0)
|
|
elif phase == 4 and flags & 4:
|
|
reads += 1
|
|
emit('READ', byte, reads)
|
|
position[1] += 1
|
|
remaining = active[3] - position[1]
|
|
if remaining == 1:
|
|
emit('ACK', 0)
|
|
if remaining == 0:
|
|
stop(0)
|
|
else:
|
|
counts[6] += 1
|
|
values = [operation, active[0] if active else 0, phase, *position, len(queue), *counts, pending, busy]
|
|
output.append('S ' + ' '.join(map(str, values)))
|
|
return output
|
|
|
|
|
|
def spi_expected(events):
|
|
out = []
|
|
active = None
|
|
sent = received = completed = cancelled = rejected = overrun = 0
|
|
tx, rx = [], []
|
|
operation = 0
|
|
def e(name, a=0, b=0):
|
|
out.append('E %d %s %d %d' % (operation, name, a, b))
|
|
def service():
|
|
nonlocal sent, received, completed, active
|
|
if active is None:
|
|
return
|
|
while rx:
|
|
value = rx.pop(0)
|
|
e('READ', value, received)
|
|
received += 1
|
|
while len(tx) < 4 and sent < len(active[1]):
|
|
value = active[1][sent]
|
|
tx.append(value)
|
|
e('WRITE', value, sent)
|
|
sent += 1
|
|
if received == len(active[1]):
|
|
identifier = active[0]
|
|
e('CS', 0)
|
|
active = None
|
|
completed += 1
|
|
e('DONE', identifier, 0)
|
|
for operation, event in enumerate(events, 1):
|
|
k = event[0]
|
|
if k == 'R':
|
|
active = None
|
|
sent = received = completed = cancelled = rejected = overrun = 0
|
|
tx, rx = [], []
|
|
elif k == 'Q':
|
|
identifier, length = event[1:3]
|
|
if active is not None or identifier == 0 or length == 0 or length > 32:
|
|
rejected += 1
|
|
e('REJECT', identifier)
|
|
else:
|
|
active = (identifier, list(event[3:]))
|
|
sent = received = 0
|
|
e('CS', 1)
|
|
service()
|
|
elif k == 'X':
|
|
clocks, xor = event[1:]
|
|
for _ in range(clocks):
|
|
if active is None or not tx:
|
|
break
|
|
value = tx.pop(0) ^ xor
|
|
if len(rx) == 4:
|
|
overrun += 1
|
|
e('OVERRUN', value)
|
|
else:
|
|
rx.append(value)
|
|
e('SHIFT', value, len(rx))
|
|
elif k == 'I':
|
|
service()
|
|
elif k == 'C' and active is not None and event[1] == active[0]:
|
|
identifier = active[0]
|
|
tx, rx = [], []
|
|
e('FLUSH')
|
|
e('CS', 0)
|
|
active = None
|
|
cancelled += 1
|
|
e('DONE', identifier, 3)
|
|
values = [operation, active[0] if active else 0, sent, received, len(tx), len(rx),
|
|
completed, cancelled, rejected, overrun, int(active is not None)]
|
|
out.append('S ' + ' '.join(map(str, values)))
|
|
return out
|
|
|
|
|
|
def irq_expected(events):
|
|
out = []
|
|
pending = 0
|
|
enabled = 7
|
|
values = [0] * 3
|
|
sequence = [0] * 3
|
|
delivered = [0] * 3
|
|
callbacks = coalesced = interrupts = ignored = 0
|
|
hook = None
|
|
operation = 0
|
|
def e(name, a=0, b=0):
|
|
out.append('E %d %s %d %d' % (operation, name, a, b))
|
|
def raise_edge(channel, value):
|
|
nonlocal pending, coalesced
|
|
if pending & (1 << channel):
|
|
coalesced += 1
|
|
pending |= 1 << channel
|
|
values[channel] = value
|
|
sequence[channel] += 1
|
|
for operation, event in enumerate(events, 1):
|
|
k = event[0]
|
|
if k == 'R':
|
|
pending, enabled = 0, 7
|
|
values, sequence, delivered = [0]*3, [0]*3, [0]*3
|
|
callbacks = coalesced = interrupts = ignored = 0
|
|
hook = None
|
|
elif k == 'E':
|
|
raise_edge(event[1], event[2])
|
|
elif k == 'W':
|
|
enabled = event[1]
|
|
elif k == 'A':
|
|
hook = (event[1], event[2])
|
|
elif k in ('I', 'J'):
|
|
interrupts += 1
|
|
budget = event[1] if k == 'J' else 3
|
|
snapshot = pending & enabled
|
|
if not snapshot or not budget:
|
|
ignored += 1
|
|
used = 0
|
|
for channel in range(3):
|
|
bit = 1 << channel
|
|
if snapshot & bit and used < budget:
|
|
used += 1
|
|
value, seq = values[channel], sequence[channel]
|
|
e('READ', channel, value)
|
|
pending &= ~bit
|
|
e('CLEAR', bit, pending)
|
|
delivered[channel] = seq
|
|
callbacks += 1
|
|
e('CALL', channel, value)
|
|
if hook is not None:
|
|
c, v = hook
|
|
hook = None
|
|
raise_edge(c, v)
|
|
e('LATCH', c, v)
|
|
out.append('S ' + ' '.join(map(str, [operation, pending, enabled, *sequence,
|
|
*delivered, callbacks, coalesced, interrupts, ignored])))
|
|
return out
|
|
|
|
|
|
def recovery_expected(events):
|
|
out = []
|
|
active = None
|
|
waiting = []
|
|
state = sent = pulses = now = origin = 0
|
|
complete = failed = cancelled = rejected = timedout = 0
|
|
line = 1
|
|
operation = 0
|
|
def e(name, a=0, b=0):
|
|
out.append('E %d %s %d %d' % (operation, name, a, b))
|
|
def begin(request):
|
|
nonlocal active, state, sent, origin
|
|
active = request
|
|
state, sent, origin = 1, 0, now
|
|
e('START', request[0], len(request[1]))
|
|
def schedule():
|
|
if state == 0 and waiting and line:
|
|
begin(waiting.pop(0))
|
|
def done(result):
|
|
nonlocal active, state, complete, failed, cancelled, sent
|
|
if result == 0:
|
|
complete += 1
|
|
elif result == 3:
|
|
cancelled += 1
|
|
else:
|
|
failed += 1
|
|
e('DONE', active[0], result)
|
|
active = None
|
|
state, sent = 0, 0
|
|
for operation, event in enumerate(events, 1):
|
|
k = event[0]
|
|
if k == 'R':
|
|
active, waiting = None, []
|
|
state = sent = pulses = now = origin = 0
|
|
complete = failed = cancelled = rejected = timedout = 0
|
|
line = 1
|
|
elif k == 'Q':
|
|
identifier, length = event[1:3]
|
|
ids = [r[0] for r in waiting] + ([active[0]] if active else [])
|
|
if not identifier or not length or length > 16 or identifier in ids or len(waiting) == 3:
|
|
rejected += 1
|
|
e('REJECT', identifier)
|
|
else:
|
|
waiting.append((identifier, list(event[3:])))
|
|
schedule()
|
|
elif k == 'B':
|
|
line = event[1]
|
|
schedule()
|
|
elif k == 'C':
|
|
identifier = event[1]
|
|
if active is not None and active[0] == identifier and state == 1:
|
|
e('STOP')
|
|
done(3)
|
|
schedule()
|
|
elif identifier in [r[0] for r in waiting]:
|
|
waiting = [r for r in waiting if r[0] != identifier]
|
|
cancelled += 1
|
|
e('DONE', identifier, 3)
|
|
elif k == 'I' and state == 1:
|
|
flag = event[1]
|
|
if flag == 2:
|
|
e('STOP')
|
|
done(1)
|
|
schedule()
|
|
elif flag == 1:
|
|
if sent < len(active[1]):
|
|
e('WRITE', active[1][sent], sent)
|
|
sent += 1
|
|
origin = now
|
|
else:
|
|
e('STOP')
|
|
done(0)
|
|
schedule()
|
|
elif k == 'T':
|
|
now = event[1]
|
|
if state == 1 and ((now-origin) & 0xffffffff) >= 10:
|
|
timedout += 1
|
|
e('ABORT', active[0])
|
|
done(2)
|
|
state, pulses = 2, 0
|
|
elif state == 2:
|
|
pulses += 1
|
|
e('CLOCK', pulses, line)
|
|
if pulses == 9:
|
|
e('RECOVER_STOP')
|
|
state = 3
|
|
elif state == 3 and line:
|
|
state = 0
|
|
schedule()
|
|
out.append('S ' + ' '.join(map(str, [operation, active[0] if active else 0,state,sent,
|
|
len(waiting),pulses,now,line,complete,failed,cancelled,rejected,timedout])))
|
|
return out
|
|
|
|
|
|
def dma_expected(events):
|
|
out = []
|
|
active = None
|
|
token = 0
|
|
offset = chunk = 0
|
|
completed = cancelled = failed = rejected = stale = 0
|
|
operation = 0
|
|
def e(name,a=0,b=0):
|
|
out.append('E %d %s %d %d' % (operation,name,a,b))
|
|
def program():
|
|
nonlocal token,chunk
|
|
token += 1
|
|
chunk = min(7, active[1]-offset)
|
|
e('PROGRAM',token,offset*256+chunk)
|
|
for operation,event in enumerate(events,1):
|
|
k=event[0]
|
|
if k=='R':
|
|
active=None
|
|
token=offset=chunk=0
|
|
completed=cancelled=failed=rejected=stale=0
|
|
elif k=='Q':
|
|
identifier,length=event[1:]
|
|
if active is not None or not identifier or not 1<=length<=64:
|
|
rejected+=1
|
|
e('REJECT',identifier)
|
|
else:
|
|
active=(identifier,length)
|
|
offset=0
|
|
program()
|
|
elif k=='C' and active is not None and event[1]==active[0]:
|
|
e('ABORT',token)
|
|
token+=1
|
|
cancelled+=1
|
|
e('DONE',active[0],3)
|
|
active=None
|
|
offset=chunk=0
|
|
elif k=='D':
|
|
supplied,amount,error=event[1:]
|
|
if active is None or supplied!=token:
|
|
stale+=1
|
|
elif error or amount==0 or amount>chunk:
|
|
e('ABORT',token)
|
|
failed+=1
|
|
token+=1
|
|
e('DONE',active[0],1)
|
|
active=None
|
|
offset=chunk=0
|
|
else:
|
|
e('CACHE',offset,amount)
|
|
offset+=amount
|
|
if offset==active[1]:
|
|
completed+=1
|
|
e('DONE',active[0],0)
|
|
active=None
|
|
offset=chunk=0
|
|
else:
|
|
program()
|
|
out.append('S '+' '.join(map(str,[operation,active[0] if active else 0,token,offset,chunk,
|
|
completed,cancelled,failed,rejected,stale])))
|
|
return out
|
|
|
|
|
|
def main():
|
|
candidate = Path(sys.argv[1])
|
|
config = json.loads((Path(__file__).parent / 'cases.json').read_text())
|
|
models = {'i2c': (i2c_cases, i2c_expected),
|
|
'spi': (lambda c: c['events'], spi_expected),
|
|
'irq': (lambda c: c['events'], irq_expected),
|
|
'recovery': (lambda c: c['events'], recovery_expected),
|
|
'dma': (lambda c: c['events'], dma_expected)}
|
|
cases, oracle = models[config['kind']]
|
|
events = cases(config)
|
|
wanted = oracle(events)
|
|
stdin = ''.join(' '.join(map(str, event)) + '\n' for event in events)
|
|
scratch = Path(tempfile.mkdtemp(prefix='embedded2-hal-'))
|
|
print('scratch: ' + str(scratch), file=sys.stderr)
|
|
for name, content in config['readonly'].items():
|
|
(scratch / name).write_text(content)
|
|
for name in config.get('candidate_files', ['driver.c']):
|
|
(scratch / name).write_bytes((candidate / name).read_bytes())
|
|
built = subprocess.run(['make', '-s'], cwd=scratch, capture_output=True, text=True, timeout=20)
|
|
if built.returncode:
|
|
print('build failed: ' + built.stderr[-3000:])
|
|
return 1
|
|
# All file access to shared scratch ends here. The child receives only raw commands.
|
|
completed = subprocess.run([str(scratch / 'hal-demo')], cwd=scratch, input=stdin,
|
|
capture_output=True, text=True, timeout=10)
|
|
if completed.stderr:
|
|
print(completed.stderr[-3000:], file=sys.stderr, end='')
|
|
if completed.returncode:
|
|
print('candidate process failed: %d' % completed.returncode)
|
|
return 1
|
|
got = completed.stdout.splitlines()
|
|
for index in range(max(len(got), len(wanted))):
|
|
actual = got[index] if index < len(got) else '<missing>'
|
|
correct = wanted[index] if index < len(wanted) else '<extra>'
|
|
if actual != correct:
|
|
print('observation %d mismatch: got %s; expected %s' % (index + 1, actual, correct))
|
|
return 1
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|