Skip to content

Colour pipeline can be freed while an encode or power estimate is using it; hold it by shared_ptr (compiled out on tiny-memory targets) #4440

Closed

Description

@zackees

Problem

A channel's colour pipeline can be freed while something is still using it.

fl::Channel owns its derived StreamingPipelineQ16 as a fl::unique_ptr (Channel::ColorPipelineStorage, src/fl/channels/channel.h). Everything that reads it takes a raw pointer:

user where how long it holds the pointer
the encode Channel::showPixels hands mPipeline.get() to ReorderingPixelIteratorAny / ColorManagedPixelSource the whole frame encode
the flux stage showPixels → ColorPipelineHooks::setFlux(mPipeline.get(), …) one call
the power estimate (#4344, PR #4438) CLEDController::colorPipeline() returns const StreamingPipelineQ16* to controller_unscaled_power_mW() one full-strip pass

Channel::reconcileColorProfile() starts with mPipeline.reset(), and it runs whenever the channel is reconfigured:

  • applyConfig
  • setColorProfile / clearColorProfile via a new config

If that happens while an encode or an estimate is in flight (another task, a driver callback, or an async show), the reader is left holding a dangling pointer: a use-after-free. With a single owner there is no way for a reader to keep the pipeline alive.

Fix

  1. Channel::ColorPipelineStorage becomes fl::shared_ptr<StreamingPipelineQ16>, built with fl::make_shared.

    • reconcileColorProfile() then only drops the channel's own reference.
    • A reader that took one keeps a valid pipeline until it finishes.
    • The existing test assertion sizeof(ColorPipelineStorage) <= 2 * sizeof(void*) still holds: a shared pointer is a pointer and a control-block pointer.
  2. Every reader takes a reference for the duration of its use:

    • showPixels copies mPipeline into a local shared_ptr before setting flux and building the iterator, and keeps it until the encode returns.
    • The power estimate gets a shared_ptr from the controller accessor, not a raw pointer.
  3. Small-memory devices pay nothing. All of this sits under FL_COLOR_PROFILE_RUNTIME, which is !FL_PLATFORM_HAS_TINY_MEMORY. On those targets (AVR and other tiny-memory parts) the following are all compiled out:

    • the storage
    • the per-frame reference
    • the colorPipeline() virtual (no vtable slot)
    • the managed branch of the power estimate

    There is no RAM and no flash cost. PR feat(power): charge a managed channel's solved drives; hold the pipeline by shared_ptr (#4344, #4440) #4438's current accessor adds 10 B flash / 4 B RAM on uno; with this change the uno build must match master exactly.

Acceptance

  • A test proves the lifetime: take a reader's reference, reconfigure or clear the channel, and the reader's pipeline is still valid and unchanged (checked under ASAN via bash test <name> --debug). The channel's own pipeline is gone or replaced.
  • uno Blink flash and RAM are byte-identical to master.
  • The ESP32-S3 bloat gate moves only by the documented amount.
  • bash test --cpp and bash lint pass.

Part of #4034 (colour pipeline program); touches P6 (#4040) and #4344.

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