-
Notifications
You must be signed in to change notification settings - Fork 139
Implement TryFromBytes for UnsafeCell #912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df97fb9 to
d5351c1
Compare
5cd1351 to
1e9980a
Compare
1e9980a to
173e1bc
Compare
joshlf
commented
Feb 23, 2024
173e1bc to
49af394
Compare
jswrenn
reviewed
Feb 26, 2024
Credit to [email protected] for figuring out how to make this design more misuse-proof. Implementing `TryFromBytes` for `UnsafeCell` isn't possible without changes to `TryFromBytes`. Previously, we passed `is_bit_valid` a `Ptr` with shared aliasing. There is no way to soundly implement `is_bit_valid` for `UnsafeCell` with a shared-aliased pointer, as there's no way to soundly inspect the bytes of an `UnsafeCell` when other references may exist to the same memory. The only way to inspect the bytes of an `UnsafeCell` in a generic context is given a mutable reference to one. This is in principle possible from `TryFromBytes`. Notably, `try_read_from` reads a `MaybeUninit<Self>` into its local stack frame, and that has `UnsafeCell`s at the same byte ranges as `Self`, so it is sound to construct a mutably-aliased `Ptr` to that local variable. Unfortunately, `is_bit_valid` previously had no way of reasoning about a mutably-aliased pointer. In order to lift this restriction, we modify `is_bit_valid` to be generic over aliasing using `A: invariant::at_least::Shared`. When `A` is `invariant::Exclusive`, we can implement `TryFromBytes::is_bit_valid` for `UnsafeCell`. However, this isn't the whole story. We need to implement `TryFromBytes` for `UnsafeCell` in the general case, even when a shared-aliased `Ptr` is passed to `is_bit_valid`. Our first design was to define calling `UnsafeCell::is_bit_valid` with a shared-aliased to be a panic condition of `is_bit_valid`. Since we only call `is_bit_valid` from our own code (`is_bit_valid` is `#[doc(hidden)]`), we can in theory simply avoid calling `UnsafeCell::is_bit_valid` with a shared-aliased pointer. However, that's very error-prone. jswrenn had the idea to instead use a post-monomorphization error, which is implemented in `Ptr::into_exclusive_or_post_monomorphization_error`. This allows us to test *at compile time* to see whether the argument to `UnsafeCell::is_bit_valid` is a shared or exclusive pointer, and fail compilation in the former case. Some readers might wonder why we couldn't use a `NoCell` bound to enforce that `is_bit_valid` could only be called with a shared-aliased pointer when `Self: NoCell`. We experimented with this, but it turns out that there is some existing code that cannot be made to work with this pattern - the type system isn't smart enough to infer bounds in some cases. Makes progress on #5
49af394 to
5d1a8e1
Compare
jswrenn
approved these changes
Feb 26, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Credit to @jswrenn for figuring out how to make this design
more misuse-proof.
Implementing
TryFromBytesforUnsafeCellisn't possible withoutchanges to
TryFromBytes. Previously, we passedis_bit_validaPtrwith shared aliasing. There is no way to soundly implement
is_bit_validforUnsafeCellwith a shared-aliased pointer, asthere's no way to soundly inspect the bytes of an
UnsafeCellwhenother references may exist to the same memory. The only way to inspect
the bytes of an
UnsafeCellin a generic context is given a mutablereference to one. This is in principle possible from
TryFromBytes.Notably,
try_read_fromreads aMaybeUninit<Self>into its localstack frame, and that has
UnsafeCells at the same byte ranges asSelf, so it is sound to construct a mutably-aliasedPtrto thatlocal variable. Unfortunately,
is_bit_validpreviously had no way ofreasoning about a mutably-aliased pointer.
In order to lift this restriction, we modify
is_bit_validto begeneric over aliasing using
A: invariant::at_least::Shared. WhenAis
invariant::Exclusive, we can implementTryFromBytes::is_bit_validfor
UnsafeCell.However, this isn't the whole story. We need to implement
TryFromBytesfor
UnsafeCellin the general case, even when a shared-aliasedPtris passed to
is_bit_valid. Our first design was to define callingUnsafeCell::is_bit_validwith a shared-aliased to be a panic conditionof
is_bit_valid. Since we only callis_bit_validfrom our own code(
is_bit_validis#[doc(hidden)]), we can in theory simply avoidcalling
UnsafeCell::is_bit_validwith a shared-aliased pointer.However, that's very error-prone.
@jswrenn had the idea to instead use a post-monomorphization error, which
is implemented in
Ptr::into_exclusive_or_post_monomorphization_error.This allows us to test at compile time to see whether the argument to
UnsafeCell::is_bit_validis a shared or exclusive pointer, and failcompilation in the former case.
Some readers might wonder why we couldn't use a
NoCellbound toenforce that
is_bit_validcould only be called with a shared-aliasedpointer when
Self: NoCell. We experimented with this, but it turns outthat there is some existing code that cannot be made to work with this
pattern - the type system isn't smart enough to infer bounds in some
cases.
Makes progress on #5