# Stack overflow

**URL:** <https://forums.freertos.org/t/stack-overflow/8292>\
**Category:** Kernel\
**Created:** [November 26, 2019, 6:36am UTC](https://forums.freertos.org/t/stack-overflow/8292 "2019-11-26T06:36:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 26, 2019, 6:36am UTC](https://forums.freertos.org/t/stack-overflow/8292/1 "2019-11-26T06:36:29Z")

</div>

**[sasikalaswathi](https://sourceforge.net/u/sasikalaswathi/)** wrote on [Tuesday, November 26, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#c673):

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?

---

<div class="post-metadata">

**Author:** ![McuOnEclipse](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mcuoneclipse/32/32_2.png) [@McuOnEclipse](https://forums.freertos.org/u/McuOnEclipse)\
**Post date:** [November 26, 2019, 6:55am UTC](https://forums.freertos.org/t/stack-overflow/8292/2 "2019-11-26T06:55:57Z")

</div>

**[xz8987f](https://sourceforge.net/u/xz8987f/)** wrote on [Tuesday, November 26, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#a32f):

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/](https://mcuoneclipse.com/2019/09/28/stack-canaries-with-gcc-checking-for-stack-overflow-at-runtime/)

I hope this helps,  
Erich

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 26, 2019, 9:32am UTC](https://forums.freertos.org/t/stack-overflow/8292/3 "2019-11-26T09:32:39Z")

</div>

**[sasikalaswathi](https://sourceforge.net/u/sasikalaswathi/)** wrote on [Tuesday, November 26, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#a32f/51a5):

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.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 26, 2019, 11:47am UTC](https://forums.freertos.org/t/stack-overflow/8292/4 "2019-11-26T11:47:47Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Tuesday, November 26, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#55f3):

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.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 26, 2019, 4:05pm UTC](https://forums.freertos.org/t/stack-overflow/8292/5 "2019-11-26T16:05:27Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, November 26, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#9f24):

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.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 28, 2019, 4:56am UTC](https://forums.freertos.org/t/stack-overflow/8292/6 "2019-11-28T04:56:27Z")

</div>

**[sasikalaswathi](https://sourceforge.net/u/sasikalaswathi/)** wrote on [Thursday, November 28, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#55f3/5a95):

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;  
}  
}

}

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 28, 2019, 5:05am UTC](https://forums.freertos.org/t/stack-overflow/8292/7 "2019-11-28T05:05:21Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, November 28, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/e3975b4812/#55f3/5a95/b136):

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.

---

<div class="post-metadata">

**Author:** ![johankraft](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@johankraft](https://forums.freertos.org/u/johankraft)\
**Post date:** [December 3, 2019, 5:25am UTC](https://forums.freertos.org/t/stack-overflow/8292/8 "2019-12-03T05:25:31Z")

</div>

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.
