-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 11, 2025
1 parent
1543bb4
commit a7c9e79
Showing
6 changed files
with
88 additions
and
56 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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
// 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, | ||
{ | ||
} | ||
} | ||
impl<T, U> Dyn<U> for T {} | ||
|
||
trait HigherRanked {} | ||
impl HigherRanked for for<'a> fn(&'a ()) {} | ||
|
||
// 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 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 | ||
|
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