Skip to content

Commit c710067

Browse files
committed
Auto merge of #1977 - saethlin:short-backtraces, r=RalfJung
Prune backtraces similar to RUST_BACKTRACE=1 logic This removes the majority of output from `cargo miri run` and `cargo miri test` in common usage. ~~I've copied the logic almost directly from `std`: https://github.com/rust-lang/rust/blob/3b186511f62b0ce20e72ede0e8e13f8787155f02/library/std/src/sys_common/backtrace.rs#L76-L77~~ ~~It might be nice to have the "some details were omitted" note and a fallback to a setting where we print everything just in case this logic goes sideways, but~~ ~~1. I'm not sure where to put the note~~ ~~2. `MIRI_BACKTRACE`, `RUST_BACKTRACE`, and `RUSTC_CTFE_BACKTRACE` already do something else. Should we repurpose or add on to the semantics of `MIRI_BACKTRACE`?~~ --- Based on this tiny silly crate: ```rust fn main() { some_function(); } fn some_function() { unsafe { let _x: &u8 = core::mem::transmute(1usize); } } #[cfg(test)] mod tests { #[test] fn it_works() { unsafe { let _x: &'static u8 = core::mem::transmute(1usize); } } } ``` `cargo miri run`: Before: ``` Finished dev [unoptimized + debuginfo] target(s) in 0.10s Running `/home/ben/.cargo/bin/cargo-miri target/miri/x86_64-unknown-linux-gnu/debug/scratch` error: Undefined Behavior: type validation failed: encountered a dangling reference (address 0x1 is unallocated) --> src/main.rs:7:23 | 7 | let _x: &u8 = core::mem::transmute(1usize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (address 0x1 is unallocated) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside `some_function` at src/main.rs:7:23 note: inside `main` at src/main.rs:2:5 --> src/main.rs:2:5 | 2 | some_function(); | ^^^^^^^^^^^^^^^ = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5 = note: inside `std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:122:18 = note: inside closure at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:145:18 = note: inside `std::ops::function::impls::<impl std::ops::FnOnce<()> for &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>::call_once` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:259:13 = note: inside `std::panicking::r#try::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:492:40 = note: inside `std::panicking::r#try::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:456:19 = note: inside `std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panic.rs:137:14 = note: inside closure at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:128:48 = note: inside `std::panicking::r#try::do_call::<[closure@std::rt::lang_start_internal::{closure#2}], isize>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:492:40 = note: inside `std::panicking::r#try::<isize, [closure@std::rt::lang_start_internal::{closure#2}]>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:456:19 = note: inside `std::panic::catch_unwind::<[closure@std::rt::lang_start_internal::{closure#2}], isize>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panic.rs:137:14 = note: inside `std::rt::lang_start_internal` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:128:20 = note: inside `std::rt::lang_start::<()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:144:17 error: aborting due to previous error ``` After: ``` Finished dev [unoptimized + debuginfo] target(s) in 0.10s Running `/home/ben/.cargo/bin/cargo-miri target/miri/x86_64-unknown-linux-gnu/debug/scratch` error: Undefined Behavior: type validation failed: encountered a dangling reference (address 0x1 is unallocated) --> src/main.rs:7:23 | 7 | let _x: &u8 = core::mem::transmute(1usize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (address 0x1 is unallocated) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside `some_function` at src/main.rs:7:23 note: inside `main` at src/main.rs:2:5 --> src/main.rs:2:5 | 2 | some_function(); | ^^^^^^^^^^^^^^^ note: Some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace. error: aborting due to previous error ``` `cargo miri test` Before: ``` Finished test [unoptimized + debuginfo] target(s) in 0.00s Running unittests (target/miri/x86_64-unknown-linux-gnu/debug/deps/scratch-9d7717efc37bb64c) running 1 test test tests::it_works ... error: Undefined Behavior: type validation failed: encountered a dangling reference (address 0x1 is unallocated) --> src/main.rs:16:35 | 16 | let _x: &'static u8 = core::mem::transmute(1usize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (address 0x1 is unallocated) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside `tests::it_works` at src/main.rs:16:35 note: inside closure at src/main.rs:14:5 --> src/main.rs:14:5 | 13 | #[test] | ------- in this procedural macro expansion 14 | / fn it_works() { 15 | | unsafe { 16 | | let _x: &'static u8 = core::mem::transmute(1usize); 17 | | } 18 | | } | |_____^ = note: inside `<[closure@src/main.rs:14:5: 18:6] as std::ops::FnOnce<()>>::call_once - shim` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5 = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5 = note: inside `tests::test::__rust_begin_short_backtrace::<fn()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:575:5 = note: inside closure at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:566:30 = note: inside `<[closure@tests::test::run_test::{closure#1}] as std::ops::FnOnce<()>>::call_once - shim(vtable)` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5 = note: inside `<std::boxed::Box<dyn std::ops::FnOnce() + std::marker::Send> as std::ops::FnOnce<()>>::call_once` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1854:9 = note: inside `<std::panic::AssertUnwindSafe<std::boxed::Box<dyn std::ops::FnOnce() + std::marker::Send>> as std::ops::FnOnce<()>>::call_once` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:271:9 = note: inside `std::panicking::r#try::do_call::<std::panic::AssertUnwindSafe<std::boxed::Box<dyn std::ops::FnOnce() + std::marker::Send>>, ()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:492:40 = note: inside `std::panicking::r#try::<(), std::panic::AssertUnwindSafe<std::boxed::Box<dyn std::ops::FnOnce() + std::marker::Send>>>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:456:19 = note: inside `std::panic::catch_unwind::<std::panic::AssertUnwindSafe<std::boxed::Box<dyn std::ops::FnOnce() + std::marker::Send>>, ()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panic.rs:137:14 = note: inside `tests::test::run_test_in_process` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:598:18 = note: inside closure at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:492:39 = note: inside `tests::test::run_test::run_test_inner` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:530:13 = note: inside `tests::test::run_test` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:562:28 = note: inside `tests::test::run_tests::<[closure@tests::test::run_tests_console::{closure#2}]>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:305:17 = note: inside `tests::test::run_tests_console` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/console.rs:290:5 = note: inside `tests::test::test_main` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:116:15 = note: inside `tests::test::test_main_static` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/test/src/lib.rs:135:5 = note: inside `main` = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5 = note: inside `std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:122:18 = note: inside closure at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:145:18 = note: inside `std::ops::function::impls::<impl std::ops::FnOnce<()> for &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>::call_once` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/ops/function.rs:259:13 = note: inside `std::panicking::r#try::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:492:40 = note: inside `std::panicking::r#try::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:456:19 = note: inside `std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panic.rs:137:14 = note: inside closure at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:128:48 = note: inside `std::panicking::r#try::do_call::<[closure@std::rt::lang_start_internal::{closure#2}], isize>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:492:40 = note: inside `std::panicking::r#try::<isize, [closure@std::rt::lang_start_internal::{closure#2}]>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panicking.rs:456:19 = note: inside `std::panic::catch_unwind::<[closure@std::rt::lang_start_internal::{closure#2}], isize>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/panic.rs:137:14 = note: inside `std::rt::lang_start_internal` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:128:20 = note: inside `std::rt::lang_start::<()>` at /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:144:17 = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to previous error error: test failed, to rerun pass '--bin scratch' ``` After: ``` Finished test [unoptimized + debuginfo] target(s) in 0.00s Running unittests (target/miri/x86_64-unknown-linux-gnu/debug/deps/scratch-9d7717efc37bb64c) running 1 test test tests::it_works ... error: Undefined Behavior: type validation failed: encountered a dangling reference (address 0x1 is unallocated) --> src/main.rs:16:35 | 16 | let _x: &'static u8 = core::mem::transmute(1usize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (address 0x1 is unallocated) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside `tests::it_works` at src/main.rs:16:35 note: inside closure at src/main.rs:14:5 --> src/main.rs:14:5 | 13 | #[test] | ------- in this procedural macro expansion 14 | / fn it_works() { 15 | | unsafe { 16 | | let _x: &'static u8 = core::mem::transmute(1usize); 17 | | } 18 | | } | |_____^ = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info) note: Some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace. error: aborting due to previous error error: test failed, to rerun pass '--bin scratch' ```
2 parents d03b4a0 + 19ecd13 commit c710067

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

src/bin/miri.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use rustc_middle::{
2929
};
3030
use rustc_session::{config::ErrorOutputType, search_paths::PathKind, CtfeBacktrace};
3131

32+
use miri::BacktraceStyle;
33+
3234
struct MiriCompilerCalls {
3335
miri_config: miri::MiriConfig,
3436
}
@@ -462,6 +464,14 @@ fn main() {
462464
let measureme_out = arg.strip_prefix("-Zmiri-measureme=").unwrap();
463465
miri_config.measureme_out = Some(measureme_out.to_string());
464466
}
467+
arg if arg.starts_with("-Zmiri-backtrace=") => {
468+
miri_config.backtrace_style = match arg.strip_prefix("-Zmiri-backtrace=") {
469+
Some("0") => BacktraceStyle::Off,
470+
Some("1") => BacktraceStyle::Short,
471+
Some("full") => BacktraceStyle::Full,
472+
_ => panic!("-Zmiri-backtrace may only be 0, 1, or full"),
473+
};
474+
}
465475
_ => {
466476
// Forward to rustc.
467477
rustc_args.push(arg);

src/diagnostics.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,46 @@ pub fn report_error<'tcx, 'mir>(
157157
}
158158
};
159159

160+
let mut stacktrace = ecx.generate_stacktrace();
161+
let has_local_frame = stacktrace.iter().any(|frame| frame.instance.def_id().is_local());
162+
match ecx.machine.backtrace_style {
163+
BacktraceStyle::Off => {
164+
// Retain one frame so that we can print a span for the error itself
165+
stacktrace.truncate(1);
166+
}
167+
BacktraceStyle::Short => {
168+
// Only prune frames if there is at least one local frame. This check ensures that if
169+
// we get a backtrace that never makes it to the user code because it has detected a
170+
// bug in the Rust runtime, we don't prune away every frame.
171+
if has_local_frame {
172+
// This is part of the logic that `std` uses to select the relevant part of a
173+
// backtrace. But here, we only look for __rust_begin_short_backtrace, not
174+
// __rust_end_short_backtrace because the end symbol comes from a call to the default
175+
// panic handler.
176+
stacktrace = stacktrace
177+
.into_iter()
178+
.take_while(|frame| {
179+
let def_id = frame.instance.def_id();
180+
let path = ecx.tcx.tcx.def_path_str(def_id);
181+
!path.contains("__rust_begin_short_backtrace")
182+
})
183+
.collect::<Vec<_>>();
184+
185+
// After we prune frames from the bottom, there are a few left that are part of the
186+
// Rust runtime. So we remove frames until we get to a local symbol, which should be
187+
// main or a test.
188+
// This len check ensures that we don't somehow remove every frame, as doing so breaks
189+
// the primary error message.
190+
while stacktrace.len() > 1
191+
&& stacktrace.last().map_or(false, |e| !e.instance.def_id().is_local())
192+
{
193+
stacktrace.pop();
194+
}
195+
}
196+
}
197+
BacktraceStyle::Full => {}
198+
}
199+
160200
e.print_backtrace();
161201
let msg = e.to_string();
162202
report_msg(
@@ -165,9 +205,17 @@ pub fn report_error<'tcx, 'mir>(
165205
&if let Some(title) = title { format!("{}: {}", title, msg) } else { msg.clone() },
166206
msg,
167207
helps,
168-
&ecx.generate_stacktrace(),
208+
&stacktrace,
169209
);
170210

211+
// Include a note like `std` does for short backtraces, but since we are opt-out not opt-in, we
212+
// do not include a note when backtraces are off.
213+
if ecx.machine.backtrace_style == BacktraceStyle::Short && has_local_frame {
214+
ecx.tcx.sess.diagnostic().note_without_error(
215+
"some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace",
216+
);
217+
}
218+
171219
// Debug-dump all locals.
172220
for (i, frame) in ecx.active_thread_stack().iter().enumerate() {
173221
trace!("-------------------");

src/eval.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ pub enum IsolatedOp {
5757
Allow,
5858
}
5959

60+
#[derive(Copy, Clone, PartialEq, Eq)]
61+
pub enum BacktraceStyle {
62+
/// Prints a terser backtrace which ideally only contains relevant information.
63+
Short,
64+
/// Prints a backtrace with all possible information.
65+
Full,
66+
/// Prints only the frame that the error occurs in.
67+
Off,
68+
}
69+
6070
/// Configuration needed to spawn a Miri instance.
6171
#[derive(Clone)]
6272
pub struct MiriConfig {
@@ -98,6 +108,7 @@ pub struct MiriConfig {
98108
pub measureme_out: Option<String>,
99109
/// Panic when unsupported functionality is encountered
100110
pub panic_on_unsupported: bool,
111+
pub backtrace_style: BacktraceStyle,
101112
}
102113

103114
impl Default for MiriConfig {
@@ -121,6 +132,7 @@ impl Default for MiriConfig {
121132
cmpxchg_weak_failure_rate: 0.8,
122133
measureme_out: None,
123134
panic_on_unsupported: false,
135+
backtrace_style: BacktraceStyle::Short,
124136
}
125137
}
126138
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub use crate::diagnostics::{
6060
NonHaltingDiagnostic, TerminationInfo,
6161
};
6262
pub use crate::eval::{
63-
create_ecx, eval_entry, AlignmentCheck, IsolatedOp, MiriConfig, RejectOpWith,
63+
create_ecx, eval_entry, AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, RejectOpWith,
6464
};
6565
pub use crate::helpers::EvalContextExt as HelpersEvalContextExt;
6666
pub use crate::machine::{

src/machine.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ pub struct Evaluator<'mir, 'tcx> {
343343
/// functionality is encountered. If `false`, an error is propagated in the Miri application context
344344
/// instead (default behavior)
345345
pub(crate) panic_on_unsupported: bool,
346+
347+
/// Equivalent setting as RUST_BACKTRACE on encountering an error.
348+
pub(crate) backtrace_style: BacktraceStyle,
346349
}
347350

348351
impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
@@ -374,6 +377,7 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
374377
string_cache: Default::default(),
375378
exported_symbols_cache: FxHashMap::default(),
376379
panic_on_unsupported: config.panic_on_unsupported,
380+
backtrace_style: config.backtrace_style,
377381
}
378382
}
379383

0 commit comments

Comments
 (0)