3

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?

2 Answers2

1

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

  • That is a very non- deterministic test. As @4386427 has pointed out, the RTOS can only check the stack in system calls or when rescheduling. Your test probably relies in the the stack being blown on thread creation rather than the test function. A better method, would be to insert a vTaskDelay(1) call at the start of fun() to force the scheduler to run in every call. – Clifford Jan 31 '20 at 14:35
0

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.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63