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