]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Rollup merge of #66850 - eddyb:span-free-formats, r=oli-obk
[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. Emscripten uses C++ exceptions in the `emcc.rs` file.
9 //! 3. All other targets use libunwind/libgcc in the `gcc.rs` file.
10 //!
11 //! More documentation about each implementation can be found in the respective
12 //! module.
13
14 #![no_std]
15 #![unstable(feature = "panic_unwind", issue = "32837")]
16 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
17        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
18
19 #![feature(core_intrinsics)]
20 #![feature(lang_items)]
21 #![feature(libc)]
22 #![feature(nll)]
23 #![feature(panic_unwind)]
24 #![feature(raw)]
25 #![feature(staged_api)]
26 #![feature(std_internals)]
27 #![feature(unwind_attributes)]
28
29 #![panic_runtime]
30 #![feature(panic_runtime)]
31
32 use alloc::boxed::Box;
33 use core::intrinsics;
34 use core::mem;
35 use core::raw;
36 use core::panic::BoxMeUp;
37
38 cfg_if::cfg_if! {
39     if #[cfg(miri)] {
40         #[path = "miri.rs"]
41         mod imp;
42     } else 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(target_os = "hermit")] {
49         #[path = "hermit.rs"]
50         mod imp;
51     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
52         #[path = "dummy.rs"]
53         mod imp;
54     } else if #[cfg(target_env = "msvc")] {
55         #[path = "seh.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
68 // Entry point for catching an exception, implemented using the `try` intrinsic
69 // in the compiler.
70 //
71 // The interaction between the `payload` function and the compiler is pretty
72 // hairy and tightly coupled, for more information see the compiler's
73 // implementation of this.
74 #[no_mangle]
75 pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
76                                                   data: *mut u8,
77                                                   data_ptr: *mut usize,
78                                                   vtable_ptr: *mut usize)
79                                                   -> u32 {
80     let mut payload = imp::payload();
81     if intrinsics::r#try(f, data, &mut payload as *mut _ as *mut _) == 0 {
82         0
83     } else {
84         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
85         *data_ptr = obj.data as usize;
86         *vtable_ptr = obj.vtable as usize;
87         1
88     }
89 }
90
91 // Entry point for raising an exception, just delegates to the platform-specific
92 // implementation.
93 #[no_mangle]
94 #[unwind(allowed)]
95 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
96     let payload = payload as *mut &mut dyn BoxMeUp;
97     imp::panic(Box::from_raw((*payload).take_box()))
98 }