| #![cfg_attr(test, allow(dead_code))] | |
| #![forbid(unsafe_op_in_unsafe_fn)] | |
| cfg_select! { | |
| // This is intentionally not enabled on iOS/tvOS/watchOS/visionOS, as it uses | |
| // several symbols that might lead to rejections from the App Store, namely | |
| // `sigaction`, `sigaltstack`, `sysctlbyname`, `mmap`, `munmap` and `mprotect`. | |
| // | |
| // This might be overly cautious, though it is also what Swift does (and they | |
| // usually have fewer qualms about forwards compatibility, since the runtime | |
| // is shipped with the OS): | |
| // <https://github.com/apple/swift/blob/swift-5.10-RELEASE/stdlib/public/runtime/CrashHandlerMacOS.cpp> | |
| any( | |
| target_os = "linux", | |
| target_os = "freebsd", | |
| target_os = "hurd", | |
| target_os = "macos", | |
| target_os = "netbsd", | |
| target_os = "openbsd", | |
| target_os = "solaris", | |
| target_os = "illumos", | |
| ) => { | |
| mod handler_signal; | |
| mod thread_info; | |
| use handler_signal as imp; | |
| } | |
| target_os = "cygwin" => { | |
| mod handler_cygwin; | |
| use handler_cygwin as imp; | |
| } | |
| _ => { | |
| mod handler_none; | |
| use handler_none as imp; | |
| } | |
| } | |
| /// # Safety | |
| /// Must be called only once, on the main thread, during program startup. | |
| pub unsafe fn init() { | |
| // miri doesn't model signals nor stack overflows and this code has some | |
| // synchronization properties that we don't want to expose to user code, | |
| // hence we disable it on miri. | |
| if cfg!(miri) { | |
| return; | |
| } | |
| // SAFETY: guaranteed by caller. | |
| unsafe { imp::init() }; | |
| } | |
| pub struct Handler { | |
| data: *mut libc::c_void, | |
| } | |
| impl Handler { | |
| pub unsafe fn new() -> Handler { | |
| // The stack overflow detection logic, even if disabled, has some | |
| // synchronization logic that should be disabled under miri. | |
| if cfg!(miri) { | |
| return Handler::null(); | |
| } | |
| imp::make_handler(false) | |
| } | |
| fn null() -> Handler { | |
| Handler { data: crate::ptr::null_mut() } | |
| } | |
| } | |
| impl Drop for Handler { | |
| fn drop(&mut self) { | |
| // The stack overflow detection logic, even if disabled, has some | |
| // synchronization logic that should be disabled under miri. | |
| if cfg!(miri) { | |
| return; | |
| } | |
| unsafe { imp::drop_handler(self.data) }; | |
| } | |
| } |