]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Rollup merge of #67734 - XAMPPRocky:master, r=skade
[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(
17     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 #![feature(abi_thiscall)]
30 #![panic_runtime]
31 #![feature(panic_runtime)]
32
33 use alloc::boxed::Box;
34 use core::intrinsics;
35 use core::mem;
36 use core::panic::BoxMeUp;
37 use core::raw;
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(target_os = "hermit")] {
47         #[path = "hermit.rs"]
48         mod imp;
49     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
50         #[path = "dummy.rs"]
51         mod imp;
52     } else if #[cfg(target_env = "msvc")] {
53         #[path = "seh.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 extern "C" {
65     /// Handler in libstd called when a panic object is dropped outside of
66     /// `catch_unwind`.
67     fn __rust_drop_panic() -> !;
68 }
69
70 mod dwarf;
71
72 // Entry point for catching an exception, implemented using the `try` intrinsic
73 // in the compiler.
74 //
75 // The interaction between the `payload` function and the compiler is pretty
76 // hairy and tightly coupled, for more information see the compiler's
77 // implementation of this.
78 #[no_mangle]
79 pub unsafe extern "C" fn __rust_maybe_catch_panic(
80     f: fn(*mut u8),
81     data: *mut u8,
82     data_ptr: *mut usize,
83     vtable_ptr: *mut usize,
84 ) -> u32 {
85     let mut payload = imp::payload();
86     if intrinsics::r#try(f, data, &mut payload as *mut _ as *mut _) == 0 {
87         0
88     } else {
89         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
90         *data_ptr = obj.data as usize;
91         *vtable_ptr = obj.vtable as usize;
92         1
93     }
94 }
95
96 // Entry point for raising an exception, just delegates to the platform-specific
97 // implementation.
98 #[no_mangle]
99 #[unwind(allowed)]
100 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
101     let payload = payload as *mut &mut dyn BoxMeUp;
102     let payload = (*payload).take_box();
103
104     // Miri panic support: cfg'd out of normal builds just to be sure.
105     // When going through normal codegen, `miri_start_panic` is a NOP, so the
106     // Miri-enabled sysroot still supports normal unwinding. But when executed in
107     // Miri, this line initiates unwinding.
108     #[cfg(miri)]
109     core::intrinsics::miri_start_panic(payload);
110
111     imp::panic(Box::from_raw(payload))
112 }