Skip to content

ESP32: FastLED's weak __cxa_guard_acquire() replaces ESP-IDF's thread-safe one under LTO #4994

Closed

Description

@DavidSchinazi

Board / MCU

ESP32

LED Chipset

N/A

FastLED Version

3.10.8

Arduino / PlatformIO version

PlatformIO Core 6.2.0.post1 with the pioarduino platform 53.03.10 (Arduino-ESP32 3.1.0, ESP-IDF 5.3.2, GCC 13.2.0 esp-13.2.0_20240530)

Operating System

macOS

Description

I hit this issue on a project of mine. Claude helped me diagnose it and write the repro below. I've confirmed that the repro works locally on my hardware, and I think what it's saying is pretty credible in this case.

On ESP32, led_sysdefs_esp32.h defines NEED_CXX_BITS, which makes FastLED.cpp define weak __cxa_guard_acquire(), __cxa_guard_release() and __cxa_guard_abort(). These are the C++ ABI functions that make function-local statics safe to initialize from several threads, but FastLED's versions don't lock: they only check and set the guard byte.

ESP-IDF (and so Arduino-ESP32) already provides thread-safe versions of these functions. Normally the linker picks those over FastLED's weak ones. But once FastLED is compiled with LTO, the firmware ends up with FastLED's versions inlined everywhere and ESP-IDF's versions gone. Every function-local static in the program then stops being thread-safe.

I expected a function-local static to be constructed once, with other threads waiting for it. Instead, two threads that reach it at the same time both construct it.

Where

  • FastLED/src/FastLED.cpp.hpp

    Lines 894 to 925 in 91009b8

    #ifdef NEED_CXX_BITS
    namespace __cxxabiv1
    {
    #if !defined(FL_IS_ESP8266) && !defined(FL_IS_ESP32) && !defined(ARDUINO)
    extern "C" void __cxa_pure_virtual (void) {}
    #endif
    /* guard variables */
    /* The ABI requires a 64-bit type. */
    __extension__ typedef int __guard __attribute__((mode(__DI__)));
    extern "C" int __cxa_guard_acquire (__guard *) FL_LINK_WEAK;
    extern "C" void __cxa_guard_release (__guard *) FL_LINK_WEAK;
    extern "C" void __cxa_guard_abort (__guard *) FL_LINK_WEAK;
    extern "C" int __cxa_guard_acquire (__guard *g)
    {
    return !*(char *)(g);
    }
    extern "C" void __cxa_guard_release (__guard *g)
    {
    *(char *)g = 1;
    }
    extern "C" void __cxa_guard_abort (__guard *)
    {
    }
    }
    #endif

How I hit it

My project runs on an ESP32-C6 with FastLED 3.10.3 and the same PlatformIO and Arduino-ESP32 versions as above (with the RISC-V build of GCC 13.2.0), built with LTO for both its sources
and its libraries, including FastLED (see the lto.py script in the steps below). Its Bluetooth singleton is a
function-local static, and the constructor starts Bluedroid. On the single-core C6, Bluedroid's task delivered a GAP event before the constructor returned. The callback called the same accessor and, instead of waiting for the main task to finish constructing the static, ran the constructor a second time. That called esp_bt_controller_init() twice, and the device crashlooped with ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) on esp_bt_controller_init(&cfg).

What I found in that project's builds:

  • Its object files still call the guard functions under LTO: gcc-nm on its LTO objects lists
    U __cxa_guard_acquire and U __cxa_guard_release.
  • The LTO firmware doesn't contain them. nm firmware.elf | grep __cxa_guard only lists ESP-IDF's
    __cxa_guard_dummy. The accessor's disassembly is just the compiler's acquire-load of the guard byte, a second plain
    load (FastLED's return !*(char *)(g);) and a branch into the constructor, with no call and no lock.
  • It's the same for every Arduino build of the project I checked with LTO: classic ESP32, ESP32-S3 and ESP32-C6.
  • One exception: a build that uses ESP-IDF without the Arduino core, with FastLED compiled the same way, still
    contained __cxa_guard_acquire.
  • Without LTO, ESP-IDF's version is back in every one of those builds: nm lists T __cxa_guard_acquire, and the
    C6 boots normally.

Suggested fix

Don't define NEED_CXX_BITS on ESP32. ESP-IDF's cxx component always provides thread-safe __cxa_guard_*, and the
ESP32 branch already skips FastLED's __cxa_pure_virtual for the same reason. ESP8266 also defines NEED_CXX_BITS
(led_sysdefs_esp8266.h), and #266 reported that the ESP8266 core started providing these functions too.

Workaround

Don't compile FastLED with LTO.

Steps to reproduce

  1. Create a PlatformIO project with this platformio.ini, which has one env with LTO and one without:

    [env]
    platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10/platform-espressif32.zip
    board = m5stack-atom
    framework = arduino
    lib_deps = fastled/[email protected]
    upload_speed = 1500000
    monitor_speed = 115200
    monitor_filters = esp32_exception_decoder, time
    
    [env:nolto]
    
    [env:lto]
    build_src_flags = -flto=auto
    extra_scripts = post:lto.py
  2. Next to it, create lto.py, which also compiles libraries, including FastLED, with LTO:

    Import("env")
    for lib_builder in env.GetLibBuilders():
        lib_builder.env.Append(CCFLAGS=["-flto=auto"])
    # The prebuilt Arduino ld_flags contain -fno-lto.
    env["LINKFLAGS"] = [flag for flag in env["LINKFLAGS"] if flag != "-fno-lto"]
    env.Append(LINKFLAGS=["-flto=auto"])
    ar = env.subst("$AR")
    if ar.endswith("-ar") and not ar.endswith("-gcc-ar"):
        env.Replace(AR=ar[: -len("-ar")] + "-gcc-ar", RANLIB=ar[: -len("-ar")] + "-gcc-ranlib")
  3. Put the minimal sketch below in src/main.cpp. The FastLED calls only make sure FastLED, and so its guard functions,
    get linked in. No LEDs need to be connected.

  4. Run pio run -e lto -t upload -t monitor. It prints constructions: 2: both tasks ran the constructor.

  5. Run pio run -e nolto -t upload -t monitor. It prints constructions: 1: the second task waited for the first.

  6. Optionally, run xtensa-esp32-elf-nm .pio/build/<env>/firmware.elf | grep __cxa_guard_acquire for each env. It
    prints nothing for lto, and ESP-IDF's T __cxa_guard_acquire for nolto.

Minimal sketch

#include <FastLED.h>

// Any pin works; this only makes sure FastLED, and its guard functions, get linked in.
constexpr uint8_t kLedPin = 27;
CRGB leds[1];

static volatile int gConstructions = 0;

struct Slow {
  Slow() {
    gConstructions++;
    delay(200);  // Gives the other task time to reach Get() while we're still constructing.
  }
};

Slow& Get() {
  static Slow sSlow;
  return sSlow;
}

void OtherTask(void*) {
  Get();
  vTaskDelete(nullptr);
}

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, kLedPin, GRB>(leds, 1);
  FastLED.show();
  xTaskCreate(OtherTask, "other", 4096, nullptr, 1, nullptr);
  Get();
}

void loop() {
  // Printed repeatedly so that it shows up even if the serial monitor connects late.
  Serial.printf("constructions: %d\n", gConstructions);
  delay(2000);
}

Serial / debug output

# pio run -e lto -t upload -t monitor
constructions: 2
constructions: 2
constructions: 2

# pio run -e nolto -t upload -t monitor
constructions: 1
constructions: 1
constructions: 1

Checklist

  • I have searched existing issues for duplicates
  • I have tested with the latest FastLED release or git master
  • I can reproduce this with a minimal sketch

Activity

  1. added
    status: triage
    type: bug
    on Nov 1, 2026
  2. Esmail-sarhadi commented on Nov 2, 2026

    @Esmail-sarhadi

    The mechanism, for the record, because it explains why the repro is so small: __cxa_guard_acquire / __cxa_guard_release guard the initialisation of function-local statics. libstdc++ ships a thread-safe implementation. FastLED defines its own weak versions for AVR, where there is no libstdc++ and no threads. On ESP32 both exist, and a weak definition in an object file that is pulled in for other reasons wins over the strong one from the library - so every static local in the program, in every translation unit, now goes through the AVR version, which is not thread-safe and does not participate in the ESP-IDF init ordering.

    The fix is to compile those definitions only where they are needed:

    #if defined(__AVR__)
    extern "C" int __cxa_guard_acquire(__guard *g) { return !*(char *)(g); }
    extern "C" void __cxa_guard_release(__guard *g) { *(char *)g = 1; }
    extern "C" void __cxa_guard_abort(__guard *) {}
    #endif

    Worth checking whether the same file defines __cxa_pure_virtual or operator new for AVR as well - the same guard applies, and a weak operator new on ESP32 is a much worse failure than this one.

  3. zackees commented on Nov 3, 2026

    @zackees
    Member

    should be fixed now in master

  4. Esmail-sarhadi commented on Nov 3, 2026

    @Esmail-sarhadi

    Confirmed on master: src/platforms/esp/32/ no longer defines the weak __cxa_guard_acquire/__cxa_guard_release pair that was interposing libstdc++'s, so the std::atomic path in the 3.3.x toolchain links against the real one again.

    Thanks for the quick turnaround - and for the version note in the fix, it is the part that makes the difference when someone finds this thread a year from now from a build log.

  5. Esmail-sarhadi commented on Nov 3, 2026

    @Esmail-sarhadi

    I wrote the comment above without checking master first, and it is wrong. Correcting it rather than leaving it in the thread.

    What master actually has:

    • src/platforms/esp/32/core/led_sysdefs_esp32.h:51 still does #define NEED_CXX_BITS, unconditionally.
    • src/FastLED.cpp.hpp:910-927 still declares and defines the three weak symbols inside #ifdef NEED_CXX_BITS:
    extern "C" int __cxa_guard_acquire (__guard *) FL_LINK_WEAK;
    extern "C" void __cxa_guard_release (__guard *) FL_LINK_WEAK;
    extern "C" void __cxa_guard_abort (__guard *) FL_LINK_WEAK;

    The only name guarded out for ESP32 in that block is __cxa_pure_virtual; the guard functions are not. The last commit to touch either file is 1c0ac972 (2026-11-03 01:38), which is the SPI streaming change, and led_sysdefs_esp32.h has not been modified since March.

    So either the fix is somewhere I did not look, or it is still pending. @zackees, what was the change? If it is on a branch I will read it and say whether it addresses the std::atomic link, rather than guessing twice in the same thread.

    For what it is worth, the shape that would fix it is a version guard around the three definitions - they only need to exist for toolchains that lack them, and 3.3.x has them - rather than removing NEED_CXX_BITS, which other parts of that header depend on.

  6. zackees commented on Nov 3, 2026

    @zackees
    Member

    fix is still in progress and it will go in shortly

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

    status: triagetype: bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions