Sub topic 10
Aliasing violations under Stacked Borrows and Tree Borrows
This chapter assumes you know Rust references and raw pointers, and that you can run cargo miri test. You will see how Miri detects aliasing violations, read its report, and switch between its two aliasing models.
What an aliasing rule is
Rust promises that a &mut T is the only way to reach its data while the reference is in use. The compiler relies on this promise to optimize. If unsafe code breaks it, the result is undefined behavior, even if the program prints the right answer natively.
Miri checks this promise with an aliasing model. A model is a set of rules for which pointers may access a location at a given time. Miri ships two models.
A violation to run
Creating a new mutable reference to x invalidates the raw pointer made earlier from x. Writing through that raw pointer afterwards breaks the rules.
fn main() {
let mut x = 10;
let raw = &mut x as *mut i32;
let r = &mut x;
unsafe { *raw = 20; }
*r += 1;
}Run it with cargo +nightly miri run. Miri stops at the write through raw and reports undefined behavior. The report says the pointer's permission was removed, and it points to three places: where the tag was created, where it was invalidated, and where the bad access happened.
Stacked Borrows
Stacked Borrows is the default model. For each memory location, Miri keeps a stack of permissions. Each pointer derived from a location gets a tag, and the stack records which tags may still access it.
- Using a pointer requires its tag to be on the stack.
- A write through one tag pops every tag above it. Those pointers can no longer be used.
- Creating
r = &mut xpushes a new tag on top ofraw's tag. The next use ofr's parent chain is fine, but the write throughrawhappens whileris still live and is considered an access that conflicts with it.
When you see "tag does not exist in the borrow stack" in a report, a pointer was used after something removed it from the stack.
Tree Borrows
Tree Borrows is the newer, experimental model. It keeps a tree of pointers instead of a stack, where each child pointer descends from the pointer it was derived from. Each node has a state, such as active, frozen or disabled, and accesses change the states.
Tree Borrows accepts some patterns that Stacked Borrows rejects, so it is often less strict. Report text differs too. You will see messages about a tag being "disabled" or "frozen" instead of a missing stack entry.
Enable it with MIRIFLAGS:
MIRIFLAGS="-Zmiri-tree-borrows" cargo +nightly miri testWhich model to use
| Question | Stacked Borrows | Tree Borrows |
|---|---|---|
| Status | Default | Opt in with `-Zmiri-tree-borrows` |
| Data structure | Stack per location | Tree of pointers |
| Strictness | Stricter | Accepts more patterns |
How a violation arises
The usual fix
Derive pointers in a consistent order, and stop using an old pointer after a newer one is created. In the example, create r first and derive the raw pointer from r, or use only the raw pointer and never create r.
Exercise
In the example, why does writing through raw count as undefined behavior even though both pointers refer to the same i32?
Show answer
Creating r = &mut x invalidates raw, because a &mut must be the only live path to the data. After that, raw has lost its permission, so a write through it violates the aliasing rules.
Exercise
A test fails under Miri's default model. What flag do you set to check it with Tree Borrows, and what does a pass under Tree Borrows alone tell you?
Show answer
Set MIRIFLAGS="-Zmiri-tree-borrows". A pass under Tree Borrows alone means the code satisfies the more permissive model, but it may still violate Stacked Borrows and may not be sound under the final Rust rules.