-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
163 additions
and
35 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
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
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
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
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
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 | ||
} |
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 | ||
|
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); | ||
} |
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
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
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