Skip to content

Commit f2573dc

Browse files
eric-wieserKha
andauthored
fix: Do not overwrite existing signal handlers (#5062)
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>
1 parent 51f01d8 commit f2573dc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/runtime/stack_overflow.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Port of the corresponding Rust code (see links below).
1818
#include <cstdlib>
1919
#include <cstring>
2020
#include <lean/lean.h>
21+
#include <initializer_list>
2122
#include "runtime/stack_overflow.h"
2223

2324
namespace lean {
@@ -45,7 +46,7 @@ stack_guard::stack_guard() {
4546
stack_guard::~stack_guard() {}
4647
#else
4748
// Install a segfault signal handler and abort with custom message if address is within stack guard.
48-
// https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/stack_overflow.rs
49+
// https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/unix/stack_overflow.rs
4950

5051

5152
// https://github.com/rust-lang/rust/blob/7c8dbd969dd0ef2af6d8bab9e03ba7ce6cac41a2/src/libstd/sys/unix/thread.rs#L293
@@ -102,12 +103,17 @@ void initialize_stack_overflow() {
102103
#ifdef LEAN_WINDOWS
103104
AddVectoredExceptionHandler(0, stack_overflow_handler);
104105
#else
105-
struct sigaction action;
106-
memset(&action, 0, sizeof(struct sigaction));
107-
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
108-
action.sa_sigaction = segv_handler;
109-
sigaction(SIGSEGV, &action, nullptr);
110-
sigaction(SIGBUS, &action, nullptr);
106+
for (auto signum : {SIGSEGV, SIGBUS}) {
107+
struct sigaction action;
108+
memset(&action, 0, sizeof(struct sigaction));
109+
sigaction(signum, nullptr, &action);
110+
// Configure our signal handler if one is not already set.
111+
if (action.sa_handler == SIG_DFL) {
112+
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
113+
action.sa_sigaction = segv_handler;
114+
sigaction(signum, &action, nullptr);
115+
}
116+
}
111117
#endif
112118
}
113119

0 commit comments

Comments
 (0)