Coverage Report

Created: 2025-12-14 07:56

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