Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICE: need to be able to compute layouts: Unknown(T) running miri with -Zunpretty-mir #107999

Closed
matthiaskrgr opened this issue Feb 13, 2023 · 4 comments · Fixed by #108012
Closed
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Feb 13, 2023

Code

You can repro with RUSTFLAGS="-Zunpretty=mir" MIRIFLAGS="-Zmir-opt-level=3" cargo miri run

// This test checks that calling `mem::{uninitialized,zeroed}` with certain types results
// in a lint.

use std::mem::{self};
struct Wrap<T> { wrapped: T }

#[allow(unused)]
fn generic<T: 'static>() {
    unsafe {
        let _val: Wrap<&'static T> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
    }
}

fn main() {}

Meta

rustc --version --verbose:

rustc 1.69.0-nightly (20081880a 2023-02-13)
binary: rustc
commit-hash: 20081880ad2a98bbc8c8293f96c5b284d1584d86
commit-date: 2023-02-13
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

Error output

warning: use of deprecated function `std::mem::uninitialized`: use `mem::MaybeUninit` instead
  --> src/main.rs:10:43
   |
10 |         let _val: Wrap<&'static T> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
   |                                           ^^^^^^^^^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

warning: field `wrapped` is never read
 --> src/main.rs:5:18
  |
5 | struct Wrap<T> { wrapped: T }
  |        ----      ^^^^^^^
  |        |
  |        field in this struct
  |
  = note: `#[warn(dead_code)]` on by default

warning: the type `Wrap<&T>` does not permit being left uninitialized
  --> src/main.rs:10:38
   |
10 |         let _val: Wrap<&'static T> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
   |                                      ^^^^^^^^^^^^^^^^^^^^
   |                                      |
   |                                      this code causes undefined behavior when executed
   |                                      help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
   |
   = note: `Wrap<&T>` must be non-null
note: because references must be non-null (in this struct field)
  --> src/main.rs:5:18
   |
5  | struct Wrap<T> { wrapped: T }
   |                  ^^^^^^^^^^
   = note: `#[warn(invalid_value)]` on by default
Backtrace


thread 'rustc' panicked at 'need to be able to compute layouts: Unknown(T)', compiler/rustc_const_eval/src/util/might_permit_raw_init.rs:111:48
stack backtrace:
   0:     0x7febf276a9ea - std::backtrace_rs::backtrace::libunwind::trace::h599c8a609f9333f9
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7febf276a9ea - std::backtrace_rs::backtrace::trace_unsynchronized::hd98c4236d5e12dd3
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7febf276a9ea - std::sys_common::backtrace::_print_fmt::h5e56057600010abc
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7febf276a9ea - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h6c880a29175ff5a3
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7febf27caafe - core::fmt::write::hfc0242af538b8a46
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/core/src/fmt/mod.rs:1232:17
   5:     0x7febf275b005 - std::io::Write::write_fmt::h01aa30c6b6ac7c12
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/io/mod.rs:1684:15
   6:     0x7febf276a7b5 - std::sys_common::backtrace::_print::hffab19929157fc64
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/sys_common/backtrace.rs:47:5
   7:     0x7febf276a7b5 - std::sys_common::backtrace::print::hcc10add6529763f5
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/sys_common/backtrace.rs:34:9
   8:     0x7febf276d54f - std::panicking::default_hook::{{closure}}::h7b790581a0467591
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/panicking.rs:267:22
   9:     0x7febf276d28b - std::panicking::default_hook::hea529077c0649ab7
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/panicking.rs:286:9
  10:     0x7febf5adcca4 - <rustc_driver_impl[25fd4ad55bd4d290]::DEFAULT_HOOK::{closure#0}::{closure#0} as core[b6202784567cb4e4]::ops::function::FnOnce<(&core[b6202784567cb4e4]::panic::panic_info::PanicInfo,)>>::call_once::{shim:vtable#0}
  11:     0x7febf276dd8a - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h9025e888b11b840d
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/alloc/src/boxed.rs:2002:9
  12:     0x7febf276dd8a - std::panicking::rust_panic_with_hook::hbf88635db69aa1e9
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/panicking.rs:692:13
  13:     0x7febf276db09 - std::panicking::begin_panic_handler::{{closure}}::h0a5dea2fedd0d5a5
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/panicking.rs:579:13
  14:     0x7febf276ae8c - std::sys_common::backtrace::__rust_end_short_backtrace::h2507953ca43dc350
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/sys_common/backtrace.rs:137:18
  15:     0x7febf276d812 - rust_begin_unwind
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/panicking.rs:575:5
  16:     0x7febf27c74a3 - core::panicking::panic_fmt::hf2288fbc08000d27
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/core/src/panicking.rs:64:14
  17:     0x7febf27c7a33 - core::result::unwrap_failed::h949aaa38a1828029
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/core/src/result.rs:1790:5
  18:     0x7febf519ebb1 - rustc_const_eval[344787e6075ccab3]::util::might_permit_raw_init::might_permit_raw_init_lax
  19:     0x7febf519e2ee - rustc_const_eval[344787e6075ccab3]::util::might_permit_raw_init::might_permit_raw_init_lax
  20:     0x7febf519dfb9 - rustc_const_eval[344787e6075ccab3]::util::might_permit_raw_init::might_permit_raw_init
  21:     0x7febf51fa047 - <rustc_query_system[6e05d7961ccd4849]::dep_graph::graph::DepGraph<rustc_middle[26406bea1d07e151]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[26406bea1d07e151]::ty::context::TyCtxt, rustc_middle[26406bea1d07e151]::ty::ParamEnvAnd<rustc_target[88529097bdb44244]::abi::TyAndLayout<rustc_middle[26406bea1d07e151]::ty::Ty>>, bool>
  22:     0x7febf63882d9 - rustc_query_system[6e05d7961ccd4849]::query::plumbing::try_execute_query::<rustc_query_impl[38ddc62a235656bd]::queries::permits_uninit_init, rustc_query_impl[38ddc62a235656bd]::plumbing::QueryCtxt>
  23:     0x7febf63ad825 - rustc_query_system[6e05d7961ccd4849]::query::plumbing::get_query::<rustc_query_impl[38ddc62a235656bd]::queries::permits_uninit_init, rustc_query_impl[38ddc62a235656bd]::plumbing::QueryCtxt, rustc_middle[26406bea1d07e151]::dep_graph::dep_node::DepKind>
  24:     0x7febf642aa67 - <rustc_query_impl[38ddc62a235656bd]::Queries as rustc_middle[26406bea1d07e151]::ty::query::QueryEngine>::permits_uninit_init
  25:     0x7febf61b9f6e - rustc_mir_transform[d56b548caf7a8cfa]::instcombine::intrinsic_assert_panics::mem_uninitialized_valid_predicate
  26:     0x7febf4f7d8a6 - <rustc_mir_transform[d56b548caf7a8cfa]::instcombine::InstCombine as rustc_middle[26406bea1d07e151]::mir::MirPass>::run_pass
  27:     0x7febf41a1fe8 - rustc_mir_transform[d56b548caf7a8cfa]::optimized_mir
  28:     0x7febf4198ff6 - <rustc_query_system[6e05d7961ccd4849]::dep_graph::graph::DepGraph<rustc_middle[26406bea1d07e151]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[26406bea1d07e151]::ty::context::TyCtxt, rustc_span[f166845bd5138698]::def_id::DefId, &rustc_middle[26406bea1d07e151]::mir::Body>
  29:     0x7febf413771d - rustc_query_system[6e05d7961ccd4849]::query::plumbing::try_execute_query::<rustc_query_impl[38ddc62a235656bd]::queries::optimized_mir, rustc_query_impl[38ddc62a235656bd]::plumbing::QueryCtxt>
  30:     0x7febf4135484 - <rustc_middle[26406bea1d07e151]::ty::context::TyCtxt>::instance_mir
  31:     0x7febf5feda35 - rustc_middle[26406bea1d07e151]::mir::pretty::write_mir_pretty
  32:     0x7febf5ab4882 - rustc_driver_impl[25fd4ad55bd4d290]::pretty::print_after_hir_lowering
  33:     0x7febf5aa02cb - <rustc_middle[26406bea1d07e151]::ty::context::GlobalCtxt>::enter::<rustc_driver_impl[25fd4ad55bd4d290]::run_compiler::{closure#1}::{closure#2}::{closure#1}, core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>
  34:     0x7febf4d0aa30 - <rustc_interface[cf329bccbcde2f18]::interface::Compiler>::enter::<rustc_driver_impl[25fd4ad55bd4d290]::run_compiler::{closure#1}::{closure#2}, core[b6202784567cb4e4]::result::Result<core[b6202784567cb4e4]::option::Option<rustc_interface[cf329bccbcde2f18]::queries::Linker>, rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>
  35:     0x7febf4d058c4 - rustc_span[f166845bd5138698]::with_source_map::<core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>, rustc_interface[cf329bccbcde2f18]::interface::run_compiler<core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>, rustc_driver_impl[25fd4ad55bd4d290]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  36:     0x7febf4d053d4 - <scoped_tls[9115c50d9b00df7e]::ScopedKey<rustc_span[f166845bd5138698]::SessionGlobals>>::set::<rustc_interface[cf329bccbcde2f18]::interface::run_compiler<core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>, rustc_driver_impl[25fd4ad55bd4d290]::run_compiler::{closure#1}>::{closure#0}, core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>
  37:     0x7febf4d04ad2 - std[b5e4c008489c853a]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[cf329bccbcde2f18]::util::run_in_thread_pool_with_globals<rustc_interface[cf329bccbcde2f18]::interface::run_compiler<core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>, rustc_driver_impl[25fd4ad55bd4d290]::run_compiler::{closure#1}>::{closure#0}, core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>
  38:     0x7febf5371eac - <<std[b5e4c008489c853a]::thread::Builder>::spawn_unchecked_<rustc_interface[cf329bccbcde2f18]::util::run_in_thread_pool_with_globals<rustc_interface[cf329bccbcde2f18]::interface::run_compiler<core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>, rustc_driver_impl[25fd4ad55bd4d290]::run_compiler::{closure#1}>::{closure#0}, core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b6202784567cb4e4]::result::Result<(), rustc_errors[51cf6c1aaeeb3d69]::ErrorGuaranteed>>::{closure#1} as core[b6202784567cb4e4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:     0x7febf2777d23 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h11a55f6f3d84245d
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/alloc/src/boxed.rs:1988:9
  40:     0x7febf2777d23 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hd367d709d06ebfbd
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/alloc/src/boxed.rs:1988:9
  41:     0x7febf2777d23 - std::sys::unix::thread::Thread::new::thread_start::h340e26124c1b592b
                               at /rustc/20081880ad2a98bbc8c8293f96c5b284d1584d86/library/std/src/sys/unix/thread.rs:108:17
  42:     0x7febf24208fd - <unknown>
  43:     0x7febf24a2d20 - <unknown>
  44:                0x0 - <unknown>

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.69.0-nightly (20081880a 2023-02-13) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED] -Z unpretty=mir -Z mir-opt-level=3

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

query stack during panic:
#0 [permits_uninit_init] checking to see if `Wrap<&T>` permits being left uninit
#1 [optimized_mir] optimizing MIR for `generic`
end of query stack

@matthiaskrgr matthiaskrgr added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. labels Feb 13, 2023
@saethlin
Copy link
Member

I wrote the PR that added the code path this is tickling. Here's an invocation that doesn't require Miri:

RUSTFLAGS="-Zunpretty=mir -Zmir-enable-passes=+InstCombine -Zinline-mir" cargo +nightly run

I do not know why -Zunpretty=mir is required. I've never touched that code. Looking at the panic path this actually hits inside might_permit_raw_init_lax, I'm shocked this doesn't panic without that flag.

@saethlin
Copy link
Member

Ah! Without the flag the interesting code is just not compiled because it's not used. Here is a minimal reproducer:

#![crate_type = "lib"]

pub fn generic<T>() {
    unsafe {
        let _v: &T = std::mem::uninitialized();
    }
}
RUSTFLAGS="-Zmir-enable-passes=+InstCombine -Zinline-mir" cargo +nightly build

@saethlin
Copy link
Member

Oh! Of course those are default MIR opt settings in --release.

So this ICEs with just cargo +nightly build --release. It should eventually land in beta. The icy code is from #105582

@compiler-errors
Copy link
Member

@rustbot claim

@bors bors closed this as completed in c528357 Feb 16, 2023
bjorn3 pushed a commit to bjorn3/rust that referenced this issue Mar 15, 2023
Don't ICE in `might_permit_raw_init` if reference is polymorphic

Emitting optimized MIR for a polymorphic function may require computing layout of a type that isn't (yet) known. This happens in the instcombine pass, for example. Let's fail gracefully in that condition.

cc `@saethlin`
fixes rust-lang#107999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants