-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interpret: ensure we check bool/char for validity when they are used …
…in a cast
- Loading branch information
Showing
10 changed files
with
159 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Make sure we find these even with many checks disabled. | ||
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation | ||
#![feature(core_intrinsics)] | ||
#![feature(custom_mir)] | ||
|
||
use std::intrinsics::mir::*; | ||
|
||
#[custom_mir(dialect = "runtime", phase = "optimized")] | ||
fn cast(ptr: *const char) -> u32 { | ||
mir! { | ||
{ | ||
RET = *ptr as u32; //~ERROR: interpreting an invalid 32-bit value as a char | ||
Return() | ||
} | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let v = u32::MAX; | ||
cast(&v as *const u32 as *const char); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/validity/invalid_char_cast.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: interpreting an invalid 32-bit value as a char: $HEX | ||
--> $DIR/invalid_char_cast.rs:LL:CC | ||
| | ||
LL | RET = *ptr as u32; | ||
| ^^^^^^^^^^^^^^^^^ interpreting an invalid 32-bit value as a char: $HEX | ||
| | ||
= 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 `cast` at $DIR/invalid_char_cast.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/invalid_char_cast.rs:LL:CC | ||
| | ||
LL | cast(&v as *const u32 as *const char); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Make sure we find these even with many checks disabled. | ||
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation | ||
#![feature(core_intrinsics)] | ||
#![feature(custom_mir)] | ||
|
||
use std::intrinsics::mir::*; | ||
|
||
#[custom_mir(dialect = "runtime", phase = "optimized")] | ||
fn switch_int(ptr: *const char) { | ||
mir! { | ||
{ | ||
match *ptr { //~ERROR: interpreting an invalid 32-bit value as a char | ||
'0' => ret, | ||
_ => ret, | ||
} | ||
} | ||
ret = { | ||
Return() | ||
} | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let v = u32::MAX; | ||
switch_int(&v as *const u32 as *const char); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/tools/miri/tests/fail/validity/invalid_char_match.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,23 @@ | ||
error: Undefined Behavior: interpreting an invalid 32-bit value as a char: $HEX | ||
--> $DIR/invalid_char_match.rs:LL:CC | ||
| | ||
LL | / match *ptr { | ||
LL | | '0' => ret, | ||
LL | | _ => ret, | ||
LL | | } | ||
| |_____________^ interpreting an invalid 32-bit value as a char: $HEX | ||
| | ||
= 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 `switch_int` at $DIR/invalid_char_match.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/invalid_char_match.rs:LL:CC | ||
| | ||
LL | switch_int(&v as *const u32 as *const char); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Make sure we find these even with many checks disabled. | ||
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation | ||
|
||
#[derive(Copy, Clone)] | ||
#[allow(unused)] | ||
enum E {A, B, C } | ||
|
||
fn cast(ptr: *const E) { unsafe { | ||
let _val = *ptr as u32; //~ERROR: enum value has invalid tag | ||
}} | ||
|
||
pub fn main() { | ||
let v = u32::MAX; | ||
cast(&v as *const u32 as *const E); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/validity/invalid_enum_cast.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: enum value has invalid tag: 0xff | ||
--> $DIR/invalid_enum_cast.rs:LL:CC | ||
| | ||
LL | let _val = *ptr as u32; | ||
| ^^^^^^^^^^^ enum value has invalid tag: 0xff | ||
| | ||
= 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 `cast` at $DIR/invalid_enum_cast.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/invalid_enum_cast.rs:LL:CC | ||
| | ||
LL | cast(&v as *const u32 as *const E); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|