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.
13 use self::imp::{make_handler, drop_handler};
15 pub use self::imp::{init, cleanup};
18 _data: *mut libc::c_void
22 pub unsafe fn new() -> Handler {
27 impl Drop for Handler {
35 #[cfg(any(target_os = "linux", target_os = "macos"))]
38 use sys_common::stack;
41 use rt::util::report_overflow;
45 use self::signal::{siginfo, sigaction, SIGBUS, SIG_DFL,
46 SA_SIGINFO, SA_ONSTACK, sigaltstack,
49 use libc::funcs::posix88::mman::{mmap, munmap};
50 use libc::consts::os::posix88::{SIGSEGV,
57 use sys_common::thread_info;
60 // This is initialized in init() and only read from after
61 static mut PAGE_SIZE: uint = 0;
64 unsafe extern fn signal_handler(signum: libc::c_int,
66 _data: *mut libc::c_void) {
68 // We can not return from a SIGSEGV or SIGBUS signal.
69 // See: https://www.gnu.org/software/libc/manual/html_node/Handler-Returns.html
71 unsafe fn term(signum: libc::c_int) -> ! {
72 use core::mem::transmute;
74 signal(signum, transmute(SIG_DFL));
79 // We're calling into functions with stack checks
80 stack::record_sp_limit(0);
82 let guard = thread_info::stack_guard();
83 let addr = (*info).si_addr as uint;
85 if guard == 0 || addr < guard - PAGE_SIZE || addr >= guard {
94 static mut MAIN_ALTSTACK: *mut libc::c_void = 0 as *mut libc::c_void;
96 pub unsafe fn init() {
97 let psize = libc::sysconf(libc::consts::os::sysconf::_SC_PAGESIZE);
99 panic!("failed to get page size");
102 PAGE_SIZE = psize as uint;
104 let mut action: sigaction = mem::zeroed();
105 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
106 action.sa_sigaction = signal_handler as sighandler_t;
107 sigaction(SIGSEGV, &action, ptr::null_mut());
108 sigaction(SIGBUS, &action, ptr::null_mut());
110 let handler = make_handler();
111 MAIN_ALTSTACK = handler._data;
112 mem::forget(handler);
115 pub unsafe fn cleanup() {
116 Handler { _data: MAIN_ALTSTACK };
119 pub unsafe fn make_handler() -> Handler {
120 let alt_stack = mmap(ptr::null_mut(),
122 PROT_READ | PROT_WRITE,
123 MAP_PRIVATE | MAP_ANON,
126 if alt_stack == MAP_FAILED {
127 panic!("failed to allocate an alternative stack");
130 let mut stack: sigaltstack = mem::zeroed();
132 stack.ss_sp = alt_stack;
134 stack.ss_size = SIGSTKSZ;
136 sigaltstack(&stack, ptr::null_mut());
138 Handler { _data: alt_stack }
141 pub unsafe fn drop_handler(handler: &mut Handler) {
142 munmap(handler._data, SIGSTKSZ);
145 type sighandler_t = *mut libc::c_void;
147 #[cfg(any(all(target_os = "linux", target_arch = "x86"), // may not match
148 all(target_os = "linux", target_arch = "x86_64"),
149 all(target_os = "linux", target_arch = "arm"), // may not match
150 all(target_os = "linux", target_arch = "aarch64"),
151 all(target_os = "linux", target_arch = "mips"), // may not match
152 all(target_os = "linux", target_arch = "mipsel"), // may not match
153 target_os = "android"))] // may not match
156 use super::sighandler_t;
158 pub static SA_ONSTACK: libc::c_int = 0x08000000;
159 pub static SA_SIGINFO: libc::c_int = 0x00000004;
160 pub static SIGBUS: libc::c_int = 7;
162 pub static SIGSTKSZ: libc::size_t = 8192;
164 pub const SIG_DFL: sighandler_t = 0i as sighandler_t;
166 // This definition is not as accurate as it could be, {si_addr} is
167 // actually a giant union. Currently we're only interested in that field,
171 si_signo: libc::c_int,
172 si_errno: libc::c_int,
173 si_code: libc::c_int,
174 pub si_addr: *mut libc::c_void
178 pub struct sigaction {
179 pub sa_sigaction: sighandler_t,
180 pub sa_mask: sigset_t,
181 pub sa_flags: libc::c_int,
182 sa_restorer: *mut libc::c_void,
185 #[cfg(any(all(stage0, target_word_size = "32"),
186 all(not(stage0), target_pointer_width = "32")))]
188 pub struct sigset_t {
189 __val: [libc::c_ulong; 32],
191 #[cfg(any(all(stage0, target_word_size = "64"),
192 all(not(stage0), target_pointer_width = "64")))]
194 pub struct sigset_t {
195 __val: [libc::c_ulong; 16],
199 pub struct sigaltstack {
200 pub ss_sp: *mut libc::c_void,
201 pub ss_flags: libc::c_int,
202 pub ss_size: libc::size_t
207 #[cfg(target_os = "macos")]
210 use super::sighandler_t;
212 pub const SA_ONSTACK: libc::c_int = 0x0001;
213 pub const SA_SIGINFO: libc::c_int = 0x0040;
214 pub const SIGBUS: libc::c_int = 10;
216 pub const SIGSTKSZ: libc::size_t = 131072;
218 pub const SIG_DFL: sighandler_t = 0i as sighandler_t;
220 pub type sigset_t = u32;
222 // This structure has more fields, but we're not all that interested in
226 pub si_signo: libc::c_int,
227 pub si_errno: libc::c_int,
228 pub si_code: libc::c_int,
229 pub pid: libc::pid_t,
230 pub uid: libc::uid_t,
231 pub status: libc::c_int,
232 pub si_addr: *mut libc::c_void
236 pub struct sigaltstack {
237 pub ss_sp: *mut libc::c_void,
238 pub ss_size: libc::size_t,
239 pub ss_flags: libc::c_int
243 pub struct sigaction {
244 pub sa_sigaction: sighandler_t,
245 pub sa_mask: sigset_t,
246 pub sa_flags: libc::c_int,
251 pub fn signal(signum: libc::c_int, handler: sighandler_t) -> sighandler_t;
252 pub fn raise(signum: libc::c_int) -> libc::c_int;
254 pub fn sigaction(signum: libc::c_int,
255 act: *const sigaction,
256 oldact: *mut sigaction) -> libc::c_int;
258 pub fn sigaltstack(ss: *const sigaltstack,
259 oss: *mut sigaltstack) -> libc::c_int;
263 #[cfg(not(any(target_os = "linux",
264 target_os = "macos")))]
268 pub unsafe fn init() {
271 pub unsafe fn cleanup() {
274 pub unsafe fn make_handler() -> super::Handler {
275 super::Handler { _data: 0i as *mut libc::c_void }
278 pub unsafe fn drop_handler(_handler: &mut super::Handler) {