]> git.proxmox.com Git - rustc.git/blob - library/panic_unwind/src/lib.rs
New upstream version 1.55.0+dfsg1
[rustc.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 #![panic_runtime]
27 #![feature(panic_runtime)]
28 // `real_imp` is unused with Miri, so silence warnings.
29 #![cfg_attr(miri, allow(dead_code))]
30
31 use alloc::boxed::Box;
32 use core::any::Any;
33 use core::panic::BoxMeUp;
34
35 cfg_if::cfg_if! {
36 if #[cfg(target_os = "emscripten")] {
37 #[path = "emcc.rs"]
38 mod real_imp;
39 } else if #[cfg(target_os = "hermit")] {
40 #[path = "hermit.rs"]
41 mod real_imp;
42 } else if #[cfg(target_env = "msvc")] {
43 #[path = "seh.rs"]
44 mod real_imp;
45 } else if #[cfg(any(
46 all(target_family = "windows", target_env = "gnu"),
47 target_os = "psp",
48 target_family = "unix",
49 all(target_vendor = "fortanix", target_env = "sgx"),
50 ))] {
51 // Rust runtime's startup objects depend on these symbols, so make them public.
52 #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
53 pub use real_imp::eh_frame_registry::*;
54 #[path = "gcc.rs"]
55 mod real_imp;
56 } else {
57 // Targets that don't support unwinding.
58 // - arch=wasm32
59 // - os=none ("bare metal" targets)
60 // - os=uefi
61 // - nvptx64-nvidia-cuda
62 // - arch=avr
63 #[path = "dummy.rs"]
64 mod real_imp;
65 }
66 }
67
68 cfg_if::cfg_if! {
69 if #[cfg(miri)] {
70 // Use the Miri runtime.
71 // We still need to also load the normal runtime above, as rustc expects certain lang
72 // items from there to be defined.
73 #[path = "miri.rs"]
74 mod imp;
75 } else {
76 // Use the real runtime.
77 use real_imp as imp;
78 }
79 }
80
81 extern "C" {
82 /// Handler in libstd called when a panic object is dropped outside of
83 /// `catch_unwind`.
84 fn __rust_drop_panic() -> !;
85
86 /// Handler in libstd called when a foreign exception is caught.
87 fn __rust_foreign_exception() -> !;
88 }
89
90 mod dwarf;
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 #[unwind(allowed)]
102 pub unsafe extern "C" fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32 {
103 let payload = Box::from_raw((*payload).take_box());
104
105 imp::panic(payload)
106 }