A Guide to Using ARM Stack Limit Registers | Interrupt

Stack overflows have notoriously plagued the development processes. They often can go undetected and can present themselves in obscure ways. We have implemented software mechanisms to protect against them, but these have limitations and still don’t protect against all conditions.


This is a companion discussion topic for the original entry at https://interrupt.memfault.com/blog/using-psp-msp-limit-registers-for-stack-overflow

Hi,
I am not able to detect whether PSPLIM or MSPLIM caused the fault.
Your UsageFault_Handler in assembly expects, that corresponding SP changed value equal to its’ limit. I am observing, that PSP is never updated into invalid value and remain on the old one.
This agrees to “ARM v8-M Architecture Reference Manual” code listings which describe these checks.
Look for “// Memory operation only performed if limit not violated”
Was this code successfully tested? It would work if you repeatedly increment SP by 1. Code checks for SP==LIM which is, in fact, valid state just before next increment which causes fault.

In my test I am allocating 60 kiB on FreeRTOS task stack (just a few kiB stack). Usagefault triggers, but all SP values are higher than their limits.