Skip to content

Commit f26c2cb

Browse files
authored
Merge pull request #699 from RalfJung/stacked-borrows-2
test another version of 'creating a shared ref must not leak the Unique'
2 parents 5adfa1d + 287ffb8 commit f26c2cb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/stacked_borrows.rs

+5
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,11 @@ trait EvalContextPrivExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
625625
};
626626

627627
// Reborrow.
628+
// TODO: With `two_phase == true`, this performs a weak reborrow for a `Unique`. That
629+
// can lead to some possibly surprising effects, if the parent permission is
630+
// `SharedReadWrite` then we now have a `Unique` in the middle of them, which "splits"
631+
// them in terms of what remains valid when the `Unique` gets used. Is that really
632+
// what we want?
628633
this.reborrow(place, size, kind, new_tag, /*force_weak:*/ two_phase, protect)?;
629634
let new_place = place.replace_tag(new_tag);
630635
// Handle two-phase borrows.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Creating a shared reference does not leak the data to raw pointers,
2+
// not even when interior mutability is involved.
3+
4+
use std::cell::Cell;
5+
use std::ptr;
6+
7+
fn main() { unsafe {
8+
let x = &mut Cell::new(0);
9+
let raw = x as *mut Cell<i32>;
10+
let x = &mut *raw;
11+
let _shr = &*x;
12+
// The state here is interesting because the top of the stack is [Unique, SharedReadWrite],
13+
// just like if we had done `x as *mut _`.
14+
// If we said that reading from a lower item is fine if the top item is `SharedReadWrite`
15+
// (one way to maybe preserve a stack discipline), then we could now read from `raw`
16+
// without invalidating `x`. That would be bad! It would mean that creating `shr`
17+
// leaked `x` to `raw`.
18+
let _val = ptr::read(raw);
19+
let _val = *x.get_mut(); //~ ERROR borrow stack
20+
} }

0 commit comments

Comments
 (0)