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_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
9 html_favicon_url
= "https://doc.rust-lang.org/favicon.ico",
10 html_root_url
= "https://doc.rust-lang.org/nightly/",
11 issue_tracker_base_url
= "https://github.com/rust-lang/rust/issues/")]
13 #![allow(unused_features)]
15 #![cfg_attr(stage0, feature(cfg_target_vendor))]
16 #![feature(core_intrinsics)]
19 #![feature(panic_runtime)]
20 #![feature(staged_api)]
21 #![feature(rustc_attrs)]
23 // Rust's "try" function, but if we're aborting on panics we just call the
24 // function as there's nothing else we need to do here.
25 #[rustc_std_internal_symbol]
26 pub unsafe extern fn __rust_maybe_catch_panic(f
: fn(*mut u8),
28 _data_ptr
: *mut usize,
29 _vtable_ptr
: *mut usize) -> u32 {
34 // "Leak" the payload and shim to the relevant abort on the platform in
37 // For Unix we just use `abort` from libc as it'll trigger debuggers, core
38 // dumps, etc, as one might expect. On Windows, however, the best option we have
39 // is the `__fastfail` intrinsics, but that's unfortunately not defined in LLVM,
40 // and the `RaiseFailFastException` function isn't available until Windows 7
41 // which would break compat with XP. For now just use `intrinsics::abort` which
42 // will kill us with an illegal instruction, which will do a good enough job for
44 #[rustc_std_internal_symbol]
45 pub unsafe extern fn __rust_start_panic(_payload
: usize) -> u32 {
48 #[cfg(any(unix, target_os = "cloudabi"))]
49 unsafe fn abort() -> ! {
54 #[cfg(any(target_os = "redox",
56 all(target_arch
= "wasm32", not(target_os
= "emscripten"))))]
57 unsafe fn abort() -> ! {
58 core
::intrinsics
::abort();
61 #[cfg(all(target_vendor="fortanix", target_env="sgx"))]
62 unsafe fn abort() -> ! {
63 extern "C" { pub fn panic_exit() -> !; }
68 // This... is a bit of an oddity. The tl;dr; is that this is required to link
69 // correctly, the longer explanation is below.
71 // Right now the binaries of libcore/libstd that we ship are all compiled with
72 // `-C panic=unwind`. This is done to ensure that the binaries are maximally
73 // compatible with as many situations as possible. The compiler, however,
74 // requires a "personality function" for all functions compiled with `-C
75 // panic=unwind`. This personality function is hardcoded to the symbol
76 // `rust_eh_personality` and is defined by the `eh_personality` lang item.
78 // So... why not just define that lang item here? Good question! The way that
79 // panic runtimes are linked in is actually a little subtle in that they're
80 // "sort of" in the compiler's crate store, but only actually linked if another
81 // isn't actually linked. This ends up meaning that both this crate and the
82 // panic_unwind crate can appear in the compiler's crate store, and if both
83 // define the `eh_personality` lang item then that'll hit an error.
85 // To handle this the compiler only requires the `eh_personality` is defined if
86 // the panic runtime being linked in is the unwinding runtime, and otherwise
87 // it's not required to be defined (rightfully so). In this case, however, this
88 // library just defines this symbol so there's at least some personality
91 // Essentially this symbol is just defined to get wired up to libcore/libstd
92 // binaries, but it should never be called as we don't link in an unwinding
94 pub mod personalities
{
98 target_arch
= "wasm32",
99 not(target_os
= "emscripten"),
102 target_os
= "windows",
104 target_arch
= "x86_64",
107 pub extern fn rust_eh_personality() {}
109 // On x86_64-pc-windows-gnu we use our own personality function that needs
110 // to return `ExceptionContinueSearch` as we're passing on all our frames.
112 #[cfg(all(target_os = "windows",
114 target_arch
= "x86_64"))]
115 pub extern fn rust_eh_personality(_record
: usize,
118 _dispatcher
: usize) -> u32 {
119 1 // `ExceptionContinueSearch`
122 // Similar to above, this corresponds to the `eh_unwind_resume` lang item
123 // that's only used on Windows currently.
125 // Note that we don't execute landing pads, so this is never called, so it's
128 #[cfg(all(target_os = "windows", target_env = "gnu"))]
129 pub extern fn rust_eh_unwind_resume() {}
131 // These two are called by our startup objects on i686-pc-windows-gnu, but
132 // they don't need to do anything so the bodies are nops.
134 #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
135 pub extern fn rust_eh_register_frames() {}
137 #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
138 pub extern fn rust_eh_unregister_frames() {}