std: avoid aliasing violations when wrapping opaque C types - #160848
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 |
Yeah, this is the exact same thing that already happens with |
Using |
I just checked, POSIX actually says that:
so the current |
| /// operational semantics are much stricter when it comes to e.g. the initiali- | ||
| /// zation state of data types and pointer aliasing. For instance, a function |
There was a problem hiding this comment.
Hyphenated things in doc blocks probably don't work correctly.
|
This looks correct to me. I worry slightly that there's a lot of other similar patterns in std that we're not looking at, but this is basically just a line-by-line change to the more relaxed semantics, so there's little to get wrong here ^^ r=me once the doc nit above is addressed |
std: avoid aliasing violations when wrapping opaque C types Fixes rust-lang#160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
std: avoid aliasing violations when wrapping opaque C types Fixes rust-lang#160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
std: avoid aliasing violations when wrapping opaque C types Fixes rust-lang#160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
std: avoid aliasing violations when wrapping opaque C types Fixes rust-lang#160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
Rollup of 14 pull requests Successful merges: - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #161456 (reduce perf impact of scalar size checks) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`)
std: avoid aliasing violations when wrapping opaque C types Fixes rust-lang#160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
std: avoid aliasing violations when wrapping opaque C types Fixes rust-lang#160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
…uwer Rollup of 21 pull requests Successful merges: - #158609 (Update sccache to 0.16.0) - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160562 (add target feature ABI checks for SPARC) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #161456 (reduce perf impact of scalar size checks) - #161528 (Add regression test to ensure optimal compilation) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161859 (Do not optimize MIR for comptime ConstFns) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`) - #161889 (Add link to ownership section in ptr::read docs) - #161890 (rustdoc: some clarifying comments) - #161891 (Mark `extern_item_impls` feature as incomplete) Failed merges: - #161702 (Use `drop_guard` in some places in {core,alloc,std})
…uwer Rollup of 21 pull requests Successful merges: - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160562 (add target feature ABI checks for SPARC) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #157218 (Track items behind `cfg_select` in the same way we do for `cfg`) - #161456 (reduce perf impact of scalar size checks) - #161528 (Add regression test to ensure optimal compilation) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161859 (Do not optimize MIR for comptime ConstFns) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`) - #161889 (Add link to ownership section in ptr::read docs) - #161890 (rustdoc: some clarifying comments) - #161891 (Mark `extern_item_impls` feature as incomplete) Failed merges: - #161702 (Use `drop_guard` in some places in {core,alloc,std})
Rollup merge of #160848 - joboet:opaque_c_alias, r=nia-e std: avoid aliasing violations when wrapping opaque C types Fixes #160815 (and some other instances of the same problem) See the new documentation of `COpaque` for 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 `Condvar` and `Mutex` abstraction 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 internal `COpaque` helper type which uses a combination of `UnsafePinned` and `MaybeUninit` to relax all relevant requirements added by Rust's operational semantics. CC @RalfJung I'd love to hear your opinion on this r? libs
It was added rust-lang#160848 I'm not sure why was it made private.
It was added rust-lang#160848 I'm not sure why was it made private.
It was added rust-lang#160848 I'm not sure why was it made private.
It was added rust-lang#160848 I'm not sure why was it made private.
…ize, r=joboet Fix a compile error on the ESP-IDF target PR rust-lang#160848 accidentally broke the Tier-3 ESP-IDF target by leaving out one reference to `attr.as_ptr()` which should've been changed to `attr.get()`. This PR does the absolute minimum to restore correct behavior for the ESP-IDF target (and Nuttx). CC @joboet @nia-e
…ize, r=joboet Fix a compile error on the ESP-IDF target PR rust-lang#160848 accidentally broke the Tier-3 ESP-IDF target by leaving out one reference to `attr.as_ptr()` which should've been changed to `attr.get()`. This PR does the absolute minimum to restore correct behavior for the ESP-IDF target (and Nuttx). CC @joboet @nia-e
…ize, r=joboet Fix a compile error on the ESP-IDF target PR rust-lang#160848 accidentally broke the Tier-3 ESP-IDF target by leaving out one reference to `attr.as_ptr()` which should've been changed to `attr.get()`. This PR does the absolute minimum to restore correct behavior for the ESP-IDF target (and Nuttx). CC @joboet @nia-e
Rollup merge of #162049 - ivmarkov:fix-espidf-thread-stack-size, r=joboet Fix a compile error on the ESP-IDF target PR #160848 accidentally broke the Tier-3 ESP-IDF target by leaving out one reference to `attr.as_ptr()` which should've been changed to `attr.get()`. This PR does the absolute minimum to restore correct behavior for the ESP-IDF target (and Nuttx). CC @joboet @nia-e
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