Skip to content

Commit 84b3142

Browse files
authored
Merge pull request #4309 from RalfJung/both-borrows-tests
move tests that are identical between SB and TB to shared files
2 parents 70ef250 + aec861b commit 84b3142

18 files changed

+94
-532
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// FIXME: this miscompiles with optimizations, see <https://github.com/rust-lang/rust/issues/132898>.
2-
//@compile-flags: -Zmir-opt-level=0
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
33

44
trait S: Sized {
55
fn tpb(&mut self, _s: Self) {}
@@ -31,9 +31,9 @@ fn two_phase3(b: bool) {
3131
));
3232
}
3333

34-
#[allow(unreachable_code)]
3534
fn two_phase_raw() {
3635
let x: &mut Vec<i32> = &mut vec![];
36+
#[allow(unreachable_code)] // The `push` itself never gets reached.
3737
x.push({
3838
// Unfortunately this does not trigger the problem of creating a
3939
// raw ponter from a pointer that had a two-phase borrow derived from
@@ -59,52 +59,12 @@ fn two_phase_overlapping2() {
5959
x.add_assign(x + *l);
6060
}
6161

62-
fn with_interior_mutability() {
63-
use std::cell::Cell;
64-
65-
trait Thing: Sized {
66-
fn do_the_thing(&mut self, _s: i32) {}
67-
}
68-
69-
impl<T> Thing for Cell<T> {}
70-
71-
let mut x = Cell::new(1);
72-
let l = &x;
73-
74-
x.do_the_thing({
75-
x.set(3);
76-
l.set(4);
77-
x.get() + l.get()
78-
});
79-
}
80-
81-
// This one really shouldn't be accepted, but since we treat 2phase as raw, we do accept it.
82-
// Tree Borrows rejects it.
83-
fn aliasing_violation() {
84-
struct Foo(u64);
85-
impl Foo {
86-
fn add(&mut self, n: u64) -> u64 {
87-
self.0 + n
88-
}
89-
}
90-
91-
let mut f = Foo(0);
92-
let alias = &mut f.0 as *mut u64;
93-
let res = f.add(unsafe {
94-
*alias = 42;
95-
0
96-
});
97-
assert_eq!(res, 42);
98-
}
99-
10062
fn main() {
10163
two_phase1();
10264
two_phase2();
10365
two_phase3(false);
10466
two_phase3(true);
10567
two_phase_raw();
106-
with_interior_mutability();
10768
two_phase_overlapping1();
10869
two_phase_overlapping2();
109-
aliasing_violation();
11070
}

src/tools/miri/tests/pass/stacked-borrows/stacked-borrows.rs renamed to src/tools/miri/tests/pass/both_borrows/basic_aliasing_model.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
13
#![feature(allocator_api)]
24
use std::ptr;
35

4-
// Test various stacked-borrows-related things.
6+
// Test various aliasing-model-related things.
57
fn main() {
68
read_does_not_invalidate1();
79
read_does_not_invalidate2();
810
mut_raw_then_mut_shr();
911
mut_shr_then_mut_raw();
1012
mut_raw_mut();
11-
mut_raw_mut2();
1213
partially_invalidate_mut();
1314
drop_after_sharing();
1415
// direct_mut_to_const_raw();
@@ -97,18 +98,6 @@ fn mut_raw_mut() {
9798
assert_eq!(x, 4);
9899
}
99100

100-
// A variant of `mut_raw_mut` that does *not* get accepted by Tree Borrows.
101-
// It's kind of an accident that we accept it in Stacked Borrows...
102-
fn mut_raw_mut2() {
103-
unsafe {
104-
let mut root = 0;
105-
let to = &mut root as *mut i32;
106-
*to = 0;
107-
let _val = root;
108-
*to = 0;
109-
}
110-
}
111-
112101
fn partially_invalidate_mut() {
113102
let data = &mut (0u8, 0u8);
114103
let reborrow = &mut *data as *mut (u8, u8);
@@ -124,15 +113,6 @@ fn drop_after_sharing() {
124113
let _len = x.len();
125114
}
126115

127-
// Make sure that coercing &mut T to *const T produces a writeable pointer.
128-
// TODO: This is currently disabled, waiting on a decision on <https://github.com/rust-lang/rust/issues/56604>
129-
/*fn direct_mut_to_const_raw() {
130-
let x = &mut 0;
131-
let y: *const i32 = x;
132-
unsafe { *(y as *mut i32) = 1; }
133-
assert_eq!(*x, 1);
134-
}*/
135-
136116
// Make sure that we can create two raw pointers from a mutable reference and use them both.
137117
fn two_raw() {
138118
unsafe {
@@ -178,7 +158,7 @@ fn disjoint_mutable_subborrows() {
178158
let b = unsafe { borrow_field_b(ptr) };
179159
b.push(4);
180160
a.push_str(" world");
181-
eprintln!("{:?} {:?}", a, b);
161+
assert_eq!(format!("{:?} {:?}", a, b), r#""hello world" [0, 1, 2, 4]"#);
182162
}
183163

184164
fn raw_ref_to_part() {
@@ -243,7 +223,7 @@ fn not_unpin_not_protected() {
243223
pub struct NotUnpin(#[allow(dead_code)] i32, PhantomPinned);
244224

245225
fn inner(x: &mut NotUnpin, f: fn(&mut NotUnpin)) {
246-
// `f` may mutate, but it may not deallocate!
226+
// `f` is allowed to deallocate `x`.
247227
f(x)
248228
}
249229

src/tools/miri/tests/pass/tree_borrows/interior_mutability.rs renamed to src/tools/miri/tests/pass/both_borrows/interior_mutability.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
//@compile-flags: -Zmiri-tree-borrows
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
23
#![allow(dangerous_implicit_autorefs)]
4+
35
use std::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
46
use std::mem::{self, MaybeUninit};
57

@@ -14,6 +16,7 @@ fn main() {
1416
ref_protector();
1517
ref_mut_protector();
1618
rust_issue_68303();
19+
two_phase();
1720
}
1821

1922
fn aliasing_mut_and_shr() {
@@ -101,13 +104,15 @@ fn unsafe_cell_invalidate() {
101104
let raw1 = &mut x as *mut _;
102105
let ref1 = unsafe { &mut *raw1 };
103106
let raw2 = ref1 as *mut _;
104-
// Now the borrow tree is:
107+
// Now the borrow stack is: raw1, ref2, raw2.
108+
//
109+
// For TB, the tree is
105110
//
106111
// Act x
107112
// Res `- raw1
108113
// Res `- ref1, raw2
109114
//
110-
// So using raw1 invalidates raw2.
115+
// Either way, using raw1 invalidates raw2.
111116
f(unsafe { mem::transmute(raw2) }, raw1);
112117
}
113118

@@ -179,3 +184,27 @@ fn rust_issue_68303() {
179184
assert!(optional.is_some());
180185
*handle = true;
181186
}
187+
188+
fn two_phase() {
189+
use std::cell::Cell;
190+
191+
trait Thing: Sized {
192+
fn do_the_thing(&mut self, _s: i32) {}
193+
}
194+
195+
impl<T> Thing for Cell<T> {}
196+
197+
let mut x = Cell::new(1);
198+
let l = &x;
199+
200+
x.do_the_thing({
201+
// In TB terms:
202+
// Several Foreign accesses (both Reads and Writes) to the location
203+
// being reborrowed. Reserved + unprotected + interior mut
204+
// makes the pointer immune to everything as long as all accesses
205+
// are child accesses to its parent pointer x.
206+
x.set(3);
207+
l.set(4);
208+
x.get() + l.get()
209+
});
210+
}

src/tools/miri/tests/pass/stacked-borrows/interior_mutability.rs

-176
This file was deleted.

src/tools/miri/tests/pass/stacked-borrows/stacked-borrows.stderr

-1
This file was deleted.

src/tools/miri/tests/pass/stacked-borrows/issue-miri-2389.stderr renamed to src/tools/miri/tests/pass/stacked_borrows/issue-miri-2389.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: integer-to-pointer cast
2-
--> tests/pass/stacked-borrows/issue-miri-2389.rs:LL:CC
2+
--> tests/pass/stacked_borrows/issue-miri-2389.rs:LL:CC
33
|
44
LL | let wildcard = &root0 as *const Cell<i32> as usize as *const Cell<i32>;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ integer-to-pointer cast
@@ -10,5 +10,5 @@ LL | let wildcard = &root0 as *const Cell<i32> as usize as *const Cell<i
1010
= help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics
1111
= help: alternatively, `MIRIFLAGS=-Zmiri-permissive-provenance` disables this warning
1212
= note: BACKTRACE:
13-
= note: inside `main` at tests/pass/stacked-borrows/issue-miri-2389.rs:LL:CC
13+
= note: inside `main` at tests/pass/stacked_borrows/issue-miri-2389.rs:LL:CC
1414

0 commit comments

Comments
 (0)