1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![cfg_attr(test, allow(dead_code))]
14 use self::imp::{make_handler, drop_handler};
16 pub use self::imp::cleanup;
17 pub use self::imp::init;
20 _data: *mut libc::c_void
24 pub unsafe fn new() -> Handler {
29 impl Drop for Handler {
37 #[cfg(any(target_os = "linux",
40 target_os = "dragonfly",
41 target_os = "freebsd",
42 target_os = "solaris",
43 all(target_os = "netbsd", not(target_vendor = "rumprun")),
44 target_os = "openbsd"))]
49 use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
50 use libc::{sigaction, SIGBUS, SIG_DFL,
51 SA_SIGINFO, SA_ONSTACK, sighandler_t};
53 use libc::{mmap, munmap};
54 use libc::{SIGSEGV, PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANON};
57 use sys_common::thread_info;
60 // This is initialized in init() and only read from after
61 static mut PAGE_SIZE: usize = 0;
63 #[cfg(any(target_os = "linux", target_os = "android"))]
64 unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
67 a: [libc::c_int; 3], // si_signo, si_errno, si_code
68 si_addr: *mut libc::c_void,
71 (*(info as *const siginfo_t)).si_addr as usize
74 #[cfg(not(any(target_os = "linux", target_os = "android")))]
75 unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
76 (*info).si_addr as usize
79 // Signal handler for the SIGSEGV and SIGBUS handlers. We've got guard pages
80 // (unmapped pages) at the end of every thread's stack, so if a thread ends
81 // up running into the guard page it'll trigger this handler. We want to
82 // detect these cases and print out a helpful error saying that the stack
83 // has overflowed. All other signals, however, should go back to what they
84 // were originally supposed to do.
86 // This handler currently exists purely to print an informative message
87 // whenever a thread overflows its stack. We then abort to exit and
88 // indicate a crash, but to avoid a misleading SIGSEGV that might lead
89 // users to believe that unsafe code has accessed an invalid pointer; the
90 // SIGSEGV encountered when overflowing the stack is expected and
93 // If this is not a stack overflow, the handler un-registers itself and
94 // then returns (to allow the original signal to be delivered again).
95 // Returning from this kind of signal handler is technically not defined
96 // to work when reading the POSIX spec strictly, but in practice it turns
97 // out many large systems and all implementations allow returning from a
98 // signal handler to work. For a more detailed explanation see the
99 // comments on #26458.
100 unsafe extern fn signal_handler(signum: libc::c_int,
101 info: *mut libc::siginfo_t,
102 _data: *mut libc::c_void) {
103 use sys_common::util::report_overflow;
105 let guard = thread_info::stack_guard().unwrap_or(0);
106 let addr = siginfo_si_addr(info);
108 // If the faulting address is within the guard page, then we print a
109 // message saying so and abort.
110 if guard != 0 && guard - PAGE_SIZE <= addr && addr < guard {
112 rtabort!("stack overflow");
114 // Unregister ourselves by reverting back to the default behavior.
115 let mut action: sigaction = mem::zeroed();
116 action.sa_sigaction = SIG_DFL;
117 sigaction(signum, &action, ptr::null_mut());
119 // See comment above for why this function returns.
123 static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
125 pub unsafe fn init() {
126 PAGE_SIZE = ::sys::os::page_size();
128 let mut action: sigaction = mem::zeroed();
129 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
130 action.sa_sigaction = signal_handler as sighandler_t;
131 sigaction(SIGSEGV, &action, ptr::null_mut());
132 sigaction(SIGBUS, &action, ptr::null_mut());
134 let handler = make_handler();
135 MAIN_ALTSTACK = handler._data;
136 mem::forget(handler);
139 pub unsafe fn cleanup() {
140 Handler { _data: MAIN_ALTSTACK };
143 unsafe fn get_stackp() -> *mut libc::c_void {
144 let stackp = mmap(ptr::null_mut(),
146 PROT_READ | PROT_WRITE,
147 MAP_PRIVATE | MAP_ANON,
150 if stackp == MAP_FAILED {
151 panic!("failed to allocate an alternative stack");
156 #[cfg(any(target_os = "linux",
158 target_os = "bitrig",
159 target_os = "netbsd",
160 target_os = "openbsd",
161 target_os = "solaris"))]
162 unsafe fn get_stack() -> libc::stack_t {
163 libc::stack_t { ss_sp: get_stackp(), ss_flags: 0, ss_size: SIGSTKSZ }
166 #[cfg(any(target_os = "freebsd",
167 target_os = "dragonfly"))]
168 unsafe fn get_stack() -> libc::stack_t {
169 libc::stack_t { ss_sp: get_stackp() as *mut i8, ss_flags: 0, ss_size: SIGSTKSZ }
172 pub unsafe fn make_handler() -> Handler {
173 let mut stack = mem::zeroed();
174 sigaltstack(ptr::null(), &mut stack);
175 // Configure alternate signal stack, if one is not already set.
176 if stack.ss_flags & SS_DISABLE != 0 {
178 sigaltstack(&stack, ptr::null_mut());
179 Handler { _data: stack.ss_sp as *mut libc::c_void }
181 Handler { _data: ptr::null_mut() }
185 pub unsafe fn drop_handler(handler: &mut Handler) {
186 if !handler._data.is_null() {
187 let stack = libc::stack_t {
188 ss_sp: ptr::null_mut(),
189 ss_flags: SS_DISABLE,
190 // Workaround for bug in MacOS implementation of sigaltstack
191 // UNIX2003 which returns ENOMEM when disabling a stack while
192 // passing ss_size smaller than MINSIGSTKSZ. According to POSIX
193 // both ss_sp and ss_size should be ignored in this case.
196 sigaltstack(&stack, ptr::null_mut());
197 munmap(handler._data, SIGSTKSZ);
202 #[cfg(not(any(target_os = "linux",
204 target_os = "bitrig",
205 target_os = "dragonfly",
206 target_os = "freebsd",
207 target_os = "solaris",
208 all(target_os = "netbsd", not(target_vendor = "rumprun")),
209 target_os = "openbsd")))]
213 pub unsafe fn init() {
216 pub unsafe fn cleanup() {
219 pub unsafe fn make_handler() -> super::Handler {
220 super::Handler { _data: ptr::null_mut() }
223 pub unsafe fn drop_handler(_handler: &mut super::Handler) {