skip to content

A Rust optimisation creates two `&mut [u8; 8]` to one buffer from a raw pointer and the output is correct; why is it still undefined behaviour?

level: seniorimportance: should knowfreq 40%

answer

  1. uniqueness while live
  2. use after a newer reborrow
  3. the optimiser relies on it
  4. UnsafeCell excuses only &T
  5. split the borrow instead

basics

~20 s

A live &mut T must be the only reference to its memory. Using the first &mut after the second one was created and written breaks the aliasing rules, which is UB however the output looks; Miri reports it.

solid answer

~50 s

The Reference's aliasing rule says a `&mut T` must point to memory that no other reference points to, and that no pointer not derived from it reads or writes, while it is live. A reference is live each time it is used, so writing through `trailer` and then reading through `payload` means two overlapping `&mut` were live at once: UB, even though both came from the same raw pointer and the program prints the right bytes. The optimiser is entitled to assume `payload[7]` did not change and reuse an earlier value. Miri flags it under both Stacked Borrows and Tree Borrows. `UnsafeCell` does not help, because it relaxes only the immutability of `&T`, never the uniqueness of `&mut`. The fix is to split the buffer with `split_at_mut`, or to do all access through the raw pointer without creating references.

code

rust · 15 lines
rust
fn checksum_in_place(buf: &mut [u8; 8]) {
    let p: *mut [u8; 8] = buf;
    // "Optimisation": one &mut to read the payload, one to write the checksum.
    let payload: &mut [u8; 8] = unsafe { &mut *p };
    let trailer: &mut [u8; 8] = unsafe { &mut *p };
    let sum = payload[..7].iter().fold(0u8, |a, b| a.wrapping_add(*b));
    trailer[7] = sum;
    payload[0] = payload[7]; // payload is used again after trailer wrote
}

fn main() {
    let mut buf = [1u8, 2, 3, 4, 5, 6, 7, 0];
    checksum_in_place(&mut buf);
    println!("{buf:?}");
}

go deeper

for a junior

Recall that a &mut must be the only reference to its memory while it is in use, and that unsafe code is bound by this too.

for a middle

Explain liveness: a reference counts as live each time it is used, so the order of uses decides whether two &mut overlap.

for a senior

Read a Miri aliasing report, know why Stacked and Tree Borrows can disagree, and rewrite the code with split_at_mut or raw-pointer-only access.

for a principal

Prefer code that passes the stricter reading and both Miri models, since the official aliasing rules are still undecided and may tighten.

## The rule for `&mut T` The Rust Reference lists **breaking the pointer aliasing rules** as undefined behaviour and outlines them: - a **`&T`** must point to memory that is **not mutated while it is live**, except for data inside an `UnsafeCell<U>`; - a **`&mut T`** must point to memory that is **not read or written by any pointer not derived from it**, and that **no other reference points to**, while it is live — *with no exceptions*; - `Box<T>` is treated much like a `&'static mut T`. **Live** is not the same as "in scope". The Reference says a reference is live each time it is dereferenced or reborrowed, and each time it is passed to or returned from a function; its liveness can never exceed the lifetime the borrow checker gave it. The exact rules are explicitly "not determined yet", which is why Miri implements candidate models. ## The snippet `checksum_in_place` takes one `&mut [u8; 8]`, turns it into a raw pointer, and creates two `&mut [u8; 8]` from that pointer: `payload` to sum bytes 0..7 and `trailer` to write the checksum into byte 7. It then reads `payload[7]` again. In a normal build it prints `[28, 2, 3, 4, 5, 6, 7, 28]`, the intended result. But at the last line, `payload` is used after `trailer` was created and written. Both cover all eight bytes, so two `&mut` to the same memory were live at once. The compiler is allowed to assume `payload` is unique, and therefore that nothing changed `payload[7]` behind its back; it may load the value early, keep it in a register, or reorder the store. Today's build happens not to, which is luck. Miri catches it under both models: | Model | Where Miri stops | Why | |---|---|---| | Stacked Borrows (default) | the first use of `payload` | creating `trailer` was a Unique retag over the whole range, which removed `payload` from the borrow stack | | Tree Borrows (`-Zmiri-tree-borrows`) | `payload[7]` on the last line | `trailer`'s write to byte 7 was a foreign write that moved `payload` to `Disabled` for that byte | The difference is instructive. A variant that touches **disjoint** bytes through the two references passes Tree Borrows and fails Stacked Borrows, because Tree Borrows tracks permissions per byte. The Reference's outline — no other reference may point to the memory — is stricter than either. Relying on the permissive model is relying on a question that is still open. ## Why `UnsafeCell` is not the escape `UnsafeCell<T>` is the **one exception** the Reference makes to the immutability of data behind a live `&`: `UnsafeCell::get` returns a `*mut T`, and writes through it are allowed while other `&UnsafeCell<T>` exist, provided you prevent conflicting accesses yourself. Its documentation is explicit about the limit: 1. it affects **only the immutability guarantee of shared references**; 2. the **uniqueness guarantee of `&mut` is unaffected** — aliasing `&mut UnsafeCell<T>`, or aliasing `&mut` of any type, is still UB; 3. it does nothing about data races between threads. Without `UnsafeCell`, writing through a pointer cast from a `&T` is UB under the immutability rule; rustc rejects the obvious form at compile time with the deny-by-default lint `invalid_reference_casting` ("assigning to `&T` is undefined behavior, consider using an `UnsafeCell`"), and Miri catches the forms the lint misses. The second snippet shows the legal pattern. How `Cell` and `RefCell` are built on this primitive is covered with interior mutability. ## Fixing the optimisation - **Split the borrow.** `buf.split_at_mut(7)` returns two non-overlapping `&mut [u8]`, which is exactly what the code wanted; the standard library's implementation is the textbook sound way to produce them, discussed in the lesson on sound abstractions. - **Stay in raw pointers.** If references must be avoided, do every read and write through `p` (with `ptr::read`/`ptr::write` or `*p.add(i)`), and create no `&mut` that outlives the next raw access. - **Reborrow in sequence.** Finish with one `&mut` before deriving the next, so their live ranges never overlap. After the fix, run the test under Miri with both models; it should pass both.

  • Is it UB to create a second `&mut` from a raw pointer if the first is never used again?
    Not under the usual reading: a reference is live when it is used, so once the first `&mut` is dead, deriving a new one from the raw pointer is the normal pattern, and `split_at_mut`'s own implementation relies on the original reference being dead. The problem in this snippet is the use of `payload` after `trailer` was created and written.
  • Can you cast a `&T` to `*mut T` and write through it if no one else reads the value?
    No. The bytes behind a live shared reference are immutable unless they are inside an `UnsafeCell`, and any write counts as a mutation even if it stores the same bytes. rustc's deny-by-default `invalid_reference_casting` lint rejects the direct form; Miri reports the indirect ones. Use `UnsafeCell`, `Cell` or a `&mut` from the start.

  • Says two &mut are fine if they come from the same raw pointer.
  • Thinks the rule only matters across threads, not in single-threaded code.
  • Believes wrapping the buffer in UnsafeCell makes aliasing &mut legal.
  • Treats correct output in a release build as proof the aliasing is harmless.
  • Assumes passing under Tree Borrows settles the question.