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.
|
|
|
|
|
|
|
|
use libc;
|
2014-11-24 03:21:17 +00:00
|
|
|
use core::prelude::*;
|
|
|
|
use self::imp::{make_handler, drop_handler};
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2014-11-24 03:21:17 +00:00
|
|
|
pub use self::imp::{init, cleanup};
|
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-07-01 03:37:11 +00:00
|
|
|
target_os = "netbsd",
|
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 sys_common::stack;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2014-11-24 03:21:17 +00:00
|
|
|
use super::Handler;
|
|
|
|
use rt::util::report_overflow;
|
|
|
|
use mem;
|
|
|
|
use ptr;
|
|
|
|
use intrinsics;
|
2015-05-23 22:07:52 +00:00
|
|
|
use sys::c::{siginfo, sigaction, SIGBUS, SIG_DFL,
|
|
|
|
SA_SIGINFO, SA_ONSTACK, sigaltstack,
|
|
|
|
SIGSTKSZ, sighandler_t, raise};
|
2014-10-23 05:29:41 +00:00
|
|
|
use libc;
|
|
|
|
use libc::funcs::posix88::mman::{mmap, munmap};
|
2015-05-23 22:07:52 +00:00
|
|
|
use libc::funcs::posix01::signal::signal;
|
2014-10-23 05:29:41 +00:00
|
|
|
use libc::consts::os::posix88::{SIGSEGV,
|
|
|
|
PROT_READ,
|
|
|
|
PROT_WRITE,
|
|
|
|
MAP_PRIVATE,
|
|
|
|
MAP_ANON,
|
|
|
|
MAP_FAILED};
|
|
|
|
|
2014-12-07 02:34:37 +00:00
|
|
|
use sys_common::thread_info;
|
|
|
|
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
// This is initialized in init() and only read from after
|
2015-03-26 00:06:52 +00:00
|
|
|
static mut PAGE_SIZE: usize = 0;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
#[no_stack_check]
|
|
|
|
unsafe extern fn signal_handler(signum: libc::c_int,
|
|
|
|
info: *mut siginfo,
|
|
|
|
_data: *mut libc::c_void) {
|
|
|
|
|
|
|
|
// We can not return from a SIGSEGV or SIGBUS signal.
|
|
|
|
// See: https://www.gnu.org/software/libc/manual/html_node/Handler-Returns.html
|
|
|
|
|
|
|
|
unsafe fn term(signum: libc::c_int) -> ! {
|
|
|
|
use core::mem::transmute;
|
|
|
|
|
|
|
|
signal(signum, transmute(SIG_DFL));
|
|
|
|
raise(signum);
|
|
|
|
intrinsics::abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're calling into functions with stack checks
|
|
|
|
stack::record_sp_limit(0);
|
|
|
|
|
2015-04-15 19:27:05 +00:00
|
|
|
let guard = thread_info::stack_guard().unwrap_or(0);
|
2015-03-26 00:06:52 +00:00
|
|
|
let addr = (*info).si_addr as usize;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2014-12-07 02:34:37 +00:00
|
|
|
if guard == 0 || addr < guard - PAGE_SIZE || addr >= guard {
|
|
|
|
term(signum);
|
|
|
|
}
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2014-12-07 02:34:37 +00:00
|
|
|
report_overflow();
|
2014-10-23 05:29:41 +00:00
|
|
|
|
2014-12-07 02:34:37 +00:00
|
|
|
intrinsics::abort()
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static mut MAIN_ALTSTACK: *mut libc::c_void = 0 as *mut libc::c_void;
|
|
|
|
|
|
|
|
pub unsafe fn init() {
|
|
|
|
let psize = libc::sysconf(libc::consts::os::sysconf::_SC_PAGESIZE);
|
|
|
|
if psize == -1 {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("failed to get page size");
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
PAGE_SIZE = psize as usize;
|
2014-10-23 05:29:41 +00:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn make_handler() -> Handler {
|
|
|
|
let alt_stack = mmap(ptr::null_mut(),
|
2015-05-23 22:07:52 +00:00
|
|
|
SIGSTKSZ,
|
2014-10-23 05:29:41 +00:00
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANON,
|
|
|
|
-1,
|
|
|
|
0);
|
|
|
|
if alt_stack == 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
|
|
|
}
|
|
|
|
|
|
|
|
let mut stack: sigaltstack = mem::zeroed();
|
|
|
|
|
|
|
|
stack.ss_sp = alt_stack;
|
|
|
|
stack.ss_flags = 0;
|
|
|
|
stack.ss_size = SIGSTKSZ;
|
|
|
|
|
|
|
|
sigaltstack(&stack, ptr::null_mut());
|
|
|
|
|
|
|
|
Handler { _data: alt_stack }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn drop_handler(handler: &mut Handler) {
|
|
|
|
munmap(handler._data, SIGSTKSZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-07-01 03:37:11 +00:00
|
|
|
target_os = "netbsd",
|
2015-01-29 07:19:28 +00:00
|
|
|
target_os = "openbsd")))]
|
2014-10-23 05:29:41 +00:00
|
|
|
mod imp {
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
pub unsafe fn init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn cleanup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn make_handler() -> super::Handler {
|
2015-01-25 21:05:03 +00:00
|
|
|
super::Handler { _data: 0 as *mut libc::c_void }
|
2014-10-23 05:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn drop_handler(_handler: &mut super::Handler) {
|
|
|
|
}
|
|
|
|
}
|