]> git.lizzy.rs Git - rust.git/blob - src/libpanic_abort/lib.rs
Rollup merge of #52769 - sinkuu:stray_test, r=alexcrichton
[rust.git] / src / libpanic_abort / lib.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementation of Rust panics via process aborts
12 //!
13 //! When compared to the implementation via unwinding, this crate is *much*
14 //! simpler! That being said, it's not quite as versatile, but here goes!
15
16 #![no_std]
17 #![unstable(feature = "panic_abort", issue = "32837")]
18 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
19        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
20        html_root_url = "https://doc.rust-lang.org/nightly/",
21        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
22 #![panic_runtime]
23 #![allow(unused_features)]
24
25 #![feature(core_intrinsics)]
26 #![feature(libc)]
27 #![feature(panic_runtime)]
28 #![feature(staged_api)]
29 #![feature(rustc_attrs)]
30
31 // Rust's "try" function, but if we're aborting on panics we just call the
32 // function as there's nothing else we need to do here.
33 #[no_mangle]
34 #[rustc_std_internal_symbol]
35 pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8),
36                                               data: *mut u8,
37                                               _data_ptr: *mut usize,
38                                               _vtable_ptr: *mut usize) -> u32 {
39     f(data);
40     0
41 }
42
43 // "Leak" the payload and shim to the relevant abort on the platform in
44 // question.
45 //
46 // For Unix we just use `abort` from libc as it'll trigger debuggers, core
47 // dumps, etc, as one might expect. On Windows, however, the best option we have
48 // is the `__fastfail` intrinsics, but that's unfortunately not defined in LLVM,
49 // and the `RaiseFailFastException` function isn't available until Windows 7
50 // which would break compat with XP. For now just use `intrinsics::abort` which
51 // will kill us with an illegal instruction, which will do a good enough job for
52 // now hopefully.
53 #[no_mangle]
54 #[rustc_std_internal_symbol]
55 pub unsafe extern fn __rust_start_panic(_payload: usize) -> u32 {
56     abort();
57
58     #[cfg(any(unix, target_os = "cloudabi"))]
59     unsafe fn abort() -> ! {
60         extern crate libc;
61         libc::abort();
62     }
63
64     #[cfg(any(target_os = "redox",
65               windows,
66               all(target_arch = "wasm32", not(target_os = "emscripten"))))]
67     unsafe fn abort() -> ! {
68         core::intrinsics::abort();
69     }
70 }
71
72 // This... is a bit of an oddity. The tl;dr; is that this is required to link
73 // correctly, the longer explanation is below.
74 //
75 // Right now the binaries of libcore/libstd that we ship are all compiled with
76 // `-C panic=unwind`. This is done to ensure that the binaries are maximally
77 // compatible with as many situations as possible. The compiler, however,
78 // requires a "personality function" for all functions compiled with `-C
79 // panic=unwind`. This personality function is hardcoded to the symbol
80 // `rust_eh_personality` and is defined by the `eh_personality` lang item.
81 //
82 // So... why not just define that lang item here? Good question! The way that
83 // panic runtimes are linked in is actually a little subtle in that they're
84 // "sort of" in the compiler's crate store, but only actually linked if another
85 // isn't actually linked. This ends up meaning that both this crate and the
86 // panic_unwind crate can appear in the compiler's crate store, and if both
87 // define the `eh_personality` lang item then that'll hit an error.
88 //
89 // To handle this the compiler only requires the `eh_personality` is defined if
90 // the panic runtime being linked in is the unwinding runtime, and otherwise
91 // it's not required to be defined (rightfully so). In this case, however, this
92 // library just defines this symbol so there's at least some personality
93 // somewhere.
94 //
95 // Essentially this symbol is just defined to get wired up to libcore/libstd
96 // binaries, but it should never be called as we don't link in an unwinding
97 // runtime at all.
98 pub mod personalities {
99     #[no_mangle]
100     #[cfg(not(all(target_os = "windows",
101                   target_env = "gnu",
102                   target_arch = "x86_64")))]
103     pub extern fn rust_eh_personality() {}
104
105     // On x86_64-pc-windows-gnu we use our own personality function that needs
106     // to return `ExceptionContinueSearch` as we're passing on all our frames.
107     #[no_mangle]
108     #[cfg(all(target_os = "windows",
109               target_env = "gnu",
110               target_arch = "x86_64"))]
111     pub extern fn rust_eh_personality(_record: usize,
112                                       _frame: usize,
113                                       _context: usize,
114                                       _dispatcher: usize) -> u32 {
115         1 // `ExceptionContinueSearch`
116     }
117
118     // Similar to above, this corresponds to the `eh_unwind_resume` lang item
119     // that's only used on Windows currently.
120     //
121     // Note that we don't execute landing pads, so this is never called, so it's
122     // body is empty.
123     #[no_mangle]
124     #[cfg(all(target_os = "windows", target_env = "gnu"))]
125     pub extern fn rust_eh_unwind_resume() {}
126
127     // These two are called by our startup objects on i686-pc-windows-gnu, but
128     // they don't need to do anything so the bodies are nops.
129     #[no_mangle]
130     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
131     pub extern fn rust_eh_register_frames() {}
132     #[no_mangle]
133     #[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
134     pub extern fn rust_eh_unregister_frames() {}
135 }