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