-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
miri: catch function calls where the argument is caller-invalid / the…
… return value callee-invalid
- Loading branch information
Showing
9 changed files
with
108 additions
and
4 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
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...i/tests/fail/validity/cast_fn_ptr1.stderr → ...ity/cast_fn_ptr_invalid_callee_arg.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
28 changes: 28 additions & 0 deletions
28
src/tools/miri/tests/fail/validity/cast_fn_ptr_invalid_callee_ret.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,28 @@ | ||
#![allow(internal_features)] | ||
#![feature(core_intrinsics, custom_mir)] | ||
|
||
use std::intrinsics::mir::*; | ||
use std::num::NonZeroU32; | ||
use std::ptr; | ||
|
||
// This function supposedly returns a NonZeroU32, but actually returns something invalid in a way that | ||
// never materializes a bad NonZeroU32 value: we take a pointer to the return place and cast the pointer | ||
// type. That way we never get an "invalid value constructed" error inside the function, it can | ||
// only possibly be detected when the return value is passed to the caller. | ||
#[custom_mir(dialect = "runtime", phase = "optimized")] | ||
fn f() -> NonZeroU32 { | ||
mir! { | ||
{ | ||
let tmp = ptr::addr_of_mut!(RET); | ||
let ptr = tmp as *mut u32; | ||
*ptr = 0; | ||
Return() | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZeroU32) }; | ||
// There's a NonZeroU32-to-u32 transmute happening here | ||
f(); //~ERROR: expected something greater or equal to 1 | ||
} |
15 changes: 15 additions & 0 deletions
15
src/tools/miri/tests/fail/validity/cast_fn_ptr_invalid_callee_ret.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: encountered 0, but expected something greater or equal to 1 | ||
--> $DIR/cast_fn_ptr_invalid_callee_ret.rs:LL:CC | ||
| | ||
LL | f(); | ||
| ^^^ constructing invalid value: encountered 0, but expected something greater or equal to 1 | ||
| | ||
= 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 $DIR/cast_fn_ptr_invalid_callee_ret.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
34 changes: 34 additions & 0 deletions
34
src/tools/miri/tests/fail/validity/cast_fn_ptr_invalid_caller_arg.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,34 @@ | ||
#![allow(internal_features)] | ||
#![feature(core_intrinsics, custom_mir)] | ||
|
||
use std::intrinsics::mir::*; | ||
use std::num::NonZeroU32; | ||
use std::ptr; | ||
|
||
fn f(c: u32) { | ||
println!("{c}"); | ||
} | ||
|
||
// Call that function in a bad way, with an invalid NonZeroU32, but without | ||
// ever materializing this as a NonZeroU32 value outside the call itself. | ||
#[custom_mir(dialect = "runtime", phase = "optimized")] | ||
fn call(f: fn(NonZeroU32)) { | ||
mir! { | ||
let _res: (); | ||
{ | ||
let c = 0; | ||
let tmp = ptr::addr_of!(c); | ||
let ptr = tmp as *const NonZeroU32; | ||
// The call site now is a NonZeroU32-to-u32 transmute. | ||
Call(_res = f(*ptr), retblock) //~ERROR: expected something greater or equal to 1 | ||
} | ||
retblock = { | ||
Return() | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let f: fn(NonZeroU32) = unsafe { std::mem::transmute(f as fn(u32)) }; | ||
call(f); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/validity/cast_fn_ptr_invalid_caller_arg.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,20 @@ | ||
error: Undefined Behavior: constructing invalid value: encountered 0, but expected something greater or equal to 1 | ||
--> $DIR/cast_fn_ptr_invalid_caller_arg.rs:LL:CC | ||
| | ||
LL | Call(_res = f(*ptr), retblock) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0, but expected something greater or equal to 1 | ||
| | ||
= 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 `call` at $DIR/cast_fn_ptr_invalid_caller_arg.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/cast_fn_ptr_invalid_caller_arg.rs:LL:CC | ||
| | ||
LL | call(f); | ||
| ^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...i/tests/fail/validity/cast_fn_ptr2.stderr → ...ity/cast_fn_ptr_invalid_caller_ret.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