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