CNX Software – Embedded Systems News

uClinux kernel panic: Stack overflow

If you’re using ucLinux, you may get kernel panic errors coming out of nowhere. There may be several reasons (buffer overflow, out of memory..), but the most common is stack overflow for the process or one of the threads.

To increase the stack size of a flat binary you’ll need to adjust the LDFLAGS as follows:


This will set the stack size to 64KB.

To change the stack size of a thread (e.g. 32KB below), you’ll need to set the stack size attribute:

How to detect which thread suffers from stack overflow?

First, you can check your code for recursive function calls and local variables (especially arrays) both of which will be added at runtime to the stack to estimate what should be the stack size. So if you have large arrays you may use a pointer + a call to malloc instead.

If this can not fix the issue, you can use the functions below to return the used and free stack size used by a particular thread.


You can also download this code as a small library from rowentel blog

On the platform we used (Sigma Designs EM8623L), this will only work the first time the thread is started as if the thread is restarted the data returned by the functions above may not be correct since the memory allocated by the stack may not be set to zero.

To work around that issue, you’ll need to allocate the stack by yourself:


However, for whatever reason, allocating the stack by ourselves had proven to make our software unstable so we only used it for debugging purposes.

Support CNX Software - Donate via PayPal, become a Patron on Patreon, or buy review samples