This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Configuring stack size and direction to avoid stack overflow

Hello,

I'm using a Cortex M-4 processor (STM32F4) and I recently encountered strange behavior of my code after minor modifications that were not directly related to the problems I was seeing.

After a bit of digging, I noticed that some of my global variables were modified without being accessed through the code. Looking at the memory mapping, it turned out these variables were located just before the stack. Increasing the stack size in the startup file solved the issue, so obviously this was a stack overflow.

But the thing is, I want to be sure that when a stack overflow occurs, a fault happens, not some random behavior. I thought I was safe because my stack was at the end of the RAM space. But as it turns out, it actually grows backwards.

So the question is: How to configure the stack direction ? And are there other ways to prevent this kind of problem?

Thank you

Parents Reply Children
  • So, my available RAM space is 0x20000000 -> 0x20003000.

    According to the .map file, my global/static variables (.data, .bss) are stored between 0x20000000 and 0x20001cd0. The stack starts right after.

    I had configured its size used to 0x400, so it used the space 0x20001cd0 -> 0x200020d0.

    I guess it would be better if it was something like 0x20002c00 -> 0x20003000 but I don't know how to do that. But even without this, I still don't understand how it could corrupt the last .bss segment.

    Using the debugger, it was clear that after setting the stack size to 0x0600, the "beginning" of the stack (0x20001cd0 to more or less 0x20001e50) stayed unused, and it was the "end" that was used.

    What do you think ? Should I be posting on the ST forums instead of here ?