Skip to content

fix(data): dispatch no longer writes past a full channel buffer - #173

Open
luizvilla wants to merge 1 commit into
owntech-foundation:mainfrom
luizvilla:fix/data-dispatch-buffer-overflow
Open

luizvilla wants to merge 1 commit into
owntech-foundation:mainfrom
luizvilla:fix/data-dispatch-buffer-overflow

Conversation

@luizvilla

Copy link
Copy Markdown
Member

Summary

In task dispatch, every enabled channel collects its values in a CHANNELS_BUFFERS_SIZE-slot (32) buffer that only data.get*() empties. The count stops at 32, but the dispatch uses it as the write index:

 data_dispatch_do_dispatch(adc)
   for each new DMA sample
     current_count = count of the channel
+    if current_count >= CHANNELS_BUFFERS_SIZE
+      current_count = CHANNELS_BUFFERS_SIZE - 1   # keep the last slot for the newest value
     active_buffer[current_count] = sample           # was: index 32, past the heap block
     increment count (stops at 32)

When it happens: an application enables a channel and never fetches it with get*(). It only peeks, or doesn't read that channel at all, for example a sensor enabled by enableDefaultSensors(). Then:

  • after 32 dispatches, each new sample goes one slot past the end of the buffer's heap block, every task period;
  • peek() returns slot 31, which never changes: the reading freezes.

Evidence

Test firmware on a Spin with the Twist 1.4.2 devicetree (env STLink):

  • enableDefaultSensors(), buck legs at 200 kHz;
  • a 100 µs critical task that only peeks V1_LOW.

Over SWD, a marker 0xA5A5 is written at index 32 of each ADC1 channel buffer, then read back as the firmware runs:

ADC1 channel buffer main This branch
count 32 32
slot [31] (what peek() returns) frozen (292, 177, 208 on every read) follows the samples (396 → 393 → 397…)
index [32], past the end marker overwritten within 1 s, changes at every read marker 0xA5A5 kept over 150 000 dispatches

Merge Danger

Door: two-way

One check in the dispatch loop.

Blast Radius: unfetched channels

  • Changes: only channels whose buffer is full. Their newest value replaces slot 31, so peek() follows the signal, and no write goes past the buffer.
  • Unchanged: channels fetched with get*() each period never fill up.
  • Downstream: applications with an unfetched channel stop corrupting the heap block after that buffer. Code that sat behind it in the heap may change behaviour, for the better.

🤖 Generated with Claude Code

In task dispatch, each channel collects its values in a
CHANNELS_BUFFERS_SIZE-slot buffer that only data.get*() empties. The count
stops at CHANNELS_BUFFERS_SIZE, but the write used it as an index: a
channel the application never fetches (it only peeks, or does not read it)
wrote one slot past its heap block every dispatch, and peek() returned a
frozen value. A full buffer now keeps its last slot for the newest value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant