]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Rollup merge of #52769 - sinkuu:stray_test, r=alexcrichton
[rust.git] / src / libpanic_unwind / lib.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementation of panics via stack unwinding
12 //!
13 //! This crate is an implementation of panics in Rust using "most native" stack
14 //! unwinding mechanism of the platform this is being compiled for. This
15 //! essentially gets categorized into three buckets currently:
16 //!
17 //! 1. MSVC targets use SEH in the `seh.rs` file.
18 //! 2. The 64-bit MinGW target half-uses SEH and half-use gcc-like information
19 //!    in the `seh64_gnu.rs` module.
20 //! 3. All other targets use libunwind/libgcc in the `gcc/mod.rs` module.
21 //!
22 //! More documentation about each implementation can be found in the respective
23 //! module.
24
25 #![no_std]
26 #![unstable(feature = "panic_unwind", issue = "32837")]
27 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
28        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
29        html_root_url = "https://doc.rust-lang.org/nightly/",
30        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
31
32 #![feature(allocator_api)]
33 #![feature(alloc)]
34 #![feature(core_intrinsics)]
35 #![feature(lang_items)]
36 #![feature(libc)]
37 #![feature(panic_unwind)]
38 #![feature(raw)]
39 #![feature(staged_api)]
40 #![feature(std_internals)]
41 #![feature(unwind_attributes)]
42 #![cfg_attr(target_env = "msvc", feature(raw))]
43
44 #![panic_runtime]
45 #![feature(panic_runtime)]
46
47 extern crate alloc;
48 extern crate libc;
49 #[cfg(not(any(target_env = "msvc", all(windows, target_arch = "x86_64", target_env = "gnu"))))]
50 extern crate unwind;
51
52 use alloc::boxed::Box;
53 use core::intrinsics;
54 use core::mem;
55 use core::raw;
56 use core::panic::BoxMeUp;
57
58 // Rust runtime's startup objects depend on these symbols, so make them public.
59 #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
60 pub use imp::eh_frame_registry::*;
61
62 // *-pc-windows-msvc
63 #[cfg(target_env = "msvc")]
64 #[path = "seh.rs"]
65 mod imp;
66
67 // x86_64-pc-windows-gnu
68 #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))]
69 #[path = "seh64_gnu.rs"]
70 mod imp;
71
72 // i686-pc-windows-gnu and all others
73 #[cfg(any(all(unix, not(target_os = "emscripten")),
74           target_os = "cloudabi",
75           target_os = "redox",
76           all(windows, target_arch = "x86", target_env = "gnu")))]
77 #[path = "gcc.rs"]
78 mod imp;
79
80 // emscripten
81 #[cfg(target_os = "emscripten")]
82 #[path = "emcc.rs"]
83 mod imp;
84
85 #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
86 #[path = "wasm32.rs"]
87 mod imp;
88
89 mod dwarf;
90 mod windows;
91
92 // Entry point for catching an exception, implemented using the `try` intrinsic
93 // in the compiler.
94 //
95 // The interaction between the `payload` function and the compiler is pretty
96 // hairy and tightly coupled, for more information see the compiler's
97 // implementation of this.
98 #[no_mangle]
99 pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
100                                                   data: *mut u8,
101                                                   data_ptr: *mut usize,
102                                                   vtable_ptr: *mut usize)
103                                                   -> u32 {
104     let mut payload = imp::payload();
105     if intrinsics::try(f, data, &mut payload as *mut _ as *mut _) == 0 {
106         0
107     } else {
108         let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
109         *data_ptr = obj.data as usize;
110         *vtable_ptr = obj.vtable as usize;
111         1
112     }
113 }
114
115 // Entry point for raising an exception, just delegates to the platform-specific
116 // implementation.
117 #[no_mangle]
118 #[unwind(allowed)]
119 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
120     let payload = payload as *mut &mut dyn BoxMeUp;
121     imp::panic(Box::from_raw((*payload).box_me_up()))
122 }