2 title: Formalizing Rust
3 categories: research rust
6 My current research project - and the main topic of my PhD thesis - is about developing a *formal model* of the [Rust programming language](https://www.rust-lang.org/) and its type system.
7 Rust is an attempt of Mozilla to find a sweet spot in the design space of programming languages: A language that's safe to use, convenient for programmers, and guards against memory errors and thread unsafety.
8 Other have [said](https://www.youtube.com/watch?v=O5vzLKg7y-k) and [written](http://www.oreilly.com/programming/free/files/why-rust.pdf) a lot on why we need such a language, so I won't lose any more words on this.
9 Let me just use this opportunity for a shameless plug: If you are curious and want to learn Rust, check out [Rust-101](https://www.ralfj.de/projects/rust-101/main.html), a hands-on Rust tutorial I wrote.
10 I am going to assume some basic familiarity with Rust in the following.
12 Why do we want to formalize Rust? First of all, I'm (becoming) a programming languages researcher, and Rust is an interesting new language to study.
13 It's going to be fun! Honestly, that's enough of a reason for me.
14 But there are other reasons: It shouldn't be a surprise that [bugs](https://github.com/rust-lang/rust/issues/24292) have [been](https://github.com/rust-lang/rust/issues/25860) [found](https://github.com/rust-lang/rust/issues/24880) in Rust.
15 There are lots of things that can be done about such bugs - my take on this is that we should try to *prove*, in a mathematical rigorous way, that no such bugs exist in Rust.
16 This goes hand-in-hand with other approaches like testing, fuzzing and [static analysis](https://homes.cs.washington.edu/~emina/pubs/crust.ase15.pdf).
17 However, we (at my [research group](http://plv.mpi-sws.org/)) are into formalizing things, so that's what we are going to do.
23 Let me just get this out of the way at the beginning: The first formal model is not going to cover all features of Rust.
24 Some of the missing pieces will hopefully be added later, some maybe not.
25 Rust is huge, it's too large to be all handled in a single go.
26 In particular, the first version is not going to cover automatic destructors, i.e., the `Drop` trait and the dropck mechanism.
27 This is, of course, a significant gap, even more so since dropck may well be the most subtle part of the type system, and the current version (as of Rust 1.3) is [known to be unsound](https://github.com/rust-lang/rust/issues/26656).
28 But we have to start somewhere, and even without `Drop`, there is a lot to be done.
30 We are also not going to have traits, unwinding and a few other pieces in the beginning.
31 This is mostly to avoid getting distracted by all the details, and keep the focus on the most important bits.
32 In terms of what we are interested in, none of these features significantly change the game.
33 I have some good ideas for how to handle them later, should we have the time for that.
35 Finally, our language and type system are not going to be exactly Rust, but something that's more amenable for formal verification.
36 Of course, we have to be careful not to introduce any significant differences on this level.
37 With Rust introducing the [MIR](https://github.com/rust-lang/rfcs/blob/master/text/1211-mir.md), the compiler actually moved much closer to our "rust-y" language.
38 So, there's some hope we might eventually establish a formal connection between MIR and our work.
42 You may wonder now, what are we even doing then?
43 We are focusing on the core type system: Ownership and borrowing.
44 The goal is to prove that well-typed Rust programs are *memory and thread safe*, which, roughly speaking, means that all executed pointer accesses are valid, and there are no data races.
45 I will often just say "memory safety", but concurrency is definitely part of the story.
47 ## The syntactic approach and its limitations
49 We could now be looking at the checks the Rust compiler is doing, and prove directly that these checks imply memory safety.
50 This is of course an important property, and actually, this [has been done](ftp://ftp.cs.washington.edu/tr/2015/03/UW-CSE-15-03-02.pdf) already.
51 However, such a proof only gets us so far: The moment your program uses [`unsafe`](https://doc.rust-lang.org/stable/book/unsafe.html), this result does not apply to your program any more.
52 It is the whole purpose of `unsafe` to permit writing dangerous code that the compiler cannot check, and hence any proof relying solely on such compiler checks simply does not apply.
53 In the following, we will consider programs containing `unsafe` as not being well-typed.
54 (We could also consider them well-typed in a more liberal type system that above result does not, and cannot, apply to. This is closer to what the Rust compiler does, but it's not helpful in our context.)
55 Note that this issue is viral: `Vec`, `RefCell`, `Rc` - all these and many more standard library data structures use `unsafe`, any if your program uses any of them, you're out of luck.
57 ## Semantics to the rescue
59 Intuitively, why do we even think that a program that uses `Vec`, but contains no `unsafe` block *itself*, should be safe?
60 It's not just the compiler checks making sure that, e.g., we don't call `push` on a shared borrow.
61 We also rely on `Vec` *behaving well* - or, I should rather say, *behaving well-typed*.
62 Yes, `Vec` uses `unsafe` - but it should not be possible to *observe* this in a well-typed safe program, calling only the interface provided by `Vec`.
63 There are some rules that Rust enforces, some invariants that safe, well-typed code automatically upholds, that people writing unsafe code have to maintain themselves.
64 `Vec` performs checks and bookkeeping to get its tracking of the size and capacity of the container right.
65 `RefCell` counts the number of outstanding borrows, to make sure at run-time that no two mutable borrows to the same cell exist.
66 `Rc` counts the number of references that are hold, and only if that reaches 0 it deallocates memory. Crucially though, it only hands out *immutable*, shared borrows to its content.
67 If any of these checks would be removed, the data structure would clearly be unsafe, and even safe code could witness that.
68 But if the Rust authors got all these checks right, then safe code cannot even tell that unsafe operations are being performed.
70 This is something that safe code and properly written unsafe code have in common: If you consider only their externally observable behavior, for all means and purposes, both are well-typed.
71 We call this *semantic well-typedness*.
72 The core idea is that we can define the inhabitants of a type solely in terms of what one can do to them.
73 For example, to check that a function is semantically well-typed, you check what it does on every well-typed input, and make sure the output makes sense.
74 It doesn't matter *how* the function performs these operations.
75 This is in contrast to the *syntactic* notion of well-typedness of a function, which involves looking at the *code* of the function and checking it against a bunch of rules.
76 Only recently, research has been able to scale up such semantic methods to languages that combine state (i.e., mutable variables, a heap) with higher-order functions - languages like Rust, where I can take a closure (a `Fn{,Mut,Once}`) and store it in memory, for later use.
77 Lucky enough, my advisor [Derek Dreyer](http://www.mpi-sws.org/~dreyer/) is one of the world experts in this field, which permits me to intensively study Rust (which I would have done anyways) and call it research!
81 So, that's what we are trying to do: Come up not only with a formal version of Rust's (syntactic) type system, but also with appropriate *semantics* for these types.
82 We then have to show that the syntactic types make semantic sense. This recovers, in a very roundabout way, the result I mentioned above:
83 Every well-typed Rust program (that doesn't use `unsafe`, not even indirectly) is memory safe.
84 However, now that we have these semantic types, we can go even further and prove that `Vec`, `Rc`, `RefCell` and others are *semantically well-typed*.
85 At this point, it may well turn out that our semantic model is too restricted and cannot account for all of the Rust data structures that people believe to be safe.
86 In this case, we have to go back to step one and change the semantics of our types, until they actually match the intuition of the "Rust invariants" people developed over the years.
88 By combining this manual proof with the first result covering all syntactically well-typed Rust programs, we finally obtain the desired proof that safe programs using only the unsafe bits that were proven correct, are memory safe.
89 No matter what the safe client does with the exposed API of the unsafe data structures, we then know in a mathematical rigorous way that everything will be all right.
90 Isn't this a beautiful theorem? I certainly think so, even with the [caveats](#scope) described above.
91 We've only recently started this though, there is still a long way to go - lots of fun to be had.
92 I will track my progress, in very high-level terms, in [this Rust issue](https://github.com/rust-lang/rust/issues/9883).
94 Once we got a proper semantic model, we also hope to be able to translate some of these insights back into the vocabulary of Rust programmers, and tell them what it is they have to be checking when writing unsafe Rust code.
95 Right now, these exact rules are clear not even to the Rust team at Mozilla.
96 Hopefully, they are eventually going to be spelled out in [The Rustonomicon](https://doc.rust-lang.org/nightly/nomicon/) and guide future Rust programmers in their unsafe endeavors.