]> git.lizzy.rs Git - rust.git/blob - library/panic_abort/src/lib.rs
Rollup merge of #79841 - fintelia:patch-6, r=kennytm
[rust.git] / library / panic_abort / src / 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(nll)]
16 #![feature(panic_runtime)]
17 #![feature(std_internals)]
18 #![feature(staged_api)]
19 #![feature(rustc_attrs)]
20 #![feature(asm)]
21
22 use core::any::Any;
23 use core::panic::BoxMeUp;
24
25 #[rustc_std_internal_symbol]
26 #[allow(improper_ctypes_definitions)]
27 pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Send + 'static) {
28     unreachable!()
29 }
30
31 // "Leak" the payload and shim to the relevant abort on the platform in question.
32 #[rustc_std_internal_symbol]
33 pub unsafe extern "C" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
34     abort();
35
36     cfg_if::cfg_if! {
37         if #[cfg(unix)] {
38             unsafe fn abort() -> ! {
39                 libc::abort();
40             }
41         } else if #[cfg(any(target_os = "hermit",
42                             all(target_vendor = "fortanix", target_env = "sgx")
43         ))] {
44             unsafe fn abort() -> ! {
45                 // call std::sys::abort_internal
46                 extern "C" {
47                     pub fn __rust_abort() -> !;
48                 }
49                 __rust_abort();
50             }
51         } else if #[cfg(all(windows, not(miri)))] {
52             // On Windows, use the processor-specific __fastfail mechanism. In Windows 8
53             // and later, this will terminate the process immediately without running any
54             // in-process exception handlers. In earlier versions of Windows, this
55             // sequence of instructions will be treated as an access violation,
56             // terminating the process but without necessarily bypassing all exception
57             // handlers.
58             //
59             // https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail
60             //
61             // Note: this is the same implementation as in libstd's `abort_internal`
62             unsafe fn abort() -> ! {
63                 const FAST_FAIL_FATAL_APP_EXIT: usize = 7;
64                 cfg_if::cfg_if! {
65                     if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
66                         asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
67                     } else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
68                         asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
69                     } else if #[cfg(target_arch = "aarch64")] {
70                         asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
71                     } else {
72                         core::intrinsics::abort();
73                     }
74                 }
75                 core::intrinsics::unreachable();
76             }
77         } else {
78             unsafe fn abort() -> ! {
79                 core::intrinsics::abort();
80             }
81         }
82     }
83 }
84
85 // This... is a bit of an oddity. The tl;dr; is that this is required to link
86 // correctly, the longer explanation is below.
87 //
88 // Right now the binaries of libcore/libstd that we ship are all compiled with
89 // `-C panic=unwind`. This is done to ensure that the binaries are maximally
90 // compatible with as many situations as possible. The compiler, however,
91 // requires a "personality function" for all functions compiled with `-C
92 // panic=unwind`. This personality function is hardcoded to the symbol
93 // `rust_eh_personality` and is defined by the `eh_personality` lang item.
94 //
95 // So... why not just define that lang item here? Good question! The way that
96 // panic runtimes are linked in is actually a little subtle in that they're
97 // "sort of" in the compiler's crate store, but only actually linked if another
98 // isn't actually linked. This ends up meaning that both this crate and the
99 // panic_unwind crate can appear in the compiler's crate store, and if both
100 // define the `eh_personality` lang item then that'll hit an error.
101 //
102 // To handle this the compiler only requires the `eh_personality` is defined if
103 // the panic runtime being linked in is the unwinding runtime, and otherwise
104 // it's not required to be defined (rightfully so). In this case, however, this
105 // library just defines this symbol so there's at least some personality
106 // somewhere.
107 //
108 // Essentially this symbol is just defined to get wired up to libcore/libstd
109 // binaries, but it should never be called as we don't link in an unwinding
110 // runtime at all.
111 pub mod personalities {
112     #[rustc_std_internal_symbol]
113     #[cfg(not(any(
114         all(target_arch = "wasm32", not(target_os = "emscripten"),),
115         all(target_os = "windows", target_env = "gnu", target_arch = "x86_64",),
116     )))]
117     pub extern "C" fn rust_eh_personality() {}
118
119     // On x86_64-pc-windows-gnu we use our own personality function that needs
120     // to return `ExceptionContinueSearch` as we're passing on all our frames.
121     #[rustc_std_internal_symbol]
122     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86_64"))]
123     pub extern "C" fn rust_eh_personality(
124         _record: usize,
125         _frame: usize,
126         _context: usize,
127         _dispatcher: usize,
128     ) -> u32 {
129         1 // `ExceptionContinueSearch`
130     }
131
132     // Similar to above, this corresponds to the `eh_catch_typeinfo` lang item
133     // that's only used on Emscripten currently.
134     //
135     // Since panics don't generate exceptions and foreign exceptions are
136     // currently UB with -C panic=abort (although this may be subject to
137     // change), any catch_unwind calls will never use this typeinfo.
138     #[rustc_std_internal_symbol]
139     #[allow(non_upper_case_globals)]
140     #[cfg(target_os = "emscripten")]
141     static rust_eh_catch_typeinfo: [usize; 2] = [0; 2];
142
143     // These two are called by our startup objects on i686-pc-windows-gnu, but
144     // they don't need to do anything so the bodies are nops.
145     #[rustc_std_internal_symbol]
146     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
147     pub extern "C" fn rust_eh_register_frames() {}
148     #[rustc_std_internal_symbol]
149     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
150     pub extern "C" fn rust_eh_unregister_frames() {}
151 }