Skip to content

Commit c98ea0d

Browse files
committed
Auto merge of rust-lang#111769 - saethlin:ctfe-backtrace-ctrlc, r=RalfJung
Print a backtrace in const eval if interrupted Demo: ```rust #![feature(const_eval_limit)] #![const_eval_limit = "0"] const OW: u64 = { let mut res: u64 = 0; let mut i = 0; while i < u64::MAX { res = res.wrapping_add(i); i += 1; } res }; fn main() { println!("{}", OW); } ``` ``` ╭ ➜ ben@archlinux:~/rust ╰ ➤ rustc +stage1 spin.rs ^Cerror[E0080]: evaluation of constant value failed --> spin.rs:8:33 | 8 | res = res.wrapping_add(i); | ^ Compilation was interrupted note: erroneous constant used --> spin.rs:15:20 | 15 | println!("{}", OW); | ^^ note: erroneous constant used --> spin.rs:15:20 | 15 | println!("{}", OW); | ^^ | = note: this note originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to previous error For more information about this error, try `rustc --explain E0080`. ```
2 parents 5f2c7d2 + 9e0d1a3 commit c98ea0d

File tree

13 files changed

+56
-18
lines changed

13 files changed

+56
-18
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3784,6 +3784,7 @@ dependencies = [
37843784
name = "rustc_driver_impl"
37853785
version = "0.0.0"
37863786
dependencies = [
3787+
"ctrlc",
37873788
"libc",
37883789
"rustc_ast",
37893790
"rustc_ast_lowering",

compiler/rustc_const_eval/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ const_eval_intern_kind = {$kind ->
146146
*[other] {""}
147147
}
148148
149+
const_eval_interrupted = compilation was interrupted
150+
149151
const_eval_invalid_align_details =
150152
invalid align passed to `{$name}`: {$align} is {$err_kind ->
151153
[not_power_of_two] not a power of 2

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::atomic::Ordering::Relaxed;
2+
13
use either::{Left, Right};
24

35
use rustc_hir::def::DefKind;
@@ -22,6 +24,7 @@ use crate::interpret::{
2224
InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking,
2325
StackPopCleanup,
2426
};
27+
use crate::CTRL_C_RECEIVED;
2528

2629
// Returns a pointer to where the result lives
2730
#[instrument(level = "trace", skip(ecx, body))]
@@ -79,7 +82,11 @@ fn eval_body_using_ecx<'mir, 'tcx, R: InterpretationResult<'tcx>>(
7982
ecx.storage_live_for_always_live_locals()?;
8083

8184
// The main interpreter loop.
82-
while ecx.step()? {}
85+
while ecx.step()? {
86+
if CTRL_C_RECEIVED.load(Relaxed) {
87+
throw_exhaust!(Interrupted);
88+
}
89+
}
8390

8491
// Intern the result
8592
intern_const_alloc_recursive(ecx, intern_kind, &ret)?;

compiler/rustc_const_eval/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ impl ReportErrorExt for ResourceExhaustionInfo {
884884
ResourceExhaustionInfo::StackFrameLimitReached => const_eval_stack_frame_limit_reached,
885885
ResourceExhaustionInfo::MemoryExhausted => const_eval_memory_exhausted,
886886
ResourceExhaustionInfo::AddressSpaceFull => const_eval_address_space_full,
887+
ResourceExhaustionInfo::Interrupted => const_eval_interrupted,
887888
}
888889
}
889890
fn add_args<G: EmissionGuarantee>(self, _: &mut Diag<'_, G>) {}

compiler/rustc_const_eval/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub mod interpret;
3232
pub mod transform;
3333
pub mod util;
3434

35+
use std::sync::atomic::AtomicBool;
36+
3537
pub use errors::ReportErrorExt;
3638

3739
use rustc_middle::{ty, util::Providers};
@@ -58,3 +60,8 @@ pub fn provide(providers: &mut Providers) {
5860
util::check_validity_requirement(tcx, init_kind, param_env_and_ty)
5961
};
6062
}
63+
64+
/// `rustc_driver::main` installs a handler that will set this to `true` if
65+
/// the compiler has been sent a request to shut down, such as by a Ctrl-C.
66+
/// This static lives here because it is only read by the interpreter.
67+
pub static CTRL_C_RECEIVED: AtomicBool = AtomicBool::new(false);

compiler/rustc_driver_impl/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
ctrlc = "3.4.4"
89
rustc_ast = { path = "../rustc_ast" }
910
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
1011
rustc_ast_passes = { path = "../rustc_ast_passes" }

compiler/rustc_driver_impl/src/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate tracing;
1919

2020
use rustc_ast as ast;
2121
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
22+
use rustc_const_eval::CTRL_C_RECEIVED;
2223
use rustc_data_structures::profiling::{
2324
get_resident_set_size, print_time_passes_entry, TimePassesFormat,
2425
};
@@ -1518,6 +1519,22 @@ pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
15181519
}
15191520
}
15201521

1522+
/// Install our usual `ctrlc` handler, which sets [`rustc_const_eval::CTRL_C_RECEIVED`].
1523+
/// Making this handler optional lets tools can install a different handler, if they wish.
1524+
pub fn install_ctrlc_handler() {
1525+
ctrlc::set_handler(move || {
1526+
// Indicate that we have been signaled to stop. If we were already signaled, exit
1527+
// immediately. In our interpreter loop we try to consult this value often, but if for
1528+
// whatever reason we don't get to that check or the cleanup we do upon finding that
1529+
// this bool has become true takes a long time, the exit here will promptly exit the
1530+
// process on the second Ctrl-C.
1531+
if CTRL_C_RECEIVED.swap(true, Ordering::Relaxed) {
1532+
std::process::exit(1);
1533+
}
1534+
})
1535+
.expect("Unable to install ctrlc handler");
1536+
}
1537+
15211538
pub fn main() -> ! {
15221539
let start_time = Instant::now();
15231540
let start_rss = get_resident_set_size();
@@ -1528,6 +1545,8 @@ pub fn main() -> ! {
15281545
signal_handler::install();
15291546
let mut callbacks = TimePassesCallbacks::default();
15301547
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
1548+
install_ctrlc_handler();
1549+
15311550
let exit_code = catch_with_exit_code(|| {
15321551
RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks)
15331552
.set_using_internal_features(using_internal_features)

compiler/rustc_middle/src/mir/interpret/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ pub enum ResourceExhaustionInfo {
482482
MemoryExhausted,
483483
/// The address space (of the target) is full.
484484
AddressSpaceFull,
485+
/// The compiler got an interrupt signal (a user ran out of patience).
486+
Interrupted,
485487
}
486488

487489
/// A trait for machine-specific errors (or other "machine stop" conditions).

src/tools/miri/src/bin/miri.rs

+4
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ fn main() {
364364
let args = rustc_driver::args::raw_args(&early_dcx)
365365
.unwrap_or_else(|_| std::process::exit(rustc_driver::EXIT_FAILURE));
366366

367+
// Install the ctrlc handler that sets `rustc_const_eval::CTRL_C_RECEIVED`, even if
368+
// MIRI_BE_RUSTC is set.
369+
rustc_driver::install_ctrlc_handler();
370+
367371
// If the environment asks us to actually be rustc, then do that.
368372
if let Some(crate_kind) = env::var_os("MIRI_BE_RUSTC") {
369373
// Earliest rustc setup.

src/tools/miri/src/concurrency/thread.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
use std::cell::RefCell;
44
use std::collections::hash_map::Entry;
55
use std::num::TryFromIntError;
6-
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
6+
use std::sync::atomic::Ordering::Relaxed;
77
use std::task::Poll;
88
use std::time::{Duration, SystemTime};
99

1010
use either::Either;
1111

12+
use rustc_const_eval::CTRL_C_RECEIVED;
1213
use rustc_data_structures::fx::FxHashMap;
1314
use rustc_hir::def_id::DefId;
1415
use rustc_index::{Idx, IndexVec};
@@ -1045,21 +1046,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10451046
/// Run the core interpreter loop. Returns only when an interrupt occurs (an error or program
10461047
/// termination).
10471048
fn run_threads(&mut self) -> InterpResult<'tcx, !> {
1048-
static SIGNALED: AtomicBool = AtomicBool::new(false);
1049-
ctrlc::set_handler(move || {
1050-
// Indicate that we have ben signaled to stop. If we were already signaled, exit
1051-
// immediately. In our interpreter loop we try to consult this value often, but if for
1052-
// whatever reason we don't get to that check or the cleanup we do upon finding that
1053-
// this bool has become true takes a long time, the exit here will promptly exit the
1054-
// process on the second Ctrl-C.
1055-
if SIGNALED.swap(true, Relaxed) {
1056-
std::process::exit(1);
1057-
}
1058-
})
1059-
.unwrap();
1060-
let this = self.eval_context_mut();
1049+
let this = self.eval_context_mut();
10611050
loop {
1062-
if SIGNALED.load(Relaxed) {
1051+
if CTRL_C_RECEIVED.load(Relaxed) {
10631052
this.machine.handle_abnormal_termination();
10641053
std::process::exit(1);
10651054
}

src/tools/tidy/src/deps.rs

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
208208
"byteorder", // via ruzstd in object in thorin-dwp
209209
"cc",
210210
"cfg-if",
211+
"cfg_aliases",
211212
"compiler_builtins",
212213
"cpufeatures",
213214
"crc32fast",
@@ -216,6 +217,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
216217
"crossbeam-epoch",
217218
"crossbeam-utils",
218219
"crypto-common",
220+
"ctrlc",
219221
"darling",
220222
"darling_core",
221223
"darling_macro",
@@ -281,6 +283,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
281283
"memmap2",
282284
"memoffset",
283285
"miniz_oxide",
286+
"nix",
284287
"nu-ansi-term",
285288
"num-conv",
286289
"num_cpus",

tests/run-make/jobserver-error/Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ include ../tools.mk
44
# ignore-cross-compile
55

66
# Test compiler behavior in case environment specifies wrong jobserver.
7+
# Note that by default, the compiler uses file descriptors 0 (stdin), 1 (stdout), 2 (stderr),
8+
# but also 3 and 4 for either end of the ctrl-c signal handler self-pipe.
79

810
all:
9-
bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=3,3" $(RUSTC)' 2>&1 | diff cannot_open_fd.stderr -
11+
bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=5,5" $(RUSTC)' 2>&1 | diff cannot_open_fd.stderr -
1012
bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=3,3" $(RUSTC) - 3</dev/null' 2>&1 | diff not_a_pipe.stderr -
1113

1214
# This test randomly fails, see https://github.com/rust-lang/rust/issues/110321

tests/run-make/jobserver-error/cannot_open_fd.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: failed to connect to jobserver from environment variable `MAKEFLAGS="--jobserver-auth=3,3"`: cannot open file descriptor 3 from the jobserver environment variable value: Bad file descriptor (os error 9)
1+
warning: failed to connect to jobserver from environment variable `MAKEFLAGS="--jobserver-auth=5,5"`: cannot open file descriptor 5 from the jobserver environment variable value: Bad file descriptor (os error 9)
22
|
33
= note: the build environment is likely misconfigured
44

0 commit comments

Comments
 (0)