Skip to content

# Socket DMA service: read each message whole, never run a partial payload - #1076

Merged
chrisgleissner merged 6 commits into
GideonZ:masterfrom
JC-000:fix/socket-dma-short-payload
Nov 10, 2026
Merged

chrisgleissner merged 6 commits into
GideonZ:masterfrom
JC-000:fix/socket-dma-short-payload

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #1051

Problem

The socket DMA service (TCP, used by the "DMA" network service and tools that push images/CRTs) can execute a command on a partial payload, and can lose stream framing after an over-size payload, so the next bytes are parsed as commands. A client that pauses mid-message (WiFi retries, a flash erase, a slow card) can have a half-received payload run as if complete. On an unauthenticated connection the receive loop also assumes every header recv() returns the full 2 bytes.

Cause, on current master:

SocketDMA::dmaThread in software/network/socket_dma.cc:481-514:

  • The 2-byte command/length headers are read with a bare recv(newsockfd, buf, 2, 0) and the code comments // assume n = 2. With the 1 s SO_RCVTIMEO a short read is not handled.
  • readSocket (socket_dma.cc:353) returns received > 0 ? received : n (the return at :361), i.e. a partial count if the stream stalls part-way; performCommand is then called with that partial length and runs the command on a truncated buffer.
  • An over-size payload is clamped (len32 = SOCKET_BUFFER_SIZE) but the surplus bytes are left in the stream and read next as a command/length pair.

What changed and why

readSocket now reads exactly length bytes or fails; it gives up a message only after SOCKET_DMA_STALL_MS (30 s) without progress, returns -1 on a mid-message close, and enables TCP keepalive so dead peers are reaped. An over-size payload is drained and skipped cleanly instead of being clamped. All header reads go through the same bounded reader.

Products. runs on the Ultimate 64, Ultimate 64 Elite II, C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L). Tested on the Ultimate 64 Elite and the C64 Ultimate.

How I tested it

Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.

Host tests (device-free, run before and after the change):

  • socket DMA service with the real dmaThread: make -B -C target/pc/linux/socketdma test
    • with the change: 13 passed, 0 failed
    • without it: 2 passed, 11 failed (master socket_dma.cc and .h)

Hardware.

Device Firmware Ultimate FPGA C64 core
Ultimate 64 Elite ac0304316 126 1.50
C64 Ultimate b0f40a17b 126 1.50

RED = master, GREEN = fix, both products via the test harness.

case RED GREEN
plain / idle-gap / mid-payload gap / truncated runs or truncates (3 of 4 wrong) all correct
client stalls past the limit socket closed after 2.33-2.35 s, half executed closed after 30.0 s, nothing executed

Host suite target/pc/linux/socketdma: 2/13 on master → 13/13 with the fix; three mutants killed.

Standard profile (./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report from python3 tools/e2e_report.py:

  • U64 Elite: 71 of 72 suites pass on master and 71 of 72 with the fixes. The one failure is the same on both, iec-partition-config: it stops at a key-send step because the upstream test's REST backend has no F2 key alias (rest_backend.py:556), a test-side limitation, not an app difference.
  • C64 Ultimate: 71 of 72 on master and 71 of 72 with the fixes. The one failure is again iec-partition-config, here at a "menu screen unavailable" step that also fails on master, so it is not from these changes.

Soaks pass on both devices: mount-cache-leak, listener-soak, network-connection.

Untested

  • Ultimate-II cartridge (U2+ / U2+L): not tested. I do not have one. Not built either for the cartridges, because the build environment I use cannot build those targets; CI builds them.
  • An authenticated client that then goes silent holds the single DMA slot indefinitely (the 30 s stall limit applies until authentication; keepalive reaps dead peers only). That is how it is built, on purpose; capping idle authenticated clients is a one-line policy change if you prefer it.

Written with Claude Opus 5.5 in Claude Code.

JC-000 and others added 2 commits November 7, 2026 11:53
Each client socket has a 1 s receive timeout, so the service can see
when it is switched off. The read loop treated that timeout as the end
of the payload. A client that paused for a second mid-payload (WiFi
retries, a flash erase, a slow card) got its program DMA'd and run
with a tail that never arrived, and the rest of the payload was then
read as commands. A pause of a second between two commands dropped
the connection, and a header split across two segments was misread.

Read every header and payload in full. A timeout is now a retry: a
message is given up only after 30 s without progress, and a client
may wait as long as it likes between commands, with TCP keepalive to
reap a dead peer. A payload that does not arrive in full is never
executed; the connection is closed. A payload larger than the buffer
was truncated to 200000 bytes and run; it is now read and discarded so
the stream stays in step. The service still notices being switched off
within a second.

The wire protocol is unchanged.

The new host test target/pc/linux/socketdma runs socket_dma.cc against
loopback sockets and stubs; it is part of make host_tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With a network password set, a client that connected and never
authenticated could sit silent for ever and hold the only socket DMA
slot; before the previous change it was dropped after 1 s. Such a
client now gets the 30 s stall limit while it waits; only a client
that may send commands may wait between them indefinitely.

C64 never waits: a client that cannot send commands cannot keep the
C64's DMA service from the next client for longer than the limit.

Tests: an unauthenticated silent client is dropped at the limit; an
authenticated one may idle past it; one header byte followed by
silence is dropped at the limit; an upload that keeps coming is loaded
whole even when it takes longer than the limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Nov 10, 2026
@chrisgleissner
chrisgleissner self-requested a review November 10, 2026 12:30

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 thanks for this. Reading each message whole is the right fix, and the host tests compile the real socket_dma.cc: 11 of 13 fail when I revert it to master.

Requesting changes on two points:

  1. Drain before auth. The over-size drain (up to 16 MB) runs before the authentication check. A client that has not authenticated can make the device read that much. Please stop before draining when the connection is unauthenticated and the command is not the authentication command.
  2. Hardware evidence. The hardware table in the description comes from a harness that is not in the PR. Please add it as an e2e suite registered in run-tests, or describe how to reproduce it, so the u64 and C64U runs can be repeated.

I'll merge once these are done.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 thank you for this fix. Reading each message whole is a real improvement, and the host tests that compile the real socket_dma.cc made it easy to review. So that you do not have to spend more time on it, I'm taking this PR over and have assigned it to myself.

I will make the one change I asked for in my review (stop before the over-size drain when the connection is not yet authenticated), run the hardware checks on the Ultimate 64 and the U2+L, and push it as separate commits on your branch. Nothing is needed from you. If you have already started on it, please say so here and I'll hold off.

…nZ#1076)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for taking this over. One heads-up: a merge of master into the branch (5b6ebf0) went out 24 s after your comment, from a base update that was already running. It only brings the branch up to date and doesn't touch your review points. Pull before you push. I won't push anything more to this branch or #1071.

Written with Claude Opus and Claude Sonnet in Claude Code.

@chrisgleissner

chrisgleissner commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

@JC-000 an update on this PR, which I'm finishing up. Thank you again for the fix.

I pushed to your branch (af1046552, on top of your master merge 5b6ebf03c):

  • Merge upstream master: the host_tests list in the top-level Makefile overlapped with the new ucitargets line; I kept both. It merged without conflict with your own master merge, which landed while I was working. (The first version of this comment said the push had already happened; it had been rejected because of your merge, and went through after I merged it.)
  • Socket DMA: drop an unauthenticated client before reading its payload: three lines in dmaThread that close the connection when a client has not authenticated and sends any command but AUTHENTICATE, before the payload is read. performCommand refused it afterwards anyway, but only after the whole payload (up to 16 MB for an over-size one) had been read.

Verification of that commit:

  • Host: new test unauthenticated_command_is_dropped_without_reading_its_payload; 14 tests pass. With the three lines removed it fails (the connection closes only after the 3 s stall limit of the test).
  • Ultimate 64 Elite and C64 Ultimate, with a network password set: an unauthenticated client sends the header of a 1 MB RUN_CRT and no payload. Before the commit the connection closed after 30 to 31 s (the stall limit), three runs on each device. With it, it closes immediately, three runs on each device. An authenticated session still gets its AUTH reply and an IDENTIFY answer. dma-round-trip passes on both.
  • Still to do: the U2+L.

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 thank you for this fix and for the real-code host tests. I have read the diff and the three-line addition I pushed, and I am merging.

Review: each command and length header is now read whole, a payload is never run partially, an over-size payload is skipped so the stream stays in sync, and a client that has not authenticated is dropped at the stall limit or, with the added lines, as soon as it sends anything but AUTHENTICATE. CI is green on the head.

Tests: the 14 host tests compile the real socket_dma.cc. With socket_dma.cc reverted, 11 of 13 original tests fail, and with only my three lines removed the new test fails.

Hardware (Ultimate 64 Elite and C64 Ultimate, network password set): an unauthenticated header for a 1 MB RUN_CRT with no payload held the connection for 30 to 31 s before the added lines and closes immediately with them, three runs each. An authenticated session still gets its AUTH reply and an IDENTIFY answer, and dma-round-trip passes on both.

Not tested: the U2+L. The addition is an early break that performCommand already did for the same condition, so I judged it unlikely to behave differently there.

@chrisgleissner
chrisgleissner merged commit b57d894 into GideonZ:master Nov 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket DMA service runs truncated and mis-framed payloads instead of whole messages

2 participants