168 lines
7.2 KiB
Python

"""Shared build family checker: expected values remain in this process."""
import json
import pathlib
import random
import shutil
import struct
import subprocess
import sys
import tempfile
import zlib
def packet(record):
seq, channel, value = record
raw = struct.pack("<IIi", seq, channel, value)
return raw + struct.pack("<I", zlib.crc32(raw) & 0xffffffff)
def commands(seed):
rng = random.Random(seed)
result = ["RESET 1234567", "PUT 1 0 11", "PUT 2 1 -12", "DRAIN 1", "STATE",
"PUT 1 0 99", "PUT 3 0 -2147483648", "DRAIN 0", "DRAIN 32",
"PUT 4 0 2147483647", "DRAIN 32", "PUT 5 2 -5", "PUT 5 2 19",
"STATE", "DRAIN 32", "PUT 6 7 -9", "DROP 7", "PUT 6 7 8", "STATE",
"DROP 8", "PUT 7 8 0", "DRAIN 33", "WIRE bad", "INVALID", "RESET 9"]
result += ["PUT %d %d %d" % (i, i % 8, i * 13 - 200) for i in range(40)]
result += ["DRAIN 7", "PUT 32 1 99", "DRAIN 31", "PUT 0 1 19", "STATE", "RESET 22"]
for i in range(200):
action = rng.randrange(10)
if action < 6:
record = (rng.randrange(100), rng.randrange(8), rng.randrange(-90000, 90000))
result.append("WIRE " + packet(record).hex() if action == 0 else "PUT %d %d %d" % record)
elif action < 8:
result.append("DRAIN %d" % rng.randrange(33))
elif action == 8:
result.append("DROP %d" % rng.randrange(8))
else:
result.append("STATE")
result += ["DRAIN 32", "STATE", "RESET 31", "PUT 0 0 100"]
for sequence in range(1, 66):
result += ["PUT %d 1 %d" % (sequence, sequence), "DROP 1"]
# The still-pending first record outlives its 64-entry deduplication history.
result += ["PUT 0 2 50", "DRAIN 32", "STATE", "RESET 4294967295", "PUT 4294967295 0 42", "DRAIN 1"]
return result
def oracle(mode, lines, profile=1, feature=0, version=1):
epoch = accepted = dropped = 0
queue, history, totals = [], [], [0] * 8
out = []
for line in lines:
fields = line.split()
op, args = fields[0], fields[1:]
status, values = 0, []
if op == "WIRE":
try:
raw = bytes.fromhex(args[0])
if len(raw) != 16 or zlib.crc32(raw[:12]) & 0xffffffff != int.from_bytes(raw[12:], "little"):
raise ValueError()
seq, channel, value = struct.unpack("<IIi", raw[:12])
if channel >= 8:
raise ValueError()
op, args = "PUT", [seq, channel, value]
except ValueError:
op = "INVALID"
if op == "RESET":
epoch = int(args[0]); accepted = dropped = 0
queue, history, totals = [], [], [0] * 8
elif op == "PUT":
seq, channel, value = map(int, args)
if channel >= 8:
status = -1
elif seq in history:
status = -2
elif mode == "flags" and feature and value < 0:
status = 1; dropped += 1
elif len(queue) >= 32:
status = -3
else:
queue.append((seq, channel, value)); history.append(seq)
history = history[-64:]; accepted += 1
elif op == "DRAIN":
limit = int(args[0])
if limit > 32:
status = -1
else:
batch, queue = queue[:limit], queue[limit:]
for seq, channel, value in batch:
if mode == "profile":
value = (value - channel * profile) * (profile + 1)
value = min(1000000, max(-1000000, value))
elif mode == "archive":
value = value * 3 + channel * 7
elif mode == "exports":
# Public wire digest: fold all bytes, including the sequence.
value = zlib.crc32(packet((seq, channel, value))[:12]) & 0xffffffff
elif mode == "static":
value = value - channel * 5
else:
value = value * (feature + 1) + version
totals[channel] += value; values.append(totals[channel])
elif op == "DROP":
channel = int(args[0])
if channel >= 8:
status = -1
else:
retained = [r for r in queue if r[1] != channel]
dropped += len(queue) - len(retained); queue = retained
elif op != "STATE":
status = -1
digest = zlib.crc32(b"".join(map(packet, queue))) & 0xffffffff
backend = profile if mode == "profile" else 1
fields = [status, epoch, accepted, dropped, len(queue), digest, backend, feature, version, len(values), *values]
out.append("R " + " ".join(map(str, fields)))
return out
def main():
data = json.loads(pathlib.Path(__file__).with_name("cases.json").read_text())
candidate = pathlib.Path(sys.argv[1])
scratch = pathlib.Path(tempfile.mkdtemp(prefix="build-family-"))
steps, expected = [], []
for index, config in enumerate(data["configs"]):
directory = "work" if data["mode"] in ("profile", "archive", "flags") else "work-%d" % index
work = scratch / directory
if not work.exists():
work.mkdir(mode=0o777)
work.chmod(0o777)
for name, source in data["support"].items():
(work / name).write_text(source)
for name in data["candidate_files"]:
shutil.copyfile(candidate / name, work / name)
lines = commands(data["seed"] + index)
input_name = "input-%d.txt" % index
(scratch / input_name).write_text("\n".join(lines) + "\n")
step = dict(config, directory=directory, input=input_name)
steps.append(step)
expected.append("BEGIN %d" % index)
if config.get("observe") == "static":
expected.append("DYNAMIC %d" % (not config["static"]))
if config.get("observe") == "exports":
expected += ["EXPORT engine_execute@@DEVICE_1.0", "EXPORT engine_reset@@DEVICE_1.0"]
expected += oracle(data["mode"], lines, config.get("profile", 1),
config.get("feature", 0), config.get("version", 1))
expected.append("END %d" % index)
(scratch / "steps.json").write_text(json.dumps(steps))
(scratch / "driver.py").write_text(data["driver"])
# All inputs and build contexts now exist. Never inspect shared scratch after execution.
result = subprocess.run([sys.executable, "-B", "driver.py"], cwd=scratch,
capture_output=True, text=True, timeout=80)
sys.stderr.write(result.stderr)
if result.returncode:
print("build-or-runtime-failed exit=%d" % result.returncode)
raise SystemExit(1)
actual = result.stdout.splitlines()
if actual != expected:
for index, (got, want) in enumerate(zip(actual, expected)):
if got != want:
print("observation %d mismatch: got=%s expected=%s" % (index, got[:180], want[:180]))
raise SystemExit(1)
print("observation-count mismatch got=%d expected=%d" % (len(actual), len(expected)))
raise SystemExit(1)
print("PASS")
if __name__ == "__main__":
main()