Aug 7, 2018 • Internship, RustEditsPermalink

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.

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:

fn demo0(x: &mut i32) -> i32 {
  // At the beginning of the function, `x` must be the "active" reference
  // for the 4 locations it points to, meaning `Uniq(x)` is at the top of the stack.
  // (It's 4 locations because `i32` has size 4.)
  let y = &mut *x; // Now `Uniq(y)` is pushed onto the stack, as new active reference.
  // The stack now contains: Uniq(y), Uniq(x), ...
  *y = 5; // Okay because `y` is active.
  *x = 3; // This "activates" `x` by popping the stack.
  // The stack now contains: Uniq(x), ...
  *y // This is UB! `Uniq(y)` is not on the stack of borrows, so `y` must not be used.
}

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:

fn demo1(x: &mut i32) -> i32 {
  // At the beginning of the function, `x` must be the "active" reference.
  let raw = x as *mut _; // Create raw pointer
  // The stack now contains: Raw, Uniq(x), ...
  let y = unsafe { &mut *raw }; // Now `y` is pushed onto the stack, as new active reference.
  // The stack now contains: Uniq(y), Raw, Uniq(x), ...
  *y = 5; // Okay because `y` is active.
  *x = 3; // This "activates" `x` by popping the stack twice.
  *y // This is UB! `Uniq(y)` is not on the stack of borrows, so `y` must not be used.
}

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:

fn demo2(x: &mut i32) -> i32 {
  // At the beginning of the function, `x` must be the "active" reference.
  let raw = x as *mut _; // Create raw pointer
  // The stack now contains: Raw, Uniq(x), ...
  let y = unsafe { &mut *raw }; // Now `y` is pushed onto the stack, as new active reference.
  // The stack now contains: Uniq(y), Raw, Uniq(x), ...
  *y = 5; // Okay because `y` is active.
  unsafe { *raw = 5 }; // Using a raw pointer, so `Raw` gets activated by popping the stack!
  // The stack now contains: Raw, Uniq(x), ...
  *y // This is UB! `Uniq(y)` is not on the stack of borrows, so `y` must not be used.
}

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:

fn demo3(x: &mut i32) -> i32 {
  // At the beginning of the function, `x` must be the "active" reference.
  let raw = x as *mut _; // Create raw pointer
  // The stack now contains: Raw, Uniq(x), ...
  let y = unsafe { & *raw }; // Now memory gets frozen (recording the timestamp)
  let _val = *y; // Okay because memory was frozen since `y` was created
  *x = 3; // This "activates" `x` by unfreezing and popping the stack.
  let z = unsafe { & *raw }; // Now memory gets frozen *again*
  *y // This is UB! Memory has been frozen strictly after `y` got created.
}

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 for all locations that it covers; this can involve unfreezing and popping the stack. If it is not possible to activate the reference this way, we have UB.

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 two 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:

fn demo4(x: &mut i32, y: &mut i32) -> i32 {
  *x = 42;
  *y = 7;
  *x // Will load 42! We can optimize away the load.
}

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 activate 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:

fn demo5(x: &mut i32, y: usize) {
  *x = 42;
  foo(y);
}

fn foo(y: usize) {
  let y = unsafe { &mut *(y as *mut i32) };
  *y = 7;
}

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 activated.

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 activated 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 activated.

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 activate 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 activate Uniq(y), but that can only be behind the barrier, so it cannot be activated.

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:

/// Information about a potentially mutable borrow
enum Mut {
  /// A unique, mutable reference
  Uniq(Timestamp),
  /// Any raw pointer, or a shared borrow with interior mutability
  Raw,
}
/// Information about any kind of borrow
enum Borrow {
  /// A mutable borrow, a raw pointer, or a shared borrow with interior mutability
  Mut(Mut),
  /// A shared borrow without interior mutability
  Frz(Timestamp)
}
/// An item in the borrow stack
enum BorStackItem {
  /// Defines which references are permitted to mutate *if* the location is not frozen
  Mut(Mut),
  /// A barrier, tracking the function it belongs to by its index on the call stack
  FnBarrier(usize)
}

struct MemoryByte {
  borrows: Vec<BorStackItem>, // used as a stack
  frz_since: Option<Timestamp>,
  /* More fields, to store the actual value and what else might be needed */
}

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.

impl MemoryByte {

  /// Check if the given borrow may be used on this location.
  fn check(&self, bor: Borrow)  bool {
    match bor {
      Frz(acc_t) =>
        // Must be frozen at least as long as the `acc_t` says.
        self.frz_since.map_or(false, |loc_t| loc_t <= acc_t),
      Mut(acc_m) =>
        // Raw pointers are fine with frozen locations. This is important because &Cell is raw!
        (acc_m.is_raw() && self.frozen_since.is_some()) ||
        self.borrows.last().map_or(false, |loc_itm| loc_itm == Mut(acc_m)),
    }
  }

  /// Activate the given existing borrow for this location, fail if that is not possible.
  fn activate(&mut self, bor: Borrow) {
    // Do NOT change anything if `bor` is already active -- in particular, if
    // it is a `Mut(Raw)` and we are frozen.
    if self.check(bor) { return; }
    let acc_m = match bor {
      Frz(acc_t) => bail!("Location should be frozen but it is not"),
      Mut(acc_m) => acc_m,
    };
    // We definitely have to unfreeze this, even if we use the topmost item.
    self.frozen_since = None;
    // Pop until we see the one we are looking for.
    while let Some(itm) = self.borrows.last() {
      match itm {
        FnBarrier(_) => {
          bail!("Trying to activate a borrow that lives behind a barrier");
        }
        Mut(loc_m) => {
          if loc_m == acc_m { return; }
          self.borrows.pop();
        }
      }
    }
    bail!("Borrow-to-activate does not exist on the stack");
  }

  /// Initiate the given (new) borrow for the location.
  /// This is "pushing to the stack", except that it also handles initiating a `Frz`.
  fn initiate(&mut self, bor: Borrow) {
    match bor {
      Frz(t) => {
        if self.frozen_since.is_none() {
          self.frozen_since = Some(t);
        }
      }
      Mut(m) => {
        if m.is_uniq() && self.frozen_since.is_some() {
          bail!("Must not initiate Uniq when frozen!");
        }
        self.borrows.push(Mut(m));
      }
    }
  }

  /// Reset the borrow tracking for this location.
  fn reset(&mut self) {
    if self.borrows.iter().any(|itm| if let FnBarrier(_) = item { true } else { false }) {
      assert!("Cannot reset while there are barriers");
    }
    self.frozen_since = None;
    self.borrows.clear();
  }
  
}

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.

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!