422 lines
16 KiB
Python
422 lines
16 KiB
Python
"""Run frozen memory-model C projects; compare raw observations to Python state models."""
|
|
import json
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
def weighted(data):
|
|
return sum((i + 1) * value for i, value in enumerate(data)) % 65521
|
|
|
|
|
|
def arena_model(events):
|
|
slots = [[0] * 7 for _ in range(8)]
|
|
memory = [0] * 256
|
|
counts = [0] * 4
|
|
output = []
|
|
for number, event in enumerate(events, 1):
|
|
kind, *args = event
|
|
rc = value = detail = 0
|
|
found = next((i for i, slot in enumerate(slots) if slot[1] and args and slot[0] == args[0]), None)
|
|
def retire(index):
|
|
slot = slots[index]
|
|
memory[slot[2]:slot[2]+slot[3]] = [0] * slot[3]
|
|
slots[index] = [0] * 7
|
|
counts[2] += 1
|
|
if kind == 'Z':
|
|
slots = [[0] * 7 for _ in range(8)]
|
|
memory = [0] * 256
|
|
counts = [0] * 4
|
|
elif kind == 'A':
|
|
ident, length, alignment = args
|
|
empty = next((i for i, slot in enumerate(slots) if not slot[1]), None)
|
|
address = None
|
|
if ident and 1 <= length <= 64 and alignment in (4, 8, 16, 32, 64) and found is None:
|
|
for offset in range(0, 257-length, alignment):
|
|
if all(not s[1] or offset+length <= s[2] or s[2]+s[3] <= offset for s in slots):
|
|
address = offset
|
|
break
|
|
if address is None or empty is None:
|
|
rc = -1
|
|
else:
|
|
slots[empty] = [ident, 1, address, length, alignment, 1, 0]
|
|
memory[address:address+length] = [0] * length
|
|
counts[0] += 1
|
|
value, detail = address, empty
|
|
elif found is None:
|
|
rc = -1
|
|
elif kind == 'S':
|
|
if not slots[found][5] or slots[found][6] == 8:
|
|
rc = -1
|
|
else:
|
|
slots[found][6] += 1
|
|
value = slots[found][6]
|
|
elif kind == 'F':
|
|
if not slots[found][5]:
|
|
rc = -1
|
|
else:
|
|
slots[found][5] = 0
|
|
if not slots[found][6]:
|
|
retire(found)
|
|
elif kind == 'C':
|
|
if not slots[found][6]:
|
|
rc = -1
|
|
else:
|
|
slots[found][6] -= 1
|
|
counts[3] += 1
|
|
if not slots[found][5] and not slots[found][6]:
|
|
retire(found)
|
|
else:
|
|
slot = slots[found]
|
|
position = args[1]
|
|
if position >= slot[3] or kind == 'W' and (not slot[5] or args[2] > 255):
|
|
rc = -1
|
|
else:
|
|
detail = slot[2] + position
|
|
if kind == 'W':
|
|
memory[detail] = args[2]
|
|
value = memory[detail]
|
|
if rc:
|
|
counts[1] += 1
|
|
state = counts + [x for slot in slots for x in slot] + memory
|
|
output.append('O ' + ' '.join(map(str, [number, rc, value, detail] + state)))
|
|
return output
|
|
|
|
|
|
def bidir_model(events):
|
|
cpu, device, dirty = [0]*64, [0]*64, [0]*8
|
|
pending = None
|
|
counts = [0]*5 # transfers completions rejected cleans invalidates
|
|
output = []
|
|
for number, event in enumerate(events, 1):
|
|
kind, *args = event
|
|
rc = value = detail = 0
|
|
if kind == 'Z':
|
|
cpu, device, dirty = [0]*64, [0]*64, [0]*8
|
|
pending = None
|
|
counts = [0]*5
|
|
elif kind == 'F':
|
|
if pending is None:
|
|
rc = -1
|
|
else:
|
|
offset, length = pending
|
|
lines = range(offset//8, (offset+length-1)//8+1)
|
|
for line in lines:
|
|
cpu[line*8:line*8+8] = device[line*8:line*8+8]
|
|
dirty[line] = 0
|
|
counts[4] += 1
|
|
value, detail = weighted(cpu[offset:offset+length]), length
|
|
pending = None
|
|
counts[1] += 1
|
|
else:
|
|
offset, length = args[:2]
|
|
if length == 0 or offset+length > 64 or len(args) == 3 and args[2] > 255:
|
|
rc = -1
|
|
elif kind == 'W':
|
|
lines = set(range(offset//8, (offset+length-1)//8+1))
|
|
held = set() if pending is None else set(range(pending[0]//8, (sum(pending)-1)//8+1))
|
|
if lines & held:
|
|
rc = -1
|
|
else:
|
|
cpu[offset:offset+length] = [(args[2]+i)&255 for i in range(length)]
|
|
for line in lines:
|
|
dirty[line] = 1
|
|
value, detail = weighted(cpu[offset:offset+length]), length
|
|
elif kind == 'T':
|
|
if pending is not None:
|
|
rc = -1
|
|
else:
|
|
for line in range(offset//8, (offset+length-1)//8+1):
|
|
if dirty[line]:
|
|
device[line*8:line*8+8] = cpu[line*8:line*8+8]
|
|
dirty[line] = 0
|
|
counts[3] += 1
|
|
value, detail = weighted(device[offset:offset+length]), length
|
|
for i in range(offset, offset+length):
|
|
device[i] ^= args[2]
|
|
pending = offset, length
|
|
counts[0] += 1
|
|
else:
|
|
value, detail = weighted(cpu[offset:offset+length]), length
|
|
if rc:
|
|
counts[2] += 1
|
|
state = ([1, *pending] if pending else [0, 0, 0]) + counts + dirty + cpu + device
|
|
output.append('O ' + ' '.join(map(str, [number, rc, value, detail] + state)))
|
|
return output
|
|
|
|
|
|
def mapping_model(events):
|
|
slots = [[0]*10 for _ in range(8)]
|
|
counts = [0]*8 # descriptors pages opened closed mapped unmapped rejected next_handle
|
|
output = []
|
|
def close(slot):
|
|
if slot[8]:
|
|
counts[0] -= 1
|
|
counts[3] += 1
|
|
slot[8] = 0
|
|
def unmap(slot):
|
|
if slot[9]:
|
|
counts[1] -= slot[4]//16
|
|
counts[5] += 1
|
|
slot[9] = 0
|
|
def retire(index):
|
|
unmap(slots[index])
|
|
close(slots[index])
|
|
slots[index] = [0]*10
|
|
for number, event in enumerate(events, 1):
|
|
kind, *args = event
|
|
rc = value = detail = 0
|
|
found = next((i for i, slot in enumerate(slots) if slot[1] and args and slot[0] == args[0]), None)
|
|
if kind == 'Z':
|
|
slots = [[0]*10 for _ in range(8)]
|
|
counts = [0]*8
|
|
elif kind == 'I':
|
|
ident, offset, length, failure = args
|
|
empty = next((i for i, slot in enumerate(slots) if not slot[1]), None)
|
|
pages = (offset % 16+length+15)//16
|
|
if not ident or failure > 3 or not 1 <= length <= 64 or offset+length > 1024 or found is not None or empty is None or counts[0] == 8 or counts[1]+pages > 16 or failure == 1:
|
|
rc = -1
|
|
else:
|
|
counts[0] += 1
|
|
counts[2] += 1
|
|
counts[7] += 1
|
|
slot = [ident, 1, offset, length, pages*16, offset%16, 0, 0, counts[7], 0]
|
|
slots[empty] = slot
|
|
if failure == 2:
|
|
close(slot)
|
|
slots[empty] = [0]*10
|
|
rc = -1
|
|
else:
|
|
counts[1] += pages
|
|
counts[4] += 1
|
|
counts[7] += 1
|
|
slot[9] = counts[7]
|
|
if failure == 3:
|
|
retire(empty)
|
|
rc = -1
|
|
else:
|
|
slot[6] = 1
|
|
value, detail = offset%16, pages*16
|
|
elif found is None:
|
|
rc = -1
|
|
else:
|
|
slot = slots[found]
|
|
if kind == 'H':
|
|
if not slot[6] or slot[7] == 8:
|
|
rc = -1
|
|
else:
|
|
slot[7] += 1
|
|
value = slot[7]
|
|
elif kind == 'C':
|
|
if not slot[6]:
|
|
rc = -1
|
|
else:
|
|
slot[6] = 0
|
|
close(slot)
|
|
if not slot[7]:
|
|
retire(found)
|
|
elif kind == 'L':
|
|
if not slot[7]:
|
|
rc = -1
|
|
else:
|
|
slot[7] -= 1
|
|
if not slot[7] and not slot[6]:
|
|
retire(found)
|
|
else:
|
|
if args[1] >= slot[3] or not slot[9]:
|
|
rc = -1
|
|
else:
|
|
value = (slot[2]+args[1])&255
|
|
detail = slot[5]+args[1]
|
|
if rc:
|
|
counts[6] += 1
|
|
output.append('O ' + ' '.join(map(str, [number, rc, value, detail] + counts + [x for slot in slots for x in slot])))
|
|
return output
|
|
|
|
|
|
def ring_model(events):
|
|
objects = [[0]*5 for _ in range(8)]
|
|
ring = [0]*4
|
|
handles = [0]*8
|
|
head = 0
|
|
count = 0
|
|
counters = [0]*4 # allocated retired rejected delivered
|
|
output = []
|
|
def drop(slot):
|
|
objects[slot][4] -= 1
|
|
if not objects[slot][4]:
|
|
objects[slot] = [0]*5
|
|
counters[1] += 1
|
|
for number, event in enumerate(events, 1):
|
|
kind, *args = event
|
|
rc = value = detail = 0
|
|
found = next((i for i, obj in enumerate(objects) if obj[2] and args and obj[0] == args[0]), None)
|
|
if kind == 'Z':
|
|
objects = [[0]*5 for _ in range(8)]
|
|
ring, handles = [0]*4, [0]*8
|
|
head = count = 0
|
|
counters = [0]*4
|
|
elif kind == 'A':
|
|
ident, contents = args
|
|
empty = next((i for i, obj in enumerate(objects) if not obj[2]), None)
|
|
if not ident or contents > 65535 or empty is None or found is not None:
|
|
rc = -1
|
|
else:
|
|
objects[empty] = [ident, contents, 1, 1, 1]
|
|
counters[0] += 1
|
|
value = empty
|
|
elif kind in ('Q', 'F'):
|
|
if found is None or not objects[found][3] or kind == 'Q' and count == 4:
|
|
rc = -1
|
|
elif kind == 'Q':
|
|
objects[found][4] += 1
|
|
ring[(head+count)%4] = found+1
|
|
count += 1
|
|
else:
|
|
objects[found][3] = 0
|
|
drop(found)
|
|
elif kind == 'K':
|
|
logical = [(head+i)%4 for i in range(count)]
|
|
position = next((i for i, physical in enumerate(logical) if objects[ring[physical]-1][0] == args[0]), None)
|
|
if position is None:
|
|
rc = -1
|
|
else:
|
|
slot = ring[logical[position]]-1
|
|
for i in range(position, count-1):
|
|
ring[(head+i)%4] = ring[(head+i+1)%4]
|
|
ring[(head+count-1)%4] = 0
|
|
count -= 1
|
|
drop(slot)
|
|
else:
|
|
consumer = args[0]
|
|
if consumer >= 8:
|
|
rc = -1
|
|
elif kind == 'P':
|
|
if handles[consumer] or not count:
|
|
rc = -1
|
|
else:
|
|
handle = ring[head]
|
|
ring[head] = 0
|
|
head = (head+1)%4
|
|
count -= 1
|
|
handles[consumer] = handle
|
|
counters[3] += 1
|
|
value, detail = objects[handle-1][1], objects[handle-1][0]
|
|
elif not handles[consumer]:
|
|
rc = -1
|
|
elif kind == 'C':
|
|
slot = handles[consumer]-1
|
|
handles[consumer] = 0
|
|
drop(slot)
|
|
else:
|
|
obj = objects[handles[consumer]-1]
|
|
value, detail = obj[1], obj[0]
|
|
if rc:
|
|
counters[2] += 1
|
|
state = [head, count] + counters + ring + handles + [x for obj in objects for x in obj]
|
|
output.append('O ' + ' '.join(map(str, [number, rc, value, detail] + state)))
|
|
return output
|
|
|
|
|
|
def cache_model(events):
|
|
channels = [[0]*17 for _ in range(4)]
|
|
counters = [0]*6 # publications acquisitions acknowledgements rejected flushes invalidations
|
|
output = []
|
|
for number, event in enumerate(events, 1):
|
|
kind, *args = event
|
|
rc = value = detail = 0
|
|
if kind == 'Z':
|
|
channels = [[0]*17 for _ in range(4)]
|
|
counters = [0]*6
|
|
elif args[0] >= 4:
|
|
rc = -1
|
|
else:
|
|
ch = channels[args[0]]
|
|
dirty, valid, seq, seen, ack = ch[12:]
|
|
if kind == 'W':
|
|
if args[1] >= 4 or args[2] > 65535 or seq != ack:
|
|
rc = -1
|
|
else:
|
|
ch[args[1]] = args[2]
|
|
ch[12] = 1
|
|
value = weighted(ch[:4])
|
|
elif kind == 'P':
|
|
if seq != ack or seq == 65535:
|
|
rc = -1
|
|
else:
|
|
if dirty:
|
|
ch[4:8] = ch[:4]
|
|
counters[4] += 1
|
|
ch[12] = 0
|
|
ch[14] += 1
|
|
counters[0] += 1
|
|
value, detail = ch[14], weighted(ch[4:8])
|
|
elif kind == 'U':
|
|
if seq == seen:
|
|
rc = -1
|
|
else:
|
|
ch[8:12] = ch[4:8]
|
|
ch[13], ch[15] = 1, seq
|
|
counters[1] += 1
|
|
counters[5] += 1
|
|
value, detail = weighted(ch[8:12]), seq
|
|
elif kind == 'A':
|
|
if seq == ack or seen != seq:
|
|
rc = -1
|
|
else:
|
|
ch[16] = seen
|
|
counters[2] += 1
|
|
value = seen
|
|
else:
|
|
if not valid:
|
|
rc = -1
|
|
else:
|
|
value, detail = weighted(ch[8:12]), seen
|
|
if rc:
|
|
counters[3] += 1
|
|
output.append('O ' + ' '.join(map(str, [number, rc, value, detail] + counters + [x for ch in channels for x in ch])))
|
|
return output
|
|
|
|
|
|
def main():
|
|
config = json.loads((Path(__file__).parent / 'cases.json').read_text())
|
|
events = config['events']
|
|
models = {'arena': arena_model, 'bidir': bidir_model, 'mapping': mapping_model,
|
|
'ring': ring_model, 'cache': cache_model}
|
|
expected = models[config['kind']](events)
|
|
stdin = ''.join(' '.join(map(str, event)) + '\n' for event in events)
|
|
scratch = Path(tempfile.mkdtemp(prefix='embedded3-memory-'))
|
|
print('scratch: ' + str(scratch), file=sys.stderr)
|
|
candidate = Path(sys.argv[1])
|
|
for name, content in config['readonly'].items():
|
|
(scratch / name).write_text(content)
|
|
for name in config['candidate_files']:
|
|
(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[-2500:])
|
|
return 1
|
|
# All inputs and build files are ready; never access the shared scratch after execution.
|
|
completed = subprocess.run([str(scratch / 'memory-demo')], cwd=scratch, input=stdin,
|
|
capture_output=True, text=True, timeout=10)
|
|
if completed.stderr:
|
|
print(completed.stderr[-2500:], file=sys.stderr, end='')
|
|
if completed.returncode:
|
|
print('candidate process failed: %d' % completed.returncode)
|
|
return 1
|
|
actual = completed.stdout.splitlines()
|
|
for index in range(max(len(actual), len(expected))):
|
|
got = actual[index] if index < len(actual) else '<missing>'
|
|
want = expected[index] if index < len(expected) else '<extra>'
|
|
if got != want:
|
|
print('observation %d mismatch: got %s; expected %s' % (index+1, got[:850], want[:850]))
|
|
return 1
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|