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