Floating Point Exception Core Dump
Asked Answered
N

2

10

I am newbie on the Linux signals, please help. The following code get core dump when run in Linux 2.6 gcc.

$ ./a.out
Floating point exception (core dumped)

The questions:
1. Since a process signal mask is installed, shouldn't the "SIGFPGE" generated by line 40 volatile int z = x/y; be blocked?
2. If it is not blocked, since a signal handler has been installed, shouldn't the "SIGFPE" be captured by the signal handler, instead of a core dump?
3. If I commented out line 40volatile int z = x/y;, and use line 42 raise(SIGFPE); instead, then everything works as I expected. What is the difference between x/0 and raise SIGFPE here?

Here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>

    void sig_handler(int signum)
    {
       printf("sig_handler() received signal %d\n", signum);
    }


    int main(int argc, char * argv[])
    {

       // setup signal mask, block all signals
       sigset_t set;
       sigfillset(&set);

       if(sigprocmask(SIG_BLOCK, &set, NULL)<0)
       {
          perror("failed to set sigmask");
          return -1;
       }

       // install signal handler for SIGFPE
       struct sigaction act;
       act.sa_handler = sig_handler;
       act.sa_mask = set;
       act.sa_flags = 0;
       if(sigaction( SIGFPE, &act, NULL)<0)
       {
          perror("sigaction failed");
          exit(-1);
       }

       volatile int x =1;
       volatile int y =0;
       volatile int z = x/y; //line 40

       //raise(SIGFPE); //line 42

       printf("point 1000\n");

       return 0;
    }
Noaccount answered 8/7, 2011 at 18:43 Comment(0)
R
4

Any SIGFPE caused by a hardware trap while the signal is blocked causes undefined behavior:

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.

(from sigprocmask specification)

Roma answered 8/7, 2011 at 18:46 Comment(3)
+1. Although the latest spec is Issue 7. :-)Hercules
@Nemo: Thanks. The rule is still there, but the wording changed slightly, so I updated my answer.Roma
Thanks for the answers. I hate "undefined".Noaccount
R
0

man signal

According to POSIX, the behavior of a process is undefined after it ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by kill(2) or raise(3). Integer division by zero has undefined result.

On some architectures it will generate a SIGFPE signal. (Also dividing the most negative integer by -1 may generate SIGFPE). Ignoring this signal might lead to an endless loop.

I discovered this after trying to work out why negative/positive infinity resulted from a division by zero: IEEE 754, division by zero

This is a place you never want to get:

void divZeroHdlr(int sig) {
  printf("div by zero: %d\n",sig)
  exit(1);}

int main(int argc, char *argv[]) {
  signal(SIGFPE, divZeroHdlr)
  int n = 1/0}
Repetend answered 4/7, 2019 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.