# A Guide to Using ARM Stack Limit Registers | Interrupt

**URL:** <https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632>\
**Category:** Blog\
**Created:** [February 14, 2023, 3:13pm UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632 "2023-02-14T15:13:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.memfault.com/discobot/32/1_2.png) [@discobot](https://community.memfault.com/u/discobot)\
**Post date:** [February 14, 2023, 3:13pm UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632/1 "2023-02-14T15:13:22Z")

</div>

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](https://interrupt.memfault.com/blog/using-psp-msp-limit-registers-for-stack-overflow)

---

<div class="post-metadata">

**Author:** ![xorly](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@xorly](https://community.memfault.com/u/xorly)\
**Post date:** [November 16, 2023, 2:19pm UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632/2 "2023-11-16T14:19:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.memfault.com/discobot/32/1_2.png) [@discobot](https://community.memfault.com/u/discobot)\
**Post date:** [November 16, 2023, 2:19pm UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632/3 "2023-11-16T14:19:08Z")

</div>



---

<div class="post-metadata">

**Author:** ![jkurtz](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jkurtz](https://community.memfault.com/u/jkurtz)\
**Post date:** [February 28, 2024, 4:35am UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632/4 "2024-02-28T04:35:39Z")

</div>

Hi @xorly,  
Apologies for the late reply, as I didn’t have notifications set. Yes this code is tested, and does properly detect the MSP and PSP in the UsageFault\_Handler. It is a slight oversight, as you mention, that it doesn’t cover all conditions. Changing to bge from beq is probably better in this case.

---

<div class="post-metadata">

**Author:** ![xorly](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@xorly](https://community.memfault.com/u/xorly)\
**Post date:** [February 28, 2024, 9:36pm UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632/5 "2024-02-28T21:36:21Z")

</div>

Hi @jkurtz ,  
presented code works as expected, but according to my observation is not applicable in more real life scenarios.

Detection in your code assumes, that in invalid value remains in SP and SP==LIM is invalid. According to my observation, neither of these is true. Can you point me to docs which tell otherwise?

My observation:  
SP==LIM is valid state and PSP is never updated into invalid value and remain on the last valid one.

Lets have SP == 6, LIM == 5, now decrement SP by 2. Fault is generated, but SP will have value == 6.

---

<div class="post-metadata">

**Author:** ![jkurtz](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jkurtz](https://community.memfault.com/u/jkurtz)\
**Post date:** [February 28, 2024, 9:57pm UTC](https://community.memfault.com/t/a-guide-to-using-arm-stack-limit-registers-interrupt/632/6 "2024-02-28T21:57:18Z")

</div>

Hi @xorly,

I possibly didn’t communicate this properly. You are correct, this was an oversight in the assembly code above. But very easily corrected, which I am sure you have addressed by now.  
I have a PR in the repository to reflect this.
