cyw43_arch_deinit() corrupts FreeRTOS idle task's termination list under static allocation, causing hard fault in uxListRemove() #3174
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#3174
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?
Title
cyw43_arch_deinit()corrupts FreeRTOS idle task's termination list under static allocation, causing hard fault inuxListRemove()Environment
98a542c1a6)configSUPPORT_STATIC_ALLOCATION= 1configSUPPORT_DYNAMIC_ALLOCATION= 1configNUMBER_OF_CORES= 1 (single core)cyw43_arch_freertos(PICO_CYW43_ARCH_FREERTOS)Summary
Calling
cyw43_arch_init()thencyw43_arch_deinit()repeatedly from a FreeRTOS task (e.g. to power-cycle Wi-Fi between HTTP posts) reliably crashes FreeRTOS with a hard fault insideuxListRemove(), called from the idle task'sprvCheckTasksWaitingTermination().Crash trace
Root cause
With
configSUPPORT_STATIC_ALLOCATION,cyw43_arch_freertos.ccreates the async-context worker task withxTaskCreateStatic(), backed byStaticTask_t task_buf, a field embedded directly inside the staticcyw43_async_context_freertosstruct (async_context_freertos_t). Thistask_bufmemory is the live FreeRTOS TCB for that task, not a separate copy.In
async_context_freertos_deinit()(src/rp2_common/pico_async_context/async_context_freertos.c):Sequence of events:
end_task_funcsetstask_should_exit; the worker task (async_context_task) exits its loop, callsxSemaphoreGive(self->task_complete_sem), then callsvTaskDelete(NULL)on itself and yields.vTaskDelete(NULL)does not immediately free/unlink the TCB — it moves it onto FreeRTOS'sxTasksWaitingTerminationlist. Only the idle task, viaprvCheckTasksWaitingTermination()→uxListRemove(), actually unlinks it later.async_context_freertos_deinit()(running in the caller task, which resumed oncetask_complete_semwas given) proceeds tomemset(self, 0, sizeof(*self)). This zeroestask_buf, i.e. it wipes the TCB while it is still linked intoxTasksWaitingTermination, corrupting the list's embeddedpxNext/pxPreviouspointers.prvCheckTasksWaitingTermination(),uxListRemove()dereferences the corrupted pointers and hard-faults.This is deterministic (not just a rare race) whenever the caller task's priority is higher than the idle task's — which is virtually always the case, since idle runs at
tskIDLE_PRIORITY(0) and never gets scheduled between steps 1 and 3 to reap the TCB first. The defaultcyw43_archworker task priority (ASYNC_CONTEXT_DEFAULT_FREERTOS_TASK_PRIORITY=tskIDLE_PRIORITY + 4) being higher than the app's calling task doesn't prevent this, since the corruption is caused by the caller task racing the idle task, not by the worker task itself.This is closely related to, but distinct from, #2687 (which fixed the analogous issue for the statically-allocated FreeRTOS timer via
xTimerPendFunctionCallsynchronization) — the same class of bug still exists for the statically-allocated task TCB, which has no equivalent "wait until actually reaped" synchronization.Reproduction
Minimal repro: in a FreeRTOS task, loop:
Crash typically occurs within the first 1-2 iterations, inside the idle task, some time after
cyw43_arch_deinit()returns.Suggested fix
In
async_context_freertos_deinit(), after takingtask_complete_sem, give the idle task a scheduling opportunity to reap the just-deleted task before zeroing the struct that backs its static TCB:A more robust fix would avoid the fixed delay and instead poll/wait until
uxTaskGetNumberOfTasks()(or equivalent) confirms the task has actually been reaped, but the above resolves the crash in practice.Files involved
src/rp2_common/pico_async_context/async_context_freertos.c(async_context_freertos_deinit)src/rp2_common/pico_cyw43_arch/cyw43_arch_freertos.c(statictask_bufallocation viacyw43_async_context_freertos)