rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.52k stars 94 forks source link

ICE: assertion failed `2 == 1` #1435

Closed matthiaskrgr closed 6 months ago

matthiaskrgr commented 7 months ago

auto-reduced (treereduce-rust):

use std::mem;

#[repr(packed(2), C)]
struct Slice([u32]);

struct PackedSized {
    f: u8,
    d: [u32; 4],
}

struct PackedUnsized {
    f: u8,
    d: Slice,
}

impl PackedSized {
    fn unsize(&self) -> &PackedUnsized {
        let len = 4usize;
        unsafe { mem::transmute((self, len)) }
    }
}

fn main() {
    unsafe {
        let p = PackedSized {
            f: 0,
            d: [1, 2, 3, 4],
        };
        let p = p.unsize() as *const PackedUnsized;
        let d = std::ptr::addr_of!((*p).d);
    }
}

original:

use std::mem;

#[repr(packed(2), C)]
struct Slice([u32]);

#[repr(packed(2), C)]
struct PackedSized {
    f: u8,
    d: [u32; 4],
}

#[repr(packed(2), C)]
struct PackedUnsized {
    f: u8,
    d: Slice,
}

impl PackedSized {
    fn unsize(&self) -> &PackedUnsized {
        // We can't unsize via a generic type since then we get the error
        // that packed structs with unsized tail don't work if the tail
        // might need dropping.
        let len = 4usize;
        unsafe { mem::transmute((self, len)) }
    }
}

fn main() { unsafe {
    let p = PackedSized { f: 0, d: [1, 2, 3, 4] };
    let p = p.unsize() as *const PackedUnsized;
    let d = std::ptr::addr_of!((*p).d);
    assert_eq!(d.cast::<u32>().read_unaligned(), 1);
} }

Version information

rustc 1.76.0-nightly (154f645d7 2023-12-05)
binary: rustc
commit-hash: 154f645d76a9752bd7ea58d7382b2e90e83557fb
commit-date: 2023-12-05
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zcodegen-backend=cranelift

Program output

``` warning: unused variable: `d` --> /tmp/icemaker_global_tempdir.plAF2EChgsBs/rustc_testrunner_tmpdir_reporting.OOg8Uc5cQuZO/mvce.rs:30:13 | 30 | let d = std::ptr::addr_of!((*p).d); | ^ help: if this is intentional, prefix it with an underscore: `_d` | = note: `#[warn(unused_variables)]` on by default warning: fields `f` and `d` are never read --> /tmp/icemaker_global_tempdir.plAF2EChgsBs/rustc_testrunner_tmpdir_reporting.OOg8Uc5cQuZO/mvce.rs:7:5 | 6 | struct PackedSized { | ----------- fields in this struct 7 | f: u8, | ^ 8 | d: [u32; 4], | ^ | = note: `#[warn(dead_code)]` on by default warning: field `f` is never read --> /tmp/icemaker_global_tempdir.plAF2EChgsBs/rustc_testrunner_tmpdir_reporting.OOg8Uc5cQuZO/mvce.rs:12:5 | 11 | struct PackedUnsized { | ------------- field in this struct 12 | f: u8, | ^ thread 'rustc' panicked at src/value_and_place.rs:30:17: assertion `left == right` failed left: 2 right: 1 stack backtrace: 0: 0x7f42a6f8b45c - std::backtrace_rs::backtrace::libunwind::trace::h35f4e5f522d43d7a at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x7f42a6f8b45c - std::backtrace_rs::backtrace::trace_unsynchronized::h0e761e8562bb70a3 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f42a6f8b45c - std::sys_common::backtrace::_print_fmt::hdbdc2dea859e18b4 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f42a6f8b45c - ::fmt::hbb910326e1a03d63 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f42a6fde4a0 - core::fmt::rt::Argument::fmt::h0aac43bc205189ec at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/core/src/fmt/rt.rs:142:9 5: 0x7f42a6fde4a0 - core::fmt::write::hdb4751abc76548d1 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/core/src/fmt/mod.rs:1120:17 6: 0x7f42a6f7f2bf - std::io::Write::write_fmt::h93b91714450cf81a at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/io/mod.rs:1810:15 7: 0x7f42a6f8b244 - std::sys_common::backtrace::_print::he1049711648b718e at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f42a6f8b244 - std::sys_common::backtrace::print::h5ab8cf6e6d0c31bd at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f42a6f8ded7 - std::panicking::default_hook::{{closure}}::hd8fcbde69a13f0dc 10: 0x7f42a6f8dc3f - std::panicking::default_hook::hb820e6e61784bba1 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/panicking.rs:292:9 11: 0x7f42a9ce8b30 - std[47c6a3bb6cb72ea7]::panicking::update_hook::>::{closure#0} 12: 0x7f42a6f8e618 - as core::ops::function::Fn>::call::h71dd819b088abc58 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/alloc/src/boxed.rs:2021:9 13: 0x7f42a6f8e618 - std::panicking::rust_panic_with_hook::hf9d24d39af723241 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/panicking.rs:783:13 14: 0x7f42a6f8e36e - std::panicking::begin_panic_handler::{{closure}}::h4932ca899b437bb6 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/panicking.rs:657:13 15: 0x7f42a6f8b926 - std::sys_common::backtrace::__rust_end_short_backtrace::h40f01f81f732b950 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f42a6f8e0d2 - rust_begin_unwind at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/panicking.rs:645:5 17: 0x7f42a6fdab65 - core::panicking::panic_fmt::hb4253499645cb1d5 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/core/src/panicking.rs:72:14 18: 0x7f42a6fdb0fb - core::panicking::assert_failed_inner::h8b4ddfd6fd095f30 19: 0x7f429d5c134f - core[25187a352b82944f]::panicking::assert_failed:: 20: 0x7f429d6564b6 - rustc_codegen_cranelift[16d1a980818d4aa1]::value_and_place::codegen_field 21: 0x7f429d658aae - ::place_field 22: 0x7f429d610bfc - rustc_codegen_cranelift[16d1a980818d4aa1]::base::codegen_place 23: 0x7f429d607a78 - rustc_codegen_cranelift[16d1a980818d4aa1]::base::codegen_fn_body 24: 0x7f429d622cba - rustc_codegen_cranelift[16d1a980818d4aa1]::driver::aot::module_codegen::{closure#0} 25: 0x7f429d61fe55 - rustc_codegen_cranelift[16d1a980818d4aa1]::driver::aot::module_codegen 26: 0x7f429d62c5fc - rustc_codegen_cranelift[16d1a980818d4aa1]::driver::aot::run_aot 27: 0x7f429d65b02a - ::codegen_crate 28: 0x7f42aba67305 - rustc_interface[b78234ab227b56f3]::passes::start_codegen 29: 0x7f42aba66a6e - ::codegen_and_build_linker 30: 0x7f42abf6f239 - rustc_interface[b78234ab227b56f3]::interface::run_compiler::, rustc_driver_impl[30ffa5efb6d67fe0]::run_compiler::{closure#0}>::{closure#0} 31: 0x7f42abf625cd - std[47c6a3bb6cb72ea7]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[30ffa5efb6d67fe0]::run_compiler::{closure#0}>::{closure#0}, core[25187a352b82944f]::result::Result<(), rustc_span[e7361cfd57c2bc5c]::ErrorGuaranteed>>::{closure#0}, core[25187a352b82944f]::result::Result<(), rustc_span[e7361cfd57c2bc5c]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[25187a352b82944f]::result::Result<(), rustc_span[e7361cfd57c2bc5c]::ErrorGuaranteed>> 32: 0x7f42abf623f7 - <::spawn_unchecked_, rustc_driver_impl[30ffa5efb6d67fe0]::run_compiler::{closure#0}>::{closure#0}, core[25187a352b82944f]::result::Result<(), rustc_span[e7361cfd57c2bc5c]::ErrorGuaranteed>>::{closure#0}, core[25187a352b82944f]::result::Result<(), rustc_span[e7361cfd57c2bc5c]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[25187a352b82944f]::result::Result<(), rustc_span[e7361cfd57c2bc5c]::ErrorGuaranteed>>::{closure#1} as core[25187a352b82944f]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 33: 0x7f42a6f984a5 - as core::ops::function::FnOnce>::call_once::hd1775f648686723a at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/alloc/src/boxed.rs:2007:9 34: 0x7f42a6f984a5 - as core::ops::function::FnOnce>::call_once::he9ea89aa13395012 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/alloc/src/boxed.rs:2007:9 35: 0x7f42a6f984a5 - std::sys::unix::thread::Thread::new::thread_start::hf7adff1f19643746 at /rustc/154f645d76a9752bd7ea58d7382b2e90e83557fb/library/std/src/sys/unix/thread.rs:108:17 36: 0x7f42a6d4d9eb - 37: 0x7f42a6dd17cc - 38: 0x0 - 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.76.0-nightly (154f645d7 2023-12-05) running on x86_64-unknown-linux-gnu note: compiler flags: -Z codegen-backend=cranelift -Z dump-mir-dir=dir query stack during panic: end of query stack warning: 3 warnings emitted ```

bjorn3 commented 7 months ago

There were a couple of fixes by RalfJ around unsized types on the rust side just yesterday. cg_clif hasn't adapted the fixes yet.