Skip to content

Commit c3aec74

Browse files
Tree Borrows MD
1 parent be789e6 commit c3aec74

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

wip/tree-borrows.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Tree Borrows
2+
3+
**Note:** This document is not normative nor endorsed by the UCG WG. It is maintained by @RalfJung and @JoJoDeveloping to reflect what is currently implemented in Miri.
4+
5+
This is not a guide! See the [Tree Borrows paper](https://plf.inf.ethz.ch/research/pldi25-tree-borrows.html) for more information.
6+
7+
Changes since publication of the paper:
8+
9+
* Interior-Mutable shared references are no longer treated like raw pointers, instead they use the new `Cell` permission. This permission allows all foreign and local accesses.
10+
* Mirroring Stacked Borrows, structs which contain an UnsafeCell now have that UnsafeCell's position tracked more finely-grained. It is no longer sufficient to just have an UnsafeCell somewhere in a struct to mark this as being interior-mutable everyhwere.
11+
12+
## MiniRust
13+
14+
Tree Borrows is documented in [MiniRust](https://github.com/minirust/minirust/tree/master/spec/mem/tree_borrows). MiniRust is written as literate code and should be readable without further explanation.
15+
16+
Instead of yet again defining Tree Borrows in prose here, we refer to this.
17+
18+
19+
### High-level summary
20+
Tree Borrows maintains a tree for each allocation. Each pointer has a tag, that identifies a node in this tree.
21+
Each node, for each offset/byte in the allocation, tracks a permission. The permission is per-byte, i.e. each byte has its own independent permission.
22+
The permission evolves according to a state machine, which depends on the access (read/write), the relation between accessed and affected node (local/foreign), the current state, and whether the current node is protected by a protector.
23+
24+
There is also an "initialized" tracking which makes protectors behave different on offsets "out of bounds" of a retag, that have not yet been accessed.
25+
These differences are not reflected in the state machines in the paper, we refer to the MiniRust implementation for the full details.
26+
27+
28+
### Differences between MiniRust and Miri
29+
MiniRust includes an idealized implementation of Tree Borrows. In particular, it models provenance/tags as tree addresses, which uniquely identify a node in the borrow tree. Miri however uses unique IDs, with the Tree being tracked more implicitly has relations between these IDs. This is an implementation detail and not relevant for understanding the semantics.
30+
Besides this representation difference, Miri also includes a number of optimizations that make Tree Borrows have acceptable performance. These include
31+
* skipping nodes based on past foreign accesses, exploiting idempotence properties in the state machine
32+
* garbage collection of unused references, which allows shrinking trees
33+
* skipping nodes based on the permissions found there
34+
35+
## Concepts Inherited From Stacked Borrows
36+
37+
### Retags
38+
Tree Borrows has retags happen in the same place as Stacked Borrows. But note that Tree Borrows treats raw pointer retags as NOPs, i.e. it does not attempt to track these.
39+
40+
### Protectors
41+
Like Stacked Borrows, Tree Borrows has protectors. These serve to ensure that references remain live throughout a function. Like Stacked Borrows, Tree Borrows has strong and weak protectors, and it protects the same values the same way.
42+
43+
### Implicit Reads and Writes
44+
Like Stacked Borrows, Tree Borrows performs implicit accesses as part of retags. Unlike Stacked Borrows, these are always reads, even for `&mut` references.
45+
46+
Also unlike Stacked Borrows, Tree Borrows has implicit accesses happen on protector end. These can be writes. See the section on "protector end semantics" in the paper for more info.
47+
48+
### UnsafeCell tracking
49+
Like Stacked Borrows, Tree Borrows tracks where there are UnsafeCells, and treats these offsets differently from other offsets. UnsafeCells are tracked in structs and tuple fields, but enums are not inspected further.
50+
51+
### Accesses
52+
Besides for the aforementioned differences in the handling of retags, what counted as a read or write in Stacked Borrows also counts as a read or write in Tree Borrows. These places are not surprising.
53+
54+
## Imprecisions
55+
56+
The following is a list of things that are _not_ UB in Tree Borrows. Some people want to make these things UB, so that more optimizations become possible. This is currently undecided and might just happen. In particular, all things listed here are already UB in Stacked Borrows.
57+
58+
* Tree Borrows does _not_ have subobject provenance, meaning that retags do not shrink the set of offsets that a reference can be used to access.
59+
* Tree Borrows does not initially consider `&mut` references writable, it only does so after the first write. In practice, this might mean that optimizations moving writes up above the first write are forbidden.
60+
61+
## Other problems
62+
* The interaction of protector end writes with the data race model is not fully thought out.
63+
* Finding a good model of exposed provenance in Tree Borrows (that does not use angelic nondeterminism) is an open research question. Until then, Tree Borrows does not support `-Zmiri-permissive-provenance`

0 commit comments

Comments
 (0)