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