]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
adjust Miri interaction with panic runtime
[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(staged_api)]
26 #![feature(std_internals)]
27 #![feature(unwind_attributes)]
28 #![feature(abi_thiscall)]
29 #![feature(rustc_attrs)]
30 #![feature(raw)]
31 #![panic_runtime]
32 #![feature(panic_runtime)]
33
34 // `real_imp` is unused with Miri, so silence warnings.
35 #![cfg_attr(miri, allow(dead_code))]
36
37 use alloc::boxed::Box;
38 use core::any::Any;
39 use core::panic::BoxMeUp;
40
41 cfg_if::cfg_if! {
42     if #[cfg(target_os = "emscripten")] {
43         #[path = "emcc.rs"]
44         mod real_imp;
45     } else if #[cfg(target_arch = "wasm32")] {
46         #[path = "dummy.rs"]
47         mod real_imp;
48     } else if #[cfg(target_os = "hermit")] {
49         #[path = "hermit.rs"]
50         mod real_imp;
51     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
52         #[path = "dummy.rs"]
53         mod real_imp;
54     } else if #[cfg(target_env = "msvc")] {
55         #[path = "seh.rs"]
56         mod real_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 real_imp::eh_frame_registry::*;
61         #[path = "gcc.rs"]
62         mod real_imp;
63     }
64 }
65
66 cfg_if::cfg_if! {
67     if #[cfg(miri)] {
68         // Use the Miri runtime.
69         // We still need to also load the normal runtime above, as rustc expects certain lang
70         // items from there to be defined.
71         #[path = "miri.rs"]
72         mod imp;
73     } else {
74         // Use the real runtime.
75         use real_imp as imp;
76     }
77 }
78
79 extern "C" {
80     /// Handler in libstd called when a panic object is dropped outside of
81     /// `catch_unwind`.
82     fn __rust_drop_panic() -> !;
83 }
84
85 mod dwarf;
86
87 #[rustc_std_internal_symbol]
88 pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static) {
89     Box::into_raw(imp::cleanup(payload))
90 }
91
92 // Entry point for raising an exception, just delegates to the platform-specific
93 // implementation.
94 #[rustc_std_internal_symbol]
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     let payload = (*payload).take_box();
99
100     imp::panic(Box::from_raw(payload))
101 }