Skip to main content

zerocopy/
layout.rs

1// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
2//
3// Copyright 2024 The Fuchsia Authors
4//
5// Licensed under the 2-Clause BSD License <LICENSE-BSD or
6// https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0
7// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
8// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
9// This file may not be copied, modified, or distributed except according to
10// those terms.
11
12use core::{mem, num::NonZeroUsize};
13
14use crate::util;
15
16/// The target pointer width, counted in bits.
17const POINTER_WIDTH_BITS: usize = mem::size_of::<usize>() * 8;
18
19/// The layout of a type which might be dynamically-sized.
20///
21/// `DstLayout` describes the layout of sized types, slice types, and "slice
22/// DSTs" - ie, those that are known by the type system to have a trailing slice
23/// (as distinguished from `dyn Trait` types - such types *might* have a
24/// trailing slice type, but the type system isn't aware of it).
25///
26/// Note that `DstLayout` does not have any internal invariants, so no guarantee
27/// is made that a `DstLayout` conforms to any of Rust's requirements regarding
28/// the layout of real Rust types or instances of types.
29#[doc(hidden)]
30#[allow(missing_debug_implementations, missing_copy_implementations)]
31#[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))]
32#[derive(Copy, Clone)]
33#[cfg_attr(kani, derive(kani::Arbitrary))]
34pub struct DstLayout {
35    pub(crate) align: NonZeroUsize,
36    pub(crate) size_info: SizeInfo,
37    // Is it guaranteed statically (without knowing a value's runtime metadata)
38    // that the top-level type contains no padding? This does *not* apply
39    // recursively - for example, `[(u8, u16)]` has `statically_shallow_unpadded
40    // = true` even though this type likely has padding inside each `(u8, u16)`.
41    pub(crate) statically_shallow_unpadded: bool,
42}
43
44#[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))]
45#[derive(Copy, Clone)]
46#[cfg_attr(kani, derive(kani::Arbitrary))]
47pub(crate) enum SizeInfo<E = usize> {
48    Sized { size: usize },
49    SliceDst(TrailingSliceLayout<E>),
50}
51
52/// The alignment and phase of a rounded size calculation.
53///
54/// If `encoded` is the stored non-zero integer, then its highest set bit is a
55/// power-of-two alignment `align`, and its remaining lower bits are a `phase`
56/// in `0..align`. Thus every non-zero bit pattern is a valid encoding, and:
57///
58/// ```text
59/// encoded = align + phase
60/// ```
61#[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))]
62#[repr(transparent)]
63#[derive(Copy, Clone)]
64#[cfg_attr(kani, derive(kani::Arbitrary))]
65pub(crate) struct RoundingAlignAndPhase(NonZeroUsize);
66
67impl RoundingAlignAndPhase {
68    /// Encodes `align` and `phase`.
69    ///
70    /// # Panics
71    ///
72    /// Panics if `align` is not a power of two and `phase` is not less than
73    /// `align`.
74    #[inline(always)]
75    #[cfg_attr(kani, kani::requires(align.is_power_of_two() && phase < align.get()))]
76    #[cfg_attr(kani, kani::ensures(|result| result.0.get() == align.get() + phase))]
77    pub(crate) const fn new(align: NonZeroUsize, phase: usize) -> Self {
78        #[cfg(kani)]
79        #[kani::proof_for_contract(RoundingAlignAndPhase::new)]
80        #[kani::solver(kissat)]
81        fn proof() {
82            RoundingAlignAndPhase::new(kani::any(), kani::any());
83        }
84
85        const_assert!(align.get().is_power_of_two());
86        const_assert!(phase < align.get());
87
88        // Since `align` is a power of two and `phase < align`, their set bits
89        // are disjoint. The result is therefore non-zero and losslessly stores
90        // both components.
91        let encoded = align.get() | phase;
92        match NonZeroUsize::new(encoded) {
93            Some(encoded) => Self(encoded),
94            None => const_unreachable!(),
95        }
96    }
97
98    /// Decodes the alignment and phase.
99    #[inline(always)]
100    #[cfg_attr(kani, kani::ensures(|&(align, phase)| {
101        align.is_power_of_two() && phase < align.get()
102            && align.get().checked_add(phase) == Some(self.0.get())
103    }))]
104    pub(crate) const fn components(self) -> (NonZeroUsize, usize) {
105        #[cfg(kani)]
106        #[kani::proof_for_contract(RoundingAlignAndPhase::components)]
107        #[kani::solver(kissat)]
108        fn proof() {
109            kani::any::<RoundingAlignAndPhase>().components();
110        }
111        // `leading_zeros <= POINTER_WIDTH_BITS - 1` because the encoded value
112        // is non-zero. Converting `leading_zeros` to `usize` cannot truncate
113        // because it is at most the number of bits in a `usize`.
114        #[allow(
115            clippy::arithmetic_side_effects,
116            clippy::as_conversions,
117            clippy::needless_nonzero_get
118        )]
119        let shift = (POINTER_WIDTH_BITS - 1) - (self.0.get().leading_zeros() as usize);
120        #[allow(clippy::arithmetic_side_effects)]
121        let align = 1usize << shift;
122        let align = match NonZeroUsize::new(align) {
123            Some(align) => align,
124            None => const_unreachable!(),
125        };
126        // `align` is the highest set bit, so XOR removes exactly that bit and
127        // leaves a value in `0..align`.
128        let phase = self.0.get() ^ align.get();
129        (align, phase)
130    }
131
132    /// Decodes the alignment, which is the highest set bit of `self`.
133    #[inline(always)]
134    #[cfg_attr(kani, kani::ensures(|align| {
135        align.is_power_of_two() && align.get() <= self.0.get()
136            && self.0.get() - align.get() < align.get()
137    }))]
138    pub(crate) const fn align(self) -> NonZeroUsize {
139        #[cfg(kani)]
140        #[kani::proof_for_contract(RoundingAlignAndPhase::align)]
141        #[kani::solver(kissat)]
142        fn proof() {
143            kani::any::<RoundingAlignAndPhase>().align();
144        }
145
146        self.components().0
147    }
148}
149
150#[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))]
151#[derive(Copy, Clone)]
152#[cfg_attr(kani, derive(kani::Arbitrary))]
153pub(crate) struct TrailingSliceLayout<E = usize> {
154    /// The offset of the first byte of the trailing slice field. Note that this
155    /// is NOT the same as the minimum size of the type. For example, consider
156    /// the following type:
157    ///
158    /// ```ignore
159    /// #[repr(C, align(2))]
160    /// struct Foo {
161    ///     header: u16,
162    ///     tag: u8,
163    ///     tail: [u8],
164    /// }
165    /// ```
166    ///
167    /// In `Foo`, `tail` is at byte offset 3. When `tail.len() == 0`, `tail` is
168    /// followed by a padding byte.
169    pub(crate) offset: usize,
170    /// The size of the element type of the trailing slice field.
171    pub(crate) elem_size: E,
172    /// The trailing slice's byte offset plus fixed trailing padding, minus the
173    /// rounding phase.
174    ///
175    /// Fixed trailing padding excludes padding added by the final alignment
176    /// rounding. The rounding alignment and phase are stored in
177    /// `size_rounding_align_and_phase`. This value is independent of the slice
178    /// length and can be smaller than, equal to, or larger than `offset`.
179    ///
180    /// For a completed layout, this value is a multiple of `DstLayout::align`.
181    /// The minimum object size equals this value when the phase is zero;
182    /// otherwise, it exceeds this value by one rounding-alignment unit.
183    pub(crate) size_base: usize,
184    /// The alignment and byte phase of the rounding applied to the trailing
185    /// slice's contribution to object size.
186    ///
187    /// The alignment `size_align` is a nonzero power of two. The phase
188    /// `size_phase` is the fixed byte contribution inside that rounding, in
189    /// `0..size_align`. Both are independent of the trailing slice's length.
190    /// For `elems` trailing elements, the enclosing object's size is:
191    ///
192    /// ```text
193    /// size_base + round_up(size_phase + elems * elem_size, size_align)
194    /// ```
195    ///
196    /// For a completed layout, `size_align` is at least `DstLayout::align`.
197    /// Packing can reduce the enclosing type's alignment and the alignment
198    /// used to place its trailing field without removing padding required by
199    /// a nested DST's own layout. For example, a `repr(packed(2))` wrapper
200    /// around a DST with alignment 4 can have an object alignment of 2 and a
201    /// rounding alignment of 4.
202    ///
203    /// The rounding parameters account for padding preserved within nested
204    /// fields. Their rounding origin can differ from the physical start of
205    /// the trailing slice, so the phase need not equal `offset % size_align`.
206    pub(crate) size_rounding_align_and_phase: RoundingAlignAndPhase,
207}
208
209impl<E> TrailingSliceLayout<E> {
210    /// Produces `self.size_base` rounded down to a multiple of `size_align`,
211    /// plus `size_phase`, where both components come from
212    /// `self.size_rounding_align_and_phase`.
213    ///
214    /// This value need not equal `self.offset`, the trailing slice's byte
215    /// offset.
216    #[inline(always)]
217    #[cfg_attr(kani, kani::ensures(|&offset| {
218        let (align, phase) = self.size_rounding_align_and_phase.components();
219        offset == util::round_down_to_next_multiple_of_alignment(self.size_base, align) + phase
220    }))]
221    const fn size_offset(&self) -> usize {
222        #[cfg(all(kani, kani_slow))]
223        #[kani::proof_for_contract(TrailingSliceLayout::<usize>::size_offset)]
224        #[kani::solver(kissat)]
225        fn proof() {
226            kani::any::<TrailingSliceLayout>().size_offset();
227        }
228
229        let (size_align, size_phase) = self.size_rounding_align_and_phase.components();
230        let aligned_base =
231            util::round_down_to_next_multiple_of_alignment(self.size_base, size_align);
232        // The aligned base and phase have disjoint set bits.
233        aligned_base | size_phase
234    }
235
236    /// Produces the largest trailing-slice byte capacity whose complete, padded
237    /// object fits in `available_bytes`, or `None` if the object does not fit
238    /// even with an empty trailing slice.
239    ///
240    /// This capacity is independent of `elem_size`: it need not represent a
241    /// whole number of elements, even for a valid Rust layout. For example, a
242    /// slice of `[u8; 3]` elements has three-byte elements and alignment 1.
243    /// With `available_bytes = 4`, this method produces `Some(4)`, although
244    /// only one whole element, occupying three bytes, fits.
245    #[inline(always)]
246    #[cfg_attr(kani, kani::ensures(|&result| {
247        let (align, phase) = self.size_rounding_align_and_phase.components();
248        result == available_bytes.checked_sub(self.size_base)
249            .map(|bytes| util::round_down_to_next_multiple_of_alignment(bytes, align))
250            .and_then(|rounded| rounded.checked_sub(phase))
251    }))]
252    const fn max_trailing_bytes(&self, available_bytes: usize) -> Option<usize> {
253        #[cfg(all(kani, kani_slow))]
254        #[kani::proof_for_contract(TrailingSliceLayout::<usize>::max_trailing_bytes)]
255        #[kani::solver(kissat)]
256        fn proof() {
257            kani::any::<TrailingSliceLayout>().max_trailing_bytes(kani::any());
258        }
259
260        #[cfg(all(kani, kani_slow))]
261        #[kani::proof_for_contract(TrailingSliceLayout::<NonZeroUsize>::max_trailing_bytes)]
262        #[kani::solver(kissat)]
263        fn proof_nonzero() {
264            kani::any::<TrailingSliceLayout<NonZeroUsize>>().max_trailing_bytes(kani::any());
265        }
266
267        let (size_align, size_phase) = self.size_rounding_align_and_phase.components();
268
269        // Interpret the normalized size formula over the nonnegative integers:
270        //
271        //   object_size(trailing_bytes)
272        //       = self.size_base
273        //           + round_up(size_phase + trailing_bytes, size_align).
274        //
275        // We seek the largest byte count with
276        // `object_size(trailing_bytes) <= available_bytes`.
277        //
278        // At zero trailing bytes, the input to rounding is just `size_phase`.
279        // Since `0 <= size_phase < size_align`, the least alignment multiple at
280        // or above it is zero for a zero phase, and `size_align` otherwise.
281        // Thus `rounded_phase = round_up(size_phase, size_align)` is exactly
282        // the rounded contribution of an empty tail.
283        let rounded_phase = if size_phase == 0 { 0 } else { size_align.get() };
284
285        // Adding the fixed base gives `minimum_size = object_size(0)`. The size
286        // formula is nondecreasing in the trailing byte count, so this is the
287        // minimum object size. If this sum exceeds `usize::MAX`, it also
288        // exceeds `available_bytes`, and no tail can fit.
289        let minimum_size = match self.size_base.checked_add(rounded_phase) {
290            Some(size) => size,
291            None => return None,
292        };
293
294        // Subtracting the minimum size gives the budget for increasing the
295        // rounded contribution. Failure means even the minimum size exceeds the
296        // available space. Success establishes `minimum_size + extra_bytes =
297        // available_bytes`.
298        let extra_bytes = match available_bytes.checked_sub(minimum_size) {
299            Some(bytes) => bytes,
300            None => return None,
301        };
302
303        // The rounded contribution can increase only by multiples of
304        // `size_align`. Its largest permitted increase is therefore
305        // `aligned_bytes = round_down(extra_bytes, size_align)`, with:
306        //
307        //   aligned_bytes <= extra_bytes < aligned_bytes + size_align.
308        //
309        // Hence `rounded_phase + aligned_bytes` is the largest alignment
310        // multiple that fits after the base: adding the base gives at most
311        // `minimum_size + extra_bytes = available_bytes`, while the next
312        // alignment multiple would exceed that budget.
313        let aligned_bytes = util::round_down_to_next_multiple_of_alignment(extra_bytes, size_align);
314
315        // Rounding an empty tail adds `rounded_phase - size_phase` bytes. This
316        // is `initial_padding`: the tail can use these bytes without increasing
317        // the rounded contribution. The two cases for `rounded_phase` give `0
318        // <= initial_padding < size_align`, so the subtraction is
319        // representable.
320        #[allow(clippy::arithmetic_side_effects)]
321        let initial_padding = rounded_phase - size_phase;
322
323        // For any nonnegative candidate byte count, the aligned limit above
324        // gives the following equivalences:
325        //
326        //   object_size(candidate_bytes) <= available_bytes
327        //       iff round_up(size_phase + candidate_bytes, size_align)
328        //           <= rounded_phase + aligned_bytes
329        //       iff size_phase + candidate_bytes
330        //           <= rounded_phase + aligned_bytes
331        //       iff candidate_bytes <= aligned_bytes + initial_padding.
332        //
333        // Removing `round_up` is valid because its upper bound is an alignment
334        // multiple. Thus `aligned_bytes + initial_padding` is exactly the
335        // largest tail that fits. Since `aligned_bytes` is a multiple of the
336        // power-of-two `size_align` and `initial_padding < size_align`, their
337        // set bits are disjoint. Bitwise OR therefore equals their sum, with no
338        // carry or overflow.
339        let trailing_bytes = aligned_bytes | initial_padding;
340        Some(trailing_bytes)
341    }
342}
343
344impl TrailingSliceLayout {
345    /// Returns the difference between the complete object size and the
346    /// trailing-slice end for `elems` elements, modulo `usize::MAX + 1`.
347    ///
348    /// If the object size fits in `usize` and contains the trailing slice,
349    /// this is the exact trailing padding. Other inputs need not describe a
350    /// Rust type; their result is still the modular difference.
351    #[inline(always)]
352    #[cfg_attr(kani, kani::ensures(|&padding| {
353        let (align, phase) = self.size_rounding_align_and_phase.components();
354        let trailing_bytes = elems.wrapping_mul(self.elem_size);
355        let mask = align.get() - 1;
356        let rounded = phase.wrapping_add(trailing_bytes).wrapping_add(mask) & !mask;
357        let object_size = self.size_base.wrapping_add(rounded);
358        padding == object_size.wrapping_sub(self.offset.wrapping_add(trailing_bytes))
359    }))]
360    pub(crate) const fn padding_for_elems(self, elems: usize) -> usize {
361        #[cfg(all(kani, kani_slow))]
362        #[kani::proof_for_contract(TrailingSliceLayout::padding_for_elems)]
363        #[kani::solver(kissat)]
364        fn proof() {
365            kani::any::<TrailingSliceLayout>().padding_for_elems(kani::any());
366        }
367
368        let (size_align, size_phase) = self.size_rounding_align_and_phase.components();
369
370        // Since `size_align` is a nonzero power of two, subtracting one gives
371        // a mask selecting precisely the remainder modulo `size_align`.
372        #[allow(clippy::arithmetic_side_effects)]
373        let size_mask = size_align.get() - 1;
374
375        // Removing whole alignment multiples from each element's size
376        // preserves the trailing-byte count modulo `size_align`. If elements
377        // occupy whole alignment units, this is zero, making the padding's
378        // independence from the element count explicit to the optimizer.
379        let elem_remainder = self.elem_size & size_mask;
380
381        // The product is congruent to `elems * self.elem_size` modulo
382        // `size_align`. Wrapping preserves that congruence because every
383        // power-of-two `size_align` divides `usize::MAX + 1`. Masking then
384        // gives the exact trailing-byte remainder in `0..size_align`.
385        let trailing_remainder = elems.wrapping_mul(elem_remainder) & size_mask;
386
387        // Adding `size_phase` gives the same remainder as the mathematical
388        // `size_phase + elems * self.elem_size`. Reducing before adding the
389        // phase also exposes this periodicity to the optimizer.
390        let rounding_input = size_phase.wrapping_add(trailing_remainder);
391
392        // `rounding_padding` is the unique value in `0..size_align` that
393        // rounds `rounding_input` up to an alignment multiple. Since only
394        // the remainder determines this value, it is also the padding added
395        // by `round_up(size_phase + elems * self.elem_size, size_align)`.
396        let rounding_padding = util::padding_needed_for(rounding_input, size_align);
397
398        // Write `trailing_bytes = elems * self.elem_size` over mathematical
399        // integers. Subtracting the trailing-slice end from the complete
400        // object size cancels `trailing_bytes`:
401        //
402        //   size_base + round_up(size_phase + trailing_bytes, size_align)
403        //       - (offset + trailing_bytes)
404        //       = size_base + size_phase - offset + rounding_padding.
405        //
406        // Wrapping computes this difference modulo `usize::MAX + 1`, even
407        // if an intermediate value or the difference is unrepresentable.
408        // If the object size fits in `usize` and contains the trailing slice,
409        // the difference is in `0..=usize::MAX`; its residue is then the
410        // exact number of padding bytes after the trailing slice.
411        self.size_base
412            .wrapping_add(size_phase)
413            .wrapping_sub(self.offset)
414            .wrapping_add(rounding_padding)
415    }
416
417    /// Computes the size described by this layout for `elems` trailing slice
418    /// elements.
419    ///
420    /// Returns `None` if the element-byte count or complete rounded size
421    /// cannot be represented as a `usize`.
422    #[inline(always)]
423    #[cfg_attr(kani, kani::ensures(|&size| {
424        size == elems.checked_mul(self.elem_size)
425            .and_then(|bytes| proofs::slice_dst_size_for_trailing_bytes(self, bytes))
426    }))]
427    pub(crate) const fn size_for_elems(self, elems: usize) -> Option<usize> {
428        #[cfg(all(kani, kani_slow))]
429        #[kani::proof_for_contract(TrailingSliceLayout::size_for_elems)]
430        #[kani::solver(kissat)]
431        fn proof() {
432            kani::any::<TrailingSliceLayout>().size_for_elems(kani::any());
433        }
434
435        // Let `size_align` and `size_phase` denote the components stored in
436        // `size_rounding_align_and_phase`. The normalized layout formula,
437        // evaluated over the nonnegative integers, is:
438        //
439        //   object_size(trailing_bytes)
440        //       = self.size_base
441        //           + round_up(size_phase + trailing_bytes, size_align)
442        //
443        // Here, `round_up` gives the least multiple of `size_align` greater
444        // than or equal to its input. Thus `object_size` is nondecreasing.
445        // By the capacity helper's contract, `max_trailing_bytes` is the
446        // largest byte count for which `object_size` fits in a `usize`.
447        // Monotonicity then gives the equivalence:
448        //
449        //   object_size(trailing_bytes) <= usize::MAX
450        //       iff trailing_bytes <= max_trailing_bytes.
451        //
452        // If the helper returns `None`, even `object_size(0)` exceeds the
453        // limit, so no element count can produce a representable size.
454        let max_trailing_bytes = match self.max_trailing_bytes(usize::MAX) {
455            Some(bytes) => bytes,
456            None => return None,
457        };
458
459        // Each of the `elems` elements contributes `self.elem_size` bytes,
460        // so substitution into the formula requires their product. Checked
461        // multiplication yields that exact product, including zero for
462        // zero-sized elements. If it overflows, the complete size cannot fit
463        // either: `object_size(trailing_bytes) >= trailing_bytes` because
464        // the base, phase, and rounding increment are all nonnegative.
465        let trailing_size = match self.elem_size.checked_mul(elems) {
466            Some(bytes) => bytes,
467            None => return None,
468        };
469
470        // The equivalence above rejects exactly the remaining cases whose
471        // complete size exceeds `usize::MAX`. After this check we have
472        // `object_size(trailing_size) <= usize::MAX`.
473        if trailing_size > max_trailing_bytes {
474            return None;
475        }
476
477        // The trailing slice starts at `self.offset` and occupies
478        // `trailing_size` bytes. Thus `trailing_end` is its end offset modulo
479        // `usize::MAX + 1`. This sum need not fit: a raw layout need not
480        // describe a Rust type, so its trailing slice need not lie within
481        // the complete object size bounded above.
482        let trailing_end = self.offset.wrapping_add(trailing_size);
483
484        // `padding_for_elems` returns `object_size(trailing_size)` minus the
485        // trailing-slice end, modulo `usize::MAX + 1`. Adding the end back
486        // therefore gives `object_size(trailing_size)` modulo the same
487        // modulus. The bound above establishes that this size fits in
488        // `usize`, so its residue is the exact size specified by the layout.
489        let size = trailing_end.wrapping_add(self.padding_for_elems(elems));
490        Some(size)
491    }
492
493    /// Returns `true` only when `self` and `other` describe the same size for
494    /// every trailing slice length.
495    ///
496    /// This recognizes sufficient conditions for equality. A `false` result
497    /// also includes equivalent size sequences that this test cannot recognize.
498    #[inline(always)]
499    #[cfg_attr(kani, kani::ensures(|&same| {
500        let (self_align, self_phase) = self.size_rounding_align_and_phase.components();
501        let (other_align, other_phase) = other.size_rounding_align_and_phase.components();
502        let initial_size = proofs::slice_dst_size_for_trailing_bytes(self, 0);
503        same == (self.elem_size == other.elem_size
504            && initial_size.is_some()
505            && initial_size == proofs::slice_dst_size_for_trailing_bytes(other, 0)
506            && ((self.elem_size % self_align.get() == 0
507                && self.elem_size % other_align.get() == 0)
508                || (self_align == other_align && self_phase == other_phase)))
509    }))]
510    pub(crate) const fn has_same_size_sequence(self, other: Self) -> bool {
511        #[cfg(all(kani, kani_slow))]
512        #[kani::proof_for_contract(TrailingSliceLayout::has_same_size_sequence)]
513        #[kani::solver(kissat)]
514        fn proof() {
515            kani::any::<TrailingSliceLayout>().has_same_size_sequence(kani::any());
516        }
517
518        // For either layout, interpret its normalized size formula over the
519        // nonnegative integers:
520        //
521        //   object_size(elems)
522        //       = size_base
523        //           + round_up(size_phase + elems * elem_size, size_align).
524        //
525        // In two cases below, we establish equality of these mathematical sizes
526        // for every `elems`. Equal sizes also have the same `usize`
527        // representability bound.
528        //
529        // Both cases require a common byte contribution at each element count.
530        // Equal element sizes give that shared term, `elems * elem_size`.
531        if self.elem_size != other.elem_size {
532            return false;
533        }
534
535        // At `elems == 0`, the element contribution vanishes. Require equal,
536        // representable initial sizes; each case below then shows that
537        // increasing `elems` adds the same amount to both initial sizes.
538        match (self.size_for_elems(0), other.size_for_elems(0)) {
539            (Some(self_size), Some(other_size)) if self_size == other_size => {}
540            _ => return false,
541        }
542
543        let (self_align, self_phase) = self.size_rounding_align_and_phase.components();
544        let (other_align, other_phase) = other.size_rounding_align_and_phase.components();
545        let self_align = self_align.get();
546        let other_align = other_align.get();
547
548        // Case 1: The element size is a multiple of both alignments.
549        //
550        // For two power-of-two alignments, the larger is divisible by the
551        // smaller. Thus `max_align` is their least common multiple, and a byte
552        // count is divisible by both alignments exactly when it is divisible by
553        // `max_align`.
554        let max_align = if self_align > other_align { self_align } else { other_align };
555
556        // If the element size is divisible by both alignments, so is `elems *
557        // elem_size`. Adding an alignment multiple commutes with rounding,
558        // giving the following identity for each layout:
559        //
560        //   round_up(size_phase + elems * elem_size, size_align)
561        //       = round_up(size_phase, size_align) + elems * elem_size.
562        //
563        // Consequently, `object_size(elems) = object_size(0) + elems *
564        // elem_size`. The initial sizes and element sizes already agree, so the
565        // complete size sequences agree. This also covers zero-sized elements,
566        // whose contribution is zero for every element count.
567        #[allow(clippy::arithmetic_side_effects)]
568        if self.elem_size % max_align == 0 {
569            return true;
570        }
571
572        // Case 2: The rounding alignments and phases match.
573        //
574        // Require a common rounding alignment before comparing phases. If the
575        // alignments differ, neither case establishes equality.
576        if self_align != other_align {
577            return false;
578        }
579
580        // If the phases also agree, both `round_up` operations have the same
581        // input and alignment at every element count. In particular, their
582        // rounded terms agree at zero. Subtracting that common term from the
583        // equal initial sizes proves `self.size_base == other.size_base`. Both
584        // the fixed and rounded contributions therefore agree for every
585        // `elems`, which establishes equality of the complete size formulas.
586        self_phase == other_phase
587    }
588
589    /// Advances the size formula by `bytes` while replacing its element size.
590    ///
591    /// Let `(size_align, size_phase)` be decoded from
592    /// `self.size_rounding_align_and_phase`. For a trailing slice length
593    /// `elems`, the returned layout describes:
594    ///
595    /// ```text
596    /// self.size_base
597    ///     + round_up(size_phase + bytes + elems * elem_size, size_align)
598    /// ```
599    ///
600    /// Here, `elem_size` is the replacement element size passed to this
601    /// method. The returned layout preserves the physical slice offset
602    /// `self.offset` and the rounding alignment; advancing changes the size
603    /// calculation.
604    ///
605    /// Returns `None` if advancing cannot produce representable normalized
606    /// components. Even when those components fit, evaluating the rounded
607    /// object size may overflow for some or all trailing slice lengths.
608    #[inline(always)]
609    #[cfg_attr(kani, kani::ensures(|result| {
610        let expected_offset = self.size_offset().checked_add(bytes);
611        match result {
612            Some(advanced) => {
613                let align = self.size_rounding_align_and_phase.align();
614                expected_offset == Some(advanced.size_offset())
615                    && advanced.offset == self.offset
616                    && advanced.elem_size == elem_size
617                    && advanced.size_rounding_align_and_phase.align() == align
618                    && (advanced.size_base & (align.get() - 1)) == (self.size_base & (align.get() - 1))
619            }
620            None => expected_offset.is_none(),
621        }
622    }))]
623    const fn advance(self, bytes: usize, elem_size: usize) -> Option<Self> {
624        #[cfg(all(kani, kani_slow))]
625        #[kani::proof_for_contract(TrailingSliceLayout::advance)]
626        #[kani::solver(kissat)]
627        fn proof() {
628            kani::any::<TrailingSliceLayout>().advance(kani::any(), kani::any());
629        }
630
631        let (size_align, size_phase) = self.size_rounding_align_and_phase.components();
632
633        // Advancing replaces the phase inside `round_up` with `size_phase +
634        // bytes`. To normalize this sum, separate it into whole alignment units
635        // and a remainder. The whole units can move outside `round_up` into the
636        // base; the remainder becomes the new phase.
637        //
638        // Since `size_align` is a nonzero power of two, `align_mask` selects
639        // exactly the bits representing a remainder modulo `size_align`.
640        // Nonzero `size_align` also makes this subtraction representable.
641        #[allow(clippy::arithmetic_side_effects)]
642        let align_mask = size_align.get() - 1;
643
644        // The base can increase by at most `usize::MAX - self.size_base`. Since
645        // only whole alignment units move into the base, the largest
646        // transferable contribution is:
647        //
648        //   whole_capacity
649        //       = round_down(usize::MAX - self.size_base, size_align).
650        //
651        // A remainder up to `align_mask` stays in the phase and consumes no
652        // base capacity. Thus the largest advanced phase that can be split into
653        // representable normalized components is `phase_capacity =
654        // whole_capacity + align_mask`. Setting the low bits of the remaining
655        // base capacity computes exactly that sum. Its two terms have disjoint
656        // set bits, so it cannot overflow. Any larger sum would transfer more
657        // whole units than the base can hold.
658        #[allow(clippy::arithmetic_side_effects)]
659        let phase_capacity = (usize::MAX - self.size_base) | align_mask;
660
661        // The existing phase already occupies `size_phase` bytes of this
662        // capacity. Subtracting it gives the greatest permitted increment:
663        //
664        //   bytes <= max_advance
665        //       iff size_phase + bytes <= phase_capacity.
666        //
667        // Since `phase_capacity >= align_mask >= size_phase`, the subtraction
668        // cannot underflow.
669        #[allow(clippy::arithmetic_side_effects)]
670        let max_advance = phase_capacity - size_phase;
671        if bytes > max_advance {
672            return None;
673        }
674
675        // This is the fixed term inside rounding after the requested
676        // advancement, before normalization. The check above proves `size_phase
677        // + bytes <= phase_capacity <= usize::MAX`, so the addition is
678        // representable.
679        #[allow(clippy::arithmetic_side_effects)]
680        let advanced_phase = size_phase + bytes;
681
682        // Taking the remainder retains precisely the part that cannot move
683        // outside rounding as whole alignment units. In particular, `0 <=
684        // normalized_phase < size_align`, as required for the new rounding
685        // parameters.
686        let normalized_phase = advanced_phase & align_mask;
687
688        // Rounding down removes precisely the remainder retained in the
689        // phase, so `whole_bytes = advanced_phase - normalized_phase`.
690        let whole_bytes =
691            util::round_down_to_next_multiple_of_alignment(advanced_phase, size_align);
692
693        // Because `advanced_phase <= phase_capacity`, we have
694        // `whole_bytes <= whole_capacity <= usize::MAX - self.size_base`.
695        // Adding this transferred contribution to the base therefore fits.
696        #[allow(clippy::arithmetic_side_effects)]
697        let size_base = self.size_base + whole_bytes;
698
699        // To verify the returned formula, let `trailing_size = elems *
700        // elem_size` and evaluate the following over the nonnegative integers.
701        // Since `whole_bytes` is a multiple of `size_align`, it can move back
702        // inside `round_up` without changing the result:
703        //
704        //   size_base + round_up(normalized_phase + trailing_size, size_align)
705        //       = self.size_base
706        //           + round_up(whole_bytes + normalized_phase + trailing_size, size_align)
707        //       = self.size_base
708        //           + round_up(size_phase + bytes + trailing_size, size_align).
709        //
710        // This is the requested advanced formula. The replacement element size
711        // supplies its new coefficient of `elems`; `offset` retains the
712        // caller's field-placement description.
713        Some(Self {
714            offset: self.offset,
715            elem_size,
716            size_base,
717            size_rounding_align_and_phase: RoundingAlignAndPhase::new(size_align, normalized_phase),
718        })
719    }
720}
721
722impl SizeInfo {
723    /// Attempts to create a `SizeInfo` from `Self` in which `elem_size` is a
724    /// `NonZeroUsize`. If `elem_size` is 0, returns `None`.
725    #[allow(unused)]
726    #[cfg_attr(not(zerocopy_inline_always), inline)]
727    #[cfg_attr(zerocopy_inline_always, inline(always))]
728    #[cfg_attr(kani, kani::ensures(|result| match (*self, *result) {
729        (SizeInfo::Sized { size }, Some(SizeInfo::Sized { size: converted })) => {
730            size == converted
731        }
732        (SizeInfo::SliceDst(original), Some(SizeInfo::SliceDst(converted))) => {
733            converted.elem_size.get() == original.elem_size
734                && converted.offset == original.offset
735                && converted.size_base == original.size_base
736                && converted.size_rounding_align_and_phase == original.size_rounding_align_and_phase
737        }
738        (SizeInfo::SliceDst(original), None) => original.elem_size == 0,
739        _ => false,
740    }))]
741    const fn try_to_nonzero_elem_size(&self) -> Option<SizeInfo<NonZeroUsize>> {
742        #[cfg(kani)]
743        #[kani::proof_for_contract(SizeInfo::try_to_nonzero_elem_size)]
744        #[kani::solver(kissat)]
745        fn proof() {
746            kani::any::<SizeInfo>().try_to_nonzero_elem_size();
747        }
748
749        Some(match *self {
750            SizeInfo::Sized { size } => SizeInfo::Sized { size },
751            SizeInfo::SliceDst(TrailingSliceLayout {
752                offset,
753                elem_size,
754                size_base,
755                size_rounding_align_and_phase,
756            }) => {
757                if let Some(elem_size) = NonZeroUsize::new(elem_size) {
758                    SizeInfo::SliceDst(TrailingSliceLayout {
759                        offset,
760                        elem_size,
761                        size_base,
762                        size_rounding_align_and_phase,
763                    })
764                } else {
765                    return None;
766                }
767            }
768        })
769    }
770}
771
772/// Returns the largest number of `elem_size`-byte elements which fit in
773/// `bytes`, along with the number of bytes those elements occupy.
774#[cfg_attr(
775    kani,
776    kani::ensures(|&(elems, used)| {
777        (match elems.checked_mul(elem_size.get()) {
778            Some(product) => {
779                product == used && used <= bytes && bytes - used < elem_size.get()
780            }
781            None => false,
782        }) && used
783            .checked_add(elem_size.get())
784            .map(|next_used| next_used > bytes)
785            .unwrap_or(true)
786    })
787)]
788#[inline(always)]
789const fn max_elems_for_bytes(bytes: usize, elem_size: NonZeroUsize) -> (usize, usize) {
790    #[cfg(kani)]
791    #[kani::proof_for_contract(max_elems_for_bytes)]
792    #[kani::solver(kissat)]
793    fn proof() {
794        max_elems_for_bytes(kani::any(), kani::any());
795    }
796
797    #[allow(clippy::arithmetic_side_effects)]
798    let elems = bytes / elem_size.get();
799    let used = match elems.checked_mul(elem_size.get()) {
800        Some(used) => used,
801        None => const_unreachable!(),
802    };
803    (elems, used)
804}
805
806#[doc(hidden)]
807#[derive(Copy, Clone)]
808#[cfg_attr(test, derive(Debug))]
809#[allow(missing_debug_implementations)]
810pub enum CastType {
811    Prefix,
812    Suffix,
813}
814
815#[cfg_attr(test, derive(Debug))]
816#[cfg_attr(kani, derive(kani::Arbitrary))]
817pub(crate) enum MetadataCastError {
818    Alignment,
819    Size,
820}
821
822impl DstLayout {
823    /// The minimum possible alignment of a type.
824    const MIN_ALIGN: NonZeroUsize = match NonZeroUsize::new(1) {
825        Some(min_align) => min_align,
826        None => const_unreachable!(),
827    };
828
829    /// The maximum theoretic possible alignment of a type.
830    ///
831    /// For compatibility with future Rust versions, this is defined as the
832    /// maximum power-of-two that fits into a `usize`. See also
833    /// [`DstLayout::CURRENT_MAX_ALIGN`].
834    pub(crate) const THEORETICAL_MAX_ALIGN: NonZeroUsize =
835        match NonZeroUsize::new(1 << (POINTER_WIDTH_BITS - 1)) {
836            Some(max_align) => max_align,
837            None => const_unreachable!(),
838        };
839
840    /// The current, documented max alignment of a type \[1\].
841    ///
842    /// \[1\] Per <https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers>:
843    ///
844    ///   The alignment value must be a power of two from 1 up to
845    ///   2<sup>29</sup>.
846    #[cfg(not(kani))]
847    #[cfg(not(target_pointer_width = "16"))]
848    pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 29) {
849        Some(max_align) => max_align,
850        None => const_unreachable!(),
851    };
852
853    #[cfg(not(kani))]
854    #[cfg(target_pointer_width = "16")]
855    pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 15) {
856        Some(max_align) => max_align,
857        None => const_unreachable!(),
858    };
859
860    /// The maximum size of an allocation \[1\].
861    ///
862    /// \[1\] Per <https://doc.rust-lang.org/1.91.1/std/ptr/index.html#allocation>:
863    ///
864    ///   For any allocation with base `address`, `size`, and a set of `addresses`,
865    ///   the following are guaranteed: [..]
866    ///
867    ///   - `size <= isize::MAX`
868    ///
869    #[allow(clippy::as_conversions)]
870    pub(crate) const MAX_SIZE: usize = isize::MAX as usize;
871
872    /// Assumes that this layout lacks static shallow padding.
873    ///
874    /// # Panics
875    ///
876    /// This method does not panic.
877    ///
878    /// # Safety
879    ///
880    /// If `self` describes the size and alignment of type that lacks static
881    /// shallow padding, unsafe code may assume that the result of this method
882    /// accurately reflects the size, alignment, and lack of static shallow
883    /// padding of that type.
884    #[cfg_attr(kani, kani::ensures(|result| {
885        result.align == self.align && result.size_info == self.size_info
886            && result.statically_shallow_unpadded
887    }))]
888    const fn assume_shallow_unpadded(self) -> Self {
889        #[cfg(kani)]
890        #[kani::proof_for_contract(DstLayout::assume_shallow_unpadded)]
891        #[kani::solver(kissat)]
892        fn proof() {
893            kani::any::<DstLayout>().assume_shallow_unpadded();
894        }
895
896        Self { statically_shallow_unpadded: true, ..self }
897    }
898
899    /// Constructs a `DstLayout` for a zero-sized type with `repr_align`
900    /// alignment (or 1). If `repr_align` is provided, then it must be a power
901    /// of two.
902    ///
903    /// # Panics
904    ///
905    /// This function panics if the supplied `repr_align` is not a power of two.
906    ///
907    /// # Safety
908    ///
909    /// Unsafe code may assume that the contract of this function is satisfied.
910    #[doc(hidden)]
911    #[must_use]
912    #[inline]
913    #[cfg_attr(kani, kani::requires(repr_align.map_or(true, |align| align.is_power_of_two())))]
914    #[cfg_attr(kani, kani::ensures(|result| {
915        result.align == repr_align.unwrap_or(Self::MIN_ALIGN)
916            && result.size_info == SizeInfo::Sized { size: 0 }
917            && result.statically_shallow_unpadded
918    }))]
919    pub const fn new_zst(repr_align: Option<NonZeroUsize>) -> DstLayout {
920        #[cfg(kani)]
921        #[kani::proof_for_contract(DstLayout::new_zst)]
922        #[kani::solver(kissat)]
923        fn proof() {
924            let _ = DstLayout::new_zst(kani::any());
925        }
926
927        let align = match repr_align {
928            Some(align) => align,
929            None => Self::MIN_ALIGN,
930        };
931
932        const_assert!(align.get().is_power_of_two());
933
934        DstLayout {
935            align,
936            size_info: SizeInfo::Sized { size: 0 },
937            statically_shallow_unpadded: true,
938        }
939    }
940
941    /// Constructs a `DstLayout` which describes `T` and assumes `T` may contain
942    /// padding.
943    ///
944    /// # Safety
945    ///
946    /// Unsafe code may assume that `DstLayout` is the correct layout for `T`.
947    #[doc(hidden)]
948    #[must_use]
949    #[inline]
950    #[cfg_attr(kani, kani::ensures(|result| {
951        result.align.get() == mem::align_of::<T>()
952            && result.size_info == SizeInfo::Sized { size: mem::size_of::<T>() }
953            && result.statically_shallow_unpadded == false
954    }))]
955    pub const fn for_type<T>() -> DstLayout {
956        #[cfg(kani)]
957        #[kani::proof_for_contract(DstLayout::for_type::<()>)]
958        fn proof_zst() {
959            let _ = DstLayout::for_type::<()>();
960        }
961
962        #[cfg(kani)]
963        #[kani::proof_for_contract(DstLayout::for_type::<(u8, u64)>)]
964        fn proof_padded() {
965            let _ = DstLayout::for_type::<(u8, u64)>();
966        }
967
968        // SAFETY: `align` is correct by construction. `T: Sized`, and so it is
969        // sound to initialize `size_info` to `SizeInfo::Sized { size }`; the
970        // `size` field is also correct by construction. `unpadded` can safely
971        // default to `false`.
972        DstLayout {
973            align: match NonZeroUsize::new(mem::align_of::<T>()) {
974                Some(align) => align,
975                None => const_unreachable!(),
976            },
977            size_info: SizeInfo::Sized { size: mem::size_of::<T>() },
978            statically_shallow_unpadded: false,
979        }
980    }
981
982    /// Produces `T`'s size and alignment with static shallow padding assumed
983    /// absent.
984    ///
985    /// This method records the assumption without checking `T` for padding.
986    /// It can be used to ignore padding within a field when computing padding
987    /// introduced by its enclosing struct.
988    ///
989    /// # Safety
990    ///
991    /// Unsafe code may rely on the result's size and alignment being correct
992    /// for `T`. Relying on its recorded absence of static shallow padding
993    /// requires an independent justification that `T` lacks such padding.
994    #[doc(hidden)]
995    #[must_use]
996    #[inline]
997    #[cfg_attr(kani, kani::ensures(|result| {
998        result.align.get() == mem::align_of::<T>()
999            && result.size_info == SizeInfo::Sized { size: mem::size_of::<T>() }
1000            && result.statically_shallow_unpadded == true
1001    }))]
1002    pub const fn for_unpadded_type<T>() -> DstLayout {
1003        #[cfg(kani)]
1004        #[kani::proof_for_contract(DstLayout::for_unpadded_type::<()>)]
1005        fn proof_zst() {
1006            let _ = DstLayout::for_unpadded_type::<()>();
1007        }
1008
1009        #[cfg(kani)]
1010        #[kani::proof_for_contract(DstLayout::for_unpadded_type::<(u8, u64)>)]
1011        fn proof_padded() {
1012            let _ = DstLayout::for_unpadded_type::<(u8, u64)>();
1013        }
1014
1015        Self::for_type::<T>().assume_shallow_unpadded()
1016    }
1017
1018    /// Constructs a `DstLayout` which describes `[T]`.
1019    ///
1020    /// # Safety
1021    ///
1022    /// Unsafe code may assume that `DstLayout` is the correct layout for `[T]`.
1023    #[cfg_attr(kani, kani::ensures(|result| {
1024        result.align.get() == mem::align_of::<T>() && result.statically_shallow_unpadded
1025            && matches!(result.size_info, SizeInfo::SliceDst(trailing)
1026                if trailing.offset == 0 && trailing.size_base == 0
1027                    && trailing.elem_size == mem::size_of::<T>()
1028                    && trailing.size_rounding_align_and_phase.components() == (result.align, 0))
1029    }))]
1030    pub(crate) const fn for_slice<T>() -> DstLayout {
1031        #[cfg(kani)]
1032        #[kani::proof_for_contract(DstLayout::for_slice::<()>)]
1033        fn proof_zst() {
1034            let _ = DstLayout::for_slice::<()>();
1035        }
1036
1037        #[cfg(kani)]
1038        #[kani::proof_for_contract(DstLayout::for_slice::<(u8, u64)>)]
1039        fn proof_padded() {
1040            let _ = DstLayout::for_slice::<(u8, u64)>();
1041        }
1042
1043        let align = match NonZeroUsize::new(mem::align_of::<T>()) {
1044            Some(align) => align,
1045            None => const_unreachable!(),
1046        };
1047
1048        // SAFETY: The alignment of a slice is equal to the alignment of its
1049        // element type, and so `align` is initialized correctly.
1050        //
1051        // Since this is just a slice type, there is no offset between the
1052        // beginning of the type and the beginning of the slice, so it is
1053        // correct to set `offset: 0`. The `elem_size` is correct by
1054        // construction. Since `[T]` is a (degenerate case of a) slice DST, it
1055        // is correct to initialize `size_info` to `SizeInfo::SliceDst`.
1056        DstLayout {
1057            align,
1058            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
1059                offset: 0,
1060                elem_size: mem::size_of::<T>(),
1061                size_base: 0,
1062                size_rounding_align_and_phase: RoundingAlignAndPhase::new(align, 0),
1063            }),
1064            statically_shallow_unpadded: true,
1065        }
1066    }
1067
1068    /// Constructs a complete `DstLayout` reflecting a `repr(C)` struct with the
1069    /// given alignment modifiers and fields.
1070    ///
1071    /// This method cannot be used to match the layout of a record with the
1072    /// default representation, as that representation is mostly unspecified.
1073    ///
1074    /// # Safety
1075    ///
1076    /// For any definition of a `repr(C)` struct, if this method is invoked with
1077    /// alignment modifiers and fields corresponding to that definition, the
1078    /// resulting `DstLayout` will correctly encode the layout of that struct.
1079    ///
1080    /// This guarantee uses one explicit compatibility premise for generic
1081    /// indirection: while Rust accepts an instantiation which transitively
1082    /// places a `repr(align)` type inside a `repr(packed)` type, that
1083    /// instantiation follows the otherwise-applicable `repr(C)`,
1084    /// `repr(packed)`, and inner-type layout rules. The Reference prohibits the
1085    /// direct form, and Rust's continued acceptance and layout behavior are
1086    /// outside the algebra proved by this implementation. Re-audit if the
1087    /// compiler rejects such an instantiation or assigns it a different
1088    /// layout.
1089    ///
1090    /// We make no guarantees to the behavior of this method when it is invoked
1091    /// with arguments that cannot correspond to a valid `repr(C)` struct.
1092    #[must_use]
1093    #[inline]
1094    #[cfg_attr(kani, kani::requires(proofs::repr_c_layout(repr_align, repr_packed, fields).is_some()))]
1095    #[cfg_attr(kani, kani::ensures(|&result| {
1096        Some(result) == proofs::repr_c_layout(repr_align, repr_packed, fields)
1097    }))]
1098    pub const fn for_repr_c_struct(
1099        repr_align: Option<NonZeroUsize>,
1100        repr_packed: Option<NonZeroUsize>,
1101        fields: &[DstLayout],
1102    ) -> DstLayout {
1103        // This harness covers up to three fields, including an optional final
1104        // DST. Arbitrary-length composition is not established by this bound.
1105        #[cfg(all(kani, kani_slow))]
1106        #[kani::proof_for_contract(DstLayout::for_repr_c_struct)]
1107        #[kani::solver(kissat)]
1108        #[kani::unwind(4)]
1109        fn proof() {
1110            let fields: [DstLayout; 3] = kani::any();
1111            let len: usize = kani::any();
1112            kani::assume(len <= fields.len());
1113            let _ = DstLayout::for_repr_c_struct(kani::any(), kani::any(), &fields[..len]);
1114        }
1115
1116        let mut layout = DstLayout::new_zst(repr_align);
1117
1118        let mut i = 0;
1119        #[allow(clippy::arithmetic_side_effects)]
1120        while i < fields.len() {
1121            #[allow(clippy::indexing_slicing)]
1122            let field = fields[i];
1123            layout = layout.extend(field, repr_packed);
1124            i += 1;
1125        }
1126
1127        layout = layout.pad_to_align();
1128
1129        // SAFETY: `layout` accurately describes the layout of a `repr(C)`
1130        // struct with `repr_align` or `repr_packed` alignment modifications and
1131        // the given `fields`. The `layout` is constructed using a sequence of
1132        // invocations of `DstLayout::{new_zst,extend,pad_to_align}`. The
1133        // documentation of these items vows that invocations in this manner
1134        // will accurately describe a type, so long as:
1135        //
1136        //  - that type is `repr(C)`,
1137        //  - its fields are enumerated in the order they appear,
1138        //  - the presence of `repr_align` and `repr_packed` are correctly accounted for.
1139        //
1140        // We respect all three of these preconditions above.
1141        layout
1142    }
1143
1144    /// Like `Layout::extend`, this creates a layout that describes a record
1145    /// whose layout consists of `self` followed by `next` that includes the
1146    /// necessary inter-field padding, but not any trailing padding.
1147    ///
1148    /// In order to match the layout of a `#[repr(C)]` struct, this method
1149    /// should be invoked for each field in declaration order. To add trailing
1150    /// padding, call `DstLayout::pad_to_align` after extending the layout for
1151    /// all fields. If `self` corresponds to a type marked with
1152    /// `repr(packed(N))`, then `repr_packed` should be set to `Some(N)`,
1153    /// otherwise `None`.
1154    ///
1155    /// This method cannot be used to match the layout of a record with the
1156    /// default representation, as that representation is mostly unspecified.
1157    ///
1158    /// # Safety
1159    ///
1160    /// If a (potentially hypothetical) valid `repr(C)` Rust type begins with
1161    /// fields whose layout are `self`, and those fields are immediately
1162    /// followed by a field whose layout is `field`, then unsafe code may rely
1163    /// on `self.extend(field, repr_packed)` producing a layout that correctly
1164    /// encompasses those two components.
1165    ///
1166    /// We make no guarantees to the behavior of this method if these fragments
1167    /// cannot appear in a valid Rust type (e.g., the concatenation of the
1168    /// layouts would lead to a size larger than `isize::MAX`).
1169    #[doc(hidden)]
1170    #[must_use]
1171    #[inline]
1172    #[cfg_attr(kani, kani::requires(proofs::extended_layout(self, field, repr_packed).is_some()))]
1173    #[cfg_attr(kani, kani::ensures(|&result| {
1174        Some(result) == proofs::extended_layout(self, field, repr_packed)
1175    }))]
1176    pub const fn extend(self, field: DstLayout, repr_packed: Option<NonZeroUsize>) -> Self {
1177        #[cfg(kani)]
1178        #[kani::proof_for_contract(DstLayout::extend)]
1179        #[kani::solver(kissat)]
1180        fn proof() {
1181            let _ = kani::any::<DstLayout>().extend(kani::any(), kani::any());
1182        }
1183
1184        use util::{max, min, padding_needed_for};
1185
1186        // If `repr_packed` is `None`, there are no alignment constraints, and
1187        // the value can be defaulted to `THEORETICAL_MAX_ALIGN`.
1188        let max_align = match repr_packed {
1189            Some(max_align) => max_align,
1190            None => Self::THEORETICAL_MAX_ALIGN,
1191        };
1192
1193        const_assert!(max_align.get().is_power_of_two());
1194
1195        // We use Kani to prove that this method is robust to future increases
1196        // in Rust's maximum allowed alignment. However, if such a change ever
1197        // actually occurs, we'd like to be notified via assertion failures.
1198        #[cfg(not(kani))]
1199        {
1200            const_debug_assert!(self.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
1201            const_debug_assert!(field.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
1202            if let Some(repr_packed) = repr_packed {
1203                const_debug_assert!(repr_packed.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
1204            }
1205        }
1206
1207        // The field's alignment is clamped by `repr_packed` (i.e., the
1208        // `repr(packed(N))` attribute, if any) [1].
1209        //
1210        // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1211        //
1212        //   The alignments of each field, for the purpose of positioning
1213        //   fields, is the smaller of the specified alignment and the alignment
1214        //   of the field's type.
1215        let field_align = min(field.align, max_align);
1216
1217        // The struct's alignment is the maximum of its previous alignment and
1218        // `field_align`.
1219        let align = max(self.align, field_align);
1220
1221        let (interfield_padding, size_info) = match self.size_info {
1222            // If the layout is already a DST, we panic; DSTs cannot be extended
1223            // with additional fields.
1224            SizeInfo::SliceDst(..) => const_panic!("Cannot extend a DST with additional fields."),
1225
1226            SizeInfo::Sized { size: preceding_size } => {
1227                // Compute the minimum amount of inter-field padding needed to
1228                // satisfy the field's alignment, and offset of the trailing
1229                // field. [1]
1230                //
1231                // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1232                //
1233                //   Inter-field padding is guaranteed to be the minimum
1234                //   required in order to satisfy each field's (possibly
1235                //   altered) alignment.
1236                let padding = padding_needed_for(preceding_size, field_align);
1237
1238                // This will not panic (and is proven to not panic, with Kani)
1239                // if the layout components can correspond to a leading layout
1240                // fragment of a valid Rust type, but may panic otherwise (e.g.,
1241                // combining or aligning the components would create a size
1242                // exceeding `isize::MAX`).
1243                let offset = match preceding_size.checked_add(padding) {
1244                    Some(offset) => offset,
1245                    None => const_panic!("Adding padding to `self`'s size overflows `usize`."),
1246                };
1247
1248                (
1249                    padding,
1250                    match field.size_info {
1251                        SizeInfo::Sized { size: field_size } => {
1252                            // If the trailing field is sized, the resulting layout
1253                            // will be sized. Its size will be the sum of the
1254                            // preceding layout, the size of the new field, and the
1255                            // size of inter-field padding between the two.
1256                            //
1257                            // This will not panic (and is proven with Kani to not
1258                            // panic) if the layout components can correspond to a
1259                            // leading layout fragment of a valid Rust type, but may
1260                            // panic otherwise (e.g., combining or aligning the
1261                            // components would create a size exceeding
1262                            // `usize::MAX`).
1263                            let size = match offset.checked_add(field_size) {
1264                                Some(size) => size,
1265                                None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
1266                            };
1267                            SizeInfo::Sized { size }
1268                        }
1269                        SizeInfo::SliceDst(TrailingSliceLayout {
1270                            offset: trailing_offset,
1271                            elem_size,
1272                            size_base,
1273                            size_rounding_align_and_phase,
1274                        }) => {
1275                            // If the trailing field is dynamically sized, so too
1276                            // will the resulting layout. The offset of the trailing
1277                            // slice component is the sum of the offset of the
1278                            // trailing field and the trailing slice offset within
1279                            // that field.
1280                            //
1281                            // This will not panic (and is proven with Kani to not
1282                            // panic) if the layout components can correspond to a
1283                            // leading layout fragment of a valid Rust type, but may
1284                            // panic otherwise (e.g., combining or aligning the
1285                            // components would create a size exceeding
1286                            // `usize::MAX`).
1287                            let trailing_offset = match offset.checked_add(trailing_offset) {
1288                                Some(offset) => offset,
1289                                None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
1290                            };
1291
1292                            // Before normalization, the composite's size is:
1293                            //
1294                            //   offset + size_base
1295                            //       + round_up(size_phase + elems * elem_size, size_align)
1296                            //
1297                            // `offset` does not participate in the rounding,
1298                            // so it can be added directly to the normalized
1299                            // base.
1300                            let size_base = match offset.checked_add(size_base) {
1301                                Some(base) => base,
1302                                None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
1303                            };
1304
1305                            SizeInfo::SliceDst(TrailingSliceLayout {
1306                                offset: trailing_offset,
1307                                elem_size,
1308                                size_base,
1309                                size_rounding_align_and_phase,
1310                            })
1311                        }
1312                    },
1313                )
1314            }
1315        };
1316
1317        let statically_shallow_unpadded = self.statically_shallow_unpadded
1318            && field.statically_shallow_unpadded
1319            && interfield_padding == 0;
1320
1321        DstLayout { align, size_info, statically_shallow_unpadded }
1322    }
1323
1324    /// Like `Layout::pad_to_align`, this routine rounds the size of this layout
1325    /// up to the nearest multiple of this type's alignment. For DST layouts,
1326    /// this updates the runtime size formula to account for trailing padding.
1327    ///
1328    /// In order to match the layout of a `#[repr(C)]` struct, this method
1329    /// should be invoked after the invocations of [`DstLayout::extend`].
1330    ///
1331    /// This method cannot be used to match the layout of a record with the
1332    /// default representation, as that representation is mostly unspecified.
1333    ///
1334    /// # Safety
1335    ///
1336    /// If a (potentially hypothetical) valid `repr(C)` type begins with fields
1337    /// whose layout are `self` followed only by zero or more bytes of trailing
1338    /// padding (not included in `self`), then unsafe code may rely on
1339    /// `self.pad_to_align()` producing a layout that correctly
1340    /// encapsulates the layout of that type.
1341    ///
1342    /// We make no guarantees to the behavior of this method if `self` cannot
1343    /// appear in a valid Rust type (e.g., because the addition of trailing
1344    /// padding would lead to a size larger than `isize::MAX`).
1345    #[doc(hidden)]
1346    #[must_use]
1347    #[inline]
1348    #[cfg_attr(kani, kani::requires(proofs::padded_layout(self).is_some()))]
1349    #[cfg_attr(kani, kani::ensures(|&result| Some(result) == proofs::padded_layout(self)))]
1350    pub const fn pad_to_align(self) -> Self {
1351        #[cfg(all(kani, kani_slow))]
1352        #[kani::proof_for_contract(DstLayout::pad_to_align)]
1353        #[kani::solver(kissat)]
1354        fn proof() {
1355            let _ = kani::any::<DstLayout>().pad_to_align();
1356        }
1357
1358        use util::padding_needed_for;
1359
1360        let (static_padding, size_info) = match self.size_info {
1361            // For sized layouts, we add the minimum amount of trailing padding
1362            // needed to satisfy alignment.
1363            SizeInfo::Sized { size: unpadded_size } => {
1364                let padding = padding_needed_for(unpadded_size, self.align);
1365                let size = match unpadded_size.checked_add(padding) {
1366                    Some(size) => size,
1367                    None => const_panic!("Adding padding caused size to overflow `usize`."),
1368                };
1369                (padding, SizeInfo::Sized { size })
1370            }
1371            // For DST layouts, trailing padding depends on the length of the
1372            // trailing DST and is computed at runtime. Normalize the composed
1373            // rounding operation back into the representation documented on
1374            // `TrailingSliceLayout`.
1375            SizeInfo::SliceDst(mut trailing) => {
1376                let (size_align, size_phase) = trailing.size_rounding_align_and_phase.components();
1377                if size_align.get() < self.align.get() {
1378                    // For a trailing-slice byte count `trailing_size`, the
1379                    // input layout describes:
1380                    //
1381                    //   trailing.size_base
1382                    //       + round_up(size_phase + trailing_size, size_align)
1383                    //
1384                    // Here, `size_align < self.align`. Both alignments are
1385                    // powers of two, so rounding the complete input size to
1386                    // `self.align` is equivalent to:
1387                    //
1388                    //   rounded_base = round_up(trailing.size_base, size_align)
1389                    //   fixed_contribution = rounded_base + size_phase
1390                    //   new_phase = fixed_contribution % self.align
1391                    //   new_base = fixed_contribution - new_phase
1392                    //   new_base + round_up(new_phase + trailing_size, self.align)
1393                    //
1394                    // `prove_dst_layout_pad_to_align_dst_outer_alignment_transition`
1395                    // checks the exact field transition and preserved
1396                    // invariants. Its `_formula` companion proves this
1397                    // identity for every representable trailing byte count.
1398                    let base_padding = padding_needed_for(trailing.size_base, size_align);
1399                    let rounded_base = match trailing.size_base.checked_add(base_padding) {
1400                        Some(base) => base,
1401                        None => const_panic!("Adding padding caused size to overflow `usize`."),
1402                    };
1403                    let fixed_contribution = match rounded_base.checked_add(size_phase) {
1404                        Some(bytes) => bytes,
1405                        None => const_panic!("Adding padding caused size to overflow `usize`."),
1406                    };
1407                    #[allow(clippy::arithmetic_side_effects)]
1408                    let phase = fixed_contribution & (self.align.get() - 1);
1409                    // Rounding down removes `phase`, the fixed contribution
1410                    // modulo `self.align`, leaving the new base.
1411                    let size_base = util::round_down_to_next_multiple_of_alignment(
1412                        fixed_contribution,
1413                        self.align,
1414                    );
1415                    trailing.size_base = size_base;
1416                    trailing.size_rounding_align_and_phase =
1417                        RoundingAlignAndPhase::new(self.align, phase);
1418                } else {
1419                    // `size_align` is a multiple of `self.align`, so
1420                    // the inner rounded size is already aligned to
1421                    // `self.align`. Only `size_base` needs to be rounded.
1422                    // `prove_dst_layout_pad_to_align_dst_inner_rounding_transition`
1423                    // checks the exact field transition and preserved
1424                    // invariants. Its `_formula` companion proves size
1425                    // equivalence for every representable trailing byte count.
1426                    let padding = padding_needed_for(trailing.size_base, self.align);
1427                    trailing.size_base = match trailing.size_base.checked_add(padding) {
1428                        Some(base) => base,
1429                        None => const_panic!("Adding padding caused size to overflow `usize`."),
1430                    };
1431                }
1432
1433                (0, SizeInfo::SliceDst(trailing))
1434            }
1435        };
1436
1437        let statically_shallow_unpadded = self.statically_shallow_unpadded && static_padding == 0;
1438
1439        DstLayout { align: self.align, size_info, statically_shallow_unpadded }
1440    }
1441
1442    /// Produces `false` when this layout is marked as having no static shallow
1443    /// padding, and `true` otherwise.
1444    ///
1445    /// A `true` result is conservative: it does not establish that the type
1446    /// contains padding. For example, this method produces `true` for
1447    /// `DstLayout::for_type::<u8>()`, since `for_type` does not record whether
1448    /// static shallow padding is absent.
1449    #[must_use]
1450    #[inline(always)]
1451    #[cfg_attr(kani, kani::ensures(|&padding| padding == !self.statically_shallow_unpadded))]
1452    pub const fn requires_static_padding(self) -> bool {
1453        #[cfg(kani)]
1454        #[kani::proof_for_contract(DstLayout::requires_static_padding)]
1455        #[kani::solver(kissat)]
1456        fn proof() {
1457            let _ = kani::any::<DstLayout>().requires_static_padding();
1458        }
1459
1460        !self.statically_shallow_unpadded
1461    }
1462
1463    /// Produces `false` only if every valid metadata describes an instance
1464    /// which requires no dynamic trailing padding.
1465    ///
1466    /// A `true` result conservatively selects the dynamic-padding path; it does
1467    /// not guarantee that any valid metadata actually requires padding.
1468    #[must_use]
1469    #[inline(always)]
1470    #[cfg_attr(kani, kani::ensures(|&padding| match self.size_info {
1471        SizeInfo::Sized { .. } => !padding,
1472        SizeInfo::SliceDst(trailing) => padding == (
1473            proofs::slice_dst_size_for_trailing_bytes(trailing, 0) != Some(trailing.offset)
1474                || trailing.elem_size % trailing.size_rounding_align_and_phase.align().get() != 0
1475        ),
1476    }))]
1477    pub const fn requires_dynamic_padding(self) -> bool {
1478        #[cfg(all(kani, kani_slow))]
1479        #[kani::proof_for_contract(DstLayout::requires_dynamic_padding)]
1480        #[kani::solver(kissat)]
1481        fn proof() {
1482            let _ = kani::any::<DstLayout>().requires_dynamic_padding();
1483        }
1484
1485        match self.size_info {
1486            SizeInfo::Sized { .. } => false,
1487            SizeInfo::SliceDst(trailing_slice_layout) => {
1488                // SAFETY: `proofs::prove_requires_dynamic_padding` formally
1489                // proves the safety-critical direction: when this predicate
1490                // returns false, no valid metadata requires dynamic padding.
1491                match trailing_slice_layout.size_for_elems(0) {
1492                    Some(initial_size) => {
1493                        #[allow(clippy::arithmetic_side_effects)]
1494                        let has_static_stride = trailing_slice_layout.elem_size
1495                            % trailing_slice_layout.size_rounding_align_and_phase.align().get()
1496                            == 0;
1497                        initial_size != trailing_slice_layout.offset || !has_static_stride
1498                    }
1499                    None => true,
1500                }
1501            }
1502        }
1503    }
1504
1505    /// Returns the largest trailing slice length whose object size is exactly
1506    /// `size`.
1507    ///
1508    /// Returns `None` if there is no such length, if this describes a sized
1509    /// type, or if the trailing slice element is zero-sized.
1510    #[inline(always)]
1511    #[cfg_attr(kani, kani::ensures(|&result| {
1512        result == match self.size_info {
1513            SizeInfo::SliceDst(trailing) if trailing.elem_size != 0 => {
1514                proofs::max_trailing_bytes(trailing, size)
1515                    .map(|bytes| bytes / trailing.elem_size)
1516                    .filter(|&elems| trailing.size_for_elems(elems) == Some(size))
1517            }
1518            _ => None,
1519        }
1520    }))]
1521    const fn metadata_for_exact_size(&self, size: usize) -> Option<usize> {
1522        #[cfg(all(kani, kani_slow))]
1523        #[kani::proof_for_contract(DstLayout::metadata_for_exact_size)]
1524        #[kani::solver(kissat)]
1525        fn proof() {
1526            kani::any::<DstLayout>().metadata_for_exact_size(kani::any());
1527        }
1528
1529        match self.size_info {
1530            SizeInfo::Sized { .. }
1531            | SizeInfo::SliceDst(TrailingSliceLayout { elem_size: 0, .. }) => None,
1532            SizeInfo::SliceDst(_) => {
1533                // Address zero is aligned to every supported alignment, and
1534                // `0 + size` cannot overflow. The validator returns the
1535                // largest metadata whose object fits in `size`; accepting it
1536                // only when the resulting object consumes all `size` bytes
1537                // turns that bounded query into an exact-size query.
1538                match self.validate_cast_and_convert_metadata(0, size, CastType::Prefix) {
1539                    Ok((elems, object_size)) if object_size == size => Some(elems),
1540                    _ => None,
1541                }
1542            }
1543        }
1544    }
1545
1546    /// Given a completed type layout, produces metadata and a split index for
1547    /// a prefix or suffix cast that satisfies the type's size and alignment
1548    /// requirements.
1549    ///
1550    /// `addr` and `bytes_len` describe the address and size of the source
1551    /// memory region, and `cast_type` selects its prefix or suffix.
1552    ///
1553    /// If the cast is valid, `validate_cast_and_convert_metadata` returns
1554    /// `Ok((elems, split_at))`. For a dynamically-sized type, `elems` is the
1555    /// maximum number of trailing slice elements for which the cast is valid.
1556    /// For sized types, `elems` is meaningless and should be ignored.
1557    /// `split_at` is the index at which to split the memory region so that the
1558    /// prefix (suffix) contains the result of the cast and the remaining
1559    /// suffix (prefix) contains the leftover bytes.
1560    ///
1561    /// There are three conditions under which a cast can fail:
1562    ///
1563    /// - The smallest possible value for the type is larger than the provided
1564    ///   memory region
1565    /// - A prefix cast is requested, and `addr` does not satisfy `self`'s
1566    ///   alignment requirement
1567    /// - A suffix cast is requested, and `addr + bytes_len` does not satisfy
1568    ///   `self`'s alignment requirement (as a consequence, since all instances
1569    ///   of the type are a multiple of its alignment, no size for the type will
1570    ///   result in a starting address which is properly aligned)
1571    ///
1572    /// # Safety
1573    ///
1574    /// When `self` accurately describes a completed type layout, including
1575    /// trailing padding, and `addr + bytes_len` fits in `usize`, callers may
1576    /// rely on the following guarantees for `Ok((elems, split_at))`:
1577    ///
1578    /// - A pointer to the type (for dynamically sized types, this includes
1579    ///   `elems` as its pointer metadata) describes an object of size `size <=
1580    ///   bytes_len`
1581    /// - If this is a prefix cast:
1582    ///   - `addr` satisfies `self`'s alignment
1583    ///   - `size == split_at`
1584    /// - If this is a suffix cast:
1585    ///   - `split_at == bytes_len - size`
1586    ///   - `addr + split_at` satisfies `self`'s alignment
1587    ///
1588    /// These guarantees require the completed-layout premise; `DstLayout`
1589    /// also permits unfinished layout fragments and arbitrary field values.
1590    /// In particular, suffix alignment relies on every object size being a
1591    /// multiple of the type's alignment.
1592    ///
1593    /// Note that this method does *not* ensure that a pointer constructed from
1594    /// its return values will be a valid pointer. In particular, this method
1595    /// does not reason about `isize` overflow, which is a requirement of many
1596    /// Rust pointer APIs, and may at some point be determined to be a validity
1597    /// invariant of pointer types themselves. This should never be a problem so
1598    /// long as the arguments to this method are derived from a known-valid
1599    /// pointer (e.g., one derived from a safe Rust reference), but it is
1600    /// nonetheless the caller's responsibility to justify that pointer
1601    /// arithmetic will not overflow based on a safety argument *other than* the
1602    /// mere fact that this method returned successfully.
1603    ///
1604    /// # Panics
1605    ///
1606    /// `validate_cast_and_convert_metadata` will panic if `self` describes a
1607    /// DST whose trailing slice element is zero-sized.
1608    ///
1609    /// If `addr + bytes_len` overflows `usize`,
1610    /// `validate_cast_and_convert_metadata` may panic, or it may return
1611    /// incorrect results. No guarantees are made about when
1612    /// `validate_cast_and_convert_metadata` will panic. The caller should not
1613    /// rely on `validate_cast_and_convert_metadata` panicking in any particular
1614    /// condition, even if `debug_assertions` are enabled.
1615    #[allow(unused)]
1616    #[inline(always)]
1617    #[cfg_attr(kani, kani::requires(addr.checked_add(bytes_len).is_some()))]
1618    #[cfg_attr(kani, kani::requires(!matches!(self.size_info,
1619        SizeInfo::SliceDst(TrailingSliceLayout { elem_size: 0, .. }))))]
1620    #[cfg_attr(kani, kani::ensures(|result| {
1621        let endpoint = match cast_type {
1622            CastType::Prefix => addr,
1623            CastType::Suffix => addr + bytes_len,
1624        };
1625        if endpoint % self.align.get() != 0 {
1626            return matches!(result, Err(MetadataCastError::Alignment));
1627        }
1628        let candidate = match self.size_info {
1629            SizeInfo::Sized { size } => {
1630                if size <= bytes_len { Some((0, size)) } else { None }
1631            }
1632            SizeInfo::SliceDst(trailing) => {
1633                proofs::max_trailing_bytes(trailing, bytes_len).and_then(|bytes| {
1634                    let elems = bytes / trailing.elem_size;
1635                    trailing.size_for_elems(elems).map(|size| (elems, size))
1636                })
1637            }
1638        };
1639        match (result, candidate) {
1640            (Ok((elems, split_at)), Some((expected_elems, size))) => {
1641                *elems == expected_elems && *split_at == match cast_type {
1642                    CastType::Prefix => size,
1643                    CastType::Suffix => bytes_len - size,
1644                }
1645            }
1646            (Err(MetadataCastError::Size), None) => true,
1647            _ => false,
1648        }
1649    }))]
1650    pub(crate) const fn validate_cast_and_convert_metadata(
1651        &self,
1652        addr: usize,
1653        bytes_len: usize,
1654        cast_type: CastType,
1655    ) -> Result<(usize, usize), MetadataCastError> {
1656        #[cfg(all(kani, kani_slow))]
1657        #[kani::proof_for_contract(DstLayout::validate_cast_and_convert_metadata)]
1658        #[kani::solver(kissat)]
1659        fn proof() {
1660            let cast_type = if kani::any() { CastType::Prefix } else { CastType::Suffix };
1661            let _ = kani::any::<DstLayout>().validate_cast_and_convert_metadata(
1662                kani::any(),
1663                kani::any(),
1664                cast_type,
1665            );
1666        }
1667
1668        // `debug_assert!`, but with `#[allow(clippy::arithmetic_side_effects)]`.
1669        macro_rules! __const_debug_assert {
1670            ($e:expr $(, $msg:expr)?) => {
1671                const_debug_assert!({
1672                    #[allow(clippy::arithmetic_side_effects)]
1673                    let e = $e;
1674                    e
1675                } $(, $msg)?);
1676            };
1677        }
1678
1679        // Note that, in practice, `self` is always a compile-time constant. We
1680        // do this check earlier than needed to ensure that we always panic as a
1681        // result of bugs in the program (such as calling this function on an
1682        // invalid type) instead of allowing this panic to be hidden if the cast
1683        // would have failed anyway for runtime reasons (such as a too-small
1684        // memory region).
1685        //
1686        // FIXME(#67): Once our MSRV is 1.65, use let-else:
1687        // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
1688        let size_info = match self.size_info.try_to_nonzero_elem_size() {
1689            Some(size_info) => size_info,
1690            None => const_panic!("attempted to cast to slice type with zero-sized element"),
1691        };
1692
1693        // Precondition
1694        __const_debug_assert!(
1695            addr.checked_add(bytes_len).is_some(),
1696            "`addr` + `bytes_len` > usize::MAX"
1697        );
1698
1699        // Alignment checks go in their own block to avoid introducing variables
1700        // into the top-level scope.
1701        {
1702            // We check alignment for `addr` (for prefix casts) or `addr +
1703            // bytes_len` (for suffix casts). For a prefix cast, the correctness
1704            // of this check is trivial - `addr` is the address the object will
1705            // live at.
1706            //
1707            // Under the documented completed-layout premise, all valid sizes
1708            // for the type are multiples of its alignment. Thus, a
1709            // validly-sized instance which lives at a validly-aligned address
1710            // must also end at a validly-aligned address. Thus, if the end
1711            // address for a suffix cast (`addr + bytes_len`) is not aligned,
1712            // then no valid start address will be aligned either.
1713            let offset = match cast_type {
1714                CastType::Prefix => 0,
1715                CastType::Suffix => bytes_len,
1716            };
1717
1718            // Addition is guaranteed not to overflow because `offset <=
1719            // bytes_len`, and `addr + bytes_len <= usize::MAX` is a
1720            // precondition of this method. Modulus is guaranteed not to divide
1721            // by zero because `self.align` is non-zero.
1722            #[allow(clippy::arithmetic_side_effects)]
1723            if (addr + offset) % self.align.get() != 0 {
1724                return Err(MetadataCastError::Alignment);
1725            }
1726        }
1727
1728        let (elems, self_bytes) = match size_info {
1729            SizeInfo::Sized { size } => {
1730                if size > bytes_len {
1731                    return Err(MetadataCastError::Size);
1732                }
1733                (0, size)
1734            }
1735            SizeInfo::SliceDst(trailing) => {
1736                let max_slice_bytes = match trailing.max_trailing_bytes(bytes_len) {
1737                    Some(bytes) => bytes,
1738                    None => return Err(MetadataCastError::Size),
1739                };
1740                // Calculate the number of elements that fit in
1741                // `max_slice_bytes`. Any remaining input bytes stay outside
1742                // the selected object unless the object's own rounding
1743                // operation consumes them as padding.
1744                //
1745                // Guaranteed not to divide by zero: `elem_size` is non-zero.
1746                let (elems, _) = max_elems_for_bytes(max_slice_bytes, trailing.elem_size);
1747                // The remainder is `max_slice_bytes - elems * elem_size`.
1748                // Writing it as a remainder exposes its bound to the
1749                // optimizer: when `elem_size <= size_align`, rounding the
1750                // remainder down to `size_align` always gives zero.
1751                let size_align = trailing.size_rounding_align_and_phase.align();
1752                // `size_phase + max_slice_bytes == max_rounded_bytes`, an
1753                // alignment multiple. The selected tail removes `unused_bytes`
1754                // from this boundary; rounding back up removes only whole
1755                // alignment units. Thus its normalized rounded size is:
1756                //
1757                //   round_up(size_phase + elems * elem_size, size_align)
1758                //       = max_rounded_bytes - round_down(unused_bytes, size_align)
1759                //
1760                // `prove_slice_dst_validator_selected_size_fits` checks this
1761                // identity and its intermediate bounds, using the relations
1762                // in `prove_max_trailing_bytes_components`. The capacity is
1763                // exact by `prove_max_trailing_bytes`; the element helper's
1764                // contract establishes the largest whole-element count.
1765                #[allow(clippy::arithmetic_side_effects)]
1766                let unused_bytes = max_slice_bytes % trailing.elem_size.get();
1767                let unused_aligned_bytes =
1768                    util::round_down_to_next_multiple_of_alignment(unused_bytes, size_align);
1769                #[allow(clippy::arithmetic_side_effects)]
1770                let max_rounded_bytes = util::round_down_to_next_multiple_of_alignment(
1771                    bytes_len - trailing.size_base,
1772                    size_align,
1773                );
1774                // `unused_aligned_bytes <= unused_bytes <= max_slice_bytes
1775                // <= max_rounded_bytes`, and `size_base + max_rounded_bytes
1776                // <= bytes_len`, so neither operation can overflow.
1777                #[allow(clippy::arithmetic_side_effects)]
1778                let self_bytes = trailing.size_base + (max_rounded_bytes - unused_aligned_bytes);
1779                (elems, self_bytes)
1780            }
1781        };
1782
1783        __const_debug_assert!(self_bytes <= bytes_len);
1784
1785        let split_at = match cast_type {
1786            CastType::Prefix => self_bytes,
1787            // Guaranteed not to underflow:
1788            // - In the `Sized` branch, only returns `size` if `size <=
1789            //   bytes_len`.
1790            // - In the `SliceDst` branch, calculates `self_bytes <=
1791            //   bytes_len`.
1792            #[allow(clippy::arithmetic_side_effects)]
1793            CastType::Suffix => bytes_len - self_bytes,
1794        };
1795
1796        Ok((elems, split_at))
1797    }
1798}
1799
1800/// Computes the affine destination-metadata map.
1801///
1802/// # Safety
1803///
1804/// `base + metadata * multiple`, evaluated over the mathematical nonnegative
1805/// integers, must not exceed `usize::MAX`.
1806#[inline(always)]
1807#[allow(unstable_name_collisions)]
1808#[cfg_attr(kani, kani::requires(metadata.checked_mul(multiple)
1809    .and_then(|scaled| base.checked_add(scaled)).is_some()))]
1810#[cfg_attr(kani, kani::ensures(|&result| {
1811    metadata.checked_mul(multiple).and_then(|scaled| base.checked_add(scaled)) == Some(result)
1812}))]
1813unsafe fn add_scaled_metadata(base: usize, metadata: usize, multiple: usize) -> usize {
1814    // The call-site safety argument establishes that both operations are
1815    // representable for valid source metadata. This harness verifies the
1816    // production implementation selected by Kani's modern rustc against
1817    // checked arithmetic under precisely that precondition. The distributive
1818    // natural-number argument which establishes the precondition is kept
1819    // beside the unsafe pointer projection where it can be reviewed with the
1820    // layout premises that it consumes.
1821    #[cfg(kani)]
1822    #[kani::proof_for_contract(add_scaled_metadata)]
1823    #[kani::solver(kissat)]
1824    fn proof() {
1825        let base: usize = kani::any();
1826        let metadata: usize = kani::any();
1827        let multiple: usize = kani::any();
1828        let Some(scaled) = metadata.checked_mul(multiple) else {
1829            kani::assume(false);
1830            loop {}
1831        };
1832        let Some(expected) = base.checked_add(scaled) else {
1833            kani::assume(false);
1834            loop {}
1835        };
1836
1837        // SAFETY: The checked operations above returned `Some`, so the
1838        // operation is representable in `usize`. The standard-library
1839        // contracts say that this rules out undefined behavior for both
1840        // unchecked operations [1][2].
1841        //
1842        // [1] Per https://doc.rust-lang.org/1.93.1/std/primitive.usize.html#method.unchecked_mul:
1843        //
1844        //   This results in undefined behavior [..] when `checked_mul` would
1845        //   return `None`.
1846        //
1847        // [2] Per https://doc.rust-lang.org/1.93.1/std/primitive.usize.html#method.unchecked_add:
1848        //
1849        //   This results in undefined behavior [..] when `checked_add` would
1850        //   return `None`.
1851        assert_eq!(unsafe { add_scaled_metadata(base, metadata, multiple) }, expected);
1852    }
1853
1854    #[allow(unused_imports)]
1855    use crate::util::polyfills::*;
1856
1857    // SAFETY: The caller promises that `base + metadata * multiple` is
1858    // representable over the nonnegative integers. Thus `metadata * multiple`
1859    // is representable too. On toolchains with inherent unchecked arithmetic,
1860    // this is exactly the condition under which `usize::unchecked_mul` does
1861    // not have undefined behavior [1]. On older supported toolchains, method
1862    // resolution selects `polyfills::NumExt::unchecked_mul`, whose safety
1863    // contract requires the same no-overflow condition. Kani checks the two
1864    // implementations in separate harnesses.
1865    //
1866    // [1] Per https://doc.rust-lang.org/1.93.1/std/primitive.usize.html#method.unchecked_mul:
1867    //
1868    //   This results in undefined behavior when `self * rhs > usize::MAX` or
1869    //   `self * rhs < usize::MIN`, i.e. when `checked_mul` would return `None`.
1870    //
1871    let scaled = unsafe { metadata.unchecked_mul(multiple) };
1872
1873    // SAFETY: The caller promises that `base + metadata * multiple` is
1874    // representable in `usize`, and `scaled == metadata * multiple`. On
1875    // toolchains with inherent unchecked arithmetic, that is exactly the
1876    // condition under which `usize::unchecked_add` does not have undefined
1877    // behavior [1]. On older supported toolchains, method resolution selects
1878    // `polyfills::NumExt::unchecked_add`, whose safety contract requires the
1879    // same no-overflow condition. Kani checks the two implementations in
1880    // separate harnesses.
1881    //
1882    // [1] Per https://doc.rust-lang.org/1.93.1/std/primitive.usize.html#method.unchecked_add:
1883    //
1884    //   This results in undefined behavior when `self + rhs > usize::MAX` or
1885    //   `self + rhs < usize::MIN`, i.e. when `checked_add` would return `None`.
1886    unsafe { base.unchecked_add(scaled) }
1887}
1888
1889pub(crate) use cast_from::CastFrom;
1890mod cast_from {
1891    use crate::*;
1892
1893    pub(crate) struct CastFrom<Dst: ?Sized> {
1894        _never: core::convert::Infallible,
1895        _marker: PhantomData<Dst>,
1896    }
1897
1898    // SAFETY: The implementation of `Project::project` preserves the address
1899    // of the referent – it only modifies pointer metadata.
1900    unsafe impl<Src, Dst> crate::pointer::cast::Cast<Src, Dst> for CastFrom<Dst>
1901    where
1902        Src: KnownLayout + ?Sized,
1903        Dst: KnownLayout + ?Sized,
1904    {
1905    }
1906
1907    // SAFETY: The implementation of `Project::project` preserves the size of
1908    // the referent (see inline comments for a more detailed proof of this).
1909    unsafe impl<Src, Dst> crate::pointer::cast::CastExact<Src, Dst> for CastFrom<Dst>
1910    where
1911        Src: KnownLayout + ?Sized,
1912        Dst: KnownLayout + ?Sized,
1913    {
1914    }
1915
1916    // SAFETY: `project` produces a pointer which refers to the same referent
1917    // bytes as its input, or to a subset of them (see inline comments for a
1918    // more detailed proof of this). It does this using provenance-preserving
1919    // operations.
1920    unsafe impl<Src, Dst> crate::pointer::cast::Project<Src, Dst> for CastFrom<Dst>
1921    where
1922        Src: KnownLayout + ?Sized,
1923        Dst: KnownLayout + ?Sized,
1924    {
1925        /// # PME
1926        ///
1927        /// Generates a post-monomorphization error if it is not possible to
1928        /// implement soundly.
1929        //
1930        // FIXME(#1817): Support Sized->Unsized and Unsized->Sized casts
1931        fn project(src: PtrInner<'_, Src>) -> *mut Dst {
1932            /// The parameters required in order to perform a pointer cast from
1933            /// `Src` to `Dst`.
1934            ///
1935            /// These are a compile-time function of the layouts of `Src`
1936            /// and `Dst`.
1937            ///
1938            /// # Safety
1939            ///
1940            /// `Src`'s alignment must not be smaller than `Dst`'s alignment.
1941            struct CastParams<Src: ?Sized, Dst: ?Sized> {
1942                inner: CastParamsInner,
1943                _src: PhantomData<Src>,
1944                _dst: PhantomData<Dst>,
1945            }
1946
1947            #[derive(Copy, Clone)]
1948            enum CastParamsInner {
1949                // At compile time (specifically, post-monomorphization time),
1950                // we need to compute two things:
1951                // - Whether, given *any* `*Src`, it is possible to construct a
1952                //   `*Dst` which addresses the same number of bytes (ie,
1953                //   whether, for any `Src` pointer metadata, there exists `Dst`
1954                //   pointer metadata that addresses the same number of bytes)
1955                // - If this is possible, any information necessary to perform
1956                //   the `Src`->`Dst` metadata conversion at runtime.
1957                //
1958                // For slice DSTs, destination metadata is an affine function
1959                // of source metadata:
1960                //
1961                //   dst_meta = offset_delta_elems + src_meta * elem_multiple
1962                //
1963                // `elem_multiple` scales the destination's trailing element
1964                // size to the source's. `offset_delta_elems` selects a
1965                // destination size equal to the source's zero-element size.
1966                // We then prove that the two complete rounded size formulas
1967                // produce the same sequence for all metadata. This is
1968                // necessary because a packed outer DST can retain a nested
1969                // field's rounding operation; comparing only physical
1970                // trailing-slice offsets is not sufficient.
1971                /// The parameters required in order to perform an
1972                /// unsized-to-unsized pointer cast from `Src` to `Dst` as
1973                /// described above.
1974                ///
1975                /// # Safety
1976                ///
1977                /// `Src` and `Dst` must both be slice DSTs.
1978                ///
1979                /// `offset_delta_elems` and `elem_multiple` must be valid as
1980                /// described above.
1981                UnsizedToUnsized { offset_delta_elems: usize, elem_multiple: usize },
1982
1983                /// The metadata of a `Dst` which has the same size as `Src:
1984                /// Sized`.
1985                ///
1986                /// # Safety
1987                ///
1988                /// `Src: Sized` and `Dst` must be a slice DST.
1989                ///
1990                /// A raw `Dst` pointer with metadata `dst_meta` must address
1991                /// `size_of::<Src>()` bytes.
1992                SizedToUnsized { dst_meta: usize },
1993
1994                /// The metadata of a `Dst` which has the same size as `Src:
1995                /// Sized`.
1996                ///
1997                /// # Safety
1998                ///
1999                /// `Src` and `Dst` must both be `Sized` and `size_of::<Src>()
2000                /// == size_of::<Dst>()`.
2001                SizedToSized,
2002            }
2003
2004            impl<Src: ?Sized, Dst: ?Sized> Copy for CastParams<Src, Dst> {}
2005            impl<Src: ?Sized, Dst: ?Sized> Clone for CastParams<Src, Dst> {
2006                fn clone(&self) -> Self {
2007                    *self
2008                }
2009            }
2010
2011            impl<Src: ?Sized, Dst: ?Sized> CastParams<Src, Dst> {
2012                /// Given a nonzero `dst.elem_size` that exactly divides
2013                /// `src.elem_size`, produces `true` only when the following
2014                /// metadata map preserves the complete object size for every
2015                /// source element count `src_meta`:
2016                ///
2017                /// ```text
2018                /// dst_meta = dst_base + src_meta * (src.elem_size / dst.elem_size)
2019                /// ```
2020                ///
2021                /// This recognizes sufficient conditions for equality. A
2022                /// `false` result also includes equivalent size sequences
2023                /// that this test cannot recognize, or an adjustment whose
2024                /// normalized components cannot be represented in `usize`.
2025                const fn size_sequences_match(
2026                    src: TrailingSliceLayout,
2027                    dst: TrailingSliceLayout,
2028                    dst_base: usize,
2029                ) -> bool {
2030                    let base_bytes = match dst_base.checked_mul(dst.elem_size) {
2031                        Some(bytes) => bytes,
2032                        None => return false,
2033                    };
2034                    let shifted_dst = match dst.advance(base_bytes, src.elem_size) {
2035                        Some(layout) => layout,
2036                        None => return false,
2037                    };
2038                    src.has_same_size_sequence(shifted_dst)
2039                }
2040
2041                /// Given the complete layouts of `Src` and `Dst`, produces
2042                /// parameters for a cast that preserves object size.
2043                ///
2044                /// A `Some` result establishes that `Src`'s alignment is at
2045                /// least `Dst`'s and that the metadata map preserves size for
2046                /// every valid source metadata value.
2047                ///
2048                /// Supports casts between sized types of equal size, from a
2049                /// sized type to a slice DST, and between slice DSTs. A slice
2050                /// DST destination must have nonzero-sized elements; between
2051                /// slice DSTs, its element size must exactly divide the
2052                /// source's nonzero element size.
2053                ///
2054                /// Produces `None` for unsupported casts or when this method
2055                /// cannot establish a size-preserving metadata map. Rejection
2056                /// does not establish that no such map exists.
2057                const fn try_compute(
2058                    src_layout: &DstLayout,
2059                    dst_layout: &DstLayout,
2060                ) -> Option<CastParams<Src, Dst>> {
2061                    if src_layout.align.get() < dst_layout.align.get() {
2062                        return None;
2063                    }
2064
2065                    let inner = match (src_layout.size_info, dst_layout.size_info) {
2066                        (
2067                            SizeInfo::Sized { size: src_size },
2068                            SizeInfo::Sized { size: dst_size },
2069                        ) => {
2070                            if src_size != dst_size {
2071                                return None;
2072                            }
2073
2074                            // SAFETY: We checked above that `src_size ==
2075                            // dst_size`.
2076                            CastParamsInner::SizedToSized
2077                        }
2078                        (SizeInfo::Sized { size: src_size }, SizeInfo::SliceDst(_)) => {
2079                            let dst_meta = match dst_layout.metadata_for_exact_size(src_size) {
2080                                Some(meta) => meta,
2081                                None => return None,
2082                            };
2083
2084                            // SAFETY: The preceding math ensures that a `Dst`
2085                            // with `dst_meta` addresses `src_size` bytes.
2086                            CastParamsInner::SizedToUnsized { dst_meta }
2087                        }
2088                        (SizeInfo::SliceDst(src), SizeInfo::SliceDst(dst)) => {
2089                            let dst_elem_size = if let Some(e) = NonZeroUsize::new(dst.elem_size) {
2090                                e
2091                            } else {
2092                                return None;
2093                            };
2094
2095                            if src.elem_size < dst.elem_size {
2096                                return None;
2097                            }
2098
2099                            let (elem_multiple, described_src_elem_size) =
2100                                super::max_elems_for_bytes(src.elem_size, dst_elem_size);
2101                            if described_src_elem_size != src.elem_size {
2102                                return None;
2103                            }
2104
2105                            // Prefer an exact destination-element adjustment
2106                            // between `src.size_offset()` and
2107                            // `dst.size_offset()`. This preserves offset-based
2108                            // cast compatibility and metadata selection when
2109                            // several lengths have the same rounded size. It
2110                            // also avoids choosing a length whose subsequent
2111                            // padding sequence differs from the source's.
2112                            let exact_offset_delta_elems =
2113                                match src.size_offset().checked_sub(dst.size_offset()) {
2114                                    Some(delta) => {
2115                                        let (elems, described_delta) =
2116                                            super::max_elems_for_bytes(delta, dst_elem_size);
2117                                        if described_delta == delta {
2118                                            Some(elems)
2119                                        } else {
2120                                            None
2121                                        }
2122                                    }
2123                                    None => None,
2124                                };
2125
2126                            let offset_delta_elems = match exact_offset_delta_elems {
2127                                Some(elems) if Self::size_sequences_match(src, dst, elems) => elems,
2128                                _ => {
2129                                    let src_zero_size = match src.size_for_elems(0) {
2130                                        Some(size) => size,
2131                                        None => return None,
2132                                    };
2133                                    let elems =
2134                                        match dst_layout.metadata_for_exact_size(src_zero_size) {
2135                                            Some(elems) => elems,
2136                                            None => return None,
2137                                        };
2138                                    if !Self::size_sequences_match(src, dst, elems) {
2139                                        return None;
2140                                    }
2141                                    elems
2142                                }
2143                            };
2144
2145                            CastParamsInner::UnsizedToUnsized {
2146                                // SAFETY: `size_sequences_match` proves that
2147                                // this affine metadata map preserves size.
2148                                offset_delta_elems,
2149                                // SAFETY: We checked above that this is an exact
2150                                // ratio of source to destination element size.
2151                                elem_multiple,
2152                            }
2153                        }
2154                        _ => return None,
2155                    };
2156
2157                    // SAFETY: We checked above that `src.align >= dst.align`.
2158                    Some(CastParams { inner, _src: PhantomData, _dst: PhantomData })
2159                }
2160            }
2161
2162            impl<Src: KnownLayout + ?Sized, Dst: KnownLayout + ?Sized> CastParams<Src, Dst> {
2163                /// Produces `Dst` metadata describing an object of the same
2164                /// size as the `Src` described by `src_meta`.
2165                ///
2166                /// # Safety
2167                ///
2168                /// `src_meta` describes a `Src` whose size is no larger than
2169                /// `isize::MAX`.
2170                #[inline(always)]
2171                unsafe fn cast_metadata(
2172                    self,
2173                    src_meta: Src::PointerMetadata,
2174                ) -> Dst::PointerMetadata {
2175                    let dst_meta = match self.inner {
2176                        CastParamsInner::UnsizedToUnsized { offset_delta_elems, elem_multiple } => {
2177                            let src_meta = src_meta.to_elem_count();
2178                            // SAFETY: `self` witnesses that this affine map
2179                            // makes `Src` and `Dst`'s complete rounded size
2180                            // formulas equal for every source metadata value.
2181                            // Interpret the arithmetic in this proof over the
2182                            // mathematical nonnegative integers. Since the
2183                            // caller promises that `src_meta` is valid `Src`
2184                            // metadata, the source object size `src_size` it
2185                            // describes is at most `isize::MAX`. Let
2186                            // `src_elem_size` and `dst_elem_size` be the
2187                            // respective trailing-slice element sizes. Then:
2188                            //
2189                            //   src_meta * src_elem_size
2190                            //       <= src_size <= isize::MAX.
2191                            //
2192                            // Since `elem_multiple` is the exact ratio
2193                            // `src_elem_size / dst_elem_size` and
2194                            // `dst_elem_size >= 1`,
2195                            //
2196                            //   src_meta * elem_multiple
2197                            //       <= src_meta * src_elem_size
2198                            //       <= isize::MAX <= usize::MAX.
2199                            //
2200                            // Thus `scaled_elems = src_meta * elem_multiple`
2201                            // is representable, and its byte contribution is
2202                            // `scaled_elems * dst_elem_size = src_meta *
2203                            // src_elem_size`. Let `base_bytes =
2204                            // offset_delta_elems * dst_elem_size`.
2205                            // `size_sequences_match` compares the source
2206                            // formula with the destination formula advanced by
2207                            // `base_bytes`. The latter's unrounded term
2208                            // contains `base_bytes + src_meta * src_elem_size`,
2209                            // and its complete size is at least that large
2210                            // (formally checked by Kani in
2211                            // `prove_size_formula_bounds_size_offset`).
2212                            // Sequence equality therefore gives, over the
2213                            // mathematical nonnegative integers,
2214                            //
2215                            //   base_bytes + src_meta * src_elem_size
2216                            //       = (offset_delta_elems + scaled_elems)
2217                            //           * dst_elem_size
2218                            //       <= src_size.
2219                            //
2220                            // Since `dst_elem_size >= 1`, the metadata sum
2221                            // `offset_delta_elems + scaled_elems` is also at
2222                            // most `src_size`, and thus at most `isize::MAX <=
2223                            // usize::MAX`. The sum is therefore representable,
2224                            // and the returned metadata describes a `Dst` of
2225                            // the same size.
2226                            // SAFETY: The bounds above establish the helper's
2227                            // complete no-overflow precondition.
2228                            unsafe {
2229                                super::add_scaled_metadata(
2230                                    offset_delta_elems,
2231                                    src_meta,
2232                                    elem_multiple,
2233                                )
2234                            }
2235                        }
2236                        CastParamsInner::SizedToUnsized { dst_meta } => dst_meta,
2237                        CastParamsInner::SizedToSized => 0,
2238                    };
2239                    Dst::PointerMetadata::from_elem_count(dst_meta)
2240                }
2241            }
2242
2243            trait Params<Src: ?Sized> {
2244                const CAST_PARAMS: CastParams<Src, Self>;
2245            }
2246
2247            impl<Src, Dst> Params<Src> for Dst
2248            where
2249                Src: KnownLayout + ?Sized,
2250                Dst: KnownLayout + ?Sized,
2251            {
2252                const CAST_PARAMS: CastParams<Src, Dst> =
2253                    match CastParams::try_compute(&Src::LAYOUT, &Dst::LAYOUT) {
2254                        Some(params) => params,
2255                        None => const_panic!(
2256                            "cannot `transmute_ref!` or `transmute_mut!` between incompatible types"
2257                        ),
2258                    };
2259            }
2260
2261            let src_meta = <Src as KnownLayout>::pointer_to_metadata(src.as_ptr());
2262            let params = <Dst as Params<Src>>::CAST_PARAMS;
2263
2264            // SAFETY: `src: PtrInner` guarantees that `src`'s referent is zero
2265            // bytes or lives in a single allocation, which means that it is no
2266            // larger than `isize::MAX` bytes [1].
2267            //
2268            // [1] https://doc.rust-lang.org/1.92.0/std/ptr/index.html#allocation
2269            let dst_meta = unsafe { params.cast_metadata(src_meta) };
2270
2271            <Dst as KnownLayout>::raw_from_ptr_len(src.as_non_null().cast(), dst_meta).as_ptr()
2272        }
2273    }
2274}
2275
2276#[cfg(any(test, all(kani, feature = "derive")))]
2277mod padding_testutil {
2278    use crate::KnownLayout;
2279
2280    #[derive(KnownLayout)]
2281    #[repr(C, align(4))]
2282    pub(super) struct Aligned<Prefix, Tail: ?Sized> {
2283        pub(super) prefix: Prefix,
2284        pub(super) tail: Tail,
2285    }
2286
2287    /// Compare direct padding with the complete size minus the slice end.
2288    /// Counts whose complete object size exceeds `usize::MAX` are omitted.
2289    pub(super) fn check_padding_for_elems<Target>(elems: usize) -> Option<usize>
2290    where
2291        Target: ?Sized + KnownLayout<PointerMetadata = usize>,
2292    {
2293        let layout = crate::trailing_slice_layout::<Target>();
2294        let trailing_size = elems.checked_mul(layout.elem_size)?;
2295
2296        // Evaluate the complete rounded size independently of both helpers:
2297        // `size_for_elems` delegates to the padding method being tested.
2298        let (alignment, phase) = layout.size_rounding_align_and_phase.components();
2299        #[allow(clippy::arithmetic_side_effects)]
2300        let mask = alignment.get() - 1;
2301        let unrounded = phase.checked_add(trailing_size)?;
2302        let rounded = unrounded.checked_add(mask)? & !mask;
2303        let object_size = layout.size_base.checked_add(rounded)?;
2304        let trailing_end = layout.offset.checked_add(trailing_size).unwrap();
2305        let expected = object_size.checked_sub(trailing_end).unwrap();
2306
2307        let padding = layout.padding_for_elems(elems);
2308        assert_eq!(padding, expected);
2309        Some(padding)
2310    }
2311
2312    pub(super) fn check_layouts(elems: usize) {
2313        // Slices have no trailing padding. The aligned fixtures cover
2314        // zero and nonzero phases, a three-byte element whose padding
2315        // cycles with the element count, and zero-sized elements.
2316        let _ = check_padding_for_elems::<[u8]>(elems);
2317        let _ = check_padding_for_elems::<[()]>(elems);
2318        let _ = check_padding_for_elems::<Aligned<[u8; 8], [u8]>>(elems);
2319        let _ = check_padding_for_elems::<Aligned<[u8; 9], [u8]>>(elems);
2320        let _ = check_padding_for_elems::<Aligned<[u8; 9], [[u8; 3]]>>(elems);
2321        let _ = check_padding_for_elems::<Aligned<[u8; 9], [()]>>(elems);
2322    }
2323}
2324
2325// FIXME(#67): For some reason, on our MSRV toolchain, this `allow` isn't
2326// enforced despite having `#![allow(unknown_lints)]` at the crate root, but
2327// putting it here works. Once our MSRV is high enough that this bug has been
2328// fixed, remove this `allow`.
2329#[allow(unknown_lints)]
2330#[cfg(test)]
2331mod tests {
2332    use super::*;
2333    use crate::PointerMetadata as _;
2334
2335    /// Models the size of nested `repr(C)` structs ending in a slice.
2336    ///
2337    /// `leading` lists structs from outermost to innermost. Each tuple gives
2338    /// the packing factor, minimum alignment, and prefix size before padding
2339    /// for the final field. `trailing_elem_size` and `trailing_alignment` give
2340    /// the slice element size and alignment. An empty `leading` describes the
2341    /// slice itself.
2342    ///
2343    /// The returned closure maps a slice length to the complete size, including
2344    /// padding at every nesting level. It returns `None` only on `usize`
2345    /// overflow; sizes above `isize::MAX` are permitted by this arithmetic model.
2346    /// Packing affects field placement, preserving each field's internal
2347    /// padding, even for descriptors that Rust would reject as types.
2348    ///
2349    /// # Panics
2350    ///
2351    /// Panics if any alignment or packing factor is not a power of two, a
2352    /// minimum alignment exceeds its packing factor, or the element size is
2353    /// not a multiple of the slice alignment.
2354    fn size_for_metadata_model(
2355        leading: &[(NonZeroUsize, NonZeroUsize, usize)],
2356        trailing_elem_size: usize,
2357        trailing_alignment: NonZeroUsize,
2358    ) -> impl Fn(usize) -> Option<usize> + '_ {
2359        // Rust requires power-of-two alignments and sizes divisible by their
2360        // alignment [1]; alignment modifiers also take powers of two [2].
2361        //
2362        // [1] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#size-and-alignment
2363        // [2] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#the-alignment-modifiers
2364        assert!(trailing_alignment.get().is_power_of_two());
2365        assert_eq!(trailing_elem_size % trailing_alignment, 0);
2366        for &(packed, align, _) in leading {
2367            assert!(packed.get().is_power_of_two());
2368            assert!(align.get().is_power_of_two());
2369            // The descriptor requires its minimum to fit under the packing
2370            // cap. It also models combinations that Rust forbids, including
2371            // simultaneous `align` and `packed` modifiers [2].
2372            assert!(align <= packed);
2373        }
2374
2375        move |elems| {
2376            // A slice shares its array section's layout [3]. Array size is
2377            // element size times length, with the element's alignment [4].
2378            //
2379            // [3] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#slice-layout
2380            // [4] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#array-layout
2381            let mut size = trailing_elem_size.checked_mul(elems)?;
2382            let mut alignment = trailing_alignment;
2383
2384            // Work outward with each field's complete size, including its
2385            // trailing padding: an enclosing representation "does not change
2386            // the layout of the fields themselves" [5]. Thus packing a nested
2387            // field preserves the padding already included in `size`.
2388            //
2389            // [5] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#representations
2390            for &(packed, align, prefix) in leading.iter().rev() {
2391                // Packing caps the field's alignment for placement [2].
2392                let field_align = alignment.min(packed);
2393
2394                // `repr(C)` places the next field at an aligned offset [6];
2395                // packing requires the least sufficient inter-field padding
2396                // [2]. Here `prefix` is the end of the preceding fields.
2397                //
2398                // [6] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#reprc-structs
2399                let offset = prefix.checked_add(util::padding_needed_for(prefix, field_align))?;
2400
2401                // `repr(C)` uses the greatest field alignment [6], and an
2402                // explicit alignment can only raise it [2]. Here `align`
2403                // summarizes the minimum from the prefix and any explicit
2404                // alignment. Both operands are <= `packed`, so their maximum
2405                // also respects the packing cap.
2406                alignment = align.max(field_align);
2407
2408                // Advance past the complete field, then round to the struct's
2409                // alignment, following `repr(C)`'s final sizing steps [6].
2410                size = offset.checked_add(size)?;
2411                size = size.checked_add(util::padding_needed_for(size, alignment))?;
2412            }
2413            // Each enclosing size is at least its field's size. Thus any
2414            // checked multiplication or addition that overflowed above would
2415            // also make the final size unrepresentable in `usize`.
2416            Some(size)
2417        }
2418    }
2419
2420    /// Computes [`size_for_metadata_model`]'s size function using `DstLayout`.
2421    ///
2422    /// Layout construction uses [`DstLayout::for_repr_c_struct`], and the
2423    /// returned closure uses [`crate::PointerMetadata::size_for_metadata`].
2424    ///
2425    /// # Panics
2426    ///
2427    /// Panics for invalid descriptors as in [`size_for_metadata_model`], or if
2428    /// `DstLayout` cannot construct the layout. This includes static size
2429    /// overflow and, in debug builds, alignments or packing factors above
2430    /// [`DstLayout::CURRENT_MAX_ALIGN`].
2431    fn size_for_metadata_via_dst_layout(
2432        leading: &[(NonZeroUsize, NonZeroUsize, usize)],
2433        trailing_elem_size: usize,
2434        trailing_alignment: NonZeroUsize,
2435    ) -> impl Fn(usize) -> Option<usize> + '_ {
2436        assert_eq!(trailing_elem_size % trailing_alignment, 0);
2437        let mut layout = DstLayout {
2438            align: trailing_alignment,
2439            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
2440                offset: 0,
2441                elem_size: trailing_elem_size,
2442                size_base: 0,
2443                size_rounding_align_and_phase: RoundingAlignAndPhase::new(trailing_alignment, 0),
2444            }),
2445            statically_shallow_unpadded: true,
2446        };
2447        for &(packed, align, prefix) in leading.iter().rev() {
2448            assert!(align <= packed);
2449            let prefix = DstLayout {
2450                align: DstLayout::MIN_ALIGN,
2451                size_info: SizeInfo::Sized { size: prefix },
2452                statically_shallow_unpadded: true,
2453            };
2454            layout = DstLayout::for_repr_c_struct(Some(align), Some(packed), &[prefix, layout]);
2455        }
2456        move |elems: usize| elems.size_for_metadata(layout)
2457    }
2458
2459    #[test]
2460    fn test_model_via_dst_layout() {
2461        let nz = |n| NonZeroUsize::new(n).unwrap();
2462        let cases: &[&[_]] = &[
2463            &[],
2464            &[(nz(8), nz(1), 3)],
2465            &[(nz(8), nz(8), 3)],
2466            &[(nz(2), nz(1), 1), (nz(8), nz(8), 3)],
2467            &[(nz(8), nz(8), 1), (nz(2), nz(1), 3), (nz(8), nz(8), 5)],
2468            &[(nz(1), nz(1), 1), (nz(2), nz(2), 3), (nz(8), nz(8), 5)],
2469        ];
2470        for &leading in cases {
2471            for &(elem_size, alignment) in &[(0, 1), (0, 8), (1, 1), (2, 2), (3, 1), (8, 8)] {
2472                let alignment = nz(alignment);
2473                let expected = size_for_metadata_model(leading, elem_size, alignment);
2474                let actual = size_for_metadata_via_dst_layout(leading, elem_size, alignment);
2475                let max_elems = usize::MAX / elem_size.max(1);
2476                for elems in (0..32).chain([
2477                    max_elems - 1,
2478                    max_elems,
2479                    max_elems.saturating_add(1),
2480                    usize::MAX,
2481                ]) {
2482                    assert_eq!(
2483                        actual(elems),
2484                        expected(elems),
2485                        "leading: {:?}, trailing: {:?}, elems: {}",
2486                        leading,
2487                        (elem_size, alignment),
2488                        elems,
2489                    );
2490                }
2491            }
2492        }
2493
2494        for prefix in [usize::MAX - 1, usize::MAX] {
2495            let leading = [(nz(1), nz(1), prefix)];
2496            let expected = size_for_metadata_model(&leading, 1, nz(1));
2497            let actual = size_for_metadata_via_dst_layout(&leading, 1, nz(1));
2498            for elems in [0, 1, 2] {
2499                assert_eq!(actual(elems), expected(elems));
2500            }
2501        }
2502    }
2503
2504    // This exercises arithmetic rather than unsafe operations; keep the
2505    // smaller comparison test enabled under Miri instead of this large search.
2506    #[test]
2507    #[cfg_attr(miri, ignore)]
2508    fn test_model_via_dst_layout_combinations() {
2509        use rand::{rngs::SmallRng, Rng as _, SeedableRng as _};
2510
2511        fn check(
2512            leading: &[(NonZeroUsize, NonZeroUsize, usize)],
2513            trailing_elem_size: usize,
2514            trailing_alignment: NonZeroUsize,
2515        ) {
2516            let expected = size_for_metadata_model(leading, trailing_elem_size, trailing_alignment);
2517            assert!(
2518                expected(0).is_some(),
2519                "unrepresentable test layout: {:?}, {:?}",
2520                leading,
2521                (trailing_elem_size, trailing_alignment)
2522            );
2523            let actual = std::panic::catch_unwind(|| {
2524                size_for_metadata_via_dst_layout(leading, trailing_elem_size, trailing_alignment)
2525            })
2526            .unwrap_or_else(|_| {
2527                panic!(
2528                    "DstLayout rejected leading: {:?}, trailing: {:?}",
2529                    leading,
2530                    (trailing_elem_size, trailing_alignment)
2531                )
2532            });
2533            let compare = |elems| {
2534                let expected = expected(elems);
2535                assert_eq!(
2536                    actual(elems),
2537                    expected,
2538                    "leading: {:?}, trailing: {:?}, elems: {}",
2539                    leading,
2540                    (trailing_elem_size, trailing_alignment),
2541                    elems,
2542                );
2543                expected
2544            };
2545
2546            let first_sixteen = 0..=16;
2547            let pointer_width = (0..POINTER_WIDTH_BITS).step_by(4).flat_map(|shift| {
2548                let elems = 1usize << shift;
2549                [elems - 1, elems, elems + 1]
2550            });
2551            let last_two = (usize::MAX - 1)..=usize::MAX;
2552            let elemses = first_sixteen.chain(pointer_width).chain(last_two);
2553            for elems in elemses {
2554                let _ = compare(elems);
2555            }
2556
2557            // The modeled size is nondecreasing. Find the last fitting length,
2558            // comparing every probe, then check both sides of that boundary.
2559            // Zero-sized elements can remain representable at usize::MAX.
2560            let (mut low, mut high) = (0, usize::MAX);
2561            while low < high {
2562                let distance = high - low;
2563                let mid = low + distance / 2 + distance % 2;
2564                if compare(mid).is_some() {
2565                    low = mid;
2566                } else {
2567                    high = mid - 1;
2568                }
2569            }
2570            assert!(compare(low).is_some());
2571            if let Some(previous) = low.checked_sub(1) {
2572                assert!(compare(previous).is_some());
2573            }
2574            if let Some(next) = low.checked_add(1) {
2575                assert_eq!(compare(next), None);
2576            }
2577        }
2578
2579        let nz = |n| NonZeroUsize::new(n).unwrap();
2580        let check_small_tails = |leading: &[_]| {
2581            for alignment in [1, 2, 4, 8] {
2582                for multiple in [0, 1, 2, 3, 7] {
2583                    check(leading, multiple * alignment, nz(alignment));
2584                }
2585            }
2586        };
2587        let mut fragments = Vec::new();
2588        for packed in [1, 2, 4] {
2589            for align in [1, 2, 4].iter().copied().filter(|&align| align <= packed) {
2590                for prefix in 0..=4 {
2591                    fragments.push((nz(packed), nz(align), prefix));
2592                }
2593            }
2594        }
2595
2596        // Exhaust every combination in this small domain at depths 0, 1, 2.
2597        check_small_tails(&[]);
2598        for &outer in &fragments {
2599            check_small_tails(&[outer]);
2600            for &inner in &fragments {
2601                check_small_tails(&[outer, inner]);
2602            }
2603        }
2604
2605        let mut rng = SmallRng::seed_from_u64(0);
2606        for depth in (0usize..=128).chain([256, 512, 1024]) {
2607            // A small prefix adds at most 4 * max_align bytes per layer.
2608            // Reserve half of usize::MAX for a large outermost prefix so
2609            // construction remains representable even at the largest depth.
2610            let align_limit =
2611                DstLayout::CURRENT_MAX_ALIGN.get().min(usize::MAX / (8 * depth.max(1)));
2612            let alignments: Vec<_> = (0..POINTER_WIDTH_BITS)
2613                .map(|shift| 1usize << shift)
2614                .take_while(|&align| align <= align_limit)
2615                .collect();
2616            let last = alignments.len() - 1;
2617            let max_align = alignments[last];
2618            for case in 0..40 {
2619                let mut leading = Vec::with_capacity(depth);
2620                for level in 0..depth {
2621                    // Mix arbitrary modifiers, increasing and decreasing
2622                    // alignments, alternating packing, and fully packed trees.
2623                    let (packed, align) = match case / 8 {
2624                        0 => {
2625                            let packed = rng.gen_range(0..=last);
2626                            (packed, rng.gen_range(0..=packed))
2627                        }
2628                        1 => (last, level % alignments.len()),
2629                        2 => (last, last - level % alignments.len()),
2630                        3 => {
2631                            let index = if level % 2 == 0 { 0 } else { last };
2632                            (index, index)
2633                        }
2634                        _ => (0, 0),
2635                    };
2636                    let (packed, align) = (alignments[packed], alignments[align]);
2637                    let prefix = if level == 0 && case % 4 == 3 {
2638                        usize::MAX / 2 + rng.gen_range(0..=2) - 1
2639                    } else {
2640                        match rng.gen_range(0..8) {
2641                            0 => 0,
2642                            1 => 1,
2643                            2 => align - 1,
2644                            3 => align,
2645                            4 => align + 1,
2646                            5 => packed - 1,
2647                            6 => packed + 1,
2648                            _ => rng.gen_range(0..=2 * max_align),
2649                        }
2650                    };
2651                    leading.push((nz(packed), nz(align), prefix));
2652                }
2653                let alignment = alignments[rng.gen_range(0..=last)];
2654                let elem_size = match case % 8 {
2655                    0 => 0,
2656                    1 => alignment,
2657                    2 => 2 * alignment,
2658                    3 => 3 * alignment,
2659                    4 => 7 * alignment,
2660                    5 => 15 * alignment,
2661                    6 => (usize::MAX / 2 / alignment) * alignment,
2662                    _ => (usize::MAX / alignment) * alignment,
2663                };
2664                check(&leading, elem_size, nz(alignment));
2665            }
2666        }
2667    }
2668
2669    const TEST_SIZE_ROUNDING_ALIGN_AND_PHASE_ALIGN: NonZeroUsize = match NonZeroUsize::new(8) {
2670        Some(align) => align,
2671        None => const_unreachable!(),
2672    };
2673    const TEST_SIZE_ROUNDING_ALIGN_AND_PHASE: RoundingAlignAndPhase =
2674        RoundingAlignAndPhase::new(TEST_SIZE_ROUNDING_ALIGN_AND_PHASE_ALIGN, 3);
2675    const TEST_SIZE_ROUNDING_ALIGN_AND_PHASE_DECODED_ALIGN: usize =
2676        TEST_SIZE_ROUNDING_ALIGN_AND_PHASE.align().get();
2677    const TEST_SIZE_ROUNDING_ALIGN_AND_PHASE_DECODED_PHASE: usize =
2678        TEST_SIZE_ROUNDING_ALIGN_AND_PHASE.components().1;
2679
2680    #[test]
2681    #[allow(clippy::arithmetic_side_effects)]
2682    fn test_size_rounding_align_and_phase_encoding() {
2683        // These constants ensure that encoding and decoding remain evaluable
2684        // on every supported compiler, including the MSRV.
2685        assert_eq!(TEST_SIZE_ROUNDING_ALIGN_AND_PHASE_DECODED_ALIGN, 8);
2686        assert_eq!(TEST_SIZE_ROUNDING_ALIGN_AND_PHASE_DECODED_PHASE, 3);
2687
2688        for shift in 0..POINTER_WIDTH_BITS {
2689            #[allow(clippy::arithmetic_side_effects)]
2690            let align = NonZeroUsize::new(1usize << shift).unwrap();
2691            for phase in [0, align.get() / 2, align.get() - 1] {
2692                let rounding = RoundingAlignAndPhase::new(align, phase);
2693                assert_eq!(rounding.align(), align);
2694                assert_eq!(rounding.components().1, phase);
2695                assert_eq!(rounding.0.get(), align.get() | phase);
2696            }
2697        }
2698
2699        let max_align = DstLayout::THEORETICAL_MAX_ALIGN;
2700        let max_phase = max_align.get() - 1;
2701        let max = RoundingAlignAndPhase::new(max_align, max_phase);
2702        assert_eq!(max.0.get(), usize::MAX);
2703        assert_eq!(max.align(), max_align);
2704        assert_eq!(max.components().1, max_phase);
2705
2706        assert_eq!(mem::size_of::<TrailingSliceLayout>(), 4 * mem::size_of::<usize>());
2707    }
2708
2709    #[test]
2710    #[allow(clippy::arithmetic_side_effects)]
2711    fn test_trailing_slice_layout_normal_form() {
2712        // Compare the stored base-and-phase formula with
2713        // `prefix + round_up(offset + elems * elem_size, align)`, where
2714        // `prefix < align`. Move whole alignment-sized chunks of `offset`
2715        // into `size_base` and retain the remainder as the phase. Also verify
2716        // that `size_offset()` recovers the exact `offset` used by casts to
2717        // choose between metadata values with the same rounded object size.
2718        for align in [1, 2, 4, 8, 16] {
2719            let align = NonZeroUsize::new(align).unwrap();
2720            for prefix in 0..align.get() {
2721                for offset in 0..32 {
2722                    let phase = offset % align;
2723                    let size_base = (offset - phase) | prefix;
2724                    let layout = TrailingSliceLayout {
2725                        offset: 0,
2726                        elem_size: 3,
2727                        size_base,
2728                        size_rounding_align_and_phase: RoundingAlignAndPhase::new(align, phase),
2729                    };
2730                    assert_eq!(layout.size_offset(), offset);
2731
2732                    for elems in 0..32 {
2733                        let unrounded = offset + elems * layout.elem_size;
2734                        let old_size =
2735                            prefix + unrounded + util::padding_needed_for(unrounded, align);
2736                        assert_eq!(layout.size_for_elems(elems), Some(old_size));
2737                    }
2738                }
2739            }
2740        }
2741    }
2742
2743    #[test]
2744    #[allow(clippy::arithmetic_side_effects)]
2745    fn test_normalized_size_bounds() {
2746        // Use checked evaluation as an independent oracle, including layouts
2747        // whose empty object is already too large to represent.
2748        let checked_size = |layout: TrailingSliceLayout, trailing_bytes: usize| {
2749            let (alignment, phase) = layout.size_rounding_align_and_phase.components();
2750            let unrounded = phase.checked_add(trailing_bytes)?;
2751            let rounded = unrounded.checked_add(util::padding_needed_for(unrounded, alignment))?;
2752            layout.size_base.checked_add(rounded)
2753        };
2754        for exponent in 0..POINTER_WIDTH_BITS {
2755            let alignment = NonZeroUsize::new(1usize << exponent).unwrap();
2756            for phase in [0, alignment.get() / 2, alignment.get() - 1] {
2757                for base in [0, 1, alignment.get() - 1, alignment.get(), usize::MAX - 1, usize::MAX]
2758                {
2759                    let mut layout = TrailingSliceLayout {
2760                        offset: 0,
2761                        elem_size: 1,
2762                        size_base: base,
2763                        size_rounding_align_and_phase: RoundingAlignAndPhase::new(alignment, phase),
2764                    };
2765                    for available in [0, 1, alignment.get() - 1, alignment.get(), usize::MAX] {
2766                        match layout.max_trailing_bytes(available) {
2767                            Some(max_bytes) => {
2768                                assert!(checked_size(layout, max_bytes).unwrap() <= available);
2769                                if let Some(next_bytes) = max_bytes.checked_add(1) {
2770                                    assert!(checked_size(layout, next_bytes)
2771                                        .map(|size| size > available)
2772                                        .unwrap_or(true));
2773                                }
2774                            }
2775                            None => {
2776                                assert!(checked_size(layout, 0)
2777                                    .map(|size| size > available)
2778                                    .unwrap_or(true));
2779                            }
2780                        }
2781                    }
2782
2783                    for elem_size in [0, 1, 3, alignment.get(), usize::MAX] {
2784                        layout.elem_size = elem_size;
2785                        let max_elems = layout
2786                            .max_trailing_bytes(usize::MAX)
2787                            .and_then(|bytes| bytes.checked_div(elem_size))
2788                            .unwrap_or(usize::MAX);
2789                        for elems in [
2790                            0,
2791                            1,
2792                            max_elems.saturating_sub(1),
2793                            max_elems,
2794                            max_elems.saturating_add(1),
2795                            usize::MAX,
2796                        ] {
2797                            let expected = elems
2798                                .checked_mul(elem_size)
2799                                .and_then(|bytes| checked_size(layout, bytes));
2800                            // The physical slice offset does not affect
2801                            // the size formula. Raw layouts may place the
2802                            // slice beyond the object, and adding its byte
2803                            // count to the offset may overflow.
2804                            for offset in [0, usize::MAX] {
2805                                layout.offset = offset;
2806                                assert_eq!(layout.size_for_elems(elems), expected);
2807                            }
2808                        }
2809                    }
2810                }
2811            }
2812        }
2813    }
2814
2815    #[test]
2816    fn test_padding_for_elems_for_rust_layouts() {
2817        use super::padding_testutil::{check_layouts, check_padding_for_elems, Aligned};
2818
2819        for elems in 0..256 {
2820            check_layouts(elems);
2821        }
2822        let size_limit = usize::MAX;
2823        for remaining in 0..16 {
2824            check_layouts(size_limit - remaining);
2825            check_layouts(size_limit / 3 - remaining);
2826        }
2827        check_layouts(usize::MAX);
2828
2829        for (elems, expected) in [(0, 3), (1, 0), (2, 1), (3, 2)] {
2830            assert_eq!(
2831                check_padding_for_elems::<Aligned<[u8; 9], [[u8; 3]]>>(elems),
2832                Some(expected)
2833            );
2834        }
2835        // Every count is valid for zero-sized trailing elements.
2836        assert_eq!(check_padding_for_elems::<Aligned<[u8; 9], [()]>>(usize::MAX), Some(3));
2837    }
2838
2839    #[test]
2840    fn test_padding_for_elems() {
2841        for exponent in 0..POINTER_WIDTH_BITS {
2842            let alignment = NonZeroUsize::new(1usize << exponent).unwrap();
2843            let mask = alignment.get() - 1;
2844            for phase in [0, alignment.get() / 2, mask] {
2845                // Include zero-sized elements, a negative fixed term, fixed
2846                // padding larger than the alignment, and overflowing sums
2847                // and products. Raw layouts need not describe Rust types.
2848                for (offset, elem_size, size_base) in [
2849                    (0, 0, 0),
2850                    (0, 1, 1),
2851                    (9, 3, 8),
2852                    (3, 3, 0),
2853                    (9, 3, 12),
2854                    (usize::MAX, usize::MAX, usize::MAX),
2855                    (usize::MAX, 0, usize::MAX),
2856                ] {
2857                    let layout = TrailingSliceLayout {
2858                        offset,
2859                        elem_size,
2860                        size_base,
2861                        size_rounding_align_and_phase: RoundingAlignAndPhase::new(alignment, phase),
2862                    };
2863                    for elems in [0, 1, 2, 3, alignment.get(), usize::MAX] {
2864                        // Independently reconstruct the complete rounded
2865                        // size and slice end, then subtract. Round up by
2866                        // adding the mask and clearing the low bits.
2867                        let trailing_bytes = elems.wrapping_mul(elem_size);
2868                        let unrounded = phase.wrapping_add(trailing_bytes);
2869                        let rounded = unrounded.wrapping_add(mask) & !mask;
2870                        let object_size = size_base.wrapping_add(rounded);
2871                        let trailing_end = offset.wrapping_add(trailing_bytes);
2872                        assert_eq!(
2873                            layout.padding_for_elems(elems),
2874                            object_size.wrapping_sub(trailing_end),
2875                        );
2876                    }
2877                }
2878            }
2879        }
2880    }
2881
2882    #[test]
2883    fn test_dst_layout_for_slice() {
2884        let layout = DstLayout::for_slice::<u32>();
2885        match layout.size_info {
2886            SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size, .. }) => {
2887                assert_eq!(offset, 0);
2888                assert_eq!(elem_size, 4);
2889            }
2890            _ => panic!("Expected SliceDst"),
2891        }
2892        assert_eq!(layout.align.get(), 4);
2893    }
2894
2895    /// Tests of when a sized `DstLayout` is extended with a sized field.
2896    #[allow(clippy::decimal_literal_representation)]
2897    #[test]
2898    fn test_dst_layout_extend_sized_with_sized() {
2899        // This macro constructs a layout corresponding to a `u8` and extends
2900        // it with a zero-sized trailing field of the specified alignment. The
2901        // resulting size and alignment must both be the field's alignment
2902        // capped by the packing limit. Exercise every supported packing limit
2903        // as well as the absence of a packing limit.
2904        macro_rules! test_align_is_size {
2905            ($n:expr) => {
2906                let base = DstLayout::for_type::<u8>();
2907                let trailing_field = DstLayout::for_type::<elain::Align<$n>>();
2908
2909                let packs = core::iter::once(None)
2910                    .chain((0..=29).map(|p| NonZeroUsize::new(2usize.pow(p))));
2911
2912                for pack in packs {
2913                    let composite = base.extend(trailing_field, pack);
2914                    let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN);
2915                    let align = $n.min(max_align.get());
2916                    assert_eq!(
2917                        composite,
2918                        DstLayout {
2919                            align: NonZeroUsize::new(align).unwrap(),
2920                            size_info: SizeInfo::Sized { size: align },
2921                            statically_shallow_unpadded: false,
2922                        }
2923                    )
2924                }
2925            };
2926        }
2927
2928        test_align_is_size!(1);
2929        test_align_is_size!(2);
2930        test_align_is_size!(4);
2931        test_align_is_size!(8);
2932        test_align_is_size!(16);
2933        test_align_is_size!(32);
2934        test_align_is_size!(64);
2935        test_align_is_size!(128);
2936        test_align_is_size!(256);
2937        test_align_is_size!(512);
2938        test_align_is_size!(1024);
2939        test_align_is_size!(2048);
2940        test_align_is_size!(4096);
2941        test_align_is_size!(8192);
2942        test_align_is_size!(16384);
2943        test_align_is_size!(32768);
2944        test_align_is_size!(65536);
2945        test_align_is_size!(131072);
2946        test_align_is_size!(262144);
2947        test_align_is_size!(524288);
2948        test_align_is_size!(1048576);
2949        test_align_is_size!(2097152);
2950        test_align_is_size!(4194304);
2951        test_align_is_size!(8388608);
2952        test_align_is_size!(16777216);
2953        test_align_is_size!(33554432);
2954        test_align_is_size!(67108864);
2955        test_align_is_size!(33554432);
2956        test_align_is_size!(134217728);
2957        test_align_is_size!(268435456);
2958        test_align_is_size!(536870912);
2959    }
2960
2961    /// Tests of when a sized `DstLayout` is extended with a DST field.
2962    #[test]
2963    fn test_dst_layout_extend_sized_with_dst() {
2964        // Test that for all combinations of real-world alignments and
2965        // `repr_packed` values, that the extension of a sized `DstLayout`` with
2966        // a DST field correctly computes the trailing offset in the composite
2967        // layout.
2968
2969        let aligns = (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap());
2970        let packs = core::iter::once(None).chain(aligns.clone().map(Some));
2971
2972        for field_type_align in aligns {
2973            for pack in packs.clone() {
2974                let base = DstLayout::for_type::<u8>();
2975                let elem_size = 42;
2976                let trailing_field_offset = 11;
2977                #[allow(clippy::arithmetic_side_effects)]
2978                let trailing_phase = trailing_field_offset % field_type_align;
2979                let trailing_base = util::round_down_to_next_multiple_of_alignment(
2980                    trailing_field_offset,
2981                    field_type_align,
2982                );
2983
2984                let trailing_field = DstLayout {
2985                    align: field_type_align,
2986                    size_info: SizeInfo::SliceDst(TrailingSliceLayout {
2987                        elem_size,
2988                        offset: trailing_field_offset,
2989                        size_base: trailing_base,
2990                        size_rounding_align_and_phase: RoundingAlignAndPhase::new(
2991                            field_type_align,
2992                            trailing_phase,
2993                        ),
2994                    }),
2995                    statically_shallow_unpadded: false,
2996                };
2997
2998                let composite = base.extend(trailing_field, pack);
2999
3000                let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN).get();
3001
3002                let align = field_type_align.get().min(max_align);
3003                let field_offset = align;
3004                let size_base = field_offset + trailing_base;
3005
3006                assert_eq!(
3007                    composite,
3008                    DstLayout {
3009                        align: NonZeroUsize::new(align).unwrap(),
3010                        size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3011                            elem_size,
3012                            offset: field_offset + trailing_field_offset,
3013                            size_base,
3014                            size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3015                                field_type_align,
3016                                trailing_phase,
3017                            ),
3018                        }),
3019                        statically_shallow_unpadded: false,
3020                    }
3021                )
3022            }
3023        }
3024    }
3025
3026    /// Tests that calling `pad_to_align` on a sized `DstLayout` adds the
3027    /// expected amount of trailing padding.
3028    #[test]
3029    fn test_dst_layout_pad_to_align_with_sized() {
3030        // For all valid alignments `align`, construct a one-byte layout aligned
3031        // to `align`, call `pad_to_align`, and assert that the size of the
3032        // resulting layout is equal to `align`.
3033        for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) {
3034            let layout = DstLayout {
3035                align,
3036                size_info: SizeInfo::Sized { size: 1 },
3037                statically_shallow_unpadded: true,
3038            };
3039
3040            assert_eq!(
3041                layout.pad_to_align(),
3042                DstLayout {
3043                    align,
3044                    size_info: SizeInfo::Sized { size: align.get() },
3045                    statically_shallow_unpadded: align.get() == 1
3046                }
3047            );
3048        }
3049
3050        // Test explicitly-provided combinations of unpadded and padded
3051        // counterparts.
3052
3053        macro_rules! test {
3054            (unpadded { size: $unpadded_size:expr, align: $unpadded_align:expr }
3055                    => padded { size: $padded_size:expr, align: $padded_align:expr }) => {
3056                let unpadded = DstLayout {
3057                    align: NonZeroUsize::new($unpadded_align).unwrap(),
3058                    size_info: SizeInfo::Sized { size: $unpadded_size },
3059                    statically_shallow_unpadded: false,
3060                };
3061                let padded = unpadded.pad_to_align();
3062
3063                assert_eq!(
3064                    padded,
3065                    DstLayout {
3066                        align: NonZeroUsize::new($padded_align).unwrap(),
3067                        size_info: SizeInfo::Sized { size: $padded_size },
3068                        statically_shallow_unpadded: false,
3069                    }
3070                );
3071            };
3072        }
3073
3074        test!(unpadded { size: 0, align: 4 } => padded { size: 0, align: 4 });
3075        test!(unpadded { size: 1, align: 4 } => padded { size: 4, align: 4 });
3076        test!(unpadded { size: 2, align: 4 } => padded { size: 4, align: 4 });
3077        test!(unpadded { size: 3, align: 4 } => padded { size: 4, align: 4 });
3078        test!(unpadded { size: 4, align: 4 } => padded { size: 4, align: 4 });
3079        test!(unpadded { size: 5, align: 4 } => padded { size: 8, align: 4 });
3080        test!(unpadded { size: 6, align: 4 } => padded { size: 8, align: 4 });
3081        test!(unpadded { size: 7, align: 4 } => padded { size: 8, align: 4 });
3082        test!(unpadded { size: 8, align: 4 } => padded { size: 8, align: 4 });
3083
3084        let current_max_align = DstLayout::CURRENT_MAX_ALIGN.get();
3085
3086        test!(unpadded { size: 1, align: current_max_align }
3087                => padded { size: current_max_align, align: current_max_align });
3088
3089        test!(unpadded { size: current_max_align + 1, align: current_max_align }
3090                => padded { size: current_max_align * 2, align: current_max_align });
3091    }
3092
3093    /// Tests that calling `pad_to_align` on a DST `DstLayout` incorporates the
3094    /// type's trailing-padding operation into its runtime size formula.
3095    #[test]
3096    fn test_dst_layout_pad_to_align_with_dst() {
3097        for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) {
3098            for offset in 0..10 {
3099                for elem_size in 0..10 {
3100                    let layout = DstLayout {
3101                        align,
3102                        size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3103                            offset,
3104                            elem_size,
3105                            size_base: offset,
3106                            size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3107                                DstLayout::MIN_ALIGN,
3108                                0,
3109                            ),
3110                        }),
3111                        statically_shallow_unpadded: false,
3112                    };
3113                    assert_eq!(
3114                        layout.pad_to_align(),
3115                        DstLayout {
3116                            align,
3117                            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3118                                offset,
3119                                elem_size,
3120                                size_base: offset - offset % align,
3121                                size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3122                                    align,
3123                                    offset % align
3124                                ),
3125                            }),
3126                            statically_shallow_unpadded: false,
3127                        }
3128                    );
3129                }
3130            }
3131        }
3132
3133        // Exercise the branch where the existing inner rounding alignment is
3134        // at least the enclosing alignment. The inner rounded term is already
3135        // aligned to two, so padding only advances `size_base` from three to
3136        // four.
3137        let inner_rounding_dominates = DstLayout {
3138            align: NonZeroUsize::new(2).unwrap(),
3139            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3140                offset: 3,
3141                elem_size: 1,
3142                size_base: 3,
3143                size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3144                    NonZeroUsize::new(4).unwrap(),
3145                    1,
3146                ),
3147            }),
3148            statically_shallow_unpadded: false,
3149        };
3150        assert_eq!(
3151            inner_rounding_dominates.pad_to_align(),
3152            DstLayout {
3153                align: NonZeroUsize::new(2).unwrap(),
3154                size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3155                    offset: 3,
3156                    elem_size: 1,
3157                    size_base: 4,
3158                    size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3159                        NonZeroUsize::new(4).unwrap(),
3160                        1
3161                    ),
3162                }),
3163                statically_shallow_unpadded: false,
3164            }
3165        );
3166
3167        // Exercise the branch where the enclosing alignment is larger. For
3168        // a trailing-slice byte count `trailing_size`, rounding
3169        // `6 + round_up(1 + trailing_size, 4)` to eight gives the equivalent
3170        // formula `8 + round_up(1 + trailing_size, 8)`.
3171        let outer_rounding_dominates = DstLayout {
3172            align: NonZeroUsize::new(8).unwrap(),
3173            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3174                offset: 7,
3175                elem_size: 1,
3176                size_base: 6,
3177                size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3178                    NonZeroUsize::new(4).unwrap(),
3179                    1,
3180                ),
3181            }),
3182            statically_shallow_unpadded: false,
3183        };
3184        assert_eq!(
3185            outer_rounding_dominates.pad_to_align(),
3186            DstLayout {
3187                align: NonZeroUsize::new(8).unwrap(),
3188                size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3189                    offset: 7,
3190                    elem_size: 1,
3191                    size_base: 8,
3192                    size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3193                        NonZeroUsize::new(8).unwrap(),
3194                        1
3195                    ),
3196                }),
3197                statically_shallow_unpadded: false,
3198            }
3199        );
3200    }
3201
3202    #[test]
3203    fn test_trailing_slice_layout_size_sequence_equivalence() {
3204        // These are the two normalized descriptions produced by a
3205        // `repr(C, packed(2))` wrapper around slice DSTs whose element types
3206        // both have size four but alignments four and two respectively. Their
3207        // formulas differ structurally, but both simplify to `2 + 4 * elems`.
3208        let align4 = TrailingSliceLayout {
3209            offset: 2,
3210            elem_size: 4,
3211            size_base: 2,
3212            size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3213                NonZeroUsize::new(4).unwrap(),
3214                0,
3215            ),
3216        };
3217        let align2 = TrailingSliceLayout {
3218            offset: 2,
3219            elem_size: 4,
3220            size_base: 2,
3221            size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3222                NonZeroUsize::new(2).unwrap(),
3223                0,
3224            ),
3225        };
3226        assert!(align4.has_same_size_sequence(align2));
3227        for elems in 0..8 {
3228            assert_eq!(align4.size_for_elems(elems), Some(2 + 4 * elems));
3229            assert_eq!(align4.size_for_elems(elems), align2.size_for_elems(elems));
3230        }
3231
3232        let dynamically_padded = TrailingSliceLayout { elem_size: 1, ..align4 };
3233        assert!(!dynamically_padded.has_same_size_sequence(align2));
3234    }
3235
3236    // This test takes a long time when running under Miri, so we skip it in
3237    // that case. This is acceptable because this is a logic test that doesn't
3238    // attempt to expose UB.
3239    #[test]
3240    #[cfg_attr(miri, ignore)]
3241    fn test_validate_cast_and_convert_metadata() {
3242        #[allow(non_local_definitions)]
3243        impl From<usize> for SizeInfo {
3244            fn from(size: usize) -> SizeInfo {
3245                SizeInfo::Sized { size }
3246            }
3247        }
3248
3249        #[allow(non_local_definitions)]
3250        impl From<(usize, usize)> for SizeInfo {
3251            fn from((offset, elem_size): (usize, usize)) -> SizeInfo {
3252                SizeInfo::SliceDst(TrailingSliceLayout {
3253                    offset,
3254                    elem_size,
3255                    size_base: offset,
3256                    size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3257                        DstLayout::MIN_ALIGN,
3258                        0,
3259                    ),
3260                })
3261            }
3262        }
3263
3264        fn layout<S: Into<SizeInfo>>(s: S, align: usize) -> DstLayout {
3265            let align = NonZeroUsize::new(align).unwrap();
3266            let size_info = match s.into() {
3267                SizeInfo::SliceDst(mut trailing) => {
3268                    let phase = trailing.size_base % align;
3269                    trailing.size_base =
3270                        util::round_down_to_next_multiple_of_alignment(trailing.size_base, align);
3271                    trailing.size_rounding_align_and_phase =
3272                        RoundingAlignAndPhase::new(align, phase);
3273                    SizeInfo::SliceDst(trailing)
3274                }
3275                size_info => size_info,
3276            };
3277            DstLayout { size_info, align, statically_shallow_unpadded: false }
3278        }
3279
3280        /// This macro accepts arguments in the form of:
3281        ///
3282        ///           layout(_, _).validate(_, _, _), Ok(Some((_, _)))
3283        ///                  |  |           |  |  |            |  |
3284        ///    size ---------+  |           |  |  |            |  |
3285        ///    align -----------+           |  |  |            |  |
3286        ///    addr ------------------------+  |  |            |  |
3287        ///    bytes_len ----------------------+  |            |  |
3288        ///    cast_type -------------------------+            |  |
3289        ///    elems ------------------------------------------+  |
3290        ///    split_at ------------------------------------------+
3291        ///
3292        /// `.validate` is shorthand for `.validate_cast_and_convert_metadata`
3293        /// for brevity.
3294        ///
3295        /// Each argument can either be an iterator or a wildcard. Each
3296        /// wildcarded variable is implicitly replaced by an iterator over a
3297        /// representative sample of values for that variable. Each `test!`
3298        /// invocation iterates over every combination of values provided by
3299        /// each variable's iterator (ie, the cartesian product) and validates
3300        /// that the results are expected.
3301        ///
3302        /// The final argument uses the same syntax, but it has a different
3303        /// meaning:
3304        /// - If it is `Ok(pat)`, then the pattern `pat` is supplied to
3305        ///   a matching assert to validate the computed result for each
3306        ///   combination of input values.
3307        /// - If it is `Err(Some(msg) | None)`, then `test!` validates that the
3308        ///   call to `validate_cast_and_convert_metadata` panics with the given
3309        ///   panic message or, if the current Rust toolchain version is too
3310        ///   early to support panicking in `const fn`s, panics with *some*
3311        ///   message. In the latter case, the `const_panic!` macro is used,
3312        ///   which emits code which causes a non-panicking error at const eval
3313        ///   time, but which does panic when invoked at runtime. Thus, it is
3314        ///   merely difficult to predict the *value* of this panic. We deem
3315        ///   that testing against the real panic strings on stable and nightly
3316        ///   toolchains is enough to ensure correctness.
3317        ///
3318        /// Note that the meta-variables that match these variables have the
3319        /// `tt` type, and some valid expressions are not valid `tt`s (such as
3320        /// `a..b`). In this case, wrap the expression in parentheses, and it
3321        /// will become valid `tt`.
3322        macro_rules! test {
3323                (
3324                    layout($size:tt, $align:tt)
3325                    .validate($addr:tt, $bytes_len:tt, $cast_type:tt), $expect:pat $(,)?
3326                ) => {
3327                    itertools::iproduct!(
3328                        test!(@generate_size $size),
3329                        test!(@generate_align $align),
3330                        test!(@generate_usize $addr),
3331                        test!(@generate_usize $bytes_len),
3332                        test!(@generate_cast_type $cast_type)
3333                    ).for_each(|(size_info, align, addr, bytes_len, cast_type)| {
3334                        // Temporarily disable the panic hook installed by the test
3335                        // harness. If we don't do this, all panic messages will be
3336                        // kept in an internal log. On its own, this isn't a
3337                        // problem, but if a non-caught panic ever happens (ie, in
3338                        // code later in this test not in this macro), all of the
3339                        // previously-buffered messages will be dumped, hiding the
3340                        // real culprit.
3341                        let previous_hook = std::panic::take_hook();
3342                        // I don't understand why, but this seems to be required in
3343                        // addition to the previous line.
3344                        std::panic::set_hook(Box::new(|_| {}));
3345                        let actual = std::panic::catch_unwind(|| {
3346                            layout(size_info, align).validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
3347                        }).map_err(|d| {
3348                            let msg = d.downcast::<&'static str>().ok().map(|s| *s.as_ref());
3349                            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");
3350                            msg
3351                        });
3352                        std::panic::set_hook(previous_hook);
3353
3354                        assert!(
3355                            matches!(actual, $expect),
3356                            "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?})" ,size_info, align, addr, bytes_len, cast_type
3357                        );
3358                    });
3359                };
3360                (@generate_usize _) => { 0..8 };
3361                // Generate sizes for both Sized and !Sized types.
3362                (@generate_size _) => {
3363                    test!(@generate_size (_)).chain(test!(@generate_size (_, _)))
3364                };
3365                // Generate sizes for both Sized and !Sized types by chaining
3366                // specified iterators for each.
3367                (@generate_size ($sized_sizes:tt | $unsized_sizes:tt)) => {
3368                    test!(@generate_size ($sized_sizes)).chain(test!(@generate_size $unsized_sizes))
3369                };
3370                // Generate sizes for Sized types.
3371                (@generate_size (_)) => { test!(@generate_size (0..8)) };
3372                (@generate_size ($sizes:expr)) => { $sizes.into_iter().map(Into::<SizeInfo>::into) };
3373                // Generate sizes for !Sized types.
3374                (@generate_size ($min_sizes:tt, $elem_sizes:tt)) => {
3375                    itertools::iproduct!(
3376                        test!(@generate_min_size $min_sizes),
3377                        test!(@generate_elem_size $elem_sizes)
3378                    ).map(Into::<SizeInfo>::into)
3379                };
3380                (@generate_fixed_size _) => { (0..8).into_iter().map(Into::<SizeInfo>::into) };
3381                (@generate_min_size _) => { 0..8 };
3382                (@generate_elem_size _) => { 1..8 };
3383                (@generate_align _) => { [1, 2, 4, 8, 16] };
3384                (@generate_opt_usize _) => { [None].into_iter().chain((0..8).map(Some).into_iter()) };
3385                (@generate_cast_type _) => { [CastType::Prefix, CastType::Suffix] };
3386                (@generate_cast_type $variant:ident) => { [CastType::$variant] };
3387                // Some expressions need to be wrapped in parentheses in order to be
3388                // valid `tt`s (required by the top match pattern). See the comment
3389                // below for more details. This arm removes these parentheses to
3390                // avoid generating an `unused_parens` warning.
3391                (@$_:ident ($vals:expr)) => { $vals };
3392                (@$_:ident $vals:expr) => { $vals };
3393            }
3394
3395        const EVENS: [usize; 8] = [0, 2, 4, 6, 8, 10, 12, 14];
3396        const ODDS: [usize; 8] = [1, 3, 5, 7, 9, 11, 13, 15];
3397
3398        // base_size is too big for the memory region.
3399        test!(
3400            layout(((1..8) | ((1..8), (1..8))), _).validate([0], [0], _),
3401            Ok(Err(MetadataCastError::Size))
3402        );
3403        test!(
3404            layout(((2..8) | ((2..8), (2..8))), _).validate([0], [1], Prefix),
3405            Ok(Err(MetadataCastError::Size))
3406        );
3407        test!(
3408            layout(((2..8) | ((2..8), (2..8))), _).validate([0x1000_0000 - 1], [1], Suffix),
3409            Ok(Err(MetadataCastError::Size))
3410        );
3411
3412        // addr is unaligned for prefix cast
3413        test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
3414        test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
3415
3416        // addr is aligned, but end of buffer is unaligned for suffix cast
3417        test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
3418        test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
3419
3420        // Unfortunately, these constants cannot easily be used in the
3421        // implementation of `validate_cast_and_convert_metadata`, since
3422        // `panic!` consumes a string literal, not an expression.
3423        //
3424        // It's important that these messages be in a separate module. If they
3425        // were at the function's top level, we'd pass them to `test!` as, e.g.,
3426        // `Err(TRAILING)`, which would run into a subtle Rust footgun - the
3427        // `TRAILING` identifier would be treated as a pattern to match rather
3428        // than a value to check for equality.
3429        mod msgs {
3430            pub(super) const TRAILING: &str =
3431                "attempted to cast to slice type with zero-sized element";
3432            pub(super) const OVERFLOW: &str = "`addr` + `bytes_len` > usize::MAX";
3433        }
3434
3435        // casts with ZST trailing element types are unsupported
3436        test!(layout((_, [0]), _).validate(_, _, _), Err(Some(msgs::TRAILING) | None),);
3437
3438        // addr + bytes_len must not overflow usize
3439        test!(layout(_, _).validate([usize::MAX], (1..100), _), Err(Some(msgs::OVERFLOW) | None));
3440        test!(layout(_, _).validate((1..100), [usize::MAX], _), Err(Some(msgs::OVERFLOW) | None));
3441        test!(
3442            layout(_, _).validate(
3443                [usize::MAX / 2 + 1, usize::MAX],
3444                [usize::MAX / 2 + 1, usize::MAX],
3445                _
3446            ),
3447            Err(Some(msgs::OVERFLOW) | None)
3448        );
3449
3450        // Validates that `validate_cast_and_convert_metadata` satisfies its own
3451        // documented safety postconditions, and also a few other properties
3452        // that aren't documented but we want to guarantee anyway.
3453        fn validate_behavior(
3454            (layout, addr, bytes_len, cast_type): (DstLayout, usize, usize, CastType),
3455        ) {
3456            if let Ok((elems, split_at)) =
3457                layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
3458            {
3459                let (size_info, align) = (layout.size_info, layout.align);
3460                let debug_str = format!(
3461                    "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?}) => ({}, {})",
3462                    size_info, align, addr, bytes_len, cast_type, elems, split_at
3463                );
3464
3465                // If this is a sized type (no trailing slice), then `elems` is
3466                // meaningless, but in practice we set it to 0. Callers are not
3467                // allowed to rely on this, but a lot of math is nicer if
3468                // they're able to, and some callers might accidentally do that.
3469                let sized = matches!(layout.size_info, SizeInfo::Sized { .. });
3470                assert!(!(sized && elems != 0), "{}", debug_str);
3471
3472                let resulting_size = match layout.size_info {
3473                    SizeInfo::Sized { size } => size,
3474                    SizeInfo::SliceDst(TrailingSliceLayout {
3475                        elem_size,
3476                        size_base,
3477                        size_rounding_align_and_phase,
3478                        ..
3479                    }) => {
3480                        let (size_align, size_phase) = size_rounding_align_and_phase.components();
3481                        let padded_size = |elems| {
3482                            let without_padding = size_phase + elems * elem_size;
3483                            size_base
3484                                + without_padding
3485                                + util::padding_needed_for(without_padding, size_align)
3486                        };
3487
3488                        let resulting_size = padded_size(elems);
3489                        // Test that `validate_cast_and_convert_metadata`
3490                        // computed the largest possible value that fits in the
3491                        // given range.
3492                        assert!(padded_size(elems + 1) > bytes_len, "{}", debug_str);
3493                        resulting_size
3494                    }
3495                };
3496
3497                // Test safety postconditions guaranteed by
3498                // `validate_cast_and_convert_metadata`.
3499                assert!(resulting_size <= bytes_len, "{}", debug_str);
3500                match cast_type {
3501                    CastType::Prefix => {
3502                        assert_eq!(addr % align, 0, "{}", debug_str);
3503                        assert_eq!(resulting_size, split_at, "{}", debug_str);
3504                    }
3505                    CastType::Suffix => {
3506                        assert_eq!(split_at, bytes_len - resulting_size, "{}", debug_str);
3507                        assert_eq!((addr + split_at) % align, 0, "{}", debug_str);
3508                    }
3509                }
3510            } else {
3511                let min_size = match layout.size_info {
3512                    SizeInfo::Sized { size } => size,
3513                    SizeInfo::SliceDst(TrailingSliceLayout {
3514                        size_base,
3515                        size_rounding_align_and_phase,
3516                        ..
3517                    }) => {
3518                        let (size_align, size_phase) = size_rounding_align_and_phase.components();
3519                        size_base + size_phase + util::padding_needed_for(size_phase, size_align)
3520                    }
3521                };
3522
3523                // If a cast is invalid, it is either because...
3524                // 1. there are insufficient bytes at the given region for type:
3525                let insufficient_bytes = bytes_len < min_size;
3526                // 2. performing the cast would misalign type:
3527                let base = match cast_type {
3528                    CastType::Prefix => 0,
3529                    CastType::Suffix => bytes_len,
3530                };
3531                let misaligned = (base + addr) % layout.align != 0;
3532
3533                assert!(insufficient_bytes || misaligned);
3534            }
3535        }
3536
3537        let sizes = 0..8;
3538        let elem_sizes = 1..8;
3539        let size_infos = sizes
3540            .clone()
3541            .map(Into::<SizeInfo>::into)
3542            .chain(itertools::iproduct!(sizes, elem_sizes).map(Into::<SizeInfo>::into));
3543        let layouts = itertools::iproduct!(size_infos, [1, 2, 4, 8, 16, 32])
3544                .filter(|(size_info, align)| !matches!(size_info, SizeInfo::Sized { size } if size % align != 0))
3545                .map(|(size_info, align)| layout(size_info, align));
3546        itertools::iproduct!(layouts, 0..8, 0..8, [CastType::Prefix, CastType::Suffix])
3547            .for_each(validate_behavior);
3548
3549        // Exercise a normalized formula which cannot be represented as
3550        // `round_up(offset + elems * elem_size, align)`: the physical trailing
3551        // slice begins at offset 7, while an inner aligned DST contributes the
3552        // size formula `2 + round_up(5 + elems, 4)` to a packed outer type.
3553        let nested_packed = DstLayout {
3554            align: NonZeroUsize::new(2).unwrap(),
3555            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
3556                offset: 7,
3557                elem_size: 1,
3558                size_base: 6,
3559                size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3560                    NonZeroUsize::new(4).unwrap(),
3561                    1,
3562                ),
3563            }),
3564            statically_shallow_unpadded: false,
3565        };
3566        itertools::iproduct!([nested_packed], 0..8, 0..24, [CastType::Prefix, CastType::Suffix])
3567            .for_each(validate_behavior);
3568
3569        assert!(matches!(
3570            nested_packed.validate_cast_and_convert_metadata(0, 8, CastType::Prefix),
3571            Err(MetadataCastError::Size)
3572        ));
3573        assert!(matches!(
3574            nested_packed.validate_cast_and_convert_metadata(0, 10, CastType::Prefix),
3575            Ok((3, 10))
3576        ));
3577    }
3578
3579    #[test]
3580    #[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
3581    fn test_validate_rust_layout() {
3582        use core::{
3583            convert::TryInto as _,
3584            ptr::{self, NonNull},
3585        };
3586
3587        use crate::util::testutil::*;
3588
3589        // This test synthesizes pointers with various metadata and uses Rust's
3590        // built-in APIs to confirm that Rust makes decisions about type layout
3591        // which are consistent with what we believe is guaranteed by the
3592        // language. If this test fails, it doesn't just mean our code is wrong
3593        // - it means we're misunderstanding the language's guarantees.
3594
3595        #[derive(Debug)]
3596        struct MacroArgs {
3597            offset: usize,
3598            align: NonZeroUsize,
3599            elem_size: Option<usize>,
3600        }
3601
3602        /// # Safety
3603        ///
3604        /// `test` promises to only call `addr_of_slice_field` on a `NonNull<T>`
3605        /// which points to a valid `T`.
3606        ///
3607        /// `with_elems` must produce a pointer which points to a valid `T`.
3608        fn test<T: ?Sized, W: Fn(usize) -> NonNull<T>>(
3609            args: MacroArgs,
3610            with_elems: W,
3611            addr_of_slice_field: Option<fn(NonNull<T>) -> NonNull<u8>>,
3612        ) {
3613            let dst = args.elem_size.is_some();
3614            let layout = {
3615                let size_info = match args.elem_size {
3616                    Some(elem_size) => SizeInfo::SliceDst(TrailingSliceLayout {
3617                        offset: args.offset,
3618                        elem_size,
3619                        size_base: util::round_down_to_next_multiple_of_alignment(
3620                            args.offset,
3621                            args.align,
3622                        ),
3623                        size_rounding_align_and_phase: RoundingAlignAndPhase::new(
3624                            args.align,
3625                            args.offset % args.align,
3626                        ),
3627                    }),
3628                    None => SizeInfo::Sized {
3629                        // Rust only supports types whose sizes are a multiple
3630                        // of their alignment. If the macro created a type like
3631                        // this:
3632                        //
3633                        //   #[repr(C, align(2))]
3634                        //   struct Foo([u8; 1]);
3635                        //
3636                        // ...then Rust will automatically round the type's size
3637                        // up to 2.
3638                        size: args.offset + util::padding_needed_for(args.offset, args.align),
3639                    },
3640                };
3641                DstLayout { size_info, align: args.align, statically_shallow_unpadded: false }
3642            };
3643
3644            for elems in 0..128 {
3645                let ptr = with_elems(elems);
3646
3647                if let Some(addr_of_slice_field) = addr_of_slice_field {
3648                    let slc_field_ptr = addr_of_slice_field(ptr).as_ptr();
3649                    // SAFETY: Both `slc_field_ptr` and `ptr` are pointers to
3650                    // the same valid Rust object.
3651                    // Work around https://github.com/rust-lang/rust-clippy/issues/12280
3652                    let offset: usize =
3653                        unsafe { slc_field_ptr.byte_offset_from(ptr.as_ptr()).try_into().unwrap() };
3654                    assert_eq!(offset, args.offset);
3655                }
3656
3657                // SAFETY: `ptr` points to a valid `T`.
3658                #[allow(clippy::multiple_unsafe_ops_per_block)]
3659                let (size, align) = unsafe {
3660                    (mem::size_of_val_raw(ptr.as_ptr()), mem::align_of_val_raw(ptr.as_ptr()))
3661                };
3662
3663                // Avoid expensive allocation when running under Miri.
3664                let assert_msg = if !cfg!(miri) {
3665                    format!("\n{:?}\nsize:{}, align:{}", args, size, align)
3666                } else {
3667                    String::new()
3668                };
3669
3670                let without_padding =
3671                    args.offset + args.elem_size.map(|elem_size| elems * elem_size).unwrap_or(0);
3672                assert!(size >= without_padding, "{}", assert_msg);
3673                assert_eq!(align, args.align.get(), "{}", assert_msg);
3674
3675                // This encodes the most important part of the test: our
3676                // understanding of how Rust determines the layout of repr(C)
3677                // types. Sized repr(C) types are trivial, but DST types have
3678                // some subtlety. Note that:
3679                // - For sized types, `without_padding` is just the size of the
3680                //   type that we constructed for `Foo`. Since we may have
3681                //   requested a larger alignment, `Foo` may actually be larger
3682                //   than this, hence `padding_needed_for`.
3683                // - For unsized types, `without_padding` is dynamically
3684                //   computed from the offset, the element size, and element
3685                //   count. We expect that the size of the object should be
3686                //   `offset + elem_size * elems` rounded up to the next
3687                //   alignment.
3688                let expected_size =
3689                    without_padding + util::padding_needed_for(without_padding, args.align);
3690                assert_eq!(expected_size, size, "{}", assert_msg);
3691
3692                // For zero-sized element types,
3693                // `validate_cast_and_convert_metadata` just panics, so we skip
3694                // testing those types.
3695                if args.elem_size.map(|elem_size| elem_size > 0).unwrap_or(true) {
3696                    let addr = ptr.addr().get();
3697                    let (got_elems, got_split_at) = layout
3698                        .validate_cast_and_convert_metadata(addr, size, CastType::Prefix)
3699                        .unwrap();
3700                    // Avoid expensive allocation when running under Miri.
3701                    let assert_msg = if !cfg!(miri) {
3702                        format!(
3703                            "{}\nvalidate_cast_and_convert_metadata({}, {})",
3704                            assert_msg, addr, size,
3705                        )
3706                    } else {
3707                        String::new()
3708                    };
3709                    assert_eq!(got_split_at, size, "{}", assert_msg);
3710                    if dst {
3711                        assert!(got_elems >= elems, "{}", assert_msg);
3712                        if got_elems != elems {
3713                            // If `validate_cast_and_convert_metadata`
3714                            // returned more elements than `elems`, that
3715                            // means that `elems` is not the maximum number
3716                            // of elements that can fit in `size` - in other
3717                            // words, there is enough padding at the end of
3718                            // the value to fit at least one more element.
3719                            // If we use this metadata to synthesize a
3720                            // pointer, despite having a different element
3721                            // count, we still expect it to have the same
3722                            // size.
3723                            let got_ptr = with_elems(got_elems);
3724                            // SAFETY: `got_ptr` is a pointer to a valid `T`.
3725                            let size_of_got_ptr = unsafe { mem::size_of_val_raw(got_ptr.as_ptr()) };
3726                            assert_eq!(size_of_got_ptr, size, "{}", assert_msg);
3727                        }
3728                    } else {
3729                        // For sized casts, the returned element value is
3730                        // technically meaningless, and we don't guarantee any
3731                        // particular value. In practice, it's always zero.
3732                        assert_eq!(got_elems, 0, "{}", assert_msg)
3733                    }
3734                }
3735            }
3736        }
3737
3738        macro_rules! validate_against_rust {
3739                ($offset:literal, $align:literal $(, $elem_size:literal)?) => {{
3740                    #[repr(C, align($align))]
3741                    struct Foo([u8; $offset]$(, [[u8; $elem_size]])?);
3742
3743                    let args = MacroArgs {
3744                        offset: $offset,
3745                        align: $align.try_into().unwrap(),
3746                        elem_size: {
3747                            #[allow(unused)]
3748                            let ret = None::<usize>;
3749                            $(let ret = Some($elem_size);)?
3750                            ret
3751                        }
3752                    };
3753
3754                    #[repr(C, align($align))]
3755                    struct FooAlign;
3756                    // Create an aligned buffer to use in order to synthesize
3757                    // pointers to `Foo`. We don't ever load values from these
3758                    // pointers - we just do arithmetic on them - so having a "real"
3759                    // block of memory as opposed to a validly-aligned-but-dangling
3760                    // pointer is only necessary to make Miri happy since we run it
3761                    // with "strict provenance" checking enabled.
3762                    let aligned_buf = Align::<_, FooAlign>::new([0u8; 1024]);
3763                    let with_elems = |elems| {
3764                        let slc = NonNull::slice_from_raw_parts(NonNull::from(&aligned_buf.t), elems);
3765                        #[allow(clippy::as_conversions)]
3766                        NonNull::new(slc.as_ptr() as *mut Foo).unwrap()
3767                    };
3768                    let addr_of_slice_field = {
3769                        #[allow(unused)]
3770                        let f = None::<fn(NonNull<Foo>) -> NonNull<u8>>;
3771                        $(
3772                            // SAFETY: `test` promises to only call `f` with a `ptr`
3773                            // to a valid `Foo`.
3774                            let f: Option<fn(NonNull<Foo>) -> NonNull<u8>> = Some(|ptr: NonNull<Foo>| unsafe {
3775                                NonNull::new(ptr::addr_of_mut!((*ptr.as_ptr()).1)).unwrap().cast::<u8>()
3776                            });
3777                            let _ = $elem_size;
3778                        )?
3779                        f
3780                    };
3781
3782                    test::<Foo, _>(args, with_elems, addr_of_slice_field);
3783                }};
3784            }
3785
3786        // Every permutation of:
3787        // - offset in [0, 4]
3788        // - align in [1, 16]
3789        // - elem_size in [0, 4] (plus no elem_size)
3790        validate_against_rust!(0, 1);
3791        validate_against_rust!(0, 1, 0);
3792        validate_against_rust!(0, 1, 1);
3793        validate_against_rust!(0, 1, 2);
3794        validate_against_rust!(0, 1, 3);
3795        validate_against_rust!(0, 1, 4);
3796        validate_against_rust!(0, 2);
3797        validate_against_rust!(0, 2, 0);
3798        validate_against_rust!(0, 2, 1);
3799        validate_against_rust!(0, 2, 2);
3800        validate_against_rust!(0, 2, 3);
3801        validate_against_rust!(0, 2, 4);
3802        validate_against_rust!(0, 4);
3803        validate_against_rust!(0, 4, 0);
3804        validate_against_rust!(0, 4, 1);
3805        validate_against_rust!(0, 4, 2);
3806        validate_against_rust!(0, 4, 3);
3807        validate_against_rust!(0, 4, 4);
3808        validate_against_rust!(0, 8);
3809        validate_against_rust!(0, 8, 0);
3810        validate_against_rust!(0, 8, 1);
3811        validate_against_rust!(0, 8, 2);
3812        validate_against_rust!(0, 8, 3);
3813        validate_against_rust!(0, 8, 4);
3814        validate_against_rust!(0, 16);
3815        validate_against_rust!(0, 16, 0);
3816        validate_against_rust!(0, 16, 1);
3817        validate_against_rust!(0, 16, 2);
3818        validate_against_rust!(0, 16, 3);
3819        validate_against_rust!(0, 16, 4);
3820        validate_against_rust!(1, 1);
3821        validate_against_rust!(1, 1, 0);
3822        validate_against_rust!(1, 1, 1);
3823        validate_against_rust!(1, 1, 2);
3824        validate_against_rust!(1, 1, 3);
3825        validate_against_rust!(1, 1, 4);
3826        validate_against_rust!(1, 2);
3827        validate_against_rust!(1, 2, 0);
3828        validate_against_rust!(1, 2, 1);
3829        validate_against_rust!(1, 2, 2);
3830        validate_against_rust!(1, 2, 3);
3831        validate_against_rust!(1, 2, 4);
3832        validate_against_rust!(1, 4);
3833        validate_against_rust!(1, 4, 0);
3834        validate_against_rust!(1, 4, 1);
3835        validate_against_rust!(1, 4, 2);
3836        validate_against_rust!(1, 4, 3);
3837        validate_against_rust!(1, 4, 4);
3838        validate_against_rust!(1, 8);
3839        validate_against_rust!(1, 8, 0);
3840        validate_against_rust!(1, 8, 1);
3841        validate_against_rust!(1, 8, 2);
3842        validate_against_rust!(1, 8, 3);
3843        validate_against_rust!(1, 8, 4);
3844        validate_against_rust!(1, 16);
3845        validate_against_rust!(1, 16, 0);
3846        validate_against_rust!(1, 16, 1);
3847        validate_against_rust!(1, 16, 2);
3848        validate_against_rust!(1, 16, 3);
3849        validate_against_rust!(1, 16, 4);
3850        validate_against_rust!(2, 1);
3851        validate_against_rust!(2, 1, 0);
3852        validate_against_rust!(2, 1, 1);
3853        validate_against_rust!(2, 1, 2);
3854        validate_against_rust!(2, 1, 3);
3855        validate_against_rust!(2, 1, 4);
3856        validate_against_rust!(2, 2);
3857        validate_against_rust!(2, 2, 0);
3858        validate_against_rust!(2, 2, 1);
3859        validate_against_rust!(2, 2, 2);
3860        validate_against_rust!(2, 2, 3);
3861        validate_against_rust!(2, 2, 4);
3862        validate_against_rust!(2, 4);
3863        validate_against_rust!(2, 4, 0);
3864        validate_against_rust!(2, 4, 1);
3865        validate_against_rust!(2, 4, 2);
3866        validate_against_rust!(2, 4, 3);
3867        validate_against_rust!(2, 4, 4);
3868        validate_against_rust!(2, 8);
3869        validate_against_rust!(2, 8, 0);
3870        validate_against_rust!(2, 8, 1);
3871        validate_against_rust!(2, 8, 2);
3872        validate_against_rust!(2, 8, 3);
3873        validate_against_rust!(2, 8, 4);
3874        validate_against_rust!(2, 16);
3875        validate_against_rust!(2, 16, 0);
3876        validate_against_rust!(2, 16, 1);
3877        validate_against_rust!(2, 16, 2);
3878        validate_against_rust!(2, 16, 3);
3879        validate_against_rust!(2, 16, 4);
3880        validate_against_rust!(3, 1);
3881        validate_against_rust!(3, 1, 0);
3882        validate_against_rust!(3, 1, 1);
3883        validate_against_rust!(3, 1, 2);
3884        validate_against_rust!(3, 1, 3);
3885        validate_against_rust!(3, 1, 4);
3886        validate_against_rust!(3, 2);
3887        validate_against_rust!(3, 2, 0);
3888        validate_against_rust!(3, 2, 1);
3889        validate_against_rust!(3, 2, 2);
3890        validate_against_rust!(3, 2, 3);
3891        validate_against_rust!(3, 2, 4);
3892        validate_against_rust!(3, 4);
3893        validate_against_rust!(3, 4, 0);
3894        validate_against_rust!(3, 4, 1);
3895        validate_against_rust!(3, 4, 2);
3896        validate_against_rust!(3, 4, 3);
3897        validate_against_rust!(3, 4, 4);
3898        validate_against_rust!(3, 8);
3899        validate_against_rust!(3, 8, 0);
3900        validate_against_rust!(3, 8, 1);
3901        validate_against_rust!(3, 8, 2);
3902        validate_against_rust!(3, 8, 3);
3903        validate_against_rust!(3, 8, 4);
3904        validate_against_rust!(3, 16);
3905        validate_against_rust!(3, 16, 0);
3906        validate_against_rust!(3, 16, 1);
3907        validate_against_rust!(3, 16, 2);
3908        validate_against_rust!(3, 16, 3);
3909        validate_against_rust!(3, 16, 4);
3910        validate_against_rust!(4, 1);
3911        validate_against_rust!(4, 1, 0);
3912        validate_against_rust!(4, 1, 1);
3913        validate_against_rust!(4, 1, 2);
3914        validate_against_rust!(4, 1, 3);
3915        validate_against_rust!(4, 1, 4);
3916        validate_against_rust!(4, 2);
3917        validate_against_rust!(4, 2, 0);
3918        validate_against_rust!(4, 2, 1);
3919        validate_against_rust!(4, 2, 2);
3920        validate_against_rust!(4, 2, 3);
3921        validate_against_rust!(4, 2, 4);
3922        validate_against_rust!(4, 4);
3923        validate_against_rust!(4, 4, 0);
3924        validate_against_rust!(4, 4, 1);
3925        validate_against_rust!(4, 4, 2);
3926        validate_against_rust!(4, 4, 3);
3927        validate_against_rust!(4, 4, 4);
3928        validate_against_rust!(4, 8);
3929        validate_against_rust!(4, 8, 0);
3930        validate_against_rust!(4, 8, 1);
3931        validate_against_rust!(4, 8, 2);
3932        validate_against_rust!(4, 8, 3);
3933        validate_against_rust!(4, 8, 4);
3934        validate_against_rust!(4, 16);
3935        validate_against_rust!(4, 16, 0);
3936        validate_against_rust!(4, 16, 1);
3937        validate_against_rust!(4, 16, 2);
3938        validate_against_rust!(4, 16, 3);
3939        validate_against_rust!(4, 16, 4);
3940    }
3941}
3942
3943#[cfg(kani)]
3944mod proofs {
3945    //! Arithmetic models and input generators for the layout proofs.
3946    //!
3947    //! The contract models specify expected results without calling the
3948    //! operation being checked. The composition models (`extended_layout`,
3949    //! `padded_layout`, and `repr_c_layout`) also define the contracts' input
3950    //! domains: `Some` supplies an expected layout, while `None` excludes an
3951    //! input from the corresponding non-panicking contract. These models check
3952    //! representability in `usize`, without imposing Rust's `isize::MAX` bound.
3953
3954    use core::alloc::Layout;
3955
3956    use super::*;
3957
3958    /// Evaluates the normalized object size for a trailing slice of
3959    /// `trailing_size` bytes:
3960    ///
3961    /// ```text
3962    /// size_base + round_up(size_phase + trailing_size, size_align)
3963    /// ```
3964    ///
3965    /// Here, `size_align` and `size_phase` come from `layout.size_rounding_align_and_phase`.
3966    /// `round_up` rounds its input up to a multiple of its alignment. Returns
3967    /// `None` if the size is not representable in `usize`.
3968    ///
3969    /// Using byte counts separates rounding from element-count multiplication
3970    /// and permits proofs over byte counts that are not whole elements.
3971    pub(super) fn slice_dst_size_for_trailing_bytes<ElementSize>(
3972        layout: TrailingSliceLayout<ElementSize>,
3973        trailing_size: usize,
3974    ) -> Option<usize> {
3975        let without_padding =
3976            layout.size_rounding_align_and_phase.components().1.checked_add(trailing_size)?;
3977        let padding =
3978            util::padding_needed_for(without_padding, layout.size_rounding_align_and_phase.align());
3979        let rounded = without_padding.checked_add(padding)?;
3980        layout.size_base.checked_add(rounded)
3981    }
3982
3983    /// Returns the least multiple of `align` greater than or equal to `bytes`,
3984    /// or `None` if that multiple exceeds `usize::MAX`.
3985    ///
3986    /// Requires a power-of-two `align`. Composition models use this checked
3987    /// operation to identify inputs for which adding padding would overflow.
3988    fn round_up(bytes: usize, align: NonZeroUsize) -> Option<usize> {
3989        bytes.checked_add(util::padding_needed_for(bytes, align))
3990    }
3991
3992    /// Models the largest trailing-slice byte count whose object size fits in
3993    /// `available` bytes. Returns `None` if even an empty trailing slice cannot
3994    /// fit. The result need not be a multiple of the element size.
3995    ///
3996    /// Subtracting `size_base` leaves the budget for the rounded contribution.
3997    /// Rounding that budget down to `size_align` and subtracting `size_phase`
3998    /// gives the largest byte count whose `slice_dst_size_for_trailing_bytes`
3999    /// result is at most `available`. Metadata and casting contracts use
4000    /// this bound without calling the production capacity calculation.
4001    pub(super) fn max_trailing_bytes(
4002        trailing: TrailingSliceLayout,
4003        available: usize,
4004    ) -> Option<usize> {
4005        let (align, phase) = trailing.size_rounding_align_and_phase.components();
4006        let capacity = available.checked_sub(trailing.size_base)?;
4007        util::round_down_to_next_multiple_of_alignment(capacity, align).checked_sub(phase)
4008    }
4009
4010    /// Models appending `field` to a sized `prefix` for `DstLayout::extend`.
4011    ///
4012    /// Places the field at the first offset after the prefix that satisfies the
4013    /// field's alignment, capped by `packed` when present. The resulting
4014    /// alignment is the maximum of the prefix's alignment and this effective
4015    /// field alignment. A DST field retains its inner rounding operation, with
4016    /// its slice offset and size base shifted by the field's placement.
4017    /// Trailing padding for the enclosing layout is left to `padded_layout`.
4018    ///
4019    /// Returns `None` for a non-power-of-two alignment, an unsized prefix, or
4020    /// arithmetic overflow. The `statically_shallow_unpadded` flag is true only
4021    /// if both inputs have it set and field placement introduces no gap.
4022    pub(super) fn extended_layout(
4023        prefix: DstLayout,
4024        field: DstLayout,
4025        packed: Option<NonZeroUsize>,
4026    ) -> Option<DstLayout> {
4027        if !prefix.align.is_power_of_two()
4028            || !field.align.is_power_of_two()
4029            || packed.map_or(false, |align| !align.is_power_of_two())
4030        {
4031            return None;
4032        }
4033        let SizeInfo::Sized { size: prefix_size } = prefix.size_info else { return None };
4034        let field_align = field.align.min(packed.unwrap_or(DstLayout::THEORETICAL_MAX_ALIGN));
4035        let field_offset = round_up(prefix_size, field_align)?;
4036        let size_info = match field.size_info {
4037            SizeInfo::Sized { size } => SizeInfo::Sized { size: field_offset.checked_add(size)? },
4038            SizeInfo::SliceDst(trailing) => SizeInfo::SliceDst(TrailingSliceLayout {
4039                offset: field_offset.checked_add(trailing.offset)?,
4040                size_base: field_offset.checked_add(trailing.size_base)?,
4041                ..trailing
4042            }),
4043        };
4044        Some(DstLayout {
4045            align: prefix.align.max(field_align),
4046            size_info,
4047            statically_shallow_unpadded: prefix.statically_shallow_unpadded
4048                && field.statically_shallow_unpadded
4049                && field_offset == prefix_size,
4050        })
4051    }
4052
4053    /// Models adding trailing padding for `DstLayout::pad_to_align`.
4054    ///
4055    /// A sized layout rounds its size up to a multiple of `layout.align`. A DST
4056    /// instead composes that rounding with its existing size expression:
4057    ///
4058    /// ```text
4059    /// round_up(size_base + round_up(size_phase + trailing_size, size_align),
4060    ///          layout.align)
4061    /// ```
4062    ///
4063    /// Here, `size_align` and `size_phase` come from the DST's
4064    /// `size_rounding_align_and_phase`. The result encodes this expression with
4065    /// one base, phase, and rounding alignment, preserving the physical slice
4066    /// offset and element size. The `statically_shallow_unpadded` flag is
4067    /// cleared only when a sized layout gains padding; DST padding is accounted
4068    /// for by the size expression.
4069    ///
4070    /// Returns `None` if `layout.align` is not a power of two or a checked
4071    /// intermediate overflows. The contract compares this expected encoding
4072    /// with the production result; the separate padding formula proofs check
4073    /// that the encoding preserves the rounded size.
4074    pub(super) fn padded_layout(layout: DstLayout) -> Option<DstLayout> {
4075        if !layout.align.is_power_of_two() {
4076            return None;
4077        }
4078        let (size_info, no_static_padding) = match layout.size_info {
4079            SizeInfo::Sized { size } => {
4080                let rounded = round_up(size, layout.align)?;
4081                (SizeInfo::Sized { size: rounded }, rounded == size)
4082            }
4083            SizeInfo::SliceDst(trailing) => {
4084                let (inner_align, phase) = trailing.size_rounding_align_and_phase.components();
4085                let (size_base, size_rounding_align_and_phase) = if inner_align >= layout.align {
4086                    (
4087                        round_up(trailing.size_base, layout.align)?,
4088                        trailing.size_rounding_align_and_phase,
4089                    )
4090                } else {
4091                    let fixed = round_up(trailing.size_base, inner_align)?.checked_add(phase)?;
4092                    let new_phase = fixed & (layout.align.get() - 1);
4093                    (fixed - new_phase, RoundingAlignAndPhase::new(layout.align, new_phase))
4094                };
4095                (
4096                    SizeInfo::SliceDst(TrailingSliceLayout {
4097                        size_base,
4098                        size_rounding_align_and_phase,
4099                        ..trailing
4100                    }),
4101                    true,
4102                )
4103            }
4104        };
4105        Some(DstLayout {
4106            size_info,
4107            statically_shallow_unpadded: layout.statically_shallow_unpadded && no_static_padding,
4108            ..layout
4109        })
4110    }
4111
4112    /// Models a complete layout for `DstLayout::for_repr_c_struct` by composing
4113    /// the field-placement and trailing-padding specifications.
4114    ///
4115    /// Starts with an empty prefix of alignment `align`, or one if absent.
4116    /// Appends fields in order, with `packed` as an optional cap on field
4117    /// alignment; the final layout is padded to its resulting alignment. Only
4118    /// the last field may be unsized, since field extension requires a sized
4119    /// prefix.
4120    ///
4121    /// Returns `None` if the initial alignment is not a power of two or any
4122    /// field extension or final padding step returns `None`.
4123    pub(super) fn repr_c_layout(
4124        align: Option<NonZeroUsize>,
4125        packed: Option<NonZeroUsize>,
4126        fields: &[DstLayout],
4127    ) -> Option<DstLayout> {
4128        let align = align.unwrap_or(DstLayout::MIN_ALIGN);
4129        if !align.is_power_of_two() {
4130            return None;
4131        }
4132        let mut layout = DstLayout {
4133            align,
4134            size_info: SizeInfo::Sized { size: 0 },
4135            statically_shallow_unpadded: true,
4136        };
4137        for &field in fields {
4138            layout = extended_layout(layout, field, packed)?;
4139        }
4140        padded_layout(layout)
4141    }
4142
4143    // The inline contract harnesses use unrestricted `Arbitrary` values.
4144    // These generators retain the narrower domains of the Rust-layout proofs.
4145
4146    /// Generates any power-of-two alignment representable in `usize`, including
4147    /// `DstLayout::THEORETICAL_MAX_ALIGN`. This restricts the alignment alone;
4148    /// callers must impose any constraints relating it to a layout's size.
4149    fn any_layout_align() -> NonZeroUsize {
4150        let exponent: u8 = kani::any();
4151        kani::assume(usize::from(exponent) < POINTER_WIDTH_BITS);
4152
4153        // `exponent < POINTER_WIDTH_BITS`, so the shift is in range and its
4154        // result is non-zero. This construction is surjective over every
4155        // power-of-two alignment up to and including
4156        // `THEORETICAL_MAX_ALIGN`. `Layout` admits that maximum alignment for
4157        // zero-sized layouts, so the validity assumptions below—not this
4158        // generator—filter combinations that cannot describe Rust types.
4159        NonZeroUsize::new(1usize << exponent).unwrap()
4160    }
4161
4162    /// Generates layouts satisfying the arithmetic restrictions used by the
4163    /// standalone Rust-layout proofs.
4164    ///
4165    /// The fixed size, or the zero-element DST size, must be accepted by
4166    /// `Layout::from_size_align`. For a DST, `size_base + size_phase` must also
4167    /// be representable and at least the physical slice offset. Component
4168    /// bounds come from `any_bounded_size_info`; `statically_shallow_unpadded`
4169    /// remains arbitrary. These conditions do not establish that a Rust type
4170    /// with this layout exists.
4171    fn any_valid_dst_layout() -> DstLayout {
4172        let align = any_layout_align();
4173        let size_info = any_bounded_size_info();
4174
4175        kani::assume(
4176            match size_info {
4177                SizeInfo::Sized { size } => Layout::from_size_align(size, align.get()),
4178                SizeInfo::SliceDst(trailing) => {
4179                    // `SliceDst` cannot encode one exact size. Validate its
4180                    // minimum size and the invariants required of the size
4181                    // formula for layouts of real Rust types.
4182                    let min_size = trailing.size_for_elems(0);
4183                    let unrounded_min_size = trailing
4184                        .size_base
4185                        .checked_add(trailing.size_rounding_align_and_phase.components().1);
4186
4187                    kani::assume(matches!(
4188                        unrounded_min_size,
4189                        Some(size) if size >= trailing.offset
4190                    ));
4191
4192                    match min_size {
4193                        Some(min_size) => Layout::from_size_align(min_size, align.get()),
4194                        None => Layout::from_size_align(usize::MAX, align.get()),
4195                    }
4196                }
4197            }
4198            .is_ok(),
4199        );
4200
4201        DstLayout { align: align, size_info: size_info, statically_shallow_unpadded: kani::any() }
4202    }
4203
4204    /// Generates either a fixed size at most `DstLayout::MAX_SIZE`, or a DST
4205    /// description from `any_bounded_trailing_layout`.
4206    ///
4207    /// Supplies bounded size components to `any_valid_dst_layout`, which adds
4208    /// constraints involving the enclosing layout's alignment.
4209    fn any_bounded_size_info() -> SizeInfo {
4210        let is_sized: bool = kani::any();
4211
4212        match is_sized {
4213            true => {
4214                let size: usize = kani::any();
4215
4216                kani::assume(size <= DstLayout::MAX_SIZE);
4217
4218                SizeInfo::Sized { size }
4219            }
4220            false => SizeInfo::SliceDst(any_bounded_trailing_layout()),
4221        }
4222    }
4223
4224    /// Generates a trailing-slice description whose element size, slice offset,
4225    /// and size base are each at most `DstLayout::MAX_SIZE` (`isize::MAX`).
4226    ///
4227    /// The rounding alignment spans all representable powers of two, and the
4228    /// phase spans all values below that alignment. No further relationships
4229    /// between these components are imposed: their combined size may overflow
4230    /// or fail to contain the slice. Standalone proofs add the relationships
4231    /// they require; contract harnesses use unrestricted `Arbitrary` values
4232    /// instead.
4233    fn any_bounded_trailing_layout() -> TrailingSliceLayout {
4234        let elem_size: usize = kani::any();
4235        let offset: usize = kani::any();
4236        let size_base: usize = kani::any();
4237        let size_align = any_layout_align();
4238        let raw_size_phase: usize = kani::any();
4239        // Since `size_align` is a power of two, masking is surjective over
4240        // precisely the values in `0..size_align`.
4241        #[allow(clippy::arithmetic_side_effects)]
4242        let size_phase = raw_size_phase & (size_align.get() - 1);
4243
4244        kani::assume(elem_size <= DstLayout::MAX_SIZE);
4245        kani::assume(offset <= DstLayout::MAX_SIZE);
4246        kani::assume(size_base <= DstLayout::MAX_SIZE);
4247
4248        TrailingSliceLayout {
4249            elem_size,
4250            offset,
4251            size_base,
4252            size_rounding_align_and_phase: RoundingAlignAndPhase::new(size_align, size_phase),
4253        }
4254    }
4255
4256    #[cfg(feature = "derive")]
4257    #[kani::proof]
4258    fn prove_padding_for_elems_for_rust_layouts() {
4259        // For each fixture, check every element count whose complete size
4260        // fits in `usize`, including all counts for zero-sized elements.
4261        padding_testutil::check_layouts(kani::any());
4262    }
4263
4264    #[cfg(kani_slow)]
4265    #[kani::proof]
4266    #[kani::solver(kissat)]
4267    fn prove_padding_for_elems() {
4268        // Check the modular result for every field value and element count,
4269        // including layouts that cannot describe a Rust type.
4270        let layout = TrailingSliceLayout {
4271            offset: kani::any(),
4272            elem_size: kani::any(),
4273            size_base: kani::any(),
4274            size_rounding_align_and_phase: RoundingAlignAndPhase(kani::any()),
4275        };
4276        let elems: usize = kani::any();
4277        let (size_align, size_phase) = layout.size_rounding_align_and_phase.components();
4278        let mask = size_align.get() - 1;
4279
4280        // Evaluate the complete size and trailing-slice end separately in
4281        // modular arithmetic, without reducing the trailing bytes first or
4282        // canceling them from the difference.
4283        let trailing_bytes = elems.wrapping_mul(layout.elem_size);
4284        let unrounded = size_phase.wrapping_add(trailing_bytes);
4285        let rounded = unrounded.wrapping_add(mask) & !mask;
4286        let object_size = layout.size_base.wrapping_add(rounded);
4287        let trailing_end = layout.offset.wrapping_add(trailing_bytes);
4288        assert_eq!(layout.padding_for_elems(elems), object_size.wrapping_sub(trailing_end));
4289    }
4290
4291    #[kani::proof]
4292    fn prove_size_rounding_align_and_phase_encoding() {
4293        let encoded: usize = kani::any();
4294        kani::assume(encoded != 0);
4295        let encoded = NonZeroUsize::new(encoded).unwrap();
4296        let rounding = RoundingAlignAndPhase(encoded);
4297
4298        let align = rounding.align();
4299        let phase = rounding.components().1;
4300        assert!(align.is_power_of_two());
4301        assert!(phase < align.get());
4302        assert_eq!(RoundingAlignAndPhase::new(align, phase), rounding);
4303    }
4304
4305    #[kani::proof]
4306    fn prove_size_rounding_align_and_phase_decoding() {
4307        let align: NonZeroUsize = kani::any();
4308        let phase: usize = kani::any();
4309        kani::assume(align.is_power_of_two());
4310        kani::assume(phase < align.get());
4311
4312        let rounding = RoundingAlignAndPhase::new(align, phase);
4313        assert_eq!(rounding.components(), (align, phase));
4314    }
4315
4316    #[cfg(kani_slow)]
4317    #[kani::proof]
4318    fn prove_trailing_slice_layout_normal_form() {
4319        let rounding_word: usize = kani::any();
4320        kani::assume(rounding_word != 0);
4321        let align = RoundingAlignAndPhase(NonZeroUsize::new(rounding_word).unwrap()).align();
4322
4323        let raw_prefix: usize = kani::any();
4324        let offset: usize = kani::any();
4325        let trailing_size: usize = kani::any();
4326        #[allow(clippy::arithmetic_side_effects)]
4327        let prefix = raw_prefix & (align.get() - 1);
4328        #[allow(clippy::arithmetic_side_effects)]
4329        let phase = offset & (align.get() - 1);
4330        #[allow(clippy::arithmetic_side_effects)]
4331        let aligned_offset = offset - phase;
4332        let layout = TrailingSliceLayout {
4333            offset: 0,
4334            elem_size: 1,
4335            size_base: aligned_offset | prefix,
4336            size_rounding_align_and_phase: RoundingAlignAndPhase::new(align, phase),
4337        };
4338
4339        assert_eq!(layout.size_offset(), offset);
4340
4341        let old_size = offset
4342            .checked_add(trailing_size)
4343            .and_then(|without_padding| {
4344                without_padding.checked_add(util::padding_needed_for(without_padding, align))
4345            })
4346            .and_then(|rounded| prefix.checked_add(rounded));
4347        assert_eq!(layout.size_for_elems(trailing_size), old_size);
4348        assert_eq!(slice_dst_size_for_trailing_bytes(layout, trailing_size), old_size);
4349    }
4350
4351    #[cfg(kani_slow)]
4352    #[kani::proof]
4353    #[kani::solver(kissat)]
4354    fn prove_element_product_preserves_alignment() {
4355        let elem_size: usize = kani::any();
4356        let elems: usize = kani::any();
4357        let align = any_layout_align();
4358        #[allow(clippy::arithmetic_side_effects)]
4359        let align_mask = align.get() - 1;
4360
4361        kani::assume(elem_size & align_mask == 0);
4362        let Some(bytes) = elem_size.checked_mul(elems) else {
4363            kani::assume(false);
4364            loop {}
4365        };
4366        assert_eq!(bytes & align_mask, 0);
4367    }
4368
4369    #[kani::proof]
4370    fn prove_trailing_slice_layout_advance() {
4371        let layout: TrailingSliceLayout = any_bounded_trailing_layout();
4372        let bytes: usize = kani::any();
4373        let elem_size: usize = kani::any();
4374        let trailing_size: usize = kani::any();
4375
4376        let size_offset = layout.size_offset();
4377        let advanced_offset = size_offset.checked_add(bytes);
4378        let advanced = layout.advance(bytes, elem_size);
4379        assert_eq!(advanced.is_some(), advanced_offset.is_some());
4380        let Some(advanced_offset) = advanced_offset else {
4381            kani::assume(false);
4382            loop {}
4383        };
4384        let Some(advanced) = advanced else { unreachable!() };
4385
4386        assert_eq!(advanced.offset, layout.offset);
4387        assert_eq!(advanced.size_offset(), advanced_offset);
4388        assert_eq!(advanced.elem_size, elem_size);
4389        assert_eq!(
4390            advanced.size_rounding_align_and_phase.align(),
4391            layout.size_rounding_align_and_phase.align()
4392        );
4393        #[allow(clippy::arithmetic_side_effects)]
4394        let size_align_mask = advanced.size_rounding_align_and_phase.align().get() - 1;
4395        assert_eq!(advanced.size_base & size_align_mask, layout.size_base & size_align_mask);
4396
4397        let size_align = layout.size_rounding_align_and_phase.align();
4398        #[allow(clippy::arithmetic_side_effects)]
4399        let size_prefix = layout.size_base & (size_align.get() - 1);
4400        let expected_size = advanced_offset
4401            .checked_add(trailing_size)
4402            .and_then(|without_padding| {
4403                without_padding.checked_add(util::padding_needed_for(without_padding, size_align))
4404            })
4405            .and_then(|rounded| size_prefix.checked_add(rounded));
4406        assert_eq!(slice_dst_size_for_trailing_bytes(advanced, trailing_size), expected_size);
4407    }
4408
4409    #[cfg(kani_slow)]
4410    #[kani::proof]
4411    fn prove_has_same_size_sequence() {
4412        let left: TrailingSliceLayout = any_bounded_trailing_layout();
4413        let right: TrailingSliceLayout = any_bounded_trailing_layout();
4414        let trailing_size: usize = kani::any();
4415
4416        kani::assume(left.has_same_size_sequence(right));
4417        let left_align = left.size_rounding_align_and_phase.align().get();
4418        let right_align = right.size_rounding_align_and_phase.align().get();
4419        let max_align = if left_align > right_align { left_align } else { right_align };
4420        #[allow(clippy::arithmetic_side_effects)]
4421        let max_align_mask = max_align - 1;
4422        if left.elem_size & max_align_mask == 0 {
4423            // In this branch `has_same_size_sequence` relies on every actual
4424            // trailing byte count being a multiple of both alignments. Prove
4425            // the formula for that entire byte class, which includes every
4426            // representable `elems * elem_size`, without introducing a
4427            // nonlinear symbolic multiplication into the harness.
4428            kani::assume(trailing_size & max_align_mask == 0);
4429        }
4430        assert_eq!(
4431            slice_dst_size_for_trailing_bytes(left, trailing_size),
4432            slice_dst_size_for_trailing_bytes(right, trailing_size)
4433        );
4434    }
4435
4436    #[kani::proof]
4437    #[kani::solver(kissat)]
4438    fn prove_max_trailing_bytes() {
4439        let trailing: TrailingSliceLayout = any_bounded_trailing_layout();
4440        let available_bytes: usize = kani::any();
4441        let trailing_bytes: usize = kani::any();
4442        let fits = slice_dst_size_for_trailing_bytes(trailing, trailing_bytes)
4443            .map(|size| size <= available_bytes)
4444            .unwrap_or(false);
4445        match trailing.max_trailing_bytes(available_bytes) {
4446            Some(max_bytes) => assert_eq!(trailing_bytes <= max_bytes, fits),
4447            None => assert!(!fits),
4448        }
4449    }
4450
4451    #[kani::proof]
4452    #[kani::solver(kissat)]
4453    fn prove_max_trailing_bytes_components() {
4454        let trailing: TrailingSliceLayout = any_bounded_trailing_layout();
4455        let bytes_len: usize = kani::any();
4456        let Some(max_slice_bytes) = trailing.max_trailing_bytes(bytes_len) else {
4457            kani::assume(false);
4458            loop {}
4459        };
4460        let (size_align, size_phase) = trailing.size_rounding_align_and_phase.components();
4461        let Some(bytes_after_base) = bytes_len.checked_sub(trailing.size_base) else {
4462            unreachable!()
4463        };
4464        let max_rounded_bytes =
4465            util::round_down_to_next_multiple_of_alignment(bytes_after_base, size_align);
4466        assert_eq!(size_phase.checked_add(max_slice_bytes), Some(max_rounded_bytes));
4467        assert!(trailing.size_base.checked_add(max_rounded_bytes).unwrap() <= bytes_len);
4468    }
4469
4470    #[kani::proof]
4471    #[kani::solver(kissat)]
4472    fn prove_slice_dst_validator_selected_size_fits() {
4473        let size_align = any_layout_align();
4474        #[allow(clippy::arithmetic_side_effects)]
4475        let align_mask = size_align.get() - 1;
4476        let raw_phase: usize = kani::any();
4477        let size_phase = raw_phase & align_mask;
4478        let raw_capacity: usize = kani::any();
4479        let max_rounded_bytes = raw_capacity & !align_mask;
4480        let size_base: usize = kani::any();
4481        let bytes_len: usize = kani::any();
4482        let trailing_size: usize = kani::any();
4483
4484        // These are exactly the relations established for every successful
4485        // capacity query by `prove_max_trailing_bytes_components`. Isolate
4486        // them from the helper's implementation to keep this arithmetic
4487        // identity independent of how that helper computes its bound.
4488        let Some(max_slice_bytes) = max_rounded_bytes.checked_sub(size_phase) else {
4489            kani::assume(false);
4490            loop {}
4491        };
4492        let Some(max_object_size) = size_base.checked_add(max_rounded_bytes) else {
4493            kani::assume(false);
4494            loop {}
4495        };
4496        kani::assume(max_object_size <= bytes_len);
4497        kani::assume(trailing_size <= max_slice_bytes);
4498
4499        // For a whole-element count selected by the validator, this unused
4500        // byte count is also `max_slice_bytes % elem_size`. The identity here
4501        // holds for every byte count satisfying the capacity bound.
4502        #[allow(clippy::arithmetic_side_effects)]
4503        let unused_bytes = max_slice_bytes - trailing_size;
4504        let unused_aligned_bytes =
4505            util::round_down_to_next_multiple_of_alignment(unused_bytes, size_align);
4506        #[allow(clippy::arithmetic_side_effects)]
4507        let object_size = size_base + (max_rounded_bytes - unused_aligned_bytes);
4508
4509        let unrounded = size_phase.checked_add(trailing_size).unwrap();
4510        let rounded =
4511            unrounded.checked_add(util::padding_needed_for(unrounded, size_align)).unwrap();
4512        assert_eq!(Some(object_size), size_base.checked_add(rounded));
4513        assert!(object_size <= bytes_len);
4514    }
4515
4516    #[kani::proof]
4517    #[kani::solver(kissat)]
4518    fn prove_slice_dst_validator_rejects_larger_trailing_size() {
4519        let trailing: TrailingSliceLayout = any_bounded_trailing_layout();
4520        let bytes_len: usize = kani::any();
4521        let larger_trailing_size: usize = kani::any();
4522        let Some(max_slice_bytes) = trailing.max_trailing_bytes(bytes_len) else {
4523            kani::assume(false);
4524            loop {}
4525        };
4526        kani::assume(larger_trailing_size > max_slice_bytes);
4527        if let Some(object_size) = slice_dst_size_for_trailing_bytes(trailing, larger_trailing_size)
4528        {
4529            assert!(object_size > bytes_len);
4530        }
4531    }
4532
4533    #[kani::proof]
4534    fn prove_slice_formula_contains_trailing_slice() {
4535        let trailing: TrailingSliceLayout = any_bounded_trailing_layout();
4536        let trailing_size: usize = kani::any();
4537        let (_, size_phase) = trailing.size_rounding_align_and_phase.components();
4538
4539        let Some(unrounded_min) = trailing.size_base.checked_add(size_phase) else {
4540            kani::assume(false);
4541            loop {}
4542        };
4543        kani::assume(unrounded_min >= trailing.offset);
4544        let Some(object_size) = slice_dst_size_for_trailing_bytes(trailing, trailing_size) else {
4545            kani::assume(false);
4546            loop {}
4547        };
4548
4549        let Some(trailing_end) = trailing.offset.checked_add(trailing_size) else { unreachable!() };
4550        assert!(trailing_end <= object_size);
4551    }
4552
4553    #[kani::proof]
4554    fn prove_finalized_slice_formula_is_aligned() {
4555        let trailing: TrailingSliceLayout = any_bounded_trailing_layout();
4556        let outer_align = any_layout_align();
4557        let trailing_size: usize = kani::any();
4558        kani::assume(trailing.size_rounding_align_and_phase.align() >= outer_align);
4559        #[allow(clippy::arithmetic_side_effects)]
4560        let outer_align_mask = outer_align.get() - 1;
4561        kani::assume(trailing.size_base & outer_align_mask == 0);
4562        let Some(object_size) = slice_dst_size_for_trailing_bytes(trailing, trailing_size) else {
4563            kani::assume(false);
4564            loop {}
4565        };
4566
4567        assert_eq!(object_size & outer_align_mask, 0);
4568    }
4569
4570    #[kani::proof]
4571    fn prove_validator_checks_selected_endpoint_alignment() {
4572        let align = any_layout_align();
4573        let addr: usize = kani::any();
4574        let bytes_len: usize = kani::any();
4575        let Some(end) = addr.checked_add(bytes_len) else {
4576            kani::assume(false);
4577            loop {}
4578        };
4579        let suffix: bool = kani::any();
4580        let cast_type = if suffix { CastType::Suffix } else { CastType::Prefix };
4581        let selected_endpoint = if suffix { end } else { addr };
4582        #[allow(clippy::arithmetic_side_effects)]
4583        let align_mask = align.get() - 1;
4584
4585        // The endpoint check precedes and is independent of the size-info
4586        // branch. A zero-sized layout makes every aligned case succeed and
4587        // isolates that control flow, including prefix/suffix split selection.
4588        let layout = DstLayout {
4589            align,
4590            size_info: SizeInfo::Sized { size: 0 },
4591            statically_shallow_unpadded: false,
4592        };
4593        match layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type) {
4594            Ok((elems, split_at)) => {
4595                assert_eq!(selected_endpoint & align_mask, 0);
4596                assert_eq!(elems, 0);
4597                if suffix {
4598                    assert_eq!(split_at, bytes_len);
4599                } else {
4600                    assert_eq!(split_at, 0);
4601                }
4602            }
4603            Err(MetadataCastError::Alignment) => {
4604                assert_ne!(selected_endpoint & align_mask, 0);
4605            }
4606            Err(MetadataCastError::Size) => unreachable!(),
4607        }
4608    }
4609
4610    #[kani::proof]
4611    fn prove_aligned_suffix_end_and_size_imply_aligned_start() {
4612        let align = any_layout_align();
4613        #[allow(clippy::arithmetic_side_effects)]
4614        let align_mask = align.get() - 1;
4615        let addr: usize = kani::any();
4616        let bytes_len: usize = kani::any();
4617        let object_size: usize = kani::any();
4618        let Some(end) = addr.checked_add(bytes_len) else {
4619            kani::assume(false);
4620            loop {}
4621        };
4622        kani::assume(end & align_mask == 0);
4623        kani::assume(object_size & align_mask == 0);
4624        kani::assume(object_size <= bytes_len);
4625
4626        #[allow(clippy::arithmetic_side_effects)]
4627        let split_at = bytes_len - object_size;
4628        let Some(object_start) = addr.checked_add(split_at) else { unreachable!() };
4629        assert_eq!(object_start & align_mask, 0);
4630    }
4631
4632    #[cfg(kani_slow)]
4633    #[kani::proof]
4634    #[kani::solver(kissat)]
4635    fn prove_size_formula_bounds_size_offset() {
4636        // Prove the bound for arbitrary byte counts, including counts that
4637        // are not whole elements. The `size_for_elems` contract relates this
4638        // formula to checked element products. This harness checks the
4639        // formula's bound independently of that implementation.
4640        let trailing: TrailingSliceLayout = kani::any();
4641        let trailing_size: usize = kani::any();
4642        let Some(object_size) = slice_dst_size_for_trailing_bytes(trailing, trailing_size) else {
4643            kani::assume(false);
4644            loop {}
4645        };
4646        let size_offset = trailing.size_offset();
4647        let Some(without_padding) = size_offset.checked_add(trailing_size) else { unreachable!() };
4648
4649        assert!(without_padding <= object_size);
4650    }
4651
4652    // Older supported compilers resolve the unchecked operation syntax in
4653    // `add_scaled_metadata` to `polyfills::NumExt`; newer compilers prefer the
4654    // inherent methods. Use UFCS to verify the fallback implementation which
4655    // Kani's modern rustc cannot select in the production helper.
4656    #[kani::proof]
4657    #[kani::solver(kissat)]
4658    fn prove_num_ext_affine_fallback_matches_checked_arithmetic() {
4659        let base: usize = kani::any();
4660        let metadata: usize = kani::any();
4661        let multiple: usize = kani::any();
4662        let Some(scaled) = metadata.checked_mul(multiple) else {
4663            kani::assume(false);
4664            loop {}
4665        };
4666        let Some(expected) = base.checked_add(scaled) else {
4667            kani::assume(false);
4668            loop {}
4669        };
4670
4671        // SAFETY: The checked multiplication returned `Some`, satisfying
4672        // `NumExt::unchecked_mul`'s no-overflow contract.
4673        let scaled =
4674            unsafe { <usize as crate::util::polyfills::NumExt>::unchecked_mul(metadata, multiple) };
4675        // SAFETY: The checked addition returned `Some`, and `scaled` is the
4676        // checked product, satisfying `NumExt::unchecked_add`'s no-overflow
4677        // contract.
4678        let actual =
4679            unsafe { <usize as crate::util::polyfills::NumExt>::unchecked_add(base, scaled) };
4680        assert_eq!(actual, expected);
4681    }
4682
4683    #[cfg(kani_slow)]
4684    #[kani::proof]
4685    fn prove_requires_dynamic_padding() {
4686        let layout: DstLayout = any_valid_dst_layout();
4687
4688        let SizeInfo::SliceDst(size_info) = layout.size_info else {
4689            kani::assume(false);
4690            loop {}
4691        };
4692
4693        let meta: usize = kani::any();
4694
4695        let Some(trailing_slice_size) = size_info.elem_size.checked_mul(meta) else {
4696            // The `trailing_slice_size` exceeds `usize::MAX`; `meta` is invalid.
4697            kani::assume(false);
4698            loop {}
4699        };
4700
4701        let Some(trailing_slice_end) = size_info.offset.checked_add(trailing_slice_size) else {
4702            // The trailing slice end exceeds `usize::MAX`; `meta` is invalid.
4703            kani::assume(false);
4704            loop {}
4705        };
4706
4707        let Some(size) = slice_dst_size_for_trailing_bytes(size_info, trailing_slice_size) else {
4708            kani::assume(false);
4709            loop {}
4710        };
4711
4712        if size > DstLayout::MAX_SIZE || trailing_slice_end > size {
4713            // This metadata cannot describe a valid instance.
4714            kani::assume(false);
4715            loop {}
4716        }
4717
4718        if !layout.requires_dynamic_padding() {
4719            assert_eq!(trailing_slice_end, size);
4720        }
4721    }
4722
4723    #[cfg(kani_slow)]
4724    #[kani::proof]
4725    fn prove_dst_layout_extend() {
4726        use crate::util::{max, min, padding_needed_for};
4727
4728        let base: DstLayout = any_valid_dst_layout();
4729        let field: DstLayout = any_valid_dst_layout();
4730        let packed: Option<NonZeroUsize> = kani::any();
4731
4732        if let Some(max_align) = packed {
4733            kani::assume(max_align.is_power_of_two());
4734            kani::assume(base.align <= max_align);
4735        }
4736
4737        // The base can only be extended if it's sized.
4738        kani::assume(matches!(base.size_info, SizeInfo::Sized { .. }));
4739        let base_size = if let SizeInfo::Sized { size } = base.size_info {
4740            size
4741        } else {
4742            unreachable!();
4743        };
4744
4745        // The field's alignment is clamped by `max_align` (i.e., the
4746        // `packed` attribute, if any) [1].
4747        //
4748        // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
4749        //
4750        //   The alignments of each field, for the purpose of positioning
4751        //   fields, is the smaller of the specified alignment and the
4752        //   alignment of the field's type.
4753        let field_align = min(field.align, packed.unwrap_or(DstLayout::THEORETICAL_MAX_ALIGN));
4754
4755        // Compute the minimum amount of inter-field padding needed to
4756        // satisfy the field's alignment, and offset of the trailing field.
4757        // [1]
4758        //
4759        // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
4760        //
4761        //   Inter-field padding is guaranteed to be the minimum required in
4762        //   order to satisfy each field's (possibly altered) alignment.
4763        let padding = padding_needed_for(base_size, field_align);
4764        let offset = match base_size.checked_add(padding) {
4765            Some(offset) => offset,
4766            None => {
4767                kani::assume(false);
4768                loop {}
4769            }
4770        };
4771
4772        // Restrict the proof to extensions whose zero-metadata composite size
4773        // is representable. This semantic condition implies the
4774        // representability of each implementation intermediate below; prove
4775        // those implications rather than assuming the intermediates.
4776        match field.size_info {
4777            SizeInfo::Sized { size } => kani::assume(offset.checked_add(size).is_some()),
4778            SizeInfo::SliceDst(trailing) => {
4779                let Some(field_min) = trailing.size_for_elems(0) else {
4780                    kani::assume(false);
4781                    loop {}
4782                };
4783                kani::assume(offset.checked_add(field_min).is_some());
4784
4785                assert!(offset.checked_add(trailing.offset).is_some());
4786                assert!(offset.checked_add(trailing.size_base).is_some());
4787            }
4788        }
4789
4790        // Under the above conditions, `DstLayout::extend` will not panic.
4791        let composite = base.extend(field, packed);
4792
4793        // The struct's alignment is the maximum of its previous alignment and
4794        // `field_align`.
4795        assert_eq!(composite.align, max(base.align, field_align));
4796
4797        // For testing purposes, we'll also construct `alloc::Layout`
4798        // stand-ins for `DstLayout`, and show that `extend` behaves
4799        // comparably on both types.
4800        let base_analog = Layout::from_size_align(base_size, base.align.get()).unwrap();
4801
4802        match field.size_info {
4803            SizeInfo::Sized { size: field_size } => {
4804                if let SizeInfo::Sized { size: composite_size } = composite.size_info {
4805                    // If the trailing field is sized, the resulting layout will
4806                    // be sized. Its size will be the sum of the preceding
4807                    // layout, the size of the new field, and the size of
4808                    // inter-field padding between the two.
4809                    assert_eq!(composite_size, offset + field_size);
4810
4811                    let field_analog =
4812                        Layout::from_size_align(field_size, field_align.get()).unwrap();
4813
4814                    if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog)
4815                    {
4816                        assert_eq!(actual_offset, offset);
4817                        assert_eq!(actual_composite.size(), composite_size);
4818                        assert_eq!(actual_composite.align(), composite.align.get());
4819                    } else {
4820                        // An error here reflects that composite of `base`
4821                        // and `field` cannot correspond to a real Rust type
4822                        // fragment, because such a fragment would violate
4823                        // the basic invariants of a valid Rust layout. At
4824                        // the time of writing, `DstLayout` is a little more
4825                        // permissive than `Layout`, so we don't assert
4826                        // anything in this branch (e.g., unreachability).
4827                    }
4828                } else {
4829                    panic!("The composite of two sized layouts must be sized.")
4830                }
4831            }
4832            SizeInfo::SliceDst(field_trailing) => {
4833                if let SizeInfo::SliceDst(composite_trailing) = composite.size_info {
4834                    // The offset of the trailing slice component is the sum
4835                    // of the offset of the trailing field and the trailing
4836                    // slice offset within that field.
4837                    assert_eq!(composite_trailing.offset, offset + field_trailing.offset);
4838                    // The elem size is unchanged.
4839                    assert_eq!(composite_trailing.elem_size, field_trailing.elem_size);
4840                    // Extension preserves the invariant that the physical
4841                    // trailing-slice field fits within the unrounded minimum
4842                    // size. This is one of the premises consumed by the
4843                    // validator soundness proof.
4844                    let composite_phase =
4845                        composite_trailing.size_rounding_align_and_phase.components().1;
4846                    let Some(composite_unrounded_min) =
4847                        composite_trailing.size_base.checked_add(composite_phase)
4848                    else {
4849                        unreachable!()
4850                    };
4851                    assert!(composite_unrounded_min >= composite_trailing.offset);
4852
4853                    // The normalized formula must continue to describe the
4854                    // size of the field at every trailing byte contribution
4855                    // for which the checked size computation succeeds. This
4856                    // is stronger than considering only products of a slice
4857                    // length and `elem_size`, and avoids a nonlinear
4858                    // multiplication in the verifier.
4859                    let trailing_size: usize = kani::any();
4860                    let field_size =
4861                        match slice_dst_size_for_trailing_bytes(field_trailing, trailing_size) {
4862                            Some(size) => size,
4863                            None => {
4864                                kani::assume(false);
4865                                loop {}
4866                            }
4867                        };
4868                    let expected_size = match offset.checked_add(field_size) {
4869                        Some(size) => size,
4870                        None => {
4871                            kani::assume(false);
4872                            loop {}
4873                        }
4874                    };
4875                    assert_eq!(
4876                        slice_dst_size_for_trailing_bytes(composite_trailing, trailing_size),
4877                        Some(expected_size)
4878                    );
4879
4880                    // `alloc::Layout` permits sizes which are not a multiple
4881                    // of alignment, so this represents the unpadded prefix of
4882                    // the DST through the start of its trailing slice.
4883                    let field_analog =
4884                        Layout::from_size_align(field_trailing.offset, field_align.get()).unwrap();
4885
4886                    if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog)
4887                    {
4888                        assert_eq!(actual_offset, offset);
4889                        assert_eq!(actual_composite.size(), composite_trailing.offset);
4890                        assert_eq!(actual_composite.align(), composite.align.get());
4891                    } else {
4892                        // An error here reflects that composite of `base`
4893                        // and `field` cannot correspond to a real Rust type
4894                        // fragment, because such a fragment would violate
4895                        // the basic invariants of a valid Rust layout. At
4896                        // the time of writing, `DstLayout` is a little more
4897                        // permissive than `Layout`, so we don't assert
4898                        // anything in this branch (e.g., unreachability).
4899                    }
4900                } else {
4901                    panic!("The extension of a layout with a DST must result in a DST.")
4902                }
4903            }
4904        }
4905    }
4906
4907    #[kani::proof]
4908    #[kani::should_panic]
4909    fn prove_dst_layout_extend_dst_panics() {
4910        let base: DstLayout = any_valid_dst_layout();
4911        let field: DstLayout = any_valid_dst_layout();
4912        let packed: Option<NonZeroUsize> = kani::any();
4913
4914        if let Some(max_align) = packed {
4915            kani::assume(max_align.is_power_of_two());
4916            kani::assume(base.align <= max_align);
4917        }
4918
4919        kani::assume(matches!(base.size_info, SizeInfo::SliceDst(..)));
4920
4921        let _ = base.extend(field, packed);
4922    }
4923
4924    #[cfg(kani_slow)]
4925    fn prove_dst_layout_pad_to_align_dst_invariants(
4926        layout: DstLayout,
4927        unpadded_trailing: TrailingSliceLayout,
4928        padded: DstLayout,
4929    ) {
4930        // Calling `pad_to_align` does not alter the `DstLayout`'s alignment.
4931        assert_eq!(padded.align, layout.align);
4932        assert_eq!(padded.statically_shallow_unpadded, layout.statically_shallow_unpadded);
4933
4934        let SizeInfo::SliceDst(padded_trailing) = padded.size_info else {
4935            panic!("The padding of a DST layout must result in a DST layout.")
4936        };
4937        assert_eq!(padded_trailing.offset, unpadded_trailing.offset);
4938        assert_eq!(padded_trailing.elem_size, unpadded_trailing.elem_size);
4939
4940        // A finalized DST formula is visibly aligned: the rounding alignment
4941        // is at least the outer alignment, and the fixed base is a multiple of
4942        // the outer alignment. This is the invariant which lets suffix
4943        // validation infer alignment of the object's start from alignment of
4944        // its end.
4945        assert!(padded_trailing.size_rounding_align_and_phase.align() >= padded.align);
4946        #[allow(clippy::arithmetic_side_effects)]
4947        let padded_align_mask = padded.align.get() - 1;
4948        assert_eq!(padded_trailing.size_base & padded_align_mask, 0);
4949        assert_eq!(padded.pad_to_align(), padded);
4950        let padded_phase = padded_trailing.size_rounding_align_and_phase.components().1;
4951        let Some(padded_unrounded_min) = padded_trailing.size_base.checked_add(padded_phase) else {
4952            unreachable!()
4953        };
4954        assert!(padded_unrounded_min >= padded_trailing.offset);
4955    }
4956
4957    #[kani::proof]
4958    fn prove_dst_layout_pad_to_align_sized() {
4959        use crate::util::padding_needed_for;
4960
4961        let layout: DstLayout = any_valid_dst_layout();
4962        let SizeInfo::Sized { size: unpadded_size } = layout.size_info else {
4963            kani::assume(false);
4964            loop {}
4965        };
4966
4967        let padded = layout.pad_to_align();
4968        assert_eq!(padded.align, layout.align);
4969        let SizeInfo::Sized { size: padded_size } = padded.size_info else {
4970            panic!("The padding of a sized layout must result in a sized layout.")
4971        };
4972
4973        // If the layout is sized, it will remain sized after padding is added.
4974        // Its sum will be its unpadded size and the size of the trailing
4975        // padding needed to satisfy its alignment requirements.
4976        let padding = padding_needed_for(unpadded_size, layout.align);
4977        assert_eq!(padded_size, unpadded_size + padding);
4978        assert_eq!(
4979            padded.statically_shallow_unpadded,
4980            layout.statically_shallow_unpadded && padding == 0
4981        );
4982
4983        // Prove that calling `DstLayout::pad_to_align` behaves identically to
4984        // `Layout::pad_to_align`.
4985        let layout_analog = Layout::from_size_align(unpadded_size, layout.align.get()).unwrap();
4986        let padded_analog = layout_analog.pad_to_align();
4987        assert_eq!(padded_analog.align(), layout.align.get());
4988        assert_eq!(padded_analog.size(), padded_size);
4989    }
4990
4991    #[cfg(kani_slow)]
4992    #[kani::proof]
4993    fn prove_dst_layout_pad_to_align_dst_inner_rounding_transition() {
4994        use crate::util::padding_needed_for;
4995
4996        let layout: DstLayout = any_valid_dst_layout();
4997        let SizeInfo::SliceDst(trailing) = layout.size_info else {
4998            kani::assume(false);
4999            loop {}
5000        };
5001        kani::assume(trailing.size_rounding_align_and_phase.align() >= layout.align);
5002
5003        // `any_valid_dst_layout` requires the zero-metadata layout to be valid.
5004        // Prove that this semantic condition bounds both the padded minimum
5005        // and the implementation intermediate; do not assume the intermediate
5006        // merely because the implementation uses it.
5007        let Some(min_size) = trailing.size_for_elems(0) else { unreachable!() };
5008        let min_padding = padding_needed_for(min_size, layout.align);
5009        let Some(padded_min_size) = min_size.checked_add(min_padding) else { unreachable!() };
5010        assert!(padded_min_size <= DstLayout::MAX_SIZE);
5011
5012        let padding = padding_needed_for(trailing.size_base, layout.align);
5013        let Some(expected_base) = trailing.size_base.checked_add(padding) else { unreachable!() };
5014
5015        let padded = layout.pad_to_align();
5016        let SizeInfo::SliceDst(padded_trailing) = padded.size_info else { unreachable!() };
5017        assert_eq!(padded_trailing.size_base, expected_base);
5018        assert_eq!(
5019            padded_trailing.size_rounding_align_and_phase,
5020            trailing.size_rounding_align_and_phase
5021        );
5022        prove_dst_layout_pad_to_align_dst_invariants(layout, trailing, padded);
5023    }
5024
5025    #[kani::proof]
5026    #[kani::solver(kissat)]
5027    fn prove_dst_layout_pad_to_align_dst_inner_rounding_formula() {
5028        use crate::util::padding_needed_for;
5029
5030        let size_align = any_layout_align();
5031        let outer_align = any_layout_align();
5032        kani::assume(size_align >= outer_align);
5033        let size_base: usize = kani::any();
5034        let raw_size_phase: usize = kani::any();
5035        #[allow(clippy::arithmetic_side_effects)]
5036        let size_phase = raw_size_phase & (size_align.get() - 1);
5037        let trailing_size: usize = kani::any();
5038
5039        // Evaluate `size_base + round_up(size_phase + trailing_size,
5040        // size_align)`, then round that complete size to `outer_align`.
5041        // Compare with the equivalent expression obtained by rounding only
5042        // `size_base` to `outer_align`, since `size_align` is a multiple of
5043        // `outer_align`. Valid Rust objects have representable sizes; discard
5044        // only trailing byte counts whose input or padded object size is not
5045        // representable.
5046        let Some(unrounded_inner) = size_phase.checked_add(trailing_size) else {
5047            kani::assume(false);
5048            loop {}
5049        };
5050        let Some(rounded_inner) =
5051            unrounded_inner.checked_add(padding_needed_for(unrounded_inner, size_align))
5052        else {
5053            kani::assume(false);
5054            loop {}
5055        };
5056        let Some(unpadded_size) = size_base.checked_add(rounded_inner) else {
5057            kani::assume(false);
5058            loop {}
5059        };
5060        let Some(expected_size) =
5061            unpadded_size.checked_add(padding_needed_for(unpadded_size, outer_align))
5062        else {
5063            kani::assume(false);
5064            loop {}
5065        };
5066
5067        let Some(padded_base) = size_base.checked_add(padding_needed_for(size_base, outer_align))
5068        else {
5069            unreachable!()
5070        };
5071        let Some(actual_size) = padded_base.checked_add(rounded_inner) else { unreachable!() };
5072        assert_eq!(actual_size, expected_size);
5073    }
5074
5075    #[cfg(kani_slow)]
5076    #[kani::proof]
5077    fn prove_dst_layout_pad_to_align_dst_outer_alignment_transition() {
5078        use crate::util::padding_needed_for;
5079
5080        let layout: DstLayout = any_valid_dst_layout();
5081        let SizeInfo::SliceDst(trailing) = layout.size_info else {
5082            kani::assume(false);
5083            loop {}
5084        };
5085        let size_align = trailing.size_rounding_align_and_phase.align();
5086        kani::assume(size_align < layout.align);
5087
5088        // `any_valid_dst_layout` requires the zero-metadata layout to be valid.
5089        // Prove that this semantic condition bounds both the padded minimum
5090        // and every implementation intermediate; do not assume the
5091        // intermediates merely because the implementation uses them.
5092        let Some(min_size) = trailing.size_for_elems(0) else { unreachable!() };
5093        let min_padding = padding_needed_for(min_size, layout.align);
5094        let Some(padded_min_size) = min_size.checked_add(min_padding) else { unreachable!() };
5095        assert!(padded_min_size <= DstLayout::MAX_SIZE);
5096
5097        let base_padding = padding_needed_for(trailing.size_base, size_align);
5098        let Some(rounded_base) = trailing.size_base.checked_add(base_padding) else {
5099            unreachable!()
5100        };
5101        let Some(t) =
5102            rounded_base.checked_add(trailing.size_rounding_align_and_phase.components().1)
5103        else {
5104            unreachable!()
5105        };
5106        #[allow(clippy::arithmetic_side_effects)]
5107        let expected_phase = t & (layout.align.get() - 1);
5108        #[allow(clippy::arithmetic_side_effects)]
5109        let expected_base = t - expected_phase;
5110
5111        let padded = layout.pad_to_align();
5112        let SizeInfo::SliceDst(padded_trailing) = padded.size_info else { unreachable!() };
5113        assert_eq!(padded_trailing.size_base, expected_base);
5114        assert_eq!(
5115            padded_trailing.size_rounding_align_and_phase,
5116            RoundingAlignAndPhase::new(layout.align, expected_phase)
5117        );
5118        prove_dst_layout_pad_to_align_dst_invariants(layout, trailing, padded);
5119    }
5120
5121    #[kani::proof]
5122    #[kani::solver(kissat)]
5123    fn prove_dst_layout_pad_to_align_dst_outer_alignment_formula() {
5124        use crate::util::padding_needed_for;
5125
5126        let size_align = any_layout_align();
5127        let outer_align = any_layout_align();
5128        kani::assume(size_align < outer_align);
5129        let size_base: usize = kani::any();
5130        let raw_size_phase: usize = kani::any();
5131        #[allow(clippy::arithmetic_side_effects)]
5132        let size_phase = raw_size_phase & (size_align.get() - 1);
5133        let trailing_size: usize = kani::any();
5134
5135        // Evaluate `size_base + round_up(size_phase + trailing_size,
5136        // size_align)`, then round that complete size to `outer_align`.
5137        let Some(unrounded_inner) = size_phase.checked_add(trailing_size) else {
5138            kani::assume(false);
5139            loop {}
5140        };
5141        let Some(rounded_inner) =
5142            unrounded_inner.checked_add(padding_needed_for(unrounded_inner, size_align))
5143        else {
5144            kani::assume(false);
5145            loop {}
5146        };
5147        let Some(unpadded_size) = size_base.checked_add(rounded_inner) else {
5148            kani::assume(false);
5149            loop {}
5150        };
5151        let Some(expected_size) =
5152            unpadded_size.checked_add(padding_needed_for(unpadded_size, outer_align))
5153        else {
5154            kani::assume(false);
5155            loop {}
5156        };
5157
5158        // Since `outer_align` is a larger multiple of `size_align`, absorb
5159        // the inner rounding into the outer rounding and split the fixed
5160        // contribution into a base and phase:
5161        //
5162        //   fixed_contribution = round_up(size_base, size_align) + size_phase
5163        //   new_phase = fixed_contribution % outer_align
5164        //   new_base = fixed_contribution - new_phase
5165        //   new_base + round_up(new_phase + trailing_size, outer_align)
5166        let Some(rounded_base) = size_base.checked_add(padding_needed_for(size_base, size_align))
5167        else {
5168            unreachable!()
5169        };
5170        let Some(t) = rounded_base.checked_add(size_phase) else { unreachable!() };
5171        #[allow(clippy::arithmetic_side_effects)]
5172        let new_phase = t & (outer_align.get() - 1);
5173        #[allow(clippy::arithmetic_side_effects)]
5174        let new_base = t - new_phase;
5175        let Some(new_unrounded_inner) = new_phase.checked_add(trailing_size) else {
5176            unreachable!()
5177        };
5178        let Some(new_rounded_inner) =
5179            new_unrounded_inner.checked_add(padding_needed_for(new_unrounded_inner, outer_align))
5180        else {
5181            unreachable!()
5182        };
5183        let Some(actual_size) = new_base.checked_add(new_rounded_inner) else { unreachable!() };
5184        assert_eq!(actual_size, expected_size);
5185    }
5186}