From 34134dbd88347a1e310d83baec1aba4f9e4b939d Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 6 Jan 2024 17:16:45 +0000 Subject: [PATCH] Add `panic-strategy-std` to bootstrap config --- config.example.toml | 4 ++++ library/alloc/src/alloc.rs | 13 ++++++------- library/core/src/str/mod.rs | 1 + library/std/src/panicking.rs | 9 ++++----- src/bootstrap/src/core/build_steps/compile.rs | 7 ++++++- src/bootstrap/src/core/config/config.rs | 12 ++++++++++++ src/bootstrap/src/lib.rs | 9 +++++++-- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/config.example.toml b/config.example.toml index f1ea6bac3ca16..64e705f21e4da 100644 --- a/config.example.toml +++ b/config.example.toml @@ -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 diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 1663aa84921c9..61fb71b6f32a3 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -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) } diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index a22c46edce254..c7268a6ce04d4 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -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); diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 66b4ec37c8ec5..7fa3351469c85 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -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}; @@ -306,7 +306,7 @@ pub mod panic_count { } #[inline] - pub fn increase(run_panic_hook: bool) -> Option { + pub fn increase(_run_panic_hook: bool) -> Option { None } @@ -470,6 +470,7 @@ pub unsafe fn r#try R>(f: F) -> Result> /// 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: F) -> Result> { + use mem::ManuallyDrop; union Data { f: ManuallyDrop, r: ManuallyDrop, @@ -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(); } diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index 33d0dffe26e6e..e173b2ac90db8 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -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 diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 3ac3e54563148..a2fa8ad07e41f 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -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 @@ -272,6 +280,7 @@ pub struct Config { pub rust_profile_generate: Option, pub rust_lto: RustcLto, pub rust_validate_mir_opts: Option, + pub rust_panic_strategy_std: PanicStrategy, pub llvm_profile_use: Option, pub llvm_profile_generate: bool, pub llvm_libunwind_default: Option, @@ -1111,6 +1120,7 @@ define_config! { download_rustc: Option = "download-rustc", lto: Option = "lto", validate_mir_opts: Option = "validate-mir-opts", + panic_strategy_std: Option = "panic-strategy-std", } } @@ -1562,6 +1572,7 @@ impl Config { stack_protector, strip, lld_mode, + panic_strategy_std, } = rust; set(&mut config.channel, channel); @@ -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() diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 871318de5955e..16d609a238d8f 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -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; @@ -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"),