Skip to content

Commit c951208

Browse files
committed
add write_does_not_invalidate_all_aliases test, and enable direct_mut_to_const_raw test in TB
1 parent f128057 commit c951208

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
mut_raw_mut();
1111
partially_invalidate_mut();
1212
drop_after_sharing();
13-
direct_mut_to_const_raw();
13+
// direct_mut_to_const_raw();
1414
two_raw();
1515
shr_and_raw();
1616
disjoint_mutable_subborrows();
@@ -19,6 +19,7 @@ fn main() {
1919
mut_below_shr();
2020
wide_raw_ptr_in_tuple();
2121
not_unpin_not_protected();
22+
write_does_not_invalidate_all_aliases();
2223
}
2324

2425
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
@@ -110,14 +111,13 @@ fn drop_after_sharing() {
110111
}
111112

112113
// Make sure that coercing &mut T to *const T produces a writeable pointer.
113-
fn direct_mut_to_const_raw() {
114-
// TODO: This is currently disabled, waiting on a decision on <https://github.com/rust-lang/rust/issues/56604>
115-
/*let x = &mut 0;
114+
// TODO: This is currently disabled, waiting on a decision on <https://github.com/rust-lang/rust/issues/56604>
115+
/*fn direct_mut_to_const_raw() {
116+
let x = &mut 0;
116117
let y: *const i32 = x;
117118
unsafe { *(y as *mut i32) = 1; }
118119
assert_eq!(*x, 1);
119-
*/
120-
}
120+
}*/
121121

122122
// Make sure that we can create two raw pointers from a mutable reference and use them both.
123123
fn two_raw() {
@@ -238,3 +238,28 @@ fn not_unpin_not_protected() {
238238
drop(unsafe { Box::from_raw(raw) });
239239
});
240240
}
241+
242+
fn write_does_not_invalidate_all_aliases() {
243+
mod other {
244+
/// Some private memory to store stuff in.
245+
static mut S: *mut i32 = 0 as *mut i32;
246+
247+
pub fn lib1(x: &&mut i32) {
248+
unsafe {
249+
S = (x as *const &mut i32).cast::<*mut i32>().read();
250+
}
251+
}
252+
253+
pub fn lib2() {
254+
unsafe {
255+
*S = 1337;
256+
}
257+
}
258+
}
259+
260+
let x = &mut 0;
261+
other::lib1(&x);
262+
*x = 42; // a write to x -- invalidates other pointers?
263+
other::lib2();
264+
assert_eq!(*x, 1337); // oops, the value changed! I guess not all pointers were invalidated
265+
}

src/tools/miri/tests/pass/tree_borrows/tree-borrows.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ fn main() {
1010
aliasing_read_only_mutable_refs();
1111
string_as_mut_ptr();
1212
two_mut_protected_same_alloc();
13+
direct_mut_to_const_raw();
1314

1415
// Stacked Borrows tests
1516
read_does_not_invalidate1();
@@ -19,7 +20,6 @@ fn main() {
1920
mut_raw_mut();
2021
partially_invalidate_mut();
2122
drop_after_sharing();
22-
direct_mut_to_const_raw();
2323
two_raw();
2424
shr_and_raw();
2525
disjoint_mutable_subborrows();
@@ -28,6 +28,7 @@ fn main() {
2828
mut_below_shr();
2929
wide_raw_ptr_in_tuple();
3030
not_unpin_not_protected();
31+
write_does_not_invalidate_all_aliases();
3132
}
3233

3334
// Tree Borrows has no issue with several mutable references existing
@@ -172,12 +173,12 @@ fn drop_after_sharing() {
172173

173174
// Make sure that coercing &mut T to *const T produces a writeable pointer.
174175
fn direct_mut_to_const_raw() {
175-
// TODO: This is currently disabled, waiting on a decision on <https://github.com/rust-lang/rust/issues/56604>
176-
/*let x = &mut 0;
176+
let x = &mut 0;
177177
let y: *const i32 = x;
178-
unsafe { *(y as *mut i32) = 1; }
178+
unsafe {
179+
*(y as *mut i32) = 1;
180+
}
179181
assert_eq!(*x, 1);
180-
*/
181182
}
182183

183184
// Make sure that we can create two raw pointers from a mutable reference and use them both.
@@ -298,3 +299,31 @@ fn not_unpin_not_protected() {
298299
drop(unsafe { Box::from_raw(raw) });
299300
});
300301
}
302+
303+
fn write_does_not_invalidate_all_aliases() {
304+
// In TB there are other ways to do that (`addr_of!(*x)` has the same tag as `x`),
305+
// but let's still make sure this SB test keeps working.
306+
307+
mod other {
308+
/// Some private memory to store stuff in.
309+
static mut S: *mut i32 = 0 as *mut i32;
310+
311+
pub fn lib1(x: &&mut i32) {
312+
unsafe {
313+
S = (x as *const &mut i32).cast::<*mut i32>().read();
314+
}
315+
}
316+
317+
pub fn lib2() {
318+
unsafe {
319+
*S = 1337;
320+
}
321+
}
322+
}
323+
324+
let x = &mut 0;
325+
other::lib1(&x);
326+
*x = 42; // a write to x -- invalidates other pointers?
327+
other::lib2();
328+
assert_eq!(*x, 1337); // oops, the value changed! I guess not all pointers were invalidated
329+
}

0 commit comments

Comments
 (0)