]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/stack_overflow.rs
add inline attributes to stage 0 methods
[rust.git] / src / libstd / sys / unix / stack_overflow.rs
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.
4 //
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.
10
11 #![cfg_attr(test, allow(dead_code))]
12
13 use libc;
14 use self::imp::{make_handler, drop_handler};
15
16 pub use self::imp::cleanup;
17 pub use self::imp::init;
18
19 pub struct Handler {
20     _data: *mut libc::c_void
21 }
22
23 impl Handler {
24     pub unsafe fn new() -> Handler {
25         make_handler()
26     }
27 }
28
29 impl Drop for Handler {
30     fn drop(&mut self) {
31         unsafe {
32             drop_handler(self);
33         }
34     }
35 }
36
37 #[cfg(any(target_os = "linux",
38           target_os = "macos",
39           target_os = "bitrig",
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"))]
45 mod imp {
46     use super::Handler;
47     use mem;
48     use ptr;
49     use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
50     use libc::{sigaction, SIGBUS, SIG_DFL,
51                SA_SIGINFO, SA_ONSTACK, sighandler_t};
52     use libc;
53     use libc::{mmap, munmap};
54     use libc::{SIGSEGV, PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANON};
55     use libc::MAP_FAILED;
56
57     use sys_common::thread_info;
58
59
60     // This is initialized in init() and only read from after
61     static mut PAGE_SIZE: usize = 0;
62
63     #[cfg(any(target_os = "linux", target_os = "android"))]
64     unsafe fn siginfo_si_addr(info: *mut libc::siginfo_t) -> usize {
65         #[repr(C)]
66         struct siginfo_t {
67             a: [libc::c_int; 3], // si_signo, si_errno, si_code
68             si_addr: *mut libc::c_void,
69         }
70
71         (*(info as *const siginfo_t)).si_addr as usize
72     }
73
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
77     }
78
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.
85     //
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
91     // well-defined.
92     //
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;
104
105         let guard = thread_info::stack_guard().unwrap_or(0);
106         let addr = siginfo_si_addr(info);
107
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 {
111             report_overflow();
112             rtabort!("stack overflow");
113         } else {
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());
118
119             // See comment above for why this function returns.
120         }
121     }
122
123     static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
124
125     pub unsafe fn init() {
126         PAGE_SIZE = ::sys::os::page_size();
127
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());
133
134         let handler = make_handler();
135         MAIN_ALTSTACK = handler._data;
136         mem::forget(handler);
137     }
138
139     pub unsafe fn cleanup() {
140         Handler { _data: MAIN_ALTSTACK };
141     }
142
143     unsafe fn get_stackp() -> *mut libc::c_void {
144         let stackp = mmap(ptr::null_mut(),
145                           SIGSTKSZ,
146                           PROT_READ | PROT_WRITE,
147                           MAP_PRIVATE | MAP_ANON,
148                           -1,
149                           0);
150         if stackp == MAP_FAILED {
151             panic!("failed to allocate an alternative stack");
152         }
153         stackp
154     }
155
156     #[cfg(any(target_os = "linux",
157               target_os = "macos",
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 }
164     }
165
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 }
170     }
171
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 {
177             stack = get_stack();
178             sigaltstack(&stack, ptr::null_mut());
179             Handler { _data: stack.ss_sp as *mut libc::c_void }
180         } else {
181             Handler { _data: ptr::null_mut() }
182         }
183     }
184
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.
194                 ss_size: SIGSTKSZ,
195             };
196             sigaltstack(&stack, ptr::null_mut());
197             munmap(handler._data, SIGSTKSZ);
198         }
199     }
200 }
201
202 #[cfg(not(any(target_os = "linux",
203               target_os = "macos",
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")))]
210 mod imp {
211     use ptr;
212
213     pub unsafe fn init() {
214     }
215
216     pub unsafe fn cleanup() {
217     }
218
219     pub unsafe fn make_handler() -> super::Handler {
220         super::Handler { _data: ptr::null_mut() }
221     }
222
223     pub unsafe fn drop_handler(_handler: &mut super::Handler) {
224     }
225 }