Skip to content

Commit 8ad2379

Browse files
Don't modify libstd to dump rustc ICEs
1 parent f00c139 commit 8ad2379

File tree

4 files changed

+46
-60
lines changed

4 files changed

+46
-60
lines changed

compiler/rustc_driver_impl/src/lib.rs

+37-26
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(lazy_cell)]
99
#![feature(decl_macro)]
10-
#![feature(ice_to_disk)]
10+
#![feature(panic_update_hook)]
1111
#![feature(let_chains)]
1212
#![recursion_limit = "256"]
1313
#![allow(rustc::potential_query_instability)]
@@ -50,9 +50,9 @@ use std::collections::BTreeMap;
5050
use std::env;
5151
use std::ffi::OsString;
5252
use std::fmt::Write as _;
53-
use std::fs;
53+
use std::fs::{self, File};
5454
use std::io::{self, IsTerminal, Read, Write};
55-
use std::panic::{self, catch_unwind};
55+
use std::panic::{self, catch_unwind, PanicInfo};
5656
use std::path::PathBuf;
5757
use std::process::{self, Command, Stdio};
5858
use std::str;
@@ -1323,31 +1323,42 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
13231323
std::env::set_var("RUST_BACKTRACE", "full");
13241324
}
13251325

1326-
panic::set_hook(Box::new(move |info| {
1327-
// If the error was caused by a broken pipe then this is not a bug.
1328-
// Write the error and return immediately. See #98700.
1329-
#[cfg(windows)]
1330-
if let Some(msg) = info.payload().downcast_ref::<String>() {
1331-
if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)") {
1332-
// the error code is already going to be reported when the panic unwinds up the stack
1333-
let handler = EarlyErrorHandler::new(ErrorOutputType::default());
1334-
let _ = handler.early_error_no_abort(msg.clone());
1335-
return;
1336-
}
1337-
};
1338-
1339-
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
1340-
// Don't do this for delayed bugs, which already emit their own more useful backtrace.
1341-
if !info.payload().is::<rustc_errors::DelayedBugPanic>() {
1342-
std::panic_hook_with_disk_dump(info, ice_path().as_deref());
1326+
panic::update_hook(Box::new(
1327+
move |default_hook: &(dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static),
1328+
info: &PanicInfo<'_>| {
1329+
// If the error was caused by a broken pipe then this is not a bug.
1330+
// Write the error and return immediately. See #98700.
1331+
#[cfg(windows)]
1332+
if let Some(msg) = info.payload().downcast_ref::<String>() {
1333+
if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)")
1334+
{
1335+
// the error code is already going to be reported when the panic unwinds up the stack
1336+
let handler = EarlyErrorHandler::new(ErrorOutputType::default());
1337+
let _ = handler.early_error_no_abort(msg.clone());
1338+
return;
1339+
}
1340+
};
13431341

1344-
// Separate the output with an empty line
1345-
eprintln!();
1346-
}
1342+
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
1343+
// Don't do this for delayed bugs, which already emit their own more useful backtrace.
1344+
if !info.payload().is::<rustc_errors::DelayedBugPanic>() {
1345+
default_hook(info);
1346+
// Separate the output with an empty line
1347+
eprintln!();
1348+
1349+
if let Some(ice_path) = ice_path()
1350+
&& let Ok(mut out) =
1351+
File::options().create(true).append(true).open(&ice_path)
1352+
{
1353+
let _ =
1354+
write!(&mut out, "{info}{:#}", std::backtrace::Backtrace::force_capture());
1355+
}
1356+
}
13471357

1348-
// Print the ICE message
1349-
report_ice(info, bug_report_url, extra_info);
1350-
}));
1358+
// Print the ICE message
1359+
report_ice(info, bug_report_url, extra_info);
1360+
},
1361+
));
13511362
}
13521363

13531364
/// Prints the ICE message, including query stack, but without backtrace.

library/std/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,6 @@ pub mod alloc;
613613
// Private support modules
614614
mod panicking;
615615

616-
#[unstable(feature = "ice_to_disk", issue = "none")]
617-
pub use panicking::panic_hook_with_disk_dump;
618-
619616
#[path = "../../backtrace/src/lib.rs"]
620617
#[allow(dead_code, unused_attributes, fuzzy_provenance_casts)]
621618
mod backtrace_rs;

library/std/src/panicking.rs

+7-29
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,6 @@ where
236236

237237
/// The default panic handler.
238238
fn default_hook(info: &PanicInfo<'_>) {
239-
panic_hook_with_disk_dump(info, None)
240-
}
241-
242-
#[unstable(feature = "ice_to_disk", issue = "none")]
243-
/// The implementation of the default panic handler.
244-
///
245-
/// It can also write the backtrace to a given `path`. This functionality is used only by `rustc`.
246-
pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path::Path>) {
247239
// If this is a double panic, make sure that we print a backtrace
248240
// for this panic. Otherwise only print it if logging is enabled.
249241
let backtrace = if info.force_no_backtrace() {
@@ -267,7 +259,7 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
267259
let thread = thread_info::current_thread();
268260
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
269261

270-
let write = |err: &mut dyn crate::io::Write, backtrace: Option<BacktraceStyle>| {
262+
let write = |err: &mut dyn crate::io::Write| {
271263
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
272264

273265
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
@@ -281,37 +273,23 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
281273
}
282274
Some(BacktraceStyle::Off) => {
283275
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
284-
if let Some(path) = path {
285-
let _ = writeln!(
286-
err,
287-
"note: a backtrace for this error was stored at `{}`",
288-
path.display(),
289-
);
290-
} else {
291-
let _ = writeln!(
292-
err,
293-
"note: run with `RUST_BACKTRACE=1` environment variable to display a \
276+
let _ = writeln!(
277+
err,
278+
"note: run with `RUST_BACKTRACE=1` environment variable to display a \
294279
backtrace"
295-
);
296-
}
280+
);
297281
}
298282
}
299283
// If backtraces aren't supported or are forced-off, do nothing.
300284
None => {}
301285
}
302286
};
303287

304-
if let Some(path) = path
305-
&& let Ok(mut out) = crate::fs::File::options().create(true).append(true).open(&path)
306-
{
307-
write(&mut out, BacktraceStyle::full());
308-
}
309-
310288
if let Some(local) = set_output_capture(None) {
311-
write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()), backtrace);
289+
write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
312290
set_output_capture(Some(local));
313291
} else if let Some(mut out) = panic_output() {
314-
write(&mut out, backtrace);
292+
write(&mut out);
315293
}
316294
}
317295

tests/run-make/dump-ice-to-disk/check.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ rm $TMPDIR/rustc-ice-*.txt
2222
# Explicitly disabling ICE dump
2323
export RUSTC_ICE=0
2424
$RUSTC src/lib.rs -Z treat-err-as-bug=1 1>$TMPDIR/rust-test-disabled.log 2>&1
25-
should_be_empty_tmp=$(ls -l $TMPDIR/rustc-ice-*.txt | wc -l)
26-
should_be_empty_dot=$(ls -l ./rustc-ice-*.txt | wc -l)
25+
should_be_empty_tmp=$(ls -l $TMPDIR/rustc-ice-*.txt 2>/dev/null | wc -l)
26+
should_be_empty_dot=$(ls -l ./rustc-ice-*.txt 2>/dev/null | wc -l)
2727

2828
echo "#### ICE Dump content:"
2929
echo $content

0 commit comments

Comments
 (0)