Skip to content

fl::unordered_map::rehash_internal puts ~1 KB of scratch on the stack; overflows ESP8266's 4 KB loop stack #4926

Closed

Description

@zackees

Summary

fl::unordered_map::rehash_internal() (src/fl/stl/unordered_map.h) declares two stack locals:

  • fl::vector_inlined<Entry, INLINED_COUNT> old, which reserves room for 8 whole Entry values inline.
  • fl::bitset<1024> old_occupied, which is 128 bytes.

For large values such as fl::Rpc's RpcEntry, that is about 1 KB of temporary stack on each grow.

Impact

ESP8266 runs setup()/loop() on a 4 KB stack. In the AutoResearch low-memory surface, binding the 23rd RPC method grows the registry from 32 to 64 buckets. That overflowed the stack, and the board boot-looped with Stack smashing detected from cont_check.

Evidence

Measured on an ESP8266EX through the resetCause RPC, cont_stack_free:

  • With 22 binds: 368 bytes free.
  • With one more bind: boot loop.
  • After moving the scratch to the heap: 1,568 bytes free with all binds in place.

Fix

Gate on the platform memory tier:

  • Where FL_PLATFORM_HAS_LARGE_MEMORY is set, keep the scratch on the stack. There is no heap round trip per rehash.
  • Otherwise (ESP8266, AVR, Teensy LC/3.x, STM32F1, …), allocate the scratch on the heap.

Found during the ESP8266 HSPI APA102 work (#4925).

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