]> git.lizzy.rs Git - rust.git/blob - src/libpanic_abort/lib.rs
Rollup merge of #66267 - GuillaumeGomez:add-rustdoc-doc, r=kinnison
[rust.git] / src / libpanic_abort / 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(html_root_url = "https://doc.rust-lang.org/nightly/",
9        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
10 #![panic_runtime]
11
12 #![allow(unused_features)]
13
14 #![feature(core_intrinsics)]
15 #![feature(libc)]
16 #![feature(nll)]
17 #![feature(panic_runtime)]
18 #![feature(staged_api)]
19 #![feature(rustc_attrs)]
20
21 // Rust's "try" function, but if we're aborting on panics we just call the
22 // function as there's nothing else we need to do here.
23 #[rustc_std_internal_symbol]
24 #[allow(improper_ctypes)]
25 pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8),
26                                               data: *mut u8,
27                                               _data_ptr: *mut usize,
28                                               _vtable_ptr: *mut usize) -> u32 {
29     f(data);
30     0
31 }
32
33 // "Leak" the payload and shim to the relevant abort on the platform in
34 // question.
35 //
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
42 // now hopefully.
43 #[rustc_std_internal_symbol]
44 pub unsafe extern fn __rust_start_panic(_payload: usize) -> u32 {
45     abort();
46
47     #[cfg(any(unix, target_os = "cloudabi"))]
48     unsafe fn abort() -> ! {
49         libc::abort();
50     }
51
52     #[cfg(any(windows,
53               all(target_arch = "wasm32", not(target_os = "emscripten"))))]
54     unsafe fn abort() -> ! {
55         core::intrinsics::abort();
56     }
57
58     #[cfg(any(target_os = "hermit",
59               all(target_vendor="fortanix", target_env="sgx")))]
60     unsafe fn abort() -> ! {
61         // call std::sys::abort_internal
62         extern "C" { pub fn __rust_abort() -> !; }
63         __rust_abort();
64     }
65 }
66
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.
69 //
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.
76 //
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.
83 //
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
88 // somewhere.
89 //
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
92 // runtime at all.
93 pub mod personalities {
94     #[no_mangle]
95     #[cfg(not(any(
96         all(
97             target_arch = "wasm32",
98             not(target_os = "emscripten"),
99         ),
100         all(
101             target_os = "windows",
102             target_env = "gnu",
103             target_arch = "x86_64",
104         ),
105     )))]
106     pub extern fn rust_eh_personality() {}
107
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.
110     #[no_mangle]
111     #[cfg(all(target_os = "windows",
112               target_env = "gnu",
113               target_arch = "x86_64"))]
114     pub extern fn rust_eh_personality(_record: usize,
115                                       _frame: usize,
116                                       _context: usize,
117                                       _dispatcher: usize) -> u32 {
118         1 // `ExceptionContinueSearch`
119     }
120
121     // Similar to above, this corresponds to the `eh_unwind_resume` lang item
122     // that's only used on Windows currently.
123     //
124     // Note that we don't execute landing pads, so this is never called, so it's
125     // body is empty.
126     #[no_mangle]
127     #[cfg(all(target_os = "windows", target_env = "gnu"))]
128     pub extern fn rust_eh_unwind_resume() {}
129
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.
132     #[no_mangle]
133     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
134     pub extern fn rust_eh_register_frames() {}
135     #[no_mangle]
136     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
137     pub extern fn rust_eh_unregister_frames() {}
138 }