Skip to content

lpc845: pinToggleRx + ws2812SctTest handlers stack-overflow at runtime (8 KB stack buffer on 16 KB SRAM) #3125

Closed

Description

@zackees

Symptom

On LPC845-BRK (16 KB SRAM) the pinToggleRx RPC handler in
examples/AutoResearch/AutoResearchLowMemory.h declares an 8 KB
stack-allocated buffer:

// examples/AutoResearch/AutoResearchLowMemory.h:187
fl::EdgeTime edges_buf[2048];

fl::EdgeTime is 4 bytes, so this is 2048 * 4 = 8192 bytes on the stack
inside one lambda. The ws2812SctTest handler at line 286 has a similar
1024-element probe (~4 KB).

.bss for the unified LowMemory build on LPC845-BRK already sits at ~8.5 KB
(measured today against current master with arm-none-eabi-size + nm).
The default LPC845 stack lives at the top of SRAM and grows down. When
pinToggleRx is invoked, the stack frame demand spikes by 8 KB on top of
.bss + Arduino core static + libc reentrancy state. We are over the 16 KB
SRAM ceiling the moment the handler executes.

Reproduction (verified today)

Tested against current master (post-#3084 / #3085 / #3087 / #3088 / #3089
bloat fixes, with FASTLED_LPC_RX_SCT_WS2812=1 enabled by default on
lpc845brk):

bash autoresearch lpc845brk --pin-toggle-rx --upload-port COM10

[lpc-pin-toggle-rx] opening COM10 @ 115200
[lpc-pin-toggle-rx] echo ok                 # <-- the device boots; echo RPC works

--- 1.1 / 1 kHz (1000 Hz / 200 ms) ---
  FAIL: no response or RPC error: None
--- 1.2 / 10 kHz (10000 Hz / 100 ms) ---
  FAIL: no response or RPC error: None
--- 1.3 / 100 kHz (100000 Hz / 100 ms) ---
  FAIL: no response or RPC error: None

The device is alive (echo round-trips). The moment a pinToggleRx
request lands, the device goes silent and never replies. Consistent with
a runtime stack overflow into the watchdog-fed live region.

Why this is the natural reading

  • LPC845-BRK enumerated as a NXP CMSIS-DAP CDC interface (VID 1FC9 /
    PID 0132); board hardware is fine.
  • bash autoresearch echo round-trip succeeds -- proves fl::Remote,
    the watchdog feed, the JSON-RPC parser, and the CDC transport are all
    wired correctly under the LowMemory build.
  • The very next RPC -- pinToggleRx -- silently never replies. The
    handler does its first work before writing any response back to the
    serial, and the first work is to allocate 8 KB on the stack. If the
    stack pointer crosses the bottom of SRAM the device traps before any
    reply is sent.
  • The ws2812SctTest handler at line 286 (when reached via the
    FASTLED_LPC_RX_SCT_WS2812 path) has the same shape with a 4 KB +
    300-byte chain plus a 1024-entry probe -- so the symptom persists
    beyond just the pin-toggle bench.

Fix shape

Three obvious ways to land this. (1) is the smallest, (2) is the
architecturally-right answer:

  1. Move the buffer to static storage (static fl::EdgeTime edges_buf[2048];).
    • One-line change. Adds the 8 KB to .bss permanently which is fine on
      a target that already runs 1-2 RPC handlers serially.
    • Pulls ws2812SctTest's probe[1024] and decoded_buf[300] /
      expected_buf[300] along the same way.
  2. Right-size the buffer to what the bench actually needs. Phase 1's
    1 kHz / 100 ms case captures 2 * 1000 * 0.1 = 200 edges plus a 50%
    headroom = 300 edges. Even the 100 kHz / 100 ms case is at most
    20,000 edges total -- and a 2048-element ring already truncates that
    on the high end. The handler logic already enforces a
    cap = expected_edges + headroom; if (cap > 2048u) cap = 2048u;
    ceiling, so the captured edge count is at most 2048; declaring an
    over-sized stack buffer to match the cap is symmetric with what
    cfg.buffer_size already controls. A tighter cap (e.g. 512 edges =
    2 KB stack) would still satisfy the Phase 1 acceptance window.
  3. Heap-allocate via fl::unique_ptr<EdgeTime[]> with a size chosen
    from cap. Smallest dynamic footprint per call, but introduces malloc
    pressure on the Low-memory bring-up which we're otherwise trying to
    avoid.

I lean (1)+(2) together: static fl::EdgeTime edges_buf[512]; (and the
matching change on the ws2812SctTest side), with the cap clamp updated
to match.

Acceptance

This issue closes when:

  • bash autoresearch lpc845brk --pin-toggle-rx --upload-port COM10
    on hardware returns three non-None results (one per frequency case)
    with at least the 1 kHz case showing success=1.
  • bash autoresearch lpc845brk --ws2812-loopback --upload-port COM10
    doesn't silently hang.
  • The handler is documented as carrying at most a bounded static
    buffer; no future change pushes a multi-KB stack allocation back in.

Refs

🤖 Filed via Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions