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