std: avoid aliasing violations when wrapping opaque C types - #160848
std: avoid aliasing violations when wrapping opaque C types#160848joboet wants to merge 1 commit into
Conversation
This comment has been minimized.
This comment has been minimized.
feb6204 to
086a6d2
Compare
This comment has been minimized.
This comment has been minimized.
|
I noticed something interesting while working on the opsem of this, and it might make sense to call it out in the documentation in order to help make the safety proofs easier to understand. Normally in Rust, if you convert a reference into a pointer, you cannot use the pointer outside the lifetime of the reference without causing aliasing violations. It's as though raw pointers have lifetimes, and copy them from the reference they're created from (but you get UB rather than a borrow checker error if you violate them). This is the root cause behind #160815; a reference was converted to a pointer, but the pointer outlived the reference. (Although With In addition to mentioning this in |
Fixes #160815 (and some other instances of the same problem)
See the new documentation of
COpaquefor a detailed description of the kinds of issues solved by this. In short: creating mutable references to the opaque types from pthread is unsound since some platforms (at least AIX) store an intrinsically list of these types and the creation of the mutable reference (e.g. in the drop glue) invalidates the other pointers to the type.This doesn't just apply to the internal pthread
CondvarandMutexabstraction as described in the issue, but also to all other opaque types – nobody is promising us that these are not internally aliased. Hence this PR adds an internalCOpaquehelper type which uses a combination ofUnsafePinnedandMaybeUninitto relax all relevant requirements added by Rust's operational semantics.CC @RalfJung I'd love to hear your opinion on this
r? libs