Stack overflow

sasikalaswathi wrote on Tuesday, November 26, 2019:

Wanted to test the vApplicationStackOverflowHook gets called after enabled the stack overflow options as 1 or 2. Could you give me the sample code to test this purpose? Roughly I set the low stack limit to particualr task (recursive function getting called inside the task), but no vApplicationStackOverflowHook function called (only HardFault_Handler gets called).
Actually the vApplicationStackOverflowHook function gets automatically called whenever it detects the stack overflow?

xz8987f wrote on Tuesday, November 26, 2019:

The stack overflow check is only performed at task context switch time. So you would need to overflow the stack area and then do a context switch (e.g. interrupted by a higher priority task).

If you want to have a check for each function call, then have a look at using stack canaries: https://mcuoneclipse.com/2019/09/28/stack-canaries-with-gcc-checking-for-stack-overflow-at-runtime/

I hope this helps,
Erich

sasikalaswathi wrote on Tuesday, November 26, 2019:

Hi Erich,
Thanks for the reply.

I read the stack canaries. But my purpose was to check the vApplicationStackOverflowHook function called whenever the stack overflow happens. While I am trying, this function didn’t get called after I chosse the options as 2. I don’t why? that’s why asked for the sample code to check the stack overflow.

richard_damon wrote on Tuesday, November 26, 2019:

As Erich says, vApplicationStackOverflowHook only gets called when the system detects that task being switched out has overflowed its stack. This is not a foolproof system, as the act of overflowing can cause system corruption and system issues before the system gets a chance to detect the overflow. Typically, it will handle mild overflow which otherwise would lead to subtle problems later, things like one task overwritting part of another tasks stack.

If you really want to catch overflows preventively, you would need to do something to check BEFORE you created the overflow, which would require help from the compiler or you manually adding code throughout your program, perhaps calling uxTaskGetStackHighWaterMark() at appropriate times.

The other alternative is to run your task as a restricted task, at which point you can memory protect the memory outside your stack, to get an access violation when it occurs.

rtel wrote on Tuesday, November 26, 2019:

The MPU version of FreeRTOS uses a memory protection region for the stack, so will catch overflows before they happen. There are also a few chips that have will do this for you - from memory the PIC24 and Cortex-M33. All these cases rely on hardware to catch a write outside of the stack region.

sasikalaswathi wrote on Thursday, November 28, 2019:

Hi Richard,

Thanks for the quick detailed response.

For a function say Task1, the function vApplicationStackOverflowHook called whenever stack overflowed. But for some times for the same function Task1, HardFault_Handler gets called. I don’t why two different output I got? Could you help me with that?

Attached the code for reference.

TaskHandle_t task1,task2,task3;
int main()
{
xTaskCreate( vTaskFunction2, “Task 2”, 60, NULL, 1,&task2);
vTaskStartScheduler();
}
void vTaskFunction2(void p)
{
int i=0;
int c=0;
UBaseType_t uxHighWaterMark;
uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
while(1)
{
for(i=10000;i<20000;i++)
{
uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
printf(“stack:%lu\n”,uxHighWaterMark);
c=i+1;
c+=(c
100+1000);
c=c+110;
}
}

}

rtel wrote on Thursday, November 28, 2019:

Unless you are using the memory protection port of FreeRTOS stack
overflow checking only happens when a task is switched out. Is is
feasible that a stack overflow results in memory corruption, and that
memory corruption results in a hard fault before the task is switched
out - so the hard fault occurs before the overflow is detected.

May I ask why you are using a recursive function? Perhaps it can be rewritten to use a plain loop instead. Then you avoid this problem.