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