rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
94.68k stars 12.2k forks source link

Rustc panicked struct cannot be packed and aligned #83107

Closed Andy-Python-Programmer closed 2 years ago

Andy-Python-Programmer commented 3 years ago

Code

#[repr(packed, align(0x1000))]
struct GDT {
    kernel_null: GDTEntry,
    kernel_code: GDTEntry,
    kernel_data: GDTEntry,
    user_null: GDTEntry,
    user_code: GDTEntry,
    user_data: GDTEntry,
}

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (44e3daf5e 2020-12-31)
binary: rustc
commit-hash: 44e3daf5eee8263dfc3a2509e78ddd1f6f783a0e
commit-date: 2020-12-31
host: x86_64-pc-windows-msvc
release: 1.51.0-nightly

Error output

error[E0587]: type has conflicting packed and align representation hints
  --> src\gdt.rs:34:1
   |
34 | / struct GDT {
35 | |     kernel_null: GDTEntry,
36 | |     kernel_code: GDTEntry,
37 | |     kernel_data: GDTEntry,
...  |
40 | |     user_data: GDTEntry,
41 | | }
   | |_^

error: internal compiler error: compiler\rustc_middle\src\ty\layout.rs:283:13: struct cannot be packed and aligned

thread 'rustc' panicked at 'Box<Any>', compiler\rustc_errors\src\lib.rs:958:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: 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.51.0-nightly (44e3daf5e 2020-12-31) running on x86_64-pc-windows-msvc

note: compiler flags: -Z unstable-options -C panic=abort -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin   

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [layout_raw] computing layout of `gdt::GDT`
#1 [layout_raw] computing layout of `core::option::Option<gdt::GDT>`
end of query stack
error: aborting due to 2 previous errors
Backtrace

``` PS H:\Andy\Rust\aero> $Env:RUST_BACKTRACE="full"; cargo run -- -L H:\Programs\qemu Compiling aero v0.1.0 (H:\Andy\Rust\aero) error[E0587]: type has conflicting packed and align representation hints --> src\gdt.rs:34:1 | 34 | / struct GDT { 35 | | kernel_null: GDTEntry, 36 | | kernel_code: GDTEntry, 37 | | kernel_data: GDTEntry, ... | 40 | | user_data: GDTEntry, 41 | | } | |_^ error: internal compiler error: compiler\rustc_middle\src\ty\layout.rs:283:13: struct cannot be packed and aligned thread 'rustc' panicked at 'Box', compiler\rustc_errors\src\lib.rs:958:9 stack backtrace: 0: 0x7ff8a16bbcf5 - ::fmt::h17841712fc816a42 1: 0x7ff8a16e84fb - core::fmt::write::ha3ea633b18d2da75 2: 0x7ff8a16ad5dd - ::fmt::h9df41d37d9186c48 3: 0x7ff8a16bfecd - std::panicking::take_hook::h78f8a454f3a4df82 4: 0x7ff8a16bf9d3 - std::panicking::take_hook::h78f8a454f3a4df82 5: 0x7ff8a1c72267 - rustc_driver::report_ice::h1d5c5221edf9c73d 6: 0x7ff8a16c09af - std::panicking::rust_panic_with_hook::h9fd6ff93f12148ea 7: 0x7ff8a6434cc0 - ::fmt::h7af67de8d2c3b61b 8: 0x7ff8a6434c49 - ::fmt::h7af67de8d2c3b61b 9: 0x7ff8a6434c61 - ::fmt::h7af67de8d2c3b61b 10: 0x7ff8a642c3cb - rustc_errors::HandlerInner::err_count::h50c6ffc480963ac8 11: 0x7ff8a642a532 - rustc_errors::Handler::bug::hb269e1fa812030f1 12: 0x7ff8a5f72f5f - rustc_middle::util::bug::bug_fmt::ha0061901ad88904d 13: 0x7ff8a5f6c260 - rustc_middle::ty::context::TyCtxt::article_and_description::h7f14ebacbcec16d1 14: 0x7ff8a5f6c203 - rustc_middle::ty::context::TyCtxt::article_and_description::h7f14ebacbcec16d1 15: 0x7ff8a5f72e89 - rustc_middle::util::bug::bug_fmt::ha0061901ad88904d 16: 0x7ff8a5f72df7 - rustc_middle::util::bug::bug_fmt::ha0061901ad88904d 17: 0x7ff8a6193605 - rustc_middle::ty::layout::provide::ha2084c0ba82f554f 18: 0x7ff8a6198f58 - rustc_middle::ty::layout::provide::ha2084c0ba82f554f 19: 0x7ff8a61910ce - ::fmt::h34fee29140973e21 20: 0x7ff8a61aa2ce - rustc_middle::ty::print::pretty::::fmt::he09b2b8aabe4d576 21: 0x7ff8a615393f - rustc_middle::dep_graph::::debug_node::h067a466f347afbfa 22: 0x7ff8a60a3b9d - ZN12rustc_middle2ty111_DERIVE_rustc_data_structures_stable_hasher_HashStable_rustc_middle_ich_StableHashingContext_ctx_FOR_SymbolName159_$LT$impl$u20$rustc_data_structures..stable_hasher..HashStable$LT$rustc_middle..ich..hcx..StableHashingContext$GT$$u20$ 23: 0x7ff8a5f9f759 - ::fmt::h49093761cb58c0fe 24: 0x7ff8a61cffcf - rustc_middle::ty::context::tls::TLV::__getit::h11706421b91a7f31 25: 0x7ff8a61a0ae5 - as rustc_target::abi::LayoutOf>::layout_of::hf8e84e4cfee5d9c9 26: 0x7ff8a604976f - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 27: 0x7ff8a5dfcd3a - ::visit_lifetime::hd4dc4e580c3cd603 28: 0x7ff8a6049c14 - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 29: 0x7ff8a5deee98 - ::visit_lifetime::hd4dc4e580c3cd603 30: 0x7ff8a61946d7 - rustc_middle::ty::layout::provide::ha2084c0ba82f554f 31: 0x7ff8a61910ce - ::fmt::h34fee29140973e21 32: 0x7ff8a61aa2ce - rustc_middle::ty::print::pretty::::fmt::he09b2b8aabe4d576 33: 0x7ff8a615393f - rustc_middle::dep_graph::::debug_node::h067a466f347afbfa 34: 0x7ff8a60a3b9d - ZN12rustc_middle2ty111_DERIVE_rustc_data_structures_stable_hasher_HashStable_rustc_middle_ich_StableHashingContext_ctx_FOR_SymbolName159_$LT$impl$u20$rustc_data_structures..stable_hasher..HashStable$LT$rustc_middle..ich..hcx..StableHashingContext$GT$$u20$ 35: 0x7ff8a5f9f759 - ::fmt::h49093761cb58c0fe 36: 0x7ff8a61cffcf - rustc_middle::ty::context::tls::TLV::__getit::h11706421b91a7f31 37: 0x7ff8a61a0ae5 - as rustc_target::abi::LayoutOf>::layout_of::hf8e84e4cfee5d9c9 38: 0x7ff8a604976f - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 39: 0x7ff8a5dfcd3a - ::visit_lifetime::hd4dc4e580c3cd603 40: 0x7ff8a6049c14 - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 41: 0x7ff8a5deedfe - ::visit_lifetime::hd4dc4e580c3cd603 42: 0x7ff8a61946d7 - rustc_middle::ty::layout::provide::ha2084c0ba82f554f 43: 0x7ff8a61910ce - ::fmt::h34fee29140973e21 44: 0x7ff8a61aa2ce - rustc_middle::ty::print::pretty::::fmt::he09b2b8aabe4d576 45: 0x7ff8a615393f - rustc_middle::dep_graph::::debug_node::h067a466f347afbfa 46: 0x7ff8a60a3b9d - ZN12rustc_middle2ty111_DERIVE_rustc_data_structures_stable_hasher_HashStable_rustc_middle_ich_StableHashingContext_ctx_FOR_SymbolName159_$LT$impl$u20$rustc_data_structures..stable_hasher..HashStable$LT$rustc_middle..ich..hcx..StableHashingContext$GT$$u20$ 47: 0x7ff8a5f9f759 - ::fmt::h49093761cb58c0fe 48: 0x7ff8a61cffcf - rustc_middle::ty::context::tls::TLV::__getit::h11706421b91a7f31 49: 0x7ff8a61a0ae5 - as rustc_target::abi::LayoutOf>::layout_of::hf8e84e4cfee5d9c9 50: 0x7ff8a604976f - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 51: 0x7ff8a5dfcdbc - ::visit_lifetime::hd4dc4e580c3cd603 52: 0x7ff8a6049c14 - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 53: 0x7ff8a5deedfe - ::visit_lifetime::hd4dc4e580c3cd603 54: 0x7ff8a61946d7 - rustc_middle::ty::layout::provide::ha2084c0ba82f554f 55: 0x7ff8a61910ce - ::fmt::h34fee29140973e21 56: 0x7ff8a61aa2ce - rustc_middle::ty::print::pretty::::fmt::he09b2b8aabe4d576 57: 0x7ff8a615393f - rustc_middle::dep_graph::::debug_node::h067a466f347afbfa 58: 0x7ff8a60a3b9d - ZN12rustc_middle2ty111_DERIVE_rustc_data_structures_stable_hasher_HashStable_rustc_middle_ich_StableHashingContext_ctx_FOR_SymbolName159_$LT$impl$u20$rustc_data_structures..stable_hasher..HashStable$LT$rustc_middle..ich..hcx..StableHashingContext$GT$$u20$ 59: 0x7ff8a5f9f759 - ::fmt::h49093761cb58c0fe 60: 0x7ff8a61cffcf - rustc_middle::ty::context::tls::TLV::__getit::h11706421b91a7f31 61: 0x7ff8a61a0ae5 - as rustc_target::abi::LayoutOf>::layout_of::hf8e84e4cfee5d9c9 62: 0x7ff8a604976f - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 63: 0x7ff8a5dfcd3a - ::visit_lifetime::hd4dc4e580c3cd603 64: 0x7ff8a6049c14 - rustc_middle::ty::structural_impls::::lift_to_tcx::h4b1c274e9c27b41d 65: 0x7ff8a5deedfe - ::visit_lifetime::hd4dc4e580c3cd603 66: 0x7ff8a61946d7 - rustc_middle::ty::layout::provide::ha2084c0ba82f554f 67: 0x7ff8a61910ce - ::fmt::h34fee29140973e21 68: 0x7ff8a61aa2ce - rustc_middle::ty::print::pretty::::fmt::he09b2b8aabe4d576 69: 0x7ff8a615393f - rustc_middle::dep_graph::::debug_node::h067a466f347afbfa 70: 0x7ff8a60a3b9d - ZN12rustc_middle2ty111_DERIVE_rustc_data_structures_stable_hasher_HashStable_rustc_middle_ich_StableHashingContext_ctx_FOR_SymbolName159_$LT$impl$u20$rustc_data_structures..stable_hasher..HashStable$LT$rustc_middle..ich..hcx..StableHashingContext$GT$$u20$ 71: 0x7ff8a5f9f759 - ::fmt::h49093761cb58c0fe 72: 0x7ff8a61cffcf - rustc_middle::ty::context::tls::TLV::__getit::h11706421b91a7f31 73: 0x7ff8a61a0ae5 - as rustc_target::abi::LayoutOf>::layout_of::hf8e84e4cfee5d9c9 74: 0x7ff8a44471fd - rustc_typeck::check::check::check_wf_new::hf875d581dce6c8aa 75: 0x7ff8a4448182 - rustc_typeck::check::check::check_item_type::h47398e2438eb86aa 76: 0x7ff8a4476b46 - ::fmt::h7504e50660f2fc6e 77: 0x7ff8a445329d - rustc_typeck::check::check::check_item_type::h47398e2438eb86aa 78: 0x7ff8a454350d - ::fmt::h59d2657f5d430788 79: 0x7ff8a44a47c6 - ::fmt::h63fe94f68923706f 80: 0x7ff8a446b90b - ::fmt::h7504e50660f2fc6e 81: 0x7ff8a44f73d4 - ::fmt::h15dabe84aedef1e5 82: 0x7ff8a43dd6f1 - rustc_typeck::check::writeback::::resolve_type_vars_in_body::h4f2ac8a0d3054276 83: 0x7ff8a440948f - rustc_typeck::check::writeback::::resolve_type_vars_in_body::h4f2ac8a0d3054276 84: 0x7ff8a45672f0 - rustc_typeck::check_crate::h5df5a5c81bc68977 85: 0x7ff8a1e7bf6a - rustc_interface::passes::QueryContext::print_stats::h17fbfce938bb87d2 86: 0x7ff8a1ca68cb - rustc_ast::util::parser::prec_let_scrutinee_needs_par::h0da38a96661182e5 87: 0x7ff8a1ca82e6 - rustc_ast::util::parser::prec_let_scrutinee_needs_par::h0da38a96661182e5 88: 0x7ff8a1c3023f - chalk_engine::table::AnswerIndex::increment::h2f98e994e5b69c67 89: 0x7ff8a1c827d1 - ::fmt::hc313d7aa265f4349 90: 0x7ff8a1c2c07c - chalk_engine::table::AnswerIndex::increment::h2f98e994e5b69c67 91: 0x7ff8a1ca9a95 - rustc_ast::util::parser::prec_let_scrutinee_needs_par::h0da38a96661182e5 92: 0x7ff8a1c744e5 - ::fmt::hc313d7aa265f4349 93: 0x7ff8a1cabc0c - rustc_ast::util::parser::prec_let_scrutinee_needs_par::h0da38a96661182e5 94: 0x7ff8a1c76395 - ::fmt::hc313d7aa265f4349 95: 0x7ff8a1c9e8f5 - rustc_ast::util::parser::prec_let_scrutinee_needs_par::h0da38a96661182e5 96: 0x7ff8a1c0d80d - ::fmt::hbce8757813226e4d 97: 0x7ff8a16cffd3 - std::sys::windows::thread::Thread::new::h0de7ecb752bc4a62 98: 0x7ff9231d7034 - BaseThreadInitThunk 99: 0x7ff9241a2651 - RtlUserThreadStart note: 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.51.0-nightly (44e3daf5e 2020-12-31) running on x86_64-pc-windows-msvc note: compiler flags: -Z unstable-options -C panic=abort -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [layout_raw] computing layout of `gdt::GDT` #1 [layout_raw] computing layout of `core::option::Option` #2 [layout_raw] computing layout of `core::cell::UnsafeCell>` #3 [layout_raw] computing layout of `spin::Once` #4 [layout_raw] computing layout of `lazy_static::lazy::Lazy` #5 [check_mod_item_types] checking item types in module `gdt` #6 [analysis] running analysis passes on this crate end of query stack ```

jonas-schievink commented 3 years ago

Can't reproduce, can you provide the full code that's needed to reproduce this?

Andy-Python-Programmer commented 3 years ago

Sure

//! This file contains the source for the GDT (Global Descriptor Table).
//! The GDT contains entries telling the CPU about memory segments.
//!
//! **Notes**: https://wiki.osdev.org/Global_Descriptor_Table

use core::intrinsics::size_of;

use lazy_static::lazy_static;

// TODO: Each GDT for every different arch.
// TODO: https://github.com/rust-lang/rust/issues/83107

/// The GDT Descriptor containing the size of offset of the table.
#[repr(C, packed)]
struct GDTDescriptor {
    /// The size of the table subtracted by 1.
    /// The size of the table is subtracted by 1 as the maximum value
    /// of `size` is 65535, while the GDT can be up to 65536 bytes.
    size: u16,
    /// The linear address of the table.
    offset: u64,
}

impl GDTDescriptor {
    /// Create a new GDT descriptor.
    #[inline]
    pub fn new(size: u16, offset: u64) -> Self {
        Self { size, offset }
    }
}

/// A GDT entry.
#[repr(C)]
struct GDTEntry {
    limit_low: u16,
    base_low: u16,
    base_middle: u8,
    access_byte: u8,
    /// Four bits of the variable is the limit and rest four bits of the
    /// variable are the flags.
    limit_hi_flags: u8,
    base_high: u8,
}

impl GDTEntry {
    /// Create a new GDT entry.
    #[inline]
    fn new(
        limit_low: u16,
        base_low: u16,
        base_middle: u8,
        access_byte: u8,
        limit_hi_flags: u8,
        base_high: u8,
    ) -> Self {
        Self {
            limit_low,
            base_low,
            base_middle,
            access_byte,
            limit_hi_flags,
            base_high,
        }
    }
}

/// The GDT.
#[repr(C, packed, align(0x1000))]
struct GDT {
    /// The kernel null segment: `0x00`.
    kernel_null: GDTEntry,
    /// The kernel code segment: `0x08`.
    kernel_code: GDTEntry,
    /// The kernel data segment: `0x10`.
    kernel_data: GDTEntry,
    /// The user null segment.
    user_null: GDTEntry,
    /// The user code segment.
    user_code: GDTEntry,
    /// The user data segment.
    user_data: GDTEntry,
}

/// Initialize the GDT.
pub fn init() {
    unsafe {
        let gdt_descriptor = GDTDescriptor::new(
            (size_of::<GDT>() - 1) as u16,
            (&GLOBAL_DESCRIPTOR_TABLE as *const _) as u64,
        );
    }
}

lazy_static! {
    /// The GDT (Global Descriptor Table).
    static ref GLOBAL_DESCRIPTOR_TABLE: GDT = GDT {
        kernel_null: GDTEntry::new(0, 0, 0, 0x00, 0x00, 0),
        kernel_code: GDTEntry::new(0, 0, 0, 0x9a, 0xa0, 0),
        kernel_data: GDTEntry::new(0, 0, 0, 0x92, 0xa0, 0),
        user_null: GDTEntry::new(0, 0, 0, 0x00, 0x00, 0),
        user_code: GDTEntry::new(0, 0, 0, 0x9a, 0xa0, 0),
        user_data: GDTEntry::new(0, 0, 0, 0x92, 0xa0, 0)
    };
}
JohnTitor commented 3 years ago

MCVE:

#[repr(packed, align(0x1000))]
pub struct Foo {
    val: u16,
}

static BAR: Foo = Foo {
    val: 0,
};

fn main() {}
apiraino commented 3 years ago

Assigning P-medium as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

@rustbot label -I-prioritize +P-medium

apiraino commented 3 years ago

bisection:

searched nightlies: from nightly-2020-06-01 to nightly-2021-03-17 regressed nightly: nightly-2020-10-27 searched commits: from https://github.com/rust-lang/rust/commit/4760b8fb886a3702ae11bfa7868d495b2675b5ed to https://github.com/rust-lang/rust/commit/fd542592f08ca0d1f7255600115c2eafdf6b5da7 regressed commit: https://github.com/rust-lang/rust/commit/b9a94c919b8cb09c186ff253360df91f223f6ef3

bisected with cargo-bisect-rustc v0.6.0 Host triple: x86_64-unknown-linux-gnu Reproduce with: ```bash cargo bisect-rustc --start=2020-06-01 --regress=ice ```