Skip to content

Rustc panics(in some cases) when repr(aligned) struct is used in struct with repr(packed) attribute #73112

Closed
@inseo-oh

Description

@inseo-oh

I was writing some code that does some x86 page table configuration, but then I noticed that Rust compiler started to panic with this code:

extern crate x86_64;

fn main() {
    use x86_64::structures::paging::page_table::PageTable;

    #[repr(C, packed)]
    struct SomeStruct {
        page_table: PageTable,
    }
}

And I did realize that I cannot use packed in SomeStruct since PageTable has align(4096) attribute. If I rewrite the above code without x86_64 crate:

fn main() {
    //use x86_64::structures::paging::page_table::PageTable;

    #[repr(transparent)]
    struct PageTableEntry {
        entry: u64,
    }

    #[repr(align(4096))]
    #[repr(C)]
    struct PageTable {
        entries: [PageTableEntry; 512],
    }

    #[repr(C, packed)]
    struct SomeStruct {
        page_table: PageTable,
    }
}

(Note that this is basically same thing as x86_64's PageTable struct)

This compiler error is displayed, which is normal:

   Compiling tester v0.1.0 (/home/pi/dandelion/tester)
error[E0588]: packed type cannot transitively contain a `#[repr(align)]` type
  --> src/main.rs:17:5
   |
17 | /     struct SomeStruct {
18 | |         page_table: PageTable,
19 | |     }
   | |_____^
   |
note: `PageTable` has a `#[repr(align)]` attribute
  --> src/main.rs:12:5
   |
12 | /     struct PageTable {
13 | |         entries: [PageTableEntry; 512],
14 | |     }
   | |_____^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0588`.
error: could not compile `tester`.

To learn more, run the command again with --verbose.

And here's output from RUST_BACKTRACE=1 cargo build --verbose(In this case, I'm trying to compile the first code):

       Fresh bit_field v0.9.0
       Fresh bitflags v1.2.1
       Fresh x86_64 v0.11.0
   Compiling tester v0.1.0 (/home/pi/dandelion/tester)
     Running `rustc --crate-name tester --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=3bcfa379acd4179e -C extra-filename=-3bcfa379acd4179e --out-dir /home/pi/dandelion/tester/target/debug/deps -C incremental=/home/pi/dandelion/tester/target/debug/incremental -L dependency=/home/pi/dandelion/tester/target/debug/deps --extern x86_64=/home/pi/dandelion/tester/target/debug/deps/libx86_64-a59195eee3b7016f.rlib`
thread 'rustc' panicked at 'DefId::expect_local: `DefId(15:482 ~ x86_64[835c]::structures[0]::paging[0]::page_table[0]::PageTable[0])` isn't local', /rustc/0262de554b4c4c5af346137bbb1664a3f6cf4df2/src/libstd/macros.rs:16:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1076
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1537
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:218
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:481
  12: rust_begin_unwind
             at src/libstd/panicking.rs:385
  13: std::panicking::begin_panic_fmt
             at src/libstd/panicking.rs:339
  14: rustc_span::def_id::DefId::expect_local::{{closure}}
  15: rustc_typeck::check::check_packed
  16: rustc_typeck::check::check_item_type
  17: rustc_middle::hir::map::Map::visit_item_likes_in_module
  18: rustc_typeck::check::check_mod_item_types
  19: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::check_mod_item_types>::compute
  20: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  21: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  22: rustc_data_structures::stack::ensure_sufficient_stack
  23: rustc_query_system::query::plumbing::get_query_impl
  24: rustc_query_system::query::plumbing::ensure_query_impl
  25: rustc_typeck::check_crate
  26: rustc_interface::passes::analysis
  27: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  28: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  29: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  30: rustc_query_system::query::plumbing::get_query_impl
  31: rustc_middle::ty::context::tls::enter_global
  32: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  33: rustc_span::with_source_map
  34: rustc_interface::interface::run_compiler_in_existing_thread_pool
  35: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.46.0-nightly (0262de554 2020-06-07) running on armv7-unknown-linux-gnueabihf

note: compiler flags: -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 [check_mod_item_types] checking item types in top-level module
#1 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `tester`.

Caused by:
  process didn't exit successfully: `rustc --crate-name tester --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=3bcfa379acd4179e -C extra-filename=-3bcfa379acd4179e --out-dir /home/pi/dandelion/tester/target/debug/deps -C incremental=/home/pi/dandelion/tester/target/debug/incremental -L dependency=/home/pi/dandelion/tester/target/debug/deps --extern x86_64=/home/pi/dandelion/tester/target/debug/deps/libx86_64-a59195eee3b7016f.rlib` (exit code: 101)

Looking at the backtrace, there's check_packed function and expect_local:

  14: rustc_span::def_id::DefId::expect_local::{{closure}}
  15: rustc_typeck::check::check_packed

While there's no line number(since the compiler is installed using rustup), thankfully there are only two calls to expect_local inside check_packed. And it seems like the first one is panicking since normal output has nothing more than note: `PageTable` has a `#[repr(align)]` attribute, but I might be wrong.

let hir_id = hir.as_local_hir_id(def_spans[0].0.expect_local());

I ran rustup update about five minutes ago before submitting this issue, but result was the same.

BTW: I initially found this bug in Clippy, but then I noticed that Rustc was doing the same thing.

Version

pi@miz:~/dandelion/tester $ rustc --version --verbose
rustc 1.46.0-nightly (0262de554 2020-06-07)
binary: rustc
commit-hash: 0262de554b4c4c5af346137bbb1664a3f6cf4df2
commit-date: 2020-06-07
host: armv7-unknown-linux-gnueabihf
release: 1.46.0-nightly
LLVM version: 10.0

Metadata

Metadata

Assignees

Labels

A-type-systemArea: Type systemC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions