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: None, compiler/rustc_const_eval/src/interpret/validity.rs #122548

Closed
matthiaskrgr opened this issue Mar 15, 2024 · 1 comment · Fixed by #122684
Closed

ICE: None, compiler/rustc_const_eval/src/interpret/validity.rs #122548

matthiaskrgr opened this issue Mar 15, 2024 · 1 comment · Fixed by #122684
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. F-const_mut_refs `#![feature(const_mut_refs)]` F-const_refs_to_static `#![feature(const_refs_to_static)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 15, 2024

auto-reduced (treereduce-rust):

#![feature(const_mut_refs)]
#![feature(const_refs_to_static)]

use std::cell::UnsafeCell;

struct Meh {
    x: &'static UnsafeCell<i32>,
}

const MUH: Meh = Meh {
    x: &mut *(&READONLY as *const _ as *mut _),
};

static READONLY: i32 = 0;

pub fn main() {}
original code

original:

//@ compile-flags: -Zunleash-the-miri-inside-of-you
//@ normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
//@ normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
#![allow(invalid_reference_casting, static_mut_refs)]
#![deny(const_eval_mutable_ptr_in_final_value)]
use std::cell::UnsafeCell;
use std::sync::POINTS_TO_MUTABLE2::*;

// this test ensures that our mutability story is sound

struct Meh {
    x: &'static UnsafeCell<i32>,
}
unsafe impl Sync for Synced {}

//~^ ERROR: it is undefined behavior to use this value
// all allocs interned here will be marked immutable.
const MUH: Meh = Meh {
    //~^ ERROR encountered mutable pointer in final value of constant
    //~| WARNING this was previously accepted by the compiler
    //~| ERROR: it is undefined behavior to use this value
    x: &mut *(&READONLY as *const _ as *mut _),
};

struct Synced {
    x: UnsafeCell<i32>,
}
unsafe impl Sync for Synced {}

// Make sure we also catch this behind a type-erased `dyn Trait` reference.
const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
//~^ ERROR: mutable pointer in final value
//~| WARNING this was previously accepted by the compiler
//~| ERROR it is undefined behavior to use this value

// Make sure we also catch mutable references in values that shouldn't have them.
static mut FOO: i32 = 0;
const SUBTLE: &mut i32 = unsafe { &mut FOO };
//~^ ERROR: it is undefined behavior to use this value
//~| static

const BLUNT: &mut i32 = &mut 42;
//~^ ERROR: mutable pointer in final value
//~| WARNING this was previously accepted by the compiler
//~| ERROR it is undefined behavior to use this value

// Check for mutable references to read-only memory.
static READONLY: i32 = 0;
static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
//~^ ERROR: it is undefined behavior to use this value
//~| pointing to read-only memory

// Check for consts pointing to mutable memory.
// These are fine as long as they are not being read.
static mut MUTABLE: i32 = 42;
const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE }; //~ERROR: undefined behavior
//~| encountered reference to mutable memory
const READS_FROM_MUTABLE: i32 = *POINTS_TO_MUTABLE1;
static mut MUTABLE_REF: &mut i32 = &mut 42;
const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
//~^ ERROR: evaluation of constant value failed
//~| accesses mutable global memory

const POINTS_TO_MUTABLE_INNER: *const i32 = &mut 42 as *mut _ as *const _;
//~^ ERROR: it is undefined behavior to use this value
//~| WARNING this was previously accepted by the compiler

const POINTS_TO_MUTABLE_INNER2: *const i32 = &mut 42 as *const _;
//~^ ERROR: mutable pointer in final value
//~| WARNING this was previously accepted by the compiler

const INTERIOR_MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
//~^ ERROR: mutable pointer in final value
//~| WARNING this was previously accepted by the compiler

struct SyncPtr<T> {
    x: *const T,
}
unsafe impl<T> Sync for SyncPtr<T> {}

// These pass the lifetime checks because of the "tail expression" / "outer scope" rule.
// (This relies on `SyncPtr` being a curly brace struct.)
// However, we intern the inner memory as read-only, so this must be rejected.
// (Also see `static-no-inner-mut` for similar tests on `static`.)
const SUBTLE: &mut i32 = unsafe { &mut FOO };
//~^ ERROR mutable pointer in final value
//~| WARNING this was previously accepted by the compiler

const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x: &mut 42 as *mut _ as *const _ };
//~^ ERROR mutable pointer in final value
//~| WARNING this was previously accepted by the compiler

const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
//~^ ERROR mutable pointer in final value
//~| WARNING this was previously accepted by the compiler

fn main() {
    unsafe {
        *MUH.x.get() = 99;
    }
}

Version information

rustc 1.78.0-nightly (d7723b219 2024-03-15)
binary: rustc
commit-hash: d7723b21910075a3c38c2b6e64b2467394c93724
commit-date: 2024-03-15
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zcrate-attr=feature(const_mut_refs) -Zcrate-attr=feature(const_refs_to_static)

Program output

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.3yun4V90lzTt/rustc_testrunner_tmpdir_reporting.rjeSi3TM1Rid/mvce.rs:11:26
   |
11 | static READONLY: i32 = 0;
   |                          ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.3yun4V90lzTt/rustc_testrunner_tmpdir_reporting.rjeSi3TM1Rid/mvce.rs`

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/validity.rs:712:65:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:     0x7f39096dc912 - std::backtrace_rs::backtrace::libunwind::trace::h2da6c6ec1776ed1b
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7f39096dc912 - std::backtrace_rs::backtrace::trace_unsynchronized::h0cb696e9cf0107ba
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f39096dc912 - std::sys_common::backtrace::_print_fmt::hfb11ed7a7d38a028
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f39096dc912 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1048d052a888b26c
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f390972dc8c - core::fmt::rt::Argument::fmt::hcf8265a03a389e34
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/core/src/fmt/rt.rs:142:9
   5:     0x7f390972dc8c - core::fmt::write::hd12c3a84e62e1cec
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/core/src/fmt/mod.rs:1153:17
   6:     0x7f39096d180f - std::io::Write::write_fmt::h236993be0d5d91de
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/io/mod.rs:1843:15
   7:     0x7f39096dc6e4 - std::sys_common::backtrace::_print::he44e9b7b149d34c3
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f39096dc6e4 - std::sys_common::backtrace::print::h6f8e8e3b9f052551
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f39096df3eb - std::panicking::default_hook::{{closure}}::h321d04c9179dccca
  10:     0x7f39096df143 - std::panicking::default_hook::hd949d1362c2e5565
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/panicking.rs:292:9
  11:     0x7f39063a68ef - std[8d86c5c35c0bd7fb]::panicking::update_hook::<alloc[b74d3dbad420fbd6]::boxed::Box<rustc_driver_impl[724f095572d73222]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f39096dfb50 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h4f6c988be7bdee68
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/alloc/src/boxed.rs:2034:9
  13:     0x7f39096dfb50 - std::panicking::rust_panic_with_hook::hb71de646be3b3469
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/panicking.rs:783:13
  14:     0x7f39096df859 - std::panicking::begin_panic_handler::{{closure}}::hbca15a8a0a3dee72
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/panicking.rs:649:13
  15:     0x7f39096dcde6 - std::sys_common::backtrace::__rust_end_short_backtrace::h885125d06f4d13f0
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f39096df5c4 - rust_begin_unwind
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/panicking.rs:645:5
  17:     0x7f390972a1a5 - core::panicking::panic_fmt::ha3facef8c988a935
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/core/src/panicking.rs:72:14
  18:     0x7f390972a263 - core::panicking::panic::h0108ad4bd7f0026d
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/core/src/panicking.rs:145:5
  19:     0x7f3909729f36 - core::option::unwrap_failed::h1e7018e297d5a5da
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/core/src/option.rs:1985:5
  20:     0x7f3908d63259 - <rustc_const_eval[1fc5f74b41ebce23]::interpret::validity::ValidityVisitor<rustc_const_eval[1fc5f74b41ebce23]::const_eval::machine::CompileTimeInterpreter>>::in_mutable_memory.cold.0
  21:     0x7f39076e03ca - <rustc_const_eval[1fc5f74b41ebce23]::interpret::validity::ValidityVisitor<rustc_const_eval[1fc5f74b41ebce23]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[1fc5f74b41ebce23]::interpret::visitor::ValueVisitor<rustc_const_eval[1fc5f74b41ebce23]::const_eval::machine::CompileTimeInterpreter>>::visit_value
  22:     0x7f3907c1a30f - rustc_const_eval[1fc5f74b41ebce23]::const_eval::eval_queries::eval_to_allocation_raw_provider
  23:     0x7f3907c18cf6 - rustc_query_impl[5406894609d381a6]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[5406894609d381a6]::query_impl::eval_to_allocation_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[35d2191f4cbfc53b]::query::erase::Erased<[u8; 24usize]>>
  24:     0x7f3907deb351 - rustc_query_system[40cbee46219fc421]::query::plumbing::try_execute_query::<rustc_query_impl[5406894609d381a6]::DynamicConfig<rustc_query_system[40cbee46219fc421]::query::caches::DefaultCache<rustc_middle[35d2191f4cbfc53b]::ty::ParamEnvAnd<rustc_middle[35d2191f4cbfc53b]::mir::interpret::GlobalId>, rustc_middle[35d2191f4cbfc53b]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[5406894609d381a6]::plumbing::QueryCtxt, false>
  25:     0x7f3907deaf2c - rustc_query_impl[5406894609d381a6]::query_impl::eval_to_allocation_raw::get_query_non_incr::__rust_end_short_backtrace
  26:     0x7f3907dec710 - rustc_const_eval[1fc5f74b41ebce23]::const_eval::eval_queries::eval_to_const_value_raw_provider
  27:     0x7f3907dec536 - rustc_query_impl[5406894609d381a6]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[5406894609d381a6]::query_impl::eval_to_const_value_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[35d2191f4cbfc53b]::query::erase::Erased<[u8; 24usize]>>
  28:     0x7f3907deb314 - rustc_query_system[40cbee46219fc421]::query::plumbing::try_execute_query::<rustc_query_impl[5406894609d381a6]::DynamicConfig<rustc_query_system[40cbee46219fc421]::query::caches::DefaultCache<rustc_middle[35d2191f4cbfc53b]::ty::ParamEnvAnd<rustc_middle[35d2191f4cbfc53b]::mir::interpret::GlobalId>, rustc_middle[35d2191f4cbfc53b]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[5406894609d381a6]::plumbing::QueryCtxt, false>
  29:     0x7f3907deae2c - rustc_query_impl[5406894609d381a6]::query_impl::eval_to_const_value_raw::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7f3907bb5b8b - <rustc_middle[35d2191f4cbfc53b]::query::plumbing::TyCtxtEnsure>::const_eval_poly
  31:     0x7f3907bb3d91 - rustc_hir_analysis[c56ac84798d79461]::check_crate
  32:     0x7f3907d987ea - rustc_interface[62605c20d0dc0fef]::passes::analysis
  33:     0x7f3907d98425 - rustc_query_impl[5406894609d381a6]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[5406894609d381a6]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[35d2191f4cbfc53b]::query::erase::Erased<[u8; 1usize]>>
  34:     0x7f3908406165 - rustc_query_system[40cbee46219fc421]::query::plumbing::try_execute_query::<rustc_query_impl[5406894609d381a6]::DynamicConfig<rustc_query_system[40cbee46219fc421]::query::caches::SingleCache<rustc_middle[35d2191f4cbfc53b]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[5406894609d381a6]::plumbing::QueryCtxt, false>
  35:     0x7f3908405ec9 - rustc_query_impl[5406894609d381a6]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7f390815e284 - rustc_interface[62605c20d0dc0fef]::interface::run_compiler::<core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>, rustc_driver_impl[724f095572d73222]::run_compiler::{closure#0}>::{closure#0}
  37:     0x7f3908623805 - std[8d86c5c35c0bd7fb]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[62605c20d0dc0fef]::util::run_in_thread_with_globals<rustc_interface[62605c20d0dc0fef]::util::run_in_thread_pool_with_globals<rustc_interface[62605c20d0dc0fef]::interface::run_compiler<core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>, rustc_driver_impl[724f095572d73222]::run_compiler::{closure#0}>::{closure#0}, core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>>::{closure#0}, core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>>
  38:     0x7f3908623632 - <<std[8d86c5c35c0bd7fb]::thread::Builder>::spawn_unchecked_<rustc_interface[62605c20d0dc0fef]::util::run_in_thread_with_globals<rustc_interface[62605c20d0dc0fef]::util::run_in_thread_pool_with_globals<rustc_interface[62605c20d0dc0fef]::interface::run_compiler<core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>, rustc_driver_impl[724f095572d73222]::run_compiler::{closure#0}>::{closure#0}, core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>>::{closure#0}, core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[d25ee0c10dd6dc91]::result::Result<(), rustc_span[6c3fee00dc2a924e]::ErrorGuaranteed>>::{closure#1} as core[d25ee0c10dd6dc91]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:     0x7f39096e91e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hab5d072bb143d8fe
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/alloc/src/boxed.rs:2020:9
  40:     0x7f39096e91e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he1f566ad483a8fda
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/alloc/src/boxed.rs:2020:9
  41:     0x7f39096e91e5 - std::sys::pal::unix::thread::Thread::new::thread_start::h23d47a8da771b95c
                               at /rustc/d7723b21910075a3c38c2b6e64b2467394c93724/library/std/src/sys/pal/unix/thread.rs:108:17
  42:     0x7f390326455a - <unknown>
  43:     0x7f39032e1a3c - <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.78.0-nightly (d7723b219 2024-03-15) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z crate-attr=feature(const_mut_refs) -Z crate-attr=feature(const_refs_to_static) -Z dump-mir-dir=dir

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `MUH`
#1 [eval_to_const_value_raw] simplifying constant for the type system `MUH`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0601`.

@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. labels Mar 15, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 15, 2024
@matthiaskrgr matthiaskrgr changed the title ICE: None, compiler/rustc_const_eval/src/interpret/validity.rs ICE: None, compiler/rustc_const_eval/src/interpret/validity.rs Mar 15, 2024
@matthiaskrgr
Copy link
Member Author

ICE since #121087 cc @oli-obk

@jieyouxu jieyouxu added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) F-const_mut_refs `#![feature(const_mut_refs)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue F-const_refs_to_static `#![feature(const_refs_to_static)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 15, 2024
@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Apr 15, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Apr 17, 2024
…after_validaiton, r=RalfJung

Delay interning errors to after validation

fixes rust-lang#122398
fixes rust-lang#122548

This improves diagnostics since validation errors are usually more helpful compared with interning errors that just make broad statements about the entire constant

r? `@RalfJung`
@bors bors closed this as completed in 5260893 Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. F-const_mut_refs `#![feature(const_mut_refs)]` F-const_refs_to_static `#![feature(const_refs_to_static)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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