Skip to content

ICE: builtin derive created an unaligned reference #127678

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
kjughx opened this issue Jul 13, 2024 · 3 comments
Open

ICE: builtin derive created an unaligned reference #127678

kjughx opened this issue Jul 13, 2024 · 3 comments
Labels
A-proc-macros Area: Procedural macros A-repr-packed Area: the naughtiest repr C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kjughx
Copy link

kjughx commented Jul 13, 2024

I'm developing a toy kernel and created a proc-macro for annotation packed structs. Using the macro along with #[derive(Debug)] causes an ice.

Code

src/main.rs

extern crate packed_macro;
use packed_macro::{packed, Packed};

pub trait _Packed_: Sized {}

pub trait FromBytes: _Packed_ {}

#[derive(Debug)]
#[packed]
pub struct Struct {
    x: u32,
}

fn main() {}

proc-macro crate:

#[proc_macro_attribute]
pub fn packed(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as ItemStruct);

    let vis = &input.vis;
    let name = &input.ident;
    let fields = &input.fields;

    let expanded = quote! {
        #[repr(C, packed)]
        #[derive(Packed, Clone, Copy)]
        #vis struct #name #fields
    };

    TokenStream::from(expanded)
}

#[proc_macro_derive(Packed)]
pub fn derive_packed(input: TokenStream) -> TokenStream {
    let name = &parse_macro_input!(input as DeriveInput).ident;

    TokenStream::from(quote! {
        impl crate::_Packed_ for #name {}
    })
}

Meta

rustc --version --verbose:

rustc 1.81.0-nightly (c6727fc9b 2024-07-12)
binary: rustc
commit-hash: c6727fc9b5c64cefa7263486497ee95e529bd0f8
commit-date: 2024-07-12
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Error output

error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:50:21: builtin derive created an unaligned reference
  --> src/main.rs:11:5
   |
8  | #[derive(Debug)]
   |          ----- in this derive macro expansion
...
11 |     x: u32,
   |     ^^^^^^
   |
   = note: this error: internal compiler error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
Backtrace

thread 'rustc' panicked at compiler/rustc_mir_transform/src/check_packed_ref.rs:50:21:
Box<dyn Any>
stack backtrace:
   0:     0x7f05ae7459e5 - std::backtrace_rs::backtrace::libunwind::trace::h90ef53671b8f35e2
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7f05ae7459e5 - std::backtrace_rs::backtrace::trace_unsynchronized::hd3a497571d63ea87
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f05ae7459e5 - std::backtrace::Backtrace::create::h8bff861c2af14d3f
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/backtrace.rs:331:13
   3:     0x7f05ae745935 - std::backtrace::Backtrace::force_capture::h84acf91f68ab21d6
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/backtrace.rs:312:9
   4:     0x7f05b1c0b9ff - std[85c3ca4c170da0bb]::panicking::update_hook::<alloc[d335478de6b4a53f]::boxed::Box<rustc_driver_impl[dc4b00a9cfefcaac]::install_ice_hook::{closure#0}>>::{closure#0}
   5:     0x7f05ae760aff - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hbe29137bf798c82c
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/alloc/src/boxed.rs:2078:9
   6:     0x7f05ae760aff - std::panicking::rust_panic_with_hook::h869b30cecbb157c4
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/panicking.rs:804:13
   7:     0x7f05b1c46151 - std[85c3ca4c170da0bb]::panicking::begin_panic::<rustc_errors[24d2db88e61fdfbe]::ExplicitBug>::{closure#0}
   8:     0x7f05b1c39106 - std[85c3ca4c170da0bb]::sys::backtrace::__rust_end_short_backtrace::<std[85c3ca4c170da0bb]::panicking::begin_panic<rustc_errors[24d2db88e61fdfbe]::ExplicitBug>::{closure#0}, !>
   9:     0x7f05b1c390b6 - std[85c3ca4c170da0bb]::panicking::begin_panic::<rustc_errors[24d2db88e61fdfbe]::ExplicitBug>
  10:     0x7f05b1c4f391 - <rustc_errors[24d2db88e61fdfbe]::diagnostic::BugAbort as rustc_errors[24d2db88e61fdfbe]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  11:     0x7f05b235dd0d - <rustc_errors[24d2db88e61fdfbe]::DiagCtxtHandle>::span_bug::<rustc_span[67819806738ee56d]::span_encoding::Span, alloc[d335478de6b4a53f]::string::String>
  12:     0x7f05b23905d8 - rustc_middle[aeeec62211ae3a75]::util::bug::opt_span_bug_fmt::<rustc_span[67819806738ee56d]::span_encoding::Span>::{closure#0}
  13:     0x7f05b239060a - rustc_middle[aeeec62211ae3a75]::ty::context::tls::with_opt::<rustc_middle[aeeec62211ae3a75]::util::bug::opt_span_bug_fmt<rustc_span[67819806738ee56d]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7f05b237c83b - rustc_middle[aeeec62211ae3a75]::ty::context::tls::with_context_opt::<rustc_middle[aeeec62211ae3a75]::ty::context::tls::with_opt<rustc_middle[aeeec62211ae3a75]::util::bug::opt_span_bug_fmt<rustc_span[67819806738ee56d]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7f05b237bbb7 - rustc_middle[aeeec62211ae3a75]::util::bug::span_bug_fmt::<rustc_span[67819806738ee56d]::span_encoding::Span>
  16:     0x7f05b323fdba - <rustc_mir_transform[e83149d28cb0f37b]::pass_manager::Lint<rustc_mir_transform[e83149d28cb0f37b]::check_packed_ref::CheckPackedRef> as rustc_middle[aeeec62211ae3a75]::mir::MirPass>::run_pass
  17:     0x7f05b3201cbb - rustc_mir_transform[e83149d28cb0f37b]::pass_manager::run_passes_inner
  18:     0x7f05b36d526c - rustc_query_impl[1a70eeac4d27cd99]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1a70eeac4d27cd99]::query_impl::mir_built::dynamic_query::{closure#2}::{closure#0}, rustc_middle[aeeec62211ae3a75]::query::erase::Erased<[u8; 8usize]>>
  19:     0x7f05b358c038 - rustc_query_system[a1535082eb398a6e]::query::plumbing::try_execute_query::<rustc_query_impl[1a70eeac4d27cd99]::DynamicConfig<rustc_query_system[a1535082eb398a6e]::query::caches::VecCache<rustc_span[67819806738ee56d]::def_id::LocalDefId, rustc_middle[aeeec62211ae3a75]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[1a70eeac4d27cd99]::plumbing::QueryCtxt, true>
  20:     0x7f05b359579c - rustc_query_impl[1a70eeac4d27cd99]::query_impl::mir_built::get_query_incr::__rust_end_short_backtrace
  21:     0x7f05b36ce63e - rustc_mir_build[ef33ca6215fc9157]::check_unsafety::check_unsafety
  22:     0x7f05b36ce3dd - rustc_query_impl[1a70eeac4d27cd99]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1a70eeac4d27cd99]::query_impl::check_unsafety::dynamic_query::{closure#2}::{closure#0}, rustc_middle[aeeec62211ae3a75]::query::erase::Erased<[u8; 0usize]>>
  23:     0x7f05b359362e - rustc_query_system[a1535082eb398a6e]::query::plumbing::try_execute_query::<rustc_query_impl[1a70eeac4d27cd99]::DynamicConfig<rustc_query_system[a1535082eb398a6e]::query::caches::VecCache<rustc_span[67819806738ee56d]::def_id::LocalDefId, rustc_middle[aeeec62211ae3a75]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[1a70eeac4d27cd99]::plumbing::QueryCtxt, true>
  24:     0x7f05b3593141 - rustc_query_impl[1a70eeac4d27cd99]::query_impl::check_unsafety::get_query_incr::__rust_end_short_backtrace
  25:     0x7f05b357cdb8 - rustc_interface[a6452cf60fc426fe]::passes::analysis
  26:     0x7f05b357bf07 - rustc_query_impl[1a70eeac4d27cd99]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1a70eeac4d27cd99]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[aeeec62211ae3a75]::query::erase::Erased<[u8; 1usize]>>
  27:     0x7f05b40ac804 - rustc_query_system[a1535082eb398a6e]::query::plumbing::try_execute_query::<rustc_query_impl[1a70eeac4d27cd99]::DynamicConfig<rustc_query_system[a1535082eb398a6e]::query::caches::SingleCache<rustc_middle[aeeec62211ae3a75]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[1a70eeac4d27cd99]::plumbing::QueryCtxt, true>
  28:     0x7f05b40ac4ba - rustc_query_impl[1a70eeac4d27cd99]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  29:     0x7f05b3fc2745 - rustc_interface[a6452cf60fc426fe]::interface::run_compiler::<core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>, rustc_driver_impl[dc4b00a9cfefcaac]::run_compiler::{closure#0}>::{closure#1}
  30:     0x7f05b3f8f589 - std[85c3ca4c170da0bb]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[a6452cf60fc426fe]::util::run_in_thread_with_globals<rustc_interface[a6452cf60fc426fe]::util::run_in_thread_pool_with_globals<rustc_interface[a6452cf60fc426fe]::interface::run_compiler<core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>, rustc_driver_impl[dc4b00a9cfefcaac]::run_compiler::{closure#0}>::{closure#1}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>
  31:     0x7f05b3f8f33a - <<std[85c3ca4c170da0bb]::thread::Builder>::spawn_unchecked_<rustc_interface[a6452cf60fc426fe]::util::run_in_thread_with_globals<rustc_interface[a6452cf60fc426fe]::util::run_in_thread_pool_with_globals<rustc_interface[a6452cf60fc426fe]::interface::run_compiler<core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>, rustc_driver_impl[dc4b00a9cfefcaac]::run_compiler::{closure#0}>::{closure#1}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#2} as core[2213ecb858fb81d5]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x7f05ae76a95b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h7aa527871fe5008e
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/alloc/src/boxed.rs:2064:9
  33:     0x7f05ae76a95b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h661f413d6ffad84b
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/alloc/src/boxed.rs:2064:9
  34:     0x7f05ae76a95b - std::sys::pal::unix::thread::Thread::new::thread_start::h77623da8b368e6ce
                               at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/sys/pal/unix/thread.rs:108:17
  35:     0x7f05ae53ca42 - start_thread
  36:     0x7f05ae5bc05c - clone3
  37:                0x0 - <unknown>


rustc version: 1.81.0-nightly (c6727fc9b 2024-07-12)
platform: x86_64-unknown-linux-gnu

query stack during panic:
#0 [mir_built] building MIR for `<impl at src/main.rs:8:10: 8:15>::fmt`
#1 [check_unsafety] unsafety-checking `<impl at src/main.rs:8:10: 8:15>::fmt`
#2 [analysis] running analysis passes on this crate
end of query stack

@kjughx kjughx added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 13, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 13, 2024
@kjughx
Copy link
Author

kjughx commented Jul 13, 2024

The error disappears when moving the order of the macros, i.e. this works fine:

#[packed]
#[derive(Debug)]
pub struct Struct {
    x: u32,
}

@petrochenkov
Copy link
Contributor

This is similar to #120873.

@saethlin saethlin added A-proc-macros Area: Procedural macros and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 13, 2024
@GrigorenkoPV
Copy link
Contributor

Regression in nightly-2023-01-31 (e972bc8...001a77f)
If you change #[derive(Debug)] to #[derive(Copy, Debug)], then it regresses only a year later, in #120847.

@workingjubilee workingjubilee added the A-repr-packed Area: the naughtiest repr label Nov 1, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-proc-macros Area: Procedural macros A-repr-packed Area: the naughtiest repr C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants