rust-lang / miri

An interpreter for Rust's mid-level intermediate representation
Apache License 2.0
4.18k stars 323 forks source link

ICE when using `miri_print_borrow_state` with `-Zmiri-disable-stacked-borrows` #3459

Open jendrikw opened 3 months ago

jendrikw commented 3 months ago

I understand that combination is a little silly, but it could have a better error message.

Reproduction: run MIRIFLAGS='-Zmiri-disable-stacked-borrows' cargo miri run with this content in main.rs:

fn main() {
    let x: i32 = 1;
    #[cfg(miri)]
    {
        extern "Rust" {
            pub fn miri_get_alloc_id(ptr: *const ()) -> u64;
            pub fn miri_print_borrow_state(alloc_id: u64, show_unnamed: bool);
        }
        unsafe { miri_print_borrow_state(miri_get_alloc_id(&x as *const i32 as *const ()), true); }
    }
}

Complete Output:

Preparing a sysroot for Miri (target: x86_64-unknown-linux-gnu)... done
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `/home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner target/miri/x86_64-unknown-linux-gnu/debug/codegolf`
thread 'rustc' panicked at src/tools/miri/src/borrow_tracker/mod.rs:348:59:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:     0x6ce043710885 - std::backtrace_rs::backtrace::libunwind::trace::hc79cced6f418596d
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x6ce043710885 - std::backtrace_rs::backtrace::trace_unsynchronized::h06f3eef6c8a22cf0
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x6ce043710885 - std::sys_common::backtrace::_print_fmt::hba273d0c77fc3421
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x6ce043710885 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h409f1e3c1e32650e
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x6ce04375fb4b - core::fmt::rt::Argument::fmt::h8811fe3c91cda7b3
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/core/src/fmt/rt.rs:142:9
   5:     0x6ce04375fb4b - core::fmt::write::h7a8f70a9b146d9ee
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/core/src/fmt/mod.rs:1153:17
   6:     0x6ce0437053ff - std::io::Write::write_fmt::h32fb818656611c58
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/io/mod.rs:1843:15
   7:     0x6ce04371065e - std::sys_common::backtrace::_print::h0dc0bbf9b429a58b
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x6ce04371065e - std::sys_common::backtrace::print::hf60182bd4aee207d
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x6ce043713159 - std::panicking::default_hook::{{closure}}::hd90db44a41f772dc
  10:     0x6ce043712e75 - std::panicking::default_hook::hd86be16b87521210
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/panicking.rs:291:9
  11:     0x6ce04007f71e - std[2c420855e262d42e]::panicking::update_hook::<alloc[56e68475af91072d]::boxed::Box<rustc_driver_impl[10b0a2e726609308]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x6ce04371385c - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h2fe2a6e53d9884ad
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/alloc/src/boxed.rs:2032:9
  13:     0x6ce04371385c - std::panicking::rust_panic_with_hook::ha4f8caa112a16574
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/panicking.rs:792:13
  14:     0x6ce0437135cd - std::panicking::begin_panic_handler::{{closure}}::hc879855deab44ed0
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/panicking.rs:649:13
  15:     0x6ce043710d49 - std::sys_common::backtrace::__rust_end_short_backtrace::h85e59f289fdfff6c
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x6ce043713337 - rust_begin_unwind
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/panicking.rs:645:5
  17:     0x6ce04375bfe6 - core::panicking::panic_fmt::h0baef2c59e253f8d
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/core/src/panicking.rs:72:14
  18:     0x6ce04375c08f - core::panicking::panic::h03522a15492dd08d
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/core/src/panicking.rs:141:5
  19:     0x6ce04375bd69 - core::option::unwrap_failed::h23509f5d574bea60
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/core/src/option.rs:1984:5
  20:      0x33091f832fb - <rustc_const_eval[631414a2f6ddeed3]::interpret::eval_context::InterpCx<miri[17c0d691d1fdb92b]::machine::MiriMachine> as miri[17c0d691d1fdb92b]::borrow_tracker::EvalContextExt>::print_borrow_state
  21:      0x33091fc37d7 - <rustc_const_eval[631414a2f6ddeed3]::interpret::eval_context::InterpCx<miri[17c0d691d1fdb92b]::machine::MiriMachine> as miri[17c0d691d1fdb92b]::shims::foreign_items::EvalContextExtPriv>::emulate_foreign_item_inner
  22:      0x33091fc01e1 - <rustc_const_eval[631414a2f6ddeed3]::interpret::eval_context::InterpCx<miri[17c0d691d1fdb92b]::machine::MiriMachine> as miri[17c0d691d1fdb92b]::shims::foreign_items::EvalContextExt>::emulate_foreign_item
  23:      0x33091f340eb - <rustc_const_eval[631414a2f6ddeed3]::interpret::eval_context::InterpCx<miri[17c0d691d1fdb92b]::machine::MiriMachine>>::eval_fn_call
  24:      0x33091fa2350 - miri[17c0d691d1fdb92b]::eval::eval_entry::{closure#0}
  25:      0x33091f9d89b - miri[17c0d691d1fdb92b]::eval::eval_entry
  26:      0x33091e80583 - <miri[378d50b251030de8]::MiriCompilerCalls as rustc_driver_impl[10b0a2e726609308]::Callbacks>::after_analysis
  27:     0x6ce041fc8f67 - rustc_interface[dbc083dc91b33e67]::interface::run_compiler::<core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>, rustc_driver_impl[10b0a2e726609308]::run_compiler::{closure#0}>::{closure#0}
  28:     0x6ce04219527f - std[2c420855e262d42e]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[dbc083dc91b33e67]::util::run_in_thread_with_globals<rustc_interface[dbc083dc91b33e67]::util::run_in_thread_pool_with_globals<rustc_interface[dbc083dc91b33e67]::interface::run_compiler<core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>, rustc_driver_impl[10b0a2e726609308]::run_compiler::{closure#0}>::{closure#0}, core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>>::{closure#0}, core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>>
  29:     0x6ce042195070 - <<std[2c420855e262d42e]::thread::Builder>::spawn_unchecked_<rustc_interface[dbc083dc91b33e67]::util::run_in_thread_with_globals<rustc_interface[dbc083dc91b33e67]::util::run_in_thread_pool_with_globals<rustc_interface[dbc083dc91b33e67]::interface::run_compiler<core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>, rustc_driver_impl[10b0a2e726609308]::run_compiler::{closure#0}>::{closure#0}, core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>>::{closure#0}, core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[a43033d72a2ad972]::result::Result<(), rustc_span[7eb36910801e3e4c]::ErrorGuaranteed>>::{closure#1} as core[a43033d72a2ad972]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  30:     0x6ce04371d28b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h5cf039e566d31df2
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/alloc/src/boxed.rs:2018:9
  31:     0x6ce04371d28b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h5b8a7e7667fbf80b
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/alloc/src/boxed.rs:2018:9
  32:     0x6ce04371d28b - std::sys::pal::unix::thread::Thread::new::thread_start::h47ad6cb551091e6a
                               at /rustc/9d5cdf75aa42faaf0b58ba21a510117e8d0051a3/library/std/src/sys/pal/unix/thread.rs:108:17
  33:     0x6ce03cea955a - <unknown>
  34:     0x6ce03cf26a3c - <unknown>
  35:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: it seems that this compiler `1.79.0-nightly (9d5cdf75a 2024-04-07)` is outdated, a newer nightly should have been released in the mean time
  |
  = note: please consider running `rustup update nightly` to update the nightly channel and check if this problem still persists
  = note: if the problem still persists, we would appreciate a bug report: https://github.com/rust-lang/miri/issues/new

note: please attach the file at `/home/jendrik/dev/rust/codegolf/rustc-ice-2024-04-08T20_28_57-84898.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED] -Z miri-disable-stacked-borrows

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack

Miri caused an ICE during evaluation. Here's the interpreter backtrace at the time of the panic:
note: the place in the program where the ICE was triggered
 --> src/main.rs:9:18
  |
9 |         unsafe { miri_print_borrow_state(miri_get_alloc_id(&x as *const i32 as *const ()), true); }
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: BACKTRACE:
  = note: inside `main` at src/main.rs:9:18: 9:97
  = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
  = note: inside `std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:155:18: 155:21
  = note: inside closure at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:159:18: 159:82
  = 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/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:284:13: 284:31
  = note: inside `std::panicking::r#try::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:552:40: 552:43
  = note: inside `std::panicking::r#try::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:516:19: 516:88
  = note: inside `std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:149:14: 149:33
  = note: inside closure at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:141:48: 141:73
  = note: inside `std::panicking::r#try::do_call::<{closure@std::rt::lang_start_internal::{closure#2}}, isize>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:552:40: 552:43
  = note: inside `std::panicking::r#try::<isize, {closure@std::rt::lang_start_internal::{closure#2}}>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:516:19: 516:88
  = note: inside `std::panic::catch_unwind::<{closure@std::rt::lang_start_internal::{closure#2}}, isize>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:149:14: 149:33
  = note: inside `std::rt::lang_start_internal` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:141:20: 141:98
  = note: inside `std::rt::lang_start::<()>` at /home/jendrik/.appdata/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:158:17: 163:6

(Side note: Contrary to the suggestion, there is no newer nightly available at the time of writing 2024-04-08T22:31+02:00. Where can I report this bug?)

saethlin commented 3 months ago

Contrary to the suggestion, there is no newer nightly available at the time of writing 2024-04-08T22:31+02:00. Where can I report this bug?

That message is from the compiler's ICE hook, report it at rust-lang/rust

RalfJung commented 3 months ago

Yeah this should probably just emit a warning or print that no borrow state is being tracked. This should be easy to do, the code to change is here:

https://github.com/rust-lang/miri/blob/324b92841dd1c1abe056cbbb53c41f9f3d9049bb/src/borrow_tracker/mod.rs#L348

Also we should probably print an error when the Alloc ID is 0, rather than just not printing anything.