Skip to content

Commit 34134db

Browse files
committed
Add panic-strategy-std to bootstrap config
1 parent efb3f11 commit 34134db

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

config.example.toml

+4
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@
504504
# Defaults to rust.overflow-checks value
505505
#overflow-checks-std = rust.overflow-checks (boolean)
506506

507+
# The panic strategy used for compiling std
508+
# This is only applied for stage1 onwards
509+
#panic-strategy-std = "unwind"
510+
507511
# Debuginfo level for most of Rust code, corresponds to the `-C debuginfo=N` option of `rustc`.
508512
# `0` - no debug info
509513
# `1` - line tables only - sufficient to generate backtraces that include line

library/alloc/src/alloc.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,14 @@ pub const fn handle_alloc_error(layout: Layout) -> ! {
377377
panic!("allocation failed");
378378
}
379379

380-
#[inline]
381-
fn rt_error(layout: Layout) -> ! {
382-
unsafe {
383-
__rust_alloc_error_handler(layout.size(), layout.align());
384-
}
385-
}
386-
387380
#[cfg(not(feature = "panic_immediate_abort"))]
388381
unsafe {
382+
#[inline]
383+
fn rt_error(layout: Layout) -> ! {
384+
unsafe {
385+
__rust_alloc_error_handler(layout.size(), layout.align());
386+
}
387+
}
389388
core::intrinsics::const_eval_select((layout,), ct_error, rt_error)
390389
}
391390

library/core/src/str/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const fn slice_error_fail_ct(_: &str, _: usize, _: usize) -> ! {
104104
}
105105

106106
#[track_caller]
107+
#[cfg(not(feature = "panic_immediate_abort"))]
107108
fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! {
108109
const MAX_DISPLAY_LENGTH: usize = 256;
109110
let trunc_len = s.floor_char_boundary(MAX_DISPLAY_LENGTH);

library/std/src/panicking.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::panic::{Location, PanicInfo, PanicPayload};
1515
use crate::any::Any;
1616
use crate::fmt;
1717
use crate::intrinsics;
18-
use crate::mem::{self, ManuallyDrop};
18+
use crate::mem;
1919
use crate::process;
2020
use crate::sync::atomic::{AtomicBool, Ordering};
2121
use crate::sync::{PoisonError, RwLock};
@@ -306,7 +306,7 @@ pub mod panic_count {
306306
}
307307

308308
#[inline]
309-
pub fn increase(run_panic_hook: bool) -> Option<MustAbort> {
309+
pub fn increase(_run_panic_hook: bool) -> Option<MustAbort> {
310310
None
311311
}
312312

@@ -470,6 +470,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
470470
/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
471471
#[cfg(not(feature = "panic_immediate_abort"))]
472472
pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
473+
use mem::ManuallyDrop;
473474
union Data<F, R> {
474475
f: ManuallyDrop<F>,
475476
r: ManuallyDrop<R>,
@@ -835,7 +836,5 @@ fn rust_panic(msg: &mut dyn PanicPayload) -> ! {
835836
#[cfg_attr(not(test), rustc_std_internal_symbol)]
836837
#[cfg(feature = "panic_immediate_abort")]
837838
fn rust_panic(_: &mut dyn PanicPayload) -> ! {
838-
unsafe {
839-
crate::intrinsics::abort();
840-
}
839+
crate::intrinsics::abort();
841840
}

src/bootstrap/src/core/build_steps/compile.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,12 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
427427
.arg("--features")
428428
.arg(features);
429429
} else {
430-
features += &builder.std_features(target);
430+
features += &builder.std_features(target, stage);
431+
if stage > 0
432+
&& matches!(builder.config.rust_panic_strategy_std, crate::PanicStrategy::Abort)
433+
{
434+
cargo.rustflag("-Cpanic=abort");
435+
}
431436
features.push_str(compiler_builtins_c_feature);
432437

433438
cargo

src/bootstrap/src/core/config/config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ impl Display for DebuginfoLevel {
105105
}
106106
}
107107

108+
#[derive(Clone, Copy, Debug, Default, PartialEq, Deserialize)]
109+
#[serde(rename_all = "lowercase")]
110+
pub enum PanicStrategy {
111+
#[default]
112+
Unwind,
113+
Abort,
114+
}
115+
108116
/// LLD in bootstrap works like this:
109117
/// - Self-contained lld: use `rust-lld` from the compiler's sysroot
110118
/// - External: use an external `lld` binary
@@ -272,6 +280,7 @@ pub struct Config {
272280
pub rust_profile_generate: Option<String>,
273281
pub rust_lto: RustcLto,
274282
pub rust_validate_mir_opts: Option<u32>,
283+
pub rust_panic_strategy_std: PanicStrategy,
275284
pub llvm_profile_use: Option<String>,
276285
pub llvm_profile_generate: bool,
277286
pub llvm_libunwind_default: Option<LlvmLibunwind>,
@@ -1111,6 +1120,7 @@ define_config! {
11111120
download_rustc: Option<StringOrBool> = "download-rustc",
11121121
lto: Option<String> = "lto",
11131122
validate_mir_opts: Option<u32> = "validate-mir-opts",
1123+
panic_strategy_std: Option<PanicStrategy> = "panic-strategy-std",
11141124
}
11151125
}
11161126

@@ -1562,6 +1572,7 @@ impl Config {
15621572
stack_protector,
15631573
strip,
15641574
lld_mode,
1575+
panic_strategy_std,
15651576
} = rust;
15661577

15671578
set(&mut config.channel, channel);
@@ -1594,6 +1605,7 @@ impl Config {
15941605
debuginfo_level_std = debuginfo_level_std_toml;
15951606
debuginfo_level_tools = debuginfo_level_tools_toml;
15961607
debuginfo_level_tests = debuginfo_level_tests_toml;
1608+
set(&mut config.rust_panic_strategy_std, panic_strategy_std);
15971609

15981610
config.rust_split_debuginfo = split_debuginfo
15991611
.as_deref()

src/bootstrap/src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//! More documentation can be found in each respective module below, and you can
1717
//! also check out the `src/bootstrap/README.md` file for more information.
1818
19+
use core::config::PanicStrategy;
1920
use std::cell::{Cell, RefCell};
2021
use std::collections::{HashMap, HashSet};
2122
use std::env;
@@ -707,8 +708,12 @@ impl Build {
707708

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

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

0 commit comments

Comments
 (0)