]> git.lizzy.rs Git - rust.git/blob - src/libpanic_abort/lib.rs
Auto merge of #63166 - ksqsf:master, r=alexcrichton
[rust.git] / src / libpanic_abort / lib.rs
1 //! Implementation of Rust panics via process aborts
2 //!
3 //! When compared to the implementation via unwinding, this crate is *much*
4 //! simpler! That being said, it's not quite as versatile, but here goes!
5
6 #![no_std]
7 #![unstable(feature = "panic_abort", issue = "32837")]
8 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
9        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
10 #![panic_runtime]
11
12 #![allow(unused_features)]
13
14 #![feature(core_intrinsics)]
15 #![feature(libc)]
16 #![feature(nll)]
17 #![feature(panic_runtime)]
18 #![feature(staged_api)]
19 #![feature(rustc_attrs)]
20
21 // Rust's "try" function, but if we're aborting on panics we just call the
22 // function as there's nothing else we need to do here.
23 #[rustc_std_internal_symbol]
24 pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8),
25                                               data: *mut u8,
26                                               _data_ptr: *mut usize,
27                                               _vtable_ptr: *mut usize) -> u32 {
28     f(data);
29     0
30 }
31
32 // "Leak" the payload and shim to the relevant abort on the platform in
33 // question.
34 //
35 // For Unix we just use `abort` from libc as it'll trigger debuggers, core
36 // dumps, etc, as one might expect. On Windows, however, the best option we have
37 // is the `__fastfail` intrinsics, but that's unfortunately not defined in LLVM,
38 // and the `RaiseFailFastException` function isn't available until Windows 7
39 // which would break compat with XP. For now just use `intrinsics::abort` which
40 // will kill us with an illegal instruction, which will do a good enough job for
41 // now hopefully.
42 #[rustc_std_internal_symbol]
43 pub unsafe extern fn __rust_start_panic(_payload: usize) -> u32 {
44     abort();
45
46     #[cfg(any(unix, target_os = "cloudabi"))]
47     unsafe fn abort() -> ! {
48         libc::abort();
49     }
50
51     #[cfg(any(windows,
52               all(target_arch = "wasm32", not(target_os = "emscripten"))))]
53     unsafe fn abort() -> ! {
54         core::intrinsics::abort();
55     }
56
57     #[cfg(all(target_vendor="fortanix", target_env="sgx"))]
58     unsafe fn abort() -> ! {
59         // call std::sys::abort_internal
60         extern "C" { pub fn __rust_abort() -> !; }
61         __rust_abort();
62     }
63 }
64
65 // This... is a bit of an oddity. The tl;dr; is that this is required to link
66 // correctly, the longer explanation is below.
67 //
68 // Right now the binaries of libcore/libstd that we ship are all compiled with
69 // `-C panic=unwind`. This is done to ensure that the binaries are maximally
70 // compatible with as many situations as possible. The compiler, however,
71 // requires a "personality function" for all functions compiled with `-C
72 // panic=unwind`. This personality function is hardcoded to the symbol
73 // `rust_eh_personality` and is defined by the `eh_personality` lang item.
74 //
75 // So... why not just define that lang item here? Good question! The way that
76 // panic runtimes are linked in is actually a little subtle in that they're
77 // "sort of" in the compiler's crate store, but only actually linked if another
78 // isn't actually linked. This ends up meaning that both this crate and the
79 // panic_unwind crate can appear in the compiler's crate store, and if both
80 // define the `eh_personality` lang item then that'll hit an error.
81 //
82 // To handle this the compiler only requires the `eh_personality` is defined if
83 // the panic runtime being linked in is the unwinding runtime, and otherwise
84 // it's not required to be defined (rightfully so). In this case, however, this
85 // library just defines this symbol so there's at least some personality
86 // somewhere.
87 //
88 // Essentially this symbol is just defined to get wired up to libcore/libstd
89 // binaries, but it should never be called as we don't link in an unwinding
90 // runtime at all.
91 pub mod personalities {
92     #[no_mangle]
93     #[cfg(not(any(
94         all(
95             target_arch = "wasm32",
96             not(target_os = "emscripten"),
97         ),
98         all(
99             target_os = "windows",
100             target_env = "gnu",
101             target_arch = "x86_64",
102         ),
103     )))]
104     pub extern fn rust_eh_personality() {}
105
106     // On x86_64-pc-windows-gnu we use our own personality function that needs
107     // to return `ExceptionContinueSearch` as we're passing on all our frames.
108     #[no_mangle]
109     #[cfg(all(target_os = "windows",
110               target_env = "gnu",
111               target_arch = "x86_64"))]
112     pub extern fn rust_eh_personality(_record: usize,
113                                       _frame: usize,
114                                       _context: usize,
115                                       _dispatcher: usize) -> u32 {
116         1 // `ExceptionContinueSearch`
117     }
118
119     // Similar to above, this corresponds to the `eh_unwind_resume` lang item
120     // that's only used on Windows currently.
121     //
122     // Note that we don't execute landing pads, so this is never called, so it's
123     // body is empty.
124     #[no_mangle]
125     #[cfg(all(target_os = "windows", target_env = "gnu"))]
126     pub extern fn rust_eh_unwind_resume() {}
127
128     // These two are called by our startup objects on i686-pc-windows-gnu, but
129     // they don't need to do anything so the bodies are nops.
130     #[no_mangle]
131     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
132     pub extern fn rust_eh_register_frames() {}
133     #[no_mangle]
134     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
135     pub extern fn rust_eh_unregister_frames() {}
136 }