Measuring Stack Usage the Hard Way (2023)
interrupt.memfault.com
interrupt.memfault.com
The "walk off the edge" technique is what is described here [0], in embedded rust we have a tool that does this automatically (from the author of the post) called `flip-link` [1]. It requires a second linker/re-linking pass to do this sizing automatically (you could also do the same manually with a linker script in one pass, if you hardcode certain sizes).
Also: The program still crashes at "random" spots instead of behaving unpredictable. But for that you could add some assembly at the beginning of each function, asserting that the sp is in the pre-defined stack range. If your hardware supports it, you could also mark the "overflow area" behind the stack as read-only and let it crash that way.
Due to a past job I'm biased to static program analysis for this task, something the post author completly neglects (doesn't know about?). At least if sound boundaries matter (e.g. safety critical stuff).
There are ways to annotate and work around this! I'm also from a safety critical background. But there aren't many (any?) OSS tools that do this completely. cargo-call-stack is the closest I've seen.
[0]: https://github.com/Dirbaio/cargo-call-stack/
I agree static analysis is preferrable, but it's also not a "just enable the check, duh" easy, sadly, and it's good to know different techniques.
If you have an MPU or an MMU (which is generally true for 32-bit microcontrollers), you can just set a range to fault a standard downwards growing stack – pretty standard stuff.
It may not be possible to dynamically redefine those though