Skip to content

Commit ec51594

Browse files
committed
Auto merge of #2046 - RalfJung:very-strict, r=RalfJung
make strict-provenance imply check-number-validity I feel like Miri not catching [this example](rust-lang/unsafe-code-guidelines#286 (comment)) with strict provenance checking enabled is surprising. OTOH, Miri suddenly complaining about uninit data in integers with `-Zmiri-strict-provenance` also might be surprising. Which one is more surprising? I don't know. We *could* go out of our way and have a mode where uninit integers are okay but provenance is not, but I am not sure if that is truly worth it. It'd be quite annoying to implement.
2 parents fc2165d + 3dcba56 commit ec51594

File tree

11 files changed

+38
-9
lines changed

11 files changed

+38
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ environment variable:
297297
* `-Zmiri-strict-provenance` enables [strict
298298
provenance](https://github.com/rust-lang/rust/issues/95228) checking in Miri. This means that
299299
casting an integer to a pointer yields a result with 'invalid' provenance, i.e., with provenance
300-
that cannot be used for any memory access. Also implies `-Zmiri-tag-raw-pointers`.
300+
that cannot be used for any memory access. Also implies `-Zmiri-tag-raw-pointers` and
301+
`-Zmiri-check-number-validity`.
301302
* `-Zmiri-symbolic-alignment-check` makes the alignment check more strict. By
302303
default, alignment is checked by casting the pointer to an integer, and making
303304
sure that is a multiple of the alignment. This can lead to cases where a

src/bin/miri.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ fn main() {
366366
"-Zmiri-strict-provenance" => {
367367
miri_config.strict_provenance = true;
368368
miri_config.tag_raw = true;
369+
miri_config.check_number_validity = true;
369370
}
370371
"-Zmiri-track-raw-pointers" => {
371372
eprintln!(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// compile-flags: -Zmiri-strict-provenance
2+
#![feature(strict_provenance)]
3+
4+
use std::mem;
5+
6+
// This is the example from
7+
// <https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-1085144431>.
8+
9+
unsafe fn deref(left: *const u8, right: *const u8) {
10+
let left_int: usize = mem::transmute(left); //~ERROR expected initialized plain (non-pointer) bytes
11+
let right_int: usize = mem::transmute(right);
12+
if left_int == right_int {
13+
// The compiler is allowed to replace `left_int` by `right_int` here...
14+
let left_ptr: *const u8 = mem::transmute(left_int);
15+
// ...which however means here it could be dereferencing the wrong pointer.
16+
let _val = *left_ptr;
17+
}
18+
}
19+
20+
fn main() {
21+
let ptr1 = &0u8 as *const u8;
22+
let ptr2 = &1u8 as *const u8;
23+
unsafe {
24+
// Two pointers with the same address but different provenance.
25+
deref(ptr1, ptr2.with_addr(ptr1.addr()));
26+
}
27+
}

tests/run-pass/btreemap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
1+
// compile-flags: -Zmiri-strict-provenance
22
#![feature(btree_drain_filter)]
33
use std::collections::{BTreeMap, BTreeSet};
44
use std::mem;

tests/run-pass/concurrency/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ignore-windows: Concurrency on Windows is not supported yet.
2-
// compile-flags: -Zmiri-disable-isolation -Zmiri-strict-provenance -Zmiri-check-number-validity
2+
// compile-flags: -Zmiri-disable-isolation -Zmiri-strict-provenance
33

44
use std::sync::{Arc, Barrier, Condvar, Mutex, Once, RwLock};
55
use std::thread;

tests/run-pass/concurrency/thread_locals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ignore-windows: Concurrency on Windows is not supported yet.
2-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
2+
// compile-flags: -Zmiri-strict-provenance
33

44
//! The main purpose of this test is to check that if we take a pointer to
55
//! thread's `t1` thread-local `A` and send it to another thread `t2`,

tests/run-pass/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
1+
// compile-flags: -Zmiri-strict-provenance
22
#![feature(new_uninit)]
33
#![feature(get_mut_unchecked)]
44

tests/run-pass/slices.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
1+
// compile-flags: -Zmiri-strict-provenance
22
#![feature(new_uninit)]
33
#![feature(slice_as_chunks)]
44
#![feature(slice_partition_dedup)]

tests/run-pass/strings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
1+
// compile-flags: -Zmiri-strict-provenance
22

33
fn empty() -> &'static str {
44
""

tests/run-pass/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
1+
// compile-flags: -Zmiri-strict-provenance
22
// Gather all references from a mutable iterator and make sure Miri notices if
33
// using them is dangerous.
44
fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {

tests/run-pass/vecdeque.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmiri-strict-provenance -Zmiri-check-number-validity
1+
// compile-flags: -Zmiri-strict-provenance
22
use std::collections::VecDeque;
33

44
fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {

0 commit comments

Comments
 (0)