Lukasz Stanek
Lukasz Stanek

Reputation: 51

How to simulate stack overflow on FreeRTOS

I configured CubeMX STM32 to use FreeRTOS stack overflow monitoring. Now I want to test that it in fact works. I tried some simple stuff like executing below function in one of the threads

`// C program to demonstrate stack overflow 
// by creating a non-terminating recursive 
// function. 

void fun(int x) 
{ 
    if (x == 1) 
       return; 
    x = 6; 
    fun(x); 
} 

   int x = 5; 
   fun(x); 

but I get HardFault.

Do you know a way to simulate stack overflow on FreeRTOS?

Upvotes: 3

Views: 1381

Answers (2)

4386427
4386427

Reputation: 44329

The stack monitoring happens when a running task is swapped out of running state.

Your program is likely to hit some HW memory limit (and generate a HardFault) before it is swapped out of running state. Consequently the stack monitor never runs.

So make an OS call inside the function that will make the task be swapped out of running state. Something like a delay/sleep or similar.

Upvotes: 0

Lukasz Stanek
Lukasz Stanek

Reputation: 51

Looks like I found the solution. All you need is to change stack size of one thread to very low and program goes to vApplicationStackOverflowHook

Upvotes: 1

Related Questions