-
Notifications
You must be signed in to change notification settings - Fork 361
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
Miri panics on sketchy code #1112
Comments
Changing |
My best guess is that this panics here: But that should only panic if your drop-fn has a non-ptr argument... but a reference is a ptr. |
The ICE will be fixed by rust-lang/rust#67254. But the code will be considered UB then, because the drop fn is a closure and not a "normal" function. Generally, code that makes any assumptions about vtable layout has unspecified behavior at best -- vtable layout is not fixed and can change any time. |
Thanks! Yeah, what I'm experimenting here is unspecified at best. Without custom DSTs, I'm trying to see how much I can get away with by stuffing my extra metadata at the end of an "empty" VTable (VTable of trait object without any functions). 😅 "Custom" references in Rust via structs (like So here I am, abusing trait objects. Hoping that one day once we maybe get custom DSTs, that won't be needed. Getting |
Well, here's the variant that doesn't make Miri bark: trait Empty {}
#[repr(transparent)]
pub struct FunnyPointer(dyn Empty);
#[repr(C)]
pub struct Meta {
drop_fn: fn(*mut ()),
size: usize,
align: usize,
}
fn nop(_x: *mut ()) {}
impl Meta {
pub fn new() -> Self {
Meta {
drop_fn: nop,
size: 0,
align: 1,
}
}
}
#[repr(C)]
pub struct FatPointer {
pub data: *const (),
pub vtable: *const (),
}
impl FunnyPointer {
pub unsafe fn from_data_ptr(data: &String, ptr: *const Meta) -> &Self {
let obj = FatPointer {
data: data as *const _ as *const (),
vtable: ptr as *const _ as *const (),
};
let obj = std::mem::transmute::<FatPointer, *mut FunnyPointer>(obj);
&*obj
}
}
fn main() {
unsafe {
let meta = Meta::new();
let hello = "hello".to_string();
let _raw: &FunnyPointer = FunnyPointer::from_data_ptr(&hello, &meta as *const _);
}
} |
I would accept a PR against rustc that makes closures work there. There's no fundamental reason why they don't work -- the actual function call logic supports them property, but some of our kind-of hacky vtable management (where the panic occurs) does not. But I am not sure if that's worth it. ;) The ICE is still definitely a bug, though. |
cargo +nightly miri
crashes on the following sketchy code:Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d2a12549c324df2b61cd5c118abd0789
Backtrace:
The text was updated successfully, but these errors were encountered: