Skip to content

Commit 0ec321f

Browse files
committed
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to alter the behavior of panics at compile time. A new compiler flag, `-C panic`, is added and accepts the values `unwind` or `panic`, with the default being `unwind`. This model affects how code is generated for the local crate, skipping generation of landing pads with `-C panic=abort`. [RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md Panic implementations are then provided by crates tagged with `#![panic_runtime]` and lazily required by crates with `#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic runtime must match the final product, and if the panic strategy is not `abort` then the entire DAG must have the same panic strategy. With the `-C panic=abort` strategy, users can expect a stable method to disable generation of landing pads, improving optimization in niche scenarios, decreasing compile time, and decreasing output binary size. With the `-C panic=unwind` strategy users can expect the existing ability to isolate failure in Rust code from the outside world. Organizationally, this commit dismantles the `sys_common::unwind` module in favor of some bits moving part of it to `libpanic_unwind` and the rest into the `panicking` module in libstd. The custom panic runtime support is pretty similar to the custom allocator support with the only major difference being how the panic runtime is injected (takes the `-C panic` flag into account).
1 parent 32683ce commit 0ec321f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1997
-742
lines changed

mk/crates.mk

+14-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ TARGET_CRATES := libc std term \
5353
getopts collections test rand \
5454
core alloc \
5555
rustc_unicode rustc_bitflags \
56-
alloc_system alloc_jemalloc
56+
alloc_system alloc_jemalloc \
57+
panic_abort panic_unwind unwind
5758
RUSTC_CRATES := rustc rustc_typeck rustc_mir rustc_borrowck rustc_resolve rustc_driver \
5859
rustc_trans rustc_back rustc_llvm rustc_privacy rustc_lint \
5960
rustc_data_structures rustc_platform_intrinsics \
@@ -72,10 +73,18 @@ DEPS_libc := core
7273
DEPS_rand := core
7374
DEPS_rustc_bitflags := core
7475
DEPS_rustc_unicode := core
76+
DEPS_panic_abort := libc alloc
77+
DEPS_panic_unwind := libc alloc unwind
78+
DEPS_unwind := libc
79+
80+
# FIXME(stage0): change this to just `RUSTFLAGS_panic_abort := ...`
81+
RUSTFLAGS1_panic_abort := -C panic=abort
82+
RUSTFLAGS2_panic_abort := -C panic=abort
83+
RUSTFLAGS3_panic_abort := -C panic=abort
7584

7685
DEPS_std := core libc rand alloc collections rustc_unicode \
7786
native:backtrace \
78-
alloc_system
87+
alloc_system panic_abort panic_unwind unwind
7988
DEPS_arena := std
8089
DEPS_glob := std
8190
DEPS_flate := std native:miniz
@@ -148,6 +157,9 @@ ONLY_RLIB_rustc_unicode := 1
148157
ONLY_RLIB_rustc_bitflags := 1
149158
ONLY_RLIB_alloc_system := 1
150159
ONLY_RLIB_alloc_jemalloc := 1
160+
ONLY_RLIB_panic_unwind := 1
161+
ONLY_RLIB_panic_abort := 1
162+
ONLY_RLIB_unwind := 1
151163

152164
TARGET_SPECIFIC_alloc_jemalloc := 1
153165

mk/tests.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ DEPS_collectionstest :=
2323
$(eval $(call RUST_CRATE,collectionstest))
2424

2525
TEST_TARGET_CRATES = $(filter-out core rustc_unicode alloc_system libc \
26-
alloc_jemalloc,$(TARGET_CRATES)) \
26+
alloc_jemalloc panic_unwind \
27+
panic_abort,$(TARGET_CRATES)) \
2728
collectionstest coretest
2829
TEST_DOC_CRATES = $(DOC_CRATES) arena flate fmt_macros getopts graphviz \
2930
log rand rbml serialize syntax term test

src/bootstrap/rustc.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ fn main() {
4848
} else {
4949
env::var_os("RUSTC_REAL").unwrap()
5050
};
51+
let stage = env::var("RUSTC_STAGE").unwrap();
5152

5253
let mut cmd = Command::new(rustc);
5354
cmd.args(&args)
54-
.arg("--cfg").arg(format!("stage{}", env::var("RUSTC_STAGE").unwrap()));
55+
.arg("--cfg").arg(format!("stage{}", stage));
5556

5657
if let Some(target) = target {
5758
// The stage0 compiler has a special sysroot distinct from what we
@@ -78,6 +79,22 @@ fn main() {
7879
cmd.args(&s.split(" ").filter(|s| !s.is_empty()).collect::<Vec<_>>());
7980
}
8081

82+
// If we're compiling specifically the `panic_abort` crate then we pass
83+
// the `-C panic=abort` option. Note that we do not do this for any
84+
// other crate intentionally as this is the only crate for now that we
85+
// ship with panic=abort.
86+
//
87+
// This... is a bit of a hack how we detect this. Ideally this
88+
// information should be encoded in the crate I guess? Would likely
89+
// require an RFC amendment to RFC 1513, however.
90+
let is_panic_abort = args.windows(2).any(|a| {
91+
&*a[0] == "--crate-name" && &*a[1] == "panic_abort"
92+
});
93+
// FIXME(stage0): remove this `stage != "0"` condition
94+
if is_panic_abort && stage != "0" {
95+
cmd.arg("-C").arg("panic=abort");
96+
}
97+
8198
// Set various options from config.toml to configure how we're building
8299
// code.
83100
if env::var("RUSTC_DEBUGINFO") == Ok("true".to_string()) {

src/liballoc_system/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
form or name",
1919
issue = "27783")]
2020
#![feature(allocator)]
21-
#![feature(libc)]
2221
#![feature(staged_api)]
23-
24-
extern crate libc;
22+
#![cfg_attr(unix, feature(libc))]
2523

2624
// The minimum alignment guaranteed by the architecture. This value is used to
2725
// add fast paths for low alignment values. In practice, the alignment is a
@@ -72,9 +70,10 @@ pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
7270

7371
#[cfg(unix)]
7472
mod imp {
73+
extern crate libc;
74+
7575
use core::cmp;
7676
use core::ptr;
77-
use libc;
7877
use MIN_ALIGN;
7978

8079
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {

src/libpanic_abort/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
name = "panic_abort"
4+
version = "0.0.0"
5+
6+
[lib]
7+
path = "lib.rs"
8+
9+
[dependencies]
10+
core = { path = "../libcore" }
11+
libc = { path = "../rustc/libc_shim" }

src/libpanic_abort/lib.rs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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() {}

src/libpanic_unwind/Cargo.lock

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libpanic_unwind/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
name = "panic_unwind"
4+
version = "0.0.0"
5+
6+
[lib]
7+
path = "lib.rs"
8+
9+
[dependencies]
10+
alloc = { path = "../liballoc" }
11+
core = { path = "../libcore" }
12+
libc = { path = "../rustc/libc_shim" }
13+
unwind = { path = "../libunwind" }

src/libstd/sys/common/dwarf/eh.rs src/libpanic_unwind/dwarf/eh.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#![allow(non_upper_case_globals)]
2222
#![allow(unused)]
2323

24-
use prelude::v1::*;
25-
use sys_common::dwarf::DwarfReader;
24+
use dwarf::DwarfReader;
2625
use core::mem;
2726

2827
pub const DW_EH_PE_omit : u8 = 0xFF;

src/libstd/sys/common/dwarf/mod.rs src/libpanic_unwind/dwarf/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
pub mod eh;
2020

21-
use prelude::v1::*;
2221
use core::mem;
2322

2423
pub struct DwarfReader {

0 commit comments

Comments
 (0)