embedded stack collision with the variable-memory

There was a strange bug I found which took me ages to find. I was implementing some stuff on a Atmel A90S8515 microcontroller. This device is quite old and has very limited memory. Both Flash and RAM. The problem occurred when sprintf from the standard library was called. When called,  any internal variables were changed (seemingly randomly). The only reason some I figured it out was because there was a soft-PWM implementation, which changed the color of a RGB led. Reading trough forums I found out that the sprintf-function is quite memory consuming, therefore uses a lot of stack related memory. As a heap is not existent (there is no dynamic data), a good reason for the strange behaviour might be the stack colliding with some global variables. Luckily, this hypothesis was underlined by this article. Therefore, what happened is, that the execution of sprintf (consuming lot of memory on the stack), overwrote global variables in the bss and or the data part of the memory. As the compiler is not able to find these kind of bugs, there is no warning or whatever. The solution was found within seconds. Some very large send/receive  buffers (defined as such in the initial phase of  the project) were just re sized from 128 bytes each to 32 bytes each, making more room for the stack!

Leave a comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.