The Unsafe Chronicles: Exhibit A: Aliasing Boxes
Channel: Jon Gjengset
Date: 2020-12-12

In this new series we take deep-dives into the world of unsafe Rust. These chronicles aren't necessarily aimed at teaching you how to write unsafe code, but instead try to showcase particular things to keep in mind when it comes to safety in Rust. In particular, in each episode we'll look at a specific (and usually tricky) source of unsoundness, and how to work around it.

This particular episode focuses on the journey to aliasing boxed values in the left-right crate (https://docs.rs/left-right/), which we developed in a previous stream, and was augmented in https://github.com/jonhoo/rust-evmap/pull/83. left-right wishes to alias types between two different copies of a data structure, but doing so leads down an interesting rabbit hole around the safety of aliasing and casting.

0:00:00 About this new series
0:01:48 left-right and evmap
0:06:31 Expressing the problem in code
0:15:28 Why is aliasing a Box not okay?
0:19:09 Aliasing Box with MaybeUninit
0:39:48 Dropping aliases
0:55:15 The cast is unsound
1:00:45 Making the cast sound
1:10:47 Q&A
1:26:43 Unsoundness from non-determinism
1:28:25 More Q&A
1:35:14 Outroduction

Some relevant issues:

evmap unsoundness issue: https://github.com/jonhoo/rust-evmap/issues/74
Aliasing rules for Box{T}: https://github.com/rust-lang/unsafe-code-guidelines/issues/258
Deterministic layout: https://github.com/rust-lang/unsafe-code-guidelines/issues/35#issuecomment-731656729
Deterministic Eq+Hash: https://github.com/jonhoo/rust-evmap/issues/78


You can watch the live version with comments at https://youtu.be/4LrQidI5z1g