X-Git-Url: https://git.ralfj.de/web.git/blobdiff_plain/48bc26c93d164a55d90e46c7ec19c2ffb1fa55a0..HEAD:/personal/_posts/2016-01-09-the-scope-of-unsafe.md?ds=sidebyside diff --git a/personal/_posts/2016-01-09-the-scope-of-unsafe.md b/personal/_posts/2016-01-09-the-scope-of-unsafe.md index 757bd73..967b4a3 100644 --- a/personal/_posts/2016-01-09-the-scope-of-unsafe.md +++ b/personal/_posts/2016-01-09-the-scope-of-unsafe.md @@ -2,6 +2,8 @@ title: The Scope of Unsafe categories: research rust reddit: /rust/comments/4065l2/the_scope_of_unsafe/ +license: CC BY-SA 4.0 +license-url: https://creativecommons.org/licenses/by-sa/4.0/ --- I'd like to talk about an important aspect of dealing with unsafe code, that still regularly seems to catch people on the wrong foot: @@ -11,10 +13,10 @@ I'd like to talk about an important aspect of dealing with unsafe code, that sti The "scope" in the title refers to the extent of the code that has to be manually checked for correctness, once `unsafe` is used. What I am saying is that the scope of `unsafe` is larger than the `unsafe` block itself. -It turns out that the underlying reason for this observation is also a nice illustration for the concept of *semantic types* that comes up in my [work on formalizing Rust]({{ site.baseurl }}{% post_url 2015-10-12-formalizing-rust %}) (or rather, its type system). +It turns out that the underlying reason for this observation is also a nice illustration for the concept of *semantic types* that comes up in my [work on formalizing Rust]({% post_url 2015-10-12-formalizing-rust %}) (or rather, its type system). Finally, this discussion will once again lead us to realize that we rely on our type systems to provide much more than just type safety. -**Update (Jan 11th):** Clarified the role of privacy; argued why `evil` is the problem. +**Update (2016-01-11):** Clarified the role of privacy; argued why `evil` is the problem. @@ -51,7 +53,7 @@ Oops! So, this example clearly shows that to evaluate the safety of types like `Vec`, we have to look at *every single function* provided by that data structure, even if it does not contain any `unsafe` code. -## The reason why +## The Reason Why Why is it the case that a safe function can break `Vec`? How can we even say that it is the safe function which is at fault, rather than some piece of `unsafe` code elsewhere in `Vec`? @@ -74,7 +76,7 @@ Even academic researchers working on Rust got this wrong, arguing that in order That's why I think it's worth dedicating an entire blog post to this point. But we are not done yet, we can actually use this observation to learn more about types and type systems. -## The semantic perspective +## The Semantic Perspective There is another way to phrase the intuition of types having additional invariants: @@ -101,7 +103,7 @@ The two types are *syntactically* equal, and the same goes for the two `evil` fu Still, `MyType::evil` is a perfectly benign function (despite its name). How can this be? -Remember that in a [previous blog post]({{ site.baseurl }}{% post_url 2015-10-12-formalizing-rust %}), I argued that types have a *semantic* aspect. +Remember that in a [previous blog post]({% post_url 2015-10-12-formalizing-rust %}), I argued that types have a *semantic* aspect. For example, a function is semantically well-typed if it *behaves* properly on all valid arguments, independently of how, *syntactically*, the function body has been written down. The reason for `MyType::evil` being fine, while `Vec::evil` is bad, is that semantically speaking, `Vec` is very different from `MyType` -- even though they look so similar. @@ -129,7 +131,7 @@ This is just as bad as a function assigning `*f = 42u32` to a `f: &mut f32`. The difference between `Vec` and `MyType` is no less significant than the difference between `f32` and `u32`. It's just that the compiler has been specifically taught about `f32`, while it doesn't know enough about the semantics of `Vec`. -## The actual scope of unsafe +## The Actual Scope of Unsafe At this point, you may be slightly worried about the safety of the Rust ecosystem. I just spent two sections arguing that the Rust compiler actually doesn't know what it is doing when it comes to checking functions that work on `Vec`. @@ -157,7 +159,7 @@ There would actually be *no bound to the scope of `unsafe`*. To formally establish safety, one would have to literally go over the entire program and prove that it doesn't misuse `Vec`. The safety promise of Rust would be pretty much useless. -This should not be entirely surprising if you read the aforementioned [post about formalizing Rust's type system]({{ site.baseurl }}{% post_url 2015-10-12-formalizing-rust %}), where I already argued that a proof of (syntactic) type safety does not help to justify safety of most Rust programs out there. +This should not be entirely surprising if you read the aforementioned [post about formalizing Rust's type system]({% post_url 2015-10-12-formalizing-rust %}), where I already argued that a proof of (syntactic) type safety does not help to justify safety of most Rust programs out there. I am now making a similar point, coming from a different angle. The fact that Rust programmers *can* use `Vec` and many other types without much care is a property of the type system that is independent of type safety.