Skip to content

Commit bf4d4c0

Browse files
committed
Auto merge of rust-lang#3766 - RalfJung:tree-borrows-int2ptr, r=RalfJung
better diagnostics for Tree Borrows + int2ptr casts - Entirely reject `-Zmiri-permissive-provenance -Zmiri-tree-borrows` since that combination just doesn't work - In the int2ptr cast warning, when Tree Borrows is enabled, do not recommend `-Zmiri-permissive-provenance`, instead note that Tree Borrows does not support int2ptr casts Fixes rust-lang/miri#3764
2 parents f98fdfc + 5e1f8e2 commit bf4d4c0

16 files changed

+201
-138
lines changed

src/tools/miri/src/bin/miri.rs

+8
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,14 @@ fn main() {
620620
"-Zmiri-unique-is-unique only has an effect when -Zmiri-tree-borrows is also used"
621621
);
622622
}
623+
// Tree Borrows + permissive provenance does not work.
624+
if miri_config.provenance_mode == ProvenanceMode::Permissive
625+
&& matches!(miri_config.borrow_tracker, Some(BorrowTrackerMethod::TreeBorrows))
626+
{
627+
show_error!(
628+
"Tree Borrows does not support integer-to-pointer casts, and is hence not compatible with permissive provenance"
629+
);
630+
}
623631

624632
debug!("rustc arguments: {:?}", rustc_args);
625633
debug!("crate arguments: {:?}", miri_config.args);

src/tools/miri/src/borrow_tracker/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ impl GlobalStateInner {
232232
pub fn remove_unreachable_allocs(&mut self, allocs: &LiveAllocs<'_, '_>) {
233233
self.root_ptr_tags.retain(|id, _| allocs.is_live(*id));
234234
}
235+
236+
pub fn borrow_tracker_method(&self) -> BorrowTrackerMethod {
237+
self.borrow_tracker_method
238+
}
235239
}
236240

237241
/// Which borrow tracking method to use

src/tools/miri/src/diagnostics.rs

+69-73
Large diffs are not rendered by default.

src/tools/miri/tests/pass/adjacent-allocs.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@revisions: stack tree
2-
//@[tree]compile-flags: -Zmiri-tree-borrows
31
//@compile-flags: -Zmiri-permissive-provenance
42

53
fn ensure_allocs_can_be_adjacent() {

src/tools/miri/tests/pass/box.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//@revisions: stack tree
2-
//@[tree]compile-flags: -Zmiri-tree-borrows -Zmiri-permissive-provenance
1+
//@compile-flags: -Zmiri-permissive-provenance
32
#![feature(ptr_internals)]
43

54
fn main() {

src/tools/miri/tests/pass/box.stack.stderr

-33
This file was deleted.

src/tools/miri/tests/pass/box.tree.stdout

-3
This file was deleted.
+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//@revisions: stack tree
2-
//@[tree]compile-flags: -Zmiri-tree-borrows -Zmiri-permissive-provenance
3-
#![feature(extern_types)]
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
3+
#![feature(extern_types, strict_provenance)]
4+
5+
use std::ptr;
46

57
extern "C" {
68
type Foo;
79
}
810

911
fn main() {
10-
let x: &Foo = unsafe { &*(16 as *const Foo) };
12+
let x: &Foo = unsafe { &*(ptr::without_provenance::<()>(16) as *const Foo) };
1113
let _y: &Foo = &*x;
1214
}

src/tools/miri/tests/pass/extern_types.stack.stderr

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
warning: integer-to-pointer cast
2-
--> $DIR/extern_types.rs:LL:CC
3-
|
4-
LL | let x: &Foo = unsafe { &*(16 as *const Foo) };
5-
| ^^^^^^^^^^^^^^^^^^ integer-to-pointer cast
6-
|
7-
= help: this program is using integer-to-pointer casts or (equivalently) `ptr::with_exposed_provenance`, which means that Miri might miss pointer bugs in this program
8-
= help: see https://doc.rust-lang.org/nightly/std/ptr/fn.with_exposed_provenance.html for more details on that operation
9-
= help: to ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead
10-
= help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics
11-
= help: alternatively, `MIRIFLAGS=-Zmiri-permissive-provenance` disables this warning
12-
= note: BACKTRACE:
13-
= note: inside `main` at $DIR/extern_types.rs:LL:CC
14-
151
warning: reborrow of reference to `extern type`
162
--> $DIR/extern_types.rs:LL:CC
173
|
18-
LL | let x: &Foo = unsafe { &*(16 as *const Foo) };
19-
| ^^^^^^^^^^^^^^^^^^^^ reborrow of a reference to `extern type` is not properly supported
4+
LL | let x: &Foo = unsafe { &*(ptr::without_provenance::<()>(16) as *const Foo) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reborrow of a reference to `extern type` is not properly supported
206
|
217
= help: `extern type` are not compatible with the Stacked Borrows aliasing model implemented by Miri; Miri may miss bugs in this code
228
= help: try running with `MIRIFLAGS=-Zmiri-tree-borrows` to use the more permissive but also even more experimental Tree Borrows aliasing checks instead

src/tools/miri/tests/pass/intptrcast.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@revisions: stack tree
2-
//@[tree]compile-flags: -Zmiri-tree-borrows
31
//@compile-flags: -Zmiri-permissive-provenance
42

53
use std::mem;

src/tools/miri/tests/pass/pointers.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@revisions: stack tree
2-
//@[tree]compile-flags: -Zmiri-tree-borrows
31
//@compile-flags: -Zmiri-permissive-provenance
42
#![feature(ptr_metadata, const_raw_ptr_comparison)]
53
#![allow(ambiguous_wide_pointer_comparisons)]

src/tools/miri/tests/pass/ptr_int_casts.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@revisions: stack tree
2+
// Tree Borrows doesn't support int2ptr casts, but let's make sure we don't immediately crash either.
23
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
//@compile-flags: -Zmiri-permissive-provenance
4+
//@[stack]compile-flags: -Zmiri-permissive-provenance
45
use std::mem;
56
use std::ptr;
67

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
warning: integer-to-pointer cast
2+
--> $DIR/ptr_int_casts.rs:LL:CC
3+
|
4+
LL | assert_eq!(1 as *const i32 as usize, 1);
5+
| ^^^^^^^^^^^^^^^ integer-to-pointer cast
6+
|
7+
= help: this program is using integer-to-pointer casts or (equivalently) `ptr::with_exposed_provenance`, which means that Miri might miss pointer bugs in this program
8+
= help: see https://doc.rust-lang.org/nightly/std/ptr/fn.with_exposed_provenance.html for more details on that operation
9+
= help: to ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead
10+
= help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics
11+
= help: Tree Borrows does not support integer-to-pointer casts, so the program is likely to go wrong when this pointer gets used
12+
= note: BACKTRACE:
13+
= note: inside `ptr_int_casts` at $DIR/ptr_int_casts.rs:LL:CC
14+
note: inside `main`
15+
--> $DIR/ptr_int_casts.rs:LL:CC
16+
|
17+
LL | ptr_int_casts();
18+
| ^^^^^^^^^^^^^^^
19+
20+
warning: integer-to-pointer cast
21+
--> $DIR/ptr_int_casts.rs:LL:CC
22+
|
23+
LL | assert_eq!((1 as *const i32).wrapping_offset(4) as usize, 1 + 4 * 4);
24+
| ^^^^^^^^^^^^^^^^^ integer-to-pointer cast
25+
|
26+
= note: BACKTRACE:
27+
= note: inside `ptr_int_casts` at $DIR/ptr_int_casts.rs:LL:CC
28+
note: inside `main`
29+
--> $DIR/ptr_int_casts.rs:LL:CC
30+
|
31+
LL | ptr_int_casts();
32+
| ^^^^^^^^^^^^^^^
33+
34+
warning: integer-to-pointer cast
35+
--> $DIR/ptr_int_casts.rs:LL:CC
36+
|
37+
LL | *val = (1 as *const u8).wrapping_offset(-4);
38+
| ^^^^^^^^^^^^^^^^ integer-to-pointer cast
39+
|
40+
= note: BACKTRACE:
41+
= note: inside `ptr_int_casts` at $DIR/ptr_int_casts.rs:LL:CC
42+
note: inside `main`
43+
--> $DIR/ptr_int_casts.rs:LL:CC
44+
|
45+
LL | ptr_int_casts();
46+
| ^^^^^^^^^^^^^^^
47+
48+
warning: integer-to-pointer cast
49+
--> $DIR/ptr_int_casts.rs:LL:CC
50+
|
51+
LL | let y = y as *const _;
52+
| ^^^^^^^^^^^^^ integer-to-pointer cast
53+
|
54+
= note: BACKTRACE:
55+
= note: inside `ptr_int_casts` at $DIR/ptr_int_casts.rs:LL:CC
56+
note: inside `main`
57+
--> $DIR/ptr_int_casts.rs:LL:CC
58+
|
59+
LL | ptr_int_casts();
60+
| ^^^^^^^^^^^^^^^
61+
62+
warning: integer-to-pointer cast
63+
--> $DIR/ptr_int_casts.rs:LL:CC
64+
|
65+
LL | let x: fn() -> i32 = unsafe { mem::transmute(y as *mut u8) };
66+
| ^^^^^^^^^^^^ integer-to-pointer cast
67+
|
68+
= note: BACKTRACE:
69+
= note: inside `ptr_int_casts` at $DIR/ptr_int_casts.rs:LL:CC
70+
note: inside `main`
71+
--> $DIR/ptr_int_casts.rs:LL:CC
72+
|
73+
LL | ptr_int_casts();
74+
| ^^^^^^^^^^^^^^^
75+
76+
warning: integer-to-pointer cast
77+
--> $DIR/ptr_int_casts.rs:LL:CC
78+
|
79+
LL | assert_eq!((-1i32) as usize as *const i32 as usize, (-1i32) as usize);
80+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ integer-to-pointer cast
81+
|
82+
= note: BACKTRACE:
83+
= note: inside `ptr_int_casts` at $DIR/ptr_int_casts.rs:LL:CC
84+
note: inside `main`
85+
--> $DIR/ptr_int_casts.rs:LL:CC
86+
|
87+
LL | ptr_int_casts();
88+
| ^^^^^^^^^^^^^^^
89+

src/tools/miri/tests/pass/ptr_int_from_exposed.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@revisions: stack tree
2+
// Tree Borrows doesn't support int2ptr casts, but let's make sure we don't immediately crash either.
23
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
//@compile-flags: -Zmiri-permissive-provenance
4+
//@[stack]compile-flags: -Zmiri-permissive-provenance
45
#![feature(strict_provenance, exposed_provenance)]
56

67
use std::ptr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: integer-to-pointer cast
2+
--> $DIR/ptr_int_from_exposed.rs:LL:CC
3+
|
4+
LL | let ptr = ptr::with_exposed_provenance::<i32>(x_usize).wrapping_offset(-128);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ integer-to-pointer cast
6+
|
7+
= help: this program is using integer-to-pointer casts or (equivalently) `ptr::with_exposed_provenance`, which means that Miri might miss pointer bugs in this program
8+
= help: see https://doc.rust-lang.org/nightly/std/ptr/fn.with_exposed_provenance.html for more details on that operation
9+
= help: to ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead
10+
= help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics
11+
= help: Tree Borrows does not support integer-to-pointer casts, so the program is likely to go wrong when this pointer gets used
12+
= note: BACKTRACE:
13+
= note: inside `ptr_roundtrip_out_of_bounds` at $DIR/ptr_int_from_exposed.rs:LL:CC
14+
note: inside `main`
15+
--> $DIR/ptr_int_from_exposed.rs:LL:CC
16+
|
17+
LL | ptr_roundtrip_out_of_bounds();
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+

0 commit comments

Comments
 (0)