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(rustc_attrs)]
23#![panic_runtime]
24#![feature(panic_runtime)]
25#![feature(c_unwind)]
26// `real_imp` is unused with Miri, so silence warnings.
27#![cfg_attr(miri, allow(dead_code))]
28#![allow(internal_features)]
29
30use alloc::boxed::Box;
31use core::any::Any;
32use core::panic::PanicPayload;
33
34cfg_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(all(target_env = "msvc", not(target_arch = "arm")))] {
46 // LLVM does not support unwinding on 32 bit ARM msvc (thumbv7a-pc-windows-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_os = "xous",
53 target_os = "solid_asp3",
54 all(target_family = "unix", not(target_os = "espidf")),
55 all(target_vendor = "fortanix", target_env = "sgx"),
56 ))] {
57 #[path = "gcc.rs"]
58 mod real_imp;
59 } else {
60 // Targets that don't support unwinding.
61 // - family=wasm
62 // - os=none ("bare metal" targets)
63 // - os=uefi
64 // - os=espidf
65 // - nvptx64-nvidia-cuda
66 // - arch=avr
67 #[path = "dummy.rs"]
68 mod real_imp;
69 }
70}
71
72cfg_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
85extern "C" {
86 /// Handler in std called when a panic object is dropped outside of
87 /// `catch_unwind`.
88 fn __rust_drop_panic() -> !;
89
90 /// Handler in std called when a foreign exception is caught.
91 fn __rust_foreign_exception() -> !;
92}
93
94#[rustc_std_internal_symbol]
95#[allow(improper_ctypes_definitions)]
96pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static) {
97 Box::into_raw(imp::cleanup(payload))
98}
99
100// Entry point for raising an exception, just delegates to the platform-specific
101// implementation.
102#[rustc_std_internal_symbol]
103pub unsafe fn __rust_start_panic(payload: &mut dyn PanicPayload) -> u32 {
104 let payload = Box::from_raw(payload.take_box());
105
106 imp::panic(payload)
107}
108