X-Git-Url: https://git.ralfj.de/web.git/blobdiff_plain/3f24eb432a68c5bbec8b408d77f13cb571c49bfa..7d1ffe9ae08d3c13d47696d9d279fa7b46af0497:/personal/_posts/2023-06-02-tree-borrows.md?ds=sidebyside diff --git a/personal/_posts/2023-06-02-tree-borrows.md b/personal/_posts/2023-06-02-tree-borrows.md index 3c4f0c7..8c82a20 100644 --- a/personal/_posts/2023-06-02-tree-borrows.md +++ b/personal/_posts/2023-06-02-tree-borrows.md @@ -1,6 +1,7 @@ --- title: "From Stacks to Trees: A new aliasing model for Rust" categories: rust research +reddit: /rust/comments/13y8a9b/from_stacks_to_trees_a_new_aliasing_model_for_rust/ --- Since last fall, [Neven](https://perso.crans.org/vanille/) has been doing an internship to develop a new aliasing model for Rust: Tree Borrows. @@ -151,9 +152,11 @@ First of all, another [major issue](https://github.com/rust-lang/unsafe-code-gui This falls out of how TB handles the aliasing allowed with `UnsafeCell`: they are treated like casts to raw pointers, so reborrowing an `&Cell` just inherits the tag (and therefore the permissions) of the parent pointer. More controversially, TB also changes how precisely things become read-only when an `&T` involves `UnsafeCell` somewhere inside `T`. -In particular, for `&(i32, Cell)`, TB allows mutating the first field, since it just treats the entire reference as "this allows aliasing". +In particular, for `&(i32, Cell)`, TB allows mutating *both* fields, including the first field which is a regular `i32`, since it just treats the entire reference as "this allows aliasing".[^1] In contrast, SB actually figured out that the first 4 bytes are read-only and only the last 4 bytes allow mutation via aliased pointers. +[^1]: This does not mean that we bless such mutation! It just means that the compiler cannot use immutability of the first field for its optimizations. Basically, immutability of that field becomes a [safety invariant instead of a validity invariant]({% post_url 2018-08-22-two-kinds-of-invariants %}): when you call foreign code, you can still rely on it not mutating that field, but within the privacy of your own code you are allowed to mutate it. See [my response here](https://www.reddit.com/r/rust/comments/13y8a9b/comment/jmlvgun/) for some more background. + The reason for this design decision is that the general philosophy with TB was to err on the side of allowing more code, having less UB (which is the opposite direction than what I used with SB). This is a deliberate choice to uncover as much of the design space as we can with these two models. Of course we wanted to make sure that TB still allows all the desired optimizations, and still has enough UB to justify the LLVM IR that rustc generates -- those were our "lower bounds" for the minimum amount of UB we need. @@ -219,3 +222,4 @@ If you run into any surprises or concerns, please let us know! The [t-opsem Zulip](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem) and the [UCG issue tracker](https://github.com/rust-lang/unsafe-code-guidelines/) are good places for such questions. That's all I got, thanks for reading -- and a shout out to Neven for doing all the actual work here (and for giving feedback on this blog post), supervising this project has been a lot of fun! +Remember to read [his write up](https://perso.crans.org/vanille/treebor/) and [watch his talk](https://www.youtube.com/watch?v=zQ76zLXesxA).