
On Wed, Mar 13, 2013 at 1:55 PM, Måns Rullgård mans@mansr.com wrote:
I suppose we could check the available stack space.
There is no way to check stack usage from C.
Well there is an architecture-specific way. A function can generally find its own stack pointer by taking the address of a local variable, so it is possible to write a function to check for stack overflow.
Performing such checks without getting into undefined behaviours is tricky if not impossible, and modern compilers are quite effective at exploiting these, rendering such checks useless. Remember the deleted null checks in the kernel a while back?
If possible, it is much nicer to use a compiler option rather than something in the application itself to check for stack overflow. On the Blackfin gcc there is a -mstack-check-l1 option to do this. It has a little overhead, but has saved my bacon. Note that the libraries also need to be compiled with that option if stack overflows within those libraries are to be detected.
Steve