1 //! Implementation of Rust panics via process aborts
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!
7 #![unstable(feature = "panic_abort", issue = "32837")]
8 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
9 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
12 #![allow(unused_features)]
13 #![deny(rust_2018_idioms)]
15 #![feature(core_intrinsics)]
18 #![feature(panic_runtime)]
19 #![feature(staged_api)]
20 #![feature(rustc_attrs)]
22 // Rust's "try" function, but if we're aborting on panics we just call the
23 // function as there's nothing else we need to do here.
24 #[rustc_std_internal_symbol]
25 pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8),
27 _data_ptr: *mut usize,
28 _vtable_ptr: *mut usize) -> u32 {
33 // "Leak" the payload and shim to the relevant abort on the platform in
36 // For Unix we just use `abort` from libc as it'll trigger debuggers, core
37 // dumps, etc, as one might expect. On Windows, however, the best option we have
38 // is the `__fastfail` intrinsics, but that's unfortunately not defined in LLVM,
39 // and the `RaiseFailFastException` function isn't available until Windows 7
40 // which would break compat with XP. For now just use `intrinsics::abort` which
41 // will kill us with an illegal instruction, which will do a good enough job for
43 #[rustc_std_internal_symbol]
44 pub unsafe extern fn __rust_start_panic(_payload: usize) -> u32 {
47 #[cfg(any(unix, target_os = "cloudabi"))]
48 unsafe fn abort() -> ! {
52 #[cfg(any(target_os = "redox",
54 all(target_arch = "wasm32", not(target_os = "emscripten"))))]
55 unsafe fn abort() -> ! {
56 core::intrinsics::abort();
59 #[cfg(all(target_vendor="fortanix", target_env="sgx"))]
60 unsafe fn abort() -> ! {
61 // call std::sys::abort_internal
62 extern "C" { pub fn __rust_abort() -> !; }
67 // This... is a bit of an oddity. The tl;dr; is that this is required to link
68 // correctly, the longer explanation is below.
70 // Right now the binaries of libcore/libstd that we ship are all compiled with
71 // `-C panic=unwind`. This is done to ensure that the binaries are maximally
72 // compatible with as many situations as possible. The compiler, however,
73 // requires a "personality function" for all functions compiled with `-C
74 // panic=unwind`. This personality function is hardcoded to the symbol
75 // `rust_eh_personality` and is defined by the `eh_personality` lang item.
77 // So... why not just define that lang item here? Good question! The way that
78 // panic runtimes are linked in is actually a little subtle in that they're
79 // "sort of" in the compiler's crate store, but only actually linked if another
80 // isn't actually linked. This ends up meaning that both this crate and the
81 // panic_unwind crate can appear in the compiler's crate store, and if both
82 // define the `eh_personality` lang item then that'll hit an error.
84 // To handle this the compiler only requires the `eh_personality` is defined if
85 // the panic runtime being linked in is the unwinding runtime, and otherwise
86 // it's not required to be defined (rightfully so). In this case, however, this
87 // library just defines this symbol so there's at least some personality
90 // Essentially this symbol is just defined to get wired up to libcore/libstd
91 // binaries, but it should never be called as we don't link in an unwinding
93 pub mod personalities {
97 target_arch = "wasm32",
98 not(target_os = "emscripten"),
101 target_os = "windows",
103 target_arch = "x86_64",
106 pub extern fn rust_eh_personality() {}
108 // On x86_64-pc-windows-gnu we use our own personality function that needs
109 // to return `ExceptionContinueSearch` as we're passing on all our frames.
111 #[cfg(all(target_os = "windows",
113 target_arch = "x86_64"))]
114 pub extern fn rust_eh_personality(_record: usize,
117 _dispatcher: usize) -> u32 {
118 1 // `ExceptionContinueSearch`
121 // Similar to above, this corresponds to the `eh_unwind_resume` lang item
122 // that's only used on Windows currently.
124 // Note that we don't execute landing pads, so this is never called, so it's
127 #[cfg(all(target_os = "windows", target_env = "gnu"))]
128 pub extern fn rust_eh_unwind_resume() {}
130 // These two are called by our startup objects on i686-pc-windows-gnu, but
131 // they don't need to do anything so the bodies are nops.
133 #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
134 pub extern fn rust_eh_register_frames() {}
136 #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
137 pub extern fn rust_eh_unregister_frames() {}