1*499dc02cSMiguel Ojeda // SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT 2*499dc02cSMiguel Ojeda 3c3739801SMiguel Ojeda // Copyright 2024 The Fuchsia Authors 4c3739801SMiguel Ojeda // 5c3739801SMiguel Ojeda // Licensed under the 2-Clause BSD License <LICENSE-BSD or 6c3739801SMiguel Ojeda // https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0 7c3739801SMiguel Ojeda // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT 8c3739801SMiguel Ojeda // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. 9c3739801SMiguel Ojeda // This file may not be copied, modified, or distributed except according to 10c3739801SMiguel Ojeda // those terms. 11c3739801SMiguel Ojeda 12c3739801SMiguel Ojeda use core::{mem, num::NonZeroUsize}; 13c3739801SMiguel Ojeda 14c3739801SMiguel Ojeda use crate::util; 15c3739801SMiguel Ojeda 16c3739801SMiguel Ojeda /// The target pointer width, counted in bits. 17c3739801SMiguel Ojeda const POINTER_WIDTH_BITS: usize = mem::size_of::<usize>() * 8; 18c3739801SMiguel Ojeda 19c3739801SMiguel Ojeda /// The layout of a type which might be dynamically-sized. 20c3739801SMiguel Ojeda /// 21c3739801SMiguel Ojeda /// `DstLayout` describes the layout of sized types, slice types, and "slice 22c3739801SMiguel Ojeda /// DSTs" - ie, those that are known by the type system to have a trailing slice 23c3739801SMiguel Ojeda /// (as distinguished from `dyn Trait` types - such types *might* have a 24c3739801SMiguel Ojeda /// trailing slice type, but the type system isn't aware of it). 25c3739801SMiguel Ojeda /// 26c3739801SMiguel Ojeda /// Note that `DstLayout` does not have any internal invariants, so no guarantee 27c3739801SMiguel Ojeda /// is made that a `DstLayout` conforms to any of Rust's requirements regarding 28c3739801SMiguel Ojeda /// the layout of real Rust types or instances of types. 29c3739801SMiguel Ojeda #[doc(hidden)] 30c3739801SMiguel Ojeda #[allow(missing_debug_implementations, missing_copy_implementations)] 31c3739801SMiguel Ojeda #[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))] 32c3739801SMiguel Ojeda #[derive(Copy, Clone)] 33c3739801SMiguel Ojeda pub struct DstLayout { 34c3739801SMiguel Ojeda pub(crate) align: NonZeroUsize, 35c3739801SMiguel Ojeda pub(crate) size_info: SizeInfo, 36c3739801SMiguel Ojeda // Is it guaranteed statically (without knowing a value's runtime metadata) 37c3739801SMiguel Ojeda // that the top-level type contains no padding? This does *not* apply 38c3739801SMiguel Ojeda // recursively - for example, `[(u8, u16)]` has `statically_shallow_unpadded 39c3739801SMiguel Ojeda // = true` even though this type likely has padding inside each `(u8, u16)`. 40c3739801SMiguel Ojeda pub(crate) statically_shallow_unpadded: bool, 41c3739801SMiguel Ojeda } 42c3739801SMiguel Ojeda 43c3739801SMiguel Ojeda #[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))] 44c3739801SMiguel Ojeda #[derive(Copy, Clone)] 45c3739801SMiguel Ojeda pub(crate) enum SizeInfo<E = usize> { 46c3739801SMiguel Ojeda Sized { size: usize }, 47c3739801SMiguel Ojeda SliceDst(TrailingSliceLayout<E>), 48c3739801SMiguel Ojeda } 49c3739801SMiguel Ojeda 50c3739801SMiguel Ojeda #[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))] 51c3739801SMiguel Ojeda #[derive(Copy, Clone)] 52c3739801SMiguel Ojeda pub(crate) struct TrailingSliceLayout<E = usize> { 53c3739801SMiguel Ojeda // The offset of the first byte of the trailing slice field. Note that this 54c3739801SMiguel Ojeda // is NOT the same as the minimum size of the type. For example, consider 55c3739801SMiguel Ojeda // the following type: 56c3739801SMiguel Ojeda // 57c3739801SMiguel Ojeda // struct Foo { 58c3739801SMiguel Ojeda // a: u16, 59c3739801SMiguel Ojeda // b: u8, 60c3739801SMiguel Ojeda // c: [u8], 61c3739801SMiguel Ojeda // } 62c3739801SMiguel Ojeda // 63c3739801SMiguel Ojeda // In `Foo`, `c` is at byte offset 3. When `c.len() == 0`, `c` is followed 64c3739801SMiguel Ojeda // by a padding byte. 65c3739801SMiguel Ojeda pub(crate) offset: usize, 66c3739801SMiguel Ojeda // The size of the element type of the trailing slice field. 67c3739801SMiguel Ojeda pub(crate) elem_size: E, 68c3739801SMiguel Ojeda } 69c3739801SMiguel Ojeda 70c3739801SMiguel Ojeda impl SizeInfo { 71c3739801SMiguel Ojeda /// Attempts to create a `SizeInfo` from `Self` in which `elem_size` is a 72c3739801SMiguel Ojeda /// `NonZeroUsize`. If `elem_size` is 0, returns `None`. 73c3739801SMiguel Ojeda #[allow(unused)] 74c3739801SMiguel Ojeda const fn try_to_nonzero_elem_size(&self) -> Option<SizeInfo<NonZeroUsize>> { 75c3739801SMiguel Ojeda Some(match *self { 76c3739801SMiguel Ojeda SizeInfo::Sized { size } => SizeInfo::Sized { size }, 77c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => { 78c3739801SMiguel Ojeda if let Some(elem_size) = NonZeroUsize::new(elem_size) { 79c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) 80c3739801SMiguel Ojeda } else { 81c3739801SMiguel Ojeda return None; 82c3739801SMiguel Ojeda } 83c3739801SMiguel Ojeda } 84c3739801SMiguel Ojeda }) 85c3739801SMiguel Ojeda } 86c3739801SMiguel Ojeda } 87c3739801SMiguel Ojeda 88c3739801SMiguel Ojeda #[doc(hidden)] 89c3739801SMiguel Ojeda #[derive(Copy, Clone)] 90c3739801SMiguel Ojeda #[cfg_attr(test, derive(Debug))] 91c3739801SMiguel Ojeda #[allow(missing_debug_implementations)] 92c3739801SMiguel Ojeda pub enum CastType { 93c3739801SMiguel Ojeda Prefix, 94c3739801SMiguel Ojeda Suffix, 95c3739801SMiguel Ojeda } 96c3739801SMiguel Ojeda 97c3739801SMiguel Ojeda #[cfg_attr(test, derive(Debug))] 98c3739801SMiguel Ojeda pub(crate) enum MetadataCastError { 99c3739801SMiguel Ojeda Alignment, 100c3739801SMiguel Ojeda Size, 101c3739801SMiguel Ojeda } 102c3739801SMiguel Ojeda 103c3739801SMiguel Ojeda impl DstLayout { 104c3739801SMiguel Ojeda /// The minimum possible alignment of a type. 105c3739801SMiguel Ojeda const MIN_ALIGN: NonZeroUsize = match NonZeroUsize::new(1) { 106c3739801SMiguel Ojeda Some(min_align) => min_align, 107c3739801SMiguel Ojeda None => const_unreachable!(), 108c3739801SMiguel Ojeda }; 109c3739801SMiguel Ojeda 110c3739801SMiguel Ojeda /// The maximum theoretic possible alignment of a type. 111c3739801SMiguel Ojeda /// 112c3739801SMiguel Ojeda /// For compatibility with future Rust versions, this is defined as the 113c3739801SMiguel Ojeda /// maximum power-of-two that fits into a `usize`. See also 114c3739801SMiguel Ojeda /// [`DstLayout::CURRENT_MAX_ALIGN`]. 115c3739801SMiguel Ojeda pub(crate) const THEORETICAL_MAX_ALIGN: NonZeroUsize = 116c3739801SMiguel Ojeda match NonZeroUsize::new(1 << (POINTER_WIDTH_BITS - 1)) { 117c3739801SMiguel Ojeda Some(max_align) => max_align, 118c3739801SMiguel Ojeda None => const_unreachable!(), 119c3739801SMiguel Ojeda }; 120c3739801SMiguel Ojeda 121c3739801SMiguel Ojeda /// The current, documented max alignment of a type \[1\]. 122c3739801SMiguel Ojeda /// 123c3739801SMiguel Ojeda /// \[1\] Per <https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers>: 124c3739801SMiguel Ojeda /// 125c3739801SMiguel Ojeda /// The alignment value must be a power of two from 1 up to 126c3739801SMiguel Ojeda /// 2<sup>29</sup>. 127c3739801SMiguel Ojeda #[cfg(not(kani))] 128c3739801SMiguel Ojeda #[cfg(not(target_pointer_width = "16"))] 129c3739801SMiguel Ojeda pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 28) { 130c3739801SMiguel Ojeda Some(max_align) => max_align, 131c3739801SMiguel Ojeda None => const_unreachable!(), 132c3739801SMiguel Ojeda }; 133c3739801SMiguel Ojeda 134c3739801SMiguel Ojeda #[cfg(not(kani))] 135c3739801SMiguel Ojeda #[cfg(target_pointer_width = "16")] 136c3739801SMiguel Ojeda pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 15) { 137c3739801SMiguel Ojeda Some(max_align) => max_align, 138c3739801SMiguel Ojeda None => const_unreachable!(), 139c3739801SMiguel Ojeda }; 140c3739801SMiguel Ojeda 141c3739801SMiguel Ojeda /// The maximum size of an allocation \[1\]. 142c3739801SMiguel Ojeda /// 143c3739801SMiguel Ojeda /// \[1\] Per <https://doc.rust-lang.org/1.91.1/std/ptr/index.html#allocation>: 144c3739801SMiguel Ojeda /// 145c3739801SMiguel Ojeda /// For any allocation with base `address`, `size`, and a set of `addresses`, 146c3739801SMiguel Ojeda /// the following are guaranteed: [..] 147c3739801SMiguel Ojeda /// 148c3739801SMiguel Ojeda /// - `size <= isize::MAX` 149c3739801SMiguel Ojeda /// 150c3739801SMiguel Ojeda #[allow(clippy::as_conversions)] 151c3739801SMiguel Ojeda pub(crate) const MAX_SIZE: usize = isize::MAX as usize; 152c3739801SMiguel Ojeda 153c3739801SMiguel Ojeda /// Assumes that this layout lacks static shallow padding. 154c3739801SMiguel Ojeda /// 155c3739801SMiguel Ojeda /// # Panics 156c3739801SMiguel Ojeda /// 157c3739801SMiguel Ojeda /// This method does not panic. 158c3739801SMiguel Ojeda /// 159c3739801SMiguel Ojeda /// # Safety 160c3739801SMiguel Ojeda /// 161c3739801SMiguel Ojeda /// If `self` describes the size and alignment of type that lacks static 162c3739801SMiguel Ojeda /// shallow padding, unsafe code may assume that the result of this method 163c3739801SMiguel Ojeda /// accurately reflects the size, alignment, and lack of static shallow 164c3739801SMiguel Ojeda /// padding of that type. 165c3739801SMiguel Ojeda const fn assume_shallow_unpadded(self) -> Self { 166c3739801SMiguel Ojeda Self { statically_shallow_unpadded: true, ..self } 167c3739801SMiguel Ojeda } 168c3739801SMiguel Ojeda 169c3739801SMiguel Ojeda /// Constructs a `DstLayout` for a zero-sized type with `repr_align` 170c3739801SMiguel Ojeda /// alignment (or 1). If `repr_align` is provided, then it must be a power 171c3739801SMiguel Ojeda /// of two. 172c3739801SMiguel Ojeda /// 173c3739801SMiguel Ojeda /// # Panics 174c3739801SMiguel Ojeda /// 175c3739801SMiguel Ojeda /// This function panics if the supplied `repr_align` is not a power of two. 176c3739801SMiguel Ojeda /// 177c3739801SMiguel Ojeda /// # Safety 178c3739801SMiguel Ojeda /// 179c3739801SMiguel Ojeda /// Unsafe code may assume that the contract of this function is satisfied. 180c3739801SMiguel Ojeda #[doc(hidden)] 181c3739801SMiguel Ojeda #[must_use] 182c3739801SMiguel Ojeda #[inline] 183c3739801SMiguel Ojeda pub const fn new_zst(repr_align: Option<NonZeroUsize>) -> DstLayout { 184c3739801SMiguel Ojeda let align = match repr_align { 185c3739801SMiguel Ojeda Some(align) => align, 186c3739801SMiguel Ojeda None => Self::MIN_ALIGN, 187c3739801SMiguel Ojeda }; 188c3739801SMiguel Ojeda 189c3739801SMiguel Ojeda const_assert!(align.get().is_power_of_two()); 190c3739801SMiguel Ojeda 191c3739801SMiguel Ojeda DstLayout { 192c3739801SMiguel Ojeda align, 193c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: 0 }, 194c3739801SMiguel Ojeda statically_shallow_unpadded: true, 195c3739801SMiguel Ojeda } 196c3739801SMiguel Ojeda } 197c3739801SMiguel Ojeda 198c3739801SMiguel Ojeda /// Constructs a `DstLayout` which describes `T` and assumes `T` may contain 199c3739801SMiguel Ojeda /// padding. 200c3739801SMiguel Ojeda /// 201c3739801SMiguel Ojeda /// # Safety 202c3739801SMiguel Ojeda /// 203c3739801SMiguel Ojeda /// Unsafe code may assume that `DstLayout` is the correct layout for `T`. 204c3739801SMiguel Ojeda #[doc(hidden)] 205c3739801SMiguel Ojeda #[must_use] 206c3739801SMiguel Ojeda #[inline] 207c3739801SMiguel Ojeda pub const fn for_type<T>() -> DstLayout { 208c3739801SMiguel Ojeda // SAFETY: `align` is correct by construction. `T: Sized`, and so it is 209c3739801SMiguel Ojeda // sound to initialize `size_info` to `SizeInfo::Sized { size }`; the 210c3739801SMiguel Ojeda // `size` field is also correct by construction. `unpadded` can safely 211c3739801SMiguel Ojeda // default to `false`. 212c3739801SMiguel Ojeda DstLayout { 213c3739801SMiguel Ojeda align: match NonZeroUsize::new(mem::align_of::<T>()) { 214c3739801SMiguel Ojeda Some(align) => align, 215c3739801SMiguel Ojeda None => const_unreachable!(), 216c3739801SMiguel Ojeda }, 217c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: mem::size_of::<T>() }, 218c3739801SMiguel Ojeda statically_shallow_unpadded: false, 219c3739801SMiguel Ojeda } 220c3739801SMiguel Ojeda } 221c3739801SMiguel Ojeda 222c3739801SMiguel Ojeda /// Constructs a `DstLayout` which describes a `T` that does not contain 223c3739801SMiguel Ojeda /// padding. 224c3739801SMiguel Ojeda /// 225c3739801SMiguel Ojeda /// # Safety 226c3739801SMiguel Ojeda /// 227c3739801SMiguel Ojeda /// Unsafe code may assume that `DstLayout` is the correct layout for `T`. 228c3739801SMiguel Ojeda #[doc(hidden)] 229c3739801SMiguel Ojeda #[must_use] 230c3739801SMiguel Ojeda #[inline] 231c3739801SMiguel Ojeda pub const fn for_unpadded_type<T>() -> DstLayout { 232c3739801SMiguel Ojeda Self::for_type::<T>().assume_shallow_unpadded() 233c3739801SMiguel Ojeda } 234c3739801SMiguel Ojeda 235c3739801SMiguel Ojeda /// Constructs a `DstLayout` which describes `[T]`. 236c3739801SMiguel Ojeda /// 237c3739801SMiguel Ojeda /// # Safety 238c3739801SMiguel Ojeda /// 239c3739801SMiguel Ojeda /// Unsafe code may assume that `DstLayout` is the correct layout for `[T]`. 240c3739801SMiguel Ojeda pub(crate) const fn for_slice<T>() -> DstLayout { 241c3739801SMiguel Ojeda // SAFETY: The alignment of a slice is equal to the alignment of its 242c3739801SMiguel Ojeda // element type, and so `align` is initialized correctly. 243c3739801SMiguel Ojeda // 244c3739801SMiguel Ojeda // Since this is just a slice type, there is no offset between the 245c3739801SMiguel Ojeda // beginning of the type and the beginning of the slice, so it is 246c3739801SMiguel Ojeda // correct to set `offset: 0`. The `elem_size` is correct by 247c3739801SMiguel Ojeda // construction. Since `[T]` is a (degenerate case of a) slice DST, it 248c3739801SMiguel Ojeda // is correct to initialize `size_info` to `SizeInfo::SliceDst`. 249c3739801SMiguel Ojeda DstLayout { 250c3739801SMiguel Ojeda align: match NonZeroUsize::new(mem::align_of::<T>()) { 251c3739801SMiguel Ojeda Some(align) => align, 252c3739801SMiguel Ojeda None => const_unreachable!(), 253c3739801SMiguel Ojeda }, 254c3739801SMiguel Ojeda size_info: SizeInfo::SliceDst(TrailingSliceLayout { 255c3739801SMiguel Ojeda offset: 0, 256c3739801SMiguel Ojeda elem_size: mem::size_of::<T>(), 257c3739801SMiguel Ojeda }), 258c3739801SMiguel Ojeda statically_shallow_unpadded: true, 259c3739801SMiguel Ojeda } 260c3739801SMiguel Ojeda } 261c3739801SMiguel Ojeda 262c3739801SMiguel Ojeda /// Constructs a complete `DstLayout` reflecting a `repr(C)` struct with the 263c3739801SMiguel Ojeda /// given alignment modifiers and fields. 264c3739801SMiguel Ojeda /// 265c3739801SMiguel Ojeda /// This method cannot be used to match the layout of a record with the 266c3739801SMiguel Ojeda /// default representation, as that representation is mostly unspecified. 267c3739801SMiguel Ojeda /// 268c3739801SMiguel Ojeda /// # Safety 269c3739801SMiguel Ojeda /// 270c3739801SMiguel Ojeda /// For any definition of a `repr(C)` struct, if this method is invoked with 271c3739801SMiguel Ojeda /// alignment modifiers and fields corresponding to that definition, the 272c3739801SMiguel Ojeda /// resulting `DstLayout` will correctly encode the layout of that struct. 273c3739801SMiguel Ojeda /// 274c3739801SMiguel Ojeda /// We make no guarantees to the behavior of this method when it is invoked 275c3739801SMiguel Ojeda /// with arguments that cannot correspond to a valid `repr(C)` struct. 276c3739801SMiguel Ojeda #[must_use] 277c3739801SMiguel Ojeda #[inline] 278c3739801SMiguel Ojeda pub const fn for_repr_c_struct( 279c3739801SMiguel Ojeda repr_align: Option<NonZeroUsize>, 280c3739801SMiguel Ojeda repr_packed: Option<NonZeroUsize>, 281c3739801SMiguel Ojeda fields: &[DstLayout], 282c3739801SMiguel Ojeda ) -> DstLayout { 283c3739801SMiguel Ojeda let mut layout = DstLayout::new_zst(repr_align); 284c3739801SMiguel Ojeda 285c3739801SMiguel Ojeda let mut i = 0; 286c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 287c3739801SMiguel Ojeda while i < fields.len() { 288c3739801SMiguel Ojeda #[allow(clippy::indexing_slicing)] 289c3739801SMiguel Ojeda let field = fields[i]; 290c3739801SMiguel Ojeda layout = layout.extend(field, repr_packed); 291c3739801SMiguel Ojeda i += 1; 292c3739801SMiguel Ojeda } 293c3739801SMiguel Ojeda 294c3739801SMiguel Ojeda layout = layout.pad_to_align(); 295c3739801SMiguel Ojeda 296c3739801SMiguel Ojeda // SAFETY: `layout` accurately describes the layout of a `repr(C)` 297c3739801SMiguel Ojeda // struct with `repr_align` or `repr_packed` alignment modifications and 298c3739801SMiguel Ojeda // the given `fields`. The `layout` is constructed using a sequence of 299c3739801SMiguel Ojeda // invocations of `DstLayout::{new_zst,extend,pad_to_align}`. The 300c3739801SMiguel Ojeda // documentation of these items vows that invocations in this manner 301c3739801SMiguel Ojeda // will accurately describe a type, so long as: 302c3739801SMiguel Ojeda // 303c3739801SMiguel Ojeda // - that type is `repr(C)`, 304c3739801SMiguel Ojeda // - its fields are enumerated in the order they appear, 305c3739801SMiguel Ojeda // - the presence of `repr_align` and `repr_packed` are correctly accounted for. 306c3739801SMiguel Ojeda // 307c3739801SMiguel Ojeda // We respect all three of these preconditions above. 308c3739801SMiguel Ojeda layout 309c3739801SMiguel Ojeda } 310c3739801SMiguel Ojeda 311c3739801SMiguel Ojeda /// Like `Layout::extend`, this creates a layout that describes a record 312c3739801SMiguel Ojeda /// whose layout consists of `self` followed by `next` that includes the 313c3739801SMiguel Ojeda /// necessary inter-field padding, but not any trailing padding. 314c3739801SMiguel Ojeda /// 315c3739801SMiguel Ojeda /// In order to match the layout of a `#[repr(C)]` struct, this method 316c3739801SMiguel Ojeda /// should be invoked for each field in declaration order. To add trailing 317c3739801SMiguel Ojeda /// padding, call `DstLayout::pad_to_align` after extending the layout for 318c3739801SMiguel Ojeda /// all fields. If `self` corresponds to a type marked with 319c3739801SMiguel Ojeda /// `repr(packed(N))`, then `repr_packed` should be set to `Some(N)`, 320c3739801SMiguel Ojeda /// otherwise `None`. 321c3739801SMiguel Ojeda /// 322c3739801SMiguel Ojeda /// This method cannot be used to match the layout of a record with the 323c3739801SMiguel Ojeda /// default representation, as that representation is mostly unspecified. 324c3739801SMiguel Ojeda /// 325c3739801SMiguel Ojeda /// # Safety 326c3739801SMiguel Ojeda /// 327c3739801SMiguel Ojeda /// If a (potentially hypothetical) valid `repr(C)` Rust type begins with 328c3739801SMiguel Ojeda /// fields whose layout are `self`, and those fields are immediately 329c3739801SMiguel Ojeda /// followed by a field whose layout is `field`, then unsafe code may rely 330c3739801SMiguel Ojeda /// on `self.extend(field, repr_packed)` producing a layout that correctly 331c3739801SMiguel Ojeda /// encompasses those two components. 332c3739801SMiguel Ojeda /// 333c3739801SMiguel Ojeda /// We make no guarantees to the behavior of this method if these fragments 334c3739801SMiguel Ojeda /// cannot appear in a valid Rust type (e.g., the concatenation of the 335c3739801SMiguel Ojeda /// layouts would lead to a size larger than `isize::MAX`). 336c3739801SMiguel Ojeda #[doc(hidden)] 337c3739801SMiguel Ojeda #[must_use] 338c3739801SMiguel Ojeda #[inline] 339c3739801SMiguel Ojeda pub const fn extend(self, field: DstLayout, repr_packed: Option<NonZeroUsize>) -> Self { 340c3739801SMiguel Ojeda use util::{max, min, padding_needed_for}; 341c3739801SMiguel Ojeda 342c3739801SMiguel Ojeda // If `repr_packed` is `None`, there are no alignment constraints, and 343c3739801SMiguel Ojeda // the value can be defaulted to `THEORETICAL_MAX_ALIGN`. 344c3739801SMiguel Ojeda let max_align = match repr_packed { 345c3739801SMiguel Ojeda Some(max_align) => max_align, 346c3739801SMiguel Ojeda None => Self::THEORETICAL_MAX_ALIGN, 347c3739801SMiguel Ojeda }; 348c3739801SMiguel Ojeda 349c3739801SMiguel Ojeda const_assert!(max_align.get().is_power_of_two()); 350c3739801SMiguel Ojeda 351c3739801SMiguel Ojeda // We use Kani to prove that this method is robust to future increases 352c3739801SMiguel Ojeda // in Rust's maximum allowed alignment. However, if such a change ever 353c3739801SMiguel Ojeda // actually occurs, we'd like to be notified via assertion failures. 354c3739801SMiguel Ojeda #[cfg(not(kani))] 355c3739801SMiguel Ojeda { 356c3739801SMiguel Ojeda const_debug_assert!(self.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get()); 357c3739801SMiguel Ojeda const_debug_assert!(field.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get()); 358c3739801SMiguel Ojeda if let Some(repr_packed) = repr_packed { 359c3739801SMiguel Ojeda const_debug_assert!(repr_packed.get() <= DstLayout::CURRENT_MAX_ALIGN.get()); 360c3739801SMiguel Ojeda } 361c3739801SMiguel Ojeda } 362c3739801SMiguel Ojeda 363c3739801SMiguel Ojeda // The field's alignment is clamped by `repr_packed` (i.e., the 364c3739801SMiguel Ojeda // `repr(packed(N))` attribute, if any) [1]. 365c3739801SMiguel Ojeda // 366c3739801SMiguel Ojeda // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers: 367c3739801SMiguel Ojeda // 368c3739801SMiguel Ojeda // The alignments of each field, for the purpose of positioning 369c3739801SMiguel Ojeda // fields, is the smaller of the specified alignment and the alignment 370c3739801SMiguel Ojeda // of the field's type. 371c3739801SMiguel Ojeda let field_align = min(field.align, max_align); 372c3739801SMiguel Ojeda 373c3739801SMiguel Ojeda // The struct's alignment is the maximum of its previous alignment and 374c3739801SMiguel Ojeda // `field_align`. 375c3739801SMiguel Ojeda let align = max(self.align, field_align); 376c3739801SMiguel Ojeda 377c3739801SMiguel Ojeda let (interfield_padding, size_info) = match self.size_info { 378c3739801SMiguel Ojeda // If the layout is already a DST, we panic; DSTs cannot be extended 379c3739801SMiguel Ojeda // with additional fields. 380c3739801SMiguel Ojeda SizeInfo::SliceDst(..) => const_panic!("Cannot extend a DST with additional fields."), 381c3739801SMiguel Ojeda 382c3739801SMiguel Ojeda SizeInfo::Sized { size: preceding_size } => { 383c3739801SMiguel Ojeda // Compute the minimum amount of inter-field padding needed to 384c3739801SMiguel Ojeda // satisfy the field's alignment, and offset of the trailing 385c3739801SMiguel Ojeda // field. [1] 386c3739801SMiguel Ojeda // 387c3739801SMiguel Ojeda // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers: 388c3739801SMiguel Ojeda // 389c3739801SMiguel Ojeda // Inter-field padding is guaranteed to be the minimum 390c3739801SMiguel Ojeda // required in order to satisfy each field's (possibly 391c3739801SMiguel Ojeda // altered) alignment. 392c3739801SMiguel Ojeda let padding = padding_needed_for(preceding_size, field_align); 393c3739801SMiguel Ojeda 394c3739801SMiguel Ojeda // This will not panic (and is proven to not panic, with Kani) 395c3739801SMiguel Ojeda // if the layout components can correspond to a leading layout 396c3739801SMiguel Ojeda // fragment of a valid Rust type, but may panic otherwise (e.g., 397c3739801SMiguel Ojeda // combining or aligning the components would create a size 398c3739801SMiguel Ojeda // exceeding `isize::MAX`). 399c3739801SMiguel Ojeda let offset = match preceding_size.checked_add(padding) { 400c3739801SMiguel Ojeda Some(offset) => offset, 401c3739801SMiguel Ojeda None => const_panic!("Adding padding to `self`'s size overflows `usize`."), 402c3739801SMiguel Ojeda }; 403c3739801SMiguel Ojeda 404c3739801SMiguel Ojeda ( 405c3739801SMiguel Ojeda padding, 406c3739801SMiguel Ojeda match field.size_info { 407c3739801SMiguel Ojeda SizeInfo::Sized { size: field_size } => { 408c3739801SMiguel Ojeda // If the trailing field is sized, the resulting layout 409c3739801SMiguel Ojeda // will be sized. Its size will be the sum of the 410c3739801SMiguel Ojeda // preceding layout, the size of the new field, and the 411c3739801SMiguel Ojeda // size of inter-field padding between the two. 412c3739801SMiguel Ojeda // 413c3739801SMiguel Ojeda // This will not panic (and is proven with Kani to not 414c3739801SMiguel Ojeda // panic) if the layout components can correspond to a 415c3739801SMiguel Ojeda // leading layout fragment of a valid Rust type, but may 416c3739801SMiguel Ojeda // panic otherwise (e.g., combining or aligning the 417c3739801SMiguel Ojeda // components would create a size exceeding 418c3739801SMiguel Ojeda // `usize::MAX`). 419c3739801SMiguel Ojeda let size = match offset.checked_add(field_size) { 420c3739801SMiguel Ojeda Some(size) => size, 421c3739801SMiguel Ojeda None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"), 422c3739801SMiguel Ojeda }; 423c3739801SMiguel Ojeda SizeInfo::Sized { size } 424c3739801SMiguel Ojeda } 425c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { 426c3739801SMiguel Ojeda offset: trailing_offset, 427c3739801SMiguel Ojeda elem_size, 428c3739801SMiguel Ojeda }) => { 429c3739801SMiguel Ojeda // If the trailing field is dynamically sized, so too 430c3739801SMiguel Ojeda // will the resulting layout. The offset of the trailing 431c3739801SMiguel Ojeda // slice component is the sum of the offset of the 432c3739801SMiguel Ojeda // trailing field and the trailing slice offset within 433c3739801SMiguel Ojeda // that field. 434c3739801SMiguel Ojeda // 435c3739801SMiguel Ojeda // This will not panic (and is proven with Kani to not 436c3739801SMiguel Ojeda // panic) if the layout components can correspond to a 437c3739801SMiguel Ojeda // leading layout fragment of a valid Rust type, but may 438c3739801SMiguel Ojeda // panic otherwise (e.g., combining or aligning the 439c3739801SMiguel Ojeda // components would create a size exceeding 440c3739801SMiguel Ojeda // `usize::MAX`). 441c3739801SMiguel Ojeda let offset = match offset.checked_add(trailing_offset) { 442c3739801SMiguel Ojeda Some(offset) => offset, 443c3739801SMiguel Ojeda None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"), 444c3739801SMiguel Ojeda }; 445c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) 446c3739801SMiguel Ojeda } 447c3739801SMiguel Ojeda }, 448c3739801SMiguel Ojeda ) 449c3739801SMiguel Ojeda } 450c3739801SMiguel Ojeda }; 451c3739801SMiguel Ojeda 452c3739801SMiguel Ojeda let statically_shallow_unpadded = self.statically_shallow_unpadded 453c3739801SMiguel Ojeda && field.statically_shallow_unpadded 454c3739801SMiguel Ojeda && interfield_padding == 0; 455c3739801SMiguel Ojeda 456c3739801SMiguel Ojeda DstLayout { align, size_info, statically_shallow_unpadded } 457c3739801SMiguel Ojeda } 458c3739801SMiguel Ojeda 459c3739801SMiguel Ojeda /// Like `Layout::pad_to_align`, this routine rounds the size of this layout 460c3739801SMiguel Ojeda /// up to the nearest multiple of this type's alignment or `repr_packed` 461c3739801SMiguel Ojeda /// (whichever is less). This method leaves DST layouts unchanged, since the 462c3739801SMiguel Ojeda /// trailing padding of DSTs is computed at runtime. 463c3739801SMiguel Ojeda /// 464c3739801SMiguel Ojeda /// The accompanying boolean is `true` if the resulting composition of 465c3739801SMiguel Ojeda /// fields necessitated static (as opposed to dynamic) padding; otherwise 466c3739801SMiguel Ojeda /// `false`. 467c3739801SMiguel Ojeda /// 468c3739801SMiguel Ojeda /// In order to match the layout of a `#[repr(C)]` struct, this method 469c3739801SMiguel Ojeda /// should be invoked after the invocations of [`DstLayout::extend`]. If 470c3739801SMiguel Ojeda /// `self` corresponds to a type marked with `repr(packed(N))`, then 471c3739801SMiguel Ojeda /// `repr_packed` should be set to `Some(N)`, otherwise `None`. 472c3739801SMiguel Ojeda /// 473c3739801SMiguel Ojeda /// This method cannot be used to match the layout of a record with the 474c3739801SMiguel Ojeda /// default representation, as that representation is mostly unspecified. 475c3739801SMiguel Ojeda /// 476c3739801SMiguel Ojeda /// # Safety 477c3739801SMiguel Ojeda /// 478c3739801SMiguel Ojeda /// If a (potentially hypothetical) valid `repr(C)` type begins with fields 479c3739801SMiguel Ojeda /// whose layout are `self` followed only by zero or more bytes of trailing 480c3739801SMiguel Ojeda /// padding (not included in `self`), then unsafe code may rely on 481c3739801SMiguel Ojeda /// `self.pad_to_align(repr_packed)` producing a layout that correctly 482c3739801SMiguel Ojeda /// encapsulates the layout of that type. 483c3739801SMiguel Ojeda /// 484c3739801SMiguel Ojeda /// We make no guarantees to the behavior of this method if `self` cannot 485c3739801SMiguel Ojeda /// appear in a valid Rust type (e.g., because the addition of trailing 486c3739801SMiguel Ojeda /// padding would lead to a size larger than `isize::MAX`). 487c3739801SMiguel Ojeda #[doc(hidden)] 488c3739801SMiguel Ojeda #[must_use] 489c3739801SMiguel Ojeda #[inline] 490c3739801SMiguel Ojeda pub const fn pad_to_align(self) -> Self { 491c3739801SMiguel Ojeda use util::padding_needed_for; 492c3739801SMiguel Ojeda 493c3739801SMiguel Ojeda let (static_padding, size_info) = match self.size_info { 494c3739801SMiguel Ojeda // For sized layouts, we add the minimum amount of trailing padding 495c3739801SMiguel Ojeda // needed to satisfy alignment. 496c3739801SMiguel Ojeda SizeInfo::Sized { size: unpadded_size } => { 497c3739801SMiguel Ojeda let padding = padding_needed_for(unpadded_size, self.align); 498c3739801SMiguel Ojeda let size = match unpadded_size.checked_add(padding) { 499c3739801SMiguel Ojeda Some(size) => size, 500c3739801SMiguel Ojeda None => const_panic!("Adding padding caused size to overflow `usize`."), 501c3739801SMiguel Ojeda }; 502c3739801SMiguel Ojeda (padding, SizeInfo::Sized { size }) 503c3739801SMiguel Ojeda } 504c3739801SMiguel Ojeda // For DST layouts, trailing padding depends on the length of the 505c3739801SMiguel Ojeda // trailing DST and is computed at runtime. This does not alter the 506c3739801SMiguel Ojeda // offset or element size of the layout, so we leave `size_info` 507c3739801SMiguel Ojeda // unchanged. 508c3739801SMiguel Ojeda size_info @ SizeInfo::SliceDst(_) => (0, size_info), 509c3739801SMiguel Ojeda }; 510c3739801SMiguel Ojeda 511c3739801SMiguel Ojeda let statically_shallow_unpadded = self.statically_shallow_unpadded && static_padding == 0; 512c3739801SMiguel Ojeda 513c3739801SMiguel Ojeda DstLayout { align: self.align, size_info, statically_shallow_unpadded } 514c3739801SMiguel Ojeda } 515c3739801SMiguel Ojeda 516c3739801SMiguel Ojeda /// Produces `true` if `self` requires static padding; otherwise `false`. 517c3739801SMiguel Ojeda #[must_use] 518c3739801SMiguel Ojeda #[inline(always)] 519c3739801SMiguel Ojeda pub const fn requires_static_padding(self) -> bool { 520c3739801SMiguel Ojeda !self.statically_shallow_unpadded 521c3739801SMiguel Ojeda } 522c3739801SMiguel Ojeda 523c3739801SMiguel Ojeda /// Produces `true` if there exists any metadata for which a type of layout 524c3739801SMiguel Ojeda /// `self` would require dynamic trailing padding; otherwise `false`. 525c3739801SMiguel Ojeda #[must_use] 526c3739801SMiguel Ojeda #[inline(always)] 527c3739801SMiguel Ojeda pub const fn requires_dynamic_padding(self) -> bool { 528c3739801SMiguel Ojeda // A `% self.align.get()` cannot panic, since `align` is non-zero. 529c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 530c3739801SMiguel Ojeda match self.size_info { 531c3739801SMiguel Ojeda SizeInfo::Sized { .. } => false, 532c3739801SMiguel Ojeda SizeInfo::SliceDst(trailing_slice_layout) => { 533c3739801SMiguel Ojeda // SAFETY: This predicate is formally proved sound by 534c3739801SMiguel Ojeda // `proofs::prove_requires_dynamic_padding`. 535c3739801SMiguel Ojeda trailing_slice_layout.offset % self.align.get() != 0 536c3739801SMiguel Ojeda || trailing_slice_layout.elem_size % self.align.get() != 0 537c3739801SMiguel Ojeda } 538c3739801SMiguel Ojeda } 539c3739801SMiguel Ojeda } 540c3739801SMiguel Ojeda 541c3739801SMiguel Ojeda /// Validates that a cast is sound from a layout perspective. 542c3739801SMiguel Ojeda /// 543c3739801SMiguel Ojeda /// Validates that the size and alignment requirements of a type with the 544c3739801SMiguel Ojeda /// layout described in `self` would not be violated by performing a 545c3739801SMiguel Ojeda /// `cast_type` cast from a pointer with address `addr` which refers to a 546c3739801SMiguel Ojeda /// memory region of size `bytes_len`. 547c3739801SMiguel Ojeda /// 548c3739801SMiguel Ojeda /// If the cast is valid, `validate_cast_and_convert_metadata` returns 549c3739801SMiguel Ojeda /// `(elems, split_at)`. If `self` describes a dynamically-sized type, then 550c3739801SMiguel Ojeda /// `elems` is the maximum number of trailing slice elements for which a 551c3739801SMiguel Ojeda /// cast would be valid (for sized types, `elem` is meaningless and should 552c3739801SMiguel Ojeda /// be ignored). `split_at` is the index at which to split the memory region 553c3739801SMiguel Ojeda /// in order for the prefix (suffix) to contain the result of the cast, and 554c3739801SMiguel Ojeda /// in order for the remaining suffix (prefix) to contain the leftover 555c3739801SMiguel Ojeda /// bytes. 556c3739801SMiguel Ojeda /// 557c3739801SMiguel Ojeda /// There are three conditions under which a cast can fail: 558c3739801SMiguel Ojeda /// - The smallest possible value for the type is larger than the provided 559c3739801SMiguel Ojeda /// memory region 560c3739801SMiguel Ojeda /// - A prefix cast is requested, and `addr` does not satisfy `self`'s 561c3739801SMiguel Ojeda /// alignment requirement 562c3739801SMiguel Ojeda /// - A suffix cast is requested, and `addr + bytes_len` does not satisfy 563c3739801SMiguel Ojeda /// `self`'s alignment requirement (as a consequence, since all instances 564c3739801SMiguel Ojeda /// of the type are a multiple of its alignment, no size for the type will 565c3739801SMiguel Ojeda /// result in a starting address which is properly aligned) 566c3739801SMiguel Ojeda /// 567c3739801SMiguel Ojeda /// # Safety 568c3739801SMiguel Ojeda /// 569c3739801SMiguel Ojeda /// The caller may assume that this implementation is correct, and may rely 570c3739801SMiguel Ojeda /// on that assumption for the soundness of their code. In particular, the 571c3739801SMiguel Ojeda /// caller may assume that, if `validate_cast_and_convert_metadata` returns 572c3739801SMiguel Ojeda /// `Some((elems, split_at))`, then: 573c3739801SMiguel Ojeda /// - A pointer to the type (for dynamically sized types, this includes 574c3739801SMiguel Ojeda /// `elems` as its pointer metadata) describes an object of size `size <= 575c3739801SMiguel Ojeda /// bytes_len` 576c3739801SMiguel Ojeda /// - If this is a prefix cast: 577c3739801SMiguel Ojeda /// - `addr` satisfies `self`'s alignment 578c3739801SMiguel Ojeda /// - `size == split_at` 579c3739801SMiguel Ojeda /// - If this is a suffix cast: 580c3739801SMiguel Ojeda /// - `split_at == bytes_len - size` 581c3739801SMiguel Ojeda /// - `addr + split_at` satisfies `self`'s alignment 582c3739801SMiguel Ojeda /// 583c3739801SMiguel Ojeda /// Note that this method does *not* ensure that a pointer constructed from 584c3739801SMiguel Ojeda /// its return values will be a valid pointer. In particular, this method 585c3739801SMiguel Ojeda /// does not reason about `isize` overflow, which is a requirement of many 586c3739801SMiguel Ojeda /// Rust pointer APIs, and may at some point be determined to be a validity 587c3739801SMiguel Ojeda /// invariant of pointer types themselves. This should never be a problem so 588c3739801SMiguel Ojeda /// long as the arguments to this method are derived from a known-valid 589c3739801SMiguel Ojeda /// pointer (e.g., one derived from a safe Rust reference), but it is 590c3739801SMiguel Ojeda /// nonetheless the caller's responsibility to justify that pointer 591c3739801SMiguel Ojeda /// arithmetic will not overflow based on a safety argument *other than* the 592c3739801SMiguel Ojeda /// mere fact that this method returned successfully. 593c3739801SMiguel Ojeda /// 594c3739801SMiguel Ojeda /// # Panics 595c3739801SMiguel Ojeda /// 596c3739801SMiguel Ojeda /// `validate_cast_and_convert_metadata` will panic if `self` describes a 597c3739801SMiguel Ojeda /// DST whose trailing slice element is zero-sized. 598c3739801SMiguel Ojeda /// 599c3739801SMiguel Ojeda /// If `addr + bytes_len` overflows `usize`, 600c3739801SMiguel Ojeda /// `validate_cast_and_convert_metadata` may panic, or it may return 601c3739801SMiguel Ojeda /// incorrect results. No guarantees are made about when 602c3739801SMiguel Ojeda /// `validate_cast_and_convert_metadata` will panic. The caller should not 603c3739801SMiguel Ojeda /// rely on `validate_cast_and_convert_metadata` panicking in any particular 604c3739801SMiguel Ojeda /// condition, even if `debug_assertions` are enabled. 605c3739801SMiguel Ojeda #[allow(unused)] 606c3739801SMiguel Ojeda #[inline(always)] 607c3739801SMiguel Ojeda pub(crate) const fn validate_cast_and_convert_metadata( 608c3739801SMiguel Ojeda &self, 609c3739801SMiguel Ojeda addr: usize, 610c3739801SMiguel Ojeda bytes_len: usize, 611c3739801SMiguel Ojeda cast_type: CastType, 612c3739801SMiguel Ojeda ) -> Result<(usize, usize), MetadataCastError> { 613c3739801SMiguel Ojeda // `debug_assert!`, but with `#[allow(clippy::arithmetic_side_effects)]`. 614c3739801SMiguel Ojeda macro_rules! __const_debug_assert { 615c3739801SMiguel Ojeda ($e:expr $(, $msg:expr)?) => { 616c3739801SMiguel Ojeda const_debug_assert!({ 617c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 618c3739801SMiguel Ojeda let e = $e; 619c3739801SMiguel Ojeda e 620c3739801SMiguel Ojeda } $(, $msg)?); 621c3739801SMiguel Ojeda }; 622c3739801SMiguel Ojeda } 623c3739801SMiguel Ojeda 624c3739801SMiguel Ojeda // Note that, in practice, `self` is always a compile-time constant. We 625c3739801SMiguel Ojeda // do this check earlier than needed to ensure that we always panic as a 626c3739801SMiguel Ojeda // result of bugs in the program (such as calling this function on an 627c3739801SMiguel Ojeda // invalid type) instead of allowing this panic to be hidden if the cast 628c3739801SMiguel Ojeda // would have failed anyway for runtime reasons (such as a too-small 629c3739801SMiguel Ojeda // memory region). 630c3739801SMiguel Ojeda // 631c3739801SMiguel Ojeda // FIXME(#67): Once our MSRV is 1.65, use let-else: 632c3739801SMiguel Ojeda // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements 633c3739801SMiguel Ojeda let size_info = match self.size_info.try_to_nonzero_elem_size() { 634c3739801SMiguel Ojeda Some(size_info) => size_info, 635c3739801SMiguel Ojeda None => const_panic!("attempted to cast to slice type with zero-sized element"), 636c3739801SMiguel Ojeda }; 637c3739801SMiguel Ojeda 638c3739801SMiguel Ojeda // Precondition 639c3739801SMiguel Ojeda __const_debug_assert!( 640c3739801SMiguel Ojeda addr.checked_add(bytes_len).is_some(), 641c3739801SMiguel Ojeda "`addr` + `bytes_len` > usize::MAX" 642c3739801SMiguel Ojeda ); 643c3739801SMiguel Ojeda 644c3739801SMiguel Ojeda // Alignment checks go in their own block to avoid introducing variables 645c3739801SMiguel Ojeda // into the top-level scope. 646c3739801SMiguel Ojeda { 647c3739801SMiguel Ojeda // We check alignment for `addr` (for prefix casts) or `addr + 648c3739801SMiguel Ojeda // bytes_len` (for suffix casts). For a prefix cast, the correctness 649c3739801SMiguel Ojeda // of this check is trivial - `addr` is the address the object will 650c3739801SMiguel Ojeda // live at. 651c3739801SMiguel Ojeda // 652c3739801SMiguel Ojeda // For a suffix cast, we know that all valid sizes for the type are 653c3739801SMiguel Ojeda // a multiple of the alignment (and by safety precondition, we know 654c3739801SMiguel Ojeda // `DstLayout` may only describe valid Rust types). Thus, a 655c3739801SMiguel Ojeda // validly-sized instance which lives at a validly-aligned address 656c3739801SMiguel Ojeda // must also end at a validly-aligned address. Thus, if the end 657c3739801SMiguel Ojeda // address for a suffix cast (`addr + bytes_len`) is not aligned, 658c3739801SMiguel Ojeda // then no valid start address will be aligned either. 659c3739801SMiguel Ojeda let offset = match cast_type { 660c3739801SMiguel Ojeda CastType::Prefix => 0, 661c3739801SMiguel Ojeda CastType::Suffix => bytes_len, 662c3739801SMiguel Ojeda }; 663c3739801SMiguel Ojeda 664c3739801SMiguel Ojeda // Addition is guaranteed not to overflow because `offset <= 665c3739801SMiguel Ojeda // bytes_len`, and `addr + bytes_len <= usize::MAX` is a 666c3739801SMiguel Ojeda // precondition of this method. Modulus is guaranteed not to divide 667c3739801SMiguel Ojeda // by 0 because `align` is non-zero. 668c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 669c3739801SMiguel Ojeda if (addr + offset) % self.align.get() != 0 { 670c3739801SMiguel Ojeda return Err(MetadataCastError::Alignment); 671c3739801SMiguel Ojeda } 672c3739801SMiguel Ojeda } 673c3739801SMiguel Ojeda 674c3739801SMiguel Ojeda let (elems, self_bytes) = match size_info { 675c3739801SMiguel Ojeda SizeInfo::Sized { size } => { 676c3739801SMiguel Ojeda if size > bytes_len { 677c3739801SMiguel Ojeda return Err(MetadataCastError::Size); 678c3739801SMiguel Ojeda } 679c3739801SMiguel Ojeda (0, size) 680c3739801SMiguel Ojeda } 681c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => { 682c3739801SMiguel Ojeda // Calculate the maximum number of bytes that could be consumed 683c3739801SMiguel Ojeda // - any number of bytes larger than this will either not be a 684c3739801SMiguel Ojeda // multiple of the alignment, or will be larger than 685c3739801SMiguel Ojeda // `bytes_len`. 686c3739801SMiguel Ojeda let max_total_bytes = 687c3739801SMiguel Ojeda util::round_down_to_next_multiple_of_alignment(bytes_len, self.align); 688c3739801SMiguel Ojeda // Calculate the maximum number of bytes that could be consumed 689c3739801SMiguel Ojeda // by the trailing slice. 690c3739801SMiguel Ojeda // 691c3739801SMiguel Ojeda // FIXME(#67): Once our MSRV is 1.65, use let-else: 692c3739801SMiguel Ojeda // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements 693c3739801SMiguel Ojeda let max_slice_and_padding_bytes = match max_total_bytes.checked_sub(offset) { 694c3739801SMiguel Ojeda Some(max) => max, 695c3739801SMiguel Ojeda // `bytes_len` too small even for 0 trailing slice elements. 696c3739801SMiguel Ojeda None => return Err(MetadataCastError::Size), 697c3739801SMiguel Ojeda }; 698c3739801SMiguel Ojeda 699c3739801SMiguel Ojeda // Calculate the number of elements that fit in 700c3739801SMiguel Ojeda // `max_slice_and_padding_bytes`; any remaining bytes will be 701c3739801SMiguel Ojeda // considered padding. 702c3739801SMiguel Ojeda // 703c3739801SMiguel Ojeda // Guaranteed not to divide by zero: `elem_size` is non-zero. 704c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 705c3739801SMiguel Ojeda let elems = max_slice_and_padding_bytes / elem_size.get(); 706c3739801SMiguel Ojeda // Guaranteed not to overflow on multiplication: `usize::MAX >= 707c3739801SMiguel Ojeda // max_slice_and_padding_bytes >= (max_slice_and_padding_bytes / 708c3739801SMiguel Ojeda // elem_size) * elem_size`. 709c3739801SMiguel Ojeda // 710c3739801SMiguel Ojeda // Guaranteed not to overflow on addition: 711c3739801SMiguel Ojeda // - max_slice_and_padding_bytes == max_total_bytes - offset 712c3739801SMiguel Ojeda // - elems * elem_size <= max_slice_and_padding_bytes == max_total_bytes - offset 713c3739801SMiguel Ojeda // - elems * elem_size + offset <= max_total_bytes <= usize::MAX 714c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 715c3739801SMiguel Ojeda let without_padding = offset + elems * elem_size.get(); 716c3739801SMiguel Ojeda // `self_bytes` is equal to the offset bytes plus the bytes 717c3739801SMiguel Ojeda // consumed by the trailing slice plus any padding bytes 718c3739801SMiguel Ojeda // required to satisfy the alignment. Note that we have computed 719c3739801SMiguel Ojeda // the maximum number of trailing slice elements that could fit 720c3739801SMiguel Ojeda // in `self_bytes`, so any padding is guaranteed to be less than 721c3739801SMiguel Ojeda // the size of an extra element. 722c3739801SMiguel Ojeda // 723c3739801SMiguel Ojeda // Guaranteed not to overflow: 724c3739801SMiguel Ojeda // - By previous comment: without_padding == elems * elem_size + 725c3739801SMiguel Ojeda // offset <= max_total_bytes 726c3739801SMiguel Ojeda // - By construction, `max_total_bytes` is a multiple of 727c3739801SMiguel Ojeda // `self.align`. 728c3739801SMiguel Ojeda // - At most, adding padding needed to round `without_padding` 729c3739801SMiguel Ojeda // up to the next multiple of the alignment will bring 730c3739801SMiguel Ojeda // `self_bytes` up to `max_total_bytes`. 731c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 732c3739801SMiguel Ojeda let self_bytes = 733c3739801SMiguel Ojeda without_padding + util::padding_needed_for(without_padding, self.align); 734c3739801SMiguel Ojeda (elems, self_bytes) 735c3739801SMiguel Ojeda } 736c3739801SMiguel Ojeda }; 737c3739801SMiguel Ojeda 738c3739801SMiguel Ojeda __const_debug_assert!(self_bytes <= bytes_len); 739c3739801SMiguel Ojeda 740c3739801SMiguel Ojeda let split_at = match cast_type { 741c3739801SMiguel Ojeda CastType::Prefix => self_bytes, 742c3739801SMiguel Ojeda // Guaranteed not to underflow: 743c3739801SMiguel Ojeda // - In the `Sized` branch, only returns `size` if `size <= 744c3739801SMiguel Ojeda // bytes_len`. 745c3739801SMiguel Ojeda // - In the `SliceDst` branch, calculates `self_bytes <= 746c3739801SMiguel Ojeda // max_toatl_bytes`, which is upper-bounded by `bytes_len`. 747c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 748c3739801SMiguel Ojeda CastType::Suffix => bytes_len - self_bytes, 749c3739801SMiguel Ojeda }; 750c3739801SMiguel Ojeda 751c3739801SMiguel Ojeda Ok((elems, split_at)) 752c3739801SMiguel Ojeda } 753c3739801SMiguel Ojeda } 754c3739801SMiguel Ojeda 755c3739801SMiguel Ojeda pub(crate) use cast_from::CastFrom; 756c3739801SMiguel Ojeda mod cast_from { 757c3739801SMiguel Ojeda use crate::*; 758c3739801SMiguel Ojeda 759c3739801SMiguel Ojeda pub(crate) struct CastFrom<Dst: ?Sized> { 760c3739801SMiguel Ojeda _never: core::convert::Infallible, 761c3739801SMiguel Ojeda _marker: PhantomData<Dst>, 762c3739801SMiguel Ojeda } 763c3739801SMiguel Ojeda 764c3739801SMiguel Ojeda // SAFETY: The implementation of `Project::project` preserves the address 765c3739801SMiguel Ojeda // of the referent – it only modifies pointer metadata. 766c3739801SMiguel Ojeda unsafe impl<Src, Dst> crate::pointer::cast::Cast<Src, Dst> for CastFrom<Dst> 767c3739801SMiguel Ojeda where 768c3739801SMiguel Ojeda Src: KnownLayout + ?Sized, 769c3739801SMiguel Ojeda Dst: KnownLayout + ?Sized, 770c3739801SMiguel Ojeda { 771c3739801SMiguel Ojeda } 772c3739801SMiguel Ojeda 773c3739801SMiguel Ojeda // SAFETY: The implementation of `Project::project` preserves the size of 774c3739801SMiguel Ojeda // the referent (see inline comments for a more detailed proof of this). 775c3739801SMiguel Ojeda unsafe impl<Src, Dst> crate::pointer::cast::CastExact<Src, Dst> for CastFrom<Dst> 776c3739801SMiguel Ojeda where 777c3739801SMiguel Ojeda Src: KnownLayout + ?Sized, 778c3739801SMiguel Ojeda Dst: KnownLayout + ?Sized, 779c3739801SMiguel Ojeda { 780c3739801SMiguel Ojeda } 781c3739801SMiguel Ojeda 782c3739801SMiguel Ojeda // SAFETY: `project` produces a pointer which refers to the same referent 783c3739801SMiguel Ojeda // bytes as its input, or to a subset of them (see inline comments for a 784c3739801SMiguel Ojeda // more detailed proof of this). It does this using provenance-preserving 785c3739801SMiguel Ojeda // operations. 786c3739801SMiguel Ojeda unsafe impl<Src, Dst> crate::pointer::cast::Project<Src, Dst> for CastFrom<Dst> 787c3739801SMiguel Ojeda where 788c3739801SMiguel Ojeda Src: KnownLayout + ?Sized, 789c3739801SMiguel Ojeda Dst: KnownLayout + ?Sized, 790c3739801SMiguel Ojeda { 791c3739801SMiguel Ojeda /// # PME 792c3739801SMiguel Ojeda /// 793c3739801SMiguel Ojeda /// Generates a post-monomorphization error if it is not possible to 794c3739801SMiguel Ojeda /// implement soundly. 795c3739801SMiguel Ojeda // 796c3739801SMiguel Ojeda // FIXME(#1817): Support Sized->Unsized and Unsized->Sized casts 797c3739801SMiguel Ojeda fn project(src: PtrInner<'_, Src>) -> *mut Dst { 798c3739801SMiguel Ojeda /// The parameters required in order to perform a pointer cast from 799c3739801SMiguel Ojeda /// `Src` to `Dst`. 800c3739801SMiguel Ojeda /// 801c3739801SMiguel Ojeda /// These are a compile-time function of the layouts of `Src` 802c3739801SMiguel Ojeda /// and `Dst`. 803c3739801SMiguel Ojeda /// 804c3739801SMiguel Ojeda /// # Safety 805c3739801SMiguel Ojeda /// 806c3739801SMiguel Ojeda /// `Src`'s alignment must not be smaller than `Dst`'s alignment. 807c3739801SMiguel Ojeda struct CastParams<Src: ?Sized, Dst: ?Sized> { 808c3739801SMiguel Ojeda inner: CastParamsInner, 809c3739801SMiguel Ojeda _src: PhantomData<Src>, 810c3739801SMiguel Ojeda _dst: PhantomData<Dst>, 811c3739801SMiguel Ojeda } 812c3739801SMiguel Ojeda 813c3739801SMiguel Ojeda #[derive(Copy, Clone)] 814c3739801SMiguel Ojeda enum CastParamsInner { 815c3739801SMiguel Ojeda // At compile time (specifically, post-monomorphization time), 816c3739801SMiguel Ojeda // we need to compute two things: 817c3739801SMiguel Ojeda // - Whether, given *any* `*Src`, it is possible to construct a 818c3739801SMiguel Ojeda // `*Dst` which addresses the same number of bytes (ie, 819c3739801SMiguel Ojeda // whether, for any `Src` pointer metadata, there exists `Dst` 820c3739801SMiguel Ojeda // pointer metadata that addresses the same number of bytes) 821c3739801SMiguel Ojeda // - If this is possible, any information necessary to perform 822c3739801SMiguel Ojeda // the `Src`->`Dst` metadata conversion at runtime. 823c3739801SMiguel Ojeda // 824c3739801SMiguel Ojeda // Assume that `Src` and `Dst` are slice DSTs, and define: 825c3739801SMiguel Ojeda // - `S_OFF = Src::LAYOUT.size_info.offset` 826c3739801SMiguel Ojeda // - `S_ELEM = Src::LAYOUT.size_info.elem_size` 827c3739801SMiguel Ojeda // - `D_OFF = Dst::LAYOUT.size_info.offset` 828c3739801SMiguel Ojeda // - `D_ELEM = Dst::LAYOUT.size_info.elem_size` 829c3739801SMiguel Ojeda // 830c3739801SMiguel Ojeda // We are trying to solve the following equation: 831c3739801SMiguel Ojeda // 832c3739801SMiguel Ojeda // D_OFF + d_meta * D_ELEM = S_OFF + s_meta * S_ELEM 833c3739801SMiguel Ojeda // 834c3739801SMiguel Ojeda // At runtime, we will be attempting to compute `d_meta`, given 835c3739801SMiguel Ojeda // `s_meta` (a runtime value) and all other parameters (which 836c3739801SMiguel Ojeda // are compile-time values). We can solve like so: 837c3739801SMiguel Ojeda // 838c3739801SMiguel Ojeda // D_OFF + d_meta * D_ELEM = S_OFF + s_meta * S_ELEM 839c3739801SMiguel Ojeda // 840c3739801SMiguel Ojeda // d_meta * D_ELEM = S_OFF - D_OFF + s_meta * S_ELEM 841c3739801SMiguel Ojeda // 842c3739801SMiguel Ojeda // d_meta = (S_OFF - D_OFF + s_meta * S_ELEM)/D_ELEM 843c3739801SMiguel Ojeda // 844c3739801SMiguel Ojeda // Since `d_meta` will be a `usize`, we need the right-hand side 845c3739801SMiguel Ojeda // to be an integer, and this needs to hold for *any* value of 846c3739801SMiguel Ojeda // `s_meta` (in order for our conversion to be infallible - ie, 847c3739801SMiguel Ojeda // to not have to reject certain values of `s_meta` at runtime). 848c3739801SMiguel Ojeda // This means that: 849c3739801SMiguel Ojeda // 850c3739801SMiguel Ojeda // - `s_meta * S_ELEM` must be a multiple of `D_ELEM` 851c3739801SMiguel Ojeda // - Since this must hold for any value of `s_meta`, `S_ELEM` 852c3739801SMiguel Ojeda // must be a multiple of `D_ELEM` 853c3739801SMiguel Ojeda // - `S_OFF - D_OFF` must be a multiple of `D_ELEM` 854c3739801SMiguel Ojeda // 855c3739801SMiguel Ojeda // Thus, let `OFFSET_DELTA_ELEMS = (S_OFF - D_OFF)/D_ELEM` and 856c3739801SMiguel Ojeda // `ELEM_MULTIPLE = S_ELEM/D_ELEM`. We can rewrite the above 857c3739801SMiguel Ojeda // expression as: 858c3739801SMiguel Ojeda // 859c3739801SMiguel Ojeda // d_meta = (S_OFF - D_OFF + s_meta * S_ELEM)/D_ELEM 860c3739801SMiguel Ojeda // 861c3739801SMiguel Ojeda // d_meta = OFFSET_DELTA_ELEMS + s_meta * ELEM_MULTIPLE 862c3739801SMiguel Ojeda // 863c3739801SMiguel Ojeda // Thus, we just need to compute the following and confirm that 864c3739801SMiguel Ojeda // they have integer solutions in order to both a) determine 865c3739801SMiguel Ojeda // whether infallible `Src` -> `Dst` casts are possible and, b) 866c3739801SMiguel Ojeda // pre-compute the parameters necessary to perform those casts 867c3739801SMiguel Ojeda // at runtime. These parameters are encapsulated in 868c3739801SMiguel Ojeda // `CastParams`, which acts as a witness that such infallible 869c3739801SMiguel Ojeda // casts are possible. 870c3739801SMiguel Ojeda /// The parameters required in order to perform an 871c3739801SMiguel Ojeda /// unsized-to-unsized pointer cast from `Src` to `Dst` as 872c3739801SMiguel Ojeda /// described above. 873c3739801SMiguel Ojeda /// 874c3739801SMiguel Ojeda /// # Safety 875c3739801SMiguel Ojeda /// 876c3739801SMiguel Ojeda /// `Src` and `Dst` must both be slice DSTs. 877c3739801SMiguel Ojeda /// 878c3739801SMiguel Ojeda /// `offset_delta_elems` and `elem_multiple` must be valid as 879c3739801SMiguel Ojeda /// described above. 880c3739801SMiguel Ojeda UnsizedToUnsized { offset_delta_elems: usize, elem_multiple: usize }, 881c3739801SMiguel Ojeda 882c3739801SMiguel Ojeda /// The metadata of a `Dst` which has the same size as `Src: 883c3739801SMiguel Ojeda /// Sized`. 884c3739801SMiguel Ojeda /// 885c3739801SMiguel Ojeda /// # Safety 886c3739801SMiguel Ojeda /// 887c3739801SMiguel Ojeda /// `Src: Sized` and `Dst` must be a slice DST. 888c3739801SMiguel Ojeda /// 889c3739801SMiguel Ojeda /// A raw `Dst` pointer with metadata `dst_meta` must address 890c3739801SMiguel Ojeda /// `size_of::<Src>()` bytes. 891c3739801SMiguel Ojeda SizedToUnsized { dst_meta: usize }, 892c3739801SMiguel Ojeda 893c3739801SMiguel Ojeda /// The metadata of a `Dst` which has the same size as `Src: 894c3739801SMiguel Ojeda /// Sized`. 895c3739801SMiguel Ojeda /// 896c3739801SMiguel Ojeda /// # Safety 897c3739801SMiguel Ojeda /// 898c3739801SMiguel Ojeda /// `Src` and `Dst` must both be `Sized` and `size_of::<Src>() 899c3739801SMiguel Ojeda /// == size_of::<Dst>()`. 900c3739801SMiguel Ojeda SizedToSized, 901c3739801SMiguel Ojeda } 902c3739801SMiguel Ojeda 903c3739801SMiguel Ojeda impl<Src: ?Sized, Dst: ?Sized> Copy for CastParams<Src, Dst> {} 904c3739801SMiguel Ojeda impl<Src: ?Sized, Dst: ?Sized> Clone for CastParams<Src, Dst> { 905c3739801SMiguel Ojeda fn clone(&self) -> Self { 906c3739801SMiguel Ojeda *self 907c3739801SMiguel Ojeda } 908c3739801SMiguel Ojeda } 909c3739801SMiguel Ojeda 910c3739801SMiguel Ojeda impl<Src: ?Sized, Dst: ?Sized> CastParams<Src, Dst> { 911c3739801SMiguel Ojeda const fn try_compute( 912c3739801SMiguel Ojeda src: &DstLayout, 913c3739801SMiguel Ojeda dst: &DstLayout, 914c3739801SMiguel Ojeda ) -> Option<CastParams<Src, Dst>> { 915c3739801SMiguel Ojeda if src.align.get() < dst.align.get() { 916c3739801SMiguel Ojeda return None; 917c3739801SMiguel Ojeda } 918c3739801SMiguel Ojeda 919c3739801SMiguel Ojeda let inner = match (src.size_info, dst.size_info) { 920c3739801SMiguel Ojeda ( 921c3739801SMiguel Ojeda SizeInfo::Sized { size: src_size }, 922c3739801SMiguel Ojeda SizeInfo::Sized { size: dst_size }, 923c3739801SMiguel Ojeda ) => { 924c3739801SMiguel Ojeda if src_size != dst_size { 925c3739801SMiguel Ojeda return None; 926c3739801SMiguel Ojeda } 927c3739801SMiguel Ojeda 928c3739801SMiguel Ojeda // SAFETY: We checked above that `src_size == 929c3739801SMiguel Ojeda // dst_size`. 930c3739801SMiguel Ojeda CastParamsInner::SizedToSized 931c3739801SMiguel Ojeda } 932c3739801SMiguel Ojeda (SizeInfo::Sized { size: src_size }, SizeInfo::SliceDst(dst)) => { 933c3739801SMiguel Ojeda let offset_delta = if let Some(od) = src_size.checked_sub(dst.offset) { 934c3739801SMiguel Ojeda od 935c3739801SMiguel Ojeda } else { 936c3739801SMiguel Ojeda return None; 937c3739801SMiguel Ojeda }; 938c3739801SMiguel Ojeda 939c3739801SMiguel Ojeda let dst_elem_size = if let Some(e) = NonZeroUsize::new(dst.elem_size) { 940c3739801SMiguel Ojeda e 941c3739801SMiguel Ojeda } else { 942c3739801SMiguel Ojeda return None; 943c3739801SMiguel Ojeda }; 944c3739801SMiguel Ojeda 945c3739801SMiguel Ojeda // PANICS: `dst_elem_size: NonZeroUsize`, so this won't 946c3739801SMiguel Ojeda // divide by zero. 947c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 948c3739801SMiguel Ojeda let delta_mod_other_elem = offset_delta % dst_elem_size.get(); 949c3739801SMiguel Ojeda 950c3739801SMiguel Ojeda if delta_mod_other_elem != 0 { 951c3739801SMiguel Ojeda return None; 952c3739801SMiguel Ojeda } 953c3739801SMiguel Ojeda 954c3739801SMiguel Ojeda // PANICS: `dst_elem_size: NonZeroUsize`, so this won't 955c3739801SMiguel Ojeda // divide by zero. 956c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 957c3739801SMiguel Ojeda let dst_meta = offset_delta / dst_elem_size.get(); 958c3739801SMiguel Ojeda 959c3739801SMiguel Ojeda // SAFETY: The preceding math ensures that a `Dst` 960c3739801SMiguel Ojeda // with `dst_meta` addresses `src_size` bytes. 961c3739801SMiguel Ojeda CastParamsInner::SizedToUnsized { dst_meta } 962c3739801SMiguel Ojeda } 963c3739801SMiguel Ojeda (SizeInfo::SliceDst(src), SizeInfo::SliceDst(dst)) => { 964c3739801SMiguel Ojeda let offset_delta = if let Some(od) = src.offset.checked_sub(dst.offset) 965c3739801SMiguel Ojeda { 966c3739801SMiguel Ojeda od 967c3739801SMiguel Ojeda } else { 968c3739801SMiguel Ojeda return None; 969c3739801SMiguel Ojeda }; 970c3739801SMiguel Ojeda 971c3739801SMiguel Ojeda let dst_elem_size = if let Some(e) = NonZeroUsize::new(dst.elem_size) { 972c3739801SMiguel Ojeda e 973c3739801SMiguel Ojeda } else { 974c3739801SMiguel Ojeda return None; 975c3739801SMiguel Ojeda }; 976c3739801SMiguel Ojeda 977c3739801SMiguel Ojeda // PANICS: `dst_elem_size: NonZeroUsize`, so this won't 978c3739801SMiguel Ojeda // divide by zero. 979c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 980c3739801SMiguel Ojeda let delta_mod_other_elem = offset_delta % dst_elem_size.get(); 981c3739801SMiguel Ojeda 982c3739801SMiguel Ojeda // PANICS: `dst_elem_size: NonZeroUsize`, so this won't 983c3739801SMiguel Ojeda // divide by zero. 984c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 985c3739801SMiguel Ojeda let elem_remainder = src.elem_size % dst_elem_size.get(); 986c3739801SMiguel Ojeda 987c3739801SMiguel Ojeda if delta_mod_other_elem != 0 988c3739801SMiguel Ojeda || src.elem_size < dst.elem_size 989c3739801SMiguel Ojeda || elem_remainder != 0 990c3739801SMiguel Ojeda { 991c3739801SMiguel Ojeda return None; 992c3739801SMiguel Ojeda } 993c3739801SMiguel Ojeda 994c3739801SMiguel Ojeda // PANICS: `dst_elem_size: NonZeroUsize`, so this won't 995c3739801SMiguel Ojeda // divide by zero. 996c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 997c3739801SMiguel Ojeda let offset_delta_elems = offset_delta / dst_elem_size.get(); 998c3739801SMiguel Ojeda 999c3739801SMiguel Ojeda // PANICS: `dst_elem_size: NonZeroUsize`, so this won't 1000c3739801SMiguel Ojeda // divide by zero. 1001c3739801SMiguel Ojeda #[allow(clippy::arithmetic_side_effects)] 1002c3739801SMiguel Ojeda let elem_multiple = src.elem_size / dst_elem_size.get(); 1003c3739801SMiguel Ojeda 1004c3739801SMiguel Ojeda CastParamsInner::UnsizedToUnsized { 1005c3739801SMiguel Ojeda // SAFETY: We checked above that this is an exact ratio. 1006c3739801SMiguel Ojeda offset_delta_elems, 1007c3739801SMiguel Ojeda // SAFETY: We checked above that this is an exact ratio. 1008c3739801SMiguel Ojeda elem_multiple, 1009c3739801SMiguel Ojeda } 1010c3739801SMiguel Ojeda } 1011c3739801SMiguel Ojeda _ => return None, 1012c3739801SMiguel Ojeda }; 1013c3739801SMiguel Ojeda 1014c3739801SMiguel Ojeda // SAFETY: We checked above that `src.align >= dst.align`. 1015c3739801SMiguel Ojeda Some(CastParams { inner, _src: PhantomData, _dst: PhantomData }) 1016c3739801SMiguel Ojeda } 1017c3739801SMiguel Ojeda } 1018c3739801SMiguel Ojeda 1019c3739801SMiguel Ojeda impl<Src: KnownLayout + ?Sized, Dst: KnownLayout + ?Sized> CastParams<Src, Dst> { 1020c3739801SMiguel Ojeda /// # Safety 1021c3739801SMiguel Ojeda /// 1022c3739801SMiguel Ojeda /// `src_meta` describes a `Src` whose size is no larger than 1023c3739801SMiguel Ojeda /// `isize::MAX`. 1024c3739801SMiguel Ojeda /// 1025c3739801SMiguel Ojeda /// The returned metadata describes a `Dst` of the same size as 1026c3739801SMiguel Ojeda /// the original `Src`. 1027c3739801SMiguel Ojeda #[inline(always)] 1028c3739801SMiguel Ojeda unsafe fn cast_metadata( 1029c3739801SMiguel Ojeda self, 1030c3739801SMiguel Ojeda src_meta: Src::PointerMetadata, 1031c3739801SMiguel Ojeda ) -> Dst::PointerMetadata { 1032c3739801SMiguel Ojeda #[allow(unused)] 1033c3739801SMiguel Ojeda use crate::util::polyfills::*; 1034c3739801SMiguel Ojeda 1035c3739801SMiguel Ojeda let dst_meta = match self.inner { 1036c3739801SMiguel Ojeda CastParamsInner::UnsizedToUnsized { offset_delta_elems, elem_multiple } => { 1037c3739801SMiguel Ojeda let src_meta = src_meta.to_elem_count(); 1038c3739801SMiguel Ojeda #[allow( 1039c3739801SMiguel Ojeda unstable_name_collisions, 1040c3739801SMiguel Ojeda clippy::multiple_unsafe_ops_per_block 1041c3739801SMiguel Ojeda )] 1042c3739801SMiguel Ojeda // SAFETY: `self` is a witness that the following 1043c3739801SMiguel Ojeda // equation holds: 1044c3739801SMiguel Ojeda // 1045c3739801SMiguel Ojeda // D_OFF + d_meta * D_ELEM = S_OFF + s_meta * S_ELEM 1046c3739801SMiguel Ojeda // 1047c3739801SMiguel Ojeda // Since the caller promises that `src_meta` is 1048c3739801SMiguel Ojeda // valid `Src` metadata, this math will not 1049c3739801SMiguel Ojeda // overflow, and the returned value will describe a 1050c3739801SMiguel Ojeda // `Dst` of the same size. 1051c3739801SMiguel Ojeda unsafe { 1052c3739801SMiguel Ojeda offset_delta_elems 1053c3739801SMiguel Ojeda .unchecked_add(src_meta.unchecked_mul(elem_multiple)) 1054c3739801SMiguel Ojeda } 1055c3739801SMiguel Ojeda } 1056c3739801SMiguel Ojeda CastParamsInner::SizedToUnsized { dst_meta } => dst_meta, 1057c3739801SMiguel Ojeda CastParamsInner::SizedToSized => 0, 1058c3739801SMiguel Ojeda }; 1059c3739801SMiguel Ojeda Dst::PointerMetadata::from_elem_count(dst_meta) 1060c3739801SMiguel Ojeda } 1061c3739801SMiguel Ojeda } 1062c3739801SMiguel Ojeda 1063c3739801SMiguel Ojeda trait Params<Src: ?Sized> { 1064c3739801SMiguel Ojeda const CAST_PARAMS: CastParams<Src, Self>; 1065c3739801SMiguel Ojeda } 1066c3739801SMiguel Ojeda 1067c3739801SMiguel Ojeda impl<Src, Dst> Params<Src> for Dst 1068c3739801SMiguel Ojeda where 1069c3739801SMiguel Ojeda Src: KnownLayout + ?Sized, 1070c3739801SMiguel Ojeda Dst: KnownLayout + ?Sized, 1071c3739801SMiguel Ojeda { 1072c3739801SMiguel Ojeda const CAST_PARAMS: CastParams<Src, Dst> = 1073c3739801SMiguel Ojeda match CastParams::try_compute(&Src::LAYOUT, &Dst::LAYOUT) { 1074c3739801SMiguel Ojeda Some(params) => params, 1075c3739801SMiguel Ojeda None => const_panic!( 1076c3739801SMiguel Ojeda "cannot `transmute_ref!` or `transmute_mut!` between incompatible types" 1077c3739801SMiguel Ojeda ), 1078c3739801SMiguel Ojeda }; 1079c3739801SMiguel Ojeda } 1080c3739801SMiguel Ojeda 1081c3739801SMiguel Ojeda let src_meta = <Src as KnownLayout>::pointer_to_metadata(src.as_ptr()); 1082c3739801SMiguel Ojeda let params = <Dst as Params<Src>>::CAST_PARAMS; 1083c3739801SMiguel Ojeda 1084c3739801SMiguel Ojeda // SAFETY: `src: PtrInner` guarantees that `src`'s referent is zero 1085c3739801SMiguel Ojeda // bytes or lives in a single allocation, which means that it is no 1086c3739801SMiguel Ojeda // larger than `isize::MAX` bytes [1]. 1087c3739801SMiguel Ojeda // 1088c3739801SMiguel Ojeda // [1] https://doc.rust-lang.org/1.92.0/std/ptr/index.html#allocation 1089c3739801SMiguel Ojeda let dst_meta = unsafe { params.cast_metadata(src_meta) }; 1090c3739801SMiguel Ojeda 1091c3739801SMiguel Ojeda <Dst as KnownLayout>::raw_from_ptr_len(src.as_non_null().cast(), dst_meta).as_ptr() 1092c3739801SMiguel Ojeda } 1093c3739801SMiguel Ojeda } 1094c3739801SMiguel Ojeda } 1095c3739801SMiguel Ojeda 1096c3739801SMiguel Ojeda // FIXME(#67): For some reason, on our MSRV toolchain, this `allow` isn't 1097c3739801SMiguel Ojeda // enforced despite having `#![allow(unknown_lints)]` at the crate root, but 1098c3739801SMiguel Ojeda // putting it here works. Once our MSRV is high enough that this bug has been 1099c3739801SMiguel Ojeda // fixed, remove this `allow`. 1100c3739801SMiguel Ojeda #[allow(unknown_lints)] 1101c3739801SMiguel Ojeda #[cfg(test)] 1102c3739801SMiguel Ojeda mod tests { 1103c3739801SMiguel Ojeda use super::*; 1104c3739801SMiguel Ojeda 1105c3739801SMiguel Ojeda #[test] 1106c3739801SMiguel Ojeda fn test_dst_layout_for_slice() { 1107c3739801SMiguel Ojeda let layout = DstLayout::for_slice::<u32>(); 1108c3739801SMiguel Ojeda match layout.size_info { 1109c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => { 1110c3739801SMiguel Ojeda assert_eq!(offset, 0); 1111c3739801SMiguel Ojeda assert_eq!(elem_size, 4); 1112c3739801SMiguel Ojeda } 1113c3739801SMiguel Ojeda _ => panic!("Expected SliceDst"), 1114c3739801SMiguel Ojeda } 1115c3739801SMiguel Ojeda assert_eq!(layout.align.get(), 4); 1116c3739801SMiguel Ojeda } 1117c3739801SMiguel Ojeda 1118c3739801SMiguel Ojeda /// Tests of when a sized `DstLayout` is extended with a sized field. 1119c3739801SMiguel Ojeda #[allow(clippy::decimal_literal_representation)] 1120c3739801SMiguel Ojeda #[test] 1121c3739801SMiguel Ojeda fn test_dst_layout_extend_sized_with_sized() { 1122c3739801SMiguel Ojeda // This macro constructs a layout corresponding to a `u8` and extends it 1123c3739801SMiguel Ojeda // with a zero-sized trailing field of given alignment `n`. The macro 1124c3739801SMiguel Ojeda // tests that the resulting layout has both size and alignment `min(n, 1125c3739801SMiguel Ojeda // P)` for all valid values of `repr(packed(P))`. 1126c3739801SMiguel Ojeda macro_rules! test_align_is_size { 1127c3739801SMiguel Ojeda ($n:expr) => { 1128c3739801SMiguel Ojeda let base = DstLayout::for_type::<u8>(); 1129c3739801SMiguel Ojeda let trailing_field = DstLayout::for_type::<elain::Align<$n>>(); 1130c3739801SMiguel Ojeda 1131c3739801SMiguel Ojeda let packs = 1132c3739801SMiguel Ojeda core::iter::once(None).chain((0..29).map(|p| NonZeroUsize::new(2usize.pow(p)))); 1133c3739801SMiguel Ojeda 1134c3739801SMiguel Ojeda for pack in packs { 1135c3739801SMiguel Ojeda let composite = base.extend(trailing_field, pack); 1136c3739801SMiguel Ojeda let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN); 1137c3739801SMiguel Ojeda let align = $n.min(max_align.get()); 1138c3739801SMiguel Ojeda assert_eq!( 1139c3739801SMiguel Ojeda composite, 1140c3739801SMiguel Ojeda DstLayout { 1141c3739801SMiguel Ojeda align: NonZeroUsize::new(align).unwrap(), 1142c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: align }, 1143c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1144c3739801SMiguel Ojeda } 1145c3739801SMiguel Ojeda ) 1146c3739801SMiguel Ojeda } 1147c3739801SMiguel Ojeda }; 1148c3739801SMiguel Ojeda } 1149c3739801SMiguel Ojeda 1150c3739801SMiguel Ojeda test_align_is_size!(1); 1151c3739801SMiguel Ojeda test_align_is_size!(2); 1152c3739801SMiguel Ojeda test_align_is_size!(4); 1153c3739801SMiguel Ojeda test_align_is_size!(8); 1154c3739801SMiguel Ojeda test_align_is_size!(16); 1155c3739801SMiguel Ojeda test_align_is_size!(32); 1156c3739801SMiguel Ojeda test_align_is_size!(64); 1157c3739801SMiguel Ojeda test_align_is_size!(128); 1158c3739801SMiguel Ojeda test_align_is_size!(256); 1159c3739801SMiguel Ojeda test_align_is_size!(512); 1160c3739801SMiguel Ojeda test_align_is_size!(1024); 1161c3739801SMiguel Ojeda test_align_is_size!(2048); 1162c3739801SMiguel Ojeda test_align_is_size!(4096); 1163c3739801SMiguel Ojeda test_align_is_size!(8192); 1164c3739801SMiguel Ojeda test_align_is_size!(16384); 1165c3739801SMiguel Ojeda test_align_is_size!(32768); 1166c3739801SMiguel Ojeda test_align_is_size!(65536); 1167c3739801SMiguel Ojeda test_align_is_size!(131072); 1168c3739801SMiguel Ojeda test_align_is_size!(262144); 1169c3739801SMiguel Ojeda test_align_is_size!(524288); 1170c3739801SMiguel Ojeda test_align_is_size!(1048576); 1171c3739801SMiguel Ojeda test_align_is_size!(2097152); 1172c3739801SMiguel Ojeda test_align_is_size!(4194304); 1173c3739801SMiguel Ojeda test_align_is_size!(8388608); 1174c3739801SMiguel Ojeda test_align_is_size!(16777216); 1175c3739801SMiguel Ojeda test_align_is_size!(33554432); 1176c3739801SMiguel Ojeda test_align_is_size!(67108864); 1177c3739801SMiguel Ojeda test_align_is_size!(33554432); 1178c3739801SMiguel Ojeda test_align_is_size!(134217728); 1179c3739801SMiguel Ojeda test_align_is_size!(268435456); 1180c3739801SMiguel Ojeda } 1181c3739801SMiguel Ojeda 1182c3739801SMiguel Ojeda /// Tests of when a sized `DstLayout` is extended with a DST field. 1183c3739801SMiguel Ojeda #[test] 1184c3739801SMiguel Ojeda fn test_dst_layout_extend_sized_with_dst() { 1185c3739801SMiguel Ojeda // Test that for all combinations of real-world alignments and 1186c3739801SMiguel Ojeda // `repr_packed` values, that the extension of a sized `DstLayout`` with 1187c3739801SMiguel Ojeda // a DST field correctly computes the trailing offset in the composite 1188c3739801SMiguel Ojeda // layout. 1189c3739801SMiguel Ojeda 1190c3739801SMiguel Ojeda let aligns = (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()); 1191c3739801SMiguel Ojeda let packs = core::iter::once(None).chain(aligns.clone().map(Some)); 1192c3739801SMiguel Ojeda 1193c3739801SMiguel Ojeda for align in aligns { 1194c3739801SMiguel Ojeda for pack in packs.clone() { 1195c3739801SMiguel Ojeda let base = DstLayout::for_type::<u8>(); 1196c3739801SMiguel Ojeda let elem_size = 42; 1197c3739801SMiguel Ojeda let trailing_field_offset = 11; 1198c3739801SMiguel Ojeda 1199c3739801SMiguel Ojeda let trailing_field = DstLayout { 1200c3739801SMiguel Ojeda align, 1201c3739801SMiguel Ojeda size_info: SizeInfo::SliceDst(TrailingSliceLayout { elem_size, offset: 11 }), 1202c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1203c3739801SMiguel Ojeda }; 1204c3739801SMiguel Ojeda 1205c3739801SMiguel Ojeda let composite = base.extend(trailing_field, pack); 1206c3739801SMiguel Ojeda 1207c3739801SMiguel Ojeda let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN).get(); 1208c3739801SMiguel Ojeda 1209c3739801SMiguel Ojeda let align = align.get().min(max_align); 1210c3739801SMiguel Ojeda 1211c3739801SMiguel Ojeda assert_eq!( 1212c3739801SMiguel Ojeda composite, 1213c3739801SMiguel Ojeda DstLayout { 1214c3739801SMiguel Ojeda align: NonZeroUsize::new(align).unwrap(), 1215c3739801SMiguel Ojeda size_info: SizeInfo::SliceDst(TrailingSliceLayout { 1216c3739801SMiguel Ojeda elem_size, 1217c3739801SMiguel Ojeda offset: align + trailing_field_offset, 1218c3739801SMiguel Ojeda }), 1219c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1220c3739801SMiguel Ojeda } 1221c3739801SMiguel Ojeda ) 1222c3739801SMiguel Ojeda } 1223c3739801SMiguel Ojeda } 1224c3739801SMiguel Ojeda } 1225c3739801SMiguel Ojeda 1226c3739801SMiguel Ojeda /// Tests that calling `pad_to_align` on a sized `DstLayout` adds the 1227c3739801SMiguel Ojeda /// expected amount of trailing padding. 1228c3739801SMiguel Ojeda #[test] 1229c3739801SMiguel Ojeda fn test_dst_layout_pad_to_align_with_sized() { 1230c3739801SMiguel Ojeda // For all valid alignments `align`, construct a one-byte layout aligned 1231c3739801SMiguel Ojeda // to `align`, call `pad_to_align`, and assert that the size of the 1232c3739801SMiguel Ojeda // resulting layout is equal to `align`. 1233c3739801SMiguel Ojeda for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) { 1234c3739801SMiguel Ojeda let layout = DstLayout { 1235c3739801SMiguel Ojeda align, 1236c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: 1 }, 1237c3739801SMiguel Ojeda statically_shallow_unpadded: true, 1238c3739801SMiguel Ojeda }; 1239c3739801SMiguel Ojeda 1240c3739801SMiguel Ojeda assert_eq!( 1241c3739801SMiguel Ojeda layout.pad_to_align(), 1242c3739801SMiguel Ojeda DstLayout { 1243c3739801SMiguel Ojeda align, 1244c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: align.get() }, 1245c3739801SMiguel Ojeda statically_shallow_unpadded: align.get() == 1 1246c3739801SMiguel Ojeda } 1247c3739801SMiguel Ojeda ); 1248c3739801SMiguel Ojeda } 1249c3739801SMiguel Ojeda 1250c3739801SMiguel Ojeda // Test explicitly-provided combinations of unpadded and padded 1251c3739801SMiguel Ojeda // counterparts. 1252c3739801SMiguel Ojeda 1253c3739801SMiguel Ojeda macro_rules! test { 1254c3739801SMiguel Ojeda (unpadded { size: $unpadded_size:expr, align: $unpadded_align:expr } 1255c3739801SMiguel Ojeda => padded { size: $padded_size:expr, align: $padded_align:expr }) => { 1256c3739801SMiguel Ojeda let unpadded = DstLayout { 1257c3739801SMiguel Ojeda align: NonZeroUsize::new($unpadded_align).unwrap(), 1258c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: $unpadded_size }, 1259c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1260c3739801SMiguel Ojeda }; 1261c3739801SMiguel Ojeda let padded = unpadded.pad_to_align(); 1262c3739801SMiguel Ojeda 1263c3739801SMiguel Ojeda assert_eq!( 1264c3739801SMiguel Ojeda padded, 1265c3739801SMiguel Ojeda DstLayout { 1266c3739801SMiguel Ojeda align: NonZeroUsize::new($padded_align).unwrap(), 1267c3739801SMiguel Ojeda size_info: SizeInfo::Sized { size: $padded_size }, 1268c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1269c3739801SMiguel Ojeda } 1270c3739801SMiguel Ojeda ); 1271c3739801SMiguel Ojeda }; 1272c3739801SMiguel Ojeda } 1273c3739801SMiguel Ojeda 1274c3739801SMiguel Ojeda test!(unpadded { size: 0, align: 4 } => padded { size: 0, align: 4 }); 1275c3739801SMiguel Ojeda test!(unpadded { size: 1, align: 4 } => padded { size: 4, align: 4 }); 1276c3739801SMiguel Ojeda test!(unpadded { size: 2, align: 4 } => padded { size: 4, align: 4 }); 1277c3739801SMiguel Ojeda test!(unpadded { size: 3, align: 4 } => padded { size: 4, align: 4 }); 1278c3739801SMiguel Ojeda test!(unpadded { size: 4, align: 4 } => padded { size: 4, align: 4 }); 1279c3739801SMiguel Ojeda test!(unpadded { size: 5, align: 4 } => padded { size: 8, align: 4 }); 1280c3739801SMiguel Ojeda test!(unpadded { size: 6, align: 4 } => padded { size: 8, align: 4 }); 1281c3739801SMiguel Ojeda test!(unpadded { size: 7, align: 4 } => padded { size: 8, align: 4 }); 1282c3739801SMiguel Ojeda test!(unpadded { size: 8, align: 4 } => padded { size: 8, align: 4 }); 1283c3739801SMiguel Ojeda 1284c3739801SMiguel Ojeda let current_max_align = DstLayout::CURRENT_MAX_ALIGN.get(); 1285c3739801SMiguel Ojeda 1286c3739801SMiguel Ojeda test!(unpadded { size: 1, align: current_max_align } 1287c3739801SMiguel Ojeda => padded { size: current_max_align, align: current_max_align }); 1288c3739801SMiguel Ojeda 1289c3739801SMiguel Ojeda test!(unpadded { size: current_max_align + 1, align: current_max_align } 1290c3739801SMiguel Ojeda => padded { size: current_max_align * 2, align: current_max_align }); 1291c3739801SMiguel Ojeda } 1292c3739801SMiguel Ojeda 1293c3739801SMiguel Ojeda /// Tests that calling `pad_to_align` on a DST `DstLayout` is a no-op. 1294c3739801SMiguel Ojeda #[test] 1295c3739801SMiguel Ojeda fn test_dst_layout_pad_to_align_with_dst() { 1296c3739801SMiguel Ojeda for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) { 1297c3739801SMiguel Ojeda for offset in 0..10 { 1298c3739801SMiguel Ojeda for elem_size in 0..10 { 1299c3739801SMiguel Ojeda let layout = DstLayout { 1300c3739801SMiguel Ojeda align, 1301c3739801SMiguel Ojeda size_info: SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }), 1302c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1303c3739801SMiguel Ojeda }; 1304c3739801SMiguel Ojeda assert_eq!(layout.pad_to_align(), layout); 1305c3739801SMiguel Ojeda } 1306c3739801SMiguel Ojeda } 1307c3739801SMiguel Ojeda } 1308c3739801SMiguel Ojeda } 1309c3739801SMiguel Ojeda 1310c3739801SMiguel Ojeda // This test takes a long time when running under Miri, so we skip it in 1311c3739801SMiguel Ojeda // that case. This is acceptable because this is a logic test that doesn't 1312c3739801SMiguel Ojeda // attempt to expose UB. 1313c3739801SMiguel Ojeda #[test] 1314c3739801SMiguel Ojeda #[cfg_attr(miri, ignore)] 1315c3739801SMiguel Ojeda fn test_validate_cast_and_convert_metadata() { 1316c3739801SMiguel Ojeda #[allow(non_local_definitions)] 1317c3739801SMiguel Ojeda impl From<usize> for SizeInfo { 1318c3739801SMiguel Ojeda fn from(size: usize) -> SizeInfo { 1319c3739801SMiguel Ojeda SizeInfo::Sized { size } 1320c3739801SMiguel Ojeda } 1321c3739801SMiguel Ojeda } 1322c3739801SMiguel Ojeda 1323c3739801SMiguel Ojeda #[allow(non_local_definitions)] 1324c3739801SMiguel Ojeda impl From<(usize, usize)> for SizeInfo { 1325c3739801SMiguel Ojeda fn from((offset, elem_size): (usize, usize)) -> SizeInfo { 1326c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) 1327c3739801SMiguel Ojeda } 1328c3739801SMiguel Ojeda } 1329c3739801SMiguel Ojeda 1330c3739801SMiguel Ojeda fn layout<S: Into<SizeInfo>>(s: S, align: usize) -> DstLayout { 1331c3739801SMiguel Ojeda DstLayout { 1332c3739801SMiguel Ojeda size_info: s.into(), 1333c3739801SMiguel Ojeda align: NonZeroUsize::new(align).unwrap(), 1334c3739801SMiguel Ojeda statically_shallow_unpadded: false, 1335c3739801SMiguel Ojeda } 1336c3739801SMiguel Ojeda } 1337c3739801SMiguel Ojeda 1338c3739801SMiguel Ojeda /// This macro accepts arguments in the form of: 1339c3739801SMiguel Ojeda /// 1340c3739801SMiguel Ojeda /// layout(_, _).validate(_, _, _), Ok(Some((_, _))) 1341c3739801SMiguel Ojeda /// | | | | | | | 1342c3739801SMiguel Ojeda /// size ---------+ | | | | | | 1343c3739801SMiguel Ojeda /// align -----------+ | | | | | 1344c3739801SMiguel Ojeda /// addr ------------------------+ | | | | 1345c3739801SMiguel Ojeda /// bytes_len ----------------------+ | | | 1346c3739801SMiguel Ojeda /// cast_type -------------------------+ | | 1347c3739801SMiguel Ojeda /// elems ------------------------------------------+ | 1348c3739801SMiguel Ojeda /// split_at ------------------------------------------+ 1349c3739801SMiguel Ojeda /// 1350c3739801SMiguel Ojeda /// `.validate` is shorthand for `.validate_cast_and_convert_metadata` 1351c3739801SMiguel Ojeda /// for brevity. 1352c3739801SMiguel Ojeda /// 1353c3739801SMiguel Ojeda /// Each argument can either be an iterator or a wildcard. Each 1354c3739801SMiguel Ojeda /// wildcarded variable is implicitly replaced by an iterator over a 1355c3739801SMiguel Ojeda /// representative sample of values for that variable. Each `test!` 1356c3739801SMiguel Ojeda /// invocation iterates over every combination of values provided by 1357c3739801SMiguel Ojeda /// each variable's iterator (ie, the cartesian product) and validates 1358c3739801SMiguel Ojeda /// that the results are expected. 1359c3739801SMiguel Ojeda /// 1360c3739801SMiguel Ojeda /// The final argument uses the same syntax, but it has a different 1361c3739801SMiguel Ojeda /// meaning: 1362c3739801SMiguel Ojeda /// - If it is `Ok(pat)`, then the pattern `pat` is supplied to 1363c3739801SMiguel Ojeda /// a matching assert to validate the computed result for each 1364c3739801SMiguel Ojeda /// combination of input values. 1365c3739801SMiguel Ojeda /// - If it is `Err(Some(msg) | None)`, then `test!` validates that the 1366c3739801SMiguel Ojeda /// call to `validate_cast_and_convert_metadata` panics with the given 1367c3739801SMiguel Ojeda /// panic message or, if the current Rust toolchain version is too 1368c3739801SMiguel Ojeda /// early to support panicking in `const fn`s, panics with *some* 1369c3739801SMiguel Ojeda /// message. In the latter case, the `const_panic!` macro is used, 1370c3739801SMiguel Ojeda /// which emits code which causes a non-panicking error at const eval 1371c3739801SMiguel Ojeda /// time, but which does panic when invoked at runtime. Thus, it is 1372c3739801SMiguel Ojeda /// merely difficult to predict the *value* of this panic. We deem 1373c3739801SMiguel Ojeda /// that testing against the real panic strings on stable and nightly 1374c3739801SMiguel Ojeda /// toolchains is enough to ensure correctness. 1375c3739801SMiguel Ojeda /// 1376c3739801SMiguel Ojeda /// Note that the meta-variables that match these variables have the 1377c3739801SMiguel Ojeda /// `tt` type, and some valid expressions are not valid `tt`s (such as 1378c3739801SMiguel Ojeda /// `a..b`). In this case, wrap the expression in parentheses, and it 1379c3739801SMiguel Ojeda /// will become valid `tt`. 1380c3739801SMiguel Ojeda macro_rules! test { 1381c3739801SMiguel Ojeda ( 1382c3739801SMiguel Ojeda layout($size:tt, $align:tt) 1383c3739801SMiguel Ojeda .validate($addr:tt, $bytes_len:tt, $cast_type:tt), $expect:pat $(,)? 1384c3739801SMiguel Ojeda ) => { 1385c3739801SMiguel Ojeda itertools::iproduct!( 1386c3739801SMiguel Ojeda test!(@generate_size $size), 1387c3739801SMiguel Ojeda test!(@generate_align $align), 1388c3739801SMiguel Ojeda test!(@generate_usize $addr), 1389c3739801SMiguel Ojeda test!(@generate_usize $bytes_len), 1390c3739801SMiguel Ojeda test!(@generate_cast_type $cast_type) 1391c3739801SMiguel Ojeda ).for_each(|(size_info, align, addr, bytes_len, cast_type)| { 1392c3739801SMiguel Ojeda // Temporarily disable the panic hook installed by the test 1393c3739801SMiguel Ojeda // harness. If we don't do this, all panic messages will be 1394c3739801SMiguel Ojeda // kept in an internal log. On its own, this isn't a 1395c3739801SMiguel Ojeda // problem, but if a non-caught panic ever happens (ie, in 1396c3739801SMiguel Ojeda // code later in this test not in this macro), all of the 1397c3739801SMiguel Ojeda // previously-buffered messages will be dumped, hiding the 1398c3739801SMiguel Ojeda // real culprit. 1399c3739801SMiguel Ojeda let previous_hook = std::panic::take_hook(); 1400c3739801SMiguel Ojeda // I don't understand why, but this seems to be required in 1401c3739801SMiguel Ojeda // addition to the previous line. 1402c3739801SMiguel Ojeda std::panic::set_hook(Box::new(|_| {})); 1403c3739801SMiguel Ojeda let actual = std::panic::catch_unwind(|| { 1404c3739801SMiguel Ojeda layout(size_info, align).validate_cast_and_convert_metadata(addr, bytes_len, cast_type) 1405c3739801SMiguel Ojeda }).map_err(|d| { 1406c3739801SMiguel Ojeda let msg = d.downcast::<&'static str>().ok().map(|s| *s.as_ref()); 1407c3739801SMiguel Ojeda assert!(msg.is_some() || cfg!(no_zerocopy_panic_in_const_and_vec_try_reserve_1_57_0), "non-string panic messages are not permitted when usage of panic in const fn is enabled"); 1408c3739801SMiguel Ojeda msg 1409c3739801SMiguel Ojeda }); 1410c3739801SMiguel Ojeda std::panic::set_hook(previous_hook); 1411c3739801SMiguel Ojeda 1412c3739801SMiguel Ojeda assert!( 1413c3739801SMiguel Ojeda matches!(actual, $expect), 1414c3739801SMiguel Ojeda "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?})" ,size_info, align, addr, bytes_len, cast_type 1415c3739801SMiguel Ojeda ); 1416c3739801SMiguel Ojeda }); 1417c3739801SMiguel Ojeda }; 1418c3739801SMiguel Ojeda (@generate_usize _) => { 0..8 }; 1419c3739801SMiguel Ojeda // Generate sizes for both Sized and !Sized types. 1420c3739801SMiguel Ojeda (@generate_size _) => { 1421c3739801SMiguel Ojeda test!(@generate_size (_)).chain(test!(@generate_size (_, _))) 1422c3739801SMiguel Ojeda }; 1423c3739801SMiguel Ojeda // Generate sizes for both Sized and !Sized types by chaining 1424c3739801SMiguel Ojeda // specified iterators for each. 1425c3739801SMiguel Ojeda (@generate_size ($sized_sizes:tt | $unsized_sizes:tt)) => { 1426c3739801SMiguel Ojeda test!(@generate_size ($sized_sizes)).chain(test!(@generate_size $unsized_sizes)) 1427c3739801SMiguel Ojeda }; 1428c3739801SMiguel Ojeda // Generate sizes for Sized types. 1429c3739801SMiguel Ojeda (@generate_size (_)) => { test!(@generate_size (0..8)) }; 1430c3739801SMiguel Ojeda (@generate_size ($sizes:expr)) => { $sizes.into_iter().map(Into::<SizeInfo>::into) }; 1431c3739801SMiguel Ojeda // Generate sizes for !Sized types. 1432c3739801SMiguel Ojeda (@generate_size ($min_sizes:tt, $elem_sizes:tt)) => { 1433c3739801SMiguel Ojeda itertools::iproduct!( 1434c3739801SMiguel Ojeda test!(@generate_min_size $min_sizes), 1435c3739801SMiguel Ojeda test!(@generate_elem_size $elem_sizes) 1436c3739801SMiguel Ojeda ).map(Into::<SizeInfo>::into) 1437c3739801SMiguel Ojeda }; 1438c3739801SMiguel Ojeda (@generate_fixed_size _) => { (0..8).into_iter().map(Into::<SizeInfo>::into) }; 1439c3739801SMiguel Ojeda (@generate_min_size _) => { 0..8 }; 1440c3739801SMiguel Ojeda (@generate_elem_size _) => { 1..8 }; 1441c3739801SMiguel Ojeda (@generate_align _) => { [1, 2, 4, 8, 16] }; 1442c3739801SMiguel Ojeda (@generate_opt_usize _) => { [None].into_iter().chain((0..8).map(Some).into_iter()) }; 1443c3739801SMiguel Ojeda (@generate_cast_type _) => { [CastType::Prefix, CastType::Suffix] }; 1444c3739801SMiguel Ojeda (@generate_cast_type $variant:ident) => { [CastType::$variant] }; 1445c3739801SMiguel Ojeda // Some expressions need to be wrapped in parentheses in order to be 1446c3739801SMiguel Ojeda // valid `tt`s (required by the top match pattern). See the comment 1447c3739801SMiguel Ojeda // below for more details. This arm removes these parentheses to 1448c3739801SMiguel Ojeda // avoid generating an `unused_parens` warning. 1449c3739801SMiguel Ojeda (@$_:ident ($vals:expr)) => { $vals }; 1450c3739801SMiguel Ojeda (@$_:ident $vals:expr) => { $vals }; 1451c3739801SMiguel Ojeda } 1452c3739801SMiguel Ojeda 1453c3739801SMiguel Ojeda const EVENS: [usize; 8] = [0, 2, 4, 6, 8, 10, 12, 14]; 1454c3739801SMiguel Ojeda const ODDS: [usize; 8] = [1, 3, 5, 7, 9, 11, 13, 15]; 1455c3739801SMiguel Ojeda 1456c3739801SMiguel Ojeda // base_size is too big for the memory region. 1457c3739801SMiguel Ojeda test!( 1458c3739801SMiguel Ojeda layout(((1..8) | ((1..8), (1..8))), _).validate([0], [0], _), 1459c3739801SMiguel Ojeda Ok(Err(MetadataCastError::Size)) 1460c3739801SMiguel Ojeda ); 1461c3739801SMiguel Ojeda test!( 1462c3739801SMiguel Ojeda layout(((2..8) | ((2..8), (2..8))), _).validate([0], [1], Prefix), 1463c3739801SMiguel Ojeda Ok(Err(MetadataCastError::Size)) 1464c3739801SMiguel Ojeda ); 1465c3739801SMiguel Ojeda test!( 1466c3739801SMiguel Ojeda layout(((2..8) | ((2..8), (2..8))), _).validate([0x1000_0000 - 1], [1], Suffix), 1467c3739801SMiguel Ojeda Ok(Err(MetadataCastError::Size)) 1468c3739801SMiguel Ojeda ); 1469c3739801SMiguel Ojeda 1470c3739801SMiguel Ojeda // addr is unaligned for prefix cast 1471c3739801SMiguel Ojeda test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment))); 1472c3739801SMiguel Ojeda test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment))); 1473c3739801SMiguel Ojeda 1474c3739801SMiguel Ojeda // addr is aligned, but end of buffer is unaligned for suffix cast 1475c3739801SMiguel Ojeda test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment))); 1476c3739801SMiguel Ojeda test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment))); 1477c3739801SMiguel Ojeda 1478c3739801SMiguel Ojeda // Unfortunately, these constants cannot easily be used in the 1479c3739801SMiguel Ojeda // implementation of `validate_cast_and_convert_metadata`, since 1480c3739801SMiguel Ojeda // `panic!` consumes a string literal, not an expression. 1481c3739801SMiguel Ojeda // 1482c3739801SMiguel Ojeda // It's important that these messages be in a separate module. If they 1483c3739801SMiguel Ojeda // were at the function's top level, we'd pass them to `test!` as, e.g., 1484c3739801SMiguel Ojeda // `Err(TRAILING)`, which would run into a subtle Rust footgun - the 1485c3739801SMiguel Ojeda // `TRAILING` identifier would be treated as a pattern to match rather 1486c3739801SMiguel Ojeda // than a value to check for equality. 1487c3739801SMiguel Ojeda mod msgs { 1488c3739801SMiguel Ojeda pub(super) const TRAILING: &str = 1489c3739801SMiguel Ojeda "attempted to cast to slice type with zero-sized element"; 1490c3739801SMiguel Ojeda pub(super) const OVERFLOW: &str = "`addr` + `bytes_len` > usize::MAX"; 1491c3739801SMiguel Ojeda } 1492c3739801SMiguel Ojeda 1493c3739801SMiguel Ojeda // casts with ZST trailing element types are unsupported 1494c3739801SMiguel Ojeda test!(layout((_, [0]), _).validate(_, _, _), Err(Some(msgs::TRAILING) | None),); 1495c3739801SMiguel Ojeda 1496c3739801SMiguel Ojeda // addr + bytes_len must not overflow usize 1497c3739801SMiguel Ojeda test!(layout(_, _).validate([usize::MAX], (1..100), _), Err(Some(msgs::OVERFLOW) | None)); 1498c3739801SMiguel Ojeda test!(layout(_, _).validate((1..100), [usize::MAX], _), Err(Some(msgs::OVERFLOW) | None)); 1499c3739801SMiguel Ojeda test!( 1500c3739801SMiguel Ojeda layout(_, _).validate( 1501c3739801SMiguel Ojeda [usize::MAX / 2 + 1, usize::MAX], 1502c3739801SMiguel Ojeda [usize::MAX / 2 + 1, usize::MAX], 1503c3739801SMiguel Ojeda _ 1504c3739801SMiguel Ojeda ), 1505c3739801SMiguel Ojeda Err(Some(msgs::OVERFLOW) | None) 1506c3739801SMiguel Ojeda ); 1507c3739801SMiguel Ojeda 1508c3739801SMiguel Ojeda // Validates that `validate_cast_and_convert_metadata` satisfies its own 1509c3739801SMiguel Ojeda // documented safety postconditions, and also a few other properties 1510c3739801SMiguel Ojeda // that aren't documented but we want to guarantee anyway. 1511c3739801SMiguel Ojeda fn validate_behavior( 1512c3739801SMiguel Ojeda (layout, addr, bytes_len, cast_type): (DstLayout, usize, usize, CastType), 1513c3739801SMiguel Ojeda ) { 1514c3739801SMiguel Ojeda if let Ok((elems, split_at)) = 1515c3739801SMiguel Ojeda layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type) 1516c3739801SMiguel Ojeda { 1517c3739801SMiguel Ojeda let (size_info, align) = (layout.size_info, layout.align); 1518c3739801SMiguel Ojeda let debug_str = format!( 1519c3739801SMiguel Ojeda "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?}) => ({}, {})", 1520c3739801SMiguel Ojeda size_info, align, addr, bytes_len, cast_type, elems, split_at 1521c3739801SMiguel Ojeda ); 1522c3739801SMiguel Ojeda 1523c3739801SMiguel Ojeda // If this is a sized type (no trailing slice), then `elems` is 1524c3739801SMiguel Ojeda // meaningless, but in practice we set it to 0. Callers are not 1525c3739801SMiguel Ojeda // allowed to rely on this, but a lot of math is nicer if 1526c3739801SMiguel Ojeda // they're able to, and some callers might accidentally do that. 1527c3739801SMiguel Ojeda let sized = matches!(layout.size_info, SizeInfo::Sized { .. }); 1528c3739801SMiguel Ojeda assert!(!(sized && elems != 0), "{}", debug_str); 1529c3739801SMiguel Ojeda 1530c3739801SMiguel Ojeda let resulting_size = match layout.size_info { 1531c3739801SMiguel Ojeda SizeInfo::Sized { size } => size, 1532c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => { 1533c3739801SMiguel Ojeda let padded_size = |elems| { 1534c3739801SMiguel Ojeda let without_padding = offset + elems * elem_size; 1535c3739801SMiguel Ojeda without_padding + util::padding_needed_for(without_padding, align) 1536c3739801SMiguel Ojeda }; 1537c3739801SMiguel Ojeda 1538c3739801SMiguel Ojeda let resulting_size = padded_size(elems); 1539c3739801SMiguel Ojeda // Test that `validate_cast_and_convert_metadata` 1540c3739801SMiguel Ojeda // computed the largest possible value that fits in the 1541c3739801SMiguel Ojeda // given range. 1542c3739801SMiguel Ojeda assert!(padded_size(elems + 1) > bytes_len, "{}", debug_str); 1543c3739801SMiguel Ojeda resulting_size 1544c3739801SMiguel Ojeda } 1545c3739801SMiguel Ojeda }; 1546c3739801SMiguel Ojeda 1547c3739801SMiguel Ojeda // Test safety postconditions guaranteed by 1548c3739801SMiguel Ojeda // `validate_cast_and_convert_metadata`. 1549c3739801SMiguel Ojeda assert!(resulting_size <= bytes_len, "{}", debug_str); 1550c3739801SMiguel Ojeda match cast_type { 1551c3739801SMiguel Ojeda CastType::Prefix => { 1552c3739801SMiguel Ojeda assert_eq!(addr % align, 0, "{}", debug_str); 1553c3739801SMiguel Ojeda assert_eq!(resulting_size, split_at, "{}", debug_str); 1554c3739801SMiguel Ojeda } 1555c3739801SMiguel Ojeda CastType::Suffix => { 1556c3739801SMiguel Ojeda assert_eq!(split_at, bytes_len - resulting_size, "{}", debug_str); 1557c3739801SMiguel Ojeda assert_eq!((addr + split_at) % align, 0, "{}", debug_str); 1558c3739801SMiguel Ojeda } 1559c3739801SMiguel Ojeda } 1560c3739801SMiguel Ojeda } else { 1561c3739801SMiguel Ojeda let min_size = match layout.size_info { 1562c3739801SMiguel Ojeda SizeInfo::Sized { size } => size, 1563c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, .. }) => { 1564c3739801SMiguel Ojeda offset + util::padding_needed_for(offset, layout.align) 1565c3739801SMiguel Ojeda } 1566c3739801SMiguel Ojeda }; 1567c3739801SMiguel Ojeda 1568c3739801SMiguel Ojeda // If a cast is invalid, it is either because... 1569c3739801SMiguel Ojeda // 1. there are insufficient bytes at the given region for type: 1570c3739801SMiguel Ojeda let insufficient_bytes = bytes_len < min_size; 1571c3739801SMiguel Ojeda // 2. performing the cast would misalign type: 1572c3739801SMiguel Ojeda let base = match cast_type { 1573c3739801SMiguel Ojeda CastType::Prefix => 0, 1574c3739801SMiguel Ojeda CastType::Suffix => bytes_len, 1575c3739801SMiguel Ojeda }; 1576c3739801SMiguel Ojeda let misaligned = (base + addr) % layout.align != 0; 1577c3739801SMiguel Ojeda 1578c3739801SMiguel Ojeda assert!(insufficient_bytes || misaligned); 1579c3739801SMiguel Ojeda } 1580c3739801SMiguel Ojeda } 1581c3739801SMiguel Ojeda 1582c3739801SMiguel Ojeda let sizes = 0..8; 1583c3739801SMiguel Ojeda let elem_sizes = 1..8; 1584c3739801SMiguel Ojeda let size_infos = sizes 1585c3739801SMiguel Ojeda .clone() 1586c3739801SMiguel Ojeda .map(Into::<SizeInfo>::into) 1587c3739801SMiguel Ojeda .chain(itertools::iproduct!(sizes, elem_sizes).map(Into::<SizeInfo>::into)); 1588c3739801SMiguel Ojeda let layouts = itertools::iproduct!(size_infos, [1, 2, 4, 8, 16, 32]) 1589c3739801SMiguel Ojeda .filter(|(size_info, align)| !matches!(size_info, SizeInfo::Sized { size } if size % align != 0)) 1590c3739801SMiguel Ojeda .map(|(size_info, align)| layout(size_info, align)); 1591c3739801SMiguel Ojeda itertools::iproduct!(layouts, 0..8, 0..8, [CastType::Prefix, CastType::Suffix]) 1592c3739801SMiguel Ojeda .for_each(validate_behavior); 1593c3739801SMiguel Ojeda } 1594c3739801SMiguel Ojeda 1595c3739801SMiguel Ojeda #[test] 1596c3739801SMiguel Ojeda #[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)] 1597c3739801SMiguel Ojeda fn test_validate_rust_layout() { 1598c3739801SMiguel Ojeda use core::{ 1599c3739801SMiguel Ojeda convert::TryInto as _, 1600c3739801SMiguel Ojeda ptr::{self, NonNull}, 1601c3739801SMiguel Ojeda }; 1602c3739801SMiguel Ojeda 1603c3739801SMiguel Ojeda use crate::util::testutil::*; 1604c3739801SMiguel Ojeda 1605c3739801SMiguel Ojeda // This test synthesizes pointers with various metadata and uses Rust's 1606c3739801SMiguel Ojeda // built-in APIs to confirm that Rust makes decisions about type layout 1607c3739801SMiguel Ojeda // which are consistent with what we believe is guaranteed by the 1608c3739801SMiguel Ojeda // language. If this test fails, it doesn't just mean our code is wrong 1609c3739801SMiguel Ojeda // - it means we're misunderstanding the language's guarantees. 1610c3739801SMiguel Ojeda 1611c3739801SMiguel Ojeda #[derive(Debug)] 1612c3739801SMiguel Ojeda struct MacroArgs { 1613c3739801SMiguel Ojeda offset: usize, 1614c3739801SMiguel Ojeda align: NonZeroUsize, 1615c3739801SMiguel Ojeda elem_size: Option<usize>, 1616c3739801SMiguel Ojeda } 1617c3739801SMiguel Ojeda 1618c3739801SMiguel Ojeda /// # Safety 1619c3739801SMiguel Ojeda /// 1620c3739801SMiguel Ojeda /// `test` promises to only call `addr_of_slice_field` on a `NonNull<T>` 1621c3739801SMiguel Ojeda /// which points to a valid `T`. 1622c3739801SMiguel Ojeda /// 1623c3739801SMiguel Ojeda /// `with_elems` must produce a pointer which points to a valid `T`. 1624c3739801SMiguel Ojeda fn test<T: ?Sized, W: Fn(usize) -> NonNull<T>>( 1625c3739801SMiguel Ojeda args: MacroArgs, 1626c3739801SMiguel Ojeda with_elems: W, 1627c3739801SMiguel Ojeda addr_of_slice_field: Option<fn(NonNull<T>) -> NonNull<u8>>, 1628c3739801SMiguel Ojeda ) { 1629c3739801SMiguel Ojeda let dst = args.elem_size.is_some(); 1630c3739801SMiguel Ojeda let layout = { 1631c3739801SMiguel Ojeda let size_info = match args.elem_size { 1632c3739801SMiguel Ojeda Some(elem_size) => { 1633c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset: args.offset, elem_size }) 1634c3739801SMiguel Ojeda } 1635c3739801SMiguel Ojeda None => SizeInfo::Sized { 1636c3739801SMiguel Ojeda // Rust only supports types whose sizes are a multiple 1637c3739801SMiguel Ojeda // of their alignment. If the macro created a type like 1638c3739801SMiguel Ojeda // this: 1639c3739801SMiguel Ojeda // 1640c3739801SMiguel Ojeda // #[repr(C, align(2))] 1641c3739801SMiguel Ojeda // struct Foo([u8; 1]); 1642c3739801SMiguel Ojeda // 1643c3739801SMiguel Ojeda // ...then Rust will automatically round the type's size 1644c3739801SMiguel Ojeda // up to 2. 1645c3739801SMiguel Ojeda size: args.offset + util::padding_needed_for(args.offset, args.align), 1646c3739801SMiguel Ojeda }, 1647c3739801SMiguel Ojeda }; 1648c3739801SMiguel Ojeda DstLayout { size_info, align: args.align, statically_shallow_unpadded: false } 1649c3739801SMiguel Ojeda }; 1650c3739801SMiguel Ojeda 1651c3739801SMiguel Ojeda for elems in 0..128 { 1652c3739801SMiguel Ojeda let ptr = with_elems(elems); 1653c3739801SMiguel Ojeda 1654c3739801SMiguel Ojeda if let Some(addr_of_slice_field) = addr_of_slice_field { 1655c3739801SMiguel Ojeda let slc_field_ptr = addr_of_slice_field(ptr).as_ptr(); 1656c3739801SMiguel Ojeda // SAFETY: Both `slc_field_ptr` and `ptr` are pointers to 1657c3739801SMiguel Ojeda // the same valid Rust object. 1658c3739801SMiguel Ojeda // Work around https://github.com/rust-lang/rust-clippy/issues/12280 1659c3739801SMiguel Ojeda let offset: usize = 1660c3739801SMiguel Ojeda unsafe { slc_field_ptr.byte_offset_from(ptr.as_ptr()).try_into().unwrap() }; 1661c3739801SMiguel Ojeda assert_eq!(offset, args.offset); 1662c3739801SMiguel Ojeda } 1663c3739801SMiguel Ojeda 1664c3739801SMiguel Ojeda // SAFETY: `ptr` points to a valid `T`. 1665c3739801SMiguel Ojeda #[allow(clippy::multiple_unsafe_ops_per_block)] 1666c3739801SMiguel Ojeda let (size, align) = unsafe { 1667c3739801SMiguel Ojeda (mem::size_of_val_raw(ptr.as_ptr()), mem::align_of_val_raw(ptr.as_ptr())) 1668c3739801SMiguel Ojeda }; 1669c3739801SMiguel Ojeda 1670c3739801SMiguel Ojeda // Avoid expensive allocation when running under Miri. 1671c3739801SMiguel Ojeda let assert_msg = if !cfg!(miri) { 1672c3739801SMiguel Ojeda format!("\n{:?}\nsize:{}, align:{}", args, size, align) 1673c3739801SMiguel Ojeda } else { 1674c3739801SMiguel Ojeda String::new() 1675c3739801SMiguel Ojeda }; 1676c3739801SMiguel Ojeda 1677c3739801SMiguel Ojeda let without_padding = 1678c3739801SMiguel Ojeda args.offset + args.elem_size.map(|elem_size| elems * elem_size).unwrap_or(0); 1679c3739801SMiguel Ojeda assert!(size >= without_padding, "{}", assert_msg); 1680c3739801SMiguel Ojeda assert_eq!(align, args.align.get(), "{}", assert_msg); 1681c3739801SMiguel Ojeda 1682c3739801SMiguel Ojeda // This encodes the most important part of the test: our 1683c3739801SMiguel Ojeda // understanding of how Rust determines the layout of repr(C) 1684c3739801SMiguel Ojeda // types. Sized repr(C) types are trivial, but DST types have 1685c3739801SMiguel Ojeda // some subtlety. Note that: 1686c3739801SMiguel Ojeda // - For sized types, `without_padding` is just the size of the 1687c3739801SMiguel Ojeda // type that we constructed for `Foo`. Since we may have 1688c3739801SMiguel Ojeda // requested a larger alignment, `Foo` may actually be larger 1689c3739801SMiguel Ojeda // than this, hence `padding_needed_for`. 1690c3739801SMiguel Ojeda // - For unsized types, `without_padding` is dynamically 1691c3739801SMiguel Ojeda // computed from the offset, the element size, and element 1692c3739801SMiguel Ojeda // count. We expect that the size of the object should be 1693c3739801SMiguel Ojeda // `offset + elem_size * elems` rounded up to the next 1694c3739801SMiguel Ojeda // alignment. 1695c3739801SMiguel Ojeda let expected_size = 1696c3739801SMiguel Ojeda without_padding + util::padding_needed_for(without_padding, args.align); 1697c3739801SMiguel Ojeda assert_eq!(expected_size, size, "{}", assert_msg); 1698c3739801SMiguel Ojeda 1699c3739801SMiguel Ojeda // For zero-sized element types, 1700c3739801SMiguel Ojeda // `validate_cast_and_convert_metadata` just panics, so we skip 1701c3739801SMiguel Ojeda // testing those types. 1702c3739801SMiguel Ojeda if args.elem_size.map(|elem_size| elem_size > 0).unwrap_or(true) { 1703c3739801SMiguel Ojeda let addr = ptr.addr().get(); 1704c3739801SMiguel Ojeda let (got_elems, got_split_at) = layout 1705c3739801SMiguel Ojeda .validate_cast_and_convert_metadata(addr, size, CastType::Prefix) 1706c3739801SMiguel Ojeda .unwrap(); 1707c3739801SMiguel Ojeda // Avoid expensive allocation when running under Miri. 1708c3739801SMiguel Ojeda let assert_msg = if !cfg!(miri) { 1709c3739801SMiguel Ojeda format!( 1710c3739801SMiguel Ojeda "{}\nvalidate_cast_and_convert_metadata({}, {})", 1711c3739801SMiguel Ojeda assert_msg, addr, size, 1712c3739801SMiguel Ojeda ) 1713c3739801SMiguel Ojeda } else { 1714c3739801SMiguel Ojeda String::new() 1715c3739801SMiguel Ojeda }; 1716c3739801SMiguel Ojeda assert_eq!(got_split_at, size, "{}", assert_msg); 1717c3739801SMiguel Ojeda if dst { 1718c3739801SMiguel Ojeda assert!(got_elems >= elems, "{}", assert_msg); 1719c3739801SMiguel Ojeda if got_elems != elems { 1720c3739801SMiguel Ojeda // If `validate_cast_and_convert_metadata` 1721c3739801SMiguel Ojeda // returned more elements than `elems`, that 1722c3739801SMiguel Ojeda // means that `elems` is not the maximum number 1723c3739801SMiguel Ojeda // of elements that can fit in `size` - in other 1724c3739801SMiguel Ojeda // words, there is enough padding at the end of 1725c3739801SMiguel Ojeda // the value to fit at least one more element. 1726c3739801SMiguel Ojeda // If we use this metadata to synthesize a 1727c3739801SMiguel Ojeda // pointer, despite having a different element 1728c3739801SMiguel Ojeda // count, we still expect it to have the same 1729c3739801SMiguel Ojeda // size. 1730c3739801SMiguel Ojeda let got_ptr = with_elems(got_elems); 1731c3739801SMiguel Ojeda // SAFETY: `got_ptr` is a pointer to a valid `T`. 1732c3739801SMiguel Ojeda let size_of_got_ptr = unsafe { mem::size_of_val_raw(got_ptr.as_ptr()) }; 1733c3739801SMiguel Ojeda assert_eq!(size_of_got_ptr, size, "{}", assert_msg); 1734c3739801SMiguel Ojeda } 1735c3739801SMiguel Ojeda } else { 1736c3739801SMiguel Ojeda // For sized casts, the returned element value is 1737c3739801SMiguel Ojeda // technically meaningless, and we don't guarantee any 1738c3739801SMiguel Ojeda // particular value. In practice, it's always zero. 1739c3739801SMiguel Ojeda assert_eq!(got_elems, 0, "{}", assert_msg) 1740c3739801SMiguel Ojeda } 1741c3739801SMiguel Ojeda } 1742c3739801SMiguel Ojeda } 1743c3739801SMiguel Ojeda } 1744c3739801SMiguel Ojeda 1745c3739801SMiguel Ojeda macro_rules! validate_against_rust { 1746c3739801SMiguel Ojeda ($offset:literal, $align:literal $(, $elem_size:literal)?) => {{ 1747c3739801SMiguel Ojeda #[repr(C, align($align))] 1748c3739801SMiguel Ojeda struct Foo([u8; $offset]$(, [[u8; $elem_size]])?); 1749c3739801SMiguel Ojeda 1750c3739801SMiguel Ojeda let args = MacroArgs { 1751c3739801SMiguel Ojeda offset: $offset, 1752c3739801SMiguel Ojeda align: $align.try_into().unwrap(), 1753c3739801SMiguel Ojeda elem_size: { 1754c3739801SMiguel Ojeda #[allow(unused)] 1755c3739801SMiguel Ojeda let ret = None::<usize>; 1756c3739801SMiguel Ojeda $(let ret = Some($elem_size);)? 1757c3739801SMiguel Ojeda ret 1758c3739801SMiguel Ojeda } 1759c3739801SMiguel Ojeda }; 1760c3739801SMiguel Ojeda 1761c3739801SMiguel Ojeda #[repr(C, align($align))] 1762c3739801SMiguel Ojeda struct FooAlign; 1763c3739801SMiguel Ojeda // Create an aligned buffer to use in order to synthesize 1764c3739801SMiguel Ojeda // pointers to `Foo`. We don't ever load values from these 1765c3739801SMiguel Ojeda // pointers - we just do arithmetic on them - so having a "real" 1766c3739801SMiguel Ojeda // block of memory as opposed to a validly-aligned-but-dangling 1767c3739801SMiguel Ojeda // pointer is only necessary to make Miri happy since we run it 1768c3739801SMiguel Ojeda // with "strict provenance" checking enabled. 1769c3739801SMiguel Ojeda let aligned_buf = Align::<_, FooAlign>::new([0u8; 1024]); 1770c3739801SMiguel Ojeda let with_elems = |elems| { 1771c3739801SMiguel Ojeda let slc = NonNull::slice_from_raw_parts(NonNull::from(&aligned_buf.t), elems); 1772c3739801SMiguel Ojeda #[allow(clippy::as_conversions)] 1773c3739801SMiguel Ojeda NonNull::new(slc.as_ptr() as *mut Foo).unwrap() 1774c3739801SMiguel Ojeda }; 1775c3739801SMiguel Ojeda let addr_of_slice_field = { 1776c3739801SMiguel Ojeda #[allow(unused)] 1777c3739801SMiguel Ojeda let f = None::<fn(NonNull<Foo>) -> NonNull<u8>>; 1778c3739801SMiguel Ojeda $( 1779c3739801SMiguel Ojeda // SAFETY: `test` promises to only call `f` with a `ptr` 1780c3739801SMiguel Ojeda // to a valid `Foo`. 1781c3739801SMiguel Ojeda let f: Option<fn(NonNull<Foo>) -> NonNull<u8>> = Some(|ptr: NonNull<Foo>| unsafe { 1782c3739801SMiguel Ojeda NonNull::new(ptr::addr_of_mut!((*ptr.as_ptr()).1)).unwrap().cast::<u8>() 1783c3739801SMiguel Ojeda }); 1784c3739801SMiguel Ojeda let _ = $elem_size; 1785c3739801SMiguel Ojeda )? 1786c3739801SMiguel Ojeda f 1787c3739801SMiguel Ojeda }; 1788c3739801SMiguel Ojeda 1789c3739801SMiguel Ojeda test::<Foo, _>(args, with_elems, addr_of_slice_field); 1790c3739801SMiguel Ojeda }}; 1791c3739801SMiguel Ojeda } 1792c3739801SMiguel Ojeda 1793c3739801SMiguel Ojeda // Every permutation of: 1794c3739801SMiguel Ojeda // - offset in [0, 4] 1795c3739801SMiguel Ojeda // - align in [1, 16] 1796c3739801SMiguel Ojeda // - elem_size in [0, 4] (plus no elem_size) 1797c3739801SMiguel Ojeda validate_against_rust!(0, 1); 1798c3739801SMiguel Ojeda validate_against_rust!(0, 1, 0); 1799c3739801SMiguel Ojeda validate_against_rust!(0, 1, 1); 1800c3739801SMiguel Ojeda validate_against_rust!(0, 1, 2); 1801c3739801SMiguel Ojeda validate_against_rust!(0, 1, 3); 1802c3739801SMiguel Ojeda validate_against_rust!(0, 1, 4); 1803c3739801SMiguel Ojeda validate_against_rust!(0, 2); 1804c3739801SMiguel Ojeda validate_against_rust!(0, 2, 0); 1805c3739801SMiguel Ojeda validate_against_rust!(0, 2, 1); 1806c3739801SMiguel Ojeda validate_against_rust!(0, 2, 2); 1807c3739801SMiguel Ojeda validate_against_rust!(0, 2, 3); 1808c3739801SMiguel Ojeda validate_against_rust!(0, 2, 4); 1809c3739801SMiguel Ojeda validate_against_rust!(0, 4); 1810c3739801SMiguel Ojeda validate_against_rust!(0, 4, 0); 1811c3739801SMiguel Ojeda validate_against_rust!(0, 4, 1); 1812c3739801SMiguel Ojeda validate_against_rust!(0, 4, 2); 1813c3739801SMiguel Ojeda validate_against_rust!(0, 4, 3); 1814c3739801SMiguel Ojeda validate_against_rust!(0, 4, 4); 1815c3739801SMiguel Ojeda validate_against_rust!(0, 8); 1816c3739801SMiguel Ojeda validate_against_rust!(0, 8, 0); 1817c3739801SMiguel Ojeda validate_against_rust!(0, 8, 1); 1818c3739801SMiguel Ojeda validate_against_rust!(0, 8, 2); 1819c3739801SMiguel Ojeda validate_against_rust!(0, 8, 3); 1820c3739801SMiguel Ojeda validate_against_rust!(0, 8, 4); 1821c3739801SMiguel Ojeda validate_against_rust!(0, 16); 1822c3739801SMiguel Ojeda validate_against_rust!(0, 16, 0); 1823c3739801SMiguel Ojeda validate_against_rust!(0, 16, 1); 1824c3739801SMiguel Ojeda validate_against_rust!(0, 16, 2); 1825c3739801SMiguel Ojeda validate_against_rust!(0, 16, 3); 1826c3739801SMiguel Ojeda validate_against_rust!(0, 16, 4); 1827c3739801SMiguel Ojeda validate_against_rust!(1, 1); 1828c3739801SMiguel Ojeda validate_against_rust!(1, 1, 0); 1829c3739801SMiguel Ojeda validate_against_rust!(1, 1, 1); 1830c3739801SMiguel Ojeda validate_against_rust!(1, 1, 2); 1831c3739801SMiguel Ojeda validate_against_rust!(1, 1, 3); 1832c3739801SMiguel Ojeda validate_against_rust!(1, 1, 4); 1833c3739801SMiguel Ojeda validate_against_rust!(1, 2); 1834c3739801SMiguel Ojeda validate_against_rust!(1, 2, 0); 1835c3739801SMiguel Ojeda validate_against_rust!(1, 2, 1); 1836c3739801SMiguel Ojeda validate_against_rust!(1, 2, 2); 1837c3739801SMiguel Ojeda validate_against_rust!(1, 2, 3); 1838c3739801SMiguel Ojeda validate_against_rust!(1, 2, 4); 1839c3739801SMiguel Ojeda validate_against_rust!(1, 4); 1840c3739801SMiguel Ojeda validate_against_rust!(1, 4, 0); 1841c3739801SMiguel Ojeda validate_against_rust!(1, 4, 1); 1842c3739801SMiguel Ojeda validate_against_rust!(1, 4, 2); 1843c3739801SMiguel Ojeda validate_against_rust!(1, 4, 3); 1844c3739801SMiguel Ojeda validate_against_rust!(1, 4, 4); 1845c3739801SMiguel Ojeda validate_against_rust!(1, 8); 1846c3739801SMiguel Ojeda validate_against_rust!(1, 8, 0); 1847c3739801SMiguel Ojeda validate_against_rust!(1, 8, 1); 1848c3739801SMiguel Ojeda validate_against_rust!(1, 8, 2); 1849c3739801SMiguel Ojeda validate_against_rust!(1, 8, 3); 1850c3739801SMiguel Ojeda validate_against_rust!(1, 8, 4); 1851c3739801SMiguel Ojeda validate_against_rust!(1, 16); 1852c3739801SMiguel Ojeda validate_against_rust!(1, 16, 0); 1853c3739801SMiguel Ojeda validate_against_rust!(1, 16, 1); 1854c3739801SMiguel Ojeda validate_against_rust!(1, 16, 2); 1855c3739801SMiguel Ojeda validate_against_rust!(1, 16, 3); 1856c3739801SMiguel Ojeda validate_against_rust!(1, 16, 4); 1857c3739801SMiguel Ojeda validate_against_rust!(2, 1); 1858c3739801SMiguel Ojeda validate_against_rust!(2, 1, 0); 1859c3739801SMiguel Ojeda validate_against_rust!(2, 1, 1); 1860c3739801SMiguel Ojeda validate_against_rust!(2, 1, 2); 1861c3739801SMiguel Ojeda validate_against_rust!(2, 1, 3); 1862c3739801SMiguel Ojeda validate_against_rust!(2, 1, 4); 1863c3739801SMiguel Ojeda validate_against_rust!(2, 2); 1864c3739801SMiguel Ojeda validate_against_rust!(2, 2, 0); 1865c3739801SMiguel Ojeda validate_against_rust!(2, 2, 1); 1866c3739801SMiguel Ojeda validate_against_rust!(2, 2, 2); 1867c3739801SMiguel Ojeda validate_against_rust!(2, 2, 3); 1868c3739801SMiguel Ojeda validate_against_rust!(2, 2, 4); 1869c3739801SMiguel Ojeda validate_against_rust!(2, 4); 1870c3739801SMiguel Ojeda validate_against_rust!(2, 4, 0); 1871c3739801SMiguel Ojeda validate_against_rust!(2, 4, 1); 1872c3739801SMiguel Ojeda validate_against_rust!(2, 4, 2); 1873c3739801SMiguel Ojeda validate_against_rust!(2, 4, 3); 1874c3739801SMiguel Ojeda validate_against_rust!(2, 4, 4); 1875c3739801SMiguel Ojeda validate_against_rust!(2, 8); 1876c3739801SMiguel Ojeda validate_against_rust!(2, 8, 0); 1877c3739801SMiguel Ojeda validate_against_rust!(2, 8, 1); 1878c3739801SMiguel Ojeda validate_against_rust!(2, 8, 2); 1879c3739801SMiguel Ojeda validate_against_rust!(2, 8, 3); 1880c3739801SMiguel Ojeda validate_against_rust!(2, 8, 4); 1881c3739801SMiguel Ojeda validate_against_rust!(2, 16); 1882c3739801SMiguel Ojeda validate_against_rust!(2, 16, 0); 1883c3739801SMiguel Ojeda validate_against_rust!(2, 16, 1); 1884c3739801SMiguel Ojeda validate_against_rust!(2, 16, 2); 1885c3739801SMiguel Ojeda validate_against_rust!(2, 16, 3); 1886c3739801SMiguel Ojeda validate_against_rust!(2, 16, 4); 1887c3739801SMiguel Ojeda validate_against_rust!(3, 1); 1888c3739801SMiguel Ojeda validate_against_rust!(3, 1, 0); 1889c3739801SMiguel Ojeda validate_against_rust!(3, 1, 1); 1890c3739801SMiguel Ojeda validate_against_rust!(3, 1, 2); 1891c3739801SMiguel Ojeda validate_against_rust!(3, 1, 3); 1892c3739801SMiguel Ojeda validate_against_rust!(3, 1, 4); 1893c3739801SMiguel Ojeda validate_against_rust!(3, 2); 1894c3739801SMiguel Ojeda validate_against_rust!(3, 2, 0); 1895c3739801SMiguel Ojeda validate_against_rust!(3, 2, 1); 1896c3739801SMiguel Ojeda validate_against_rust!(3, 2, 2); 1897c3739801SMiguel Ojeda validate_against_rust!(3, 2, 3); 1898c3739801SMiguel Ojeda validate_against_rust!(3, 2, 4); 1899c3739801SMiguel Ojeda validate_against_rust!(3, 4); 1900c3739801SMiguel Ojeda validate_against_rust!(3, 4, 0); 1901c3739801SMiguel Ojeda validate_against_rust!(3, 4, 1); 1902c3739801SMiguel Ojeda validate_against_rust!(3, 4, 2); 1903c3739801SMiguel Ojeda validate_against_rust!(3, 4, 3); 1904c3739801SMiguel Ojeda validate_against_rust!(3, 4, 4); 1905c3739801SMiguel Ojeda validate_against_rust!(3, 8); 1906c3739801SMiguel Ojeda validate_against_rust!(3, 8, 0); 1907c3739801SMiguel Ojeda validate_against_rust!(3, 8, 1); 1908c3739801SMiguel Ojeda validate_against_rust!(3, 8, 2); 1909c3739801SMiguel Ojeda validate_against_rust!(3, 8, 3); 1910c3739801SMiguel Ojeda validate_against_rust!(3, 8, 4); 1911c3739801SMiguel Ojeda validate_against_rust!(3, 16); 1912c3739801SMiguel Ojeda validate_against_rust!(3, 16, 0); 1913c3739801SMiguel Ojeda validate_against_rust!(3, 16, 1); 1914c3739801SMiguel Ojeda validate_against_rust!(3, 16, 2); 1915c3739801SMiguel Ojeda validate_against_rust!(3, 16, 3); 1916c3739801SMiguel Ojeda validate_against_rust!(3, 16, 4); 1917c3739801SMiguel Ojeda validate_against_rust!(4, 1); 1918c3739801SMiguel Ojeda validate_against_rust!(4, 1, 0); 1919c3739801SMiguel Ojeda validate_against_rust!(4, 1, 1); 1920c3739801SMiguel Ojeda validate_against_rust!(4, 1, 2); 1921c3739801SMiguel Ojeda validate_against_rust!(4, 1, 3); 1922c3739801SMiguel Ojeda validate_against_rust!(4, 1, 4); 1923c3739801SMiguel Ojeda validate_against_rust!(4, 2); 1924c3739801SMiguel Ojeda validate_against_rust!(4, 2, 0); 1925c3739801SMiguel Ojeda validate_against_rust!(4, 2, 1); 1926c3739801SMiguel Ojeda validate_against_rust!(4, 2, 2); 1927c3739801SMiguel Ojeda validate_against_rust!(4, 2, 3); 1928c3739801SMiguel Ojeda validate_against_rust!(4, 2, 4); 1929c3739801SMiguel Ojeda validate_against_rust!(4, 4); 1930c3739801SMiguel Ojeda validate_against_rust!(4, 4, 0); 1931c3739801SMiguel Ojeda validate_against_rust!(4, 4, 1); 1932c3739801SMiguel Ojeda validate_against_rust!(4, 4, 2); 1933c3739801SMiguel Ojeda validate_against_rust!(4, 4, 3); 1934c3739801SMiguel Ojeda validate_against_rust!(4, 4, 4); 1935c3739801SMiguel Ojeda validate_against_rust!(4, 8); 1936c3739801SMiguel Ojeda validate_against_rust!(4, 8, 0); 1937c3739801SMiguel Ojeda validate_against_rust!(4, 8, 1); 1938c3739801SMiguel Ojeda validate_against_rust!(4, 8, 2); 1939c3739801SMiguel Ojeda validate_against_rust!(4, 8, 3); 1940c3739801SMiguel Ojeda validate_against_rust!(4, 8, 4); 1941c3739801SMiguel Ojeda validate_against_rust!(4, 16); 1942c3739801SMiguel Ojeda validate_against_rust!(4, 16, 0); 1943c3739801SMiguel Ojeda validate_against_rust!(4, 16, 1); 1944c3739801SMiguel Ojeda validate_against_rust!(4, 16, 2); 1945c3739801SMiguel Ojeda validate_against_rust!(4, 16, 3); 1946c3739801SMiguel Ojeda validate_against_rust!(4, 16, 4); 1947c3739801SMiguel Ojeda } 1948c3739801SMiguel Ojeda } 1949c3739801SMiguel Ojeda 1950c3739801SMiguel Ojeda #[cfg(kani)] 1951c3739801SMiguel Ojeda mod proofs { 1952c3739801SMiguel Ojeda use core::alloc::Layout; 1953c3739801SMiguel Ojeda 1954c3739801SMiguel Ojeda use super::*; 1955c3739801SMiguel Ojeda 1956c3739801SMiguel Ojeda impl kani::Arbitrary for DstLayout { 1957c3739801SMiguel Ojeda fn any() -> Self { 1958c3739801SMiguel Ojeda let align: NonZeroUsize = kani::any(); 1959c3739801SMiguel Ojeda let size_info: SizeInfo = kani::any(); 1960c3739801SMiguel Ojeda 1961c3739801SMiguel Ojeda kani::assume(align.is_power_of_two()); 1962c3739801SMiguel Ojeda kani::assume(align < DstLayout::THEORETICAL_MAX_ALIGN); 1963c3739801SMiguel Ojeda 1964c3739801SMiguel Ojeda // For testing purposes, we most care about instantiations of 1965c3739801SMiguel Ojeda // `DstLayout` that can correspond to actual Rust types. We use 1966c3739801SMiguel Ojeda // `Layout` to verify that our `DstLayout` satisfies the validity 1967c3739801SMiguel Ojeda // conditions of Rust layouts. 1968c3739801SMiguel Ojeda kani::assume( 1969c3739801SMiguel Ojeda match size_info { 1970c3739801SMiguel Ojeda SizeInfo::Sized { size } => Layout::from_size_align(size, align.get()), 1971c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size: _ }) => { 1972c3739801SMiguel Ojeda // `SliceDst` cannot encode an exact size, but we know 1973c3739801SMiguel Ojeda // it is at least `offset` bytes. 1974c3739801SMiguel Ojeda Layout::from_size_align(offset, align.get()) 1975c3739801SMiguel Ojeda } 1976c3739801SMiguel Ojeda } 1977c3739801SMiguel Ojeda .is_ok(), 1978c3739801SMiguel Ojeda ); 1979c3739801SMiguel Ojeda 1980c3739801SMiguel Ojeda Self { align: align, size_info: size_info, statically_shallow_unpadded: kani::any() } 1981c3739801SMiguel Ojeda } 1982c3739801SMiguel Ojeda } 1983c3739801SMiguel Ojeda 1984c3739801SMiguel Ojeda impl kani::Arbitrary for SizeInfo { 1985c3739801SMiguel Ojeda fn any() -> Self { 1986c3739801SMiguel Ojeda let is_sized: bool = kani::any(); 1987c3739801SMiguel Ojeda 1988c3739801SMiguel Ojeda match is_sized { 1989c3739801SMiguel Ojeda true => { 1990c3739801SMiguel Ojeda let size: usize = kani::any(); 1991c3739801SMiguel Ojeda 1992c3739801SMiguel Ojeda kani::assume(size <= DstLayout::MAX_SIZE); 1993c3739801SMiguel Ojeda 1994c3739801SMiguel Ojeda SizeInfo::Sized { size } 1995c3739801SMiguel Ojeda } 1996c3739801SMiguel Ojeda false => SizeInfo::SliceDst(kani::any()), 1997c3739801SMiguel Ojeda } 1998c3739801SMiguel Ojeda } 1999c3739801SMiguel Ojeda } 2000c3739801SMiguel Ojeda 2001c3739801SMiguel Ojeda impl kani::Arbitrary for TrailingSliceLayout { 2002c3739801SMiguel Ojeda fn any() -> Self { 2003c3739801SMiguel Ojeda let elem_size: usize = kani::any(); 2004c3739801SMiguel Ojeda let offset: usize = kani::any(); 2005c3739801SMiguel Ojeda 2006c3739801SMiguel Ojeda kani::assume(elem_size < DstLayout::MAX_SIZE); 2007c3739801SMiguel Ojeda kani::assume(offset < DstLayout::MAX_SIZE); 2008c3739801SMiguel Ojeda 2009c3739801SMiguel Ojeda TrailingSliceLayout { elem_size, offset } 2010c3739801SMiguel Ojeda } 2011c3739801SMiguel Ojeda } 2012c3739801SMiguel Ojeda 2013c3739801SMiguel Ojeda #[kani::proof] 2014c3739801SMiguel Ojeda fn prove_requires_dynamic_padding() { 2015c3739801SMiguel Ojeda let layout: DstLayout = kani::any(); 2016c3739801SMiguel Ojeda 2017c3739801SMiguel Ojeda let SizeInfo::SliceDst(size_info) = layout.size_info else { 2018c3739801SMiguel Ojeda kani::assume(false); 2019c3739801SMiguel Ojeda loop {} 2020c3739801SMiguel Ojeda }; 2021c3739801SMiguel Ojeda 2022c3739801SMiguel Ojeda let meta: usize = kani::any(); 2023c3739801SMiguel Ojeda 2024c3739801SMiguel Ojeda let Some(trailing_slice_size) = size_info.elem_size.checked_mul(meta) else { 2025c3739801SMiguel Ojeda // The `trailing_slice_size` exceeds `usize::MAX`; `meta` is invalid. 2026c3739801SMiguel Ojeda kani::assume(false); 2027c3739801SMiguel Ojeda loop {} 2028c3739801SMiguel Ojeda }; 2029c3739801SMiguel Ojeda 2030c3739801SMiguel Ojeda let Some(unpadded_size) = size_info.offset.checked_add(trailing_slice_size) else { 2031c3739801SMiguel Ojeda // The `unpadded_size` exceeds `usize::MAX`; `meta`` is invalid. 2032c3739801SMiguel Ojeda kani::assume(false); 2033c3739801SMiguel Ojeda loop {} 2034c3739801SMiguel Ojeda }; 2035c3739801SMiguel Ojeda 2036c3739801SMiguel Ojeda if unpadded_size >= DstLayout::MAX_SIZE { 2037c3739801SMiguel Ojeda // The `unpadded_size` exceeds `isize::MAX`; `meta` is invalid. 2038c3739801SMiguel Ojeda kani::assume(false); 2039c3739801SMiguel Ojeda loop {} 2040c3739801SMiguel Ojeda } 2041c3739801SMiguel Ojeda 2042c3739801SMiguel Ojeda let trailing_padding = util::padding_needed_for(unpadded_size, layout.align); 2043c3739801SMiguel Ojeda 2044c3739801SMiguel Ojeda if !layout.requires_dynamic_padding() { 2045c3739801SMiguel Ojeda assert!(trailing_padding == 0); 2046c3739801SMiguel Ojeda } 2047c3739801SMiguel Ojeda } 2048c3739801SMiguel Ojeda 2049c3739801SMiguel Ojeda #[kani::proof] 2050c3739801SMiguel Ojeda fn prove_dst_layout_extend() { 2051c3739801SMiguel Ojeda use crate::util::{max, min, padding_needed_for}; 2052c3739801SMiguel Ojeda 2053c3739801SMiguel Ojeda let base: DstLayout = kani::any(); 2054c3739801SMiguel Ojeda let field: DstLayout = kani::any(); 2055c3739801SMiguel Ojeda let packed: Option<NonZeroUsize> = kani::any(); 2056c3739801SMiguel Ojeda 2057c3739801SMiguel Ojeda if let Some(max_align) = packed { 2058c3739801SMiguel Ojeda kani::assume(max_align.is_power_of_two()); 2059c3739801SMiguel Ojeda kani::assume(base.align <= max_align); 2060c3739801SMiguel Ojeda } 2061c3739801SMiguel Ojeda 2062c3739801SMiguel Ojeda // The base can only be extended if it's sized. 2063c3739801SMiguel Ojeda kani::assume(matches!(base.size_info, SizeInfo::Sized { .. })); 2064c3739801SMiguel Ojeda let base_size = if let SizeInfo::Sized { size } = base.size_info { 2065c3739801SMiguel Ojeda size 2066c3739801SMiguel Ojeda } else { 2067c3739801SMiguel Ojeda unreachable!(); 2068c3739801SMiguel Ojeda }; 2069c3739801SMiguel Ojeda 2070c3739801SMiguel Ojeda // Under the above conditions, `DstLayout::extend` will not panic. 2071c3739801SMiguel Ojeda let composite = base.extend(field, packed); 2072c3739801SMiguel Ojeda 2073c3739801SMiguel Ojeda // The field's alignment is clamped by `max_align` (i.e., the 2074c3739801SMiguel Ojeda // `packed` attribute, if any) [1]. 2075c3739801SMiguel Ojeda // 2076c3739801SMiguel Ojeda // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers: 2077c3739801SMiguel Ojeda // 2078c3739801SMiguel Ojeda // The alignments of each field, for the purpose of positioning 2079c3739801SMiguel Ojeda // fields, is the smaller of the specified alignment and the 2080c3739801SMiguel Ojeda // alignment of the field's type. 2081c3739801SMiguel Ojeda let field_align = min(field.align, packed.unwrap_or(DstLayout::THEORETICAL_MAX_ALIGN)); 2082c3739801SMiguel Ojeda 2083c3739801SMiguel Ojeda // The struct's alignment is the maximum of its previous alignment and 2084c3739801SMiguel Ojeda // `field_align`. 2085c3739801SMiguel Ojeda assert_eq!(composite.align, max(base.align, field_align)); 2086c3739801SMiguel Ojeda 2087c3739801SMiguel Ojeda // Compute the minimum amount of inter-field padding needed to 2088c3739801SMiguel Ojeda // satisfy the field's alignment, and offset of the trailing field. 2089c3739801SMiguel Ojeda // [1] 2090c3739801SMiguel Ojeda // 2091c3739801SMiguel Ojeda // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers: 2092c3739801SMiguel Ojeda // 2093c3739801SMiguel Ojeda // Inter-field padding is guaranteed to be the minimum required in 2094c3739801SMiguel Ojeda // order to satisfy each field's (possibly altered) alignment. 2095c3739801SMiguel Ojeda let padding = padding_needed_for(base_size, field_align); 2096c3739801SMiguel Ojeda let offset = base_size + padding; 2097c3739801SMiguel Ojeda 2098c3739801SMiguel Ojeda // For testing purposes, we'll also construct `alloc::Layout` 2099c3739801SMiguel Ojeda // stand-ins for `DstLayout`, and show that `extend` behaves 2100c3739801SMiguel Ojeda // comparably on both types. 2101c3739801SMiguel Ojeda let base_analog = Layout::from_size_align(base_size, base.align.get()).unwrap(); 2102c3739801SMiguel Ojeda 2103c3739801SMiguel Ojeda match field.size_info { 2104c3739801SMiguel Ojeda SizeInfo::Sized { size: field_size } => { 2105c3739801SMiguel Ojeda if let SizeInfo::Sized { size: composite_size } = composite.size_info { 2106c3739801SMiguel Ojeda // If the trailing field is sized, the resulting layout will 2107c3739801SMiguel Ojeda // be sized. Its size will be the sum of the preceding 2108c3739801SMiguel Ojeda // layout, the size of the new field, and the size of 2109c3739801SMiguel Ojeda // inter-field padding between the two. 2110c3739801SMiguel Ojeda assert_eq!(composite_size, offset + field_size); 2111c3739801SMiguel Ojeda 2112c3739801SMiguel Ojeda let field_analog = 2113c3739801SMiguel Ojeda Layout::from_size_align(field_size, field_align.get()).unwrap(); 2114c3739801SMiguel Ojeda 2115c3739801SMiguel Ojeda if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog) 2116c3739801SMiguel Ojeda { 2117c3739801SMiguel Ojeda assert_eq!(actual_offset, offset); 2118c3739801SMiguel Ojeda assert_eq!(actual_composite.size(), composite_size); 2119c3739801SMiguel Ojeda assert_eq!(actual_composite.align(), composite.align.get()); 2120c3739801SMiguel Ojeda } else { 2121c3739801SMiguel Ojeda // An error here reflects that composite of `base` 2122c3739801SMiguel Ojeda // and `field` cannot correspond to a real Rust type 2123c3739801SMiguel Ojeda // fragment, because such a fragment would violate 2124c3739801SMiguel Ojeda // the basic invariants of a valid Rust layout. At 2125c3739801SMiguel Ojeda // the time of writing, `DstLayout` is a little more 2126c3739801SMiguel Ojeda // permissive than `Layout`, so we don't assert 2127c3739801SMiguel Ojeda // anything in this branch (e.g., unreachability). 2128c3739801SMiguel Ojeda } 2129c3739801SMiguel Ojeda } else { 2130c3739801SMiguel Ojeda panic!("The composite of two sized layouts must be sized.") 2131c3739801SMiguel Ojeda } 2132c3739801SMiguel Ojeda } 2133c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { 2134c3739801SMiguel Ojeda offset: field_offset, 2135c3739801SMiguel Ojeda elem_size: field_elem_size, 2136c3739801SMiguel Ojeda }) => { 2137c3739801SMiguel Ojeda if let SizeInfo::SliceDst(TrailingSliceLayout { 2138c3739801SMiguel Ojeda offset: composite_offset, 2139c3739801SMiguel Ojeda elem_size: composite_elem_size, 2140c3739801SMiguel Ojeda }) = composite.size_info 2141c3739801SMiguel Ojeda { 2142c3739801SMiguel Ojeda // The offset of the trailing slice component is the sum 2143c3739801SMiguel Ojeda // of the offset of the trailing field and the trailing 2144c3739801SMiguel Ojeda // slice offset within that field. 2145c3739801SMiguel Ojeda assert_eq!(composite_offset, offset + field_offset); 2146c3739801SMiguel Ojeda // The elem size is unchanged. 2147c3739801SMiguel Ojeda assert_eq!(composite_elem_size, field_elem_size); 2148c3739801SMiguel Ojeda 2149c3739801SMiguel Ojeda let field_analog = 2150c3739801SMiguel Ojeda Layout::from_size_align(field_offset, field_align.get()).unwrap(); 2151c3739801SMiguel Ojeda 2152c3739801SMiguel Ojeda if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog) 2153c3739801SMiguel Ojeda { 2154c3739801SMiguel Ojeda assert_eq!(actual_offset, offset); 2155c3739801SMiguel Ojeda assert_eq!(actual_composite.size(), composite_offset); 2156c3739801SMiguel Ojeda assert_eq!(actual_composite.align(), composite.align.get()); 2157c3739801SMiguel Ojeda } else { 2158c3739801SMiguel Ojeda // An error here reflects that composite of `base` 2159c3739801SMiguel Ojeda // and `field` cannot correspond to a real Rust type 2160c3739801SMiguel Ojeda // fragment, because such a fragment would violate 2161c3739801SMiguel Ojeda // the basic invariants of a valid Rust layout. At 2162c3739801SMiguel Ojeda // the time of writing, `DstLayout` is a little more 2163c3739801SMiguel Ojeda // permissive than `Layout`, so we don't assert 2164c3739801SMiguel Ojeda // anything in this branch (e.g., unreachability). 2165c3739801SMiguel Ojeda } 2166c3739801SMiguel Ojeda } else { 2167c3739801SMiguel Ojeda panic!("The extension of a layout with a DST must result in a DST.") 2168c3739801SMiguel Ojeda } 2169c3739801SMiguel Ojeda } 2170c3739801SMiguel Ojeda } 2171c3739801SMiguel Ojeda } 2172c3739801SMiguel Ojeda 2173c3739801SMiguel Ojeda #[kani::proof] 2174c3739801SMiguel Ojeda #[kani::should_panic] 2175c3739801SMiguel Ojeda fn prove_dst_layout_extend_dst_panics() { 2176c3739801SMiguel Ojeda let base: DstLayout = kani::any(); 2177c3739801SMiguel Ojeda let field: DstLayout = kani::any(); 2178c3739801SMiguel Ojeda let packed: Option<NonZeroUsize> = kani::any(); 2179c3739801SMiguel Ojeda 2180c3739801SMiguel Ojeda if let Some(max_align) = packed { 2181c3739801SMiguel Ojeda kani::assume(max_align.is_power_of_two()); 2182c3739801SMiguel Ojeda kani::assume(base.align <= max_align); 2183c3739801SMiguel Ojeda } 2184c3739801SMiguel Ojeda 2185c3739801SMiguel Ojeda kani::assume(matches!(base.size_info, SizeInfo::SliceDst(..))); 2186c3739801SMiguel Ojeda 2187c3739801SMiguel Ojeda let _ = base.extend(field, packed); 2188c3739801SMiguel Ojeda } 2189c3739801SMiguel Ojeda 2190c3739801SMiguel Ojeda #[kani::proof] 2191c3739801SMiguel Ojeda fn prove_dst_layout_pad_to_align() { 2192c3739801SMiguel Ojeda use crate::util::padding_needed_for; 2193c3739801SMiguel Ojeda 2194c3739801SMiguel Ojeda let layout: DstLayout = kani::any(); 2195c3739801SMiguel Ojeda 2196c3739801SMiguel Ojeda let padded = layout.pad_to_align(); 2197c3739801SMiguel Ojeda 2198c3739801SMiguel Ojeda // Calling `pad_to_align` does not alter the `DstLayout`'s alignment. 2199c3739801SMiguel Ojeda assert_eq!(padded.align, layout.align); 2200c3739801SMiguel Ojeda 2201c3739801SMiguel Ojeda if let SizeInfo::Sized { size: unpadded_size } = layout.size_info { 2202c3739801SMiguel Ojeda if let SizeInfo::Sized { size: padded_size } = padded.size_info { 2203c3739801SMiguel Ojeda // If the layout is sized, it will remain sized after padding is 2204c3739801SMiguel Ojeda // added. Its sum will be its unpadded size and the size of the 2205c3739801SMiguel Ojeda // trailing padding needed to satisfy its alignment 2206c3739801SMiguel Ojeda // requirements. 2207c3739801SMiguel Ojeda let padding = padding_needed_for(unpadded_size, layout.align); 2208c3739801SMiguel Ojeda assert_eq!(padded_size, unpadded_size + padding); 2209c3739801SMiguel Ojeda 2210c3739801SMiguel Ojeda // Prove that calling `DstLayout::pad_to_align` behaves 2211c3739801SMiguel Ojeda // identically to `Layout::pad_to_align`. 2212c3739801SMiguel Ojeda let layout_analog = 2213c3739801SMiguel Ojeda Layout::from_size_align(unpadded_size, layout.align.get()).unwrap(); 2214c3739801SMiguel Ojeda let padded_analog = layout_analog.pad_to_align(); 2215c3739801SMiguel Ojeda assert_eq!(padded_analog.align(), layout.align.get()); 2216c3739801SMiguel Ojeda assert_eq!(padded_analog.size(), padded_size); 2217c3739801SMiguel Ojeda } else { 2218c3739801SMiguel Ojeda panic!("The padding of a sized layout must result in a sized layout.") 2219c3739801SMiguel Ojeda } 2220c3739801SMiguel Ojeda } else { 2221c3739801SMiguel Ojeda // If the layout is a DST, padding cannot be statically added. 2222c3739801SMiguel Ojeda assert_eq!(padded.size_info, layout.size_info); 2223c3739801SMiguel Ojeda } 2224c3739801SMiguel Ojeda } 2225c3739801SMiguel Ojeda } 2226