Skip to content

fix(fastpin): prevent null-register access in forced software pins without pin maps #5056

Closed

Description

@zackees

Context

Native child of #5024, found while validating #5044's real ClearCore forced-software configuration. This is a distinct shared fallback correctness defect, present in baseline 94dab9c6b, not a failure caused by missing board hardware.

Round 1 traced actual selection: defining FASTLED_FORCE_SOFTWARE_PINS selects the common FastPin class in src/fl/system/fastpin_base.h:69-123; SAME53's platform specialization is intentionally excluded by src/platforms/arm/same53/fastpin_arm_same53.h:16-20. same53/led_sysdefs_arm_same53.h:8 defines FASTLED_NO_PINMAP because the connector API does not expose Arduino register maps.

Round 2 verified the defect and contract: _init() at fastpin_base.h:74-80 skips every assignment with FASTLED_NO_PINMAP. sPinMask, sPort and sInPort are zero-initialized static storage (definitions122-124). hi/lo (92-94), pointer overloads (103-105), set/fastset (107-110) and hival/loval (113-115) still dereference those pointers. GenericSoftwareSPIOutput::init calls setOutput but that cannot initialize them. Ordinary bulk sees two null port identities as equal and enters same-port snapshot code, then hival/loval dereference zero. Forced interrupt-safe output enters scalar hi/lo and still dereferences zero. Compiler optimization may exploit this undefined behavior; it must not be treated as a harmless no-op.

src/fastled_config.h:13-16 publicly documents the option as forcing digitalWrite access for debugging, limited to Arduino environments. ClearCore is Arduino and src/platforms/arm/same53/pin_same53.hpp:15-35 supplies correct connector-aware pinMode/digitalWrite adapters. There is no found contract excluding Arduino NoPinMap boards. The existing runtime fl::Pin in src/platforms/generic_pin.h:68-105 already falls back to digitalWrite/digitalRead when register pointers are absent; the forced compile-time FastPin does not.

The root's real ClearCore audit build with FL_SOFTWARE_SPI_COMPILE_PROBE=1,FASTLED_FORCE_SOFTWARE_PINS=1 currently stops at an additional API mismatch: forced FastPin has no validpin() while the probe asserts the hardware specialization's validity. Removing only those probe assertions would reveal the unsafe writer; compilation alone must not certify it.

Proposal

Implement a safe portable forced-software pin path for Arduino cores without register pin maps, preserving connector polarity via the native digitalWrite API. Ensure the SPI writer selects per-pin logical operations and never compares dummy/null port identities to enter snapshot/whole-port streaming. Retain mapped-Arduino behavior where appropriate, and never route a NoPinMap fallback through fabricated physical register pointers or a fake register bank.

A precise compile-time rejection is acceptable only for configurations actually outside the documented supported contract, with evidence and a clear diagnostic. Do not relabel ClearCore as unsupported to obtain a green gate. Scope is the forced compile-time fallback and its real software-SPI selection; runtime Pin is reference evidence, not proof it fixes this class.

Acceptance criteria

  • Establish RED through the actual FASTLED_FORCE_SOFTWARE_PINS && FASTLED_NO_PINMAP FastPin and GenericSoftwareSPIOutput path, calling init then scalar/word, bytes, repeat and pixels with/without start bits. Use an instrumented real platform adapter or actual compiled target output. Prove the zero/uninitialized-port access and lack of native digitalWrite traffic before implementation; do not copy the writer into a model or rely on the missing-validpin compile error as the sole behavioral repro.
  • GREEN produces correct native digitalWrite pin/value operations, MSB data/setup/clock sequence, exact sample count and final logical LOW. Cover all256 values/all8 bit positions, zero/one/multiple byte lengths, pixel boundaries/start bits/modifiers, unrelated-pin activity and normal/inverted connector semantics. No dereference of null output/input pointer, zero-mask output, fake whole-port snapshot or unintended streaming eligibility is allowed.
  • Mutate the fixed production selection/forced pin path back to the baseline empty NoPinMap init plus pointer dereferences, leaving the focused test intact; the same gate must fail. Demonstrate restoration returns GREEN. A fixture that already bypasses the production forced pin class cannot establish this.
  • Compile and inspect actual ClearCore forced-software objects for IO0/IO1 and IO2/IO3 with and without FAST_SPI_INTERRUPTS_WRITE_PINS. Retain genuine normal-hardware pin validity assertions; adapt probe assertions only because the documented forced class lacks that hardware-only API. Never define FASTLED_ALL_PINS_VALID or substitute success metadata.
  • Expand real-core compile/codegen coverage to reachable forced common fallback variants: Renesas UNO R4, RP2040/RP2350, STM32, Apollo3 and CI13xx where supported Arduino builds exist, plus mapped AVR/SAMD control. Check each platform's forced dispatch: some headers retain hardware specializations despite the define, so their behavior must be classified honestly rather than assumed identical. Preserve platform native pinMode/mux setup and actual package pin validity boundaries.
  • Preserve conservative SPI timing and concurrency contracts. Portable digitalWrite calls can be slower; this is a correctness restoration, with no invented throughput gain. New latency credits, skipped padding or physical waveform/speed claims require measurement. Compilation/codegen and adapter traces do not establish electrical validation.

Decisions

Next steps

  1. Preserve hardware-only validity assertions while allowing actual forced-mode probe compilation; inspect resulting forced production output without accepting unsafe code as a pass.
  2. Establish forced NoPinMap RED, implement smallest real adapter-based fallback and SPI eligibility correction, then run GREEN and restored-baseline mutation.
  3. Validate actual target matrix, preserve normal mapped/hardware regressions and document any genuinely unsupported non-Arduino configuration explicitly.

Open questions

Which legacy FastPin full-port methods have external forced-mode callers needs an inventory before choosing safe semantics or explicit method-level rejection. Individual cores may ignore the force define via retained specializations; matrix coverage must trace actual selection. Metadata-only duplicate searches for FASTLED_FORCE_SOFTWARE_PINS and FASTLED_NO_PINMAP found no strong same-component fallback correctness issue.

Activity

  1. added
    sanitized
    on Nov 5, 2026
  2. zackees commented on Nov 7, 2026

    @zackees
    MemberAuthor

    #5137 fixes software SPI on forced no-pin-map pins, so this stays open. Still unmet: a gate that fails when the forced FastPin branch is reverted, ClearCore object inspection, interrupt-mode, STM32 and CI13XX forced builds, inverted connectors and unrelated-pin activity. Follow-ups: clockless #5136, Apollo3 #5138.

  3. zackees commented on Nov 22, 2026

    @zackees
    MemberAuthor

    Status before PR #5394 closes this.

    The defect is fixed on master by #5137 (118409a):

    • src/fl/system/fastpin_base.h:86-103: with forced software pins and no pin map, FastPin resolves to NoPinMapPin. The pointer-storing class (:104-163) is only used where a pin map exists, and its _init() now always sets the pointer.
    • src/fl/system/fastpin_no_pinmap.h:28-50: every write goes through fl::digitalWrite. port() returns nullptr and nothing dereferences it.
    • src/platforms/shared/spi_bitbang/generic_software_spi.h:72-75: pins without a pin map always take the per-pin write path.
    • Callers that bypass hi()/lo() (fix(spi-bitbang): platform policies bypass hi()/lo() on FastPin subclasses that override them #5122): platform BitbangPin register paths are guarded against these pins. Clockless drivers that write port() directly refuse to compile with forced pins: STM32, Renesas, LPC, LPC PLU, D51, the ESP8266 block driver and AVR.

    Gate (PR #5394): tests/fl/system/fastpin_forced_no_pinmap.cpp runs GenericSoftwareSPIOutput (init, writeByte, writeBytes, writeBytesValueRaw, writePixels) on the FastPin that fastpin_base.h actually selects. It checks MSB-first data, the bit count, and that the clock ends LOW. With the pre-#5137 header restored it crashes with SIGSEGV in init; with the fix it passes, including under --debug sanitizers.

    Not covered, accepted as a limitation: the ClearCore object inspection and the forced-mode compile matrix on the other boards. Both need real board builds. The host-side gate exercises the selected production class on the path that used to dereference null. A regression in an individual platform's register path is covered by the compile-time refusals above, not by this test.

  4. moved this from Triage to Done in FastLED Trackeron Nov 22, 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

    sanitized

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions