Catching SIGSEGV with signal() in 2.6
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 );
}intmain( int argc, char* argv[] )
{
{
char* p = NULL;
InfoMemoryAccess( p );
p = (char*)&p;
InfoMemoryAccess( p ); }
exit( 0 );
}
Quoted 2 lines · Kevin B. Hendricks
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).
If the code would be correct you'd see the expected behavior.
Quoted 5 lines
void SignalHdl( int sig )
{
bSignal = 1;
longjmp( check_env, sig );
}
Since you jump out of a signal handling you must use siglongmp
Quoted 5 lines
int
check( TestFunc func, void* p )
{
int result;
bSignal = 0;
if ( !setjmp(check_env) )
And sigsetjmp(check_env, 1) here.
Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
Hi,
Quoted 3 lines
If the code would be correct you'd see the expected behavior. Since you jump out of a signal handling you must use siglongmp And
sigsetjmp(check_env, 1)here.
So the code has been wrong since the beginning and we were just "lucky" it worked in all pre-2.6 kernels?
I have no doubt you are right but forgiving my ignorance here, please explain why must we use siglongjmp when longjmping out of a signal handler given that
1. before the next use of the handler we use signal again to properly set the signal handler (and the set of masked signals).
and
2. the mask of blocked signals will include sigsegv upon entry to the signal handler and therefore it will be masked after the normal longjmp since a normal longjmp wil not change the set of masked symbols.
What am I missing that makes sigsetjmp and siglongjmp a requirement, or is this just part of some specification someplace?
Kevin
Quoted 2 lines · Kevin B. Hendricks
So the code has been wrong since the beginning and we were just "lucky" it worked in all pre-2.6 kernels?
The old code depended on undefined behavior.
Quoted 2 lines
1. before the next use of the handler we use signal again to properly set the signal handler (and the set of masked signals).
Where do you set the signal mask? That's the point. You don't. This means jumping from the signal handler causes the signal to remain blocked. And then
~~~~
If any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated while they are blocked, the result is undefined, unless the signal was generated by the kill() function, the sigqueue() function, or the raise() function.
~~~~
(see pthread_sigmask in POSIX) comes into play.
The second SIGSEGV signal is created with the signal blocked and since it's neither of the functions mentioned in the text below which creates the signal anything can happen. The old kernel queued the signal, the new kernel terminates the process which is much better IMO. Try the attached program to see why. Also note, the 2.4 behavior is inconsistent. If no handler is installed the process is terminated, regardless of the signal being masked.
Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#include <signal.h>
int *p;
void
sh (int sig)
{
}
int
main(void)
{
sigset_t s;
sigemptyset (&s);
sigaddset (&s, SIGSEGV);
sigprocmask (SIG_BLOCK, &s, 0);
signal(SIGSEGV, sh);
return *p;
}
Hi Ulrich,
Quoted 1 line
The old code depended on undefined behavior.
Thanks for explanation. It makes perfect sense. I appreciate it.
Our bad assumption was that using signal to install a signalhandler on a specific signal unblocked that specific signal, but as you show it does not.
I will try to get a fix using sigsetjmp/siglongjmp or fork/wait into the forthcoming OOo 1.1.2 tree so that no more "problems" are reported.
Kevin
Quoted 7 lines · Ulrich Drepper
Quoted 2 lines · Kevin B. Hendricks
So the code has been wrong since the beginning and we were just "lucky" it worked in all pre-2.6 kernels?
The old code depended on undefined behavior.
Maybe it's simply *old* code, possibly written under libc5. There, signal() used SA_RESETHAND which implies SA_NODEFER which in turn did not block the signal and exiting from the signal handler via longjmp was OK.
With the new signal() behaviour in glibc2 one may get results undefined by POSIX but it still worked as before because the sigprocmask was ignored for SIGSEGV under Linux <2.6.
It's the combination of new glibc2 and new kernel that makes code like the mentioned one break.
It has nothing to do with POSIX - for POSIX all of this is "undefined/implementation defined behaviour". I had chosen to stay compatible...
Ciao, ET.
Signature · 3 lines
Not every program claims to be POSIX compliant (who reads 3600 pages of difficult to obtain specs?) - some are simply Linux programs...