add Stacked Borrows ToC to latest post and expand Stacked Borrows 1 post to hopefully...
[web.git] / ralf / _posts / 2019-04-30-stacked-borrows-2.md
1 ---
2 title: "Stacked Borrows 2"
3 categories: rust research
4 forum: https://internals.rust-lang.org/t/stacked-borrows-2/9951
5 ---
6
7 Recently, I have [significantly updated](https://github.com/rust-lang/miri/pull/695) Stacked Borrows in order to fix some issues with the handling of shared references that were uncovered in the previous version.
8 In this post, I will describe what the new version looks like and how it differs from [Stacked Borrows 1]({% post_url 2018-11-16-stacked-borrows-implementation %}).
9 I assume some familiarity with the prior version and will not explain everything from scratch.
10
11 <!-- MORE -->
12
13 ## Stacked Borrows: Table of Contents
14
15 Before we start, let me give a brief overview over the posts on Stacked Borrows that I have written so far.
16 I didn't plan this out in advance, so things are a bit more messy than I would like.
17
18 * [Stacked Borrows: An Aliasing Model For Rust]({% post_url 2018-08-07-stacked-borrows %}) is the first post of the series and describes my initial ideas of what Stacked Borrows would look like before I started implementing them. It might be interesting for some of the context it gives, but is largely superseded by the next post.
19 * [Stacked Borrows Implemented]({% post_url 2018-11-16-stacked-borrows-implementation %}) describes Stacked Borrows 1 and my experience implementing it. It is self-contained; I was not happy with some of my explanations in the original post so I decided to give it another shot. This is the best post to start with, if you are catching up.
20 * [Barriers and Two-phase Borrows in Stacked Borrows]({% post_url 2018-12-26-stacked-borrows-barriers %}) describes how I extended Stacked Borrows 1 with partial support for two-phase borrows and explains the idea of "barriers". You do not have to have read that post to understand Stacked Borrows 2, except for the parts that specifically refer to barriers and two-phase borrows.
21
22 ## The problem
23
24 The problem I wanted to solve with Stacked Borrows 2 was that the first version of Stacked Borrows only performed very little tracking of shared references.
25 My thinking was, if the location is read-only anyway, then it does not harm to grant anyone read access.
26 However, [as @arielby noted](https://github.com/rust-lang/unsafe-code-guidelines/issues/87), this leads to loss of optimization potential in cases where a function receives a mutable reference (which is supposed to have no aliases) and then creates a shared reference from it:
27 {% highlight rust %}
28 fn main() {
29     let x = &mut 0u32;
30     let p = x as *mut u32;
31     foo(x, p);
32 }
33
34 fn foo(a: &mut u32, y: *mut u32) -> u32 {
35     *a = 1;
36     let _b = &*a; // This freezes `x`. Frozen locations can be read by any raw pointer.
37     unsafe { *y = 2; } // Hence, this legal in Stacked Borrows.
38     return *a; // But we want to optimize this to always return 1!
39 }
40 {% endhighlight %}
41 Stacked Borrows 1 allowed any raw pointer to read a frozen location.
42 Basically, once a location is frozen we do not keep track of which pointer is allowed to read; we just allow all pointers to read.
43 However, that means that `&*a` above (reborrowing a mutable reference as a shared reference) has the unintended side-effect of permitting raw pointers to access `*a`!
44 This violates the idea that `a` is a unique pointer: we'd like to assume that `foo` can only ever return `1`, because `y` cannot possibly alias with `a`.
45 Hence calling `foo` like we do here with `a` and `y` pointing to the same thing must be undefined behavior---and yet, Stacked Borrows 1 considered the example above to be legal.
46
47 ## The solution
48
49 To fix this, I had to replace the mechanism of "freezing" by something else.
50 Remember that in Stacked Borrows 1, when a shared reference was created, we stored the timestamp at which that happened and also recorded in memory that the location this reference points to is frozen since now.
51 Whenever a shared reference gets used, we check that the location is frozen *at least* since our reference got created.
52 This is in contrast to mutable references, where we require the exact unique ID of that reference to be present in the borrow stack.
53
54 To rule out cases like the example above, instead of freezing a location and allowing *all* shared reference created henceforth to access this location, we keep track precisely of *which* shared references are allowed access.
55 The per-location borrow stack now consists of items that grant a particular *permission* to a pointer identified by a particular *tag*:
56 {% highlight rust %}
57 pub struct Item {
58     /// The pointers the permission is granted to.
59     tag: Tag,
60     /// The permission this item grants.
61     perm: Permission,
62 }
63
64 pub enum Permission {
65     /// Grants unique mutable access.
66     Unique,
67     /// Grants shared mutable access.
68     SharedReadWrite,
69     /// Greants shared read-only access.
70     SharedReadOnly,
71 }
72 {% endhighlight %}
73 There is no longer a separate "frozen" state; keeping shared references read-only is now handled by the borrow stack itself:
74 {% highlight rust %}
75 pub struct Stack {
76     borrows: Vec<Item>
77 }
78 {% endhighlight %}
79 The *tag* is also simpler than it was before: there are no longer separate tags for mutable and shared references.
80 {% highlight rust %}
81 pub type PtrId = NonZeroU64;
82 pub enum Tag {
83     Tagged(PtrId),
84     Untagged,
85 }
86 {% endhighlight %}
87 Just like before, a new `Tag` gets picked on every retagging; in particular whenever a reference gets created with `&mut <expr>`/`& <expr>` and when a reference gets cast to a raw pointer.
88 Other than that (like on pointer arithmetic), the tag just gets propagated to keep track of where this pointer comes from.
89 However, unlike before, the only difference between mutable and shared references is the permissions that are associated with that tag.
90
91 ### Memory accesses
92
93 But before we go into creation of new references, let us look at how permissions in the borrow stack affect what happens at a memory access is legal.
94 So assume that some pointer tagged `tag` is used to either read from or write to memory.
95 For each affected location, we go through two steps: first we try to find the *granting item*, then we remove *incompatible items*.
96
97 **Finding the granting item.**
98 To find the granting item, we traverse the borrow stack top-to-bottom and search for an item that has the same tag.
99 If this is a write access, we go on looking until we find an item with the right tag whose permission is `SharedReadWrite` or `Unique`---we do not consider items with `SharedReadOnly` permission to grant a write access.
100 This is the item that grants pointers tagged `tag` the permission to perform the given access, hence the name (the same concept used to be called "matching item" in Stacked Borrows 1).
101 Once we found the granting item, we remember its position in the stack; that will be important for the second step.
102 If no granting item can be found, the access causes undefined behavior.
103
104 For example, if the stack is
105 ```
106 [ Item { tag: Tagged(0), perm: Unique },
107   Item { tag: Tagged(1), perm: SharedReadOnly} ]
108 ```
109 then a read access with tag `Tagged(1)` is granted by the item at index 1; a read/write access with tag `Tagged(0)` is granted by the item at index 0, but a write access with tag `Tagged(1)` is not granted by any item (and thus not allowed).
110 Just to keep things shorter and easier to read, in the following I will use a short-hand syntax for writing down items, so the above stack would look as follows:
111 ```
112 [ (0: Unique), (1: SharedReadOnly) ]
113 ```
114
115 To consider a second example, if the stack is
116 ```
117 [ (0: Unique), (Untagged: SharedReadWrite), (Untagged: SharedReadOnly) ]
118 ```
119 then a read access with an `Untagged` pointer is granted by the item at index 2, but a write access with an `Untagged` pointer is only granted by the item at index 1.
120
121 **Removing incompatible items.**
122 In the second step, we traverse all the items *above* the granting item in the stack, and see if they are compatible with this access.
123 This realizes the idea (already present in the original Stacked Borrows) that using one pointer disallows future uses of another pointer.
124 For example, if the current stack is
125 ```
126 [ (0: Unique), (1: Unique) ]
127 ```
128 then doing any kind of access with a `Tagged(0)` pointer should remove the `1: Unique` item from the stack.
129 This matches the part of Stacked Borrows 1 where items got popped off the stack until the granting item is at the top.
130 We say that the granting `Unique` permission is *incompatible* with the `Unique` permission of the item higher up the stack, and hence the latter must be removed.
131
132 However, in the new model, we don't always remove *all* items that are above the granting item:
133 ```
134 [ (0: Unique), (1: SharedReadOnly), (2: SharedReadOnly) ]
135 ```
136 In a situation like this, there are two pointers that may be used for reading, `Tagged(1)` and `Tagged(2)`.
137 Using either of them should not have an impact on the other one, and the fact that their items are in a particular order on the stack has no impact.
138 In other words, a granting `SharedReadOnly` permission is *compatible* with other `SharedReadOnly` permissions, and hence when using a `SharedReadOnly` permission to grant an access, other `SharedReadOnly` permissions above it are maintained.
139
140 Sometimes, being compatible depends on the kind of access that was performed:
141 in our last example stack, if we *write* with a `Tagged(0)` pointer, that should remove the `SharedReadOnly` tags because writing to a unique pointer should make sure that that pointer is at the top of the stack.
142 In other words, on a write, a `Unique` permission is incompatible with everything.
143 However, if we just *read* with a `Tagged(0)` pointer, that's fine!
144 This is to allow [safe code like this](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d9aa504b1be72a0f55fb5a744cba69ff) that interleaves reads from aliasing mutable and shared references.
145 To enable this, on a read, a `Unique` permission is only incompatible with other `Unique` permissions.
146
147 The following table fully documents which permissions are compatible with which other permissions on a read or write access, respectively:
148
149 |↓ **Granting item** \ **Compatible with** → | `Unique`  | `SharedReadWrite`   | `SharedReadOnly`  |
150 |---:|---|---|---|
151 |`Unique`                                    | no | only reads | only reads  |
152 |`SharedReadWrite`                           | no | yes  | only reads |
153 |`SharedReadOnly`                            | no | no  | only reads |
154
155 So, for example, if the granting item has `SharedReadWrite` permissions, then all `Unique` above it in the stack will be removed, and moreover on a write, all `SharedReadOnly` above it will be removed.
156 This makes sure that the pointers for which those `SharedReadOnly` permission grants read access cannot be used any more: those pointers assumed the location was read-only, so we must make sure that after a write, they become invalid.
157
158 The name "stack" is a bit of a misnomer at this point, because we do *not* follow a stack discipline when removing items: just because some item got removed due to incompatibility, does not mean all items above it will also get removed.
159 I will discuss [later][no-stack] why a strict stack discipline does not work.
160 But I felt it would be even more confusing to rename the entire thing at this point, and the order of elements in the "stack" still *does* matter, so I kept the name.
161
162 In Stacked Borrows 2, there is no more action occurring when a pointer gets dereferenced: the tags only matter for the actual access.
163 This simplification is possible because the rules for accesses are now more complicated than before, so there is no need for extra checks on the dereference operation.
164 (In particular, previously the dereference checks relied on knowing the Rust type at which the access happens for proper handling of interior mutability; but now all that information is encoded using `SharedReadWrite`/`SharedReadOnly` tags as we will see. Not relying on types any more here also [fixed an annoying issue around `UnsafeCell`](https://github.com/rust-lang/miri/issues/615).)
165
166 ### Retagging: when permissions get added
167
168 The framework of permissions in a stack with a notion of (in)compatibility that we have seen so far allows us to express some ideas like:
169
170 * "A `Unique` pointer will get invalid when any of its parent pointers get used."<br> That is realized by having "no" in the entire first column of our table: every access removes all `Unique` permissions above the granting item.
171 * "A `SharedReadOnly` pointer will get invalid when the location gets written to."<br> This gets realized by having "only reads" in the entire last column of the table and not granting write accesses with a `SharedReadOnly` permission. In addition to that, however, we *also* need to ensure that in the stack we never have a `Unique` or `SharedReadWrite` *on top* of a `SharedReadOnly`! In a nonsense stack like `[ (0: SharedReadOnly), (1: SharedReadWrite) ]` we could write with a `Tagged(1)` pointer without invalidating the `Tagged(0)` pointers.
172
173 Now the question is, how to we *use* this "language"?
174 We have to define which items and permissions get added to the borrow stack.
175 Just like before, this happens during retagging.
176 Before we discuss retagging in general, let us look at our motivating example and see how retagging works there in Stacked Borrows 2, and how this program is defined to cause UB:
177 {% highlight rust %}
178 fn main() {
179     let x = &mut 0u32;
180     Retag(x); // Tagged(0)
181     // Stack: [ (0: Unique) ]
182
183     let p = x as *mut u32;
184     Retag([raw] p); // Untagged
185     // Stack: [ (0: Unique), (Untagged: SharedReadWrite) ]
186
187     foo(x, p);
188 }
189
190 fn foo(a: &mut u32, y: *mut u32) -> u32 {
191     Retag(a); // Tagged(1)
192     // Stack: [ (0: Unique), (1: Unique) ]
193
194     *a = 1;
195
196     let _b = &*a;
197     Retag(_b); // Tagged(2)
198     // Stack: [ (0: Unique), (1: Unique), (2: SharedReadOnly) ]
199
200     // UB: y is Untagged, and there is no granting item in the stack!
201     unsafe { *y = 2; }
202
203     return *a;
204 }
205 {% endhighlight %}
206 Initially, `x` with tag `Tagged(0)` is the only reference, and the stack says that this is the only pointer with any kind of permission.
207 Next, we cast `x` to a raw pointer.
208 The raw retagging of `p` turns `p` into an `Untagged` pointer, and adds a new item granting thusly tagged pointers `SharedReadWrite` permission.
209 (Really, in the MIR it will say `&mut *x as *mut u32`, so there will be an additional `Unique` permission for the temporary mutable reference, but that makes no difference and I hope [we will change that eventually](https://github.com/rust-lang/rfcs/pull/2582).)
210
211 Then `foo` gets called, which starts with the usual retagging of all reference arguments.
212 `a` is originally `Tagged(0)`, and retagging a mutable reference acts like an access (just like in Stacked Borrows 1), so the first thing that happens is that all items above the granting item that are incompatible with a write access for a `Unique` permission get removed from the stack.
213 In our case, this means that the `Untagged: SharedReadWrite` gets removed.
214 Then, `a` gets the new tag `Tagged(1)` and `1: Unique` gets pushed on top of the stack.
215 Nothing interesting happens when writing to `a`.
216 When `_b` gets created, it gets assigned the new tag `Tagged(2)`, and a new item `2: SharedReadOnly` is pushed to the stack.
217 As we can see, shared and mutable references no longer differ in the tag they carry; the only difference is what kind of permission they get granted.
218 (I think this is a nice improvement from Stacked Borrows 1, where shared references had a different kind of tag. In particular, transmutes between mutable references and shared pointers no longer need any kind of special treatment.)
219 Finally, we come to the interesting point: the program writes to `y`.
220 That pointer is `Untagged`, and there is no item granting any access to such pointers, and thus the access is UB!
221 This is in contrast to Stacked Borrows 1, where instead of `2: SharedReadOnly` we set a special "frozen" marker on this location that would, as a side-effect, also grant untagged pointers read-only access.
222
223 In general, during retagging, we start with some original tag used by our "parent" pointer, and we have a fresh tag `N` that will be used for the new pointer.
224 We have to decide which permission to grant so this tag, and where in the "stack" to insert it.
225 (We will not always insert new items at the top---again, as we will see following a strict stack discipline does not work.)
226 All of this happens for every location that the reference covers (as determined by its type).
227
228 **Retagging a mutable reference.**
229 The simplest case of retagging is handling mutable references:
230 just like in Stacked Borrows 1, this starts by performing the actions of a write access with the parent tag.
231 `Unique` permissions are incompatible with anything on a write, so all items above the granting item get removed.
232 Then we add `N: Unique` on top of the stack to grant the new tag unique access to this location.
233
234 This encodes the idea that mutable references must be used in a well-nested way---if you just consider mutable references, Stacked Borrows 2 still follows a stack discipline.
235
236 **Retagging a shared reference.**
237 When retagging a shared reference, we have to be mindful of `UnsafeCell`.
238
239 Outside of `UnsafeCell`, we start by performing the actions of a read access with the parent tag.
240 Then we push `N: SharedReadOnly` on top of the borrow stack.
241 This way, we grant the new pointer read-only access but make sure that it gets invalidated on the next write through any aliasing pointer (because all write-granting items are below us, and thus we test for compatibility when they get used).
242 There might be items left between the granting item and the one we just added, but that's okay: if any of them gets used for reading, that will be compatible with the `SharedReadOnly` according to our table above; and if any of them gets used for writing then it is important that our `SharedReadOnly` gets removed.
243
244 When we are inside an `UnsafeCell`, we will *not* perform the actions of a memory access!
245 Interior mutability allows all sorts of crazy aliasing, and in particular, one can call a function with signature
246 {% highlight rust %}
247 fn aliasing(refcell: &RefCell<i32>, inner: &mut i32)
248 {% endhighlight %}
249 such that `inner` points *into* `refcell`, i.e., the two pointers overlap!
250 Retagging `refcell` must not remove the `Unique` item associated with `inner`.
251
252 So, when retagging inside an `UnsafeCell`, we *find* the write-granting item for the parent's tag, and then we add `N: SharedReadWrite` just above it.
253 This grants write access, as is clearly needed for interior mutability.
254 We cannot add the new item at the top of the stack because that would add it *on top of* the item granting `inner`.
255 Any write to `inner` would remove our item from the stack!
256 Instead, we make our item sit just on top of its parent, reflecting the way one pointer got derived from the other.
257
258 **Retagging a raw pointer.**
259 Retagging for raw pointers happens only immediately after a reference-to-raw-pointer cast.
260 Unlike in Stacked Borrows 1, retagging depends on whether this is a `*const T` or `*mut T` pointer---this is a departure from the principle that these two types are basically the same (except for variance).
261 I have recently learned that the borrow checker actually handles `*mut` and `*const*` casts differently; also see [this long comment](https://github.com/rust-lang/rust/issues/56604#issuecomment-477954315).
262 Given that the idea of Stacked Borrows is to start with what the borrow checker does and extrapolate to a dynamic model that also encompasses raw pointers, I felt that it makes sense for now to mirror this behavior in Stacked Borrows.
263 This is certainly not a final decision though, and I feel we should eventually have a discussion whether we should make the borrow checker and Stacked Borrows both treat `*const T` and `*mut T` the same.
264
265 When casting to a `*mut T`, we basically behave like in the above case for inside an `UnsafeCell` behind a shared reference: we find the write-granting item for our parent's tag, and we add `Untagged: SharedReadWrite` just on top of it.
266 The way compatibility is defined for `SharedReadWrite`, there can be many such items next to each other on the stack, and using any one of them will not affect the others.
267 However, writing with a `Unique` permission further up the stack *will* invalidate all of them, reflecting the idea that when writing to a mutable reference, all raw pointers previously created from this reference become invalid.
268
269 When casting to a `*const T`, we behave just like retagging for a shared reference `&T` (including being mindful of `UnsafeCell`).
270 There isn't really anything that a `*const T` can do that a shared reference cannot (both in terms of aliasing and mutation), so modeling them the same way makes sense.
271
272 ### Two-phase borrows
273
274 Stacked Borrows 1 [had some support for two-phase borrows]({% post_url 2018-12-26-stacked-borrows-barriers %}), but some advanced forms of two-phase borrows that used to be allowed by the borrow checker [could not be handled](https://github.com/rust-lang/rust/issues/56254).
275 With the additional flexibility of Stacked Borrows 2 and its departure from a strict stack discipline, it is possible to accept at least the known examples of this pattern that were previously rejected:
276 {% highlight rust %}
277 fn two_phase_overlapping1() {
278     let mut x = vec![];
279     let p = &x;
280     x.push(p.len());
281 }
282 {% endhighlight %}
283 Previously, when the implicit reborrow of `x` in `x.push` got executed, that would remove the item for `p` from the stack.
284 This happens as part of the implicit write access that occurs when a mutable reference gets retagged.
285 With Stacked Borrows 2, we can do something else:
286 when retagging a two-phase mutable borrow, we do *not* perform the actions of a write access.
287 Instead, we just find the write-granting item for our parent's tag, and then add `N: Unique` just above it.
288 This has the consequence that on the first write access to this new pointer, everything on top of it will be removed, but until then the existing item for `p` remains on the stack and can be used just as before.
289
290 Just like with Stacked Borrows 1, we then proceed by doing a *shared* reborrow of the parent's tag from `N`.
291 This way, the parent pointer can be used in the ways a shared reference can (including writes, if there is interior mutability) without invalidating `N`.
292 This is somewhat strange because we then have "parent tag - new tag - parent tag" on the stack in that order, so we no longer properly reflect the way pointers got derived from each other.
293 More analysis will be needed to figure out the consequences of this.
294
295 We should also try to fully understand the effect this "weak" form of a mutable reborrow (without a virtual write access) has on the optimizations that can be performed.
296 Most of the cases of Stacked Borrows violations I found in the standard library are caused by the fact that even just *creating* a mutable reference asserts that it is unique and invalidates aliasing pointers, so if we could weaken that we would remove one of the most common causes of UB caused by Stacked Borrows.
297 On the other hand, this means that the compiler will have a harder time reordering uses of mutable references with function calls, because there are fewer cases where a mutable reference is actually assumed to be unique.
298
299 ### Barriers are dead, long live protectors
300
301 With the departure from a strict stack discipline, I also had to re-think the concept of [barriers]({% post_url 2018-12-26-stacked-borrows-barriers %}).
302 The name was anyway terrible, so I replaced barriers by *protectors*: an `Item` actually consists not only of a `Tag` and a `Permission`, but also optionally of a "protector" represented as a `CallId` referencing some function call (i.e., some stack frame).
303 As long as that function call is running, the item with the protector may not be removed from the stack (or else we have UB).
304 This has pretty much the same effects as barriers did in Stacked Borrows 1.
305
306 ## Why not a strict stack discipline?
307 [no-stack]: #why-not-a-strict-stack-discipline
308
309 The biggest surprise for me in designing Stacked Borrows 2 was that I was not able to enforce a strict stack discipline any more.
310 For retagging, that is only partially surprising; really in Stacked Borrows 1 we already added barriers below the "frozen" marker sitting next to a stack, and the `aliasing` example with the `RefCell` mentioned above only worked due to a hack that relied on reusing existing items in the middle of the stack instead of pushing a new one.
311 However, I had initially hoped that the rules for memory accesses would be slightly different:
312 my plan was that after finding the granting item, we would seek upwards through the stack and find the first incompatible item, and then remove everything starting there.
313 That would be a stack discipline; we would basically pop items until all items above the granting item are compatible.
314
315 Unfortunately, that would reject many reasonable programs, such as:
316 {% highlight rust %}
317 fn test(x: &mut [i32]) -> i32 { unsafe {
318   let raw = x.as_mut_ptr(); // implicitly: as_mut_ptr(&mut *x)
319   let _val = x[1];
320   return *raw;
321 } }
322 {% endhighlight %}
323 The issue with this example is that when calling `as_mut_ptr`, we reborrow `x`.
324 So after the first line the stack would look something like (using `x` as notation for `x`'s tag)
325 ```
326 [ ..., (x: Unique), (_: Unique), (Untagged: SharedReadWrite) ]
327 ```
328 In other words, there would be some `Unique` item *between* the items for `x` and `raw`.
329 When reading from `x` in the second line, we determine that this `Unique` tag is not compatible.
330 This is important; we have to ensure that any access from a parent pointer invalidates `Unique` pointers---that's what makes them unique!
331 However, if we follow a stack discipline, that means we have to pop the `Untagged: SharedReadWrite` *and* the `_: Unique` off the stack, making the third line UB because the raw pointer got invalidated.
332
333 `test` seems like a perfectly reasonable function, and in fact this pattern is used in the standard library.
334 So in order to allow such code, accesses in Stacked Borrows 2 do *not* follow a stack discipline:
335 we remove all incompatible items above the granting item and ignore any interleaved compatible items.
336 As a consequence, line 2 in the above program removes `_: Unique` but keeps `Untagged: SharedReadWrite`, so line 3 is okay.
337
338 However, this means we also accept the following, even if we eventually do fully precise tracking of raw pointers:
339 {% highlight rust %}
340 fn main() { unsafe {
341   let x = &mut 0;
342   let raw1 = x as *mut _;
343   // Stack: [ (x: Unique), (raw1: SharedReadWrite) ]
344
345   let tmp = &mut *raw1;
346   let raw2 = tmp as *mut _;
347   // Stack: [ (x: Unique), (raw1: SharedReadWrite), (tmp: Unique), (raw2: SharedReadWrite) ]
348
349   *raw1 = 1; // This will invalidate tmp, but not raw2.
350   // Stack: [ (x: Unique), (raw1: SharedReadWrite), (raw2: SharedReadWrite) ]
351
352   let _val = *raw2;
353 } }
354 {% endhighlight %}
355 Naively I would assume that if we tell LLVM that `tmp` is a unique pointer, it will conclude that `raw2` cannot alias with `raw1` as that was not derived from `tmp`, and hence LLVM might conclude that `_val` must be 0.
356 This means the program above would have to have UB.
357 However, in the current framework I do not see a way to make it UB without also making the reasonable example from the beginning of this section UB.
358 So maybe we can find a way to express to LLVM precisely what we mean, or maybe we can only exploit some of these properties in home-grown optimizations, or maybe we find a way to have UB in the second example but not in the first.
359 (Like, maybe we do the stack-like behavior on writes but keep the more lenient current behavior on reads? That seems annoying to implement though. Maybe a stack is just the wrong data structure, and we should use something more tree-like.)
360
361 ## Conclusion
362
363 Stacked Borrows 2 shares with Stacked Borrows 1 just the general structure: pointers are tagged, and there is a per-location stack indicating which tags are allowed to perform which kinds of operations on this location.
364 In the new version, shared references are tagged the same way as mutable references (just with an ID to distinguish multiple references pointing to the same location); the stack keeps track of which IDs are read-only (shared) pointers and which are unique (mutable) pointers.
365 The only operations affected by Stacked Borrows 2 are memory accesses and the `Retag` instructions explicitly represented in the MIR; there is no longer any action on a pointer dereference.
366 This balances the extra complexity of the new access rules (the new implementation is actually a dozen lines shorter than the old, despite long comments).
367 The "stack" is unfortunately not used in a completely stack-like fashion though.
368
369 I leave it as an exercise to the reader to convince yourself that the [key properties of Stacked Borrows 1]({% post_url 2018-11-16-stacked-borrows-implementation %}#5-key-properties) still hold, and that uniqueness of mutable references is maintained even if we do a shared reborrow, as long as we keep that shared reference to ourselves.
370
371 The new model gives some wiggle-room in both the notion of which permissions are compatible with others and where exactly which permissions are added in the "stack" in a reborrow.
372 We could use that wiggle-room to experiment with a bunch of closely related models to see how they affect which code gets accepted and analyze their impact on optimizations.
373
374 I am quite excited by this new model!
375 It puts us into a good position to do more precise tracking for raw pointers, similar to what already happens for shared references (and something I was worried about in the original model).
376 That will be needed for compatibility with LLVM.
377 However, [there are still some known issues](https://github.com/rust-lang/unsafe-code-guidelines/issues?q=is%3Aissue+is%3Aopen+label%3Atopic-stacked-borrows), and also the fact that we do not actually use the "stack" as a stack indicates that maybe we should use a different structure there (potentially something more like a tree).
378 This is definitely not the final word, but I think it is a step in the right direction, and I am curious to see how it works out.
379 As usual, if you have any questions or comments, please join the [discussion in the forums](https://internals.rust-lang.org/t/stacked-borrows-2/9951)!