Skip to content
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

renamed to as_mut_bytes #5

Closed
wants to merge 1 commit into from
Closed

renamed to as_mut_bytes #5

wants to merge 1 commit into from

Conversation

dorryspears
Copy link
Owner

test

@dorryspears dorryspears force-pushed the rename-as_bytes_mut branch 2 times, most recently from c39b863 to bff9148 Compare February 13, 2024 01:36
dorryspears pushed a commit that referenced this pull request Feb 20, 2024
dorryspears pushed a commit that referenced this pull request Feb 20, 2024
dorryspears pushed a commit that referenced this pull request Feb 24, 2024
Previously, our test harness for `TryFromBytes` types assumed that those
types also implemented `NoCell`. This commit introduces machinery that
allows us to conditionally execute certain steps of our tests depending
on whether or not the type under test implements `NoCell`.

This commit also lifts the `T: NoCell` bound in `TryFromBytes for
MaybeUninit<T>` in order to test this machinery.

Makes progress on #5

Co-authored-by: Jack Wrenn <jswrenn@amazon.com>
dorryspears pushed a commit that referenced this pull request Feb 24, 2024
This allows the callback passed to `with_passing_test_cases` to access
its argument mutably. We don't make use of this ability in this commit,
but it will become important later when we implement `TryFromBytes` for
`UnsafeCell`.

Makes progress on #5
dorryspears pushed a commit that referenced this pull request Mar 3, 2024
Credit to jswrenn@amazon.com 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
dorryspears pushed a commit that referenced this pull request Mar 3, 2024
Makes progress towards #5.
dorryspears pushed a commit that referenced this pull request Mar 3, 2024
Makes progress on #5

Closes google#962

Co-authored-by: Jack Wrenn <jswrenn@amazon.com>
dorryspears pushed a commit that referenced this pull request Mar 3, 2024
joshlf pushed a commit that referenced this pull request May 3, 2024
…le#1119)

For example, `try_from_ref` is now `try_ref_from`.

See: google#1095

Makes progress on #5
dorryspears pushed a commit that referenced this pull request Jul 1, 2024
dorryspears pushed a commit that referenced this pull request Jul 1, 2024
Closes google#1013.

Makes progress towards #5.
dorryspears pushed a commit that referenced this pull request Jul 1, 2024
We do support deriving `TryFromBytes` on unions, and our
conservative assumptions of their validity match intuition.

Makes progress towards #5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant