Skip to content

Commit 11f6add

Browse files
Felipe Nevesespressif-bot
authored andcommitted
expression_with_stack: added a tweak on TCB stackpointers to avoid false trigger of stack overflow
1 parent a700035 commit 11f6add

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

components/newlib/test/test_shared_stack_printf.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,38 @@ void external_stack_function(void)
2121
void another_external_stack_function(void)
2222
{
2323
//We can even use Freertos resources inside of this context.
24-
printf("We can even use FreeRTOS resources delaying..., sp=%p\n", get_sp());
25-
vTaskDelay(100);
26-
printf("Done!, sp=%p\n", get_sp());
24+
printf("We can even use FreeRTOS resources... yielding, sp=%p\n", get_sp());
25+
taskYIELD();
2726
shared_stack_sp = (StackType_t *)get_sp();
2827
}
2928

3029
TEST_CASE("test printf using shared buffer stack", "[newlib]")
3130
{
3231
portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE);
3332

34-
TEST_ASSERT(shared_stack != NULL);
33+
TEST_ASSERT_NOT_NULL(shared_stack);
3534

3635
SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
3736
TEST_ASSERT_NOT_NULL(printf_lock);
38-
printf("SP: %p\n", get_sp());
37+
printf("current task sp: %p\n", get_sp());
3938
printf("shared_stack: %p\n", (void *)shared_stack);
39+
printf("shared_stack expected top: %p\n", (void *)(shared_stack + SHARED_STACK_SIZE));
40+
4041

4142
esp_execute_shared_stack_function(printf_lock,
4243
shared_stack,
4344
SHARED_STACK_SIZE,
4445
external_stack_function);
4546

46-
TEST_ASSERT(((shared_stack_sp >= shared_stack_sp) &&
47+
TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
4748
(shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
4849

4950
esp_execute_shared_stack_function(printf_lock,
5051
shared_stack,
5152
SHARED_STACK_SIZE,
5253
another_external_stack_function);
5354

54-
TEST_ASSERT(((shared_stack_sp >= shared_stack_sp) &&
55+
TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
5556
(shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
5657

5758
vSemaphoreDelete(printf_lock);

components/xtensa/expression_with_stack_xtensa.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
#include <freertos/xtensa_rtos.h>
1717
#include <freertos/xtensa_context.h>
1818
#include <setjmp.h>
19+
#include <string.h>
1920

20-
StackType_t *shared_stack;
21-
shared_stack_function shared_stack_callback;
22-
jmp_buf shared_stack_env;
23-
bool shared_stack_function_done = false;
24-
portMUX_TYPE shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
21+
StackType_t *xtensa_shared_stack;
22+
shared_stack_function xtensa_shared_stack_callback;
23+
jmp_buf xtensa_shared_stack_env;
24+
bool xtensa_shared_stack_function_done = false;
25+
static portMUX_TYPE xtensa_shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
26+
static void *current_task_stack = NULL;
2527

2628
extern void esp_shared_stack_invoke_function(void);
2729

@@ -31,6 +33,15 @@ static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
3133
esp_clear_watchpoint(1);
3234
uint32_t watchpoint_place = ((uint32_t)stack + 32) & ~0x1f ;
3335
#endif
36+
//We need also to tweak current task stackpointer to avoid erroneous
37+
//stack overflow indication, so fills the stack with freertos known pattern:
38+
memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
39+
40+
StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
41+
//Then put the fake stack inside of TCB:
42+
current_task_stack = current->pxDummy6;
43+
current->pxDummy6 = (void *)stack;
44+
3445
StackType_t *top_of_stack = stack + stack_size;
3546

3647
//Align stack to a 16byte boundary, as required by CPU specific:
@@ -40,7 +51,7 @@ static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
4051
esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE);
4152
#endif
4253

43-
shared_stack = top_of_stack;
54+
xtensa_shared_stack = top_of_stack;
4455
}
4556

4657

@@ -52,21 +63,24 @@ void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size
5263
assert(function);
5364

5465
xSemaphoreTake(lock, portMAX_DELAY);
55-
portENTER_CRITICAL(&shared_stack_spinlock);
56-
shared_stack_function_done = false;
66+
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
67+
xtensa_shared_stack_function_done = false;
5768
esp_switch_stack_setup(stack, stack_size);
58-
shared_stack_callback = function;
59-
portEXIT_CRITICAL(&shared_stack_spinlock);
69+
xtensa_shared_stack_callback = function;
70+
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
6071

61-
setjmp(shared_stack_env);
62-
if(!shared_stack_function_done) {
72+
setjmp(xtensa_shared_stack_env);
73+
if(!xtensa_shared_stack_function_done) {
6374
esp_shared_stack_invoke_function();
6475
}
6576

66-
portENTER_CRITICAL(&shared_stack_spinlock);
77+
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
6778
StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
79+
80+
//Restore current task stack:
81+
current->pxDummy6 = (StackType_t *)current_task_stack;
6882
vPortSetStackWatchpoint(current->pxDummy6);
69-
portEXIT_CRITICAL(&shared_stack_spinlock);
83+
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
7084

7185
xSemaphoreGive(lock);
7286
}

components/xtensa/expression_with_stack_xtensa_asm.S

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
#include <freertos/xtensa_context.h>
1616

17-
.extern shared_stack
18-
.extern shared_stack_callback
19-
.extern shared_stack_function_done
17+
.extern xtensa_shared_stack
18+
.extern xtensa_shared_stack_callback
19+
.extern xtensa_shared_stack_function_done
20+
.extern xtensa_shared_stack_env
2021
.extern longjmp
2122
.text
2223

@@ -29,16 +30,18 @@
2930
esp_shared_stack_invoke_function:
3031

3132
#ifndef __XTENSA_CALL0_ABI__
32-
movi a0, 0 /* no need to rotate window, it will be destroyed anyway */
33-
movi a6, shared_stack
33+
movi a0, 0 /* must not rotate the window here, */
34+
/* the state of execution for shared stack */
35+
/* functions will be completely destroyed at end */
36+
movi a6, xtensa_shared_stack
3437
l32i sp, a6, 0 /* load shared stack pointer */
35-
movi a12, shared_stack_callback
38+
movi a12, xtensa_shared_stack_callback
3639
l32i a12, a12, 0
3740
callx4 a12 /* call user function */
38-
movi a6, shared_stack_function_done
41+
movi a6, xtensa_shared_stack_function_done
3942
movi a7, 1
4043
s32i a7, a6, 0 /* hint the function was finished */
41-
movi a6, shared_stack_env
44+
movi a6, xtensa_shared_stack_env
4245
movi a7, 0
4346
movi a12, longjmp
4447
callx4 a12 /* jump to last clean state previously saved */

0 commit comments

Comments
 (0)