-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ICE in unaligned references MIR check #108122
Comments
cc @RalfJung |
ICE originates from: rust/compiler/rustc_mir_transform/src/check_packed_ref.rs Lines 44 to 51 in 96834f0
@nnethercote this is related to derives on packed structs somehow, it seems. An MCVE would be quite useful. |
I think this is the code that causes the ICE, but clearly the Is it possible that Probably needs a hacked compiler that prints some more info in the affected branch. |
// due to compiler wrongfully complaining re: Copy impl missing for packed struct
#![allow(unaligned_references)]
#[macro_use]
extern crate hdf5_derive;
#[derive(H5Type, Copy, Clone)]
#[repr(packed)]
struct P2(i8, u32); reproduces the issue, and removing the Expanded#![feature(prelude_import)]
#![allow(unaligned_references)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
#[macro_use]
extern crate hdf5_derive;
#[repr(packed)]
struct P2(i8, u32);
#[allow(dead_code, unused_variables, unused_attributes)]
const _IMPL_H5TYPE_FOR_P2: () = {
extern crate hdf5 as _h5;
#[automatically_derived]
unsafe impl _h5::types::H5Type for P2 {
#[inline]
fn type_descriptor() -> _h5::types::TypeDescriptor {
let origin: *const P2 = ::std::ptr::null();
let mut fields = <[_]>::into_vec(
#[rustc_box]
::alloc::boxed::Box::new([
_h5::types::CompoundField {
name: "0".to_owned(),
ty: <i8 as _h5::types::H5Type>::type_descriptor(),
offset: unsafe { &((*origin).0) as *const _ as _ },
index: 0,
},
_h5::types::CompoundField {
name: "1".to_owned(),
ty: <u32 as _h5::types::H5Type>::type_descriptor(),
offset: unsafe { &((*origin).1) as *const _ as _ },
index: 0,
},
]),
);
for i in 0..fields.len() {
fields[i].index = i;
}
let size = ::std::mem::size_of::<P2>();
_h5::types::TypeDescriptor::Compound(_h5::types::CompoundType {
fields,
size,
})
}
}
};
#[automatically_derived]
impl ::core::marker::Copy for P2 {}
#[automatically_derived]
impl ::core::clone::Clone for P2 {
#[inline]
fn clone(&self) -> P2 {
let _: ::core::clone::AssertParamIsClone<i8>;
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
} |
That expanded code is definitely UB. let origin: *const P2 = ::std::ptr::null();
// ...
unsafe { &((*origin).0) as *const _ as _ } Here a NULL ptr is being dereferenced. This looks like an incorrect offset_of implementation. offset_of is tricky to implement, that's why we have a crate for it. But still we shouldn't ICE of course. That code above would trigger the "created reference to unaligned field" error. But that doesn't explain why it enters the Looking at the docs and code for /// If the given `DefId` belongs to a trait that was automatically derived, returns `true`.
pub fn is_builtin_derive(self, def_id: DefId) -> bool {
self.has_attr(def_id, sym::automatically_derived)
}
Or is the issue here that Not sure whom to ping for questions like this... @petrochenkov maybe?
Is that from the HDF5 crate? That warning is now a hard error and allowing it was never a good idea. I wonder why they say that the complaint is incorrect. I don't think I have seen this as an issue so we were not aware of this problem. |
Also Cc @aldanor -- this issue affects hdf5. |
Reproducer: #[derive(Copy, Clone)]
#[repr(packed)]
pub struct Packed(i8, u32);
trait Foo {
fn evil(&self);
}
#[automatically_derived]
impl Foo for Packed {
fn evil(&self) {
unsafe {
&self.1;
}
}
}
fn main() {} @rustbot label -E-needs-mcve |
Just FYI that link above will rot in this context: When this stops ICEing I will replace it with the non-ICEing output of the compiler. |
Yeah, So, before #104429, an unaligned reference within a function marked with So, the question of whether user code can use A related question: why does I see several possibilities to address this ICE.
That may be in reference to the old "that does not derive |
Actually, it could be simpler than this. We could just remove the |
👍
Yeah I think that makes most sense. |
@RalfJung thanks for pinging and @clubby789 thanks for fixing! (also, agreed re: UB-like offset-of logic, but it was most likely written before |
https://miri.saethlin.dev/logs/hdf5-derive/0.8.1.html
Meta
Error output
Backtrace
The text was updated successfully, but these errors were encountered: