]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Rollup merge of #58255 - taiki-e:librustc_metadata-2018, r=Centril
[rust.git] / src / libpanic_unwind / lib.rs
1 //! Implementation of panics via stack unwinding
2 //!
3 //! This crate is an implementation of panics in Rust using "most native" stack
4 //! unwinding mechanism of the platform this is being compiled for. This
5 //! essentially gets categorized into three buckets currently:
6 //!
7 //! 1. MSVC targets use SEH in the `seh.rs` file.
8 //! 2. The 64-bit MinGW target half-uses SEH and half-use gcc-like information
9 //!    in the `seh64_gnu.rs` module.
10 //! 3. All other targets use libunwind/libgcc in the `gcc/mod.rs` module.
11 //!
12 //! More documentation about each implementation can be found in the respective
13 //! module.
14
15 #![no_std]
16 #![unstable(feature = "panic_unwind", issue = "32837")]
17 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
18        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
19
20 #![feature(allocator_api)]
21 #![feature(alloc)]
22 #![feature(core_intrinsics)]
23 #![feature(lang_items)]
24 #![feature(libc)]
25 #![feature(nll)]
26 #![feature(panic_unwind)]
27 #![feature(raw)]
28 #![feature(staged_api)]
29 #![feature(std_internals)]
30 #![feature(unwind_attributes)]
31
32 #![panic_runtime]
33 #![feature(panic_runtime)]
34
35 extern crate alloc;
36 extern crate libc;
37 #[cfg(not(any(target_env = "msvc", all(windows, target_arch = "x86_64", target_env = "gnu"))))]
38 extern crate unwind;
39
40 use alloc::boxed::Box;
41 use core::intrinsics;
42 use core::mem;
43 use core::raw;
44 use core::panic::BoxMeUp;
45
46 #[macro_use]
47 mod macros;
48
49 cfg_if! {
50     if #[cfg(target_os = "emscripten")] {
51         #[path = "emcc.rs"]
52         mod imp;
53     } else if #[cfg(target_arch = "wasm32")] {
54         #[path = "dummy.rs"]
55         mod imp;
56     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
57         #[path = "dummy.rs"]
58         mod imp;
59     } else if #[cfg(target_env = "msvc")] {
60         #[path = "seh.rs"]
61         mod imp;
62     } else if #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))] {
63         #[path = "seh64_gnu.rs"]
64         mod imp;
65     } else {
66         // Rust runtime's startup objects depend on these symbols, so make them public.
67         #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
68         pub use imp::eh_frame_registry::*;
69         #[path = "gcc.rs"]
70         mod imp;
71     }
72 }
73
74 mod dwarf;
75 mod windows;
76
77 // Entry point for catching an exception, implemented using the `try` intrinsic
78 // in the compiler.
79 //
80 // The interaction between the `payload` function and the compiler is pretty
81 // hairy and tightly coupled, for more information see the compiler's
82 // implementation of this.
83 #[no_mangle]
84 pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
85                                                   data: *mut u8,
86                                                   data_ptr: *mut usize,
87                                                   vtable_ptr: *mut usize)
88                                                   -> u32 {
89     let mut payload = imp::payload();
90     if intrinsics::try(f, data, &mut payload as *mut _ as *mut _) == 0 {
91         0
92     } else {
93         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
94         *data_ptr = obj.data as usize;
95         *vtable_ptr = obj.vtable as usize;
96         1
97     }
98 }
99
100 // Entry point for raising an exception, just delegates to the platform-specific
101 // implementation.
102 #[no_mangle]
103 #[unwind(allowed)]
104 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
105     let payload = payload as *mut &mut dyn BoxMeUp;
106     imp::panic(Box::from_raw((*payload).box_me_up()))
107 }