catching process stack overflows in Unix

catching process stack overflows in Unix

Post by James R. Cord » Thu, 20 Apr 1995 04:00:00



Forgive me if this is a FAQ, but I am facing a
problem that has annoyed me about Unix for as long as
I can remember.  Here's how it works:

I have a deeply recursive procedure that I use extensively.  
Being deeply recursive, it eats stack space like crazy.
Eventually, it runs out of process stack segment (or runs
into the stack limit) and gets a SIGSEGV.  Fine.

BUT ... in Unix, you can't catch the signal because if you
are out of stack space your signal handler can't activate.
Dumb.  But I need to catch it because running out of stack space
in this way is meaningful in my application, and it should
recover gracefully.

SO ... what's the best way to handle this?  Surely one of
you hotshot hackers can figure a way to catch the problem?

CONSTRAINTS: ANSI C, POSIX, no alternate threads or stacks,
no alternate implementations of recursion, please.  

Ideas?

Thanks
Jim Cordy

 
 
 

catching process stack overflows in Unix

Post by Thomas E. Gilber » Sat, 22 Apr 1995 04:00:00



 ...

Quote:

> BUT ... in Unix, you can't catch the signal because if you
> are out of stack space your signal handler can't activate.
> Dumb.  But I need to catch it because running out of stack space
> in this way is meaningful in my application, and it should
> recover gracefully.

 ..

Quote:> CONSTRAINTS: ANSI C, POSIX, no alternate threads or stacks,
> no alternate implementations of recursion, please.  

Why, not just check for the recursion level and stop when it
reachs some insane value. If the amount on the stack varies
then you can always use a setjump at the top and then another
setjump on each recursive call and compute the difference between
the stack pointers. Just because POSIX or ANSI C doesn't define
something doesn't mean you can't use it. This is the real world
and standards never address all the real constraints imposed by
systems. So, why not use sigaltstack(2) on those systems that
provide it?

Tom Gilbert
Contract Software Engineer
Harris Space Systems

 
 
 

1. Catching a stack-overflow?

Actually, this is more of a generic Unix question.  I'm looking for ways
to catch overflow of the C-stack.

On Solaris I could use sigstack() to change the signal stack, catch the
segmentation fault that is normally signalled on a stack-overflow and
try to deduce (how?) that I've got a stack overflow.  This awkward route
is appearently not supported on Linux.

Anything better?  I would be so happy with

        signal(SIGSTACK, stack_low);

and some mechanism of specifying when I consider the stack unreasonable
low.

I've got lots of routines that need to traverse tree-like structures
that can either be very deep (so I run out of resources), or contain
loops.

There should be something better than changing all recursive routines
into non-recursive implementations using explicit checks on the
allocation of the required data ...

Please cc by E-mail if you have something useful

        Thanks --- Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jan Wielemaker                Author of SWI-Prolog and the XPCE GUI library
SWI, University of Amsterdam  http://www.swi.psy.uva.nl/usr/jan/SWI-Prolog.html

2. FTP & ipfwadm

3. Buffer Overflows: Question about the internals of process creation and stack maintenence.

4. #==== What is actually happening when I config & compile a kernel ?? ====#

5. Process Kernel Stack and Interrupt Stack

6. Using a variable in a symbolic link

7. demystifying process stack vs. java thread stack on solaris

8. talk problem in RH 5.0

9. Catch Integer Overflow

10. Catching Overflow/Underflow errors

11. How to catch Floating point overflow/underflow

12. catching signals for bad stack

13. Handling stack-overflow?