0:00 / 0:00
#unsaferust#pointeraliasing#undefinedbehavior
Unsafe Rust is not C
Jack O'Connor
3.3K subscribers • 1 video on Hallway
- Recommended by 1 person: 1 in engineering communities.
- Presented by Jack O'Connor
Advanced. Deep dive for people who know the field.
38K views4 years ago1 recommendation
Programming languagesSystems programmingRustCMemory management
Jack O'Connor explains the differences between unsafe Rust and C, focusing on pointer aliasing rules and undefined behavior.
Unsafe Rust is like C in some ways, but there are tricky rules that unsafe Rust has to follow that don't exist in C. C also has some tricky rules of its own. This is a talk about some of these differences, particularly when it comes to pointer aliasing. Contents: introduction a familiar example sprinkling unsafe doesn't help raw pointers a "false positive" mutable aliasing is UB confirming UB with Miri comparing assembly a pathological example strict aliasing in C overlapping in C aliasing and overlapping in unsafe Rust Why do compilers do this to us? outro Slides: - a familiar example: godbolt.org/z/vss7Wb585 - sprinkling unsafe doesn't help: godbolt.org/z/T55Tx8KEn - raw pointers: godbolt.org/z/dzo6Pan5q - a "false positive": godbolt.org/z/jjTGPE1fK - mutable aliasing is UB: godbolt.org/z/T78MxzE9G (updated, see errata below) - confirming UB with Miri: play.rust-lang.org/?version=stable&mode=debug&edition=2021&g… - comparing assembly: godbolt.org/z/rzv7Taof6 - a pathological example: play.rust-lang.org/?version=stable&mode=debug&edition=2021&g… - strict aliasing in C: godbolt.org/z/7qWdfG5MG - overlapping in C: godbolt.org/z/f73Ejjnbj - permissive aliasing in Rust: godbolt.org/z/oh8q663sn - overlapping in Rust: godbolt.org/z/bcheT4KoM - Why do compilers do this to us? godbolt.org/z/shTTcv69h Errata: - The godbolt example at ("mutable aliasing is UB" above) stops working if you update the compiler version to 1.75.0 or newer. As of Rust 1.82.0, to get the wrong answer in release mode as intended, you need to add #[inline(never)] above foo. I've updated the godbolt link with this change. Thanks to Francisco Garrido de Figueiredo for catching this one. - At I said that Miri "inserts extra checks into your code", but that's not actually how Miri works. (Though it is how ASan and UBSan work.) Instead, Miri directly interprets the "mid-level intermediate representation" of your program, which is produced by the compiler. Thanks to Tyler Bloom for catching this one. - At I show an example of breaking the "strict aliasing" rule in C, and that example does indeed produce undefined behavior today. But as of just last month, a proposal called "Improved Rules for Tag Compatibility" (N3003) has been adopted into C23 that will make this particular example legal. More egregious violations of this rule, like casting an int pointer to a float pointer, will continue to be UB. Thanks to Michael Grunder for pointing this out. (Update: Actually this particular example might never become legal, because the struct "tags" aren't the same? It's not clear to me.) - At I claimed that some pointer casts were allowed, but that's probably incorrect. It might still be true that compilers reason aWhy it's here
1 person recommends thisWhat people say. Engineers recommend this talk for its clear explanation of pointer aliasing and undefined behavior in unsafe Rust compared to C.