Skip to content

add test for sketchy vtable #1113

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

Merged
merged 2 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e862c01aadb2d029864f7bb256cf6c85bbb5d7e4
12307b3b08edee543a78fb9d4a837fbd6d6ac0fa
46 changes: 46 additions & 0 deletions tests/compile-fail/issue-miri-1112.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
trait Empty {}

#[repr(transparent)]
pub struct FunnyPointer(dyn Empty);

#[repr(C)]
pub struct Meta {
drop_fn: fn(&mut ()),
size: usize,
align: usize,
}

impl Meta {
pub fn new() -> Self {
Meta {
drop_fn: |_| {},
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); //~ ERROR invalid drop fn in vtable
&*obj
}
}

fn main() {
unsafe {
let meta = Meta::new();
let hello = "hello".to_string();
let _raw: &FunnyPointer = FunnyPointer::from_data_ptr(&hello, &meta as *const _);
}
}