Coverage Report

Created: 2025-11-16 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerocopy-0.8.14/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(Copy, Clone, Debug, PartialEq, Eq))]
30
pub struct DstLayout {
31
    pub(crate) align: NonZeroUsize,
32
    pub(crate) size_info: SizeInfo,
33
}
34
35
#[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))]
36
#[derive(Copy, Clone)]
37
pub(crate) enum SizeInfo<E = usize> {
38
    Sized { size: usize },
39
    SliceDst(TrailingSliceLayout<E>),
40
}
41
42
#[cfg_attr(any(kani, test), derive(Debug, PartialEq, Eq))]
43
#[derive(Copy, Clone)]
44
pub(crate) struct TrailingSliceLayout<E = usize> {
45
    // The offset of the first byte of the trailing slice field. Note that this
46
    // is NOT the same as the minimum size of the type. For example, consider
47
    // the following type:
48
    //
49
    //   struct Foo {
50
    //       a: u16,
51
    //       b: u8,
52
    //       c: [u8],
53
    //   }
54
    //
55
    // In `Foo`, `c` is at byte offset 3. When `c.len() == 0`, `c` is followed
56
    // by a padding byte.
57
    pub(crate) offset: usize,
58
    // The size of the element type of the trailing slice field.
59
    pub(crate) elem_size: E,
60
}
61
62
impl SizeInfo {
63
    /// Attempts to create a `SizeInfo` from `Self` in which `elem_size` is a
64
    /// `NonZeroUsize`. If `elem_size` is 0, returns `None`.
65
    #[allow(unused)]
66
0
    const fn try_to_nonzero_elem_size(&self) -> Option<SizeInfo<NonZeroUsize>> {
67
0
        Some(match *self {
68
0
            SizeInfo::Sized { size } => SizeInfo::Sized { size },
69
0
            SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => {
70
0
                if let Some(elem_size) = NonZeroUsize::new(elem_size) {
71
0
                    SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
72
                } else {
73
0
                    return None;
74
                }
75
            }
76
        })
77
0
    }
78
}
79
80
#[doc(hidden)]
81
#[derive(Copy, Clone)]
82
#[cfg_attr(test, derive(Debug))]
83
#[allow(missing_debug_implementations)]
84
pub enum CastType {
85
    Prefix,
86
    Suffix,
87
}
88
89
#[cfg_attr(test, derive(Debug))]
90
pub(crate) enum MetadataCastError {
91
    Alignment,
92
    Size,
93
}
94
95
impl DstLayout {
96
    /// The minimum possible alignment of a type.
97
    const MIN_ALIGN: NonZeroUsize = match NonZeroUsize::new(1) {
98
        Some(min_align) => min_align,
99
        None => const_unreachable!(),
100
    };
101
102
    /// The maximum theoretic possible alignment of a type.
103
    ///
104
    /// For compatibility with future Rust versions, this is defined as the
105
    /// maximum power-of-two that fits into a `usize`. See also
106
    /// [`DstLayout::CURRENT_MAX_ALIGN`].
107
    pub(crate) const THEORETICAL_MAX_ALIGN: NonZeroUsize =
108
        match NonZeroUsize::new(1 << (POINTER_WIDTH_BITS - 1)) {
109
            Some(max_align) => max_align,
110
            None => const_unreachable!(),
111
        };
112
113
    /// The current, documented max alignment of a type \[1\].
114
    ///
115
    /// \[1\] Per <https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers>:
116
    ///
117
    ///   The alignment value must be a power of two from 1 up to
118
    ///   2<sup>29</sup>.
119
    #[cfg(not(kani))]
120
    pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 28) {
121
        Some(max_align) => max_align,
122
        None => const_unreachable!(),
123
    };
124
125
    /// Constructs a `DstLayout` for a zero-sized type with `repr_align`
126
    /// alignment (or 1). If `repr_align` is provided, then it must be a power
127
    /// of two.
128
    ///
129
    /// # Panics
130
    ///
131
    /// This function panics if the supplied `repr_align` is not a power of two.
132
    ///
133
    /// # Safety
134
    ///
135
    /// Unsafe code may assume that the contract of this function is satisfied.
136
    #[doc(hidden)]
137
    #[must_use]
138
    #[inline]
139
0
    pub const fn new_zst(repr_align: Option<NonZeroUsize>) -> DstLayout {
140
0
        let align = match repr_align {
141
0
            Some(align) => align,
142
0
            None => Self::MIN_ALIGN,
143
        };
144
145
0
        const_assert!(align.get().is_power_of_two());
146
147
0
        DstLayout { align, size_info: SizeInfo::Sized { size: 0 } }
148
0
    }
149
150
    /// Constructs a `DstLayout` which describes `T`.
151
    ///
152
    /// # Safety
153
    ///
154
    /// Unsafe code may assume that `DstLayout` is the correct layout for `T`.
155
    #[doc(hidden)]
156
    #[must_use]
157
    #[inline]
158
0
    pub const fn for_type<T>() -> DstLayout {
159
        // SAFETY: `align` is correct by construction. `T: Sized`, and so it is
160
        // sound to initialize `size_info` to `SizeInfo::Sized { size }`; the
161
        // `size` field is also correct by construction.
162
        DstLayout {
163
0
            align: match NonZeroUsize::new(mem::align_of::<T>()) {
164
0
                Some(align) => align,
165
0
                None => const_unreachable!(),
166
            },
167
0
            size_info: SizeInfo::Sized { size: mem::size_of::<T>() },
168
        }
169
0
    }
170
171
    /// Constructs a `DstLayout` which describes `[T]`.
172
    ///
173
    /// # Safety
174
    ///
175
    /// Unsafe code may assume that `DstLayout` is the correct layout for `[T]`.
176
0
    pub(crate) const fn for_slice<T>() -> DstLayout {
177
        // SAFETY: The alignment of a slice is equal to the alignment of its
178
        // element type, and so `align` is initialized correctly.
179
        //
180
        // Since this is just a slice type, there is no offset between the
181
        // beginning of the type and the beginning of the slice, so it is
182
        // correct to set `offset: 0`. The `elem_size` is correct by
183
        // construction. Since `[T]` is a (degenerate case of a) slice DST, it
184
        // is correct to initialize `size_info` to `SizeInfo::SliceDst`.
185
        DstLayout {
186
0
            align: match NonZeroUsize::new(mem::align_of::<T>()) {
187
0
                Some(align) => align,
188
0
                None => const_unreachable!(),
189
            },
190
0
            size_info: SizeInfo::SliceDst(TrailingSliceLayout {
191
0
                offset: 0,
192
0
                elem_size: mem::size_of::<T>(),
193
0
            }),
194
        }
195
0
    }
196
197
    /// Like `Layout::extend`, this creates a layout that describes a record
198
    /// whose layout consists of `self` followed by `next` that includes the
199
    /// necessary inter-field padding, but not any trailing padding.
200
    ///
201
    /// In order to match the layout of a `#[repr(C)]` struct, this method
202
    /// should be invoked for each field in declaration order. To add trailing
203
    /// padding, call `DstLayout::pad_to_align` after extending the layout for
204
    /// all fields. If `self` corresponds to a type marked with
205
    /// `repr(packed(N))`, then `repr_packed` should be set to `Some(N)`,
206
    /// otherwise `None`.
207
    ///
208
    /// This method cannot be used to match the layout of a record with the
209
    /// default representation, as that representation is mostly unspecified.
210
    ///
211
    /// # Safety
212
    ///
213
    /// If a (potentially hypothetical) valid `repr(C)` Rust type begins with
214
    /// fields whose layout are `self`, and those fields are immediately
215
    /// followed by a field whose layout is `field`, then unsafe code may rely
216
    /// on `self.extend(field, repr_packed)` producing a layout that correctly
217
    /// encompasses those two components.
218
    ///
219
    /// We make no guarantees to the behavior of this method if these fragments
220
    /// cannot appear in a valid Rust type (e.g., the concatenation of the
221
    /// layouts would lead to a size larger than `isize::MAX`).
222
    #[doc(hidden)]
223
    #[must_use]
224
    #[inline]
225
0
    pub const fn extend(self, field: DstLayout, repr_packed: Option<NonZeroUsize>) -> Self {
226
        use util::{max, min, padding_needed_for};
227
228
        // If `repr_packed` is `None`, there are no alignment constraints, and
229
        // the value can be defaulted to `THEORETICAL_MAX_ALIGN`.
230
0
        let max_align = match repr_packed {
231
0
            Some(max_align) => max_align,
232
0
            None => Self::THEORETICAL_MAX_ALIGN,
233
        };
234
235
0
        const_assert!(max_align.get().is_power_of_two());
236
237
        // We use Kani to prove that this method is robust to future increases
238
        // in Rust's maximum allowed alignment. However, if such a change ever
239
        // actually occurs, we'd like to be notified via assertion failures.
240
        #[cfg(not(kani))]
241
        {
242
0
            const_debug_assert!(self.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
243
0
            const_debug_assert!(field.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
244
0
            if let Some(repr_packed) = repr_packed {
245
0
                const_debug_assert!(repr_packed.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
246
0
            }
247
        }
248
249
        // The field's alignment is clamped by `repr_packed` (i.e., the
250
        // `repr(packed(N))` attribute, if any) [1].
251
        //
252
        // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
253
        //
254
        //   The alignments of each field, for the purpose of positioning
255
        //   fields, is the smaller of the specified alignment and the alignment
256
        //   of the field's type.
257
0
        let field_align = min(field.align, max_align);
258
259
        // The struct's alignment is the maximum of its previous alignment and
260
        // `field_align`.
261
0
        let align = max(self.align, field_align);
262
263
0
        let size_info = match self.size_info {
264
            // If the layout is already a DST, we panic; DSTs cannot be extended
265
            // with additional fields.
266
0
            SizeInfo::SliceDst(..) => const_panic!("Cannot extend a DST with additional fields."),
267
268
0
            SizeInfo::Sized { size: preceding_size } => {
269
                // Compute the minimum amount of inter-field padding needed to
270
                // satisfy the field's alignment, and offset of the trailing
271
                // field. [1]
272
                //
273
                // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
274
                //
275
                //   Inter-field padding is guaranteed to be the minimum
276
                //   required in order to satisfy each field's (possibly
277
                //   altered) alignment.
278
0
                let padding = padding_needed_for(preceding_size, field_align);
279
280
                // This will not panic (and is proven to not panic, with Kani)
281
                // if the layout components can correspond to a leading layout
282
                // fragment of a valid Rust type, but may panic otherwise (e.g.,
283
                // combining or aligning the components would create a size
284
                // exceeding `isize::MAX`).
285
0
                let offset = match preceding_size.checked_add(padding) {
286
0
                    Some(offset) => offset,
287
0
                    None => const_panic!("Adding padding to `self`'s size overflows `usize`."),
288
                };
289
290
0
                match field.size_info {
291
0
                    SizeInfo::Sized { size: field_size } => {
292
                        // If the trailing field is sized, the resulting layout
293
                        // will be sized. Its size will be the sum of the
294
                        // preceeding layout, the size of the new field, and the
295
                        // size of inter-field padding between the two.
296
                        //
297
                        // This will not panic (and is proven with Kani to not
298
                        // panic) if the layout components can correspond to a
299
                        // leading layout fragment of a valid Rust type, but may
300
                        // panic otherwise (e.g., combining or aligning the
301
                        // components would create a size exceeding
302
                        // `usize::MAX`).
303
0
                        let size = match offset.checked_add(field_size) {
304
0
                            Some(size) => size,
305
0
                            None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
306
                        };
307
0
                        SizeInfo::Sized { size }
308
                    }
309
                    SizeInfo::SliceDst(TrailingSliceLayout {
310
0
                        offset: trailing_offset,
311
0
                        elem_size,
312
                    }) => {
313
                        // If the trailing field is dynamically sized, so too
314
                        // will the resulting layout. The offset of the trailing
315
                        // slice component is the sum of the offset of the
316
                        // trailing field and the trailing slice offset within
317
                        // that field.
318
                        //
319
                        // This will not panic (and is proven with Kani to not
320
                        // panic) if the layout components can correspond to a
321
                        // leading layout fragment of a valid Rust type, but may
322
                        // panic otherwise (e.g., combining or aligning the
323
                        // components would create a size exceeding
324
                        // `usize::MAX`).
325
0
                        let offset = match offset.checked_add(trailing_offset) {
326
0
                            Some(offset) => offset,
327
0
                            None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
328
                        };
329
0
                        SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
330
                    }
331
                }
332
            }
333
        };
334
335
0
        DstLayout { align, size_info }
336
0
    }
337
338
    /// Like `Layout::pad_to_align`, this routine rounds the size of this layout
339
    /// up to the nearest multiple of this type's alignment or `repr_packed`
340
    /// (whichever is less). This method leaves DST layouts unchanged, since the
341
    /// trailing padding of DSTs is computed at runtime.
342
    ///
343
    /// In order to match the layout of a `#[repr(C)]` struct, this method
344
    /// should be invoked after the invocations of [`DstLayout::extend`]. If
345
    /// `self` corresponds to a type marked with `repr(packed(N))`, then
346
    /// `repr_packed` should be set to `Some(N)`, otherwise `None`.
347
    ///
348
    /// This method cannot be used to match the layout of a record with the
349
    /// default representation, as that representation is mostly unspecified.
350
    ///
351
    /// # Safety
352
    ///
353
    /// If a (potentially hypothetical) valid `repr(C)` type begins with fields
354
    /// whose layout are `self` followed only by zero or more bytes of trailing
355
    /// padding (not included in `self`), then unsafe code may rely on
356
    /// `self.pad_to_align(repr_packed)` producing a layout that correctly
357
    /// encapsulates the layout of that type.
358
    ///
359
    /// We make no guarantees to the behavior of this method if `self` cannot
360
    /// appear in a valid Rust type (e.g., because the addition of trailing
361
    /// padding would lead to a size larger than `isize::MAX`).
362
    #[doc(hidden)]
363
    #[must_use]
364
    #[inline]
365
0
    pub const fn pad_to_align(self) -> Self {
366
        use util::padding_needed_for;
367
368
0
        let size_info = match self.size_info {
369
            // For sized layouts, we add the minimum amount of trailing padding
370
            // needed to satisfy alignment.
371
0
            SizeInfo::Sized { size: unpadded_size } => {
372
0
                let padding = padding_needed_for(unpadded_size, self.align);
373
0
                let size = match unpadded_size.checked_add(padding) {
374
0
                    Some(size) => size,
375
0
                    None => const_panic!("Adding padding caused size to overflow `usize`."),
376
                };
377
0
                SizeInfo::Sized { size }
378
            }
379
            // For DST layouts, trailing padding depends on the length of the
380
            // trailing DST and is computed at runtime. This does not alter the
381
            // offset or element size of the layout, so we leave `size_info`
382
            // unchanged.
383
0
            size_info @ SizeInfo::SliceDst(_) => size_info,
384
        };
385
386
0
        DstLayout { align: self.align, size_info }
387
0
    }
388
389
    /// Validates that a cast is sound from a layout perspective.
390
    ///
391
    /// Validates that the size and alignment requirements of a type with the
392
    /// layout described in `self` would not be violated by performing a
393
    /// `cast_type` cast from a pointer with address `addr` which refers to a
394
    /// memory region of size `bytes_len`.
395
    ///
396
    /// If the cast is valid, `validate_cast_and_convert_metadata` returns
397
    /// `(elems, split_at)`. If `self` describes a dynamically-sized type, then
398
    /// `elems` is the maximum number of trailing slice elements for which a
399
    /// cast would be valid (for sized types, `elem` is meaningless and should
400
    /// be ignored). `split_at` is the index at which to split the memory region
401
    /// in order for the prefix (suffix) to contain the result of the cast, and
402
    /// in order for the remaining suffix (prefix) to contain the leftover
403
    /// bytes.
404
    ///
405
    /// There are three conditions under which a cast can fail:
406
    /// - The smallest possible value for the type is larger than the provided
407
    ///   memory region
408
    /// - A prefix cast is requested, and `addr` does not satisfy `self`'s
409
    ///   alignment requirement
410
    /// - A suffix cast is requested, and `addr + bytes_len` does not satisfy
411
    ///   `self`'s alignment requirement (as a consequence, since all instances
412
    ///   of the type are a multiple of its alignment, no size for the type will
413
    ///   result in a starting address which is properly aligned)
414
    ///
415
    /// # Safety
416
    ///
417
    /// The caller may assume that this implementation is correct, and may rely
418
    /// on that assumption for the soundness of their code. In particular, the
419
    /// caller may assume that, if `validate_cast_and_convert_metadata` returns
420
    /// `Some((elems, split_at))`, then:
421
    /// - A pointer to the type (for dynamically sized types, this includes
422
    ///   `elems` as its pointer metadata) describes an object of size `size <=
423
    ///   bytes_len`
424
    /// - If this is a prefix cast:
425
    ///   - `addr` satisfies `self`'s alignment
426
    ///   - `size == split_at`
427
    /// - If this is a suffix cast:
428
    ///   - `split_at == bytes_len - size`
429
    ///   - `addr + split_at` satisfies `self`'s alignment
430
    ///
431
    /// Note that this method does *not* ensure that a pointer constructed from
432
    /// its return values will be a valid pointer. In particular, this method
433
    /// does not reason about `isize` overflow, which is a requirement of many
434
    /// Rust pointer APIs, and may at some point be determined to be a validity
435
    /// invariant of pointer types themselves. This should never be a problem so
436
    /// long as the arguments to this method are derived from a known-valid
437
    /// pointer (e.g., one derived from a safe Rust reference), but it is
438
    /// nonetheless the caller's responsibility to justify that pointer
439
    /// arithmetic will not overflow based on a safety argument *other than* the
440
    /// mere fact that this method returned successfully.
441
    ///
442
    /// # Panics
443
    ///
444
    /// `validate_cast_and_convert_metadata` will panic if `self` describes a
445
    /// DST whose trailing slice element is zero-sized.
446
    ///
447
    /// If `addr + bytes_len` overflows `usize`,
448
    /// `validate_cast_and_convert_metadata` may panic, or it may return
449
    /// incorrect results. No guarantees are made about when
450
    /// `validate_cast_and_convert_metadata` will panic. The caller should not
451
    /// rely on `validate_cast_and_convert_metadata` panicking in any particular
452
    /// condition, even if `debug_assertions` are enabled.
453
    #[allow(unused)]
454
    #[inline(always)]
455
0
    pub(crate) const fn validate_cast_and_convert_metadata(
456
0
        &self,
457
0
        addr: usize,
458
0
        bytes_len: usize,
459
0
        cast_type: CastType,
460
0
    ) -> Result<(usize, usize), MetadataCastError> {
461
        // `debug_assert!`, but with `#[allow(clippy::arithmetic_side_effects)]`.
462
        macro_rules! __const_debug_assert {
463
            ($e:expr $(, $msg:expr)?) => {
464
                const_debug_assert!({
465
                    #[allow(clippy::arithmetic_side_effects)]
466
                    let e = $e;
467
                    e
468
                } $(, $msg)?);
469
            };
470
        }
471
472
        // Note that, in practice, `self` is always a compile-time constant. We
473
        // do this check earlier than needed to ensure that we always panic as a
474
        // result of bugs in the program (such as calling this function on an
475
        // invalid type) instead of allowing this panic to be hidden if the cast
476
        // would have failed anyway for runtime reasons (such as a too-small
477
        // memory region).
478
        //
479
        // TODO(#67): Once our MSRV is 1.65, use let-else:
480
        // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
481
0
        let size_info = match self.size_info.try_to_nonzero_elem_size() {
482
0
            Some(size_info) => size_info,
483
0
            None => const_panic!("attempted to cast to slice type with zero-sized element"),
484
        };
485
486
        // Precondition
487
0
        __const_debug_assert!(
488
0
            addr.checked_add(bytes_len).is_some(),
489
0
            "`addr` + `bytes_len` > usize::MAX"
490
        );
491
492
        // Alignment checks go in their own block to avoid introducing variables
493
        // into the top-level scope.
494
        {
495
            // We check alignment for `addr` (for prefix casts) or `addr +
496
            // bytes_len` (for suffix casts). For a prefix cast, the correctness
497
            // of this check is trivial - `addr` is the address the object will
498
            // live at.
499
            //
500
            // For a suffix cast, we know that all valid sizes for the type are
501
            // a multiple of the alignment (and by safety precondition, we know
502
            // `DstLayout` may only describe valid Rust types). Thus, a
503
            // validly-sized instance which lives at a validly-aligned address
504
            // must also end at a validly-aligned address. Thus, if the end
505
            // address for a suffix cast (`addr + bytes_len`) is not aligned,
506
            // then no valid start address will be aligned either.
507
0
            let offset = match cast_type {
508
0
                CastType::Prefix => 0,
509
0
                CastType::Suffix => bytes_len,
510
            };
511
512
            // Addition is guaranteed not to overflow because `offset <=
513
            // bytes_len`, and `addr + bytes_len <= usize::MAX` is a
514
            // precondition of this method. Modulus is guaranteed not to divide
515
            // by 0 because `align` is non-zero.
516
            #[allow(clippy::arithmetic_side_effects)]
517
0
            if (addr + offset) % self.align.get() != 0 {
518
0
                return Err(MetadataCastError::Alignment);
519
0
            }
520
        }
521
522
0
        let (elems, self_bytes) = match size_info {
523
0
            SizeInfo::Sized { size } => {
524
0
                if size > bytes_len {
525
0
                    return Err(MetadataCastError::Size);
526
0
                }
527
0
                (0, size)
528
            }
529
0
            SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => {
530
                // Calculate the maximum number of bytes that could be consumed
531
                // - any number of bytes larger than this will either not be a
532
                // multiple of the alignment, or will be larger than
533
                // `bytes_len`.
534
0
                let max_total_bytes =
535
0
                    util::round_down_to_next_multiple_of_alignment(bytes_len, self.align);
536
                // Calculate the maximum number of bytes that could be consumed
537
                // by the trailing slice.
538
                //
539
                // TODO(#67): Once our MSRV is 1.65, use let-else:
540
                // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
541
0
                let max_slice_and_padding_bytes = match max_total_bytes.checked_sub(offset) {
542
0
                    Some(max) => max,
543
                    // `bytes_len` too small even for 0 trailing slice elements.
544
0
                    None => return Err(MetadataCastError::Size),
545
                };
546
547
                // Calculate the number of elements that fit in
548
                // `max_slice_and_padding_bytes`; any remaining bytes will be
549
                // considered padding.
550
                //
551
                // Guaranteed not to divide by zero: `elem_size` is non-zero.
552
                #[allow(clippy::arithmetic_side_effects)]
553
0
                let elems = max_slice_and_padding_bytes / elem_size.get();
554
                // Guaranteed not to overflow on multiplication: `usize::MAX >=
555
                // max_slice_and_padding_bytes >= (max_slice_and_padding_bytes /
556
                // elem_size) * elem_size`.
557
                //
558
                // Guaranteed not to overflow on addition:
559
                // - max_slice_and_padding_bytes == max_total_bytes - offset
560
                // - elems * elem_size <= max_slice_and_padding_bytes == max_total_bytes - offset
561
                // - elems * elem_size + offset <= max_total_bytes <= usize::MAX
562
                #[allow(clippy::arithmetic_side_effects)]
563
0
                let without_padding = offset + elems * elem_size.get();
564
                // `self_bytes` is equal to the offset bytes plus the bytes
565
                // consumed by the trailing slice plus any padding bytes
566
                // required to satisfy the alignment. Note that we have computed
567
                // the maximum number of trailing slice elements that could fit
568
                // in `self_bytes`, so any padding is guaranteed to be less than
569
                // the size of an extra element.
570
                //
571
                // Guaranteed not to overflow:
572
                // - By previous comment: without_padding == elems * elem_size +
573
                //   offset <= max_total_bytes
574
                // - By construction, `max_total_bytes` is a multiple of
575
                //   `self.align`.
576
                // - At most, adding padding needed to round `without_padding`
577
                //   up to the next multiple of the alignment will bring
578
                //   `self_bytes` up to `max_total_bytes`.
579
                #[allow(clippy::arithmetic_side_effects)]
580
0
                let self_bytes =
581
0
                    without_padding + util::padding_needed_for(without_padding, self.align);
582
0
                (elems, self_bytes)
583
            }
584
        };
585
586
0
        __const_debug_assert!(self_bytes <= bytes_len);
587
588
0
        let split_at = match cast_type {
589
0
            CastType::Prefix => self_bytes,
590
            // Guaranteed not to underflow:
591
            // - In the `Sized` branch, only returns `size` if `size <=
592
            //   bytes_len`.
593
            // - In the `SliceDst` branch, calculates `self_bytes <=
594
            //   max_toatl_bytes`, which is upper-bounded by `bytes_len`.
595
            #[allow(clippy::arithmetic_side_effects)]
596
0
            CastType::Suffix => bytes_len - self_bytes,
597
        };
598
599
0
        Ok((elems, split_at))
600
0
    }
601
}
602
603
// TODO(#67): For some reason, on our MSRV toolchain, this `allow` isn't
604
// enforced despite having `#![allow(unknown_lints)]` at the crate root, but
605
// putting it here works. Once our MSRV is high enough that this bug has been
606
// fixed, remove this `allow`.
607
#[allow(unknown_lints)]
608
#[cfg(test)]
609
mod tests {
610
    use super::*;
611
612
    /// Tests of when a sized `DstLayout` is extended with a sized field.
613
    #[allow(clippy::decimal_literal_representation)]
614
    #[test]
615
    fn test_dst_layout_extend_sized_with_sized() {
616
        // This macro constructs a layout corresponding to a `u8` and extends it
617
        // with a zero-sized trailing field of given alignment `n`. The macro
618
        // tests that the resulting layout has both size and alignment `min(n,
619
        // P)` for all valid values of `repr(packed(P))`.
620
        macro_rules! test_align_is_size {
621
            ($n:expr) => {
622
                let base = DstLayout::for_type::<u8>();
623
                let trailing_field = DstLayout::for_type::<elain::Align<$n>>();
624
625
                let packs =
626
                    core::iter::once(None).chain((0..29).map(|p| NonZeroUsize::new(2usize.pow(p))));
627
628
                for pack in packs {
629
                    let composite = base.extend(trailing_field, pack);
630
                    let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN);
631
                    let align = $n.min(max_align.get());
632
                    assert_eq!(
633
                        composite,
634
                        DstLayout {
635
                            align: NonZeroUsize::new(align).unwrap(),
636
                            size_info: SizeInfo::Sized { size: align }
637
                        }
638
                    )
639
                }
640
            };
641
        }
642
643
        test_align_is_size!(1);
644
        test_align_is_size!(2);
645
        test_align_is_size!(4);
646
        test_align_is_size!(8);
647
        test_align_is_size!(16);
648
        test_align_is_size!(32);
649
        test_align_is_size!(64);
650
        test_align_is_size!(128);
651
        test_align_is_size!(256);
652
        test_align_is_size!(512);
653
        test_align_is_size!(1024);
654
        test_align_is_size!(2048);
655
        test_align_is_size!(4096);
656
        test_align_is_size!(8192);
657
        test_align_is_size!(16384);
658
        test_align_is_size!(32768);
659
        test_align_is_size!(65536);
660
        test_align_is_size!(131072);
661
        test_align_is_size!(262144);
662
        test_align_is_size!(524288);
663
        test_align_is_size!(1048576);
664
        test_align_is_size!(2097152);
665
        test_align_is_size!(4194304);
666
        test_align_is_size!(8388608);
667
        test_align_is_size!(16777216);
668
        test_align_is_size!(33554432);
669
        test_align_is_size!(67108864);
670
        test_align_is_size!(33554432);
671
        test_align_is_size!(134217728);
672
        test_align_is_size!(268435456);
673
    }
674
675
    /// Tests of when a sized `DstLayout` is extended with a DST field.
676
    #[test]
677
    fn test_dst_layout_extend_sized_with_dst() {
678
        // Test that for all combinations of real-world alignments and
679
        // `repr_packed` values, that the extension of a sized `DstLayout`` with
680
        // a DST field correctly computes the trailing offset in the composite
681
        // layout.
682
683
        let aligns = (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap());
684
        let packs = core::iter::once(None).chain(aligns.clone().map(Some));
685
686
        for align in aligns {
687
            for pack in packs.clone() {
688
                let base = DstLayout::for_type::<u8>();
689
                let elem_size = 42;
690
                let trailing_field_offset = 11;
691
692
                let trailing_field = DstLayout {
693
                    align,
694
                    size_info: SizeInfo::SliceDst(TrailingSliceLayout { elem_size, offset: 11 }),
695
                };
696
697
                let composite = base.extend(trailing_field, pack);
698
699
                let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN).get();
700
701
                let align = align.get().min(max_align);
702
703
                assert_eq!(
704
                    composite,
705
                    DstLayout {
706
                        align: NonZeroUsize::new(align).unwrap(),
707
                        size_info: SizeInfo::SliceDst(TrailingSliceLayout {
708
                            elem_size,
709
                            offset: align + trailing_field_offset,
710
                        }),
711
                    }
712
                )
713
            }
714
        }
715
    }
716
717
    /// Tests that calling `pad_to_align` on a sized `DstLayout` adds the
718
    /// expected amount of trailing padding.
719
    #[test]
720
    fn test_dst_layout_pad_to_align_with_sized() {
721
        // For all valid alignments `align`, construct a one-byte layout aligned
722
        // to `align`, call `pad_to_align`, and assert that the size of the
723
        // resulting layout is equal to `align`.
724
        for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) {
725
            let layout = DstLayout { align, size_info: SizeInfo::Sized { size: 1 } };
726
727
            assert_eq!(
728
                layout.pad_to_align(),
729
                DstLayout { align, size_info: SizeInfo::Sized { size: align.get() } }
730
            );
731
        }
732
733
        // Test explicitly-provided combinations of unpadded and padded
734
        // counterparts.
735
736
        macro_rules! test {
737
            (unpadded { size: $unpadded_size:expr, align: $unpadded_align:expr }
738
                    => padded { size: $padded_size:expr, align: $padded_align:expr }) => {
739
                let unpadded = DstLayout {
740
                    align: NonZeroUsize::new($unpadded_align).unwrap(),
741
                    size_info: SizeInfo::Sized { size: $unpadded_size },
742
                };
743
                let padded = unpadded.pad_to_align();
744
745
                assert_eq!(
746
                    padded,
747
                    DstLayout {
748
                        align: NonZeroUsize::new($padded_align).unwrap(),
749
                        size_info: SizeInfo::Sized { size: $padded_size },
750
                    }
751
                );
752
            };
753
        }
754
755
        test!(unpadded { size: 0, align: 4 } => padded { size: 0, align: 4 });
756
        test!(unpadded { size: 1, align: 4 } => padded { size: 4, align: 4 });
757
        test!(unpadded { size: 2, align: 4 } => padded { size: 4, align: 4 });
758
        test!(unpadded { size: 3, align: 4 } => padded { size: 4, align: 4 });
759
        test!(unpadded { size: 4, align: 4 } => padded { size: 4, align: 4 });
760
        test!(unpadded { size: 5, align: 4 } => padded { size: 8, align: 4 });
761
        test!(unpadded { size: 6, align: 4 } => padded { size: 8, align: 4 });
762
        test!(unpadded { size: 7, align: 4 } => padded { size: 8, align: 4 });
763
        test!(unpadded { size: 8, align: 4 } => padded { size: 8, align: 4 });
764
765
        let current_max_align = DstLayout::CURRENT_MAX_ALIGN.get();
766
767
        test!(unpadded { size: 1, align: current_max_align }
768
                => padded { size: current_max_align, align: current_max_align });
769
770
        test!(unpadded { size: current_max_align + 1, align: current_max_align }
771
                => padded { size: current_max_align * 2, align: current_max_align });
772
    }
773
774
    /// Tests that calling `pad_to_align` on a DST `DstLayout` is a no-op.
775
    #[test]
776
    fn test_dst_layout_pad_to_align_with_dst() {
777
        for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) {
778
            for offset in 0..10 {
779
                for elem_size in 0..10 {
780
                    let layout = DstLayout {
781
                        align,
782
                        size_info: SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }),
783
                    };
784
                    assert_eq!(layout.pad_to_align(), layout);
785
                }
786
            }
787
        }
788
    }
789
790
    // This test takes a long time when running under Miri, so we skip it in
791
    // that case. This is acceptable because this is a logic test that doesn't
792
    // attempt to expose UB.
793
    #[test]
794
    #[cfg_attr(miri, ignore)]
795
    fn test_validate_cast_and_convert_metadata() {
796
        #[allow(non_local_definitions)]
797
        impl From<usize> for SizeInfo {
798
            fn from(size: usize) -> SizeInfo {
799
                SizeInfo::Sized { size }
800
            }
801
        }
802
803
        #[allow(non_local_definitions)]
804
        impl From<(usize, usize)> for SizeInfo {
805
            fn from((offset, elem_size): (usize, usize)) -> SizeInfo {
806
                SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
807
            }
808
        }
809
810
        fn layout<S: Into<SizeInfo>>(s: S, align: usize) -> DstLayout {
811
            DstLayout { size_info: s.into(), align: NonZeroUsize::new(align).unwrap() }
812
        }
813
814
        /// This macro accepts arguments in the form of:
815
        ///
816
        ///           layout(_, _, _).validate(_, _, _), Ok(Some((_, _)))
817
        ///                  |  |  |           |  |  |            |  |
818
        ///    base_size ----+  |  |           |  |  |            |  |
819
        ///    align -----------+  |           |  |  |            |  |
820
        ///    trailing_size ------+           |  |  |            |  |
821
        ///    addr ---------------------------+  |  |            |  |
822
        ///    bytes_len -------------------------+  |            |  |
823
        ///    cast_type ----------------------------+            |  |
824
        ///    elems ---------------------------------------------+  |
825
        ///    split_at ---------------------------------------------+
826
        ///
827
        /// `.validate` is shorthand for `.validate_cast_and_convert_metadata`
828
        /// for brevity.
829
        ///
830
        /// Each argument can either be an iterator or a wildcard. Each
831
        /// wildcarded variable is implicitly replaced by an iterator over a
832
        /// representative sample of values for that variable. Each `test!`
833
        /// invocation iterates over every combination of values provided by
834
        /// each variable's iterator (ie, the cartesian product) and validates
835
        /// that the results are expected.
836
        ///
837
        /// The final argument uses the same syntax, but it has a different
838
        /// meaning:
839
        /// - If it is `Ok(pat)`, then the pattern `pat` is supplied to
840
        ///   a matching assert to validate the computed result for each
841
        ///   combination of input values.
842
        /// - If it is `Err(Some(msg) | None)`, then `test!` validates that the
843
        ///   call to `validate_cast_and_convert_metadata` panics with the given
844
        ///   panic message or, if the current Rust toolchain version is too
845
        ///   early to support panicking in `const fn`s, panics with *some*
846
        ///   message. In the latter case, the `const_panic!` macro is used,
847
        ///   which emits code which causes a non-panicking error at const eval
848
        ///   time, but which does panic when invoked at runtime. Thus, it is
849
        ///   merely difficult to predict the *value* of this panic. We deem
850
        ///   that testing against the real panic strings on stable and nightly
851
        ///   toolchains is enough to ensure correctness.
852
        ///
853
        /// Note that the meta-variables that match these variables have the
854
        /// `tt` type, and some valid expressions are not valid `tt`s (such as
855
        /// `a..b`). In this case, wrap the expression in parentheses, and it
856
        /// will become valid `tt`.
857
        macro_rules! test {
858
                ($(:$sizes:expr =>)?
859
                    layout($size:tt, $align:tt)
860
                    .validate($addr:tt, $bytes_len:tt, $cast_type:tt), $expect:pat $(,)?
861
                ) => {
862
                    itertools::iproduct!(
863
                        test!(@generate_size $size),
864
                        test!(@generate_align $align),
865
                        test!(@generate_usize $addr),
866
                        test!(@generate_usize $bytes_len),
867
                        test!(@generate_cast_type $cast_type)
868
                    ).for_each(|(size_info, align, addr, bytes_len, cast_type)| {
869
                        // Temporarily disable the panic hook installed by the test
870
                        // harness. If we don't do this, all panic messages will be
871
                        // kept in an internal log. On its own, this isn't a
872
                        // problem, but if a non-caught panic ever happens (ie, in
873
                        // code later in this test not in this macro), all of the
874
                        // previously-buffered messages will be dumped, hiding the
875
                        // real culprit.
876
                        let previous_hook = std::panic::take_hook();
877
                        // I don't understand why, but this seems to be required in
878
                        // addition to the previous line.
879
                        std::panic::set_hook(Box::new(|_| {}));
880
                        let actual = std::panic::catch_unwind(|| {
881
                            layout(size_info, align).validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
882
                        }).map_err(|d| {
883
                            let msg = d.downcast::<&'static str>().ok().map(|s| *s.as_ref());
884
                            assert!(msg.is_some() || cfg!(not(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0)), "non-string panic messages are not permitted when `--cfg zerocopy_panic_in_const_and_vec_try_reserve` is set");
885
                            msg
886
                        });
887
                        std::panic::set_hook(previous_hook);
888
889
                        assert!(
890
                            matches!(actual, $expect),
891
                            "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?})" ,size_info, align, addr, bytes_len, cast_type
892
                        );
893
                    });
894
                };
895
                (@generate_usize _) => { 0..8 };
896
                // Generate sizes for both Sized and !Sized types.
897
                (@generate_size _) => {
898
                    test!(@generate_size (_)).chain(test!(@generate_size (_, _)))
899
                };
900
                // Generate sizes for both Sized and !Sized types by chaining
901
                // specified iterators for each.
902
                (@generate_size ($sized_sizes:tt | $unsized_sizes:tt)) => {
903
                    test!(@generate_size ($sized_sizes)).chain(test!(@generate_size $unsized_sizes))
904
                };
905
                // Generate sizes for Sized types.
906
                (@generate_size (_)) => { test!(@generate_size (0..8)) };
907
                (@generate_size ($sizes:expr)) => { $sizes.into_iter().map(Into::<SizeInfo>::into) };
908
                // Generate sizes for !Sized types.
909
                (@generate_size ($min_sizes:tt, $elem_sizes:tt)) => {
910
                    itertools::iproduct!(
911
                        test!(@generate_min_size $min_sizes),
912
                        test!(@generate_elem_size $elem_sizes)
913
                    ).map(Into::<SizeInfo>::into)
914
                };
915
                (@generate_fixed_size _) => { (0..8).into_iter().map(Into::<SizeInfo>::into) };
916
                (@generate_min_size _) => { 0..8 };
917
                (@generate_elem_size _) => { 1..8 };
918
                (@generate_align _) => { [1, 2, 4, 8, 16] };
919
                (@generate_opt_usize _) => { [None].into_iter().chain((0..8).map(Some).into_iter()) };
920
                (@generate_cast_type _) => { [CastType::Prefix, CastType::Suffix] };
921
                (@generate_cast_type $variant:ident) => { [CastType::$variant] };
922
                // Some expressions need to be wrapped in parentheses in order to be
923
                // valid `tt`s (required by the top match pattern). See the comment
924
                // below for more details. This arm removes these parentheses to
925
                // avoid generating an `unused_parens` warning.
926
                (@$_:ident ($vals:expr)) => { $vals };
927
                (@$_:ident $vals:expr) => { $vals };
928
            }
929
930
        const EVENS: [usize; 8] = [0, 2, 4, 6, 8, 10, 12, 14];
931
        const ODDS: [usize; 8] = [1, 3, 5, 7, 9, 11, 13, 15];
932
933
        // base_size is too big for the memory region.
934
        test!(
935
            layout(((1..8) | ((1..8), (1..8))), _).validate([0], [0], _),
936
            Ok(Err(MetadataCastError::Size))
937
        );
938
        test!(
939
            layout(((2..8) | ((2..8), (2..8))), _).validate([0], [1], Prefix),
940
            Ok(Err(MetadataCastError::Size))
941
        );
942
        test!(
943
            layout(((2..8) | ((2..8), (2..8))), _).validate([0x1000_0000 - 1], [1], Suffix),
944
            Ok(Err(MetadataCastError::Size))
945
        );
946
947
        // addr is unaligned for prefix cast
948
        test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
949
        test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
950
951
        // addr is aligned, but end of buffer is unaligned for suffix cast
952
        test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
953
        test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
954
955
        // Unfortunately, these constants cannot easily be used in the
956
        // implementation of `validate_cast_and_convert_metadata`, since
957
        // `panic!` consumes a string literal, not an expression.
958
        //
959
        // It's important that these messages be in a separate module. If they
960
        // were at the function's top level, we'd pass them to `test!` as, e.g.,
961
        // `Err(TRAILING)`, which would run into a subtle Rust footgun - the
962
        // `TRAILING` identifier would be treated as a pattern to match rather
963
        // than a value to check for equality.
964
        mod msgs {
965
            pub(super) const TRAILING: &str =
966
                "attempted to cast to slice type with zero-sized element";
967
            pub(super) const OVERFLOW: &str = "`addr` + `bytes_len` > usize::MAX";
968
        }
969
970
        // casts with ZST trailing element types are unsupported
971
        test!(layout((_, [0]), _).validate(_, _, _), Err(Some(msgs::TRAILING) | None),);
972
973
        // addr + bytes_len must not overflow usize
974
        test!(layout(_, _).validate([usize::MAX], (1..100), _), Err(Some(msgs::OVERFLOW) | None));
975
        test!(layout(_, _).validate((1..100), [usize::MAX], _), Err(Some(msgs::OVERFLOW) | None));
976
        test!(
977
            layout(_, _).validate(
978
                [usize::MAX / 2 + 1, usize::MAX],
979
                [usize::MAX / 2 + 1, usize::MAX],
980
                _
981
            ),
982
            Err(Some(msgs::OVERFLOW) | None)
983
        );
984
985
        // Validates that `validate_cast_and_convert_metadata` satisfies its own
986
        // documented safety postconditions, and also a few other properties
987
        // that aren't documented but we want to guarantee anyway.
988
        fn validate_behavior(
989
            (layout, addr, bytes_len, cast_type): (DstLayout, usize, usize, CastType),
990
        ) {
991
            if let Ok((elems, split_at)) =
992
                layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
993
            {
994
                let (size_info, align) = (layout.size_info, layout.align);
995
                let debug_str = format!(
996
                    "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?}) => ({}, {})",
997
                    size_info, align, addr, bytes_len, cast_type, elems, split_at
998
                );
999
1000
                // If this is a sized type (no trailing slice), then `elems` is
1001
                // meaningless, but in practice we set it to 0. Callers are not
1002
                // allowed to rely on this, but a lot of math is nicer if
1003
                // they're able to, and some callers might accidentally do that.
1004
                let sized = matches!(layout.size_info, SizeInfo::Sized { .. });
1005
                assert!(!(sized && elems != 0), "{}", debug_str);
1006
1007
                let resulting_size = match layout.size_info {
1008
                    SizeInfo::Sized { size } => size,
1009
                    SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => {
1010
                        let padded_size = |elems| {
1011
                            let without_padding = offset + elems * elem_size;
1012
                            without_padding + util::padding_needed_for(without_padding, align)
1013
                        };
1014
1015
                        let resulting_size = padded_size(elems);
1016
                        // Test that `validate_cast_and_convert_metadata`
1017
                        // computed the largest possible value that fits in the
1018
                        // given range.
1019
                        assert!(padded_size(elems + 1) > bytes_len, "{}", debug_str);
1020
                        resulting_size
1021
                    }
1022
                };
1023
1024
                // Test safety postconditions guaranteed by
1025
                // `validate_cast_and_convert_metadata`.
1026
                assert!(resulting_size <= bytes_len, "{}", debug_str);
1027
                match cast_type {
1028
                    CastType::Prefix => {
1029
                        assert_eq!(addr % align, 0, "{}", debug_str);
1030
                        assert_eq!(resulting_size, split_at, "{}", debug_str);
1031
                    }
1032
                    CastType::Suffix => {
1033
                        assert_eq!(split_at, bytes_len - resulting_size, "{}", debug_str);
1034
                        assert_eq!((addr + split_at) % align, 0, "{}", debug_str);
1035
                    }
1036
                }
1037
            } else {
1038
                let min_size = match layout.size_info {
1039
                    SizeInfo::Sized { size } => size,
1040
                    SizeInfo::SliceDst(TrailingSliceLayout { offset, .. }) => {
1041
                        offset + util::padding_needed_for(offset, layout.align)
1042
                    }
1043
                };
1044
1045
                // If a cast is invalid, it is either because...
1046
                // 1. there are insufficent bytes at the given region for type:
1047
                let insufficient_bytes = bytes_len < min_size;
1048
                // 2. performing the cast would misalign type:
1049
                let base = match cast_type {
1050
                    CastType::Prefix => 0,
1051
                    CastType::Suffix => bytes_len,
1052
                };
1053
                let misaligned = (base + addr) % layout.align != 0;
1054
1055
                assert!(insufficient_bytes || misaligned);
1056
            }
1057
        }
1058
1059
        let sizes = 0..8;
1060
        let elem_sizes = 1..8;
1061
        let size_infos = sizes
1062
            .clone()
1063
            .map(Into::<SizeInfo>::into)
1064
            .chain(itertools::iproduct!(sizes, elem_sizes).map(Into::<SizeInfo>::into));
1065
        let layouts = itertools::iproduct!(size_infos, [1, 2, 4, 8, 16, 32])
1066
                .filter(|(size_info, align)| !matches!(size_info, SizeInfo::Sized { size } if size % align != 0))
1067
                .map(|(size_info, align)| layout(size_info, align));
1068
        itertools::iproduct!(layouts, 0..8, 0..8, [CastType::Prefix, CastType::Suffix])
1069
            .for_each(validate_behavior);
1070
    }
1071
1072
    #[test]
1073
    #[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
1074
    fn test_validate_rust_layout() {
1075
        use crate::util::testutil::*;
1076
        use core::{
1077
            convert::TryInto as _,
1078
            ptr::{self, NonNull},
1079
        };
1080
1081
        // This test synthesizes pointers with various metadata and uses Rust's
1082
        // built-in APIs to confirm that Rust makes decisions about type layout
1083
        // which are consistent with what we believe is guaranteed by the
1084
        // language. If this test fails, it doesn't just mean our code is wrong
1085
        // - it means we're misunderstanding the language's guarantees.
1086
1087
        #[derive(Debug)]
1088
        struct MacroArgs {
1089
            offset: usize,
1090
            align: NonZeroUsize,
1091
            elem_size: Option<usize>,
1092
        }
1093
1094
        /// # Safety
1095
        ///
1096
        /// `test` promises to only call `addr_of_slice_field` on a `NonNull<T>`
1097
        /// which points to a valid `T`.
1098
        ///
1099
        /// `with_elems` must produce a pointer which points to a valid `T`.
1100
        fn test<T: ?Sized, W: Fn(usize) -> NonNull<T>>(
1101
            args: MacroArgs,
1102
            with_elems: W,
1103
            addr_of_slice_field: Option<fn(NonNull<T>) -> NonNull<u8>>,
1104
        ) {
1105
            let dst = args.elem_size.is_some();
1106
            let layout = {
1107
                let size_info = match args.elem_size {
1108
                    Some(elem_size) => {
1109
                        SizeInfo::SliceDst(TrailingSliceLayout { offset: args.offset, elem_size })
1110
                    }
1111
                    None => SizeInfo::Sized {
1112
                        // Rust only supports types whose sizes are a multiple
1113
                        // of their alignment. If the macro created a type like
1114
                        // this:
1115
                        //
1116
                        //   #[repr(C, align(2))]
1117
                        //   struct Foo([u8; 1]);
1118
                        //
1119
                        // ...then Rust will automatically round the type's size
1120
                        // up to 2.
1121
                        size: args.offset + util::padding_needed_for(args.offset, args.align),
1122
                    },
1123
                };
1124
                DstLayout { size_info, align: args.align }
1125
            };
1126
1127
            for elems in 0..128 {
1128
                let ptr = with_elems(elems);
1129
1130
                if let Some(addr_of_slice_field) = addr_of_slice_field {
1131
                    let slc_field_ptr = addr_of_slice_field(ptr).as_ptr();
1132
                    // SAFETY: Both `slc_field_ptr` and `ptr` are pointers to
1133
                    // the same valid Rust object.
1134
                    #[allow(clippy::incompatible_msrv)]
1135
                    // Work around https://github.com/rust-lang/rust-clippy/issues/12280
1136
                    let offset: usize =
1137
                        unsafe { slc_field_ptr.byte_offset_from(ptr.as_ptr()).try_into().unwrap() };
1138
                    assert_eq!(offset, args.offset);
1139
                }
1140
1141
                // SAFETY: `ptr` points to a valid `T`.
1142
                let (size, align) = unsafe {
1143
                    (mem::size_of_val_raw(ptr.as_ptr()), mem::align_of_val_raw(ptr.as_ptr()))
1144
                };
1145
1146
                // Avoid expensive allocation when running under Miri.
1147
                let assert_msg = if !cfg!(miri) {
1148
                    format!("\n{:?}\nsize:{}, align:{}", args, size, align)
1149
                } else {
1150
                    String::new()
1151
                };
1152
1153
                let without_padding =
1154
                    args.offset + args.elem_size.map(|elem_size| elems * elem_size).unwrap_or(0);
1155
                assert!(size >= without_padding, "{}", assert_msg);
1156
                assert_eq!(align, args.align.get(), "{}", assert_msg);
1157
1158
                // This encodes the most important part of the test: our
1159
                // understanding of how Rust determines the layout of repr(C)
1160
                // types. Sized repr(C) types are trivial, but DST types have
1161
                // some subtlety. Note that:
1162
                // - For sized types, `without_padding` is just the size of the
1163
                //   type that we constructed for `Foo`. Since we may have
1164
                //   requested a larger alignment, `Foo` may actually be larger
1165
                //   than this, hence `padding_needed_for`.
1166
                // - For unsized types, `without_padding` is dynamically
1167
                //   computed from the offset, the element size, and element
1168
                //   count. We expect that the size of the object should be
1169
                //   `offset + elem_size * elems` rounded up to the next
1170
                //   alignment.
1171
                let expected_size =
1172
                    without_padding + util::padding_needed_for(without_padding, args.align);
1173
                assert_eq!(expected_size, size, "{}", assert_msg);
1174
1175
                // For zero-sized element types,
1176
                // `validate_cast_and_convert_metadata` just panics, so we skip
1177
                // testing those types.
1178
                if args.elem_size.map(|elem_size| elem_size > 0).unwrap_or(true) {
1179
                    let addr = ptr.addr().get();
1180
                    let (got_elems, got_split_at) = layout
1181
                        .validate_cast_and_convert_metadata(addr, size, CastType::Prefix)
1182
                        .unwrap();
1183
                    // Avoid expensive allocation when running under Miri.
1184
                    let assert_msg = if !cfg!(miri) {
1185
                        format!(
1186
                            "{}\nvalidate_cast_and_convert_metadata({}, {})",
1187
                            assert_msg, addr, size,
1188
                        )
1189
                    } else {
1190
                        String::new()
1191
                    };
1192
                    assert_eq!(got_split_at, size, "{}", assert_msg);
1193
                    if dst {
1194
                        assert!(got_elems >= elems, "{}", assert_msg);
1195
                        if got_elems != elems {
1196
                            // If `validate_cast_and_convert_metadata`
1197
                            // returned more elements than `elems`, that
1198
                            // means that `elems` is not the maximum number
1199
                            // of elements that can fit in `size` - in other
1200
                            // words, there is enough padding at the end of
1201
                            // the value to fit at least one more element.
1202
                            // If we use this metadata to synthesize a
1203
                            // pointer, despite having a different element
1204
                            // count, we still expect it to have the same
1205
                            // size.
1206
                            let got_ptr = with_elems(got_elems);
1207
                            // SAFETY: `got_ptr` is a pointer to a valid `T`.
1208
                            let size_of_got_ptr = unsafe { mem::size_of_val_raw(got_ptr.as_ptr()) };
1209
                            assert_eq!(size_of_got_ptr, size, "{}", assert_msg);
1210
                        }
1211
                    } else {
1212
                        // For sized casts, the returned element value is
1213
                        // technically meaningless, and we don't guarantee any
1214
                        // particular value. In practice, it's always zero.
1215
                        assert_eq!(got_elems, 0, "{}", assert_msg)
1216
                    }
1217
                }
1218
            }
1219
        }
1220
1221
        macro_rules! validate_against_rust {
1222
                ($offset:literal, $align:literal $(, $elem_size:literal)?) => {{
1223
                    #[repr(C, align($align))]
1224
                    struct Foo([u8; $offset]$(, [[u8; $elem_size]])?);
1225
1226
                    let args = MacroArgs {
1227
                        offset: $offset,
1228
                        align: $align.try_into().unwrap(),
1229
                        elem_size: {
1230
                            #[allow(unused)]
1231
                            let ret = None::<usize>;
1232
                            $(let ret = Some($elem_size);)?
1233
                            ret
1234
                        }
1235
                    };
1236
1237
                    #[repr(C, align($align))]
1238
                    struct FooAlign;
1239
                    // Create an aligned buffer to use in order to synthesize
1240
                    // pointers to `Foo`. We don't ever load values from these
1241
                    // pointers - we just do arithmetic on them - so having a "real"
1242
                    // block of memory as opposed to a validly-aligned-but-dangling
1243
                    // pointer is only necessary to make Miri happy since we run it
1244
                    // with "strict provenance" checking enabled.
1245
                    let aligned_buf = Align::<_, FooAlign>::new([0u8; 1024]);
1246
                    let with_elems = |elems| {
1247
                        let slc = NonNull::slice_from_raw_parts(NonNull::from(&aligned_buf.t), elems);
1248
                        #[allow(clippy::as_conversions)]
1249
                        NonNull::new(slc.as_ptr() as *mut Foo).unwrap()
1250
                    };
1251
                    let addr_of_slice_field = {
1252
                        #[allow(unused)]
1253
                        let f = None::<fn(NonNull<Foo>) -> NonNull<u8>>;
1254
                        $(
1255
                            // SAFETY: `test` promises to only call `f` with a `ptr`
1256
                            // to a valid `Foo`.
1257
                            let f: Option<fn(NonNull<Foo>) -> NonNull<u8>> = Some(|ptr: NonNull<Foo>| unsafe {
1258
                                NonNull::new(ptr::addr_of_mut!((*ptr.as_ptr()).1)).unwrap().cast::<u8>()
1259
                            });
1260
                            let _ = $elem_size;
1261
                        )?
1262
                        f
1263
                    };
1264
1265
                    test::<Foo, _>(args, with_elems, addr_of_slice_field);
1266
                }};
1267
            }
1268
1269
        // Every permutation of:
1270
        // - offset in [0, 4]
1271
        // - align in [1, 16]
1272
        // - elem_size in [0, 4] (plus no elem_size)
1273
        validate_against_rust!(0, 1);
1274
        validate_against_rust!(0, 1, 0);
1275
        validate_against_rust!(0, 1, 1);
1276
        validate_against_rust!(0, 1, 2);
1277
        validate_against_rust!(0, 1, 3);
1278
        validate_against_rust!(0, 1, 4);
1279
        validate_against_rust!(0, 2);
1280
        validate_against_rust!(0, 2, 0);
1281
        validate_against_rust!(0, 2, 1);
1282
        validate_against_rust!(0, 2, 2);
1283
        validate_against_rust!(0, 2, 3);
1284
        validate_against_rust!(0, 2, 4);
1285
        validate_against_rust!(0, 4);
1286
        validate_against_rust!(0, 4, 0);
1287
        validate_against_rust!(0, 4, 1);
1288
        validate_against_rust!(0, 4, 2);
1289
        validate_against_rust!(0, 4, 3);
1290
        validate_against_rust!(0, 4, 4);
1291
        validate_against_rust!(0, 8);
1292
        validate_against_rust!(0, 8, 0);
1293
        validate_against_rust!(0, 8, 1);
1294
        validate_against_rust!(0, 8, 2);
1295
        validate_against_rust!(0, 8, 3);
1296
        validate_against_rust!(0, 8, 4);
1297
        validate_against_rust!(0, 16);
1298
        validate_against_rust!(0, 16, 0);
1299
        validate_against_rust!(0, 16, 1);
1300
        validate_against_rust!(0, 16, 2);
1301
        validate_against_rust!(0, 16, 3);
1302
        validate_against_rust!(0, 16, 4);
1303
        validate_against_rust!(1, 1);
1304
        validate_against_rust!(1, 1, 0);
1305
        validate_against_rust!(1, 1, 1);
1306
        validate_against_rust!(1, 1, 2);
1307
        validate_against_rust!(1, 1, 3);
1308
        validate_against_rust!(1, 1, 4);
1309
        validate_against_rust!(1, 2);
1310
        validate_against_rust!(1, 2, 0);
1311
        validate_against_rust!(1, 2, 1);
1312
        validate_against_rust!(1, 2, 2);
1313
        validate_against_rust!(1, 2, 3);
1314
        validate_against_rust!(1, 2, 4);
1315
        validate_against_rust!(1, 4);
1316
        validate_against_rust!(1, 4, 0);
1317
        validate_against_rust!(1, 4, 1);
1318
        validate_against_rust!(1, 4, 2);
1319
        validate_against_rust!(1, 4, 3);
1320
        validate_against_rust!(1, 4, 4);
1321
        validate_against_rust!(1, 8);
1322
        validate_against_rust!(1, 8, 0);
1323
        validate_against_rust!(1, 8, 1);
1324
        validate_against_rust!(1, 8, 2);
1325
        validate_against_rust!(1, 8, 3);
1326
        validate_against_rust!(1, 8, 4);
1327
        validate_against_rust!(1, 16);
1328
        validate_against_rust!(1, 16, 0);
1329
        validate_against_rust!(1, 16, 1);
1330
        validate_against_rust!(1, 16, 2);
1331
        validate_against_rust!(1, 16, 3);
1332
        validate_against_rust!(1, 16, 4);
1333
        validate_against_rust!(2, 1);
1334
        validate_against_rust!(2, 1, 0);
1335
        validate_against_rust!(2, 1, 1);
1336
        validate_against_rust!(2, 1, 2);
1337
        validate_against_rust!(2, 1, 3);
1338
        validate_against_rust!(2, 1, 4);
1339
        validate_against_rust!(2, 2);
1340
        validate_against_rust!(2, 2, 0);
1341
        validate_against_rust!(2, 2, 1);
1342
        validate_against_rust!(2, 2, 2);
1343
        validate_against_rust!(2, 2, 3);
1344
        validate_against_rust!(2, 2, 4);
1345
        validate_against_rust!(2, 4);
1346
        validate_against_rust!(2, 4, 0);
1347
        validate_against_rust!(2, 4, 1);
1348
        validate_against_rust!(2, 4, 2);
1349
        validate_against_rust!(2, 4, 3);
1350
        validate_against_rust!(2, 4, 4);
1351
        validate_against_rust!(2, 8);
1352
        validate_against_rust!(2, 8, 0);
1353
        validate_against_rust!(2, 8, 1);
1354
        validate_against_rust!(2, 8, 2);
1355
        validate_against_rust!(2, 8, 3);
1356
        validate_against_rust!(2, 8, 4);
1357
        validate_against_rust!(2, 16);
1358
        validate_against_rust!(2, 16, 0);
1359
        validate_against_rust!(2, 16, 1);
1360
        validate_against_rust!(2, 16, 2);
1361
        validate_against_rust!(2, 16, 3);
1362
        validate_against_rust!(2, 16, 4);
1363
        validate_against_rust!(3, 1);
1364
        validate_against_rust!(3, 1, 0);
1365
        validate_against_rust!(3, 1, 1);
1366
        validate_against_rust!(3, 1, 2);
1367
        validate_against_rust!(3, 1, 3);
1368
        validate_against_rust!(3, 1, 4);
1369
        validate_against_rust!(3, 2);
1370
        validate_against_rust!(3, 2, 0);
1371
        validate_against_rust!(3, 2, 1);
1372
        validate_against_rust!(3, 2, 2);
1373
        validate_against_rust!(3, 2, 3);
1374
        validate_against_rust!(3, 2, 4);
1375
        validate_against_rust!(3, 4);
1376
        validate_against_rust!(3, 4, 0);
1377
        validate_against_rust!(3, 4, 1);
1378
        validate_against_rust!(3, 4, 2);
1379
        validate_against_rust!(3, 4, 3);
1380
        validate_against_rust!(3, 4, 4);
1381
        validate_against_rust!(3, 8);
1382
        validate_against_rust!(3, 8, 0);
1383
        validate_against_rust!(3, 8, 1);
1384
        validate_against_rust!(3, 8, 2);
1385
        validate_against_rust!(3, 8, 3);
1386
        validate_against_rust!(3, 8, 4);
1387
        validate_against_rust!(3, 16);
1388
        validate_against_rust!(3, 16, 0);
1389
        validate_against_rust!(3, 16, 1);
1390
        validate_against_rust!(3, 16, 2);
1391
        validate_against_rust!(3, 16, 3);
1392
        validate_against_rust!(3, 16, 4);
1393
        validate_against_rust!(4, 1);
1394
        validate_against_rust!(4, 1, 0);
1395
        validate_against_rust!(4, 1, 1);
1396
        validate_against_rust!(4, 1, 2);
1397
        validate_against_rust!(4, 1, 3);
1398
        validate_against_rust!(4, 1, 4);
1399
        validate_against_rust!(4, 2);
1400
        validate_against_rust!(4, 2, 0);
1401
        validate_against_rust!(4, 2, 1);
1402
        validate_against_rust!(4, 2, 2);
1403
        validate_against_rust!(4, 2, 3);
1404
        validate_against_rust!(4, 2, 4);
1405
        validate_against_rust!(4, 4);
1406
        validate_against_rust!(4, 4, 0);
1407
        validate_against_rust!(4, 4, 1);
1408
        validate_against_rust!(4, 4, 2);
1409
        validate_against_rust!(4, 4, 3);
1410
        validate_against_rust!(4, 4, 4);
1411
        validate_against_rust!(4, 8);
1412
        validate_against_rust!(4, 8, 0);
1413
        validate_against_rust!(4, 8, 1);
1414
        validate_against_rust!(4, 8, 2);
1415
        validate_against_rust!(4, 8, 3);
1416
        validate_against_rust!(4, 8, 4);
1417
        validate_against_rust!(4, 16);
1418
        validate_against_rust!(4, 16, 0);
1419
        validate_against_rust!(4, 16, 1);
1420
        validate_against_rust!(4, 16, 2);
1421
        validate_against_rust!(4, 16, 3);
1422
        validate_against_rust!(4, 16, 4);
1423
    }
1424
}