From 54f257b9091600e7b2e855541da91c71d8971fb1 Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Wed, 12 Feb 2025 10:27:27 +0800 Subject: [PATCH] fix(freertos): fix a bug in `prvTaskDeleteWithCaps` related to coprocessors When a coprocessor is used, the stack pointer is altered. It must be restored before freeing the memory allocated to the task. --- components/freertos/esp_additions/idf_additions.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/components/freertos/esp_additions/idf_additions.c b/components/freertos/esp_additions/idf_additions.c index 0fc655194d..c320e39638 100644 --- a/components/freertos/esp_additions/idf_additions.c +++ b/components/freertos/esp_additions/idf_additions.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -100,15 +100,18 @@ err: configASSERT( eRunning != eTaskGetState( xTaskToDelete ) ); + /* We can delete the task and free the memory buffers. + * First, we must call `vTaskDelete` so that the port task delete callback is called. + * On targets that have coprocessors, it may be possible that the stack pointer is modified (restored) + * during this phase, hence, it must be done before getting the statuc buffers out of the task. */ + vTaskDelete( xTaskToDelete ); + + /* Free the memory buffers */ xResult = xTaskGetStaticBuffers( xTaskToDelete, &puxStackBuffer, &pxTaskBuffer ); configASSERT( xResult == pdTRUE ); configASSERT( puxStackBuffer != NULL ); configASSERT( pxTaskBuffer != NULL ); - /* We can delete the task and free the memory buffers. */ - vTaskDelete( xTaskToDelete ); - - /* Free the memory buffers */ heap_caps_free( puxStackBuffer ); vPortFree( pxTaskBuffer ); }