Skip to content

Commit

Permalink
Add panic-strategy-std to bootstrap config
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Jan 6, 2024
1 parent efb3f11 commit 34134db
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
4 changes: 4 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@
# Defaults to rust.overflow-checks value
#overflow-checks-std = rust.overflow-checks (boolean)

# The panic strategy used for compiling std
# This is only applied for stage1 onwards
#panic-strategy-std = "unwind"

# Debuginfo level for most of Rust code, corresponds to the `-C debuginfo=N` option of `rustc`.
# `0` - no debug info
# `1` - line tables only - sufficient to generate backtraces that include line
Expand Down
13 changes: 6 additions & 7 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,14 @@ pub const fn handle_alloc_error(layout: Layout) -> ! {
panic!("allocation failed");
}

#[inline]
fn rt_error(layout: Layout) -> ! {
unsafe {
__rust_alloc_error_handler(layout.size(), layout.align());
}
}

#[cfg(not(feature = "panic_immediate_abort"))]
unsafe {
#[inline]
fn rt_error(layout: Layout) -> ! {
unsafe {
__rust_alloc_error_handler(layout.size(), layout.align());
}
}
core::intrinsics::const_eval_select((layout,), ct_error, rt_error)
}

Expand Down
1 change: 1 addition & 0 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const fn slice_error_fail_ct(_: &str, _: usize, _: usize) -> ! {
}

#[track_caller]
#[cfg(not(feature = "panic_immediate_abort"))]
fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! {
const MAX_DISPLAY_LENGTH: usize = 256;
let trunc_len = s.floor_char_boundary(MAX_DISPLAY_LENGTH);
Expand Down
9 changes: 4 additions & 5 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::panic::{Location, PanicInfo, PanicPayload};
use crate::any::Any;
use crate::fmt;
use crate::intrinsics;
use crate::mem::{self, ManuallyDrop};
use crate::mem;
use crate::process;
use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sync::{PoisonError, RwLock};
Expand Down Expand Up @@ -306,7 +306,7 @@ pub mod panic_count {
}

#[inline]
pub fn increase(run_panic_hook: bool) -> Option<MustAbort> {
pub fn increase(_run_panic_hook: bool) -> Option<MustAbort> {
None
}

Expand Down Expand Up @@ -470,6 +470,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
#[cfg(not(feature = "panic_immediate_abort"))]
pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
use mem::ManuallyDrop;
union Data<F, R> {
f: ManuallyDrop<F>,
r: ManuallyDrop<R>,
Expand Down Expand Up @@ -835,7 +836,5 @@ fn rust_panic(msg: &mut dyn PanicPayload) -> ! {
#[cfg_attr(not(test), rustc_std_internal_symbol)]
#[cfg(feature = "panic_immediate_abort")]
fn rust_panic(_: &mut dyn PanicPayload) -> ! {
unsafe {
crate::intrinsics::abort();
}
crate::intrinsics::abort();
}
7 changes: 6 additions & 1 deletion src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,12 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
.arg("--features")
.arg(features);
} else {
features += &builder.std_features(target);
features += &builder.std_features(target, stage);
if stage > 0
&& matches!(builder.config.rust_panic_strategy_std, crate::PanicStrategy::Abort)
{
cargo.rustflag("-Cpanic=abort");
}
features.push_str(compiler_builtins_c_feature);

cargo
Expand Down
12 changes: 12 additions & 0 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ impl Display for DebuginfoLevel {
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PanicStrategy {
#[default]
Unwind,
Abort,
}

/// LLD in bootstrap works like this:
/// - Self-contained lld: use `rust-lld` from the compiler's sysroot
/// - External: use an external `lld` binary
Expand Down Expand Up @@ -272,6 +280,7 @@ pub struct Config {
pub rust_profile_generate: Option<String>,
pub rust_lto: RustcLto,
pub rust_validate_mir_opts: Option<u32>,
pub rust_panic_strategy_std: PanicStrategy,
pub llvm_profile_use: Option<String>,
pub llvm_profile_generate: bool,
pub llvm_libunwind_default: Option<LlvmLibunwind>,
Expand Down Expand Up @@ -1111,6 +1120,7 @@ define_config! {
download_rustc: Option<StringOrBool> = "download-rustc",
lto: Option<String> = "lto",
validate_mir_opts: Option<u32> = "validate-mir-opts",
panic_strategy_std: Option<PanicStrategy> = "panic-strategy-std",
}
}

Expand Down Expand Up @@ -1562,6 +1572,7 @@ impl Config {
stack_protector,
strip,
lld_mode,
panic_strategy_std,
} = rust;

set(&mut config.channel, channel);
Expand Down Expand Up @@ -1594,6 +1605,7 @@ impl Config {
debuginfo_level_std = debuginfo_level_std_toml;
debuginfo_level_tools = debuginfo_level_tools_toml;
debuginfo_level_tests = debuginfo_level_tests_toml;
set(&mut config.rust_panic_strategy_std, panic_strategy_std);

config.rust_split_debuginfo = split_debuginfo
.as_deref()
Expand Down
9 changes: 7 additions & 2 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! More documentation can be found in each respective module below, and you can
//! also check out the `src/bootstrap/README.md` file for more information.

use core::config::PanicStrategy;
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::env;
Expand Down Expand Up @@ -707,8 +708,12 @@ impl Build {

/// Gets the space-separated set of activated features for the standard
/// library.
fn std_features(&self, target: TargetSelection) -> String {
let mut features = " panic-unwind".to_string();
fn std_features(&self, target: TargetSelection, stage: u32) -> String {
let mut features = match self.config.rust_panic_strategy_std {
PanicStrategy::Abort if stage > 0 => " panic_immediate_abort",
_ => " panic-unwind",
}
.to_string();

match self.config.llvm_libunwind(target) {
LlvmLibunwind::InTree => features.push_str(" llvm-libunwind"),
Expand Down

0 comments on commit 34134db

Please sign in to comment.