detecting stack overflow during compile time?

how can I do this? anyone used -fstack-check option with gcc? How does it work? Any idea on checking the state of the stack during run time?
Thanks in advance.

Am I asking a dumb question here? or too generic?

Jinma,

I’d say your asking a question that is better asked (or googled) in a gcc forum than a QNX one since this is a compiler specific option.

Tim

If the stack grow to big you’ll get a SIGSEGV, not sure the fstack-check is relevant under QNX6?

I just wanted to try here first to see if there were any QNX users who tried such option.
I do handle the SIGSEGV in my code when it happens, but I just wanted to capture obvious stack overflow during compile time.

I think I found the answer from the QNX documentation, it seems like -f stack-check option is not available in QNX. Below is the text from the documentation under the title “Heap Analysis: Making Memory Errors a Thing of the Past”


Bounds checking GCC

Bounds checking GCC is a variant of GCC that allows individual modules to be compiled with bounds checking enabled. When a heap buffer is allocated within a checked module, information about the buffer is added to the runtime information about the memory space kept on behalf of the compiler. Attempts to dereference or update the pointer in checked modules invokes intrinsic functions that obtain information about the bounds of the object – it may be stack, heap or an object in the data segment – and checks to see that the reference is in bounds. When an access is out of bounds, the runtime environment generates an error.

The bounds checking variant of GCC hasn’t been ported to the Neutrino environment. In order to check objects that are kept within the data segment of the application, the compiler runtime environment requires some Unix functions that aren’t provided by Neutrino. The intrinsics would have to be modified to work in the Neutrino environment.

The model for obtaining information about heap buffers with this compiler is also slightly different than the model employed by the malloc library. Instead of this, the compiler includes an alternative malloc implementation that registers checked heap buffers with a tree data structure outside of the program’s control. This tree is used for searches made by the intrinsics to obtain information about checked objects. This technique may take more time than the malloc mechanism for some programs, and is incompatible with the checking and memory leak detection provided by the malloc library. Rather than performing multiple test runs, a port which reimplemented the compiler intrinsics to obtain heap buffer information from the malloc library would be desirable.

You might want to look at pclint ( www.gimple.com )

Bounds checking comes with an obvious overhead price. I think that you can do the same thing with C++ without needing a special implementation of GCC.

The gimple.com link seems to be broken Mario. At least it is right now.

how can I do this in C++?

Woops my bad gimpel.com/

You could use std::vector in combinaison with at(). For standard array (allocated on the stack) you can use boost::array ( you need boost for that ).

But this is at run time, not at compile time.

Instead of an array you could create your own array like object.
Your accessors do the bounds checking.

There are probably standard objects that do this already.

The new gcc (4.4.2) that will be coming out with 6.5.0 support a new -Wframe-larger-than=NUMBER option

The new -Wframe-larger-than=NUMBER option directs GCC to emit a warning if any stack frame is larger than NUMBER bytes. This may be used to help ensure that code fits within a limited amount of stack space.

thanks a bunch for all your input, it was helpful.