2014-12-12 23:39:27 +00:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-10-23 05:29:41 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2016-01-23 07:49:57 +00:00
|
|
|
|
2015-12-18 12:29:49 +00:00
|
|
|
#![cfg_attr(test, allow(dead_code))]
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
use libc;
|
2014-11-24 03:21:17 +00:00
|
|
|
use self::imp::{make_handler, drop_handler};
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2015-12-18 12:29:49 +00:00
|
|
|
pub use self::imp::cleanup;
|
|
|
|
pub use self::imp::init;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
pub struct Handler {
|
|
|
|
_data: *mut libc::c_void
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler {
|
|
|
|
pub unsafe fn new() -> Handler {
|
2014-11-24 03:21:17 +00:00
|
|
|
make_handler()
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Handler {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2014-11-24 03:21:17 +00:00
|
|
|
drop_handler(self);
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 07:19:28 +00:00
|
|
|
#[cfg(any(target_os = "linux",
|
|
|
|
target_os = "macos",
|
2015-01-17 07:51:04 +00:00
|
|
|
target_os = "bitrig",
|
2015-11-01 21:56:31 +00:00
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
2016-01-28 11:02:31 +00:00
|
|
|
target_os = "solaris",
|
2015-09-21 17:16:24 +00:00
|
|
|
all(target_os = "netbsd", not(target_vendor = "rumprun")),
|
2015-01-29 07:19:28 +00:00
|
|
|
target_os = "openbsd"))]
|
2014-10-23 05:29:41 +00:00
|
|
|
mod imp {
|
2014-11-24 03:21:17 +00:00
|
|
|
use super::Handler;
|
|
|
|
use mem;
|
|
|
|
use ptr;
|
2016-02-15 14:45:52 +00:00
|
|
|
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
|
2015-11-03 00:23:22 +00:00
|
|
|
use libc::{sigaction, SIGBUS, SIG_DFL,
|
2015-12-18 12:29:49 +00:00
|
|
|
SA_SIGINFO, SA_ONSTACK, sighandler_t};
|
2014-10-23 05:29:41 +00:00
|
|
|
use libc;
|
2015-11-03 00:23:22 +00:00
|
|
|
use libc::{mmap, munmap};
|
2015-07-27 20:41:35 +00:00
|
|
|
use libc::{SIGSEGV, PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANON};
|
|
|
|
use libc::MAP_FAILED;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2014-12-07 02:34:37 +00:00
|
|
|
use sys_common::thread_info;
|
|
|
|
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2015-11-03 00:23:22 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2015-12-23 10:32:02 +00:00
|
|
|
unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
|
2015-11-03 00:23:22 +00:00
|
|
|
#[repr(C)]
|
|
|
|
struct siginfo_t {
|
2016-04-04 14:18:44 +00:00
|
|
|
a: [libc::c_int; 3], // si_signo, si_errno, si_code
|
2015-11-03 00:23:22 +00:00
|
|
|
si_addr: *mut libc::c_void,
|
|
|
|
}
|
|
|
|
|
2015-12-23 10:32:02 +00:00
|
|
|
(*(info as *const siginfo_t)).si_addr as usize
|
2015-11-03 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
2015-12-23 10:32:02 +00:00
|
|
|
unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
|
|
|
|
(*info).si_addr as usize
|
2015-11-03 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 20:41:35 +00:00
|
|
|
// Signal handler for the SIGSEGV and SIGBUS handlers. We've got guard pages
|
|
|
|
// (unmapped pages) at the end of every thread's stack, so if a thread ends
|
|
|
|
// up running into the guard page it'll trigger this handler. We want to
|
|
|
|
// detect these cases and print out a helpful error saying that the stack
|
|
|
|
// has overflowed. All other signals, however, should go back to what they
|
|
|
|
// were originally supposed to do.
|
|
|
|
//
|
|
|
|
// This handler currently exists purely to print an informative message
|
2016-01-31 23:30:32 +00:00
|
|
|
// whenever a thread overflows its stack. We then abort to exit and
|
|
|
|
// indicate a crash, but to avoid a misleading SIGSEGV that might lead
|
|
|
|
// users to believe that unsafe code has accessed an invalid pointer; the
|
|
|
|
// SIGSEGV encountered when overflowing the stack is expected and
|
|
|
|
// well-defined.
|
2015-07-27 20:41:35 +00:00
|
|
|
//
|
2016-01-31 23:30:32 +00:00
|
|
|
// If this is not a stack overflow, the handler un-registers itself and
|
|
|
|
// then returns (to allow the original signal to be delivered again).
|
|
|
|
// Returning from this kind of signal handler is technically not defined
|
|
|
|
// to work when reading the POSIX spec strictly, but in practice it turns
|
|
|
|
// out many large systems and all implementations allow returning from a
|
|
|
|
// signal handler to work. For a more detailed explanation see the
|
|
|
|
// comments on #26458.
|
2014-10-23 05:29:41 +00:00
|
|
|
unsafe extern fn signal_handler(signum: libc::c_int,
|
2015-11-03 00:23:22 +00:00
|
|
|
info: *mut libc::siginfo_t,
|
2015-07-27 20:41:35 +00:00
|
|
|
_data: *mut libc::c_void) {
|
2015-12-18 12:29:49 +00:00
|
|
|
use sys_common::util::report_overflow;
|
|
|
|
|
Use a range to identify SIGSEGV in stack guards
Previously, the `guard::init()` and `guard::current()` functions were
returning a `usize` address representing the top of the stack guard,
respectively for the main thread and for spawned threads. The `SIGSEGV`
handler on `unix` targets checked if a fault was within one page below
that address, if so reporting it as a stack overflow.
Now `unix` targets report a `Range<usize>` representing the guard
memory, so it can cover arbitrary guard sizes. Non-`unix` targets which
always return `None` for guards now do so with `Option<!>`, so they
don't pay any overhead.
For `linux-gnu` in particular, the previous guard upper-bound was
`stackaddr + guardsize`, as the protected memory was *inside* the stack.
This was a glibc bug, and starting from 2.27 they are moving the guard
*past* the end of the stack. However, there's no simple way for us to
know where the guard page actually lies, so now we declare it as the
whole range of `stackaddr ± guardsize`, and any fault therein will be
called a stack overflow. This fixes #47863.
2018-01-31 19:41:29 +00:00
|
|
|
let guard = thread_info::stack_guard().unwrap_or(0..0);
|
2015-12-23 10:32:02 +00:00
|
|
|
let addr = siginfo_si_addr(info);
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2015-07-27 20:41:35 +00:00
|
|
|
// If the faulting address is within the guard page, then we print a
|
2016-01-31 23:30:32 +00:00
|
|
|
// message saying so and abort.
|
Use a range to identify SIGSEGV in stack guards
Previously, the `guard::init()` and `guard::current()` functions were
returning a `usize` address representing the top of the stack guard,
respectively for the main thread and for spawned threads. The `SIGSEGV`
handler on `unix` targets checked if a fault was within one page below
that address, if so reporting it as a stack overflow.
Now `unix` targets report a `Range<usize>` representing the guard
memory, so it can cover arbitrary guard sizes. Non-`unix` targets which
always return `None` for guards now do so with `Option<!>`, so they
don't pay any overhead.
For `linux-gnu` in particular, the previous guard upper-bound was
`stackaddr + guardsize`, as the protected memory was *inside* the stack.
This was a glibc bug, and starting from 2.27 they are moving the guard
*past* the end of the stack. However, there's no simple way for us to
know where the guard page actually lies, so now we declare it as the
whole range of `stackaddr ± guardsize`, and any fault therein will be
called a stack overflow. This fixes #47863.
2018-01-31 19:41:29 +00:00
|
|
|
if guard.start <= addr && addr < guard.end {
|
2015-07-27 20:41:35 +00:00
|
|
|
report_overflow();
|
2016-01-31 23:30:32 +00:00
|
|
|
rtabort!("stack overflow");
|
|
|
|
} else {
|
|
|
|
// Unregister ourselves by reverting back to the default behavior.
|
|
|
|
let mut action: sigaction = mem::zeroed();
|
|
|
|
action.sa_sigaction = SIG_DFL;
|
|
|
|
sigaction(signum, &action, ptr::null_mut());
|
|
|
|
|
|
|
|
// See comment above for why this function returns.
|
2014-12-07 02:34:37 +00:00
|
|
|
}
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 06:49:50 +00:00
|
|
|
static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
let handler = make_handler();
|
|
|
|
MAIN_ALTSTACK = handler._data;
|
|
|
|
mem::forget(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn cleanup() {
|
|
|
|
Handler { _data: MAIN_ALTSTACK };
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:21:39 +00:00
|
|
|
unsafe fn get_stackp() -> *mut libc::c_void {
|
2016-01-28 00:46:45 +00:00
|
|
|
let stackp = mmap(ptr::null_mut(),
|
|
|
|
SIGSTKSZ,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANON,
|
|
|
|
-1,
|
|
|
|
0);
|
|
|
|
if stackp == MAP_FAILED {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("failed to allocate an alternative stack");
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
2016-02-03 02:21:39 +00:00
|
|
|
stackp
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "bitrig",
|
|
|
|
target_os = "netbsd",
|
2016-02-21 22:57:24 +00:00
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "solaris"))]
|
2016-02-03 02:21:39 +00:00
|
|
|
unsafe fn get_stack() -> libc::stack_t {
|
|
|
|
libc::stack_t { ss_sp: get_stackp(), ss_flags: 0, ss_size: SIGSTKSZ }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "freebsd",
|
|
|
|
target_os = "dragonfly"))]
|
|
|
|
unsafe fn get_stack() -> libc::stack_t {
|
|
|
|
libc::stack_t { ss_sp: get_stackp() as *mut i8, ss_flags: 0, ss_size: SIGSTKSZ }
|
2016-01-27 01:37:18 +00:00
|
|
|
}
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2016-01-27 01:37:18 +00:00
|
|
|
pub unsafe fn make_handler() -> Handler {
|
2016-02-15 14:45:52 +00:00
|
|
|
let mut stack = mem::zeroed();
|
|
|
|
sigaltstack(ptr::null(), &mut stack);
|
|
|
|
// Configure alternate signal stack, if one is not already set.
|
|
|
|
if stack.ss_flags & SS_DISABLE != 0 {
|
|
|
|
stack = get_stack();
|
|
|
|
sigaltstack(&stack, ptr::null_mut());
|
|
|
|
Handler { _data: stack.ss_sp as *mut libc::c_void }
|
|
|
|
} else {
|
|
|
|
Handler { _data: ptr::null_mut() }
|
|
|
|
}
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn drop_handler(handler: &mut Handler) {
|
2016-02-15 14:45:52 +00:00
|
|
|
if !handler._data.is_null() {
|
|
|
|
let stack = libc::stack_t {
|
|
|
|
ss_sp: ptr::null_mut(),
|
|
|
|
ss_flags: SS_DISABLE,
|
2017-03-12 18:13:35 +00:00
|
|
|
// Workaround for bug in macOS implementation of sigaltstack
|
2016-02-15 14:45:52 +00:00
|
|
|
// UNIX2003 which returns ENOMEM when disabling a stack while
|
|
|
|
// passing ss_size smaller than MINSIGSTKSZ. According to POSIX
|
|
|
|
// both ss_sp and ss_size should be ignored in this case.
|
|
|
|
ss_size: SIGSTKSZ,
|
|
|
|
};
|
|
|
|
sigaltstack(&stack, ptr::null_mut());
|
|
|
|
munmap(handler._data, SIGSTKSZ);
|
|
|
|
}
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "linux",
|
2015-01-29 07:19:28 +00:00
|
|
|
target_os = "macos",
|
2015-01-17 07:51:04 +00:00
|
|
|
target_os = "bitrig",
|
2015-11-01 21:56:31 +00:00
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
2016-01-28 11:02:31 +00:00
|
|
|
target_os = "solaris",
|
2015-09-21 17:16:24 +00:00
|
|
|
all(target_os = "netbsd", not(target_vendor = "rumprun")),
|
2015-01-29 07:19:28 +00:00
|
|
|
target_os = "openbsd")))]
|
2014-10-23 05:29:41 +00:00
|
|
|
mod imp {
|
2015-09-03 18:33:38 +00:00
|
|
|
use ptr;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
pub unsafe fn init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn cleanup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn make_handler() -> super::Handler {
|
2015-09-03 06:49:50 +00:00
|
|
|
super::Handler { _data: ptr::null_mut() }
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn drop_handler(_handler: &mut super::Handler) {
|
|
|
|
}
|
|
|
|
}
|