-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interpret: adjust vtable validity check for higher-ranked types
- Loading branch information
Lukas Markeffsky
committed
Jan 10, 2025
1 parent
6afee11
commit a7d6eae
Showing
6 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/tools/miri/tests/fail/validity/dyn-trait-leak-check.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Test that transmuting from `&dyn Dyn<fn(&'static ())>` to `&dyn Dyn<for<'a> fn(&'a ())>` is UB. | ||
// | ||
// The vtable of `() as Dyn<fn(&'static ())>` and `() as Dyn<for<'a> fn(&'a ())>` can have | ||
// different entries and, because in the former the entry for `foo` is vacant, this test will | ||
// segfault at runtime. | ||
|
||
trait Dyn<U> { | ||
fn foo(&self) | ||
where | ||
U: HigherRanked, | ||
{ | ||
U::call() | ||
} | ||
} | ||
impl<T, U> Dyn<U> for T {} | ||
|
||
trait HigherRanked { | ||
fn call(); | ||
} | ||
impl HigherRanked for for<'a> fn(&'a ()) { | ||
fn call() { | ||
println!("higher ranked"); | ||
} | ||
} | ||
|
||
// 2nd candidate is required so that selecting `(): Dyn<fn(&'static ())>` will | ||
// evaluate the candidates and fail the leak check instead of returning the | ||
// only applicable candidate. | ||
trait Unsatisfied {} | ||
impl<T: Unsatisfied> HigherRanked for T { | ||
fn call() { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
fn main() { | ||
let x: &dyn Dyn<fn(&'static ())> = &(); | ||
let y: &dyn Dyn<for<'a> fn(&'a ())> = unsafe { std::mem::transmute(x) }; | ||
//~^ ERROR: wrong trait in wide pointer vtable | ||
y.foo(); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/tools/miri/tests/fail/validity/dyn-trait-leak-check.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: Undefined Behavior: constructing invalid value: wrong trait in wide pointer vtable: expected `Dyn<for<'a> fn(&'a ())>`, but encountered `Dyn<fn(&())>` | ||
--> tests/fail/validity/dyn-trait-leak-check.rs:LL:CC | ||
| | ||
LL | let y: &dyn Dyn<for<'a> fn(&'a ())> = unsafe { std::mem::transmute(x) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: wrong trait in wide pointer vtable: expected `Dyn<for<'a> fn(&'a ())>`, but encountered `Dyn<fn(&())>` | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `main` at tests/fail/validity/dyn-trait-leak-check.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
26 changes: 26 additions & 0 deletions
26
src/tools/miri/tests/pass/validity/dyn-trait-bivariant-transmutes.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Test that transmuting between subtypes of dyn traits is fine, even in the | ||
// "wrong direction", i.e. going from a lower-ranked to a higher-ranked dyn trait. | ||
|
||
trait Dyn<U: ?Sized> {} | ||
impl<T, U: ?Sized> Dyn<U> for T {} | ||
|
||
struct Wrapper<T: ?Sized>(T); | ||
|
||
fn main() { | ||
let x: &dyn Dyn<fn(&'static ())> = &(); | ||
let _y: &dyn for<'a> Dyn<fn(&'a ())> = unsafe { std::mem::transmute(x) }; | ||
|
||
let x: &dyn for<'a> Dyn<fn(&'a ())> = &(); | ||
let _y: &dyn Dyn<fn(&'static ())> = unsafe { std::mem::transmute(x) }; | ||
|
||
let x: &dyn Dyn<dyn Dyn<fn(&'static ())>> = &(); | ||
let _y: &dyn for<'a> Dyn<dyn Dyn<fn(&'a ())>> = unsafe { std::mem::transmute(x) }; | ||
|
||
let x: &dyn for<'a> Dyn<dyn Dyn<fn(&'a ())>> = &(); | ||
let _y: &dyn Dyn<dyn Dyn<fn(&'static ())>> = unsafe { std::mem::transmute(x) }; | ||
|
||
// This lowers to a ptr-to-ptr cast (which behaves like a transmute) | ||
// and not an unsizing coercion: | ||
let x: *const dyn for<'a> Dyn<&'a ()> = &(); | ||
let _y: *const Wrapper<dyn Dyn<&'static ()>> = x as _; | ||
} |