forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rust-lang#2636 - RalfJung:scalar-field-retag, r=oli-obk
Stacked Borrows: make scalar field retagging the default I think it is time to finally close this soundness gap. Any objections? :) Unfortunately the latest released versions of hashbrown and scopeguard can fail under full field retagging. The fixes have landed in the git repos but have not been released yet. I don't know if scalar field retagging as enabled by this PR is sufficient to cause problems with these crates, but it seems likely that this would be the case -- e.g. if both `value` and `dropfn` are scalars, the entire scopeguard struct will be a `ScalarPair` and thus get field retagging. However, given that we actually generate LLVM `noalias` for these cases, it seems prudent to inform users of this risk. They can easily set `-Zmiri-field-retag=none` to opt-out of this change. Cc rust-lang/miri#2528
- Loading branch information
Showing
14 changed files
with
76 additions
and
42 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
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
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,16 +1,15 @@ | ||
// 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 = unsafe { &mut (*xraw).1 }; // let-bind to avoid 2phase | ||
let ret = Some(ret); | ||
let _val = unsafe { *xraw }; // invalidate xref | ||
ret | ||
ret //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
} | ||
|
||
fn main() { | ||
match foo(&mut (1, 2)) { | ||
Some(_x) => {} //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
Some(_x) => {} | ||
None => {} | ||
} | ||
} |
19 changes: 12 additions & 7 deletions
19
tests/fail/stacked_borrows/return_invalid_mut_option.stderr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
// 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 | ||
ret //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
} | ||
|
||
fn main() { | ||
foo(&mut (1, 2)).0; //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
foo(&mut (1, 2)).0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
// 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 | ||
ret //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
} | ||
|
||
fn main() { | ||
match foo(&mut (1, 2)) { | ||
Some(_x) => {} //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
Some(_x) => {} | ||
None => {} | ||
} | ||
} |
19 changes: 12 additions & 7 deletions
19
tests/fail/stacked_borrows/return_invalid_shr_option.stderr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
// 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 | ||
ret //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
} | ||
|
||
fn main() { | ||
foo(&mut (1, 2)).0; //~ ERROR: /retag .* tag does not exist in the borrow stack/ | ||
foo(&mut (1, 2)).0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//@compile-flags: -Zmiri-retag-fields=none | ||
|
||
struct Newtype<'a>(&'a mut i32); | ||
|
||
fn dealloc_while_running(_n: Newtype<'_>, dealloc: impl FnOnce()) { | ||
dealloc(); | ||
} | ||
|
||
// Make sure that we do *not* retag the fields of `Newtype`. | ||
fn main() { | ||
let ptr = Box::into_raw(Box::new(0i32)); | ||
#[rustfmt::skip] // I like my newlines | ||
unsafe { | ||
dealloc_while_running( | ||
Newtype(&mut *ptr), | ||
|| drop(Box::from_raw(ptr)), | ||
) | ||
}; | ||
} |
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,6 +1,6 @@ | ||
0..1: [ SharedReadWrite<TAG> ] | ||
0..1: [ SharedReadWrite<TAG> ] | ||
0..1: [ SharedReadWrite<TAG> ] | ||
0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ] | ||
0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ] | ||
0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ] | ||
0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ] | ||
0..1: [ unknown-bottom(..<TAG>) ] |