Skip to content

Commit

Permalink
fix: Do not overwrite existing signal handlers (#5062)
Browse files Browse the repository at this point in the history
Such handlers can come from address sanitizers and similar. When
combined with #4971, this forward-ports
rust-lang/rust@676b9bc
/ rust-lang/rust#69685

---------

Co-authored-by: Sebastian Ullrich <sebasti@nullri.ch>
  • Loading branch information
eric-wieser and Kha authored Aug 19, 2024
1 parent 51f01d8 commit f2573dc
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/runtime/stack_overflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Port of the corresponding Rust code (see links below).
#include <cstdlib>
#include <cstring>
#include <lean/lean.h>
#include <initializer_list>
#include "runtime/stack_overflow.h"

namespace lean {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(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;
action.sa_sigaction = segv_handler;
sigaction(signum, &action, nullptr);
}
}
#endif
}

Expand Down

0 comments on commit f2573dc

Please sign in to comment.