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