From 5034a2f36d9f6a071abb23c9490cbe314748d2f9 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:28:20 -0500 Subject: [PATCH 1/4] net/uci: correct the interface-index comment in net_dhcp_acquire The comment said the U64E has a single interface (index 0). On fw 3a1ff9ff, Ethernet (rmii_interface.cc, init prio 51) always registers as index 0, cable or not, and WiFi (wifi.cc, prio 52) as index 1. The WiFi lease was observed on iface 1 at 10.43.23.83. Comment-only change; the probe loop already covers indices 0..3. PRG sha256, identical on origin/master (249f65d) and this branch: uci b61ba7924b5c5e91077ceeaac8b08f4246bfb2c3515df2c9a80104d658e764bc uci-onchip 45bea88a6c27138009de543394241054e3a31873aa5766c466158b316b18da01 uci-comb 2b3e6c5d7070e43d7a591b4280f519918bd4b02921cf776819ec22e6f6ea3900 uci-m3 b124ea9f25d26bfeefc0ff5bc1585876a9256a2ca7d476ccf2f071d09870f35f Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci/net.s | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/net/uci/net.s b/src/net/uci/net.s index 054cf27..8234324 100644 --- a/src/net/uci/net.s +++ b/src/net/uci/net.s @@ -501,9 +501,10 @@ net_poll: ; -> push_wait -> check_err -> read 12 bytes -> drain resp ; -> drain status -> ack ; -; Interface fallback: the U64E has a single interface (index 0), but the -; C64 Ultimate has Ethernet AND WiFi — a box on WiFi returns 0.0.0.0 for -; index 0. We probe indices 0..NET_DHCP_MAX_IFACE-1 and take the first +; Interface fallback: on both the U64E (fw 3a1ff9ff) and the C64 Ultimate, +; Ethernet registers as index 0 whether or not a cable is in, and WiFi as +; index 1 (rmii_interface.cc / wifi.cc init order), so a box on WiFi returns +; 0.0.0.0 for index 0. We probe indices 0..NET_DHCP_MAX_IFACE-1 and take the first ; one with a non-zero lease. A CMD_FAILED on an out-of-range index is ; cleaned up (drain + ack) and treated like "no lease on this interface". ; @@ -532,8 +533,8 @@ net_dhcp_acquire: lda #UCI_CMD_GET_IPADDR jsr uci_put_byte - ; Interface index — 0 first (only iface on U64E; Ethernet on the - ; C64 Ultimate), then 1.. (C64U WiFi) until one has a lease. + ; Interface index — 0 first (Ethernet), then 1.. (WiFi is 1) + ; until one has a lease. lda @iface_idx jsr uci_put_byte @@ -544,8 +545,8 @@ net_dhcp_acquire: jsr uci_check_err bcc @no_err - ; Command failed for this interface (e.g. index out of range on - ; single-interface firmware). Clean up response/status state so + ; Command failed for this interface (e.g. an index past the + ; last registered interface). Clean up response/status state so ; the next probe starts from idle, then advance. lda #UCI_ERR_CMD_FAILED sta net_last_error From 815f6487538ee307631fe60b76f155f92429a96d Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 4 Oct 2026 21:16:57 -0500 Subject: [PATCH 2/4] net/uci: take a DHCP lease only from a full GET_IPADDR reply An interface index at or past getNumberOfInterfaces() is answered by network_target.cc with an EMPTY, valid reply and the status line "82,PARAMETER(S) OUT OF RANGE"; the $DF1C ERROR bit stays clear (its one setter is a PUSH while not idle), so uci_check_err returns C=0. The old probe then read 12 bytes that were never sent: * uci_read_resp_bytes spun its 65,536 fenced iterations for the first missing byte (~7.5 s per out-of-range probe at 48 MHz, ~6 min at 1 MHz), reached whenever no in-range interface has a lease; * whatever uci_ipaddr_resp already held was copied to net_local_ip and, if non-zero, returned with C=0. The second needs an empty or short reply with no full reply before it in the same call, which firmware does not produce on a box with at least one interface (in-range indices come first and always return 12 bytes), so it is a contract fix, not a reproduced field failure. Now: an empty reply (DATA_AV low once VALID) skips the read; only uci_resp_count == 12 counts as an answer; net_local_ip is zeroed on entry so a failed call never reads as the last call's address. Same codes as before ($83 no lease, $82 rejected push, $89 timeout). NET_CODE -32 B on uci / uci-onchip / uci-comb; uci-m3 has its own probe and is unchanged. tools/test_uci_dhcp_probe.py runs the shipped bytes against a modelled firmware that answers GET_IPADDR from the bytes written to $DF1D. Co-Authored-By: Claude Opus 5.5 (1M context) --- pytest.ini | 6 +- src/net/uci/net.s | 46 ++++- tools/test_uci_dhcp_probe.py | 323 +++++++++++++++++++++++++++++++++++ 3 files changed, 365 insertions(+), 10 deletions(-) create mode 100644 tools/test_uci_dhcp_probe.py diff --git a/pytest.ini b/pytest.ini index d641f08..bb70f27 100644 --- a/pytest.ini +++ b/pytest.ini @@ -30,9 +30,10 @@ ; A green run here does NOT mean the C64 test suites passed. conftest.py ; prints that on every invocation; do not remove it. ; -; FIVE modules here are not build-independent: test_uci_data_acc.py, +; SIX modules here are not build-independent: test_uci_data_acc.py, ; test_uci_timeout_recovery.py, test_uci_reply_valid_wait.py, -; test_uci_abort_recovery.py and test_uci_resp_read_wait.py execute +; test_uci_abort_recovery.py, test_uci_resp_read_wait.py and +; test_uci_dhcp_probe.py execute ; the shipped 6502 bytes out of ; build/c64-https.prg, so they need a BACKEND=uci build in build/. That is ; deliberate — they are still pure-logic (no VICE, no hardware, well under @@ -74,6 +75,7 @@ testpaths = tools/test_trust_store_format.py tools/test_uci_abort_recovery.py tools/test_uci_data_acc.py + tools/test_uci_dhcp_probe.py tools/test_uci_reply_valid_wait.py tools/test_uci_resp_read_wait.py tools/test_uci_timeout_recovery.py diff --git a/src/net/uci/net.s b/src/net/uci/net.s index 8234324..5b3dd0f 100644 --- a/src/net/uci/net.s +++ b/src/net/uci/net.s @@ -67,6 +67,7 @@ .import uci_drain_resp .import uci_drain_status .import uci_ack +.import uci_settle .import uci_resp_dst .import uci_resp_max .import uci_resp_count @@ -505,13 +506,22 @@ net_poll: ; Ethernet registers as index 0 whether or not a cable is in, and WiFi as ; index 1 (rmii_interface.cc / wifi.cc init order), so a box on WiFi returns ; 0.0.0.0 for index 0. We probe indices 0..NET_DHCP_MAX_IFACE-1 and take the first -; one with a non-zero lease. A CMD_FAILED on an out-of-range index is -; cleaned up (drain + ack) and treated like "no lease on this interface". +; one with a non-zero lease. +; +; An out-of-range index is NOT a command failure: network_target.cc answers +; it with an EMPTY reply and "82,PARAMETER(S) OUT OF RANGE" on the status +; channel, and the $DF1C ERROR bit stays clear (its only setter is a PUSH +; while not idle). So the reply is checked by length: only a full 12-byte +; record is a lease. An empty reply skips uci_read_resp_bytes altogether — +; its per-byte spin is iteration-counted (~7.5 s at 48 MHz, ~6 min at +; 1 MHz) — and anything short of 12 bytes reads as "no lease here" +; without touching net_local_ip, so a buffer left over from an earlier +; call can never be reported as this call's lease. ; ; The 12-byte response layout is IP(4) + Netmask(4) + Gateway(4). We copy -; the first 4 bytes into net_local_ip. If all probed interfaces yield a -; zero IP we return C=1 with net_last_error = UCI_ERR_NO_IP (or -; UCI_ERR_CMD_FAILED if the last probe failed at the command layer). +; the first 4 bytes into net_local_ip. If all probed interfaces yield no +; lease we return C=1 with net_last_error = UCI_ERR_NO_IP (or +; UCI_ERR_CMD_FAILED if the last push was rejected). ; ; Clobbers: A, X, Y ; Output: C=0 on success (net_local_ip populated), C=1 on failure @@ -522,6 +532,11 @@ NET_DHCP_MAX_IFACE = 4 ; probe interface indices 0..3 net_dhcp_acquire: lda #$00 sta @iface_idx ; SMC-style local, no-ZP file convention + ldx #3 ; no lease until a probe returns one: a +@clear_ip: ; failed call reads 0.0.0.0, never the last + sta net_local_ip,x ; call's address + dex + bpl @clear_ip @next_iface: jsr uci_wait_idle @@ -564,7 +579,14 @@ net_dhcp_acquire: @iface_idx: .byte 0 @no_err: - ; Read the 12-byte response into uci_ipaddr_resp. + ; Read the 12-byte response into uci_ipaddr_resp. The reply is + ; already VALID (uci_push_wait), so DATA_AV low means it is empty. + lda #$00 + sta uci_resp_count + lda UCI_STATUS + jsr uci_settle + and #UCI_STAT_DATA_AV + beq @drain lda #uci_ipaddr_resp @@ -576,12 +598,18 @@ net_dhcp_acquire: ; Drain anything we didn't consume (should be zero for 12 bytes, ; but this is cheap insurance against firmware revisions that ; return a longer record). +@drain: jsr uci_drain_resp bcs @dhcp_wait_to ; drain wedged — surface as DHCP fail jsr uci_drain_status bcs @dhcp_wait_to jsr uci_ack + ; Anything but the full record is "no lease on this interface". + lda uci_resp_count + cmp #12 + bne @no_ip + ; Copy the first 4 bytes (IP) into net_local_ip. ldx #3 @copy_ip: @@ -598,6 +626,7 @@ net_dhcp_acquire: ora net_local_ip+3 bne @have_ip +@no_ip: lda #UCI_ERR_NO_IP sta net_last_error @@ -605,8 +634,9 @@ net_dhcp_acquire: inc @iface_idx lda @iface_idx cmp #NET_DHCP_MAX_IFACE - bcc @next_iface - sec ; every interface probed, none had a lease + bcs @probed_all ; C=1: every interface probed, none leased + jmp @next_iface ; long branch: out of BCC range +@probed_all: rts @have_ip: diff --git a/tools/test_uci_dhcp_probe.py b/tools/test_uci_dhcp_probe.py new file mode 100644 index 0000000..b9f5803 --- /dev/null +++ b/tools/test_uci_dhcp_probe.py @@ -0,0 +1,323 @@ +#!/usr/bin/env python3 +"""net_dhcp_acquire must take a lease only from a full GET_IPADDR reply. + +WHAT THIS TESTS + +``net_dhcp_acquire`` (src/net/uci/net.s) probes UCI interface indices 0..3 +with GET_IPADDR and takes the first non-zero address. The firmware +(``network_target.cc``, unchanged from GideonZ/1541ultimate 915cfbe7 in +2015 through 3a1ff9ff) answers an index at or past +``getNumberOfInterfaces()`` with ``c_message_empty`` and the status line +``"82,PARAMETER(S) OUT OF RANGE"``. That is an ordinary, VALID reply: the +``$DF1C`` ERROR bit has one setter, a PUSH while not idle +(``command_protocol.vhd``), so ``uci_check_err`` returns C=0 and the old +code went on to read 12 bytes that were never sent. Two consequences: + + * ``uci_read_resp_bytes`` waits out its 65,536 fenced spins for the + first missing byte. That spin is iteration-counted, not TOD-bounded: + ~5,490 cycles per spin, so ~7.5 s per out-of-range probe at 48 MHz, + ~5.6 s at 64 MHz and ~6 min at 1 MHz. It is reached only when no + in-range interface has a lease (the loop stops at the first one). + * whatever ``uci_ipaddr_resp`` already held is copied to + ``net_local_ip`` and, if non-zero, returned as a lease with C=0. + +The second one needs a probe that returns fewer than 12 bytes with no full +reply before it in the same call. On firmware every in-range index +returns 12 bytes and in-range indices are probed first, so on a box with +at least one interface (every U64E and C64U: Ethernet registers as index +0, cable or not) the buffer is always freshly written before an empty +probe can read it. The stale-lease checks below therefore pin the +CONTRACT -- only a complete reply yields a lease -- against a modelled +zero-interface or short-reply firmware; they do not reproduce a failure +seen on a device. + +HOW IT TESTS IT + +The shipped machine code (``build/c64-https.prg`` at ``build/labels.txt`` +addresses) runs on ``test_uci_data_acc.py``'s 6502 interpreter against +``test_uci_timeout_recovery.py``'s register-file model, extended with a +firmware that answers GET_IPADDR from the command bytes actually written +to ``$DF1D``. The ``uci_fence`` delay loops are shortened in the loaded +image (inner count 217 -> 1): they are timing, not logic, and at full +length one out-of-range probe is ~140 M interpreted instructions. Wasted +waiting is measured in ``$DF1C`` reads per probe, which the fence does not +change. + +Runs standalone or under pytest (exit codes as its siblings: 0 pass, +1 fail, 2 cannot run; ``C64_UCI_TESTS_OPTIONAL=1`` opts out):: + + python3 tools/test_uci_dhcp_probe.py +""" + +import os +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from test_uci_data_acc import CPU, CPUError # noqa: E402 +import test_uci_timeout_recovery as base # noqa: E402 +from test_uci_timeout_recovery import ( # noqa: E402 + CommandInterface, Memory, Unavailable, VoluntarySkip, +) +from _skip_policy import require, verdict # noqa: E402 + +OPT_OUT_ENV = "C64_UCI_TESTS_OPTIONAL" +CERTIFIES = "net_dhcp_acquire's interface probe" + +UCI_ERR_NO_IP = 0x83 +UCI_TARGET_NETWORK = 0x03 +UCI_CMD_GET_IPADDR = 0x05 + +OK = b"00,OK" +OUT_OF_RANGE = b"82,PARAMETER(S) OUT OF RANGE" + +NEEDED_LABELS = ("net_dhcp_acquire", "net_local_ip", "net_last_error", + "uci_ipaddr_resp", "uci_status_len", "uci_status_force") + +# ldx #UCI_FENCE_OUTER / lda #UCI_FENCE_INNER / sbc #1 / bne / dex / bne +FENCE = bytes([0xA2, 0x05, 0xA9, 0xD9, 0xE9, 0x01, 0xD0, 0xFC, 0xCA, 0xD0, + 0xF7]) +INNER_OFFSET = 3 + +# $DF1C reads a probe may spend. A full reply costs a few dozen; the old +# read of an empty reply costs 65,536 on its own. +PROBE_READ_CEILING = 1000 + +LEASE_A = bytes([10, 43, 23, 83]) +LEASE_B = bytes([10, 53, 21, 158]) + + +def _reply(ip): + """GET_IPADDR's 12-byte record: IP, netmask, gateway.""" + return bytes(ip) + bytes([255, 255, 255, 0]) + bytes(ip[:3]) + b"\x01" + + +class Raw: + """A reply sent verbatim, not built from an IP.""" + + def __init__(self, data): + self.data = bytes(data) + + +class Firmware(CommandInterface): + """Answers GET_IPADDR from the bytes written to $DF1D, as + network_target.cc does. `ifaces` is one entry per registered + interface: an IP (4 bytes, all-zero = no lease) or a Raw reply.""" + + def __init__(self, ifaces): + super().__init__() + self.ifaces = list(ifaces) + self.cmd = [] + self.probes = [] # interface index per GET_IPADDR + self.reads_per_probe = [] # $DF1C reads from push to next push + + def read(self, addr): + if addr == 0xDF1C and self.reads_per_probe: + self.reads_per_probe[-1] += 1 + return super().read(addr) + + def write(self, addr, value): + if addr == 0xDF1D: + self.cmd.append(value) + return + if addr == 0xDF1C and value & 0x01 and self.state == 0: + self.replies = [self._answer(self.cmd)] + self.cmd = [] + self.reads_per_probe.append(0) + super().write(addr, value) + + def _answer(self, cmd): + if (len(cmd) != 3 or cmd[0] != UCI_TARGET_NETWORK + or cmd[1] != UCI_CMD_GET_IPADDR): + raise AssertionError("unexpected command bytes %s" + % " ".join("%02X" % b for b in cmd)) + index = cmd[2] + self.probes.append(index) + if index >= len(self.ifaces): + return (b"", OUT_OF_RANGE) + entry = self.ifaces[index] + return (entry.data if isinstance(entry, Raw) else _reply(entry), OK) + + +def _fast_fences(image): + out = bytearray(image) + count = 0 + at = out.find(FENCE) + while at >= 0: + out[at + INNER_OFFSET] = 0x01 + count += 1 + at = out.find(FENCE, at + 1) + if count == 0: + raise Unavailable("no uci_fence sequence found in the PRG — the " + "macro changed; update FENCE in this test") + return bytes(out) + + +def _machine(ifaces): + labels = base._labels() + for needed in NEEDED_LABELS: + if needed not in labels: + raise Unavailable( + "%s is not in build/labels.txt — this is not a BACKEND=uci " + "build; rebuild with `make BACKEND=uci " + "USE_NISTCURVES_ONCHIP=1`" % needed) + if not base.PRG.is_file(): + raise Unavailable("build/c64-https.prg is missing") + raw = base.PRG.read_bytes() + fw = Firmware(ifaces) + mem = Memory(_fast_fences(raw[2:]), raw[0] | (raw[1] << 8), fw, + tod_reads_per_tenth=10_000) # no TOD wait may expire + for name in ("uci_status_len", "uci_status_force", "net_last_error"): + mem.write(labels[name], 0) + return CPU(mem), mem, fw, labels + + +def _require(*args): + try: + return _machine(*args) + except Unavailable as exc: + if os.environ.get(OPT_OUT_ENV) != "1": + raise + require(False, str(exc), executed=0, total=len(TESTS), + certifies=CERTIFIES, opt_out_env=OPT_OUT_ENV) + + +def _acquire(cpu, labels): + return cpu.call(labels["net_dhcp_acquire"], budget=20_000_000) + + +def _ip(mem, labels): + return bytes(mem.read(labels["net_local_ip"] + i) for i in range(4)) + + +def _err(mem, labels): + return mem.read(labels["net_last_error"]) + + +def _probe_costs_bounded(fw): + worst = max(fw.reads_per_probe) + base._check(worst < PROBE_READ_CEILING, ( + "a probe spent %d $DF1C reads (per probe, indices %s: %s). An empty " + "GET_IPADDR reply is being read as if 12 bytes were coming: " + "uci_read_resp_bytes spins 65,536 fenced iterations for the first " + "missing byte, ~7.5 s per probe at 48 MHz and ~6 min at 1 MHz" + % (worst, fw.probes, fw.reads_per_probe))) + + +def test_lease_on_index_zero(): + """Control: Ethernet with a lease. One probe, C=0, that address.""" + cpu, mem, fw, labels = _require([LEASE_A, bytes(4)]) + carry = _acquire(cpu, labels) + base._check(carry is False and _ip(mem, labels) == LEASE_A, ( + "C=%d ip=%s err=$%02X" % (carry, list(_ip(mem, labels)), + _err(mem, labels)))) + base._check(fw.probes == [0], "probed %s, expected [0]" % fw.probes) + base._check(_err(mem, labels) == 0, "net_last_error not cleared") + + +def test_lease_on_wifi_index_one(): + """Control: a box on WiFi. Index 0 answers 0.0.0.0, index 1 has it.""" + cpu, mem, fw, labels = _require([bytes(4), LEASE_B]) + carry = _acquire(cpu, labels) + base._check(carry is False and _ip(mem, labels) == LEASE_B, ( + "C=%d ip=%s err=$%02X" % (carry, list(_ip(mem, labels)), + _err(mem, labels)))) + base._check(fw.probes == [0, 1], "probed %s" % fw.probes) + base._check(_err(mem, labels) == 0, "net_last_error not cleared") + + +def test_no_lease_does_not_wait_out_the_empty_probes(): + """Two interfaces, neither leased: probes 2 and 3 are out of range. + Expected $83, and no probe may spin out a read of an empty reply.""" + cpu, mem, fw, labels = _require([bytes(4), bytes(4)]) + carry = _acquire(cpu, labels) + base._check(carry is True and _err(mem, labels) == UCI_ERR_NO_IP, ( + "C=%d err=$%02X, expected C=1 $83" % (carry, _err(mem, labels)))) + base._check(fw.probes == [0, 1, 2, 3], "probed %s" % fw.probes) + base._check(fw.idle and fw.pushes_rejected == 0, + "interface left %s, %d push(es) rejected" + % (fw.describe(), fw.pushes_rejected)) + _probe_costs_bounded(fw) + + +def test_reinit_does_not_return_the_previous_lease(): + """'I' re-init in one PRG run: the first call leases LEASE_A; by the + second the modelled firmware has no interfaces, so every probe is + out of range. The second call must not report LEASE_A again.""" + cpu, mem, fw, labels = _require([LEASE_A]) + base._check(_acquire(cpu, labels) is False + and _ip(mem, labels) == LEASE_A, + "first acquire did not lease (control)") + fw.ifaces = [] + fw.probes, fw.reads_per_probe = [], [] + carry = _acquire(cpu, labels) + base._check(carry is True, ( + "second acquire returned C=0 with ip=%s from %d out-of-range " + "probes — the previous call's reply, still in uci_ipaddr_resp" + % (list(_ip(mem, labels)), len(fw.probes)))) + base._check(_err(mem, labels) == UCI_ERR_NO_IP, + "net_last_error=$%02X, expected $83" % _err(mem, labels)) + _probe_costs_bounded(fw) + + +def test_uninitialised_buffer_is_not_a_lease(): + """First call, zero interfaces, uci_ipaddr_resp holding whatever RAM + held (UCI_BSS is not zeroed at boot).""" + cpu, mem, fw, labels = _require([]) + for i, b in enumerate(b"\xde\xad\xbe\xef" + bytes(8)): + mem.write(labels["uci_ipaddr_resp"] + i, b) + carry = _acquire(cpu, labels) + base._check(carry is True and _err(mem, labels) == UCI_ERR_NO_IP, ( + "C=%d ip=%s err=$%02X: leftover buffer bytes were reported as a " + "lease" % (carry, list(_ip(mem, labels)), _err(mem, labels)))) + + +def test_short_reply_is_not_a_lease(): + """A reply shorter than the 12-byte record (4 bytes: an IP and nothing + else) is not GET_IPADDR's answer and must not be taken as a lease.""" + cpu, mem, fw, labels = _require([LEASE_A]) # first fill the buffer + _acquire(cpu, labels) + fw.ifaces = [Raw(LEASE_B)] # a 4-byte reply + fw.probes, fw.reads_per_probe = [], [] + carry = _acquire(cpu, labels) + base._check(carry is True, ( + "a 4-byte reply was accepted as a lease (ip=%s)" + % list(_ip(mem, labels)))) + + +TESTS = ( + test_lease_on_index_zero, + test_lease_on_wifi_index_one, + test_no_lease_does_not_wait_out_the_empty_probes, + test_reinit_does_not_return_the_previous_lease, + test_uninitialised_buffer_is_not_a_lease, + test_short_reply_is_not_a_lease, +) + + +def main(): + passed = failed = 0 + for test in TESTS: + try: + test() + except VoluntarySkip as exc: + print("SKIP: %s" % exc) + return verdict(passed, failed, + skipped=len(TESTS) - passed - failed, + opt_out_env=OPT_OUT_ENV, certifies=CERTIFIES) + except Unavailable as exc: + print("CANNOT RUN: %s" % exc) + return 2 + except (AssertionError, CPUError) as exc: + failed += 1 + print("FAIL %s: %s" % (test.__name__, exc)) + else: + passed += 1 + print("ok %s" % test.__name__) + print("%d/%d passed, %d assertions" % (len(TESTS) - failed, len(TESTS), + base.ASSERTIONS_RUN)) + return verdict(passed, failed, certifies=CERTIFIES) + + +if __name__ == "__main__": + sys.exit(main()) From 728b95e5d46d64c7dc18cb3b0841e320613c326a Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 5 Oct 2026 06:15:12 -0500 Subject: [PATCH 3/4] net/uci: DHCP probe review nits; drop the pre-check the spin fix made redundant Rebased onto 191b783 (fix/uci-resp-bounded-wait), which removed the 65,536-iteration DATA_AV spin from uci_read_resp_bytes. An empty reply now returns from the read at once with uci_resp_count = 0. - net_dhcp_acquire's own empty-reply pre-check is dropped: the DATA_AV test, the uci_resp_count clear and the uci_settle import. The uci_resp_count != 12 check alone rejects an empty reply. Against b8fdf8f6 (191b783 uci-onchip) the no-lease case already passes without it, and the stale-lease cases still fail. - Spin-cost figures are superseded and removed. e602e2b stated as fact "~7.5 s per out-of-range probe at 48 MHz, ~6 min at 1 MHz", and its test docstring added ~5.6 s at 64 MHz. Those were DERIVED (65,536 x ~5,489 cycles), not measured, and described a wait 191b783 no longer has. One hardware point exists for the old wait: on dce1b94 (45bea88a) a forced out-of-range probe took 8.1 s from key to screen at 48 MHz on the U64E. - Test: every failed-acquire case asserts net_local_ip reads 0.0.0.0. This kills the zero-on-entry mutant. The entry clear is kept for a caller that skips net_init (none in tree today), per net_abi.inc's "zero until net_dhcp_acquire succeeds". - The uninitialised-buffer case becomes leftover state on direct entry. uci_ipaddr_resp is file-backed in CRYPTO_HOT and loads as 12 zero bytes, so the case needs earlier code in the same RAM lifetime. - net.s: the CMD_FAILED arm no longer cites an out-of-range index; it only handles a rejected push. - Test docstring: the indexed GET_IPADDR handler dates from b4046f34 (2018), not 915cfbe7. - uci-m3/net.s: no longer claims "same contract". It accepts a reply of 4 bytes or more; uci requires the full 12. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci-m3/net.s | 4 ++- src/net/uci/net.s | 31 ++++++++--------- tools/test_uci_dhcp_probe.py | 64 +++++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/net/uci-m3/net.s b/src/net/uci-m3/net.s index bd04474..e95dcc3 100644 --- a/src/net/uci-m3/net.s +++ b/src/net/uci-m3/net.s @@ -305,7 +305,9 @@ m3_release: ; ============================================================================= ; net_dhcp_acquire — the firmware's lease via GET_IPADDR, interfaces 0..3, -; first non-zero address wins (same contract as src/net/uci/net.s). +; first non-zero address wins. Same loop as src/net/uci/net.s, but NOT the +; same reply rule: this one takes any reply of >= 4 bytes, where that one +; requires the full 12-byte record. ; Out: C=0 net_local_ip set; C=1 $83 no lease, or $82/$89. ; Clobbers: A, X, Y ; ============================================================================= diff --git a/src/net/uci/net.s b/src/net/uci/net.s index 5b3dd0f..bf12353 100644 --- a/src/net/uci/net.s +++ b/src/net/uci/net.s @@ -67,7 +67,6 @@ .import uci_drain_resp .import uci_drain_status .import uci_ack -.import uci_settle .import uci_resp_dst .import uci_resp_max .import uci_resp_count @@ -512,11 +511,13 @@ net_poll: ; it with an EMPTY reply and "82,PARAMETER(S) OUT OF RANGE" on the status ; channel, and the $DF1C ERROR bit stays clear (its only setter is a PUSH ; while not idle). So the reply is checked by length: only a full 12-byte -; record is a lease. An empty reply skips uci_read_resp_bytes altogether — -; its per-byte spin is iteration-counted (~7.5 s at 48 MHz, ~6 min at -; 1 MHz) — and anything short of 12 bytes reads as "no lease here" -; without touching net_local_ip, so a buffer left over from an earlier -; call can never be reported as this call's lease. +; record is a lease. Anything shorter (uci_resp_count != 12, an empty +; reply included) reads as "no lease here" without touching net_local_ip, +; so a buffer left over from an earlier call can never be reported as this +; call's lease. net_local_ip is also zeroed on entry. net_init already +; does that on the menu path; it is kept for a caller that skips net_init +; (none in tree today), per net_abi.inc's "zero until net_dhcp_acquire +; succeeds". ; ; The 12-byte response layout is IP(4) + Netmask(4) + Gateway(4). We copy ; the first 4 bytes into net_local_ip. If all probed interfaces yield no @@ -560,9 +561,10 @@ net_dhcp_acquire: jsr uci_check_err bcc @no_err - ; Command failed for this interface (e.g. an index past the - ; last registered interface). Clean up response/status state so - ; the next probe starts from idle, then advance. + ; The push was rejected (the interface was not idle). Clean up + ; response/status state so the next probe starts from idle, then + ; advance. An out-of-range index does NOT land here — see the + ; header. lda #UCI_ERR_CMD_FAILED sta net_last_error jsr uci_drain_resp @@ -579,14 +581,8 @@ net_dhcp_acquire: @iface_idx: .byte 0 @no_err: - ; Read the 12-byte response into uci_ipaddr_resp. The reply is - ; already VALID (uci_push_wait), so DATA_AV low means it is empty. - lda #$00 - sta uci_resp_count - lda UCI_STATUS - jsr uci_settle - and #UCI_STAT_DATA_AV - beq @drain + ; Read the 12-byte response into uci_ipaddr_resp. An empty reply + ; returns at once with uci_resp_count = 0 (the read does not wait). lda #uci_ipaddr_resp @@ -598,7 +594,6 @@ net_dhcp_acquire: ; Drain anything we didn't consume (should be zero for 12 bytes, ; but this is cheap insurance against firmware revisions that ; return a longer record). -@drain: jsr uci_drain_resp bcs @dhcp_wait_to ; drain wedged — surface as DHCP fail jsr uci_drain_status diff --git a/tools/test_uci_dhcp_probe.py b/tools/test_uci_dhcp_probe.py index b9f5803..aff8d81 100644 --- a/tools/test_uci_dhcp_probe.py +++ b/tools/test_uci_dhcp_probe.py @@ -5,23 +5,18 @@ ``net_dhcp_acquire`` (src/net/uci/net.s) probes UCI interface indices 0..3 with GET_IPADDR and takes the first non-zero address. The firmware -(``network_target.cc``, unchanged from GideonZ/1541ultimate 915cfbe7 in -2015 through 3a1ff9ff) answers an index at or past +(``network_target.cc``; the indexed GET_IPADDR handler dates from +GideonZ/1541ultimate b4046f34, 2018, and is byte-identical through +3a1ff9ff) answers an index at or past ``getNumberOfInterfaces()`` with ``c_message_empty`` and the status line ``"82,PARAMETER(S) OUT OF RANGE"``. That is an ordinary, VALID reply: the ``$DF1C`` ERROR bit has one setter, a PUSH while not idle (``command_protocol.vhd``), so ``uci_check_err`` returns C=0 and the old -code went on to read 12 bytes that were never sent. Two consequences: +code went on to copy 12 bytes that were never sent: whatever +``uci_ipaddr_resp`` already held went to ``net_local_ip`` and, if +non-zero, was returned as a lease with C=0. - * ``uci_read_resp_bytes`` waits out its 65,536 fenced spins for the - first missing byte. That spin is iteration-counted, not TOD-bounded: - ~5,490 cycles per spin, so ~7.5 s per out-of-range probe at 48 MHz, - ~5.6 s at 64 MHz and ~6 min at 1 MHz. It is reached only when no - in-range interface has a lease (the loop stops at the first one). - * whatever ``uci_ipaddr_resp`` already held is copied to - ``net_local_ip`` and, if non-zero, returned as a lease with C=0. - -The second one needs a probe that returns fewer than 12 bytes with no full +That needs a probe that returns fewer than 12 bytes with no full reply before it in the same call. On firmware every in-range index returns 12 bytes and in-range indices are probed first, so on a box with at least one interface (every U64E and C64U: Ethernet registers as index @@ -38,10 +33,9 @@ ``test_uci_timeout_recovery.py``'s register-file model, extended with a firmware that answers GET_IPADDR from the command bytes actually written to ``$DF1D``. The ``uci_fence`` delay loops are shortened in the loaded -image (inner count 217 -> 1): they are timing, not logic, and at full -length one out-of-range probe is ~140 M interpreted instructions. Wasted -waiting is measured in ``$DF1C`` reads per probe, which the fence does not -change. +image (inner count 217 -> 1): they are timing, not logic. Each probe's +``$DF1C`` reads are also counted and bounded, so a wait re-introduced on +the empty-reply path fails here rather than only being slow. Runs standalone or under pytest (exit codes as its siblings: 0 pass, 1 fail, 2 cannot run; ``C64_UCI_TESTS_OPTIONAL=1`` opts out):: @@ -79,8 +73,7 @@ 0xF7]) INNER_OFFSET = 3 -# $DF1C reads a probe may spend. A full reply costs a few dozen; the old -# read of an empty reply costs 65,536 on its own. +# $DF1C reads a probe may spend. A full reply costs a few dozen. PROBE_READ_CEILING = 1000 LEASE_A = bytes([10, 43, 23, 83]) @@ -197,13 +190,21 @@ def _err(mem, labels): def _probe_costs_bounded(fw): worst = max(fw.reads_per_probe) base._check(worst < PROBE_READ_CEILING, ( - "a probe spent %d $DF1C reads (per probe, indices %s: %s). An empty " - "GET_IPADDR reply is being read as if 12 bytes were coming: " - "uci_read_resp_bytes spins 65,536 fenced iterations for the first " - "missing byte, ~7.5 s per probe at 48 MHz and ~6 min at 1 MHz" + "a probe spent %d $DF1C reads (per probe, indices %s: %s): an " + "empty GET_IPADDR reply is being waited on as if 12 bytes were " + "coming" % (worst, fw.probes, fw.reads_per_probe))) +def _ip_cleared(mem, labels, when): + """A failed acquire must leave net_local_ip at 0.0.0.0, not at the + address a previous call (or anything else) left there.""" + base._check(_ip(mem, labels) == bytes(4), ( + "%s: net_local_ip reads %s after a FAILED acquire; a caller that " + "does not check carry, or a rig reading it, sees a stale address" + % (when, list(_ip(mem, labels))))) + + def test_lease_on_index_zero(): """Control: Ethernet with a lease. One probe, C=0, that address.""" cpu, mem, fw, labels = _require([LEASE_A, bytes(4)]) @@ -237,6 +238,7 @@ def test_no_lease_does_not_wait_out_the_empty_probes(): base._check(fw.idle and fw.pushes_rejected == 0, "interface left %s, %d push(es) rejected" % (fw.describe(), fw.pushes_rejected)) + _ip_cleared(mem, labels, "no lease") _probe_costs_bounded(fw) @@ -257,19 +259,26 @@ def test_reinit_does_not_return_the_previous_lease(): % (list(_ip(mem, labels)), len(fw.probes)))) base._check(_err(mem, labels) == UCI_ERR_NO_IP, "net_last_error=$%02X, expected $83" % _err(mem, labels)) + _ip_cleared(mem, labels, "re-init") _probe_costs_bounded(fw) -def test_uninitialised_buffer_is_not_a_lease(): - """First call, zero interfaces, uci_ipaddr_resp holding whatever RAM - held (UCI_BSS is not zeroed at boot).""" +def test_leftover_state_on_direct_entry_is_not_a_lease(): + """Direct entry (a rig trampoline, which skips net_init) with state + left over in the same RAM lifetime: uci_ipaddr_resp and net_local_ip + hold another address, and every probe is out of range. NOT a fresh + PRG load: uci_ipaddr_resp is file-backed in CRYPTO_HOT and loads as + 12 zero bytes, so this needs earlier code to have run.""" cpu, mem, fw, labels = _require([]) - for i, b in enumerate(b"\xde\xad\xbe\xef" + bytes(8)): + for i, b in enumerate(LEASE_B + bytes(8)): mem.write(labels["uci_ipaddr_resp"] + i, b) + for i, b in enumerate(LEASE_B): + mem.write(labels["net_local_ip"] + i, b) carry = _acquire(cpu, labels) base._check(carry is True and _err(mem, labels) == UCI_ERR_NO_IP, ( "C=%d ip=%s err=$%02X: leftover buffer bytes were reported as a " "lease" % (carry, list(_ip(mem, labels)), _err(mem, labels)))) + _ip_cleared(mem, labels, "direct entry") def test_short_reply_is_not_a_lease(): @@ -283,6 +292,7 @@ def test_short_reply_is_not_a_lease(): base._check(carry is True, ( "a 4-byte reply was accepted as a lease (ip=%s)" % list(_ip(mem, labels)))) + _ip_cleared(mem, labels, "short reply") TESTS = ( @@ -290,7 +300,7 @@ def test_short_reply_is_not_a_lease(): test_lease_on_wifi_index_one, test_no_lease_does_not_wait_out_the_empty_probes, test_reinit_does_not_return_the_previous_lease, - test_uninitialised_buffer_is_not_a_lease, + test_leftover_state_on_direct_entry_is_not_a_lease, test_short_reply_is_not_a_lease, ) From 4d00be34f0689a93584e4cd9eec3973a35f1cff9 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:43:01 -0500 Subject: [PATCH 4/4] net/uci: net_tcp_send fails a SOCKET_WRITE reply that is not a 2-byte count net_tcp_send read the "bytes written" reply into uci_write_resp without checking how many bytes arrived. uci_read_resp_bytes stores only what was sent, so an empty or 1-byte reply left the previous chunk's count in place and the send advanced on it. The #230(a) mis-parse shape is exactly that: "21,UNKNOWN COMMAND" with no data. A good 4-byte send followed by an empty reply returned C=0 with net_last_error $00. A reply count other than 2 is now a failed send: $87 UCI_ERR_SHORT_WRITE, C=1. That is the same verdict and code as the $FFFF arm beside it, and as uci-m3's send, which already checks m3_rd_count == 2. No new code. The reply is fully drained and acked first, so the interface is left idle. Tests: two checks added to tools/test_uci_timeout_recovery.py (a stale count after a good send, and 0/1-byte replies). Both fail on 728b95e and pass here. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci/net.s | 12 ++++++- tools/test_uci_timeout_recovery.py | 54 +++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/net/uci/net.s b/src/net/uci/net.s index bf12353..aee3082 100644 --- a/src/net/uci/net.s +++ b/src/net/uci/net.s @@ -793,7 +793,8 @@ net_tcp_connect: ; Response: 2 bytes = written_lo/hi (LE). If written != requested we set ; UCI_ERR_SHORT_WRITE but still return C=0 so the caller can continue ; (mirrors ip65 behaviour that treats short writes as best-effort). A -; written count of $FFFF (lwip_send's -1) is a failed send: C=1, $87. +; written count of $FFFF (lwip_send's -1), or a reply that is not exactly +; the 2-byte count, is a failed send: C=1, $87. ; ============================================================================= net_tcp_send: sta uci_send_ptr_lo @@ -939,8 +940,17 @@ net_tcp_send: ; and GREW uci_send_rem by one per round trip, so the loop ran ; ~65k SOCKET_WRITEs (~45 min) before rem wrapped to zero and it ; returned C=0. A chunk is at most 800 B, so bit 15 is never a count. + ; + ; A reply that is not exactly 2 bytes carries no count at all, and + ; uci_write_resp would still hold the PREVIOUS chunk's: the #230(a) + ; mis-parse ("21,UNKNOWN COMMAND", empty reply) is that shape. Same + ; verdict as uci-m3's send: $87, C=1. + lda uci_resp_count + cmp #2 + bne @sb_no_count lda uci_write_resp+1 bpl @sb_counted +@sb_no_count: lda #UCI_ERR_SHORT_WRITE sta net_last_error sec diff --git a/tools/test_uci_timeout_recovery.py b/tools/test_uci_timeout_recovery.py index ecc4fcf..42cc754 100644 --- a/tools/test_uci_timeout_recovery.py +++ b/tools/test_uci_timeout_recovery.py @@ -90,6 +90,13 @@ expire on purpose. (``test_uci_data_acc.py`` freezes the TOD for the opposite reason: there, a timeout would be the bug.) +ALSO: THE WRITE REPLY'S LENGTH + +The last two checks pin net_tcp_send's handling of a SOCKET_WRITE reply +that is not the 2-byte written count (empty or 1 byte). uci_read_resp_bytes +stores only what arrived, so without a length check uci_write_resp keeps +the previous chunk's count and the send "succeeds" on it. + Runs standalone or under pytest:: python3 tools/test_uci_timeout_recovery.py @@ -109,7 +116,7 @@ from _skip_policy import VoluntarySkip, cannot_run, require, verdict # noqa: E402 OPT_OUT_ENV = "C64_UCI_TESTS_OPTIONAL" -CERTIFIES = "net_tcp_send's timeout exits (#194)" +CERTIFIES = "net_tcp_send's timeout exits (#194) and its reply-length check" # --- register bits (uci_regs.inc) ------------------------------------------ UCI_STAT_DATA_AV = 0x80 @@ -133,6 +140,7 @@ # uci_errors.inc UCI_ERR_READ_FAIL = 0x86 +UCI_ERR_SHORT_WRITE = 0x87 UCI_ERR_WAIT_TIMEOUT = 0x89 # How many $DF1C reads the modelled firmware takes to answer a command or to @@ -560,6 +568,48 @@ def test_a_clean_send_neither_aborts_nor_leaves_the_phase_open(): "a clean send had %d push(es) rejected" % uci.pushes_rejected) +def _send_again(cpu, mem, labels): + """A second net_tcp_send of the same payload, error byte cleared.""" + mem.write(labels["net_last_error"], 0) + cpu.a = SEND_SRC & 0xFF + cpu.x = SEND_SRC >> 8 + return _send(cpu, labels) + + +def _assert_failed_send(carry, mem, uci, labels, what): + err = mem.read(labels["net_last_error"]) + _check(carry is True, ( + "net_tcp_send returned C=0 after %s: SOCKET_WRITE's reply carried " + "no 2-byte written count, so the send was reported done on a count " + "nobody sent (net_last_error $%02X)" % (what, err))) + _check(err == UCI_ERR_SHORT_WRITE, + "net_last_error is $%02X after %s, expected $87 " + "UCI_ERR_SHORT_WRITE" % (err, what)) + _check(uci.idle and uci.pushes_rejected == 0, + "interface left in state %s after %s" + % (_leftover_state(uci), what)) + + +def test_a_countless_write_reply_does_not_reuse_the_previous_count(): + """Stale count: a good send fills uci_write_resp with 4; the next + SOCKET_WRITE comes back empty (the #230(a) mis-parse answers + "21,UNKNOWN COMMAND" with no data). Reading 0 bytes leaves the 4 in + place, and without a reply-length check the send succeeds on it.""" + cpu, mem, uci, labels = _require( + _machine, [(b"\x04\x00", b"00,OK"), (b"", b"21,UNKNOWN COMMAND")]) + _check(_send(cpu, labels) is False, "the first (good) send failed") + _assert_failed_send(_send_again(cpu, mem, labels), mem, uci, labels, + "an empty reply following a good send") + + +def test_a_short_write_reply_is_a_failed_send(): + """0- and 1-byte replies on a first send: not a written count either.""" + for reply in (b"", b"\x04"): + cpu, mem, uci, labels = _require(_machine, [(reply, b"00,OK")]) + _assert_failed_send(_send(cpu, labels), mem, uci, labels, + "a %d-byte reply" % len(reply)) + + # --------------------------------------------------------------------------- # Dual-mode runner (same conventions as tools/test_uci_data_acc.py) # --------------------------------------------------------------------------- @@ -569,6 +619,8 @@ def test_a_clean_send_neither_aborts_nor_leaves_the_phase_open(): test_push_wait_timeout_leaves_the_interface_idle, test_a_send_timeout_does_not_get_the_next_poll_rejected, test_a_clean_send_neither_aborts_nor_leaves_the_phase_open, + test_a_countless_write_reply_does_not_reuse_the_previous_count, + test_a_short_write_reply_is_a_failed_send, ) EXIT_OK, EXIT_FAILED, EXIT_CANNOT_RUN = 0, 1, 2