Skip to content

[LOW] In-FastLED OOM allocator wrapper — diagnostic emit at allocation site (complement to LPC8xx#30) #3301

Closed as not planned

Description

@zackees

Motivation

FastLED already controls every allocation site in its own code via cpp linting (bare_allocation_checker.py -- bans raw new/malloc/delete/free in src/, requires fl::unique_ptr / fl::shared_ptr). This means we have a chokepoint we can instrument: a FastLED-owned allocator wrapper that checks every allocation for null and emits a diagnostic before the caller dereferences the null pointer.

The same OOM problem on the Arduino-core side is filed as zackees/ArduinoCore-LPC8xx#30 (HardFault override) -- that fix catches most OOM crashes once they cascade into null-deref → HardFault. But it has two limitations:

  1. Silent miss case: if the first access via the null pointer is a small-offset read/write that lands in flash (vector table region), the access is silently ignored on M0+ and the program continues with garbage state. No HardFault fires, no emit, eventually crashes downstream with a confusing PC.
  2. Worse PC info when it DOES fault: the captured PC points at the use site, not the allocation site. Decoding which new call returned null requires objdumping the firmware.

A FastLED-side OOM trap catches both: every allocation is checked at the point of allocation, the size is logged, the diagnostic is emitted before any null pointer is propagated to a caller.

Priority

LOW. The LPC8xx-side HardFault override (#30 on that repo) covers the bulk of the diagnostic gap. The remaining ~20% of OOM cases that slip through (silent flash-region writes) are rare in C++ object access patterns -- virtual calls, multi-byte field accesses, and array iteration all eventually walk into unmapped memory and trigger HardFault. The FastLED-side trap is a refinement that turns "FAULT: HardFault PC=0x..." into "FAULT: OOM size=0x40 at allocator.cpp.hpp:153", which is faster to triage but not the primary fix.

This issue exists so the leverage is documented and a future contributor can pick it up; we are not blocking #3300 / #3102 on it.

Proposed design

Single chokepoint

src/fl/stl/allocator.cpp.hpp already centralizes FastLED's allocation primitive (fl::Malloc -- the one function that the bare_allocation_checker linter steers every site toward). Add a null-check + emit:

void* Malloc(size_t bytes) FL_NO_EXCEPT {
    void* p = (DefaultAlloc(bytes));
    if (!p) {
        fl::diagnostics::emit_oom(bytes);  // see "platform emit hook" below
    }
    return p;
}

Platform emit hook (fl::diagnostics::emit_oom)

Lives in src/fl/diagnostics/oom.h + oom.cpp.hpp. Per-platform:

Platform Emit path Reboot
LPC8xx (M0+) direct USART0 TXDAT sync write (mirrors the Arduino-core HardFault emit's helper if #30 lands) AIRCR.SYSRESETREQ
STM32 (M0/M3/M4/M7) direct USART1 / lowest-numbered enabled USART AIRCR.SYSRESETREQ
RP2040 UART0 register write AIRCR.SYSRESETREQ
AVR / ATmega direct UDR0 register write wdt_enable(WDTO_15MS); while(1){}
ESP32 / Teensy 4 / host fprintf(stderr, ...) + abort() (FPU + plentiful RAM; OOM is recoverable, this is debug-build only) --

The "no allocation, no printf, no Serial.print" rule applies to the embedded paths. The hook signature:

namespace fl { namespace diagnostics {
[[noreturn]] void emit_oom(fl::size bytes) FL_NO_EXCEPT;
}}

Per-platform format

FAULT: OOM size=0x<hex> at <PC of caller> -- the PC is captured via __builtin_return_address(0) so the emit identifies the failing allocation site. Decode via arm-none-eabi-addr2line firmware.elf 0x<pc> to get file:line.

Gating

  • Default ON for low-memory platforms (!FL_PLATFORM_HAS_LARGE_MEMORY).
  • Default OFF for large-memory platforms (FPU + 256 KB+ RAM; OOM there is a programming error, not a hardware reality, and the emit cost isn't justified).
  • User opt-out via FASTLED_NO_OOM_TRAP for production builds that prefer the bare-pointer-fast-path.

Acceptance criteria

  • fl::Malloc in src/fl/stl/allocator.cpp.hpp checks for null and calls fl::diagnostics::emit_oom(bytes) on failure.
  • fl::diagnostics::emit_oom implemented for LPC8xx, STM32, RP2040, AVR, host -- each in its own <platform>/oom.impl.hpp shim.
  • Caller-PC included in the emit via __builtin_return_address(0).
  • FASTLED_NO_OOM_TRAP opt-out gate.
  • Default-on for !FL_PLATFORM_HAS_LARGE_MEMORY, default-off otherwise.
  • Flash budget per platform: <= ~250 B (the emit helpers are mostly shared with the per-platform fault-emit path that latest release will not install in ardduino w/o manual edits #30 establishes for LPC).

Cross-references

Out of scope

  • Replacing every existing call site's fl::Malloc with the new checking version -- they already go through fl::Malloc per the bare_allocation_checker linter. No call-site changes needed.
  • Adding a per-instantiation memory-resource override (we already have fl::memory_resource). The OOM trap fires at the default resource's do_allocate; custom resources can opt in by routing through fl::Malloc or skip the trap entirely.
  • Recovering from OOM (caller-side fallback). This is hardware-side detection only; recovery is the application's choice.

Refs

  • src/fl/stl/allocator.cpp.hpp -- the chokepoint.
  • src/fl/stl/malloc.cpp.hpp -- the platform malloc wrapper.
  • ci/lint_cpp/bare_allocation_checker.py -- the lint rule that steers all allocations to fl::Malloc.
  • agents/docs/cpp-standards.md -- the project's "no bare new/delete" rule.

Filed during /clud-fix burn-down on #3208 while diagnosing the #3102 / #3300 bring-up echo silence.

Activity

  1. zackees commented on Jun 23, 2026

    @zackees
    MemberAuthor

    Tracking this under the new LPC845-BRK / LPC8xx AutoResearch coordination meta: #3348.

    Keeping this issue open for now as optional LPC8xx validation safety-net work. If diagnostics are deferred, close this separately with rationale from #3348 rather than folding it silently into the meta.

  2. zackees commented on Jul 6, 2026

    @zackees
    MemberAuthor

    Closing as not planned. We are not pursuing the standalone in-FastLED allocator OOM wrapper path.

    Active LPC board tracking is consolidated under #3348, with only #3300 and #3302 remaining as sub-issues.

  3. moved this from Triage to Done in FastLED Trackeron Jul 6, 2026
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