-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #872 - RalfJung:retag-shallow, r=oli-obk
Make Retag shallow A shallow retag does not traverse into fields of compound typed to search for references to retag. It only retags "top-level"/"bare" references (and boxes). This helps with rust-lang/unsafe-code-guidelines#125 because it also means that we do not add protectors for references passed in fields of a struct (or other compound types). Until we know what the rules should be for protectors, I prefer to be less aggressive about what we are rejecting. This also matches our work-in-progress Coq formalization.
- Loading branch information
Showing
8 changed files
with
93 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
tests/compile-fail/stacked_borrows/return_invalid_mut_option.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
// Make sure that we cannot return a `&mut` that got already invalidated, not even in an `Option`. | ||
// Due to shallow reborrowing, the error only surfaces when we look into the `Option`. | ||
fn foo(x: &mut (i32, i32)) -> Option<&mut i32> { | ||
let xraw = x as *mut (i32, i32); | ||
let ret = Some(unsafe { &mut (*xraw).1 }); | ||
let ret = unsafe { &mut (*xraw).1 }; // let-bind to avoid 2phase | ||
let ret = Some(ret); | ||
let _val = unsafe { *xraw }; // invalidate xref | ||
ret //~ ERROR borrow stack | ||
ret | ||
} | ||
|
||
fn main() { | ||
foo(&mut (1, 2)); | ||
match foo(&mut (1, 2)) { | ||
Some(_x) => {}, //~ ERROR borrow stack | ||
None => {}, | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
tests/compile-fail/stacked_borrows/return_invalid_mut_tuple.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
// Make sure that we cannot return a `&mut` that got already invalidated, not even in a tuple. | ||
// Due to shallow reborrowing, the error only surfaces when we look into the tuple. | ||
fn foo(x: &mut (i32, i32)) -> (&mut i32,) { | ||
let xraw = x as *mut (i32, i32); | ||
let ret = (unsafe { &mut (*xraw).1 },); | ||
let _val = unsafe { *xraw }; // invalidate xref | ||
ret //~ ERROR borrow stack | ||
ret | ||
} | ||
|
||
fn main() { | ||
foo(&mut (1, 2)); | ||
foo(&mut (1, 2)).0; //~ ERROR: borrow stack | ||
} |
8 changes: 6 additions & 2 deletions
8
tests/compile-fail/stacked_borrows/return_invalid_shr_option.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
// Make sure that we cannot return a `&` that got already invalidated, not even in an `Option`. | ||
// Due to shallow reborrowing, the error only surfaces when we look into the `Option`. | ||
fn foo(x: &mut (i32, i32)) -> Option<&i32> { | ||
let xraw = x as *mut (i32, i32); | ||
let ret = Some(unsafe { &(*xraw).1 }); | ||
unsafe { *xraw = (42, 23) }; // unfreeze | ||
ret //~ ERROR borrow stack | ||
ret | ||
} | ||
|
||
fn main() { | ||
foo(&mut (1, 2)); | ||
match foo(&mut (1, 2)) { | ||
Some(_x) => {}, //~ ERROR borrow stack | ||
None => {}, | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
tests/compile-fail/stacked_borrows/return_invalid_shr_tuple.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
// Make sure that we cannot return a `&` that got already invalidated, not even in a tuple. | ||
// Due to shallow reborrowing, the error only surfaces when we look into the tuple. | ||
fn foo(x: &mut (i32, i32)) -> (&i32,) { | ||
let xraw = x as *mut (i32, i32); | ||
let ret = (unsafe { &(*xraw).1 },); | ||
unsafe { *xraw = (42, 23) }; // unfreeze | ||
ret //~ ERROR borrow stack | ||
ret | ||
} | ||
|
||
fn main() { | ||
foo(&mut (1, 2)); | ||
foo(&mut (1, 2)).0; //~ ERROR borrow stack | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::cell::{RefCell, Ref, RefMut}; | ||
|
||
fn main() { | ||
basic(); | ||
ref_protector(); | ||
ref_mut_protector(); | ||
} | ||
|
||
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()) | ||
} |