Skip to content

Commit

Permalink
add write_does_not_invalidate_all_aliases test, and enable direct_mut…
Browse files Browse the repository at this point in the history
…_to_const_raw test in TB
  • Loading branch information
RalfJung committed Aug 2, 2023
1 parent f128057 commit c951208
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
37 changes: 31 additions & 6 deletions src/tools/miri/tests/pass/stacked-borrows/stacked-borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
mut_raw_mut();
partially_invalidate_mut();
drop_after_sharing();
direct_mut_to_const_raw();
// direct_mut_to_const_raw();
two_raw();
shr_and_raw();
disjoint_mutable_subborrows();
Expand All @@ -19,6 +19,7 @@ fn main() {
mut_below_shr();
wide_raw_ptr_in_tuple();
not_unpin_not_protected();
write_does_not_invalidate_all_aliases();
}

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

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

// Make sure that we can create two raw pointers from a mutable reference and use them both.
fn two_raw() {
Expand Down Expand Up @@ -238,3 +238,28 @@ fn not_unpin_not_protected() {
drop(unsafe { Box::from_raw(raw) });
});
}

fn write_does_not_invalidate_all_aliases() {
mod other {
/// Some private memory to store stuff in.
static mut S: *mut i32 = 0 as *mut i32;

pub fn lib1(x: &&mut i32) {
unsafe {
S = (x as *const &mut i32).cast::<*mut i32>().read();
}
}

pub fn lib2() {
unsafe {
*S = 1337;
}
}
}

let x = &mut 0;
other::lib1(&x);
*x = 42; // a write to x -- invalidates other pointers?
other::lib2();
assert_eq!(*x, 1337); // oops, the value changed! I guess not all pointers were invalidated
}
39 changes: 34 additions & 5 deletions src/tools/miri/tests/pass/tree_borrows/tree-borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
aliasing_read_only_mutable_refs();
string_as_mut_ptr();
two_mut_protected_same_alloc();
direct_mut_to_const_raw();

// Stacked Borrows tests
read_does_not_invalidate1();
Expand All @@ -19,7 +20,6 @@ fn main() {
mut_raw_mut();
partially_invalidate_mut();
drop_after_sharing();
direct_mut_to_const_raw();
two_raw();
shr_and_raw();
disjoint_mutable_subborrows();
Expand All @@ -28,6 +28,7 @@ fn main() {
mut_below_shr();
wide_raw_ptr_in_tuple();
not_unpin_not_protected();
write_does_not_invalidate_all_aliases();
}

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

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

// Make sure that we can create two raw pointers from a mutable reference and use them both.
Expand Down Expand Up @@ -298,3 +299,31 @@ fn not_unpin_not_protected() {
drop(unsafe { Box::from_raw(raw) });
});
}

fn write_does_not_invalidate_all_aliases() {
// In TB there are other ways to do that (`addr_of!(*x)` has the same tag as `x`),
// but let's still make sure this SB test keeps working.

mod other {
/// Some private memory to store stuff in.
static mut S: *mut i32 = 0 as *mut i32;

pub fn lib1(x: &&mut i32) {
unsafe {
S = (x as *const &mut i32).cast::<*mut i32>().read();
}
}

pub fn lib2() {
unsafe {
*S = 1337;
}
}
}

let x = &mut 0;
other::lib1(&x);
*x = 42; // a write to x -- invalidates other pointers?
other::lib2();
assert_eq!(*x, 1337); // oops, the value changed! I guess not all pointers were invalidated
}

0 comments on commit c951208

Please sign in to comment.