]> git.lizzy.rs Git - rust.git/blob - library/panic_unwind/src/lib.rs
Rollup merge of #85989 - jyn514:gitignore-cleanup, r=ehuss
[rust.git] / library / panic_unwind / src / 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(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
17 #![feature(core_intrinsics)]
18 #![feature(lang_items)]
19 #![feature(nll)]
20 #![feature(panic_unwind)]
21 #![feature(staged_api)]
22 #![feature(std_internals)]
23 #![feature(unwind_attributes)]
24 #![feature(abi_thiscall)]
25 #![feature(rustc_attrs)]
26 #![feature(raw)]
27 #![panic_runtime]
28 #![feature(panic_runtime)]
29 // `real_imp` is unused with Miri, so silence warnings.
30 #![cfg_attr(miri, allow(dead_code))]
31
32 use alloc::boxed::Box;
33 use core::any::Any;
34 use core::panic::BoxMeUp;
35
36 cfg_if::cfg_if! {
37     if #[cfg(target_os = "emscripten")] {
38         #[path = "emcc.rs"]
39         mod real_imp;
40     } else if #[cfg(target_os = "hermit")] {
41         #[path = "hermit.rs"]
42         mod real_imp;
43     } else if #[cfg(target_env = "msvc")] {
44         #[path = "seh.rs"]
45         mod real_imp;
46     } else if #[cfg(any(
47         all(target_family = "windows", target_env = "gnu"),
48         target_os = "psp",
49         target_family = "unix",
50         all(target_vendor = "fortanix", target_env = "sgx"),
51     ))] {
52         // Rust runtime's startup objects depend on these symbols, so make them public.
53         #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
54         pub use real_imp::eh_frame_registry::*;
55         #[path = "gcc.rs"]
56         mod real_imp;
57     } else {
58         // Targets that don't support unwinding.
59         // - arch=wasm32
60         // - os=none ("bare metal" targets)
61         // - os=uefi
62         // - nvptx64-nvidia-cuda
63         // - arch=avr
64         #[path = "dummy.rs"]
65         mod real_imp;
66     }
67 }
68
69 cfg_if::cfg_if! {
70     if #[cfg(miri)] {
71         // Use the Miri runtime.
72         // We still need to also load the normal runtime above, as rustc expects certain lang
73         // items from there to be defined.
74         #[path = "miri.rs"]
75         mod imp;
76     } else {
77         // Use the real runtime.
78         use real_imp as imp;
79     }
80 }
81
82 extern "C" {
83     /// Handler in libstd called when a panic object is dropped outside of
84     /// `catch_unwind`.
85     fn __rust_drop_panic() -> !;
86
87     /// Handler in libstd called when a foreign exception is caught.
88     fn __rust_foreign_exception() -> !;
89 }
90
91 mod dwarf;
92
93 #[rustc_std_internal_symbol]
94 #[allow(improper_ctypes_definitions)]
95 pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static) {
96     Box::into_raw(imp::cleanup(payload))
97 }
98
99 // Entry point for raising an exception, just delegates to the platform-specific
100 // implementation.
101 #[rustc_std_internal_symbol]
102 #[unwind(allowed)]
103 pub unsafe extern "C" fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32 {
104     let payload = Box::from_raw((*payload).take_box());
105
106     imp::panic(payload)
107 }