Skip to content

Commit

Permalink
Rollup merge of rust-lang#69685 - cuviper:soft-segv, r=sfackler
Browse files Browse the repository at this point in the history
unix: Don't override existing SIGSEGV/BUS handlers

Although `stack_overflow::init` runs very early in the process, even
before `main`, there may already be signal handlers installed for things
like the address sanitizer. In that case, just leave it alone, and don't
bother trying to allocate our own signal stacks either.

Fixes rust-lang#69524.
  • Loading branch information
Centril authored Mar 8, 2020
2 parents 8a30198 + db75c97 commit d9b718f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/libstd/sys/unix/stack_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ impl Handler {
pub unsafe fn new() -> Handler {
make_handler()
}

fn null() -> Handler {
Handler { _data: crate::ptr::null_mut() }
}
}

impl Drop for Handler {
Expand Down Expand Up @@ -108,13 +112,20 @@ mod imp {
}

static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
static mut NEED_ALTSTACK: bool = false;

pub unsafe fn init() {
let mut action: sigaction = mem::zeroed();
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
action.sa_sigaction = signal_handler as sighandler_t;
sigaction(SIGSEGV, &action, ptr::null_mut());
sigaction(SIGBUS, &action, ptr::null_mut());
for &signal in &[SIGSEGV, SIGBUS] {
sigaction(signal, ptr::null_mut(), &mut action);
// Configure our signal handler if one is not already set.
if action.sa_sigaction == SIG_DFL {
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
action.sa_sigaction = signal_handler as sighandler_t;
sigaction(signal, &action, ptr::null_mut());
NEED_ALTSTACK = true;
}
}

let handler = make_handler();
MAIN_ALTSTACK = handler._data;
Expand Down Expand Up @@ -152,6 +163,9 @@ mod imp {
}

pub unsafe fn make_handler() -> Handler {
if !NEED_ALTSTACK {
return Handler::null();
}
let mut stack = mem::zeroed();
sigaltstack(ptr::null(), &mut stack);
// Configure alternate signal stack, if one is not already set.
Expand All @@ -160,7 +174,7 @@ mod imp {
sigaltstack(&stack, ptr::null_mut());
Handler { _data: stack.ss_sp as *mut libc::c_void }
} else {
Handler { _data: ptr::null_mut() }
Handler::null()
}
}

Expand Down Expand Up @@ -198,7 +212,7 @@ mod imp {
pub unsafe fn cleanup() {}

pub unsafe fn make_handler() -> super::Handler {
super::Handler { _data: ptr::null_mut() }
super::Handler::null()
}

pub unsafe fn drop_handler(_handler: &mut super::Handler) {}
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/sanitize/badfree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// needs-sanitizer-support
// only-x86_64
//
// compile-flags: -Z sanitizer=address -O
//
// run-fail
// error-pattern: AddressSanitizer: SEGV

use std::ffi::c_void;

extern "C" {
fn free(ptr: *mut c_void);
}

fn main() {
unsafe {
free(1 as *mut c_void);
}
}

0 comments on commit d9b718f

Please sign in to comment.