From e4fb50e6f3f5dff93dd46cad687f8f0877192c13 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Wed, 4 Dec 2019 11:25:02 -0300 Subject: [PATCH] expression_with_stack_xtensa: fixed stack pointer to avoid its overflow on heap block metadata. --- .../newlib/test/test_shared_stack_printf.c | 17 +++++++---------- .../xtensa/expression_with_stack_xtensa.c | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/components/newlib/test/test_shared_stack_printf.c b/components/newlib/test/test_shared_stack_printf.c index b99959f71..5daa248a2 100644 --- a/components/newlib/test/test_shared_stack_printf.c +++ b/components/newlib/test/test_shared_stack_printf.c @@ -8,25 +8,22 @@ #include "esp_expression_with_stack.h" //makes sure this is not the task stack... -void check_stack(portSTACK_TYPE *sp, portSTACK_TYPE *base_sp) +void another_external_stack_function(void) { - StaticTask_t *hacked_task = (StaticTask_t *)xTaskGetCurrentTaskHandle(); - portSTACK_TYPE *task_sp = (portSTACK_TYPE *)hacked_task->pxDummy1; - TEST_ASSERT((intptr_t)task_sp < (intptr_t)base_sp || - (intptr_t)task_sp >= (intptr_t)sp); + //We can even use Freertos resources inside of this context. + vTaskDelay(100); + printf("Executing this another printf inside a function with external stack"); } TEST_CASE("test printf using shared buffer stack", "[newlib]") { portSTACK_TYPE *shared_stack = malloc(8192 * sizeof(portSTACK_TYPE)); - portSTACK_TYPE *ext_stack_top = (portSTACK_TYPE *)&shared_stack[0] + - ((sizeof(8192 * sizeof(portSTACK_TYPE))) / - sizeof(portSTACK_TYPE)); TEST_ASSERT(shared_stack != NULL); SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex(); - ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock, shared_stack,8192,printf("Executing printf from external stack! \n")); - ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock, shared_stack,8192,check_stack(ext_stack_top, shared_stack)); + ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock, shared_stack,8192,printf("Executing this printf from external stack! \n")); + ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock, shared_stack,8192,another_external_stack_function()); + vSemaphoreDelete(printf_lock); free(shared_stack); } diff --git a/components/xtensa/expression_with_stack_xtensa.c b/components/xtensa/expression_with_stack_xtensa.c index c24470971..310d92448 100644 --- a/components/xtensa/expression_with_stack_xtensa.c +++ b/components/xtensa/expression_with_stack_xtensa.c @@ -10,7 +10,7 @@ StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size) sizeof(StackType_t)); //Align stack to a 16byte boundary, as required by CPU specific: - top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 1) - + top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 31) - ALIGNUP(0x10, sizeof(XtSolFrame) )) & ~0xf);