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