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: Layout != Layout #3400

Closed
matthiaskrgr opened this issue Mar 23, 2024 · 7 comments · Fixed by rust-lang/rust#123049
Closed

ICE: Layout != Layout #3400

matthiaskrgr opened this issue Mar 23, 2024 · 7 comments · Fixed by rust-lang/rust#123049
Assignees

Comments

@matthiaskrgr
Copy link
Member

tests/mir-opt/async_closure_shims.rs

//@ edition:2021
// skip-filecheck
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(async_closure, noop_waker, async_fn_traits)]

use std::future::Future;
use std::ops::{AsyncFnMut, AsyncFnOnce};
use std::pin::pin;
use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
    let mut fut = pin!(fut);
    let ctx = &mut Context::from_waker(Waker::noop());

    loop {
        match fut.as_mut().poll(ctx) {
            Poll::Pending => {}
            Poll::Ready(t) => break t,
        }
    }
}

async fn call_mut(f: &mut impl AsyncFnMut(i32)) {
    f(0).await;
}

async fn call_once(f: impl AsyncFnOnce(i32)) {
    f(1).await;
}

async fn call_normal<F: Future<Output = ()>>(f: &impl Fn(i32) -> F) {
    f(1).await;
}

// EMIT_MIR async_closure_shims.main-{closure#0}-{closure#0}.coroutine_closure_by_move.0.mir
// EMIT_MIR async_closure_shims.main-{closure#0}-{closure#0}-{closure#0}.coroutine_by_move.0.mir
// EMIT_MIR async_closure_shims.main-{closure#0}-{closure#1}.coroutine_closure_by_ref.0.mir
pub fn main() {
    block_on(async {
        let b = 2i32;
        let mut async_closure = async move |a: i32| {
            let a = &a;
            let b = &b;
        };
        call_mut(&mut async_closure).await;
        call_once(async_closure).await;

        let async_closure = async move |a: i32| {
            let a = &a;
        };
        call_normal(&async_closure).await;
    });
}

cargo miri run =>

Preparing a sysroot for Miri (target: x86_64-unknown-linux-gnu)... done
WARNING: Ignoring `RUSTC_WRAPPER` environment variable, Miri does not support wrapping.
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `/home/matthias/.rustup/toolchains/master/bin/cargo-miri runner target/miri/x86_64-unknown-linux-gnu/debug/miricrash`
warning: unused variable: `a`
  --> src/main.rs:43:17
   |
43 |             let a = &a;
   |                 ^ help: if this is intentional, prefix it with an underscore: `_a`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `b`
  --> src/main.rs:44:17
   |
44 |             let b = &b;
   |                 ^ help: if this is intentional, prefix it with an underscore: `_b`

warning: unused variable: `a`
  --> src/main.rs:50:17
   |
50 |             let a = &a;
   |                 ^ help: if this is intentional, prefix it with an underscore: `_a`

thread 'rustc' panicked at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/compiler/rustc_const_eval/src/interpret/terminator.rs:464:9:
assertion `left == right` failed
  left: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) }
 right: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 0..=18446744073709551615 }), fields: Primitive, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) }
stack backtrace:
   0:     0x7ffbbdc3cfd5 - std::backtrace_rs::backtrace::libunwind::trace::h48d354627a74bc58
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7ffbbdc3cfd5 - std::backtrace_rs::backtrace::trace_unsynchronized::h4508321e2542b6ac
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ffbbdc3cfd5 - std::sys_common::backtrace::_print_fmt::hdfda8261c72df561
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7ffbbdc3cfd5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hc40009887a537c13
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7ffbbdc8e07b - core::fmt::rt::Argument::fmt::ha3dc7dd6b6fca21a
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/core/src/fmt/rt.rs:142:9
   5:     0x7ffbbdc8e07b - core::fmt::write::h8437e735cd76b6e4
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/core/src/fmt/mod.rs:1153:17
   6:     0x7ffbbdc31cef - std::io::Write::write_fmt::h6d5be71476888e90
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/io/mod.rs:1843:15
   7:     0x7ffbbdc3cdae - std::sys_common::backtrace::_print::h74631273272a9e63
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7ffbbdc3cdae - std::sys_common::backtrace::print::haa65f55c1cb47a40
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7ffbbdc3fa29 - std::panicking::default_hook::{{closure}}::h7714116d9373466f
  10:     0x7ffbbdc3f793 - std::panicking::default_hook::h113257df00e0f39d
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/panicking.rs:292:9
  11:     0x7ffbba5cc3df - std[8a89dbba75908d0d]::panicking::update_hook::<alloc[1f2071bdd531992b]::boxed::Box<rustc_driver_impl[f4179f9ab23b96e3]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7ffbbdc401c8 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h9a84547210a755cf
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/alloc/src/boxed.rs:2029:9
  13:     0x7ffbbdc401c8 - std::panicking::rust_panic_with_hook::h8d38c4a03b431567
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/panicking.rs:789:13
  14:     0x7ffbbdc3fed6 - std::panicking::begin_panic_handler::{{closure}}::h128f51293fa958d7
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/panicking.rs:657:13
  15:     0x7ffbbdc3d499 - std::sys_common::backtrace::__rust_end_short_backtrace::ha1f75ae71b0d9c49
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7ffbbdc3fc07 - rust_begin_unwind
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/panicking.rs:645:5
  17:     0x7ffbbdc8a516 - core::panicking::panic_fmt::h358d493012846cd7
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/core/src/panicking.rs:72:14
  18:     0x7ffbbdc8aa0e - core::panicking::assert_failed_inner::hb1318a32469c3e96
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/core/src/panicking.rs:339:17
  19:     0x605768c9c558 - core[f45bbd2da099a22]::panicking::assert_failed::<rustc_target[40f90215956c6549]::abi::Layout, rustc_target[40f90215956c6549]::abi::Layout>
  20:     0x605768c873d2 - <rustc_const_eval[c03759a990b6c5d6]::interpret::eval_context::InterpCx<miri[21b6ec86b7601489]::machine::MiriMachine>>::pass_argument::<core[f45bbd2da099a22]::iter::adapters::filter::Filter<core[f45bbd2da099a22]::iter::adapters::zip::Zip<core[f45bbd2da099a22]::slice::iter::Iter<rustc_const_eval[c03759a990b6c5d6]::interpret::terminator::FnArg<miri[21b6ec86b7601489]::machine::Provenance>>, core[f45bbd2da099a22]::slice::iter::Iter<rustc_target[40f90215956c6549]::abi::call::ArgAbi<rustc_middle[c8c15faa8e9beb8b]::ty::Ty>>>, <rustc_const_eval[c03759a990b6c5d6]::interpret::eval_context::InterpCx<miri[21b6ec86b7601489]::machine::MiriMachine>>::eval_fn_call::{closure#2}>>
  21:     0x605768cd22a6 - <rustc_const_eval[c03759a990b6c5d6]::interpret::eval_context::InterpCx<miri[21b6ec86b7601489]::machine::MiriMachine>>::eval_fn_call
  22:     0x605768d30ae7 - <rustc_const_eval[c03759a990b6c5d6]::interpret::eval_context::InterpCx<miri[21b6ec86b7601489]::machine::MiriMachine> as miri[21b6ec86b7601489]::concurrency::thread::EvalContextExt>::run_threads
  23:     0x605768d3daf9 - miri[21b6ec86b7601489]::eval::eval_entry
  24:     0x605768c1b1dc - <miri[a2ea3133bfc7258b]::MiriCompilerCalls as rustc_driver_impl[f4179f9ab23b96e3]::Callbacks>::after_analysis
  25:     0x7ffbbc67c08d - rustc_interface[8341846da6c7d0ff]::interface::run_compiler::<core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>, rustc_driver_impl[f4179f9ab23b96e3]::run_compiler::{closure#0}>::{closure#0}
  26:     0x7ffbbc759917 - std[8a89dbba75908d0d]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[8341846da6c7d0ff]::util::run_in_thread_with_globals<rustc_interface[8341846da6c7d0ff]::util::run_in_thread_pool_with_globals<rustc_interface[8341846da6c7d0ff]::interface::run_compiler<core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>, rustc_driver_impl[f4179f9ab23b96e3]::run_compiler::{closure#0}>::{closure#0}, core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>>::{closure#0}, core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>>
  27:     0x7ffbbc759744 - <<std[8a89dbba75908d0d]::thread::Builder>::spawn_unchecked_<rustc_interface[8341846da6c7d0ff]::util::run_in_thread_with_globals<rustc_interface[8341846da6c7d0ff]::util::run_in_thread_pool_with_globals<rustc_interface[8341846da6c7d0ff]::interface::run_compiler<core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>, rustc_driver_impl[f4179f9ab23b96e3]::run_compiler::{closure#0}>::{closure#0}, core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>>::{closure#0}, core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f45bbd2da099a22]::result::Result<(), rustc_span[24b8481ad64c70d2]::ErrorGuaranteed>>::{closure#1} as core[f45bbd2da099a22]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  28:     0x7ffbbdc49989 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hd41657c9a166bce4
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/alloc/src/boxed.rs:2015:9
  29:     0x7ffbbdc49989 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hed92e47eb903f916
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/alloc/src/boxed.rs:2015:9
  30:     0x7ffbbdc49989 - std::sys::pal::unix::thread::Thread::new::thread_start::h5fcbd92b3a001a45
                               at /rustc/e50ab294714ae706f85948efb17fac3c599ba2d3/library/std/src/sys/pal/unix/thread.rs:108:17
  31:     0x7ffbb73bb55a - <unknown>
  32:     0x7ffbb7438a3c - <unknown>
  33:                0x0 - <unknown>

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

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

note: please attach the file at `/tmp/miricrash/rustc-ice-2024-03-23T22_36_59-689320.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED] -C target-cpu=native

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:49:29
   |
49 |         let async_closure = async move |a: i32| {
   |                             ^^^^^^^^^^^^^^^^^^^
   |
   = note: BACKTRACE:
   = note: inside closure at src/main.rs:49:29: 49:48
note: inside closure
  --> src/main.rs:33:5
   |
33 |     f(1).await;
   |     ^^^^
note: inside closure
  --> src/main.rs:52:37
   |
52 |         call_normal(&async_closure).await;
   |                                     ^^^^^
note: inside `block_on::<(), {async block@src/main.rs:40:14: 53:6}>`
  --> src/main.rs:17:15
   |
17 |         match fut.as_mut().poll(ctx) {
   |               ^^^^^^^^^^^^^^^^^^^^^^
note: inside `main`
  --> src/main.rs:40:5
   |
40 | /     block_on(async {
41 | |         let b = 2i32;
42 | |         let mut async_closure = async move |a: i32| {
43 | |             let a = &a;
...  |
52 | |         call_normal(&async_closure).await;
53 | |     });
   | |______^
   = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/matthias/.rustup/toolchains/master/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/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:155:18: 155:21
   = note: inside closure at /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/rt.rs:166:18: 166: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/matthias/.rustup/toolchains/master/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/matthias/.rustup/toolchains/master/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/matthias/.rustup/toolchains/master/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/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/panic.rs:146:14: 146:33
   = note: inside closure at /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/rt.rs:148:48: 148:73
   = note: inside `std::panicking::r#try::do_call::<{closure@std::rt::lang_start_internal::{closure#2}}, isize>` at /home/matthias/.rustup/toolchains/master/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/matthias/.rustup/toolchains/master/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/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/panic.rs:146:14: 146:33
   = note: inside `std::rt::lang_start_internal` at /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/rt.rs:148:20: 148:98
   = note: inside `std::rt::lang_start::<()>` at /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/std/src/rt.rs:165:17: 170:6

warning: 3 warnings emitted

miri 0.1.0 (e50ab29 2024-03-23)

@matthiaskrgr
Copy link
Member Author

bit smaller

//@ edition:2021
// skip-filecheck
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(async_closure, noop_waker, async_fn_traits)]

use std::future::Future;
use std::pin::pin;
use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
    let mut fut = pin!(fut);
    let ctx = &mut Context::from_waker(Waker::noop());

    loop {
        match fut.as_mut().poll(ctx) {
            Poll::Pending => {}
            Poll::Ready(t) => break t,
        }
    }
}

async fn call_normal<F: Future<Output = ()>>(f: &impl Fn(i32) -> F) {
    f(1).await;
}

pub fn main() {
    block_on(async {
        let async_closure = async move |a: i32| {};
        call_normal(&async_closure).await;
    });
}

@bjorn3
Copy link
Member

bjorn3 commented Mar 24, 2024

rust-lang/rust#120712 has a bug causing cg_clif to crash on tests/ui/async-await/async-closures/once.rs. This miri crash here may be related. I suspect fn_sig_for_fn_abi may need to be updated for async closures.

@RalfJung
Copy link
Member

Yeah this looks like the types in the function signatures are not coherent with the corresponding arguments in the Call terminator.

Cc @compiler-errors

@oli-obk
Copy link
Contributor

oli-obk commented Mar 24, 2024

Why doesn't the following hit the same ICE? Should be fine to call a function expecting a raw pointer via a fn pointer saying its type is a reference

fn main() {
    fn foo(_: *const u32) {}
    let x: fn(*const u32) = foo;
    let x: fn(&u32) = unsafe {std::mem::transmute(x)};
    x(&43);
}

@oli-obk
Copy link
Contributor

oli-obk commented Mar 24, 2024

types in the function signatures are not coherent with the corresponding arguments in the Call terminator.

Ah. That's the key part. We don't explicitly do type punning via fn ptrs, but literally calling a function expecting a raw pointer with a reference typed value. Yea, that's probably invalid MIR and we need to teach validation about it

@RalfJung
Copy link
Member

RalfJung commented Mar 24, 2024

Yes, caller vs callee mismatches are fine and carefully handled. But the ICE arises due to a mismatch between two different ways of computing the caller-side type: by computing the caller-side fn sig, and by computing the type of the argument that the caller passes.

@compiler-errors
Copy link
Member

I'll take a look!

@compiler-errors compiler-errors self-assigned this Mar 24, 2024
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 26, 2024
…rcvr, r=oli-obk

In `ConstructCoroutineInClosureShim`, pass receiver by mut ref, not mut pointer

The receivers were compatible at codegen time, but did not necessarily have the same layouts due to niches, which was caught by miri.

Fixes rust-lang/miri#3400

r? oli-obk
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 27, 2024
Rollup merge of rust-lang#123049 - compiler-errors:coroutine-closure-rcvr, r=oli-obk

In `ConstructCoroutineInClosureShim`, pass receiver by mut ref, not mut pointer

The receivers were compatible at codegen time, but did not necessarily have the same layouts due to niches, which was caught by miri.

Fixes rust-lang/miri#3400

r? oli-obk
github-actions bot pushed a commit that referenced this issue Mar 29, 2024
…li-obk

In `ConstructCoroutineInClosureShim`, pass receiver by mut ref, not mut pointer

The receivers were compatible at codegen time, but did not necessarily have the same layouts due to niches, which was caught by miri.

Fixes #3400

r? oli-obk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants