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