Skip to content

Commit 069d8fd

Browse files
committed
test for Stacked Borrows error during vtable validation
1 parent ada7b72 commit 069d8fd

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tests/fail/stacked_borrows/vtable.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// error-pattern: vtable pointer does not have permission
2+
#![feature(ptr_metadata)]
3+
4+
trait Foo {}
5+
6+
impl Foo for u32 {}
7+
8+
fn uwu(thin: *const (), meta: &'static ()) -> *const dyn Foo {
9+
core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) })
10+
}
11+
12+
fn main() {
13+
unsafe {
14+
let orig = 1_u32;
15+
let x = &orig as &dyn Foo;
16+
let (ptr, meta) = (x as *const dyn Foo).to_raw_parts();
17+
let _ = uwu(ptr, core::mem::transmute(meta));
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: Undefined Behavior: type validation failed: encountered vtable pointer does not have permission to read drop function pointer
2+
--> RUSTLIB/core/src/ptr/metadata.rs:LL:CC
3+
|
4+
LL | unsafe { PtrRepr { components: PtrComponents { data_address, metadata } }.const_ptr }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered vtable pointer does not have permission to read drop function pointer
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
10+
= note: inside `std::ptr::from_raw_parts::<dyn Foo>` at RUSTLIB/core/src/ptr/metadata.rs:LL:CC
11+
note: inside `uwu` at $DIR/vtable.rs:LL:CC
12+
--> $DIR/vtable.rs:LL:CC
13+
|
14+
LL | core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) })
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
note: inside `main` at $DIR/vtable.rs:LL:CC
17+
--> $DIR/vtable.rs:LL:CC
18+
|
19+
LL | let _ = uwu(ptr, core::mem::transmute(meta));
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
23+
24+
error: aborting due to previous error
25+

0 commit comments

Comments
 (0)