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

clippy ICE: normalization works on validated constants: InterpErrorInfo #101113

Closed
matthiaskrgr opened this issue Aug 28, 2022 · 6 comments
Closed
Labels
A-clippy Area: Clippy C-bug Category: This is a bug. 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.

Comments

@matthiaskrgr
Copy link
Member

Code

// Check that constants with interior mutability inside unions are rejected
// during validation.
//
// Note that this test case relies on undefined behaviour to construct a
// constant with interior mutability that is "invisible" to the static checks.
// If for some reason this approach no longer works, it is should be fine to
// remove the test case.
//
// build-fail
// stderr-per-bitwidth
#![feature(const_mut_refs)]

use std::cell::Cell;
use std::mem::ManuallyDrop;

#[repr(C)]
struct S {
    x: u32,
    y: E,
}

#[repr(u32)]
enum E {
    A,
    B(U)
}

union U {
    cell: ManuallyDrop<Cell<u32>>,
}

const C: S = {
    let s = S { x: 0, y: E::A };
    // Go through an &u32 reference which is definitely not allowed to mutate anything.
    let p = &s.x as *const u32 as *mut u32;
    // Change enum tag to E::B.
    unsafe { *p.add(1) = 1 };
    s
};

fn main() { //~ ERROR it is undefined behavior to use this value
    let _: &'static _ = &C; //~ ERROR erroneous constant used
    //~^ WARN this was previously accepted
}

This happens with clippy clippy 0.1.65 (eaadb89 2022-08-27) (master toolchain) but not with clippy nightly clippy 0.1.65 (c07a8b4 2022-08-26), since clippy was not updated in a week I strongly suspect that this is caused by some underlying rustc change :/

warning: variant `B` is never constructed
  --> invalid-union.rs:25:5
   |
23 | enum E {
   |      - variant in this enum
24 |     A,
25 |     B(U)
   |     ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: field `cell` is never read
  --> invalid-union.rs:29:5
   |
28 | union U {
   |       - field in this union
29 |     cell: ManuallyDrop<Cell<u32>>,
   |     ^^^^

thread 'rustc' panicked at 'normalization works on validated constants: InterpErrorInfo(InterpErrorInfoInner { kind: using uninitialized data, but this operation requires initialized memory, backtrace: None })', compiler/rustc_const_eval/src/const_eval/eval_queries.rs:136:36
stack backtrace:
   0:     0x7ff41b9fc520 - std::backtrace_rs::backtrace::libunwind::trace::h1f37105be43a6ced
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   1:     0x7ff41b9fc520 - std::backtrace_rs::backtrace::trace_unsynchronized::hb4b9c2d184196576
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ff41b9fc520 - std::sys_common::backtrace::_print_fmt::h3f0d553949314bd7
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7ff41b9fc520 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h39b07f730bb6a9d4
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7ff41ba5724e - core::fmt::write::h56db6eaf035ee5bc
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/core/src/fmt/mod.rs:1202:17
   5:     0x7ff41b9ed2b5 - std::io::Write::write_fmt::hdb2b15796d01739f
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/io/mod.rs:1672:15
   6:     0x7ff41b9ff1e3 - std::sys_common::backtrace::_print::h107b12e8ee4b3787
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7ff41b9ff1e3 - std::sys_common::backtrace::print::ha1362b333134c0ca
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7ff41b9ff1e3 - std::panicking::default_hook::{{closure}}::h45225b21ea090e46
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/panicking.rs:295:22
   9:     0x7ff41b9feecf - std::panicking::default_hook::hb1ecc4e16a8eec76
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/panicking.rs:314:9
  10:     0x561cd1b4fdef - clippy_driver[756e4ebdfa4774ff]::ICE_HOOK::{closure#0}::{closure#0}
  11:     0x7ff41b9ffa1d - std::panicking::rust_panic_with_hook::hce3d07342ecb6e24
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/panicking.rs:702:17
  12:     0x7ff41b9ff877 - std::panicking::begin_panic_handler::{{closure}}::h1abbb6a787420726
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/panicking.rs:588:13
  13:     0x7ff41b9fca3c - std::sys_common::backtrace::__rust_end_short_backtrace::hbbd088378c5d6279
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/sys_common/backtrace.rs:138:18
  14:     0x7ff41b9ff592 - rust_begin_unwind
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/panicking.rs:584:5
  15:     0x7ff41ba53d83 - core::panicking::panic_fmt::hbbf169a0329ab688
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/core/src/panicking.rs:142:14
  16:     0x7ff41ba54103 - core::result::unwrap_failed::ha3373e8a9854af58
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/core/src/result.rs:1785:5
  17:     0x7ff41dc2dc7d - rustc_const_eval[d1fb3b10c343a724]::const_eval::eval_queries::op_to_const
  18:     0x7ff41ddac2c0 - <alloc[544ca457b09a5913]::vec::Vec<rustc_middle[eeb0489fdb81cdb3]::mir::ConstantKind> as alloc[544ca457b09a5913]::vec::spec_from_iter::SpecFromIter<rustc_middle[eeb0489fdb81cdb3]::mir::ConstantKind, core[a4f388abfe64fe23]::iter::adapters::GenericShunt<core[a4f388abfe64fe23]::iter::adapters::map::Map<core[a4f388abfe64fe23]::ops::range::Range<usize>, rustc_const_eval[d1fb3b10c343a724]::const_eval::try_destructure_mir_constant::{closure#0}>, core[a4f388abfe64fe23]::result::Result<core[a4f388abfe64fe23]::convert::Infallible, rustc_middle[eeb0489fdb81cdb3]::mir::interpret::error::InterpErrorInfo>>>>::from_iter
  19:     0x7ff41df089a8 - rustc_const_eval[d1fb3b10c343a724]::const_eval::try_destructure_mir_constant
  20:     0x7ff41df08717 - <rustc_const_eval[d1fb3b10c343a724]::provide::{closure#1} as core[a4f388abfe64fe23]::ops::function::FnOnce<(rustc_middle[eeb0489fdb81cdb3]::ty::context::TyCtxt, rustc_middle[eeb0489fdb81cdb3]::ty::ParamEnvAnd<rustc_middle[eeb0489fdb81cdb3]::mir::ConstantKind>)>>::call_once
  21:     0x7ff41df098c8 - rustc_query_system[91bce7e2254c0a97]::query::plumbing::get_query::<rustc_query_impl[107a3a16776f9175]::queries::try_destructure_mir_constant, rustc_query_impl[107a3a16776f9175]::plumbing::QueryCtxt>
  22:     0x7ff41df0942d - <rustc_query_impl[107a3a16776f9175]::Queries as rustc_middle[eeb0489fdb81cdb3]::ty::query::QueryEngine>::try_destructure_mir_constant
  23:     0x7ff41cdf6176 - <rustc_middle[eeb0489fdb81cdb3]::ty::context::TyCtxt>::destructure_mir_constant
  24:     0x561cd1deb75a - clippy_lints[33711be3d19c60f0]::non_copy_const::is_value_unfrozen_raw::inner
  25:     0x561cd1deb7a0 - clippy_lints[33711be3d19c60f0]::non_copy_const::is_value_unfrozen_raw::inner
  26:     0x561cd1deb7a0 - clippy_lints[33711be3d19c60f0]::non_copy_const::is_value_unfrozen_raw::inner
  27:     0x561cd1debbd4 - <clippy_lints[33711be3d19c60f0]::non_copy_const::NonCopyConst as rustc_lint[9a62ef8746d748d7]::passes::LateLintPass>::check_item
  28:     0x7ff41efa81a1 - <rustc_lint[9a62ef8746d748d7]::late::LateLintPassObjects as rustc_lint[9a62ef8746d748d7]::passes::LateLintPass>::check_item
  29:     0x7ff41e2d0303 - <rustc_lint[9a62ef8746d748d7]::late::LateContextAndPass<rustc_lint[9a62ef8746d748d7]::late::LateLintPassObjects> as rustc_hir[d28af53393254686]::intravisit::Visitor>::visit_nested_item
  30:     0x7ff41e2e7657 - rustc_hir[d28af53393254686]::intravisit::walk_mod::<rustc_lint[9a62ef8746d748d7]::late::LateContextAndPass<rustc_lint[9a62ef8746d748d7]::late::LateLintPassObjects>>
  31:     0x7ff41e2cf173 - rustc_lint[9a62ef8746d748d7]::late::late_lint_pass_crate::<rustc_lint[9a62ef8746d748d7]::late::LateLintPassObjects>
  32:     0x7ff41de8dba4 - rustc_lint[9a62ef8746d748d7]::late::late_lint_crate::<rustc_lint[9a62ef8746d748d7]::BuiltinCombinedLateLintPass>
  33:     0x7ff41de8d126 - <rustc_session[b8c4a953f96b5b9c]::session::Session>::time::<(), rustc_lint[9a62ef8746d748d7]::late::check_crate<rustc_lint[9a62ef8746d748d7]::BuiltinCombinedLateLintPass, rustc_interface[3bb52438fbf2157c]::passes::analysis::{closure#5}::{closure#1}::{closure#2}::{closure#0}::{closure#0}>::{closure#0}::{closure#0}>
  34:     0x7ff41de8d029 - <rustc_session[b8c4a953f96b5b9c]::session::Session>::time::<(), rustc_interface[3bb52438fbf2157c]::passes::analysis::{closure#5}::{closure#1}::{closure#2}::{closure#0}>
  35:     0x7ff41de77cef - <core[a4f388abfe64fe23]::panic::unwind_safe::AssertUnwindSafe<rustc_interface[3bb52438fbf2157c]::passes::analysis::{closure#5}::{closure#1}> as core[a4f388abfe64fe23]::ops::function::FnOnce<()>>::call_once
  36:     0x7ff41dc12ba6 - rustc_interface[3bb52438fbf2157c]::passes::analysis
  37:     0x7ff41dfcc7af - rustc_query_system[91bce7e2254c0a97]::query::plumbing::try_execute_query::<rustc_query_impl[107a3a16776f9175]::plumbing::QueryCtxt, rustc_query_system[91bce7e2254c0a97]::query::caches::DefaultCache<(), core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>>
  38:     0x7ff41dfcc4c0 - rustc_query_system[91bce7e2254c0a97]::query::plumbing::get_query::<rustc_query_impl[107a3a16776f9175]::queries::analysis, rustc_query_impl[107a3a16776f9175]::plumbing::QueryCtxt>
  39:     0x7ff41db1457d - <rustc_interface[3bb52438fbf2157c]::passes::QueryContext>::enter::<rustc_driver[3b95325100af7080]::run_compiler::{closure#1}::{closure#2}::{closure#3}, core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>
  40:     0x7ff41daff41e - <rustc_interface[3bb52438fbf2157c]::interface::Compiler>::enter::<rustc_driver[3b95325100af7080]::run_compiler::{closure#1}::{closure#2}, core[a4f388abfe64fe23]::result::Result<core[a4f388abfe64fe23]::option::Option<rustc_interface[3bb52438fbf2157c]::queries::Linker>, rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>
  41:     0x7ff41dafa7dc - rustc_span[1be22e6d7ecbf06b]::with_source_map::<core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>, rustc_interface[3bb52438fbf2157c]::interface::create_compiler_and_run<core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>, rustc_driver[3b95325100af7080]::run_compiler::{closure#1}>::{closure#1}>
  42:     0x7ff41dafa262 - rustc_interface[3bb52438fbf2157c]::interface::create_compiler_and_run::<core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>, rustc_driver[3b95325100af7080]::run_compiler::{closure#1}>
  43:     0x7ff41daf8e01 - <scoped_tls[9975ea76ad39cd3f]::ScopedKey<rustc_span[1be22e6d7ecbf06b]::SessionGlobals>>::set::<rustc_interface[3bb52438fbf2157c]::interface::run_compiler<core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>, rustc_driver[3b95325100af7080]::run_compiler::{closure#1}>::{closure#0}, core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>
  44:     0x7ff41daf8aef - std[39df21e20e19c219]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[3bb52438fbf2157c]::util::run_in_thread_pool_with_globals<rustc_interface[3bb52438fbf2157c]::interface::run_compiler<core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>, rustc_driver[3b95325100af7080]::run_compiler::{closure#1}>::{closure#0}, core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>::{closure#0}, core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>
  45:     0x7ff41e06b899 - <<std[39df21e20e19c219]::thread::Builder>::spawn_unchecked_<rustc_interface[3bb52438fbf2157c]::util::run_in_thread_pool_with_globals<rustc_interface[3bb52438fbf2157c]::interface::run_compiler<core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>, rustc_driver[3b95325100af7080]::run_compiler::{closure#1}>::{closure#0}, core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>::{closure#0}, core[a4f388abfe64fe23]::result::Result<(), rustc_errors[1320dabcf4b25fef]::ErrorGuaranteed>>::{closure#1} as core[a4f388abfe64fe23]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  46:     0x7ff41ba09643 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h267264e81121a930
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/alloc/src/boxed.rs:1940:9
  47:     0x7ff41ba09643 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h36d685e1b4755ccb
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/alloc/src/boxed.rs:1940:9
  48:     0x7ff41ba09643 - std::sys::unix::thread::Thread::new::thread_start::h22e88d4eed15a26d
                               at /rustc/eaadb8947b850a025404082f6297766c4680a42a/library/std/src/sys/unix/thread.rs:108:17
  49:     0x7ff41b66978d - <unknown>
  50:     0x7ff41b6ea8e4 - clone
  51:                0x0 - <unknown>

error: internal compiler error: unexpected panic

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new

note: Clippy version: clippy 0.1.65 (eaadb89 2022-08-27)

query stack during panic:
#0 [try_destructure_mir_constant] destructuring mir constant
#1 [analysis] running analysis passes on this crate
end of query stack
warning: 2 warnings emitted
@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. A-clippy Area: Clippy labels Aug 28, 2022
@matthiaskrgr
Copy link
Member Author

Unlike #100771 I was not able to reproduce this one with rustc and -Zextra-const-ub-checks

@Jarcho
Copy link
Contributor

Jarcho commented Aug 28, 2022

I'm surprised that even compiles. My guess is destructure_mir_constant is failing when called on the union value since it was never initialized.

@hudson-ayers
Copy link
Contributor

A much simpler MCVE that triggers this same ICE:

const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> =
    core::mem::MaybeUninit::uninit();

fn main() {}

running today's clippy (Clippy version: clippy 0.1.65 (59e7a30 2022-09-11)) on this source causes the ICE in this issue.

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c5643cbd7ce11d350c914088429b6e2e

@hudson-ayers
Copy link
Contributor

Before anyone asks "why would you create a constant that contains a Cell" the answer is that this is inside a macro where we wanted to create a static array of non-copy types of variable length. Toy example:

const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> =
    core::mem::MaybeUninit::uninit();
static mut BUF: [MaybeUninit<Cell<&'static ()>>; $LEN] = [UNINIT; $LEN];

@Jarcho
Copy link
Contributor

Jarcho commented Sep 25, 2022

The example assigning uninit looks like a rustc bug. The error is from attempting to read the discriminant value for the union which is somehow uninitialized despite being set by uninit(). This is a little different from the first example which has memory which should be uninitialized being read.

Ignore the previous. Looks like the active union field isn't kept track of in the value.

bors added a commit to rust-lang/rust-clippy that referenced this issue Sep 27, 2022
Don't lint `*_interior_mutable_const` on unions due to potential ICE.

fixes #9445
cc rust-lang/rust#101113

This started ICE'ing sometime last month due to stricter UB checks. I'm not sure how we could check the value of a union as MIRI doesn't seem to store which field is currently active.

changelog: Don't ICE on const unions containing a `!Freeze` type.
@matthiaskrgr
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-clippy Area: Clippy C-bug Category: This is a bug. 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.
Projects
None yet
Development

No branches or pull requests

3 participants