thread_local not supported; unsafe to use c++ exceptions in multi-core setting #291
Labels
No labels
bazel
bluetooth
board settings
boot_stage2
bug
build
c++
can't reproduce
clang
clib-interface
cmsis
code cleanliness
code size
compiler version issues
cyw43_driver
debug
discussion
documentation
duplicate
dynamic_clock
elf2uf2
enhancement
errata
filesystem
flash layout
floating point
FreeRTOS
hardware_adc
hardware_claim
hardware_clocks
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
hardware_irq
hardware_pio
hardware_pwm
hardware_rcp
hardware_rtc
hardware_spi
hardware_sync
hardware_uart
hardware_vreg
hardware_watchdog
hardware_xosc
help wanted
host
IAR
installation
invalid
linker scripts
lto
lwip
mbedtls
missing API
needs reporter detail
networking
newlib
newlib-multicore
non-c
non-cmake
non-gcc
openocd
orphan PR
OS integration
pico_async_context
pico_base
pico_binary_info
pico_flash
picolibc
pico_lwip
pico_malloc
pico_multicore
pico_printf
pico_runtime
pico_stdio
pico_stdio_usb
pico_sync
pico_time
pico_util
pioasm
port
question
review
review2
rtos interop
runtime init
static library
svd
test
tinyusb
vscode
wifi integration
wontfix
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk#291
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
pico-sdk doesn't support thread-local storage (thread_local keyword), which is required for c++ exceptions to function properly in a multi-core environment. Internally libstdc++ stores some global exception state in a thread_local variable (
static __thread abi::__cxa_eh_globals global;in eh_globals.cc).If now both RP2040 cores throw or catch exceptions at the same time they are "mixed", because the thread_local state variable is the exact same variable on both cores. There are also no synchronization locks, so both cores can write to the same variable at the same time.
The code below shows how core0's std::current_exception() is overwritten by core1. The expected output is:
The actual output is:
Wrong analysis:
static __thread abi::__cxa_eh_globals globalis only used when_GLIBCXX_HAVE_TLSis defined. In our case the "normal"static __cxa_eh_globals eh_globalsis actually used (by both cores), so obviously a c++ exception on one core then overwrites an active exception on the other core.By the way, adding thread-local storage support is relatively easy: provide a custom implementation of
__emutls_get_addressand then use-Wl,--wrap=to use it. Thenthread_localworks just fine, but as stated that doesn't solve the issue.A solution could be to wrap the functions
__cxa_get_globalsand__cxa_get_globals_fast. One potential problem: the pointer they return is to a libstdc++-internal structure (__cxa_eh_globals), so different GCC versions can have different definitions for it.I have a proof of principle for gcc-arm-none-eabi-10-2020-q4-major at:
https://github.com/geurtv/pico-sdk/tree/develop
file:
https://github.com/geurtv/pico-sdk/blob/develop/src/rp2_common/pico_standard_link/wrap_eh_globals.cpp
commit:
github.com/geurtv/pico-sdk@f869eb6181yeah, adding __thread support makes sense (but then reentrant support for lib functions makes sense which probably suggests we should build newlib ourselves with different options compared to the one shipped with gcc-arm-eabi-none)
is __cxa_get_globals in newlib or gcc libs? in any case, yes it would make sense for C++ exceptions to work on both cores (you are the first to notice that they don't... although in fairness we have them turned off by default).
It's all gcc as far as I can tell.
__cxa_get_globalsand__cxa_get_globals_fastare both in libsup++,__emutls_get_address(for thread-local storage) is in libgcc.FYI: I've also just added a wrapper for
__emutls_get_address. Not sure how complete the implementation is, but thread-local storage seems to work now.https://github.com/geurtv/pico-sdk/blob/develop/src/rp2_common/pico_standard_link/wrap_emutls.c
Would be good if this integrated well with an RTOS so that similar problems don't happen when there are multiple tasks running on one core. For example, FreeRTOS has vTaskSetThreadLocalStoragePointer() and pvTaskGetThreadLocalStoragePointer().
note it is worth rechecking this as thread local support was added via #2910
Nope,
__threadandthread_localwork correctly, but exceptions still don't. Note that gcc libs are built without thread local support (_GLIBCXX_HAVE_TLSnot defined), so the internal variable active exceptions are stored in (probably stillstatic __cxa_eh_globals eh_globals) is still the same single instance for both cores.Tested with Arm GNU Toolchain Version 15.2.Rel1.
moving exception part to 2.3.0
we are now building our own RISC-V GCC toolchain, so maybe we can look at building a thread-safe version for both Arm and GCC
Note, that is a bit annoying though; in other places __thread has been used anyway, and just defaulted to a single instance when not using our TLS support