0

Checking for Stack Overflow on an Arduino
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Checking for Stack Overflow on an Arduino

by tkinsman on Sun Jul 14, 2019 11:22 am

I want to teach the students about planning for failures, and knowing when they happen.

A failure that happens sporadically is stack overflow. I run out of SRAM.

How do others check for this in your Arduino Uno R3 code?

This is hard to make a test for, because it only happens *sometimes*.
Does anyone have some valid test snippets?

tkinsman
 
Posts: 158
Joined: Sun Sep 25, 2016 2:04 pm

Re: Checking for Stack Overflow on an Arduino

by adafruit_support_bill on Sun Jul 14, 2019 12:32 pm

The UNO will not throw an exception or give you any definitive indication that you have blown the stack. Depending on the state of the heap, it may crash, behave erratically or go completely unnoticed.

There are some diagnostics that you can use to monitor the amount of space between the top of stack and top of heap. See this guide for details:

http://learn.adafruit.com/memories-of-an-arduino

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Checking for Stack Overflow on an Arduino

by tkinsman on Sun Jul 14, 2019 3:30 pm

Talking with a former embedded engineer, I got the following ideas:

1. In the setup() routine, allocate an array that is the size of the rest of the stack, and load it with 0xFF's everywhere.
Then, after running your program for a few days you figure that your program has probably generated every interrupt it is going to generate, and loaded every subroutine it will need. So then look at the stack of 0xFF's, to see how much memory you actually used on the stack. This can be used in some situations to reduce your stack size when linking.

2. In the loop() routine:
2A. Allocate a static variable as one of the last variables.
2B. On the first time through the loop( ), set that static variable to something known, like 0xABCD0123, of 0xDEADBEEF.
2C. In subsequent passes through the loop, check that the static variable is still set to whatever you set it to. If it changes, then something went wrong... like a stack overflow.

These are just ideas, and I have not implemented any code yet, but I thought they were good ideas to record and share here.

tkinsman
 
Posts: 158
Joined: Sun Sep 25, 2016 2:04 pm

Re: Checking for Stack Overflow on an Arduino

by adafruit_support_bill on Sun Jul 14, 2019 6:37 pm

Yes. Those are all reasonable methods to determine if a stack overrun has occurred. Of course, you might crash before you can exercise the diagnostic. In practice, for critical applications you would estimate usage, design in a safety factor, then apply diagnostics to assure that the actual usage agreed with the estimates.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Checking for Stack Overflow on an Arduino

by kevinjwalters on Thu Aug 01, 2019 8:28 am

We have been discussing how CircuitPython/MicroPython does or can do this to rule it out as a contributor to a strange problem. It has similar features to the ones you've mentioned which can be enabled when the interpreter is compiled via #define statements:


Depending on how far you want to go in discussing this you could talk about:

  • coverage reports for tests as an attempt to exercise all of the code and most uses of the stack,
  • memory corruption from programming errors and hazards of traditional C programming - no array bounds checking, encouraging pointer use, etc
  • potential security vulernabilities due to former,
  • evolution of hardware features to limit risk, memory management units (MMU), "advanced" features like no execute.

kevinjwalters
 
Posts: 432
Joined: Sun Oct 01, 2017 3:15 pm

Re: Checking for Stack Overflow on an Arduino

by kevinjwalters on Tue Aug 06, 2019 7:47 pm

I did wonder after that whether maximum stack size for C was predictable if variable length arrays (VLA) were not permitted and alloca() not used. I just found a discussion on this and the answer is only yes if recursion is not permitted, Better Embedded System SW: Don’t Overflow the Stack refers to MISRA C rule 70 which prohibits this.

kevinjwalters
 
Posts: 432
Joined: Sun Oct 01, 2017 3:15 pm

Re: Checking for Stack Overflow on an Arduino

by adafruit_support_bill on Wed Aug 07, 2019 6:13 am

Keep in mind that strings are essentially variable length arrays. And any string manipulation has the potential to change the length of the array. C++ allows you to pass strings (and many other variable types) by value or by reference. This can have a major impact on stack utilization - as well as performance.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.