Track the bench scripts (run_overnight.py and its stages, the link and ITS client libraries, the phone-vs-node comparison) with the first reports. Changes for the spare XIAO (no cell): - run_overnight.py picks the single Espressif board by default and refuses to test a build that is not this checkout's HEAD. - The unsigned soak stops as "transmit path wedged" after 40 resource_limit refusals in a row and tries whether a repeated STATION_CONFIGURE clears it. - "Refused requests put nothing on the radio" counts only own transmissions; frames heard from other stations are copied by RAW_CAPTURE too. - start_overnight.ps1: optional build and flash, host C tests in the background, Windows kept awake. setup_workstation.ps1 checks and installs git, Python 3.11, pyserial, asn1tools, MSYS2 gcc and make, ESP-IDF 6.0.2. - obu-firmware/docs/overnight-bench-test.tex (and PDF) describe the test, the workstation set-up and the findings of the 2026-11-25 runs.
161 lines
7.8 KiB
Python
161 lines
7.8 KiB
Python
"""Stage 3: does the station survive garbage on the link?
|
|
|
|
A seeded random generator sends frames the station must refuse or ignore: valid CRC around random link messages,
|
|
random fragment flags/offsets/totals, truncated and corrupted frames, noise, oversize length fields. After every
|
|
burst the station must still answer STATUS_REQUEST, must not have reset (uptime keeps growing, no ROM banner, no
|
|
panic text), and must not have lost heap.
|
|
|
|
Safety rules, so a fuzz run can never put anything on the air or change what the board stores:
|
|
* it starts from a freshly reset, UNCONFIGURED station, and never sends a STATION_CONFIGURE that decodes
|
|
(valid bodies are 6 or 16 octets with small field values: every body here is the wrong length or has a field out of
|
|
range), so the radio never starts and BTP_DATA_REQUEST can only answer not_configured;
|
|
* it never sends CREDENTIALS_PROVISION / CREDENTIALS_ERASE, the PKI requests (they create keys and write NVS) or
|
|
the trust bootstrap/update.
|
|
A failure prints the seed and the last frames sent (hex), so it can be replayed with --seed.
|
|
"""
|
|
import collections
|
|
import random
|
|
import struct
|
|
import time
|
|
|
|
import linklib as ll
|
|
from bench_common import FAIL, INFO, PASS, WARN
|
|
|
|
S = "fuzz"
|
|
# opcodes the generator may use: harmless without a configured radio, or unknown
|
|
SAFE_KNOWN = [ll.POTI_UPDATE, ll.BTP_DATA_REQUEST, ll.SF_IDCHANGE_SUBSCRIBE, ll.SF_IDCHANGE_UNSUBSCRIBE, ll.SF_IDCHANGE_EVENT_RESPONSE,
|
|
ll.SF_IDCHANGE_TRIGGER, ll.SF_ID_LOCK, ll.SF_ID_UNLOCK, ll.STATUS_REQUEST, ll.RAW_CAPTURE, ll.PKI_STATUS]
|
|
UNKNOWN = [o for o in range(0x00, 0x100)
|
|
if o not in (0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D)
|
|
and not 0x0E <= o <= 0x17] # everything PKI is excluded, known or not
|
|
|
|
|
|
def bad_configure_body(rnd):
|
|
"""A STATION_CONFIGURE body the station must refuse: wrong length, or a field out of range."""
|
|
kind = rnd.randrange(4)
|
|
if kind == 0:
|
|
n = rnd.choice([n for n in range(0, 40) if n not in (6, 16)])
|
|
return bytes(rnd.getrandbits(8) for _ in range(n))
|
|
if kind == 1:
|
|
return struct.pack("<BHBBB", rnd.getrandbits(8), rnd.getrandbits(16), rnd.getrandbits(8), rnd.randrange(2, 256), rnd.getrandbits(8))
|
|
if kind == 2:
|
|
return struct.pack("<BHBBB", 2, 180, 5, rnd.randrange(2), rnd.randrange(2, 256))
|
|
return struct.pack("<BBB6sBHBBBB", 2, 0, rnd.randrange(2, 256), bytes(6), 0, 180, 5, rnd.randrange(3, 256), 2, 5)
|
|
|
|
|
|
def random_message(rnd):
|
|
"""(opcode, flags, seq, body) of a random link message."""
|
|
roll = rnd.random()
|
|
if roll < 0.12:
|
|
return ll.STATION_CONFIGURE, 0, rnd.getrandbits(16), bad_configure_body(rnd)
|
|
op = rnd.choice(SAFE_KNOWN) if roll < 0.7 else rnd.choice(UNKNOWN)
|
|
flags = 0 if rnd.random() < 0.6 else rnd.choice([1, 2, 3, 4, 5, 0x80, 0xFF])
|
|
n = rnd.choice([0, 1, 2, 3, 4, 5, 8, 16, 26, 31, 32, 64, 100, 255, 256, 500, 504])
|
|
body = bytes(rnd.getrandbits(8) for _ in range(n))
|
|
if flags and rnd.random() < 0.7 and n >= 4: # fragment header with believable numbers
|
|
total = rnd.choice([n, n + 10, 600, 1536, 1537, 4000, 65535, 0])
|
|
body = struct.pack("<HH", total, rnd.choice([0, 0, n - 4, 100, 65535])) + body[4:]
|
|
return op, flags, rnd.getrandbits(16), body
|
|
|
|
|
|
def random_wire(rnd):
|
|
"""Bytes for the serial port: usually a valid frame, sometimes broken in one way or another."""
|
|
op, flags, seq, body = random_message(rnd)
|
|
msg = ll.link_message(op, seq, body, flags)
|
|
roll = rnd.random()
|
|
if roll < 0.55:
|
|
return ll.frame(ll.LINK, msg)
|
|
if roll < 0.65:
|
|
return ll.frame(ll.LINK, msg, bad_crc=True)
|
|
if roll < 0.72:
|
|
return ll.frame(ll.LINK, msg)[:rnd.randrange(1, max(2, len(msg)))] # cut off: the next frame completes it
|
|
if roll < 0.78:
|
|
return bytes(rnd.getrandbits(8) for _ in range(rnd.randrange(1, 40))) # noise
|
|
if roll < 0.82:
|
|
return b"\xAA\x55" + bytes([rnd.getrandbits(8)]) + struct.pack("<H", rnd.choice([1537, 2000, 65535, 0xAA55]))
|
|
if roll < 0.86:
|
|
return ll.frame(rnd.choice([0x00, 0x11, 0x42, 0x7E, 0xFF]), msg) # unknown frame type
|
|
if roll < 0.90:
|
|
return ll.frame(ll.LINK, msg[:rnd.randrange(0, 4)]) # a link message shorter than its header
|
|
if roll < 0.93:
|
|
return ll.frame(ll.LINK, bytes(rnd.getrandbits(8) for _ in range(rnd.choice([513, 600, 1000, 1536])))) # over one message
|
|
if roll < 0.96:
|
|
return ll.frame(ll.LINK, b"")
|
|
return ll.frame(ll.LINK, msg) * rnd.randrange(2, 6)
|
|
|
|
|
|
def run(ctx, seconds, seed=None):
|
|
L, R = ctx.link, ctx.res
|
|
ctx.phase = S
|
|
seed = seed if seed is not None else random.SystemRandom().randrange(1 << 32)
|
|
rnd = random.Random(seed)
|
|
R.add(S, "seed", INFO, "%d (replay with --fuzz-seed %d)" % (seed, seed))
|
|
L.reboot()
|
|
s0 = L.wait_status(20)
|
|
if not R.ok(S, "station answers before the fuzz run", s0 is not None):
|
|
return
|
|
R.ok(S, "fuzz starts unconfigured (nothing can reach the radio)", s0["configured"] == 0)
|
|
if s0["configured"]:
|
|
return
|
|
ring = collections.deque(maxlen=25)
|
|
sent = bursts = 0
|
|
boots0 = L.boots
|
|
panics0 = len(L.panics)
|
|
end = min(time.time() + seconds, ctx.deadline or 1e18)
|
|
start_heap = last_heap = worst_heap = s0["heap_free"]
|
|
failed = False
|
|
while time.time() < end and not failed:
|
|
for _ in range(rnd.randrange(20, 120)):
|
|
wire = random_wire(rnd)
|
|
ring.append(wire)
|
|
try:
|
|
L.write(wire)
|
|
except Exception as e:
|
|
R.add(S, "serial write", FAIL, repr(e))
|
|
failed = True
|
|
break
|
|
sent += 1
|
|
if rnd.random() < 0.3:
|
|
time.sleep(0.002)
|
|
bursts += 1
|
|
# A cut-off frame leaves the station's decoder waiting for up to 1536 payload octets; zeros finish it (bad CRC,
|
|
# counted, then it resyncs on the next 0xAA55). Without this the liveness request below would be swallowed.
|
|
L.write(b"\x00" * 1542)
|
|
time.sleep(0.4)
|
|
s = None
|
|
for _ in range(3):
|
|
s = L.status(2.0)
|
|
if s:
|
|
break
|
|
time.sleep(0.3)
|
|
L.log_status(s, S)
|
|
if s is None:
|
|
R.add(S, "station stopped answering after a fuzz burst", FAIL, "burst %d, seed %d; last frames:\n%s" % (bursts, seed, _dump(ring)))
|
|
failed = True
|
|
else:
|
|
if s["uptime_ms"] < s0["uptime_ms"] or L.boots > boots0:
|
|
R.add(S, "station reset during fuzz", FAIL, "burst %d, seed %d, uptime %d; last frames:\n%s" % (bursts, seed, s["uptime_ms"], _dump(ring)))
|
|
failed = True
|
|
if s["configured"]:
|
|
R.add(S, "fuzz managed to configure the station", FAIL, "burst %d, seed %d; last frames:\n%s" % (bursts, seed, _dump(ring)))
|
|
failed = True
|
|
worst_heap = min(worst_heap, s["heap_free"])
|
|
last_heap = s["heap_free"]
|
|
s0 = dict(s0, uptime_ms=s["uptime_ms"])
|
|
if len(L.panics) > panics0:
|
|
R.add(S, "panic or error text on the console", FAIL, "%s; seed %d; last frames:\n%s" % (L.panics[panics0:][:3], seed, _dump(ring)))
|
|
failed = True
|
|
L.log_status(L.status(), S)
|
|
if not failed:
|
|
R.add(S, "fuzz run", PASS, "%d frames in %d bursts, no reset, no panic, always answered" % (sent, bursts))
|
|
R.ok(S, "heap after fuzz is within 8 KB of the start", abs(last_heap - start_heap) <= 8192,
|
|
"start %d, end %d, lowest %d" % (start_heap, last_heap, worst_heap), WARN)
|
|
R.metric(S, "frames_sent", sent)
|
|
R.metric(S, "device_counts", {k: v for k, v in L.counts.items() if k.startswith(("frames_", "frag_", "confirm"))})
|
|
# leave the board in a known state
|
|
L.reboot()
|
|
|
|
|
|
def _dump(ring):
|
|
return "\n".join(" " + w.hex() for w in list(ring)[-12:])
|