1 #![cfg_attr(test, allow(dead_code))]
3 use self::imp::{drop_handler, make_handler};
5 pub use self::imp::cleanup;
6 pub use self::imp::init;
9 _data: *mut libc::c_void,
13 pub unsafe fn new() -> Handler {
17 fn null() -> Handler {
18 Handler { _data: crate::ptr::null_mut() }
22 impl Drop for Handler {
33 target_os = "dragonfly",
34 target_os = "freebsd",
35 target_os = "solaris",
36 target_os = "illumos",
37 all(target_os = "netbsd", not(target_vendor = "rumprun")),
46 use libc::{mmap, munmap};
47 use libc::{sigaction, sighandler_t, SA_ONSTACK, SA_SIGINFO, SIGBUS, SIG_DFL};
48 use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
49 use libc::{MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, SIGSEGV};
51 use crate::sys::unix::os::page_size;
52 use crate::sys_common::thread_info;
54 #[cfg(any(target_os = "linux", target_os = "android"))]
55 unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
58 a: [libc::c_int; 3], // si_signo, si_errno, si_code
59 si_addr: *mut libc::c_void,
62 (*(info as *const siginfo_t)).si_addr as usize
65 #[cfg(not(any(target_os = "linux", target_os = "android")))]
66 unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
67 (*info).si_addr as usize
70 // Signal handler for the SIGSEGV and SIGBUS handlers. We've got guard pages
71 // (unmapped pages) at the end of every thread's stack, so if a thread ends
72 // up running into the guard page it'll trigger this handler. We want to
73 // detect these cases and print out a helpful error saying that the stack
74 // has overflowed. All other signals, however, should go back to what they
75 // were originally supposed to do.
77 // This handler currently exists purely to print an informative message
78 // whenever a thread overflows its stack. We then abort to exit and
79 // indicate a crash, but to avoid a misleading SIGSEGV that might lead
80 // users to believe that unsafe code has accessed an invalid pointer; the
81 // SIGSEGV encountered when overflowing the stack is expected and
84 // If this is not a stack overflow, the handler un-registers itself and
85 // then returns (to allow the original signal to be delivered again).
86 // Returning from this kind of signal handler is technically not defined
87 // to work when reading the POSIX spec strictly, but in practice it turns
88 // out many large systems and all implementations allow returning from a
89 // signal handler to work. For a more detailed explanation see the
90 // comments on #26458.
91 unsafe extern "C" fn signal_handler(
93 info: *mut libc::siginfo_t,
94 _data: *mut libc::c_void,
96 use crate::sys_common::util::report_overflow;
98 let guard = thread_info::stack_guard().unwrap_or(0..0);
99 let addr = siginfo_si_addr(info);
101 // If the faulting address is within the guard page, then we print a
102 // message saying so and abort.
103 if guard.start <= addr && addr < guard.end {
105 rtabort!("stack overflow");
107 // Unregister ourselves by reverting back to the default behavior.
108 let mut action: sigaction = mem::zeroed();
109 action.sa_sigaction = SIG_DFL;
110 sigaction(signum, &action, ptr::null_mut());
112 // See comment above for why this function returns.
116 static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
117 static mut NEED_ALTSTACK: bool = false;
119 pub unsafe fn init() {
120 let mut action: sigaction = mem::zeroed();
121 for &signal in &[SIGSEGV, SIGBUS] {
122 sigaction(signal, ptr::null_mut(), &mut action);
123 // Configure our signal handler if one is not already set.
124 if action.sa_sigaction == SIG_DFL {
125 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
126 action.sa_sigaction = signal_handler as sighandler_t;
127 sigaction(signal, &action, ptr::null_mut());
128 NEED_ALTSTACK = true;
132 let handler = make_handler();
133 MAIN_ALTSTACK = handler._data;
134 mem::forget(handler);
137 pub unsafe fn cleanup() {
138 Handler { _data: MAIN_ALTSTACK };
141 unsafe fn get_stackp() -> *mut libc::c_void {
144 SIGSTKSZ + page_size(),
145 PROT_READ | PROT_WRITE,
146 MAP_PRIVATE | MAP_ANON,
150 if stackp == MAP_FAILED {
151 panic!("failed to allocate an alternative stack");
153 let guard_result = libc::mprotect(stackp, page_size(), PROT_NONE);
154 if guard_result != 0 {
155 panic!("failed to set up alternative stack guard page");
157 stackp.add(page_size())
163 target_os = "freebsd",
164 target_os = "netbsd",
165 target_os = "openbsd",
166 target_os = "solaris",
167 target_os = "illumos"
169 unsafe fn get_stack() -> libc::stack_t {
170 libc::stack_t { ss_sp: get_stackp(), ss_flags: 0, ss_size: SIGSTKSZ }
173 #[cfg(target_os = "dragonfly")]
174 unsafe fn get_stack() -> libc::stack_t {
175 libc::stack_t { ss_sp: get_stackp() as *mut i8, ss_flags: 0, ss_size: SIGSTKSZ }
178 pub unsafe fn make_handler() -> Handler {
180 return Handler::null();
182 let mut stack = mem::zeroed();
183 sigaltstack(ptr::null(), &mut stack);
184 // Configure alternate signal stack, if one is not already set.
185 if stack.ss_flags & SS_DISABLE != 0 {
187 sigaltstack(&stack, ptr::null_mut());
188 Handler { _data: stack.ss_sp as *mut libc::c_void }
194 pub unsafe fn drop_handler(handler: &mut Handler) {
195 if !handler._data.is_null() {
196 let stack = libc::stack_t {
197 ss_sp: ptr::null_mut(),
198 ss_flags: SS_DISABLE,
199 // Workaround for bug in macOS implementation of sigaltstack
200 // UNIX2003 which returns ENOMEM when disabling a stack while
201 // passing ss_size smaller than MINSIGSTKSZ. According to POSIX
202 // both ss_sp and ss_size should be ignored in this case.
205 sigaltstack(&stack, ptr::null_mut());
206 // We know from `get_stackp` that the alternate stack we installed is part of a mapping
207 // that started one page earlier, so walk back a page and unmap from there.
208 munmap(handler._data.sub(page_size()), SIGSTKSZ + page_size());
216 target_os = "dragonfly",
217 target_os = "freebsd",
218 target_os = "solaris",
219 target_os = "illumos",
220 all(target_os = "netbsd", not(target_vendor = "rumprun")),
221 target_os = "openbsd"
224 pub unsafe fn init() {}
226 pub unsafe fn cleanup() {}
228 pub unsafe fn make_handler() -> super::Handler {
229 super::Handler::null()
232 pub unsafe fn drop_handler(_handler: &mut super::Handler) {}