Skip to content

Commit 10244b5

Browse files
authored
Merge pull request #477 from RalfJung/miri-validate
Validate more things
2 parents 26f9d61 + c9cf034 commit 10244b5

24 files changed

+67
-56
lines changed

src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,26 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
255255

256256
const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::MutStatic);
257257

258-
#[inline(always)]
259258
fn enforce_validity(ecx: &EvalContext<'a, 'mir, 'tcx, Self>) -> bool {
260-
ecx.machine.validate
259+
if !ecx.machine.validate {
260+
return false;
261+
}
262+
263+
// Some functions are whitelisted until we figure out how to fix them.
264+
// We walk up the stack a few frames to also cover their callees.
265+
const WHITELIST: &[&str] = &[
266+
// Uses mem::uninitialized
267+
"std::ptr::read",
268+
];
269+
for frame in ecx.stack().iter()
270+
.rev().take(3)
271+
{
272+
let name = frame.instance.to_string();
273+
if WHITELIST.iter().any(|white| name.starts_with(white)) {
274+
return false;
275+
}
276+
}
277+
true
261278
}
262279

263280
/// Returns Ok() when the function was handled, fail otherwise

tests/compile-fail/cast_box_int_to_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Validation makes this fail in the wrong place
2-
// compile-flags: -Zmir-emit-validate=0
2+
// compile-flags: -Zmir-emit-validate=0 -Zmiri-disable-validation
33

44
fn main() {
55
let b = Box::new(42);

tests/compile-fail/cast_int_to_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Validation makes this fail in the wrong place
2-
// compile-flags: -Zmir-emit-validate=0
2+
// compile-flags: -Zmir-emit-validate=0 -Zmiri-disable-validation
33

44
fn main() {
55
let g = unsafe {

tests/compile-fail/execute_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Validation makes this fail in the wrong place
2-
// compile-flags: -Zmir-emit-validate=0
2+
// compile-flags: -Zmir-emit-validate=0 -Zmiri-disable-validation
33

44
#![feature(box_syntax)]
55

tests/compile-fail/fn_ptr_offset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Validation makes this fail in the wrong place
2-
// compile-flags: -Zmir-emit-validate=0
2+
// compile-flags: -Zmir-emit-validate=0 -Zmiri-disable-validation
33

44
use std::mem;
55

tests/compile-fail/invalid_bool.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
//ignore-test FIXME: do some basic validation of invariants for all values in flight
2-
//This does currently not get caught becuase it compiles to SwitchInt, which
3-
//has no knowledge about data invariants.
4-
51
fn main() {
6-
let b = unsafe { std::mem::transmute::<u8, bool>(2) };
7-
if b { unreachable!() } else { unreachable!() } //~ ERROR constant evaluation error
8-
//~^ NOTE invalid boolean value read
2+
let _b = unsafe { std::mem::transmute::<u8, bool>(2) }; //~ ERROR encountered 2, but expected something in the range 0..=1
93
}

tests/compile-fail/invalid_bool2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Validation makes this fail in the wrong place
2+
// compile-flags: -Zmir-emit-validate=0 -Zmiri-disable-validation
3+
14
fn main() {
25
let b = unsafe { std::mem::transmute::<u8, bool>(2) };
36
let _x = b == true; //~ ERROR invalid boolean value read

tests/compile-fail/invalid_char.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
assert!(std::char::from_u32(-1_i32 as u32).is_none());
3+
let _ = match unsafe { std::mem::transmute::<i32, char>(-1) } { //~ ERROR encountered 4294967295, but expected something in the range 0..=1114111
4+
'a' => {true},
5+
'b' => {false},
6+
_ => {true},
7+
};
8+
}

tests/compile-fail/match_char2.rs renamed to tests/compile-fail/invalid_char2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Validation makes this fail in the wrong place
2+
// compile-flags: -Zmir-emit-validate=0 -Zmiri-disable-validation
3+
14
fn main() {
25
assert!(std::char::from_u32(-1_i32 as u32).is_none());
36
let c = unsafe { std::mem::transmute::<i32, char>(-1) };
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
// Validation makes this fail in the wrong place
2-
// compile-flags: -Zmir-emit-validate=0
3-
41
#[repr(C)]
52
pub enum Foo {
63
A, B, C, D
74
}
85

96
fn main() {
10-
let f = unsafe { std::mem::transmute::<i32, Foo>(42) };
11-
match f {
12-
Foo::A => {}, //~ ERROR invalid enum discriminant
13-
Foo::B => {},
14-
Foo::C => {},
15-
Foo::D => {},
16-
}
7+
let _f = unsafe { std::mem::transmute::<i32, Foo>(42) }; //~ ERROR encountered invalid enum discriminant 42
178
}

0 commit comments

Comments
 (0)