]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Rollup merge of #67666 - lzutao:ptr-null-cmp, r=dtolnay
[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 #![panic_runtime]
30 #![feature(panic_runtime)]
31
32 use alloc::boxed::Box;
33 use core::intrinsics;
34 use core::mem;
35 use core::panic::BoxMeUp;
36 use core::raw;
37
38 cfg_if::cfg_if! {
39     if #[cfg(target_os = "emscripten")] {
40         #[path = "emcc.rs"]
41         mod imp;
42     } else if #[cfg(target_arch = "wasm32")] {
43         #[path = "dummy.rs"]
44         mod imp;
45     } else if #[cfg(target_os = "hermit")] {
46         #[path = "hermit.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 {
55         // Rust runtime's startup objects depend on these symbols, so make them public.
56         #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
57         pub use imp::eh_frame_registry::*;
58         #[path = "gcc.rs"]
59         mod imp;
60     }
61 }
62
63 mod dwarf;
64
65 // Entry point for catching an exception, implemented using the `try` intrinsic
66 // in the compiler.
67 //
68 // The interaction between the `payload` function and the compiler is pretty
69 // hairy and tightly coupled, for more information see the compiler's
70 // implementation of this.
71 #[no_mangle]
72 pub unsafe extern "C" fn __rust_maybe_catch_panic(
73     f: fn(*mut u8),
74     data: *mut u8,
75     data_ptr: *mut usize,
76     vtable_ptr: *mut usize,
77 ) -> u32 {
78     let mut payload = imp::payload();
79     if intrinsics::r#try(f, data, &mut payload as *mut _ as *mut _) == 0 {
80         0
81     } else {
82         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
83         *data_ptr = obj.data as usize;
84         *vtable_ptr = obj.vtable as usize;
85         1
86     }
87 }
88
89 // Entry point for raising an exception, just delegates to the platform-specific
90 // implementation.
91 #[no_mangle]
92 #[unwind(allowed)]
93 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
94     let payload = payload as *mut &mut dyn BoxMeUp;
95     let payload = (*payload).take_box();
96
97     // Miri panic support: cfg'd out of normal builds just to be sure.
98     // When going through normal codegen, `miri_start_panic` is a NOP, so the
99     // Miri-enabled sysroot still supports normal unwinding. But when executed in
100     // Miri, this line initiates unwinding.
101     #[cfg(miri)]
102     core::intrinsics::miri_start_panic(payload);
103
104     imp::panic(Box::from_raw(payload))
105 }