Skip to main content

std/sys/pal/unix/stack_overflow/
mod.rs

1#![cfg_attr(test, allow(dead_code))]
2#![forbid(unsafe_op_in_unsafe_fn)]
3
4cfg_select! {
5    // This is intentionally not enabled on iOS/tvOS/watchOS/visionOS, as it uses
6    // several symbols that might lead to rejections from the App Store, namely
7    // `sigaction`, `sigaltstack`, `sysctlbyname`, `mmap`, `munmap` and `mprotect`.
8    //
9    // This might be overly cautious, though it is also what Swift does (and they
10    // usually have fewer qualms about forwards compatibility, since the runtime
11    // is shipped with the OS):
12    // <https://github.com/apple/swift/blob/swift-5.10-RELEASE/stdlib/public/runtime/CrashHandlerMacOS.cpp>
13    //
14    // miri doesn't model signals nor stack overflows and this code has some
15    // synchronization properties that we don't want to expose to user code,
16    // hence we disable it on miri.
17    all(
18        not(miri),
19        any(
20            target_os = "linux",
21            target_os = "freebsd",
22            target_os = "hurd",
23            target_os = "macos",
24            target_os = "netbsd",
25            target_os = "openbsd",
26            target_os = "solaris",
27            target_os = "illumos",
28        )
29    ) => {
30        mod handler_signal;
31        mod thread_info;
32        use handler_signal as imp;
33    }
34    target_os = "cygwin" => {
35        mod handler_cygwin;
36        use handler_cygwin as imp;
37    }
38    _ => {
39        mod handler_none;
40        use handler_none as imp;
41    }
42}
43
44pub use self::imp::init;
45use self::imp::{drop_handler, make_handler};
46
47pub struct Handler {
48    data: *mut libc::c_void,
49}
50
51impl Handler {
52    pub unsafe fn new() -> Handler {
53        make_handler(false)
54    }
55
56    fn null() -> Handler {
57        Handler { data: crate::ptr::null_mut() }
58    }
59}
60
61impl Drop for Handler {
62    fn drop(&mut self) {
63        unsafe {
64            drop_handler(self.data);
65        }
66    }
67}