Stacked Borrows: An Aliasing Model For Rust
In this post, I am proposing “Stacked Borrows”: A set of rules defining which kinds of aliasing are allowed in Rust. This is intended to answer the question which pointer may be used when to perform which kinds of memory accesses.
This is a long-standing open question of many unsafe code authors, and also by compiler authors who want to add more optimizations. The model I am proposing here is by far not the first attempt at giving a definition: The model is heavily based on ideas by @arielb1 and @ubsan, and of course taking into account the lessons I learned last year when I took my first stab at defining such a model, dubbed “Types as Contracts”.
But before I delve into my latest proposal, I want to briefly discuss a key difference between my previous model and this one: “Types as Contracts” was a fully “validity”-based model, while “Stacked Borrows” is (to some extent) “access”-based.
Update: Since publishing this post, I have written another blog post about a slightly adjusted version of Stacked Borrows (the first version that actually got implemented). That other post is self-contained, so if you are just interested in the current state of Stacked Borrows, I suggest you go there. Only go on reading here if you want some additional historic context. /Update
1 Validity-based vs. Access-based
An “access”-based model is one where certain properties – in this case, mutable references being unique and shared references pointing to read-only memory – are only enforced when the reference is actually used to access memory. In contrast, a “validity”-based model requires these properties to always hold for all references that could be used. In both cases, violating a property that the model requires to hold is undefined behavior..
Essentially, with a validity-based model like “Types as Contracts”, the basic idea is that all data is always valid according to the type it is given. Enforcing the restrictions of such a model (e.g., when checking whether a program has undefined behavior) amounts to eagerly checking all reachable data for validity. An access-based model, on the other hand, only requires data to be valid when used. Enforcing it amounts to lazily checking the bare minimum at each operation.
Validity-based models have several advantages: Eager checking means we can typically identify which code is actually responsible for producing the “bad” value. “All data must always be valid” is also easier to explain than a long list of operations and the kind of restrictions they place upon the data.
However, in Rust, we cannot talk about references and whether the are valid at their given type without talking about lifetimes. With “Types as Contracts”, the exact place where a lifetime ended turned out to be really important. Not only did this make the specification complex and hard to understand; the implementation in Miri also had to actively work against the compiler’s general intention to forget about lifetimes as early as possible. With non-lexical lifetimes, the “end” of a lifetime is not even so clearly defined any more.
2 Stacking Borrows
For these reasons, my second proposal makes lifetimes in general and the result of lifetime inference in particular completely irrelevant for whether a program has undefined behavior (UB). This is one of the core design goals.
If you need some more context on undefined behavior and how it relates to compiler optimizations, I suggest you read my blog post on this topic first. It’s not a long post, and I cannot repeat everything again here. :)
The central idea of this model (and its precursors by @arielb1 and @ubsan) is that, for every location, we keep track of the references that are allowed to access this location.
(I will discuss later just how we keep track of this; for now, let’s just assume it can be done.)
This forms a stack: When we have an &mut i32
, we can reborrow it to obtain a new reference.
That new reference is now the one that must be used for this location, but the old reference it was created from cannot be forgotten: At some point, the reborrow will expire and the old reference will be “active” again.
We will have other items on that stack as well, so we will write Uniq(x)
to indicate that x
is the unique reference permitted to access this location.
Let us look at an example:
Of course, this example would not compile because the borrow checker would complain. However, in my interpretation, the reason it complains is that if it accepted the program, we would have UB in safe code!
This is worth pondering a bit: The model defines program semantics without taking lifetimes into account, so we can run programs and ask whether they have UB without ever doing lifetime inference or borrow checking (very much unlike “Types as Contracts”). One important property, then, is that if the program has UB and does not use any unsafe code, the borrow checker must detect this. In some sense, my model defines a dynamic version of the borrow checker that works without lifetimes. It turns out that even with non-lexical lifetimes, the borrow structure for a given location is still well-nested, which is why we can arrange borrows in a stack.
2.1 Raw Pointers
Let us bypass the borrow checker by adding some unsafe code to our program:
What happens here is that we are casting x
to a raw pointer.
For raw pointers, we cannot really keep track of where and how they have been created – raw pointers can be safely cast to and from integers, and data could flow arbitrarily.
So, when a &mut
is cast to *mut
like above, we instead push Raw
onto the stack, indicating that any raw pointer may be used to access this location.
(The usual restrictions about address arithmetic across allocations still apply, I am just talking about the borrow checking here.)
In the next line, we use a raw pointer to create y
.
That is okay because Raw
is active.
As usual when a reference is created, we push it onto the stack.
This makes y
the active reference, so we can use it in the next line.
And again, using x
pops the stack until x
is active – in this case, this removes both the Uniq(y)
and the Raw
, making y
unusable and causing UB in the last line.
Let us look at another example involving raw pointers:
Because raw pointers are tracked on the stack, they have to follow the well-nested structure.
y
was “created from” raw
, so using raw
again invalidates y
!
This is exactly in symmetry with the first example where y
was “created from” x
, so using x
again invalidated y
.
2.2 Shared References
For shared references, of course, we do not have a single reference which is the only one with permission to access. The key property we have to model is that shared references point to memory that does not change (assuming no interior mutability is involved). The memory is, so to speak, frozen.
For this purpose, we tag shared references with some kind of “timestamp” indicating when it was created. We also have an extra flag for each location storing since when the location is frozen. Using a shared reference to access memory is okay if memory has been frozen continuously since the reference was created.
We can see this in action in the following example:
Shared references with interior mutability do not really have any restrictions in terms of what can happen to memory, so we treat them basically like raw pointers.
2.3 Recap
For every location in memory, we keep track of a stack of borrows (Uniq(_)
or Raw
), and potentially “top off” this stack by freezing the location.
A frozen location is never written to, and no Uniq
is pushed.
Whenever a mutable reference is created, a matching Uniq
is pushed onto the stack for every location “covered by” the reference – i.e., the locations that would be accessed when the reference is used (starting at where it points to, and going on for size_of_val
many bytes).
Whenever a shared reference is created, if there is no interior mutability, we freeze the locations if they are not already frozen.
If there is interior mutability, we just push a Raw
.
Whenever a raw pointer is created from a mutable reference, we push a Raw
.
(Nothing happens when a raw pointer is created from a shared reference.)
A mutable reference x
is “active” for a location if that location is not frozen and Uniq(x)
is on top of the stack.
A shared reference without interior mutability is active if the location is frozen at least since the location was created.
A shared reference with interior mutability is active is Raw
is on top of the stack.
Whenever a reference is used to do something (anything), we make sure that it is active again for all locations that it covers; this can involve unfreezing and popping the stack. If it is not possible to reactivate the reference this way, we have UB.
Update: I previously talked about “activating” the reference instead of “reactivating it”; I decided to change terminology to make it clearer that this is an old reference being used again, so it must be on the stack already (but might not be at the top). /Update
3 Tracking Borrows
So far, I have just been assuming that we can somehow keep a connection between a reference like x
in the code above, and an item Uniq(x)
on the stack.
I also said we are keeping track of when a shared reference was created.
To realize this, we need to somehow have information “tagged” to the reference.
In particular, notice that x
and y
in the first example have the same address.
If we compared them as raw pointers, they would turn out equal.
And yet, it makes a huge difference if we use x
or y
!
If you read my previous post on why pointers are complicated, this should not come as too much of a surprise. There is more to a pointer, or a reference (I am using these terms mostly interchangeably), than the address in memory that it points to.
For the purpose of this model, we assume that a value of reference type consists of two parts: An address in memory, and a tag used to store the time when the reference was created. “Time” here is a rather abstract notion, we really just need some counter that we bump up every time a new reference is created. This gives us a unique ID for each mutable reference – and, as we have seen, for shared references we actually exploit the fact that IDs are handed out in increasing order (so that we can test if a reference was created before or after a location was frozen). So, we can actually treat mutable and shard references uniformly in that both just record, in their tag, the time at which they were created.
Whenever I said above that we have Uniq(x)
on the stack, what I really meant is that we have Uniq(t_x)
on the stack, where t_x
is some clock value, and that the “tag” of x
is t_x
.
For the sake of readability, I will continue to use the Uniq(x)
notation below.
Since raw pointers are not tracked, we can erase the tag when casting a reference to a raw pointer. This means our tag does not interfere with pointer-integer casts, which means there are a whole bunch of complicated questions we do not have to worry about. :)
Of course, these tags do not exist on real hardware. But that is besides the point. When specifying program behavior, we can work with an “instrumented machine” that has extra state which is not present on the real machine, as long as we only use that extra state to define whether a program is UB or not: On real hardware, we can ignore programs that are UB (they may just do whatever), so the extra state does not matter.
Tags are something I wanted to avoid in “Types as Contracts” – that was one of the initial design constraints I had put upon myself, in the hope of avoiding the trouble coming with “complicated pointers”. However, I now came to the conclusion that tagging pointers is a price worth paying if it means we can make lifetimes irrelevant.
4 Retagging and Barriers
I hope you now have a clear idea of the basic structure of the model I am proposing: The stack of borrows, the freeze flag, and references tagged with the time at which they got created. The full model is not quite as simple, but it is not much more complicated either. We need to add just two more concepts: Retagging and barriers.
4.1 Retagging
Remember that every time we create a mutable borrow, we assign it the current
clock values as its tag. Since the tag can never be changed, this means two
different variables can never have the same tag – right? Well, unfortunately,
things are not so simple: Using
e.g. transmute_copy
or a union
, one can make a copy of a reference in a way that Rust does not
even notice.
Still, we would like to make statements about code like this:
The trouble is, we cannot prevent the outside world from passing bogus &mut
that have the same tag.
Does this mean we are back to square one in terms of making aliased mutable references UB?
Lucky enough, we are not! We have a lot of machinery at our disposal, we just have to tweak it a little.
What we will do is, every time a reference comes “into” our function (this can be a function argument, but also loading it from memory or getting it as the return value of some other function), we perform “retagging”: We change the tags of the mutable references to the current clock value, bumping up the clock after every tag we assign, and then we push those new tags on top of the borrow stack. This way, we can know – without making any assumptions about foreign code – that all references have distinct IDs. In particular, two different references can never be both “active” for the same location at the same time.
With this additional step, it is now easy to argue that demo4
above is UB when x
and y
alias, no matter their initial tag:
After using x
, we know it is active.
Next we use and reactivate y
, which has to pop Uniq(x)
as they have distinct tags.
Finally, we use x
again even though it is no longer in the stack, triggering UB.
(A Uniq
is only ever pushed when it is created, so it is never in the stack more than once.)
4.2 Barriers
There is one more concept I would like to add: Barriers.
The model would make a lot of sense even without barriers – but adding barriers rules out some more behavior that I do not think we want to allow.
It is also needed to explain why we can put the noalias
parameter attribute on our functions when generating LLVM IR.
Consider the following code:
The question is: Can we reorder the *x = 42;
down to the end of demo5
?
Notice that we are not using x
again, so we cannot assume that x
is active at the end of demo5
!
This is the usual trouble with access-based models.
However, someone might conceivably call demo5
with y
being x as *mut _ as usize
, which means reordering could change program behavior.
To fix this, we have to make sure that if someone actually calls demo5
this way, we have UB even though x
is not used again.
To this end, I propose to turn the dial a little more towards a validity-based model by imposing some extra constraints.
We want to ensure that turning the integer y
into a reference does not pop x
from the stack and continue executing the program (we want UB instead).
This could happen if the stack contained, somewhere, a Raw
.
Remember that we do not tag raw pointers, so when a raw pointer was involved in creating x
, that Raw
item will still be on the stack, enabling any raw pointer to be used to access this location.
This is sometimes crucial, but in this case, demo5
should be able to prevent those old historic borrows involved in creating x
from being reactivated.
The idea is to put a “barrier” into the stack of all function arguments when demo5
gets called, and to make it UB to pop that barrier from the stack before demo5
returns.
This way, all the borrows further down in the stack (below Uniq(x)
) are temporarily disabled and cannot be reactivated while demo5
runs.
This means that even if y
happens to be the memory address x
points to, it is UB to cast y
to a reference because the Raw
item cannot be reactivated.
Another way to think about barriers is as follows:
The model generally ignores lifetimes and does not know how long they last.
All we know is that when a reference is used, its lifetime must be ongoing, so we say that is when we reactivate the borrow.
On top of this, barriers encode the fact that, when a reference is passed as an argument to a function, then its lifetime (whatever it is) extends beyond the current function call.
In our example, this means that no borrow further up the stack (these are the borrows with even longer lifetimes) can be used while demo5
is running.
A nice side-effect of barriers in combination with renumbering is that even if demo4
from the previous subsection would not use its arguments at all, it would still be UB to call it with two aliasing references:
When renumbering x
, we are pushing a barrier. Renumbering y
would attempt to reactivate Uniq(y)
, but that can only be behind the barrier, so it cannot be reactivated.
5 The Model in Code
Now we have everything together. Instead of giving another recap, I will try to give an alternative, more precise description of the model in the form of pseudo Rust code. This is essentially a draft of the code that will hopefully be in Miri soon, to actually dynamically track the borrow stack and enforce the rules. This is also how I go about developing such models – I use some form of pseudo-Rust, which I find it easier to be precise in than pure English. Some details have been omitted in the high-level description so far, they should all be in this code.
If you are only interested in the high-level picture, feel free to skip to the end. The rest of this is more like a specification than an explanatory blog post. The nice thing is that even with the spec, this post is still shorter than the one introducing “Types as Contracts”. :)
5.1 Per-Location Operations
Imagine we have a type MemoryByte
storing the per-location information in memory.
This is where we put the borrow stack and the information about freezing:
Next, we define some per-location operations that we will use later to define what happens when working with references.
Below, assert!
is used for things that should always be true because of interpreter invariants (i.e., Miri will ICE if they fail to hold), and bail!
is used to indicate that the program has UB.
5.2 MIR operations
Finally, we enhance some MIR operations with bookkeeping, following the model I described above. This is where the code gets more “pseudo” and less Rust. ;)
For each of these operation, we iterate over all affected locations; let us call the loop variable loc
of type MemoryByte
.
We also have a variable tag
with the tag of the pointer we are operating on (loading, or storing, or casting to a raw pointer, …).
Moreover, we have a boolean variable in_unsafe_cell
indicating whether, according to the type of the pointer, the location we are currently working on is covered by an UnsafeCell
.
(This realizes the conditions checking whether we have interior mutability or not.)
For example, in &Cell<i32>
, all 4 locations are inside an UnsafeCell
.
However, in &(i32, Cell<i32>)
, only the last 4 of the 8 covered locations are inside an UnsafeCell
.
Finally, given a reference type, a tag, and whether we are inside an UnsafeCell
, we can compute the matching Borrow
:
Mutable references use Mut(Uniq(tag))
, shared references in an UnsafeCell
use Mut(Raw)
and other shared references use Frz(tag)
.
We use bor
to refer to the Borrow
of the pointer we are working on.
Now we can look at what happens for each operation.
- Using a raw pointer directly is desugared to creating a shared reference (when reading) or a mutable reference (when writing), and using that. The appropriate steps below apply.
- Any time we use a (mutable or shared) reference to access memory, and any time we pass a reference to “the outside world” (passing it to a function, storing it in memory, returning it to our caller; also below structs or enums but not below unions or pointer indirectons), we reactivate.
loc.reactivate(borrow)
.
- Any time a new reference is created (any time we run an expression
&mut foo
or&foo
), we (re)borrow.- Bump up the clock, and remember the old time as
new_tag
. - Compute
new_bor
fromnew_tag
and the type of the reference being created. if loc.check(new_bor) {
- The new borrow is already active! This can happen because a mutable reference can be shared multiple times. We do not do anything else.
As a special exception, we do not reactivate
bor
even though it is “used”, because that would unfreeze the location!
} else {
- We might be creating a reference to a local variable. In that case,
loc.reset()
. Otherwise,reactivate(bor)
. initiate(new_bor)
}
- The new borrow is already active! This can happen because a mutable reference can be shared multiple times. We do not do anything else.
As a special exception, we do not reactivate
- Use
new_tag
for the new reference.
- Bump up the clock, and remember the old time as
- Any time a reference is passed to us from “the outside world” (as function argument, loaded from memory, or returned from a callee; also below structs or enums but not below unions or pointer indirectons), we retag.
- Bump up the clock, and remember the old time as
new_tag
. - Compute
new_bor
fromnew_tag
and the type of the reference being created. reactivate(bor)
.- If this is a function argument coming in:
loc.borrows.push(FnBarrier(stack_height))
. initiate(new_bor)
. Note that this is a NOP ifnew_bor
is already active – in particular, if the location is frozen and this is a shared reference with interior mutability, we do not push anything on top of the barrier. This is important, because any reactivation that unfreezes this location must lead to UB while the barrier is still present.- Change reference tag to
new_tag
.
- Bump up the clock, and remember the old time as
- Any time a raw pointer is created from a reference, we might have to do a raw reborrow.
reactivate(bor)
.initiate(Mut(Raw))
. This is a NOP when coming from a shared reference.
- Any time a function returns, we have to clean up the barriers.
- Iterate over all of memory and remove the matching
FnBarrier
. This is where the “stack” becomes a bit of a lie, because we also remove barriers from the middle of a stack.
This could be optimized by adding an indirection, so we just have to record somewhere that this function call has ended.
- Iterate over all of memory and remove the matching
- Any time memory is deallocated, this counts as mutation, so the usual rules for that apply. After that, the stacks of the deallocated bytes must not contain any barriers.
If you want to test your own understanding of “Stacked Borrows”, I invite you to go back to Section 2.2 of “Types as Contracts” and look at the three examples here.
Ignore the Validate
calls, that part is no longer relevant.
These are examples of optimizations we would like to be valid, and in fact all three of them are still valid with “Stacked Borrows”.
Can you argue why that is the case?
Summary
I have described (yet) another Rust memory model that defines when a reference may be used to perform which memory operations. The main design constraint of this model is that lifetimes should not matter for program execution. To my own surprise, the model actually ended up being fairly simple, all things considered.
I think I covered most of the relevant features, though I will have to take a closer look at two-phase borrows and see if they need some further changes to the model.
Of course, now the big question is whether this model actually “works” – does it permit all the code we want to permit (does it even permit all safe code), and does it rule out enough code such that we can get useful optimizations? I hope to explore this question further in the following weeks by implementing a dynamic checker to test the model on real code. It is just easier to answer these questions when you do not have to manually reevaluate all examples after every tiny change. However, I can always use more examples, so if you think you found some interesting or surprising corner case, please let me know!
As always, if you have any questions or comments, feel free to ask in the forums.
Posted on Ralf's Ramblings on Aug 7, 2018.
Comments? Drop me a mail or leave a note in the forum!