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(cfg_emscripten_wasm_eh)]
18#![feature(cfg_select)]
19#![feature(core_intrinsics)]
20#![feature(lang_items)]
21#![feature(panic_unwind)]
22#![feature(staged_api)]
23#![feature(std_internals)]
24#![feature(rustc_attrs)]
25#![panic_runtime]
26#![feature(panic_runtime)]
27#![allow(internal_features)]
28#![warn(unreachable_pub)]
29#![deny(unsafe_op_in_unsafe_fn)]
30
31use alloc::boxed::Box;
32use core::any::Any;
33use core::panic::PanicPayload;
34
35cfg_select! {
36 all(target_os = "emscripten", not(emscripten_wasm_eh)) => {
37 #[path = "emcc.rs"]
38 mod imp;
39 }
40 target_os = "hermit" => {
41 #[path = "hermit.rs"]
42 mod imp;
43 }
44 target_os = "l4re" => {
45 // L4Re is unix family but does not yet support unwinding.
46 #[path = "dummy.rs"]
47 mod imp;
48 }
49 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(any(target_os = "espidf", target_os = "nuttx"))),
55 all(target_vendor = "fortanix", target_env = "sgx"),
56 target_family = "wasm",
57 ) => {
58 #[path = "gcc.rs"]
59 mod imp;
60 }
61 miri => {
62 // Use the Miri runtime on Windows as miri doesn't support funclet based unwinding,
63 // only landingpad based unwinding. Also use the Miri runtime on unsupported platforms.
64 #[path = "miri.rs"]
65 mod imp;
66 }
67 all(target_env = "msvc", not(target_arch = "arm")) => {
68 // LLVM does not support unwinding on 32 bit ARM msvc (thumbv7a-pc-windows-msvc)
69 #[path = "seh.rs"]
70 mod imp;
71 }
72 _ => {
73 // Targets that don't support unwinding.
74 // - os=none ("bare metal" targets)
75 // - os=uefi
76 // - os=espidf
77 // - nvptx64-nvidia-cuda
78 // - arch=avr
79 #[path = "dummy.rs"]
80 mod imp;
81 }
82}
83
84unsafe extern "C" {
85 /// Handler in std called when a panic object is dropped outside of
86 /// `catch_unwind`.
87 #[rustc_std_internal_symbol]
88 unsafefn __rust_drop_panic() -> !;
89
90 /// Handler in std called when a foreign exception is caught.
91 #[rustc_std_internal_symbol]
92 unsafefn __rust_foreign_exception() -> !;
93}
94
95#[rustc_std_internal_symbol]
96#[allow(improper_ctypes_definitions)]
97pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static) {
98 unsafe { Box::into_raw(imp::cleanup(ptr:payload)) }
99}
100
101// Entry point for raising an exception, just delegates to the platform-specific
102// implementation.
103#[rustc_std_internal_symbol]
104pub unsafe fn __rust_start_panic(payload: &mut dyn PanicPayload) -> u32 {
105 unsafe {
106 let payload: Box = Box::from_raw(payload.take_box());
107
108 imp::panic(data:payload)
109 }
110}
111