rust-lang / rustc_codegen_cranelift

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

ICE: extern type field offset #1436

Closed matthiaskrgr closed 6 months ago

matthiaskrgr commented 6 months ago

from rustc: ./tests/ui/extern/extern-types-field-offset.rs

auto-reduced (treereduce-rust):

#![feature(extern_types)]

extern "C" {
    type Opaque;
}

struct Newtype(Opaque);

struct S {
    i: i32,
    j: i32,
    a: Newtype,
}

fn main() {
    let buf = [0i32; 4];

    let x: &S = unsafe { &*(&buf as *const _ as *const S) };

    let _field = &x.a;
}

original:

#![feature(extern_types)]

extern "C" {
    type Opaque;
}

struct Newtype(Opaque);

struct S {
    i: i32,
    j: i32,
    a: Newtype,
}

fn main() {
    let buf = [0i32; 4];

    let x: &Newtype = unsafe { &*(&buf as *const _ as *const Newtype) };
    // Projecting to the newtype works, because it is always at offset 0.
    let _field = &x.0;

    let x: &S = unsafe { &*(&buf as *const _ as *const S) };
    // Accessing sized fields is perfectly fine, even at non-zero offsets.
    let _field = &x.i;
    let _field = &x.j;
    // This needs to compute the field offset, but we don't know the type's alignment,
    // so this panics.
    let _field = &x.a; //~ERROR: does not have a known offset
}

Version information

rustc 1.76.0-nightly (77d169975 2023-12-13)
binary: rustc
commit-hash: 77d16997566dfb2384bcbc504e2579c7ae973666
commit-date: 2023-12-13
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: fields `i` and `j` are never read --> /tmp/icemaker_global_tempdir.PxgKg5hoooyA/rustc_testrunner_tmpdir_reporting.4ri42q0caYiT/mvce.rs:10:5 | 9 | struct S { | - fields in this struct 10 | i: i32, | ^ 11 | j: i32, | ^ | = note: `#[warn(dead_code)]` on by default thread 'rustc' panicked at src/value_and_place.rs:739:61: called `Option::unwrap()` on a `None` value stack backtrace: 0: 0x7f3e2d98b45c - std::backtrace_rs::backtrace::libunwind::trace::h8eaf990e4a504b86 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x7f3e2d98b45c - std::backtrace_rs::backtrace::trace_unsynchronized::hbc236c387b7f1e2f at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f3e2d98b45c - std::sys_common::backtrace::_print_fmt::h854fa9a625b79fe7 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f3e2d98b45c - ::fmt::h367ac729761cc34a at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f3e2d9de510 - core::fmt::rt::Argument::fmt::hc045d8fab7f11469 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/core/src/fmt/rt.rs:142:9 5: 0x7f3e2d9de510 - core::fmt::write::h5adaab5759d35e9c at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/core/src/fmt/mod.rs:1120:17 6: 0x7f3e2d97f34f - std::io::Write::write_fmt::hdf01958bd3129cc1 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/io/mod.rs:1810:15 7: 0x7f3e2d98b244 - std::sys_common::backtrace::_print::hef493aa049da10fb at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f3e2d98b244 - std::sys_common::backtrace::print::hceb63a7aa0b95526 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f3e2d98df47 - std::panicking::default_hook::{{closure}}::h379064c2e91c2583 10: 0x7f3e2d98dcaf - std::panicking::default_hook::h74d2197136da14a8 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/panicking.rs:292:9 11: 0x7f3e30735c30 - std[5e29b6ead2e77b88]::panicking::update_hook::>::{closure#0} 12: 0x7f3e2d98e688 - as core::ops::function::Fn>::call::he81fb398d05e4b0f at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/alloc/src/boxed.rs:2029:9 13: 0x7f3e2d98e688 - std::panicking::rust_panic_with_hook::h2b6da4caf2f04b60 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/panicking.rs:783:13 14: 0x7f3e2d98e3a9 - std::panicking::begin_panic_handler::{{closure}}::h830b6617ba119779 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/panicking.rs:649:13 15: 0x7f3e2d98b926 - std::sys_common::backtrace::__rust_end_short_backtrace::h55a117102e902ec7 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f3e2d98e142 - rust_begin_unwind at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/panicking.rs:645:5 17: 0x7f3e2d9dabd5 - core::panicking::panic_fmt::hf8c847bebf55f403 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/core/src/panicking.rs:72:14 18: 0x7f3e2d9dac93 - core::panicking::panic::hed6e296997f82f3a at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/core/src/panicking.rs:144:5 19: 0x7f3e24059957 - ::place_field 20: 0x7f3e240113cc - rustc_codegen_cranelift[871d4eb929383ae1]::base::codegen_place 21: 0x7f3e24008258 - rustc_codegen_cranelift[871d4eb929383ae1]::base::codegen_fn_body 22: 0x7f3e24023417 - rustc_codegen_cranelift[871d4eb929383ae1]::driver::aot::module_codegen::{closure#0} 23: 0x7f3e24020685 - rustc_codegen_cranelift[871d4eb929383ae1]::driver::aot::module_codegen 24: 0x7f3e2402cd2c - rustc_codegen_cranelift[871d4eb929383ae1]::driver::aot::run_aot 25: 0x7f3e2405bd6a - ::codegen_crate 26: 0x7f3e32783dc5 - rustc_interface[1010bb494e88232]::passes::start_codegen 27: 0x7f3e3278352e - ::codegen_and_build_linker 28: 0x7f3e32a3afad - rustc_interface[1010bb494e88232]::interface::run_compiler::, rustc_driver_impl[143a96d1e9339ddc]::run_compiler::{closure#0}>::{closure#0} 29: 0x7f3e329af54a - std[5e29b6ead2e77b88]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[143a96d1e9339ddc]::run_compiler::{closure#0}>::{closure#0}, core[52cdeeaf75e7a502]::result::Result<(), rustc_span[80493f288ab27be]::ErrorGuaranteed>>::{closure#0}, core[52cdeeaf75e7a502]::result::Result<(), rustc_span[80493f288ab27be]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[52cdeeaf75e7a502]::result::Result<(), rustc_span[80493f288ab27be]::ErrorGuaranteed>> 30: 0x7f3e329af373 - <::spawn_unchecked_, rustc_driver_impl[143a96d1e9339ddc]::run_compiler::{closure#0}>::{closure#0}, core[52cdeeaf75e7a502]::result::Result<(), rustc_span[80493f288ab27be]::ErrorGuaranteed>>::{closure#0}, core[52cdeeaf75e7a502]::result::Result<(), rustc_span[80493f288ab27be]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[52cdeeaf75e7a502]::result::Result<(), rustc_span[80493f288ab27be]::ErrorGuaranteed>>::{closure#1} as core[52cdeeaf75e7a502]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 31: 0x7f3e2d998515 - as core::ops::function::FnOnce>::call_once::ha17502cbcaf4d2cc at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/alloc/src/boxed.rs:2015:9 32: 0x7f3e2d998515 - as core::ops::function::FnOnce>::call_once::h928bfdaf626c1d3d at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/alloc/src/boxed.rs:2015:9 33: 0x7f3e2d998515 - std::sys::unix::thread::Thread::new::thread_start::he4ab37baefb01514 at /rustc/77d16997566dfb2384bcbc504e2579c7ae973666/library/std/src/sys/unix/thread.rs:108:17 34: 0x7f3e2d7799eb - 35: 0x7f3e2d7fd7cc - 36: 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 (77d169975 2023-12-13) 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: 1 warning emitted ```

bjorn3 commented 6 months ago

Fixed by https://github.com/rust-lang/rustc_codegen_cranelift/commit/fdcf56c5b74b84d79ba9d4262574464e49808ad2