Skip to content

Commit 01345db

Browse files
committed
Add MaybeValid type
`MaybeValid<T>` is a `T` which might not be valid. It is similar to `MaybeUninit<T>`, but it is slightly more strict: any byte in `T` which is guaranteed to be initialized is also guaranteed to be initialized in `MaybeValid<T>` (see the doc comment for a more precise definition). `MaybeValid` is a building block of the `TryFromBytes` design outlined in #5. Makes progress on #5
1 parent 557dac0 commit 01345db

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

src/lib.rs

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,199 @@ safety_comment! {
18141814
assert_unaligned!(mem::MaybeUninit<()>, MaybeUninit<u8>);
18151815
}
18161816

1817+
/// A value which might or might not constitute a valid instance of `T`.
1818+
///
1819+
/// `MaybeValid<T>` has the same layout (size and alignment) and field offsets
1820+
/// as `T`. Unlike `T`, it may contain any bit pattern, except that
1821+
/// uninitialized bytes may only appear in `MaybeValid<T>` at byte offsets where
1822+
/// they may appear in `T`. This is a dynamic property: if, at a particular byte
1823+
/// offset, a valid enum discriminant is set, the subsequent bytes may only have
1824+
/// uninitialized bytes as specified by the corresponding enum variant.
1825+
///
1826+
/// Formally, given `m: MaybeValid<T>` and a byte offset, `b` in the range `[0,
1827+
/// size_of_val(m))`:
1828+
/// - If, in all valid instances `t: T`, the byte at offset `b` in `t` is
1829+
/// initialized, then the byte at offset `b` within `m` is guaranteed to be
1830+
/// initialized.
1831+
/// - Let `c` be the contents of the byte range `[0, b)` in `m`. Let `TT` be the
1832+
/// subset of valid instances of `T` which contain `c` in the offset range
1833+
/// `[0, b)`. If, for all instances of `t: T` in `TT`, the byte at offset `b`
1834+
/// in `t` is initialized, then the byte at offset `b` in `m` is guaranteed to
1835+
/// be initialized.
1836+
///
1837+
/// Pragmatically, this means that if `m` is guaranteed to contain an enum
1838+
/// type at a particular offset, and the enum discriminant stored in `m`
1839+
/// corresponds to a valid variant of that enum type, then it is guaranteed
1840+
/// that the appropriate bytes of `m` are initialized as defined by that
1841+
/// variant's bit validity (although note that the variant may contain another
1842+
/// enum type, in which case the same rules apply depending on the state of
1843+
/// its discriminant, and so on recursively).
1844+
///
1845+
/// # Safety
1846+
///
1847+
/// Unsafe code may assume that an instance of `MaybeValid` satisfies the
1848+
/// constraints described above. Unsafe code may produce a `MaybeValid` or
1849+
/// modify the bytes of an existing `MaybeValid` so long as these constraints
1850+
/// are upheld. It is unsound to produce a `MaybeValid` which fails to uphold
1851+
/// these constraints.
1852+
#[repr(transparent)]
1853+
pub struct MaybeValid<T: ?Sized + KnownLayout> {
1854+
inner: MaybeUninit<T>,
1855+
}
1856+
1857+
safety_comment! {
1858+
/// SAFETY:
1859+
/// - `AsBytes`: `MaybeValid` requires that, if a byte in `T` is always
1860+
/// initialized, the equivalent byte in `MaybeValid<T>` must be
1861+
/// initialized. `T: AsBytes` implies that all bytes in `T` must always be
1862+
/// initialized, and so all bytes in `MaybeValid<T>` must always be
1863+
/// initialized, and so `MaybeValid<T>` satisfies `AsBytes`.
1864+
/// - `Unaligned`: `MaybeValid<T>` has the same alignment as `T`.
1865+
/// - `KnownLayout`: Since `MaybeUninit<T>` is a `repr(transparent)` wrapper
1866+
/// around `T::MaybeUninit`:
1867+
/// - They have the same prefix size, alignment, and trailing slice
1868+
/// element size
1869+
/// - It is valid to perform an `as` cast in either direction, and this
1870+
/// operation preserves referent size
1871+
///
1872+
/// TODO(#5): Implement `FromZeroes` and `FromBytes` for `MaybeValid<T>` and
1873+
/// `MaybeValid<[T]>`.
1874+
unsafe_impl!(T: ?Sized + KnownLayout + AsBytes => AsBytes for MaybeValid<T>);
1875+
unsafe_impl!(T: ?Sized + KnownLayout + Unaligned => Unaligned for MaybeValid<T>);
1876+
unsafe_impl_known_layout!(T: ?Sized + KnownLayout => #[repr(MaybeUninit<T>)] MaybeValid<T>);
1877+
}
1878+
1879+
// impl<T: KnownLayout> Default for MaybeValid<T> {
1880+
// fn default() -> MaybeValid<T> {
1881+
// // SAFETY: All of the bytes of `inner` are initialized to 0, and so the
1882+
// // safety invariant on `MaybeValid` is upheld.
1883+
// MaybeValid { inner: MaybeUninit::zeroed() }
1884+
// }
1885+
// }
1886+
1887+
impl<T: KnownLayout + ?Sized> MaybeValid<T> {
1888+
/// Converts this `&MaybeValid<T>` to a `&T`.
1889+
///
1890+
/// # Safety
1891+
///
1892+
/// `self` must contain a valid `T`.
1893+
pub unsafe fn assume_valid_ref(&self) -> &T {
1894+
// SAFETY: The caller has promised that `self` contains a valid `T`.
1895+
// Since `Self` is `repr(transparent)`, it has the same layout as
1896+
// `MaybeUninit<T>`, which in turn is guaranteed to have the same layout
1897+
// as `T`. Thus, it is sound to treat `self.inner` as containing a valid
1898+
// `T`.
1899+
unsafe { self.inner.assume_init_ref() }
1900+
}
1901+
1902+
/// Converts this `&mut MaybeValid<T>` to a `&mut T`.
1903+
///
1904+
/// # Safety
1905+
///
1906+
/// `self` must contain a valid `T`.
1907+
pub unsafe fn assume_valid_mut(&mut self) -> &mut T {
1908+
// SAFETY: The caller has promised that `self` contains a valid `T`.
1909+
// Since `Self` is `repr(transparent)`, it has the same layout as
1910+
// `MaybeUninit<T>`, which in turn is guaranteed to have the same layout
1911+
// as `T`. Thus, it is sound to treat `self.inner` as containing a valid
1912+
// `T`.
1913+
unsafe { self.inner.assume_init_mut() }
1914+
}
1915+
1916+
/// Gets a view of this `&T` as a `&MaybeValid<T>`.
1917+
///
1918+
/// There is no mutable equivalent to this function, as producing a `&mut
1919+
/// MaybeValid<T>` from a `&mut T` would allow safe code to write invalid
1920+
/// values which would be accessible through `&mut T`.
1921+
pub fn from_ref(r: &T) -> &MaybeValid<T> {
1922+
let m: *const MaybeUninit<T> = MaybeUninit::from_ref(r);
1923+
#[allow(clippy::as_conversions)]
1924+
let ptr = m as *const MaybeValid<T>;
1925+
// SAFETY: Since `Self` is `repr(transparent)`, it has the same layout
1926+
// as `MaybeUninit<T>`, so the size and alignment here are valid.
1927+
//
1928+
// `MaybeValid<T>`'s bit validity constraints are weaker than those of
1929+
// `T`, so this is guaranteed not to produce an invalid `MaybeValid<T>`.
1930+
// If it were possible to write a different value for `MaybeValid<T>`
1931+
// through the returned reference, it could result in an invalid value
1932+
// being exposed via the `&T`. Luckily, the only way for mutation to
1933+
// happen is if `T` contains an `UnsafeCell` and the caller uses it to
1934+
// perform interior mutation. Importantly, `T` containing an
1935+
// `UnsafeCell` does not permit interior mutation through
1936+
// `MaybeValid<T>`, so it doesn't permit writing uninitialized or
1937+
// otherwise invalid values which would be visible through the original
1938+
// `&T`.
1939+
unsafe { &*ptr }
1940+
}
1941+
}
1942+
1943+
impl<T: KnownLayout<MaybeUninit = mem::MaybeUninit<T>>> MaybeValid<T> {
1944+
/// Converts this `MaybeValid<T>` to a `T`.
1945+
///
1946+
/// # Safety
1947+
///
1948+
/// `self` must contain a valid `T`.
1949+
pub const unsafe fn assume_valid(self) -> T {
1950+
// SAFETY: The caller has promised that `self` contains a valid `T`.
1951+
// Since `Self` is `repr(transparent)`, it has the same layout as
1952+
// `MaybeUninit<T>`, which in turn is guaranteed to have the same layout
1953+
// as `T`. Thus, it is sound to treat `self.inner` as containing a valid
1954+
// `T`.
1955+
unsafe { self.inner.assume_init() }
1956+
}
1957+
}
1958+
1959+
impl<T: KnownLayout<MaybeUninit = mem::MaybeUninit<T>>> MaybeValid<[T]> {
1960+
/// Converts a `MaybeValid<[T]>` to a `[MaybeValid<T>]`.
1961+
///
1962+
/// `MaybeValid<T>` has the same layout as `T`, so these layouts are
1963+
/// equivalent.
1964+
pub const fn as_slice_of_maybe_valids(&self) -> &[MaybeValid<T>] {
1965+
let inner: &[<T as KnownLayout>::MaybeUninit] = &self.inner.inner;
1966+
let inner_ptr: *const [<T as KnownLayout>::MaybeUninit] = inner;
1967+
// Note: this Clippy warning is only emitted on our MSRV (1.61), but not
1968+
// on later versions of Clippy. Thus, we consider it spurious.
1969+
#[allow(clippy::as_conversions)]
1970+
let ret_ptr = inner_ptr as *const [MaybeValid<T>];
1971+
// SAFETY: Since `inner` is a `&[MaybeUninit<T>]`, and `MaybeValid<T>`
1972+
// is a `repr(transparent)` struct around `MaybeUninit<T>`, `inner` has
1973+
// the same layout as `&[MaybeValid<T>]`.
1974+
unsafe { &*ret_ptr }
1975+
}
1976+
}
1977+
1978+
impl<const N: usize, T: KnownLayout> MaybeValid<[T; N]> {
1979+
/// Converts a `MaybeValid<[T; N]>` to a `MaybeValid<[T]>`.
1980+
// TODO(#64): Make this `const` once our MSRV is >= 1.64.0 (when
1981+
// `slice_from_raw_parts` was stabilized as `const`).
1982+
pub fn as_slice(&self) -> &MaybeValid<[T]> {
1983+
let base: *const MaybeValid<[T; N]> = self;
1984+
let slice_of_t: *const [T] = ptr::slice_from_raw_parts(base.cast::<T>(), N);
1985+
// Note: this Clippy warning is only emitted on our MSRV (1.61), but not
1986+
// on later versions of Clippy. Thus, we consider it spurious.
1987+
#[allow(clippy::as_conversions)]
1988+
let mv_of_slice = slice_of_t as *const MaybeValid<[T]>;
1989+
// SAFETY: `MaybeValid<T>` is a `repr(transparent)` wrapper around
1990+
// `MaybeUninit<T>`, which in turn has the same layout as `T`. Thus, the
1991+
// trailing slices of `[T]` and of `MaybeValid<[T]>` both have element
1992+
// type `T`. Since the number of elements is preserved during an `as`
1993+
// cast of slice/DST pointers, the resulting `*const MaybeValid<[T]>`
1994+
// has the same number of elements - and thus the same length - as the
1995+
// original `*const [T]`.
1996+
//
1997+
// Thanks to their layouts, `MaybeValid<[T; N]>` and `MaybeValid<[T]>`
1998+
// have the same alignment, so `mv_of_slice` is guaranteed to be
1999+
// aligned.
2000+
unsafe { &*mv_of_slice }
2001+
}
2002+
}
2003+
2004+
impl<T: ?Sized + KnownLayout> Debug for MaybeValid<T> {
2005+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2006+
f.pad(core::any::type_name::<Self>())
2007+
}
2008+
}
2009+
18172010
/// A type with no alignment requirement.
18182011
///
18192012
/// An `Unalign` wraps a `T`, removing any alignment requirement. `Unalign<T>`
@@ -3853,6 +4046,91 @@ mod tests {
38534046
assert_eq!(unsafe { m.assume_init_ref() }, &Cell::new(2));
38544047
}
38554048

4049+
#[test]
4050+
fn test_maybe_valid() {
4051+
let m = MaybeValid::<usize>::default();
4052+
// SAFETY: all bit patterns are valid `usize`s, and `m` is initialized.
4053+
let u = unsafe { m.assume_valid() };
4054+
// This ensures that Miri can see whether `u` (and thus `m`) has been
4055+
// properly initialized.
4056+
assert_eq!(u, u);
4057+
4058+
fn bytes_to_maybe_valid(bytes: &mut [u8]) -> &mut MaybeValid<[u8]> {
4059+
// SAFETY: `MaybeValid<[u8]>` has the same layout as `[u8]`, and
4060+
// `bytes` is initialized.
4061+
unsafe {
4062+
#[allow(clippy::as_conversions)]
4063+
let m = &mut *(bytes as *mut [u8] as *mut MaybeValid<[u8]>);
4064+
m
4065+
}
4066+
}
4067+
4068+
let mut bytes = [0u8, 1, 2];
4069+
let m = bytes_to_maybe_valid(&mut bytes[..]);
4070+
4071+
// SAFETY: `m` was created from a valid `[u8]`.
4072+
let r = unsafe { m.assume_valid_ref() };
4073+
assert_eq!(r.len(), 3);
4074+
assert_eq!(r, [0, 1, 2]);
4075+
4076+
// SAFETY: `m` was created from a valid `[u8]`.
4077+
let r = unsafe { m.assume_valid_mut() };
4078+
assert_eq!(r.len(), 3);
4079+
assert_eq!(r, [0, 1, 2]);
4080+
4081+
r[0] = 1;
4082+
assert_eq!(bytes, [1, 1, 2]);
4083+
4084+
let mut bytes = [0u8, 1, 2];
4085+
let m = bytes_to_maybe_valid(&mut bytes[..]);
4086+
let slc = m.as_slice_of_maybe_valids();
4087+
assert_eq!(slc.len(), 3);
4088+
for i in 0u8..3 {
4089+
// SAFETY: `m` was created from a valid `[u8]`.
4090+
let u = unsafe { slc[usize::from(i)].assume_valid_ref() };
4091+
assert_eq!(u, &i);
4092+
}
4093+
}
4094+
4095+
#[test]
4096+
fn test_maybe_valid_as_slice() {
4097+
let mut m = MaybeValid::<[u8; 3]>::default();
4098+
// SAFETY: all bit patterns are valid `[u8; 3]`s, and `m` is
4099+
// initialized.
4100+
unsafe { *m.assume_valid_mut() = [0, 1, 2] };
4101+
4102+
let slc = m.as_slice().as_slice_of_maybe_valids();
4103+
assert_eq!(slc.len(), 3);
4104+
4105+
for i in 0u8..3 {
4106+
// SAFETY: `m` was initialized as a valid `[u8; 3]`.
4107+
let u = unsafe { slc[usize::from(i)].assume_valid_ref() };
4108+
assert_eq!(u, &i);
4109+
}
4110+
}
4111+
4112+
#[test]
4113+
fn test_maybe_valid_from_ref() {
4114+
use core::cell::Cell;
4115+
4116+
let u = 1usize;
4117+
let m = MaybeValid::from_ref(&u);
4118+
// SAFETY: `m` was constructed from a valid `&usize`.
4119+
assert_eq!(unsafe { m.assume_valid_ref() }, &1usize);
4120+
4121+
// Test that interior mutability doesn't affect correctness or
4122+
// soundness.
4123+
4124+
let c = Cell::new(1usize);
4125+
let m = MaybeValid::from_ref(&c);
4126+
// SAFETY: `m` was constructed from a valid `&usize`.
4127+
assert_eq!(unsafe { m.assume_valid_ref() }, &Cell::new(1));
4128+
4129+
c.set(2);
4130+
// SAFETY: `m` was constructed from a valid `&usize`.
4131+
assert_eq!(unsafe { m.assume_valid_ref() }, &Cell::new(2));
4132+
}
4133+
38564134
#[test]
38574135
fn test_unalign() {
38584136
// Test methods that don't depend on alignment.
@@ -4771,6 +5049,12 @@ mod tests {
47715049
assert_impls!(MaybeUninit<NotZerocopy>: !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
47725050
assert_impls!(MaybeUninit<MaybeUninit<NotZerocopy>>: !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
47735051

5052+
assert_impls!(MaybeValid<u8>: Unaligned, AsBytes, !FromZeroes, !FromBytes);
5053+
assert_impls!(MaybeValid<MaybeValid<u8>>: Unaligned, AsBytes, !FromZeroes, !FromBytes);
5054+
assert_impls!(MaybeValid<[u8]>: Unaligned, AsBytes, !FromZeroes, !FromBytes);
5055+
assert_impls!(MaybeValid<NotZerocopy>: !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5056+
assert_impls!(MaybeValid<MaybeValid<NotZerocopy>>: !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5057+
47745058
assert_impls!(Wrapping<u8>: FromZeroes, FromBytes, AsBytes, Unaligned);
47755059
assert_impls!(Wrapping<NotZerocopy>: !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
47765060

0 commit comments

Comments
 (0)