[Python-Dev] stack check on Unix: any suggestions?

Barry Scott barry@scottb.demon.co.uk
Tue, 29 Aug 2000 22:21:04 +0100


Use the problem as the solution.

The problem is that you get a SIGSEGV after you fall off the end of the stack.
(I'm assuming you always have guard pages between the stack end and other memory
zones. Otherwise you will not get the SEGV).

If you probe ahead of the stack to trigger the SIGSEGV you can use the signal
handler to trap the probe and recover gracefully. Use posix signal handling
everywhere for portability (don't mix posix and not and expect signals to work
BTW).

jmp_buf probe_env;

int CheckStack()	/* untested */
	{
	if( setjmp( &probe_env ) == 0 )
		{
		char buf[32];
		/* need code to deal with direction of stack */
		if( grow_down )
			buf[-65536] = 1;
		else
			buf[65536] = 1;
		return 1; /* stack is fine of 64k */
		}
	else
		{
		return 0; /* will run out of stack soon */
		}
	}

void sigsegv_handler( int )
	{
	longjmp( &probe_env );
	}

			Barry (not just a Windows devo <wink>)