The Unsafe Chronicles: Exhibit A: Aliasing Boxes
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
Other Videos By Jon Gjengset
2021-08-31 | Crust of Rust: async/await |
2021-07-10 | Implementing Hazard Pointers in Rust (part 2) |
2021-06-26 | Implementing Hazard Pointers in Rust |
2021-06-13 | Lock-Free to Wait-Free Simulation in Rust (part 2) |
2021-05-22 | Lock-Free to Wait-Free Simulation in Rust |
2021-04-30 | Crust of Rust: Dispatch and Fat Pointers |
2021-04-02 | Crust of Rust: Atomics and Memory Ordering |
2021-03-13 | Crust of Rust: The Drop Check |
2021-02-20 | Crust of Rust: Subtyping and Variance |
2021-01-23 | Q&A January 2021 (now with cat) |
2020-12-12 | The Unsafe Chronicles: Exhibit A: Aliasing Boxes |
2020-11-21 | A Cool Generic Concurrency Primitive in Rust |
2020-11-14 | Crust of Rust: Sorting Algorithms |
2020-10-23 | Thesis: Partial State in Dataflow-Based Materialized Views |
2020-08-19 | Q&A August #2 2020 |
2020-08-09 | Q&A August 2020 |
2020-08-05 | Crust of Rust: Channels |
2020-07-25 | Thesis Talk: The Evaluation Chapter |
2020-06-17 | Crust of Rust: Smart Pointers and Interior Mutability |
2020-05-27 | Crust of Rust: Iterators |
2020-04-29 | Crust of Rust: Declarative Macros |