From 0b65607d25f515f66e477c06515b8393476b74b6 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 15 Aug 2024 18:05:03 +0000 Subject: [PATCH 1/2] fix: Do not overwrite existing signal handlers Such handlers can come from address sanitizers and similar. This forward-ports https://github.com/rust-lang/rust/commit/676b9bc477dfe58971b7df9df4e3a053bb187dee. --- src/runtime/stack_overflow.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/runtime/stack_overflow.cpp b/src/runtime/stack_overflow.cpp index 262ebab73982..c152cff9581a 100644 --- a/src/runtime/stack_overflow.cpp +++ b/src/runtime/stack_overflow.cpp @@ -18,6 +18,7 @@ Port of the corresponding Rust code (see links below). #include #include #include +#include #include "runtime/stack_overflow.h" namespace lean { @@ -45,7 +46,7 @@ stack_guard::stack_guard() { stack_guard::~stack_guard() {} #else // Install a segfault signal handler and abort with custom message if address is within stack guard. -// https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/stack_overflow.rs +// https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/unix/stack_overflow.rs // https://github.com/rust-lang/rust/blob/7c8dbd969dd0ef2af6d8bab9e03ba7ce6cac41a2/src/libstd/sys/unix/thread.rs#L293 @@ -102,12 +103,17 @@ void initialize_stack_overflow() { #ifdef LEAN_WINDOWS AddVectoredExceptionHandler(0, stack_overflow_handler); #else - struct sigaction action; - memset(&action, 0, sizeof(struct sigaction)); - action.sa_flags = SA_SIGINFO | SA_ONSTACK; - action.sa_sigaction = segv_handler; - sigaction(SIGSEGV, &action, nullptr); - sigaction(SIGBUS, &action, nullptr); + for (auto signum : {SIGSEGV, SIGBUS}) { + struct sigaction action; + memset(&action, 0, sizeof(struct sigaction)); + sigaction(SIGSEGV, nullptr, &action); + // Configure our signal handler if one is not already set. + if (action.sa_handler == SIG_DFL) { + action.sa_flags = SA_SIGINFO | SA_ONSTACK; + action.sa_sigaction = segv_handler; + sigaction(signum, &action, nullptr); + } + } #endif } From 51dfd1176f4f1946346206c0bdf97850be3c53df Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Fri, 16 Aug 2024 10:58:51 +0100 Subject: [PATCH 2/2] Update src/runtime/stack_overflow.cpp Co-authored-by: Sebastian Ullrich --- src/runtime/stack_overflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/stack_overflow.cpp b/src/runtime/stack_overflow.cpp index c152cff9581a..73cb51c426d1 100644 --- a/src/runtime/stack_overflow.cpp +++ b/src/runtime/stack_overflow.cpp @@ -106,7 +106,7 @@ void initialize_stack_overflow() { for (auto signum : {SIGSEGV, SIGBUS}) { struct sigaction action; memset(&action, 0, sizeof(struct sigaction)); - sigaction(SIGSEGV, nullptr, &action); + sigaction(signum, nullptr, &action); // Configure our signal handler if one is not already set. if (action.sa_handler == SIG_DFL) { action.sa_flags = SA_SIGINFO | SA_ONSTACK;