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