]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Auto merge of #55519 - fhartwig:hashmap-index-example, r=Centril
[rust.git] / src / libpanic_unwind / 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. The 64-bit MinGW target half-uses SEH and half-use gcc-like information
9 //!    in the `seh64_gnu.rs` module.
10 //! 3. All other targets use libunwind/libgcc in the `gcc/mod.rs` module.
11 //!
12 //! More documentation about each implementation can be found in the respective
13 //! module.
14
15 #![no_std]
16 #![unstable(feature = "panic_unwind", issue = "32837")]
17 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
18        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
19        html_root_url = "https://doc.rust-lang.org/nightly/",
20        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
21
22 #![feature(allocator_api)]
23 #![feature(alloc)]
24 #![feature(core_intrinsics)]
25 #![feature(lang_items)]
26 #![feature(libc)]
27 #![feature(nll)]
28 #![feature(panic_unwind)]
29 #![feature(raw)]
30 #![feature(staged_api)]
31 #![feature(std_internals)]
32 #![feature(unwind_attributes)]
33
34 #![panic_runtime]
35 #![feature(panic_runtime)]
36
37 extern crate alloc;
38 extern crate libc;
39 #[cfg(not(any(target_env = "msvc", all(windows, target_arch = "x86_64", target_env = "gnu"))))]
40 extern crate unwind;
41
42 use alloc::boxed::Box;
43 use core::intrinsics;
44 use core::mem;
45 use core::raw;
46 use core::panic::BoxMeUp;
47
48 #[macro_use]
49 mod macros;
50
51 cfg_if! {
52     if #[cfg(target_os = "emscripten")] {
53         #[path = "emcc.rs"]
54         mod imp;
55     } else if #[cfg(target_arch = "wasm32")] {
56         #[path = "dummy.rs"]
57         mod imp;
58     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
59         #[path = "dummy.rs"]
60         mod imp;
61     } else if #[cfg(target_env = "msvc")] {
62         #[path = "seh.rs"]
63         mod imp;
64     } else if #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))] {
65         #[path = "seh64_gnu.rs"]
66         mod imp;
67     } else {
68         // Rust runtime's startup objects depend on these symbols, so make them public.
69         #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
70         pub use imp::eh_frame_registry::*;
71         #[path = "gcc.rs"]
72         mod imp;
73     }
74 }
75
76 mod dwarf;
77 mod windows;
78
79 // Entry point for catching an exception, implemented using the `try` intrinsic
80 // in the compiler.
81 //
82 // The interaction between the `payload` function and the compiler is pretty
83 // hairy and tightly coupled, for more information see the compiler's
84 // implementation of this.
85 #[no_mangle]
86 pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
87                                                   data: *mut u8,
88                                                   data_ptr: *mut usize,
89                                                   vtable_ptr: *mut usize)
90                                                   -> u32 {
91     let mut payload = imp::payload();
92     if intrinsics::try(f, data, &mut payload as *mut _ as *mut _) == 0 {
93         0
94     } else {
95         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
96         *data_ptr = obj.data as usize;
97         *vtable_ptr = obj.vtable as usize;
98         1
99     }
100 }
101
102 // Entry point for raising an exception, just delegates to the platform-specific
103 // implementation.
104 #[no_mangle]
105 #[unwind(allowed)]
106 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
107     let payload = payload as *mut &mut dyn BoxMeUp;
108     imp::panic(Box::from_raw((*payload).box_me_up()))
109 }