|
| 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 Rust panics via process aborts |
| 12 | +//! |
| 13 | +//! When compared to the implementation via unwinding, this crate is *much* |
| 14 | +//! simpler! That being said, it's not quite as versatile, but here goes! |
| 15 | +
|
| 16 | +#![no_std] |
| 17 | +#![crate_name = "panic_abort"] |
| 18 | +#![crate_type = "rlib"] |
| 19 | +#![unstable(feature = "panic_abort", issue = "32837")] |
| 20 | +#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", |
| 21 | + html_favicon_url = "https://doc.rust-lang.org/favicon.ico", |
| 22 | + html_root_url = "https://doc.rust-lang.org/nightly/", |
| 23 | + issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")] |
| 24 | +#![cfg_attr(not(stage0), deny(warnings))] |
| 25 | + |
| 26 | +#![feature(staged_api)] |
| 27 | + |
| 28 | +#![cfg_attr(not(stage0), panic_runtime)] |
| 29 | +#![cfg_attr(not(stage0), feature(panic_runtime))] |
| 30 | +#![cfg_attr(unix, feature(libc))] |
| 31 | +#![cfg_attr(windows, feature(core_intrinsics))] |
| 32 | + |
| 33 | +// Rust's "try" function, but if we're aborting on panics we just call the |
| 34 | +// function as there's nothing else we need to do here. |
| 35 | +#[no_mangle] |
| 36 | +pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8), |
| 37 | + data: *mut u8, |
| 38 | + _data_ptr: *mut usize, |
| 39 | + _vtable_ptr: *mut usize) -> u32 { |
| 40 | + f(data); |
| 41 | + 0 |
| 42 | +} |
| 43 | + |
| 44 | +// "Leak" the payload and shim to the relevant abort on the platform in |
| 45 | +// question. |
| 46 | +// |
| 47 | +// For Unix we just use `abort` from libc as it'll trigger debuggers, core |
| 48 | +// dumps, etc, as one might expect. On Windows, however, the best option we have |
| 49 | +// is the `__fastfail` intrinsics, but that's unfortunately not defined in LLVM, |
| 50 | +// and the `RaiseFailFastException` function isn't available until Windows 7 |
| 51 | +// which would break compat with XP. For now just use `intrinsics::abort` which |
| 52 | +// will kill us with an illegal instruction, which will do a good enough job for |
| 53 | +// now hopefully. |
| 54 | +#[no_mangle] |
| 55 | +pub unsafe extern fn __rust_start_panic(_data: usize, _vtable: usize) -> u32 { |
| 56 | + return abort(); |
| 57 | + |
| 58 | + #[cfg(unix)] |
| 59 | + unsafe fn abort() -> ! { |
| 60 | + extern crate libc; |
| 61 | + libc::abort(); |
| 62 | + } |
| 63 | + |
| 64 | + #[cfg(windows)] |
| 65 | + unsafe fn abort() -> ! { |
| 66 | + core::intrinsics::abort(); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// This... is a bit of an oddity. The tl;dr; is that this is required to link |
| 71 | +// correctly, the longer explanation is below. |
| 72 | +// |
| 73 | +// Right now the binaries of libcore/libstd that we ship are all compiled with |
| 74 | +// `-C panic=unwind`. This is done to ensure that the binaries are maximally |
| 75 | +// compatible with as many situations as possible. The compiler, however, |
| 76 | +// requires a "personality function" for all functions compiled with `-C |
| 77 | +// panic=unwind`. This personality function is hardcoded to the symbol |
| 78 | +// `rust_eh_personality` and is defined by the `eh_personality` lang item. |
| 79 | +// |
| 80 | +// So... why not just define that lang item here? Good question! The way that |
| 81 | +// panic runtimes are linked in is actually a little subtle in that they're |
| 82 | +// "sort of" in the compiler's crate store, but only actually linked if another |
| 83 | +// isn't actually linked. This ends up meaning that both this crate and the |
| 84 | +// panic_unwind crate can appear in the compiler's crate store, and if both |
| 85 | +// define the `eh_personality` lang item then that'll hit an error. |
| 86 | +// |
| 87 | +// To handle this the compiler only requires the `eh_personality` is defined if |
| 88 | +// the panic runtime being linked in is the unwinding runtime, and otherwise |
| 89 | +// it's not required to be defined (rightfully so). In this case, however, this |
| 90 | +// library just defines this symbol so there's at least some personality |
| 91 | +// somewhere. |
| 92 | +// |
| 93 | +// Essentially this symbol is just defined to get wired up to libcore/libstd |
| 94 | +// binaries, but it should never be called as we don't link in an unwinding |
| 95 | +// runtime at all. |
| 96 | +#[no_mangle] |
| 97 | +#[cfg(not(stage0))] |
| 98 | +pub extern fn rust_eh_personality() {} |
| 99 | + |
| 100 | +// Similar to above, this corresponds to the `eh_unwind_resume` lang item that's |
| 101 | +// only used on Windows currently. |
| 102 | +#[no_mangle] |
| 103 | +#[cfg(all(not(stage0), target_os = "windows", target_env = "gnu"))] |
| 104 | +pub extern fn rust_eh_unwind_resume() {} |
| 105 | + |
| 106 | +#[no_mangle] |
| 107 | +#[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))] |
| 108 | +pub extern fn rust_eh_register_frames() {} |
| 109 | + |
| 110 | +#[no_mangle] |
| 111 | +#[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))] |
| 112 | +pub extern fn rust_eh_unregister_frames() {} |
0 commit comments