first version of rust post
[web.git] / ralf / _drafts / formalizing-rust.md
1 ---
2 title: Formalizing Rust
3 categories: research rust
4 ---
5
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/).
7 Rust is an attempt of Mozilla to find a sweet spot in the design space of programming languages: A langauge 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 langue, 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.
11
12 Why do we want to formalize Rust? First of all, I'm (becoming) a programming languages researcher, and Rust is an interesting new langauge to study. Honestly, that's enough of a reason for me.
13 But there's 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/26656) [found](https://github.com/rust-lang/rust/issues/24880) in Rust.
14 There is 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.
15 This goes hand-in-hand with other approaches like testing, fuzzing and [static analysis](https://homes.cs.washington.edu/~emina/pubs/crust.ase15.pdf).
16 We (at my research group) are into formalizing things, so that's what we are going to do.
17
18 ## Limitations
19
20 Let's just get this out of the way at the beginning: The first formal model is not going to cover all features of Rust.
21 Some of the missing pieces will hopefully be added later, some maybe not.
22 Rust is huge, it's too large to be all handled in a single go.
23 So, the first version is not going to cover traits, trait objects, type polymorphism, `Drop`, slices, the `'static` lifetime, nor unwinding on panic.
24 I know, that's a long list.
25 Some of these I have some good ideas for, and I don't think they should be too hard (like slices or unwinding), others I am pretty sure will be hard (like `Drop`).
26
27 ## So what's left?
28
29 You may wonder now, what are we even doing then?
30 We are focusing on the core typesystem: Ownership and borrowing.
31 There are some interesting nuts to crack here: What we are looking for is a *semantic model* of Rust's type system.
32 Let me first explain why this is the way to go.
33
34 The goal is to prove that well-typed Rust programs are *memory safe*, which, roughly speaking, means that all executed pointer accesses are valid.
35 We could now be looking at the checks the Rust compiler is doing, and proving that these checks imply memory safety.
36 This is of course and important property, and actually, this [has been done](ftp://ftp.cs.washington.edu/tr/2015/03/UW-CSE-15-03-02.pdf) already.
37 However, it is not good enough: 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.
38 It is the whole purpose of `unsafe` to disable some compiler checks, and hence any proof relying solely on such compiler checks simply does not imply to programs using `unsafe`.
39 In some sense, programs containing `unsafe` are not well-typed.
40 (More strictly speaking, they are well-typed in a more liberal type system that above result does not, and cannot, apply to.)
41 Note that this 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.
42
43 Intuitively, why do we even think that a program that uses `Vec`, but contains no `unsafe` *itself*, should be safe?
44 It's not (just) the compiler checks.
45 We also rely on `Vec` *behaving well* - or, I should rather say, *behaving well-typed*.
46 Yes, `Vec` uses `unsafe` - but it should not be possible to *observe* this is a well-typed programs, calling only the safe interface provided by `Vec`.
47 There are some rules that Rust uphelds, some invariants that safe, well-typed code automatically preserves, that people writing unsafe code have to maintain themselves.
48 `Vec` performs checks and bookkeeping to get its tracking of the size and capacitity of the container right.
49 `RefCell` counts the number of outstanding borrows, to make sure at run-time that no two mutable borrowsto the same cell exist.
50 `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 conent.
51 If any of these checks would be removed, the data structure would clearly be unsafe, and even safe code could witness that.
52 But if the Rust authors got all these checks right, then safe code cannot even tell that unsafe operations are being performed.
53
54 This means there is something to *manually prove* about unsafe code: It has to behave as if it were well-typed.
55 This is a notion that can actually be captured formally.
56 Besdies the merely syntactic notion of well-typedness that relies on a type checker matching the syntax of the program against a bunch of rules, one can define a *semantic* notion of well-typedess that is defined solely in terms of what one can *do* with a piece of code.
57 For example, for a function to be syntactically well-typed, one goes ahead and looks at its code, and makes sure that code is written the right way.
58 For a function to be semantically well-typed, however, one checks what the function *does* on every possible input, how the output looks like.
59 The actual code doing this, does not matter.
60 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 `Fn` and store it in memory, for later use.
61
62 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.
63 We then have to show that the syntactic types make semantic sense. This recovers, in a very roundabout way, the result I mentioned above:
64 Every well-typed Rust program (that doesn't use `unsafe`, not even indirectly) is memory safe.
65 However, now that we have these semantic types, we can go further and prove that `Vec`, `Rc`, `RefCell` are also *semantically well-typed*.
66 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.
67
68 Note that there are many semantic models we could give to the syntactc type system of Rust.
69 But most of these will be too restricted, they will not actually allow `Rc` or `RefCell` (those two are particularily interesting) to be semantically well-typed - or some other data structure that is commonly believed to be safe to use.
70 Hence coming up with the right semantic model involves understanding what it is that makes all these, and many more, data structures "right" - and fitting that into a formal definition that captures all the right properties.
71 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.
72 At this point, these exact rules are clear not even to the Rust team at Mozilla.
73 At some point, hopefully, they are going to be laid out in [The Rustonomicon](https://doc.rust-lang.org/nightly/nomicon/) and guide future Rust programmers in their unsafe endavors.