Open
Description
Turns out that stacked borrows and self-referential generators don't go well together. This code fails in Miri:
#![feature(generators, generator_trait)]
use std::{
ops::{Generator, GeneratorState},
pin::Pin,
};
fn firstn() -> impl Generator<Yield = u64, Return = ()> {
static move || {
let mut num = 0;
let num = &mut num;
yield *num;
*num += 1; //~ ERROR: borrow stack
yield *num;
*num += 1;
yield *num;
*num += 1;
}
}
fn main() {
let mut generator_iterator = firstn();
let mut pin = unsafe { Pin::new_unchecked(&mut generator_iterator) };
let mut sum = 0;
while let GeneratorState::Yielded(x) = pin.as_mut().resume() {
sum += x;
}
println!("{}", sum);
}
The reason it fails is that each time through the loop, we call Pin::as_mut
, which creates a fresh mutable reference to the generator. Since mutable references are unique, that invalidates the pointer that points from one field of the generator to another.
This is basically a particularly bad instance of #133.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RalfJung commentedon Jun 23, 2019
In some sense there are two problems here:
Pin<&mut T>
gets passed around, it gets retagged the same way as an&mut T
, assuring uniqueness of this pointer to the entire memory range. This is probably not something we want. So maybe we need a "no retagging for private fields" kind of thing, similar to what we might need for protectors. This is the (relatively) easy part.Pin::as_mut
creates a mutable reference internally (before wrapping it inPin
again). So even if we fix the above, we'd still assert uniqueness here. In some sense it would be more correct if aPin<&mut T>
would actually be represented as aNonNull<T>
, as that's a more honest reflection of the aliasing situation: it's not a unique pointer, there can be pointers inside this data structure that alias. This is the hard part.If we still had
PinMut
as a separate type, this would be fixable, but withPin<impl Deref>
, that's hard. And even then,map_unchecked_mut
still creates a mutable reference and even passes it as a function argument, which is about as strongly asserting uniqueness as we can -- and which we don't want, aliasing pointers are allowed to exist.RalfJung commentedon Jun 25, 2019
If we ignore the fact that references are wrapped inside the
Pin
struct, self-referential generators do the equivalent of:I explicitly wanted that code to be UB. While raw pointers aliasing with each other is allowed, having a single raw pointer and a mutable reference acts pretty much like having two mutable references -- after all, between the two of them, it's enough if one side does not allow aliasing, since "aliases-with" is a symmetric relation. If we replace
raw_ptr
by another reference above, the code is rejected by both the borrow checker and Stacked Borrows.So to fix this problem, I see two general options:
Pin
methods would have to do lots oftransmute
to avoid ever creating a "bare" reference. Andmap_unchecked_mut
has a problem. It should probably use raw pointers (and RFC for an operator to take a raw reference rfcs#2582 to get a field address). On the other hand, at least we have a plan.HadrienG2 commentedon Jun 29, 2019
@RalfJung: Could this be resolved by turning
Pin<&'a mut T>
into some abstraction around(&'a UnsafeCell<T>; PhantomData<&'a mut T>)
that does not actually hold a reference, but only behaves like one and spawns actual references on demand?This would likely require turning Pin into Compiler Magic(tm) that is hard to implement for user-defined pointer types, though, and I can imagine backwards-incompatible implications in other areas such as changing the safety contract for extracting mutable references from Pin...
EDIT: Ah, I see that you explored a
NonNull
-based variant of this strategy above.RalfJung commentedon Jun 29, 2019
You don't need
UnsafeCell
,*mut T
would do it. And yes that's basically what I had in mind with option (1).CAD97 commentedon Jun 29, 2019
The way I understand how Pin works, the self referential references borrow their validity from the one in the pin. So under SB, they're retagged from the pin.
The most minimal change that would make pin play nicely with SB I think would be to somehow keep the interior references valid even while the unsafe map_unchecked_mut reference is used.
Could it be possible to not retag for pin's map_unchecked_mut and only pop tags when the reference is used to access that memory location? (This is super spitball, sorry)
RalfJung commentedon Jun 29, 2019
Correct.
However, then the pin itself eventually gets retagged, and that kills the self-referential borrows. This currently happens all the time, but could be reduced to just "inside the
Pin
implementation" if we make retag respect privacy, or just generally not enter structs, or so.Well it'd need a magic marker or so.
That's basically option (2) from above.
[-]Stacked Borrows vs self-referential generators[/-][+]Stacked Borrows vs self-referential structs[/+]RalfJung commentedon Aug 13, 2019
#194 indicates that this is a problem with self-referential structs in general, not just self-referential generators. That's not surprising, so I generalized the issue title.
async_await
in Rust 1.39.0 rust-lang/rust#63209Aaron1011 commentedon Aug 22, 2019
@RalfJung: Related to your example of:
While working on pin-project, I wanted to write this code:
The key thing here is the
unproject
method. Basically, I want to be able to the following:&mut Self
to a*mut Self
Self
(&mut self.field
, etc.)*mut Self
to a&mut Self
, after all of the mutable fields references have gone out of scope.However, Miri flags this as UB for (I believe) a similar reason as your example - creating a mutable reference to a field ends up transitively asserts that we have unique ownership of the base type. Therefore, any pre-existing raw pointers to the base type will be invalidated.
Allowing this kind of pattern would make
pin-project
much more useful. It would allow the following pattern:This is especially useful when working with enums - here's a real-world example in Hyper.
However, I can't see a way to avoid creating and later 'upgrading' a raw pointer when trying to safely abstract this pattern in
pin-project
.54 remaining items