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

Use/require byte equality in VarZeroVec #1103

Merged
merged 7 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions utils/zerovec/src/ule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ pub use plain::PlainOldULE;
///
/// # Safety
///
/// See the safety invariant documented on [`Self::from_byte_slice_unchecked()`] to implement this trait.
/// There must be no padding bytes involved in this type: [`Self::as_byte_slice()`] MUST return
Manishearth marked this conversation as resolved.
Show resolved Hide resolved
/// a slice of initialized bytes provided that `Self` is initialized.
///
/// This method _must_ be implemented to return the same result as [`ULE::parse_byte_slice()`].
Manishearth marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`ULE::as_byte_slice()`] should return a slice that is the in-memory representation of `Self`,
/// i.e. it should be just a pointer cast, and `mem::size_of_val(self) == mem::size_of_val(self.as_byte_slice())`=
///
/// # Equality invariant
///
/// A non-safety invariant is that if `Self` implements `PartialEq`, it *must* be logically equivalent to
/// byte equality on `.as_byte_slice()`. Failure to follow this invariant will not cause undefined
/// behavior, but may cause problems in the `PartialEq` implementations of `ZeroVec` and `VarZeroVec`,
/// as well as the predictable operation of `ZeroMap`
Manishearth marked this conversation as resolved.
Show resolved Hide resolved
pub unsafe trait ULE
where
Self: Sized,
Expand Down Expand Up @@ -66,9 +79,9 @@ where
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a default impl for from_byte_slice_unchecked as well. For ULE, you can use mem::size_of to figure out how long to make the resulting slice. For VarULE, I think you can just cast it, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't cast it for VarULE because there's no way to generically do the inverse operation of size_of_val() ("I have X bytes, tell me what the pointer metadata should be"). For example, consider what would happen with stuff like struct ComplexULE(u8, char::ULE, [u32::ULE]), calculating the pointer metadata for that is very specific to that type.

Now fortunately the only kind of DST we can theoretically support are slice DSTs and compound slice DSTs, but just to give an example of why the inverse of size_of_val() is untractable: how woudl you solve that for trait objects? At least for compound slice-based DSTs rustc could theoretically add APIs to std::ptr::metadata that let you do this.

/// # Safety
///
/// In most cases, the implementation of this function should involve re-casting the pointer.
/// The implementation of this function should involve re-casting the pointer.
/// It is up to the implementation to reason about the safety. Keep in mind that `&[Self]` and
/// `&[u8]` may have different lengths.
/// `&[u8]` may have different lengths (but should cover the same data).
#[allow(clippy::wrong_self_convention)] // https://github.com/rust-lang/rust-clippy/issues/7219
fn as_byte_slice(slice: &[Self]) -> &[u8];
}
Expand Down Expand Up @@ -194,6 +207,16 @@ pub trait AsVarULE {
///
/// [`VarULE::from_byte_slice_unchecked()`] _must_ be implemented to return the same result
/// as [`VarULE::parse_byte_slice()`] provided both are passed the same validly parsing byte slices.
///
/// [`VarULE::as_byte_slice()`] should return a slice that is the in-memory representation of `Self`,
/// i.e. it should be just a pointer cast, and `mem::size_of_val(self) == mem::size_of_val(self.as_byte_slice())`
///
/// # Equality invariant
///
/// A non-safety invariant is that if `Self` implements `PartialEq`, it *must* be logically equivalent to
/// byte equality on `.as_byte_slice()`. Failure to follow this invariant will not cause undefined
/// behavior, but may cause problems in the `PartialEq` implementations of `ZeroVec` and `VarZeroVec`,
/// as well as the predictable operation of `ZeroMap`.
pub unsafe trait VarULE: 'static {
/// The error type to used by [`VarULE::parse_byte_slice()`]
type Error;
Expand Down Expand Up @@ -233,7 +256,7 @@ pub unsafe trait VarULE: 'static {
///
/// # Safety
///
/// In most cases, the implementation of this function should involve re-casting the pointer.
/// The implementation of this function should involve re-casting the pointer.
Manishearth marked this conversation as resolved.
Show resolved Hide resolved
/// It is up to the implementation to reason about the safety.
fn as_byte_slice(&self) -> &[u8];
}
10 changes: 4 additions & 6 deletions utils/zerovec/src/varzerovec/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,10 @@ pub fn get_serializable_bytes<T: AsVarULE>(elements: &[T]) -> Option<Vec<u8>> {
let mut offset: u32 = 0;
for element in elements {
vec.extend(&offset.as_unaligned().0);
let len_u32: u32 = element
.as_unaligned()
.as_byte_slice()
.len()
.try_into()
.ok()?;
let ule = element.as_unaligned();
let slice = ule.as_byte_slice();
debug_assert_eq!(mem::size_of_val(ule), mem::size_of_val(slice));
let len_u32: u32 = slice.len().try_into().ok()?;
offset = offset.checked_add(len_u32)?;
}
vec.reserve(offset as usize);
Expand Down
10 changes: 7 additions & 3 deletions utils/zerovec/src/varzerovec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ impl<'a, T: AsVarULE> VarZeroVec<'a, T> {
///
/// This can be passed back to [`Self::parse_byte_slice()`]
pub fn get_encoded_slice(&self) -> &[u8] {
self.get_components().entire_slice()
match self.0 {
VarZeroVecInner::Owned(ref vec) => vec.entire_slice(),
VarZeroVecInner::Borrowed(vec) => vec.entire_slice(),
}
}

/// For a slice of `T`, get a list of bytes that can be passed to
Expand Down Expand Up @@ -501,8 +504,9 @@ where
{
#[inline]
fn eq(&self, other: &VarZeroVec<'b, T>) -> bool {
// Note: T implements PartialEq but not T::ULE
self.iter().eq(other.iter())
// VarULE has an API guarantee that this is equivalent
// to `T::VarULE::eq()`
self.get_encoded_slice().eq(other.get_encoded_slice())
}
}

Expand Down
5 changes: 5 additions & 0 deletions utils/zerovec/src/varzerovec/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ impl<T: AsVarULE> VarZeroVecOwned<T> {
self.get_components().to_vec()
}

#[inline]
pub fn entire_slice(&self) -> &[u8] {
&self.entire_slice
}

/// Insert an element at index `idx`
pub fn insert(&mut self, index: usize, element: &T) {
let len = self.len();
Expand Down
5 changes: 3 additions & 2 deletions utils/zerovec/src/varzerovec/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ where
{
#[inline]
fn eq(&self, other: &VarZeroVecULE<T>) -> bool {
// Note: T implements PartialEq but not T::ULE
self.iter().eq(other.iter())
// VarULE has an API guarantee that this is equivalent
// to `T::VarULE::eq()`
self.entire_slice.eq(&other.entire_slice)
}
}

Expand Down