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