114 lines
5.0 KiB
Python

"""Private byte-protocol oracle; the candidate executable only emits observations."""
import json
from pathlib import Path
import subprocess
import sys
import tempfile
def hash32(data):
value = 2166136261
for byte in data:
value = ((value ^ byte) * 16777619) & 0xffffffff
return value
def expected(number, line):
args = line.split()
try:
assert len(args) == 3 and args[0].isdigit() and args[1].isdigit()
pad, chunk = map(int, args[:2])
data = bytes.fromhex(args[2])
assert 0 <= pad <= 7 and 1 <= chunk <= 1024 and 1 <= len(data) <= 1024
except (AssertionError, ValueError):
return ['I ' + str(number)]
error = declared = checksum = fingerprint = 0
events = []
if len(data) < 10:
error = 4
elif data[:3] != b'\xa5\x5a\x01' or data[3] > 32:
error = 3
else:
declared = data[3]
length = int.from_bytes(data[4:6], 'little')
if 10 + length != len(data):
error = 4
else:
checksum = int.from_bytes(data[-4:], 'little')
if checksum != hash32(data[:-4]):
error = 5
else:
payload = data[6:-4]
position = 0
for _ in range(declared):
if position + 2 > len(payload):
error = 6
break
tag, size = payload[position:position+2]
position += 2
body = payload[position:position+size]
position += size
if len(body) != size or tag == 1 and size != 10 or tag == 2 and size > 32 or tag == 3 and size != 16:
error = 6
break
source = tick = value = auxiliary = 0
if tag == 1:
source = int.from_bytes(body[:2], 'little')
value = int.from_bytes(body[2:6], 'little')
auxiliary = int.from_bytes(body[6:10], 'little')
elif tag == 3:
source = int.from_bytes(body[:4], 'little')
tick = int.from_bytes(body[4:12], 'little')
value = int.from_bytes(body[12:16], 'little')
else:
value = hash32(body)
auxiliary = size
event = [tag, size, source, tick, value, auxiliary]
events.append(event)
for field in [tag, size, source, tick & 0xffffffff, tick >> 32, value, auxiliary]:
fingerprint = (fingerprint * 33 + field) & 0xffffffff
if not error and position != len(payload):
error = 6
lines = ['O ' + ' '.join(map(str, [number, error, len(data), (len(data)+chunk-1)//chunk, declared, len(events), checksum, fingerprint]))]
lines.extend('E ' + ' '.join(map(str, [number, index] + event)) for index, event in enumerate(events))
return lines
def main():
candidate = Path(sys.argv[1])
private = json.loads(Path(__file__).with_name('cases.json').read_text())
inputs = '\n'.join(case['line'] for case in private['cases']) + '\n'
wanted = [line for number, case in enumerate(private['cases'], 1) for line in expected(number, case['line'])]
# All root filesystem reads/writes in shared scratch finish before compilation or execution.
root = Path(tempfile.mkdtemp(prefix='wire-reader-'))
for name, contents in private['context'].items():
root.joinpath(name).write_text(contents)
for name in ('cursor.c', 'decoder.c'):
root.joinpath(name).write_bytes(candidate.joinpath(name).read_bytes())
command = ['gcc', '-std=c11', '-O1', '-g', '-Wall', '-Wextra', '-Werror', '-fsanitize=alignment', '-fsanitize-recover=alignment', 'main.c', 'input.c', 'observation.c', 'cursor.c', 'decoder.c', '-o', 'wire-reader']
built = subprocess.run(command, cwd=root, capture_output=True, text=True, timeout=30)
if built.returncode:
print('BUILD_FAIL ' + built.stderr[-1000:])
return 1
result = subprocess.run([str(root / 'wire-reader')], input=inputs, cwd=root, capture_output=True, text=True, timeout=15)
# Below this point only the in-memory child result is examined.
if result.returncode:
print('RUN_FAIL ' + str(result.returncode))
return 1
diagnostic = next((line for line in result.stderr.splitlines() if 'runtime error:' in line), None)
if diagnostic:
print('RUNTIME_DIAGNOSTIC ' + diagnostic[:240])
return 1
sys.stderr.write(result.stderr[-2000:])
actual = result.stdout.splitlines()
if actual != wanted:
first = next((i for i, pair in enumerate(zip(actual, wanted)) if pair[0] != pair[1]), min(len(actual), len(wanted)))
print('OBSERVATION_MISMATCH line={} actual={} expected={}'.format(first, actual[first:first+1], wanted[first:first+1]))
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())