merge two SB test files
This commit is contained in:
parent
c4e86e103e
commit
955f961f83
@ -1,5 +1,5 @@
|
||||
// compile-flags: -Zmiri-retag-fields
|
||||
use std::cell::{Cell, RefCell, UnsafeCell};
|
||||
use std::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
|
||||
use std::mem::{self, MaybeUninit};
|
||||
|
||||
fn main() {
|
||||
@ -9,6 +9,10 @@ fn main() {
|
||||
unsafe_cell_2phase();
|
||||
unsafe_cell_deallocate();
|
||||
unsafe_cell_invalidate();
|
||||
refcell_basic();
|
||||
ref_protector();
|
||||
ref_mut_protector();
|
||||
rust_issue_68303();
|
||||
}
|
||||
|
||||
fn aliasing_mut_and_shr() {
|
||||
@ -100,3 +104,72 @@ fn f(_x: &UnsafeCell<i32>, y: *mut i32) {
|
||||
// So using raw1 invalidates raw2.
|
||||
f(unsafe { mem::transmute(raw2) }, raw1);
|
||||
}
|
||||
|
||||
fn refcell_basic() {
|
||||
let c = RefCell::new(42);
|
||||
{
|
||||
let s1 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let s2 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
}
|
||||
{
|
||||
let mut m = c.borrow_mut();
|
||||
let _z: i32 = *m;
|
||||
{
|
||||
let s: &i32 = &*m;
|
||||
let _x = *s;
|
||||
}
|
||||
*m = 23;
|
||||
let _z: i32 = *m;
|
||||
}
|
||||
{
|
||||
let s1 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let s2 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
}
|
||||
}
|
||||
|
||||
// Adding a Stacked Borrows protector for `Ref` would break this
|
||||
fn ref_protector() {
|
||||
fn break_it(rc: &RefCell<i32>, r: Ref<'_, i32>) {
|
||||
// `r` has a shared reference, it is passed in as argument and hence
|
||||
// a protector is added that marks this memory as read-only for the entire
|
||||
// duration of this function.
|
||||
drop(r);
|
||||
// *oops* here we can mutate that memory.
|
||||
*rc.borrow_mut() = 2;
|
||||
}
|
||||
|
||||
let rc = RefCell::new(0);
|
||||
break_it(&rc, rc.borrow())
|
||||
}
|
||||
|
||||
fn ref_mut_protector() {
|
||||
fn break_it(rc: &RefCell<i32>, r: RefMut<'_, i32>) {
|
||||
// `r` has a shared reference, it is passed in as argument and hence
|
||||
// a protector is added that marks this memory as inaccessible for the entire
|
||||
// duration of this function
|
||||
drop(r);
|
||||
// *oops* here we can mutate that memory.
|
||||
*rc.borrow_mut() = 2;
|
||||
}
|
||||
|
||||
let rc = RefCell::new(0);
|
||||
break_it(&rc, rc.borrow_mut())
|
||||
}
|
||||
|
||||
/// Make sure we do not have bad enum layout optimizations.
|
||||
fn rust_issue_68303() {
|
||||
let optional = Some(RefCell::new(false));
|
||||
let mut handle = optional.as_ref().unwrap().borrow_mut();
|
||||
assert!(optional.is_some());
|
||||
*handle = true;
|
||||
}
|
||||
|
@ -1,78 +0,0 @@
|
||||
// compile-flags: -Zmiri-retag-fields
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
|
||||
fn main() {
|
||||
basic();
|
||||
ref_protector();
|
||||
ref_mut_protector();
|
||||
rust_issue_68303();
|
||||
}
|
||||
|
||||
fn basic() {
|
||||
let c = RefCell::new(42);
|
||||
{
|
||||
let s1 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let s2 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
}
|
||||
{
|
||||
let mut m = c.borrow_mut();
|
||||
let _z: i32 = *m;
|
||||
{
|
||||
let s: &i32 = &*m;
|
||||
let _x = *s;
|
||||
}
|
||||
*m = 23;
|
||||
let _z: i32 = *m;
|
||||
}
|
||||
{
|
||||
let s1 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let s2 = c.borrow();
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
let _x: i32 = *s1;
|
||||
let _y: i32 = *s2;
|
||||
}
|
||||
}
|
||||
|
||||
// Adding a Stacked Borrows protector for `Ref` would break this
|
||||
fn ref_protector() {
|
||||
fn break_it(rc: &RefCell<i32>, r: Ref<'_, i32>) {
|
||||
// `r` has a shared reference, it is passed in as argument and hence
|
||||
// a protector is added that marks this memory as read-only for the entire
|
||||
// duration of this function.
|
||||
drop(r);
|
||||
// *oops* here we can mutate that memory.
|
||||
*rc.borrow_mut() = 2;
|
||||
}
|
||||
|
||||
let rc = RefCell::new(0);
|
||||
break_it(&rc, rc.borrow())
|
||||
}
|
||||
|
||||
fn ref_mut_protector() {
|
||||
fn break_it(rc: &RefCell<i32>, r: RefMut<'_, i32>) {
|
||||
// `r` has a shared reference, it is passed in as argument and hence
|
||||
// a protector is added that marks this memory as inaccessible for the entire
|
||||
// duration of this function
|
||||
drop(r);
|
||||
// *oops* here we can mutate that memory.
|
||||
*rc.borrow_mut() = 2;
|
||||
}
|
||||
|
||||
let rc = RefCell::new(0);
|
||||
break_it(&rc, rc.borrow_mut())
|
||||
}
|
||||
|
||||
/// Make sure we do not have bad enum layout optimizations.
|
||||
fn rust_issue_68303() {
|
||||
let optional = Some(RefCell::new(false));
|
||||
let mut handle = optional.as_ref().unwrap().borrow_mut();
|
||||
assert!(optional.is_some());
|
||||
*handle = true;
|
||||
}
|
Loading…
Reference in New Issue
Block a user