Skip to content

Commit f0d4387

Browse files
committed
add test for sketchy vtable
1 parent 4ebc030 commit f0d4387

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/compile-fail/issue-miri-1112.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
trait Empty {}
2+
3+
#[repr(transparent)]
4+
pub struct FunnyPointer(dyn Empty);
5+
6+
#[repr(C)]
7+
pub struct Meta {
8+
drop_fn: fn(&mut ()),
9+
size: usize,
10+
align: usize,
11+
}
12+
13+
impl Meta {
14+
pub fn new() -> Self {
15+
Meta {
16+
drop_fn: |_| {},
17+
size: 0,
18+
align: 1,
19+
}
20+
}
21+
}
22+
23+
#[repr(C)]
24+
pub struct FatPointer {
25+
pub data: *const (),
26+
pub vtable: *const (),
27+
}
28+
29+
impl FunnyPointer {
30+
pub unsafe fn from_data_ptr(data: &String, ptr: *const Meta) -> &Self {
31+
let obj = FatPointer {
32+
data: data as *const _ as *const (),
33+
vtable: ptr as *const _ as *const (),
34+
};
35+
let obj = std::mem::transmute::<FatPointer, *mut FunnyPointer>(obj); //~ ERROR invalid drop fn in vtable
36+
&*obj
37+
}
38+
}
39+
40+
fn main() {
41+
unsafe {
42+
let meta = Meta::new();
43+
let hello = "hello".to_string();
44+
let _raw: &FunnyPointer = FunnyPointer::from_data_ptr(&hello, &meta as *const _);
45+
}
46+
}

0 commit comments

Comments
 (0)