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(strict_provenance)]
23#![feature(exposed_provenance)]
24#![feature(rustc_attrs)]
25#![panic_runtime]
26#![feature(panic_runtime)]
27#![feature(c_unwind)]
28// `real_imp` is unused with Miri, so silence warnings.
29#![cfg_attr(miri, allow(dead_code))]
30#![allow(internal_features)]
31
32use alloc::boxed::Box;
33use core::any::Any;
34use core::panic::PanicPayload;
35
36cfg_if::cfg_if! {
37 if #[cfg(target_os = "emscripten")] {
38 #[path = "emcc.rs"]
39 mod real_imp;
40 } else if #[cfg(target_os = "hermit")] {
41 #[path = "hermit.rs"]
42 mod real_imp;
43 } else if #[cfg(target_os = "l4re")] {
44 // L4Re is unix family but does not yet support unwinding.
45 #[path = "dummy.rs"]
46 mod real_imp;
47 } else if #[cfg(all(target_env = "msvc", not(target_arch = "arm")))] {
48 // LLVM does not support unwinding on 32 bit ARM msvc (thumbv7a-pc-windows-msvc)
49 #[path = "seh.rs"]
50 mod real_imp;
51 } else if #[cfg(any(
52 all(target_family = "windows", target_env = "gnu"),
53 target_os = "psp",
54 target_os = "xous",
55 target_os = "solid_asp3",
56 all(target_family = "unix", not(target_os = "espidf")),
57 all(target_vendor = "fortanix", target_env = "sgx"),
58 target_family = "wasm",
59 ))] {
60 #[path = "gcc.rs"]
61 mod real_imp;
62 } else {
63 // Targets that don't support unwinding.
64 // - os=none ("bare metal" targets)
65 // - os=uefi
66 // - os=espidf
67 // - nvptx64-nvidia-cuda
68 // - arch=avr
69 #[path = "dummy.rs"]
70 mod real_imp;
71 }
72}
73
74cfg_if::cfg_if! {
75 if #[cfg(miri)] {
76 // Use the Miri runtime.
77 // We still need to also load the normal runtime above, as rustc expects certain lang
78 // items from there to be defined.
79 #[path = "miri.rs"]
80 mod imp;
81 } else {
82 // Use the real runtime.
83 use real_imp as imp;
84 }
85}
86
87extern "C" {
88 /// Handler in std called when a panic object is dropped outside of
89 /// `catch_unwind`.
90 fn __rust_drop_panic() -> !;
91
92 /// Handler in std called when a foreign exception is caught.
93 fn __rust_foreign_exception() -> !;
94}
95
96#[rustc_std_internal_symbol]
97#[allow(improper_ctypes_definitions)]
98pub 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]
105pub unsafe fn __rust_start_panic(payload: &mut dyn PanicPayload) -> u32 {
106 let payload = Box::from_raw(payload.take_box());
107
108 imp::panic(payload)
109}
110