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