From b4d8d2638cb06f60d3c45653f5e6054940b84b8d Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 7 Oct 2019 00:28:34 +0200 Subject: [PATCH] Also attach our signal handler to SIGBUS, because MacOSX sometimes raises it on stack overflow --- src/simix/smx_global.cpp | 46 ++++++++++--------- .../simix/stack-overflow/stack-overflow.tesh | 2 +- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 4028e9a21a..7414de1529 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -54,19 +54,20 @@ XBT_ATTRIB_NORETURN static void inthandler(int) #ifndef _WIN32 static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/) { - if (siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_ACCERR) { - fprintf(stderr, "Access violation detected.\n" - "This probably comes from a programming error in your code, or from a stack\n" - "overflow. If you are certain of your code, try increasing the stack size\n" - " --cfg=contexts/stack-size=XXX (current size is %u KiB).\n" - "\n" - "If it does not help, this may have one of the following causes:\n" - "a bug in SimGrid, a bug in the OS or a bug in a third-party libraries.\n" - "Failing hardware can sometimes generate such errors too.\n" - "\n" - "If you think you've found a bug in SimGrid, please report it along with a\n" - "Minimal Working Example (MWE) reproducing your problem and a full backtrace\n" - "of the fault captured with gdb or valgrind.\n", + if ((siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_ACCERR) || siginfo->si_signo == SIGBUS) { + fprintf(stderr, + "Access violation or Bus error detected.\n" + "This probably comes from a programming error in your code, or from a stack\n" + "overflow. If you are certain of your code, try increasing the stack size\n" + " --cfg=contexts/stack-size=XXX (current size is %u KiB).\n" + "\n" + "If it does not help, this may have one of the following causes:\n" + "a bug in SimGrid, a bug in the OS or a bug in a third-party libraries.\n" + "Failing hardware can sometimes generate such errors too.\n" + "\n" + "If you think you've found a bug in SimGrid, please report it along with a\n" + "Minimal Working Example (MWE) reproducing your problem and a full backtrace\n" + "of the fault captured with gdb or valgrind.\n", smx_context_stack_size / 1024); } else if (siginfo->si_signo == SIGSEGV) { fprintf(stderr, "Segmentation fault.\n"); @@ -113,14 +114,17 @@ static void install_segvhandler() action.sa_flags = SA_ONSTACK | SA_RESETHAND | SA_SIGINFO; sigemptyset(&action.sa_mask); - if (sigaction(SIGSEGV, &action, &old_action) == -1) { - XBT_WARN("Failed to register signal handler for SIGSEGV: %s", strerror(errno)); - return; - } - if ((old_action.sa_flags & SA_SIGINFO) || old_action.sa_handler != SIG_DFL) { - XBT_DEBUG("A signal handler was already installed for SIGSEGV (%p). Restore it.", - (old_action.sa_flags & SA_SIGINFO) ? (void*)old_action.sa_sigaction : (void*)old_action.sa_handler); - sigaction(SIGSEGV, &old_action, nullptr); + /* Linux tend to raise only SIGSEGV where other systems also raise SIGBUS on severe error */ + for (int sig : {SIGSEGV, SIGBUS}) { + if (sigaction(sig, &action, &old_action) == -1) { + XBT_WARN("Failed to register signal handler for signal %d: %s", sig, strerror(errno)); + continue; + } + if ((old_action.sa_flags & SA_SIGINFO) || old_action.sa_handler != SIG_DFL) { + XBT_DEBUG("A signal handler was already installed for signal %d (%p). Restore it.", sig, + (old_action.sa_flags & SA_SIGINFO) ? (void*)old_action.sa_sigaction : (void*)old_action.sa_handler); + sigaction(sig, &old_action, nullptr); + } } } diff --git a/teshsuite/simix/stack-overflow/stack-overflow.tesh b/teshsuite/simix/stack-overflow/stack-overflow.tesh index c7180faaed..11c9899b7d 100644 --- a/teshsuite/simix/stack-overflow/stack-overflow.tesh +++ b/teshsuite/simix/stack-overflow/stack-overflow.tesh @@ -1,7 +1,7 @@ ! expect signal SIGSEGV $ ${bindir:=.}/stack-overflow --cfg=contexts/stack-size:96 ${srcdir:=.}/examples/platforms/small_platform.xml > [Tremblay:master:(1) 0.000000] [test/INFO] Launching our nice bugged recursive function... -> Access violation detected. +> Access violation or Bus error detected. > This probably comes from a programming error in your code, or from a stack > overflow. If you are certain of your code, try increasing the stack size > --cfg=contexts/stack-size=XXX (current size is 96 KiB). -- 2.20.1