Re: Catching SIGSEGV with signal() in 2.6

From: Kevin B. Hendricks
Date: Mon Apr 05 2004 - 19:42:51 EST


Hi,

Just in case this helps, this is a simplified testcase of the OpenOffice.org
code in question that always worked under 2.4 kernels on multiple
architectures but fails on 2.6.X kernels on those same multiple platforms.

For some reason, the segfault generated by trying to write to address 0 can
not be properly caught anymore (or at least it appears that way to me).

Hope this helps.

Kevin

[kbhend@base1 solar]$ cat testcase.c
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

typedef int (*TestFunc)( void* );
static jmp_buf check_env;
static int bSignal;

void SignalHdl( int sig )
{
bSignal = 1;
longjmp( check_env, sig );
}

int check( TestFunc func, void* p )
{
int result;
bSignal = 0;
if ( !setjmp( check_env ) )
{
signal( SIGSEGV, SignalHdl );
signal( SIGBUS, SignalHdl );
result = func( p );
signal( SIGSEGV, SIG_DFL );
signal( SIGBUS, SIG_DFL );
}
if ( bSignal )
return -1;
else
return 0;
}

int GetAtAddress( void* p )
{
return *((char*)p);
}

int SetAtAddress( void* p )
{
return *((char*)p) = 0;
}

int CheckGetAccess( void* p )
{
int b;
b = -1 != check( (TestFunc)GetAtAddress, p );
return b;
}

int CheckSetAccess( void* p )
{
int b;
b = -1 != check( (TestFunc)SetAtAddress, p );
return b;
}

void InfoMemoryAccess( char* p )
{
if ( CheckGetAccess( p ) )
printf( "can read address %p\n", p );
else
printf( "can not read address %p\n", p );

if ( CheckSetAccess( p ) )
printf( "can write address %p\n", p );
else
printf( "can not write address %p\n", p );
}

int
main( int argc, char* argv[] )
{
{
char* p = NULL;
InfoMemoryAccess( p );
p = (char*)&p;
InfoMemoryAccess( p );
}
exit( 0 );
}


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/