Skip to contentSkip to conversation

2.4, arch-dependent floating point exception and trap handling

DISCUSSION Daniel Barlow 2 participants 2 messages Jul 30, 2002 – Jul 31, 2002

lore
Share & tools
lore
#0 lore raw

I'm seeing discrepancies between the way that floating point exceptions are handled on different architectures. Briefly, I have a program that uses the glibc function feenableexcept() to enable SIGFPE on divide-by-zero errors, then installs a SIGFPE handler that uses sigsetjmp/siglongjmp, then divides 1.0 by 0.0 twice.

What I expect to happen is for my signal handler to be called twice. What actually happens varies:

On Alpha: 2.4.19-pre1-ac2, gcc version 2.95.4 20011002, PCA56 (SX164)

:; cc -o foo foo.c -lm -mieee
foo.c: In function `main':
foo.c:23: warning: assignment from incompatible pointer type :; ./foo
exceptions enabled: 0
exceptions enabled: 40000
hello
in handler
abort
in handler
abort
terminating

- exactly as expected

On x86: 2.4.19-rc1, gcc version 2.95.4 20011002 , Pentium III (Coppermine)

my signal handler is called the first time, and then the second attempt gets "inf". I've run this under gdb as well; the second SIGFPE is not received by the process

:; gcc -o foo foo.c -lm
foo.c: In function `main':
foo.c:23: warning: assignment from incompatible pointer type :; ./foo
exceptions enabled: 0
exceptions enabled: 4
hello
in handler
abort
a/b=inf
terminating

On PPC: 2.4.19-pre8, gcc version 2.95.4 20011002, 266MHz 740/750 rev 2.2 (tangerine iMac) I have x86-like behaviour

:; cc -o foo foo.c -lm
foo.c: In function `main':
foo.c:23: warning: assignment from incompatible pointer type :; ./foo
exceptions enabled: 0
exceptions enabled: 4000000
hello
in handler
abort
a/b=inf
terminating

All of these are uniprocessor machines running uniprocessor kernels. The behaviour I'd like _most_ is for the pre-signal environment to be restored, but at least to get the same behaviour everywhere would be a good thing.

I append the test program I've been using so you can tell me whether my whole approach is misguided

---cut here---
#include <signal.h>
#include <stdio.h>
#include <sys/signal.h>
#include <ucontext.h>
#include <fenv.h>
#include <setjmp.h>

sigjmp_buf env;
double a=1.0,b=0.0;

int handler(int signal, struct siginfo *info, struct ucontext *context) {

    printf("in handler\n");
    siglongjmp(env,signal);
}
main() {
    struct sigaction sa;

    a=1.0;
    printf("exceptions enabled: %x\n", fegetexcept());
    feenableexcept(FE_DIVBYZERO);   
    printf("exceptions enabled: %x\n", fegetexcept());
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO | SA_RESTART;
    sigaction(SIGFPE, &sa, NULL); 
    printf("hello\n");
    /* first time */
    if(sigsetjmp(env,1)) printf("abort\n");
    else                 printf("a/b=%f\n", a/b);

    /* now try again, see if things were restored */
    if(sigsetjmp(env,1)) printf("abort\n");
    else                 printf("a/b=%f\n", a/b);
    printf("terminating\n");
}

--- cut here ---

If this message should have been sent elsewhere (e.g. to port-specific lists or maintainers) please feel free to tell me where and/or to forward it.

Thanks!

-dan

Signature · 1 line
  http://ww.telent.net/cliki/ - Link farm for free CL-on-Unix resources 
#1 lore raw

Hi,

Quoted 8 lines

I'm seeing discrepancies between the way that floating point exceptions are handled on different architectures. Briefly, I have a program that uses the glibc function feenableexcept() to enable SIGFPE on divide-by-zero errors, then installs a SIGFPE handler that uses sigsetjmp/siglongjmp, then divides 1.0 by 0.0 twice.

What I expect to happen is for my signal handler to be called twice. What actually happens varies:

...

Quoted 3 lines

my signal handler is called the first time, and then the second attempt gets "inf". I've run this under gdb as well; the second SIGFPE is not received by the process

The x86 like behaviour happens on ppc64 too:

exceptions enabled: 0
exceptions enabled: 4000000
hello
in handler
abort
a/b=inf
terminating

There is a reason for this. A signal handler might want to do floating point so exceptions are disabled upon entering a signal handler.

Now the question that needs to be answered is whether sigsetjmp should save the floating point exception register. I asked our resident standards expert Chris Yeoh and the answer is no.

So the x86, ppc32 and ppc64 behaviour looks correct.

Anton