Skip to content

Commit 5d064f2

Browse files
compiler-errorscuviper
authored andcommitted
Don't modify libstd to dump rustc ICEs
(cherry picked from commit 8ad2379)
1 parent dba7025 commit 5d064f2

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;
@@ -1363,31 +1363,42 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
13631363
std::env::set_var("RUST_BACKTRACE", "full");
13641364
}
13651365

1366-
panic::set_hook(Box::new(move |info| {
1367-
// If the error was caused by a broken pipe then this is not a bug.
1368-
// Write the error and return immediately. See #98700.
1369-
#[cfg(windows)]
1370-
if let Some(msg) = info.payload().downcast_ref::<String>() {
1371-
if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)") {
1372-
// the error code is already going to be reported when the panic unwinds up the stack
1373-
let handler = EarlyErrorHandler::new(ErrorOutputType::default());
1374-
let _ = handler.early_error_no_abort(msg.clone());
1375-
return;
1376-
}
1377-
};
1378-
1379-
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
1380-
// Don't do this for delayed bugs, which already emit their own more useful backtrace.
1381-
if !info.payload().is::<rustc_errors::DelayedBugPanic>() {
1382-
std::panic_hook_with_disk_dump(info, ice_path().as_deref());
1366+
panic::update_hook(Box::new(
1367+
move |default_hook: &(dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static),
1368+
info: &PanicInfo<'_>| {
1369+
// If the error was caused by a broken pipe then this is not a bug.
1370+
// Write the error and return immediately. See #98700.
1371+
#[cfg(windows)]
1372+
if let Some(msg) = info.payload().downcast_ref::<String>() {
1373+
if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)")
1374+
{
1375+
// the error code is already going to be reported when the panic unwinds up the stack
1376+
let handler = EarlyErrorHandler::new(ErrorOutputType::default());
1377+
let _ = handler.early_error_no_abort(msg.clone());
1378+
return;
1379+
}
1380+
};
13831381

1384-
// Separate the output with an empty line
1385-
eprintln!();
1386-
}
1382+
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
1383+
// Don't do this for delayed bugs, which already emit their own more useful backtrace.
1384+
if !info.payload().is::<rustc_errors::DelayedBugPanic>() {
1385+
default_hook(info);
1386+
// Separate the output with an empty line
1387+
eprintln!();
1388+
1389+
if let Some(ice_path) = ice_path()
1390+
&& let Ok(mut out) =
1391+
File::options().create(true).append(true).open(&ice_path)
1392+
{
1393+
let _ =
1394+
write!(&mut out, "{info}{:#}", std::backtrace::Backtrace::force_capture());
1395+
}
1396+
}
13871397

1388-
// Print the ICE message
1389-
report_ice(info, bug_report_url, extra_info);
1390-
}));
1398+
// Print the ICE message
1399+
report_ice(info, bug_report_url, extra_info);
1400+
},
1401+
));
13911402
}
13921403

13931404
/// 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 panic_count::get_count() >= 2 {
@@ -265,7 +257,7 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
265257
let thread = thread_info::current_thread();
266258
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
267259

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

271263
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
@@ -279,37 +271,23 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
279271
}
280272
Some(BacktraceStyle::Off) => {
281273
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
282-
if let Some(path) = path {
283-
let _ = writeln!(
284-
err,
285-
"note: a backtrace for this error was stored at `{}`",
286-
path.display(),
287-
);
288-
} else {
289-
let _ = writeln!(
290-
err,
291-
"note: run with `RUST_BACKTRACE=1` environment variable to display a \
274+
let _ = writeln!(
275+
err,
276+
"note: run with `RUST_BACKTRACE=1` environment variable to display a \
292277
backtrace"
293-
);
294-
}
278+
);
295279
}
296280
}
297281
// If backtraces aren't supported, do nothing.
298282
None => {}
299283
}
300284
};
301285

302-
if let Some(path) = path
303-
&& let Ok(mut out) = crate::fs::File::options().create(true).append(true).open(&path)
304-
{
305-
write(&mut out, BacktraceStyle::full());
306-
}
307-
308286
if let Some(local) = set_output_capture(None) {
309-
write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()), backtrace);
287+
write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
310288
set_output_capture(Some(local));
311289
} else if let Some(mut out) = panic_output() {
312-
write(&mut out, backtrace);
290+
write(&mut out);
313291
}
314292
}
315293

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)