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 #[cfg(any(target_os = "linux", target_os = "android"))]
61 unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
64 a: [libc::c_int; 3], // si_signo, si_errno, si_code
65 si_addr: *mut libc::c_void,
68 (*(info as *const siginfo_t)).si_addr as usize
71 #[cfg(not(any(target_os = "linux", target_os = "android")))]
72 unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
73 (*info).si_addr as usize
76 // Signal handler for the SIGSEGV and SIGBUS handlers. We've got guard pages
77 // (unmapped pages) at the end of every thread's stack, so if a thread ends
78 // up running into the guard page it'll trigger this handler. We want to
79 // detect these cases and print out a helpful error saying that the stack
80 // has overflowed. All other signals, however, should go back to what they
81 // were originally supposed to do.
83 // This handler currently exists purely to print an informative message
84 // whenever a thread overflows its stack. We then abort to exit and
85 // indicate a crash, but to avoid a misleading SIGSEGV that might lead
86 // users to believe that unsafe code has accessed an invalid pointer; the
87 // SIGSEGV encountered when overflowing the stack is expected and
90 // If this is not a stack overflow, the handler un-registers itself and
91 // then returns (to allow the original signal to be delivered again).
92 // Returning from this kind of signal handler is technically not defined
93 // to work when reading the POSIX spec strictly, but in practice it turns
94 // out many large systems and all implementations allow returning from a
95 // signal handler to work. For a more detailed explanation see the
96 // comments on #26458.
97 unsafe extern fn signal_handler(signum: libc::c_int,
98 info: *mut libc::siginfo_t,
99 _data: *mut libc::c_void) {
100 use sys_common::util::report_overflow;
102 let guard = thread_info::stack_guard().unwrap_or(0..0);
103 let addr = siginfo_si_addr(info);
105 // If the faulting address is within the guard page, then we print a
106 // message saying so and abort.
107 if guard.start <= addr && addr < guard.end {
109 rtabort!("stack overflow");
111 // Unregister ourselves by reverting back to the default behavior.
112 let mut action: sigaction = mem::zeroed();
113 action.sa_sigaction = SIG_DFL;
114 sigaction(signum, &action, ptr::null_mut());
116 // See comment above for why this function returns.
120 static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
122 pub unsafe fn init() {
123 let mut action: sigaction = mem::zeroed();
124 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
125 action.sa_sigaction = signal_handler as sighandler_t;
126 sigaction(SIGSEGV, &action, ptr::null_mut());
127 sigaction(SIGBUS, &action, ptr::null_mut());
129 let handler = make_handler();
130 MAIN_ALTSTACK = handler._data;
131 mem::forget(handler);
134 pub unsafe fn cleanup() {
135 Handler { _data: MAIN_ALTSTACK };
138 unsafe fn get_stackp() -> *mut libc::c_void {
139 let stackp = mmap(ptr::null_mut(),
141 PROT_READ | PROT_WRITE,
142 MAP_PRIVATE | MAP_ANON,
145 if stackp == MAP_FAILED {
146 panic!("failed to allocate an alternative stack");
151 #[cfg(any(target_os = "linux",
153 target_os = "bitrig",
154 target_os = "netbsd",
155 target_os = "openbsd",
156 target_os = "solaris"))]
157 unsafe fn get_stack() -> libc::stack_t {
158 libc::stack_t { ss_sp: get_stackp(), ss_flags: 0, ss_size: SIGSTKSZ }
161 #[cfg(any(target_os = "freebsd",
162 target_os = "dragonfly"))]
163 unsafe fn get_stack() -> libc::stack_t {
164 libc::stack_t { ss_sp: get_stackp() as *mut i8, ss_flags: 0, ss_size: SIGSTKSZ }
167 pub unsafe fn make_handler() -> Handler {
168 let mut stack = mem::zeroed();
169 sigaltstack(ptr::null(), &mut stack);
170 // Configure alternate signal stack, if one is not already set.
171 if stack.ss_flags & SS_DISABLE != 0 {
173 sigaltstack(&stack, ptr::null_mut());
174 Handler { _data: stack.ss_sp as *mut libc::c_void }
176 Handler { _data: ptr::null_mut() }
180 pub unsafe fn drop_handler(handler: &mut Handler) {
181 if !handler._data.is_null() {
182 let stack = libc::stack_t {
183 ss_sp: ptr::null_mut(),
184 ss_flags: SS_DISABLE,
185 // Workaround for bug in macOS implementation of sigaltstack
186 // UNIX2003 which returns ENOMEM when disabling a stack while
187 // passing ss_size smaller than MINSIGSTKSZ. According to POSIX
188 // both ss_sp and ss_size should be ignored in this case.
191 sigaltstack(&stack, ptr::null_mut());
192 munmap(handler._data, SIGSTKSZ);
197 #[cfg(not(any(target_os = "linux",
199 target_os = "bitrig",
200 target_os = "dragonfly",
201 target_os = "freebsd",
202 target_os = "solaris",
203 all(target_os = "netbsd", not(target_vendor = "rumprun")),
204 target_os = "openbsd")))]
208 pub unsafe fn init() {
211 pub unsafe fn cleanup() {
214 pub unsafe fn make_handler() -> super::Handler {
215 super::Handler { _data: ptr::null_mut() }
218 pub unsafe fn drop_handler(_handler: &mut super::Handler) {