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",
38 target_os
= "openbsd"))]
40 use sys_common
::stack
;
43 use rt
::util
::report_overflow
;
47 use self::signal
::{siginfo
, sigaction
, SIGBUS
, SIG_DFL
,
48 SA_SIGINFO
, SA_ONSTACK
, sigaltstack
,
51 use libc
::funcs
::posix88
::mman
::{mmap, munmap}
;
52 use libc
::consts
::os
::posix88
::{SIGSEGV
,
59 use sys_common
::thread_info
;
62 // This is initialized in init() and only read from after
63 static mut PAGE_SIZE
: usize = 0;
66 unsafe extern fn signal_handler(signum
: libc
::c_int
,
68 _data
: *mut libc
::c_void
) {
70 // We can not return from a SIGSEGV or SIGBUS signal.
71 // See: https://www.gnu.org/software/libc/manual/html_node/Handler-Returns.html
73 unsafe fn term(signum
: libc
::c_int
) -> ! {
74 use core
::mem
::transmute
;
76 signal(signum
, transmute(SIG_DFL
));
81 // We're calling into functions with stack checks
82 stack
::record_sp_limit(0);
84 let guard
= thread_info
::stack_guard().unwrap_or(0);
85 let addr
= (*info
).si_addr
as usize;
87 if guard
== 0 || addr
< guard
- PAGE_SIZE
|| addr
>= guard
{
96 static mut MAIN_ALTSTACK
: *mut libc
::c_void
= 0 as *mut libc
::c_void
;
98 pub unsafe fn init() {
99 let psize
= libc
::sysconf(libc
::consts
::os
::sysconf
::_SC_PAGESIZE
);
101 panic
!("failed to get page size");
104 PAGE_SIZE
= psize
as usize;
106 let mut action
: sigaction
= mem
::zeroed();
107 action
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
108 action
.sa_sigaction
= signal_handler
as sighandler_t
;
109 sigaction(SIGSEGV
, &action
, ptr
::null_mut());
110 sigaction(SIGBUS
, &action
, ptr
::null_mut());
112 let handler
= make_handler();
113 MAIN_ALTSTACK
= handler
._data
;
114 mem
::forget(handler
);
117 pub unsafe fn cleanup() {
118 Handler { _data: MAIN_ALTSTACK }
;
121 pub unsafe fn make_handler() -> Handler
{
122 let alt_stack
= mmap(ptr
::null_mut(),
124 PROT_READ
| PROT_WRITE
,
125 MAP_PRIVATE
| MAP_ANON
,
128 if alt_stack
== MAP_FAILED
{
129 panic
!("failed to allocate an alternative stack");
132 let mut stack
: sigaltstack
= mem
::zeroed();
134 stack
.ss_sp
= alt_stack
;
136 stack
.ss_size
= SIGSTKSZ
;
138 sigaltstack(&stack
, ptr
::null_mut());
140 Handler { _data: alt_stack }
143 pub unsafe fn drop_handler(handler
: &mut Handler
) {
144 munmap(handler
._data
, SIGSTKSZ
);
147 pub type sighandler_t
= *mut libc
::c_void
;
149 #[cfg(any(all(target_os = "linux", target_arch = "x86"), // may not match
150 all(target_os
= "linux", target_arch
= "x86_64"),
151 all(target_os
= "linux", target_arch
= "arm"), // may not match
152 all(target_os
= "linux", target_arch
= "aarch64"),
153 all(target_os
= "linux", target_arch
= "mips"), // may not match
154 all(target_os
= "linux", target_arch
= "mipsel"), // may not match
155 all(target_os
= "linux", target_arch
= "powerpc"), // may not match
156 target_os
= "android"))] // may not match
159 pub use super::sighandler_t
;
161 pub static SA_ONSTACK
: libc
::c_int
= 0x08000000;
162 pub static SA_SIGINFO
: libc
::c_int
= 0x00000004;
163 pub static SIGBUS
: libc
::c_int
= 7;
165 pub static SIGSTKSZ
: libc
::size_t
= 8192;
167 pub const SIG_DFL
: sighandler_t
= 0 as sighandler_t
;
169 // This definition is not as accurate as it could be, {si_addr} is
170 // actually a giant union. Currently we're only interested in that field,
174 si_signo
: libc
::c_int
,
175 si_errno
: libc
::c_int
,
176 si_code
: libc
::c_int
,
177 pub si_addr
: *mut libc
::c_void
181 pub struct sigaction
{
182 pub sa_sigaction
: sighandler_t
,
183 pub sa_mask
: sigset_t
,
184 pub sa_flags
: libc
::c_int
,
185 sa_restorer
: *mut libc
::c_void
,
188 #[cfg(target_pointer_width = "32")]
190 pub struct sigset_t
{
191 __val
: [libc
::c_ulong
; 32],
193 #[cfg(target_pointer_width = "64")]
195 pub struct sigset_t
{
196 __val
: [libc
::c_ulong
; 16],
200 pub struct sigaltstack
{
201 pub ss_sp
: *mut libc
::c_void
,
202 pub ss_flags
: libc
::c_int
,
203 pub ss_size
: libc
::size_t
208 #[cfg(any(target_os = "macos",
209 target_os
= "bitrig",
210 target_os
= "openbsd"))]
213 pub use super::sighandler_t
;
215 pub const SA_ONSTACK
: libc
::c_int
= 0x0001;
216 pub const SA_SIGINFO
: libc
::c_int
= 0x0040;
217 pub const SIGBUS
: libc
::c_int
= 10;
219 #[cfg(target_os = "macos")]
220 pub const SIGSTKSZ
: libc
::size_t
= 131072;
221 #[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
222 pub const SIGSTKSZ
: libc
::size_t
= 40960;
224 pub const SIG_DFL
: sighandler_t
= 0 as sighandler_t
;
226 pub type sigset_t
= u32;
228 // This structure has more fields, but we're not all that interested in
230 #[cfg(target_os = "macos")]
233 pub si_signo
: libc
::c_int
,
234 pub si_errno
: libc
::c_int
,
235 pub si_code
: libc
::c_int
,
236 pub pid
: libc
::pid_t
,
237 pub uid
: libc
::uid_t
,
238 pub status
: libc
::c_int
,
239 pub si_addr
: *mut libc
::c_void
242 #[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
245 pub si_signo
: libc
::c_int
,
246 pub si_code
: libc
::c_int
,
247 pub si_errno
: libc
::c_int
,
249 pub si_addr
: *mut libc
::c_void
253 pub struct sigaltstack
{
254 pub ss_sp
: *mut libc
::c_void
,
255 pub ss_size
: libc
::size_t
,
256 pub ss_flags
: libc
::c_int
260 pub struct sigaction
{
261 pub sa_sigaction
: sighandler_t
,
262 pub sa_mask
: sigset_t
,
263 pub sa_flags
: libc
::c_int
,
268 pub fn signal(signum
: libc
::c_int
, handler
: sighandler_t
) -> sighandler_t
;
269 pub fn raise(signum
: libc
::c_int
) -> libc
::c_int
;
271 pub fn sigaction(signum
: libc
::c_int
,
272 act
: *const sigaction
,
273 oldact
: *mut sigaction
) -> libc
::c_int
;
275 pub fn sigaltstack(ss
: *const sigaltstack
,
276 oss
: *mut sigaltstack
) -> libc
::c_int
;
280 #[cfg(not(any(target_os = "linux",
282 target_os
= "bitrig",
283 target_os
= "openbsd")))]
287 pub unsafe fn init() {
290 pub unsafe fn cleanup() {
293 pub unsafe fn make_handler() -> super::Handler
{
294 super::Handler { _data: 0 as *mut libc::c_void }
297 pub unsafe fn drop_handler(_handler
: &mut super::Handler
) {