]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Simplify SaveHandler trait
[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 #![deny(rust_2018_idioms)]
21
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 use alloc::boxed::Box;
36 use core::intrinsics;
37 use core::mem;
38 use core::raw;
39 use core::panic::BoxMeUp;
40
41 cfg_if::cfg_if! {
42     if #[cfg(target_os = "emscripten")] {
43         #[path = "emcc.rs"]
44         mod imp;
45     } else if #[cfg(target_arch = "wasm32")] {
46         #[path = "dummy.rs"]
47         mod imp;
48     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
49         #[path = "dummy.rs"]
50         mod imp;
51     } else if #[cfg(target_env = "msvc")] {
52         #[path = "seh.rs"]
53         mod imp;
54     } else if #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))] {
55         #[path = "seh64_gnu.rs"]
56         mod imp;
57     } else {
58         // Rust runtime's startup objects depend on these symbols, so make them public.
59         #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
60         pub use imp::eh_frame_registry::*;
61         #[path = "gcc.rs"]
62         mod imp;
63     }
64 }
65
66 mod dwarf;
67 mod windows;
68
69 // Entry point for catching an exception, implemented using the `try` intrinsic
70 // in the compiler.
71 //
72 // The interaction between the `payload` function and the compiler is pretty
73 // hairy and tightly coupled, for more information see the compiler's
74 // implementation of this.
75 #[no_mangle]
76 pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
77                                                   data: *mut u8,
78                                                   data_ptr: *mut usize,
79                                                   vtable_ptr: *mut usize)
80                                                   -> u32 {
81     let mut payload = imp::payload();
82     if intrinsics::r#try(f, data, &mut payload as *mut _ as *mut _) == 0 {
83         0
84     } else {
85         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
86         *data_ptr = obj.data as usize;
87         *vtable_ptr = obj.vtable as usize;
88         1
89     }
90 }
91
92 // Entry point for raising an exception, just delegates to the platform-specific
93 // implementation.
94 #[no_mangle]
95 #[unwind(allowed)]
96 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
97     let payload = payload as *mut &mut dyn BoxMeUp;
98     imp::panic(Box::from_raw((*payload).box_me_up()))
99 }