zerocopy/layout.rs
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
10use core::{mem, num::NonZeroUsize};
11
12use crate::util;
13
14/// The target pointer width, counted in bits.
15const 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)]
31pub 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)]
43pub(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)]
50pub(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
68impl 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 const fn try_to_nonzero_elem_size(&self) -> Option<SizeInfo<NonZeroUsize>> {
73 Some(match *self {
74 SizeInfo::Sized { size } => SizeInfo::Sized { size },
75 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => {
76 if let Some(elem_size) = NonZeroUsize::new(elem_size) {
77 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
78 } else {
79 return None;
80 }
81 }
82 })
83 }
84}
85
86#[doc(hidden)]
87#[derive(Copy, Clone)]
88#[cfg_attr(test, derive(Debug))]
89#[allow(missing_debug_implementations)]
90pub enum CastType {
91 Prefix,
92 Suffix,
93}
94
95#[cfg_attr(test, derive(Debug))]
96pub(crate) enum MetadataCastError {
97 Alignment,
98 Size,
99}
100
101impl 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 const fn assume_shallow_unpadded(self) -> Self {
152 Self { statically_shallow_unpadded: true, ..self }
153 }
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 pub const fn new_zst(repr_align: Option<NonZeroUsize>) -> DstLayout {
170 let align = match repr_align {
171 Some(align) => align,
172 None => Self::MIN_ALIGN,
173 };
174
175 const_assert!(align.get().is_power_of_two());
176
177 DstLayout {
178 align,
179 size_info: SizeInfo::Sized { size: 0 },
180 statically_shallow_unpadded: true,
181 }
182 }
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 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 align: match NonZeroUsize::new(mem::align_of::<T>()) {
200 Some(align) => align,
201 None => const_unreachable!(),
202 },
203 size_info: SizeInfo::Sized { size: mem::size_of::<T>() },
204 statically_shallow_unpadded: false,
205 }
206 }
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 pub const fn for_unpadded_type<T>() -> DstLayout {
218 Self::for_type::<T>().assume_shallow_unpadded()
219 }
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 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 align: match NonZeroUsize::new(mem::align_of::<T>()) {
237 Some(align) => align,
238 None => const_unreachable!(),
239 },
240 size_info: SizeInfo::SliceDst(TrailingSliceLayout {
241 offset: 0,
242 elem_size: mem::size_of::<T>(),
243 }),
244 statically_shallow_unpadded: true,
245 }
246 }
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 pub const fn for_repr_c_struct(
265 repr_align: Option<NonZeroUsize>,
266 repr_packed: Option<NonZeroUsize>,
267 fields: &[DstLayout],
268 ) -> DstLayout {
269 let mut layout = DstLayout::new_zst(repr_align);
270
271 let mut i = 0;
272 #[allow(clippy::arithmetic_side_effects)]
273 while i < fields.len() {
274 #[allow(clippy::indexing_slicing)]
275 let field = fields[i];
276 layout = layout.extend(field, repr_packed);
277 i += 1;
278 }
279
280 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 layout
295 }
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 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 let max_align = match repr_packed {
331 Some(max_align) => max_align,
332 None => Self::THEORETICAL_MAX_ALIGN,
333 };
334
335 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 const_debug_assert!(self.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
343 const_debug_assert!(field.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
344 if let Some(repr_packed) = repr_packed {
345 const_debug_assert!(repr_packed.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
346 }
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 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 let align = max(self.align, field_align);
362
363 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 SizeInfo::SliceDst(..) => const_panic!("Cannot extend a DST with additional fields."),
367
368 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 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 let offset = match preceding_size.checked_add(padding) {
386 Some(offset) => offset,
387 None => const_panic!("Adding padding to `self`'s size overflows `usize`."),
388 };
389
390 (
391 padding,
392 match field.size_info {
393 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 let size = match offset.checked_add(field_size) {
406 Some(size) => size,
407 None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
408 };
409 SizeInfo::Sized { size }
410 }
411 SizeInfo::SliceDst(TrailingSliceLayout {
412 offset: trailing_offset,
413 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 let offset = match offset.checked_add(trailing_offset) {
428 Some(offset) => offset,
429 None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
430 };
431 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
432 }
433 },
434 )
435 }
436 };
437
438 let statically_shallow_unpadded = self.statically_shallow_unpadded
439 && field.statically_shallow_unpadded
440 && interfield_padding == 0;
441
442 DstLayout { align, size_info, statically_shallow_unpadded }
443 }
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 pub const fn pad_to_align(self) -> Self {
477 use util::padding_needed_for;
478
479 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 SizeInfo::Sized { size: unpadded_size } => {
483 let padding = padding_needed_for(unpadded_size, self.align);
484 let size = match unpadded_size.checked_add(padding) {
485 Some(size) => size,
486 None => const_panic!("Adding padding caused size to overflow `usize`."),
487 };
488 (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 size_info @ SizeInfo::SliceDst(_) => (0, size_info),
495 };
496
497 let statically_shallow_unpadded = self.statically_shallow_unpadded && static_padding == 0;
498
499 DstLayout { align: self.align, size_info, statically_shallow_unpadded }
500 }
501
502 /// Produces `true` if `self` requires static padding; otherwise `false`.
503 #[must_use]
504 #[inline(always)]
505 pub const fn requires_static_padding(self) -> bool {
506 !self.statically_shallow_unpadded
507 }
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 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 match self.size_info {
517 SizeInfo::Sized { .. } => false,
518 SizeInfo::SliceDst(trailing_slice_layout) => {
519 // SAFETY: This predicate is formally proved sound by
520 // `proofs::prove_requires_dynamic_padding`.
521 trailing_slice_layout.offset % self.align.get() != 0
522 || trailing_slice_layout.elem_size % self.align.get() != 0
523 }
524 }
525 }
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 pub(crate) const fn validate_cast_and_convert_metadata(
594 &self,
595 addr: usize,
596 bytes_len: usize,
597 cast_type: CastType,
598 ) -> 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 let size_info = match self.size_info.try_to_nonzero_elem_size() {
620 Some(size_info) => size_info,
621 None => const_panic!("attempted to cast to slice type with zero-sized element"),
622 };
623
624 // Precondition
625 __const_debug_assert!(
626 addr.checked_add(bytes_len).is_some(),
627 "`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 let offset = match cast_type {
646 CastType::Prefix => 0,
647 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 if (addr + offset) % self.align.get() != 0 {
656 return Err(MetadataCastError::Alignment);
657 }
658 }
659
660 let (elems, self_bytes) = match size_info {
661 SizeInfo::Sized { size } => {
662 if size > bytes_len {
663 return Err(MetadataCastError::Size);
664 }
665 (0, size)
666 }
667 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 let max_total_bytes =
673 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 let max_slice_and_padding_bytes = match max_total_bytes.checked_sub(offset) {
680 Some(max) => max,
681 // `bytes_len` too small even for 0 trailing slice elements.
682 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 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 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 let self_bytes =
719 without_padding + util::padding_needed_for(without_padding, self.align);
720 (elems, self_bytes)
721 }
722 };
723
724 __const_debug_assert!(self_bytes <= bytes_len);
725
726 let split_at = match cast_type {
727 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 CastType::Suffix => bytes_len - self_bytes,
735 };
736
737 Ok((elems, split_at))
738 }
739}
740
741pub(crate) use cast_from_raw::cast_from_raw;
742mod 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 pub(crate) fn cast_from_raw<Src, Dst>(src: PtrInner<'_, Src>) -> PtrInner<'_, Dst>
757 where
758 Src: KnownLayout<PointerMetadata = usize> + ?Sized,
759 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 const fn try_compute(src: &DstLayout, dst: &DstLayout) -> Option<CastParams> {
833 if src.align.get() < dst.align.get() {
834 return None;
835 }
836
837 let (src, dst) = if let (SizeInfo::SliceDst(src), SizeInfo::SliceDst(dst)) =
838 (src.size_info, dst.size_info)
839 {
840 (src, dst)
841 } else {
842 return None;
843 };
844
845 let offset_delta = if let Some(od) = src.offset.checked_sub(dst.offset) {
846 od
847 } else {
848 return None;
849 };
850
851 let dst_elem_size = if let Some(e) = NonZeroUsize::new(dst.elem_size) {
852 e
853 } else {
854 return None;
855 };
856
857 // PANICS: `dst_elem_size: NonZeroUsize`, so this won't div by zero.
858 #[allow(clippy::arithmetic_side_effects)]
859 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 let elem_remainder = src.elem_size % dst_elem_size.get();
864
865 if delta_mod_other_elem != 0 || src.elem_size < dst.elem_size || elem_remainder != 0
866 {
867 return None;
868 }
869
870 // PANICS: `dst_elem_size: NonZeroUsize`, so this won't div by zero.
871 #[allow(clippy::arithmetic_side_effects)]
872 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 let elem_multiple = src.elem_size / dst_elem_size.get();
877
878 // SAFETY: We checked above that `src.align >= dst.align`.
879 Some(CastParams {
880 // SAFETY: We checked above that this is an exact ratio.
881 offset_delta_elems,
882 // SAFETY: We checked above that this is an exact ratio.
883 elem_multiple,
884 })
885 }
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 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)]
907 unsafe {
908 self.offset_delta_elems
909 .unchecked_add(src_meta.unchecked_mul(self.elem_multiple))
910 }
911 }
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 let src_meta = <Src as KnownLayout>::pointer_to_metadata(src.as_non_null().as_ptr());
933 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 let dst_meta = unsafe { params.cast_metadata(src_meta) };
938
939 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 unsafe { PtrInner::new(dst) }
948 }
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)]
957mod 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!(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");
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 let (size, align) = unsafe {
1508 (mem::size_of_val_raw(ptr.as_ptr()), mem::align_of_val_raw(ptr.as_ptr()))
1509 };
1510
1511 // Avoid expensive allocation when running under Miri.
1512 let assert_msg = if !cfg!(miri) {
1513 format!("\n{:?}\nsize:{}, align:{}", args, size, align)
1514 } else {
1515 String::new()
1516 };
1517
1518 let without_padding =
1519 args.offset + args.elem_size.map(|elem_size| elems * elem_size).unwrap_or(0);
1520 assert!(size >= without_padding, "{}", assert_msg);
1521 assert_eq!(align, args.align.get(), "{}", assert_msg);
1522
1523 // This encodes the most important part of the test: our
1524 // understanding of how Rust determines the layout of repr(C)
1525 // types. Sized repr(C) types are trivial, but DST types have
1526 // some subtlety. Note that:
1527 // - For sized types, `without_padding` is just the size of the
1528 // type that we constructed for `Foo`. Since we may have
1529 // requested a larger alignment, `Foo` may actually be larger
1530 // than this, hence `padding_needed_for`.
1531 // - For unsized types, `without_padding` is dynamically
1532 // computed from the offset, the element size, and element
1533 // count. We expect that the size of the object should be
1534 // `offset + elem_size * elems` rounded up to the next
1535 // alignment.
1536 let expected_size =
1537 without_padding + util::padding_needed_for(without_padding, args.align);
1538 assert_eq!(expected_size, size, "{}", assert_msg);
1539
1540 // For zero-sized element types,
1541 // `validate_cast_and_convert_metadata` just panics, so we skip
1542 // testing those types.
1543 if args.elem_size.map(|elem_size| elem_size > 0).unwrap_or(true) {
1544 let addr = ptr.addr().get();
1545 let (got_elems, got_split_at) = layout
1546 .validate_cast_and_convert_metadata(addr, size, CastType::Prefix)
1547 .unwrap();
1548 // Avoid expensive allocation when running under Miri.
1549 let assert_msg = if !cfg!(miri) {
1550 format!(
1551 "{}\nvalidate_cast_and_convert_metadata({}, {})",
1552 assert_msg, addr, size,
1553 )
1554 } else {
1555 String::new()
1556 };
1557 assert_eq!(got_split_at, size, "{}", assert_msg);
1558 if dst {
1559 assert!(got_elems >= elems, "{}", assert_msg);
1560 if got_elems != elems {
1561 // If `validate_cast_and_convert_metadata`
1562 // returned more elements than `elems`, that
1563 // means that `elems` is not the maximum number
1564 // of elements that can fit in `size` - in other
1565 // words, there is enough padding at the end of
1566 // the value to fit at least one more element.
1567 // If we use this metadata to synthesize a
1568 // pointer, despite having a different element
1569 // count, we still expect it to have the same
1570 // size.
1571 let got_ptr = with_elems(got_elems);
1572 // SAFETY: `got_ptr` is a pointer to a valid `T`.
1573 let size_of_got_ptr = unsafe { mem::size_of_val_raw(got_ptr.as_ptr()) };
1574 assert_eq!(size_of_got_ptr, size, "{}", assert_msg);
1575 }
1576 } else {
1577 // For sized casts, the returned element value is
1578 // technically meaningless, and we don't guarantee any
1579 // particular value. In practice, it's always zero.
1580 assert_eq!(got_elems, 0, "{}", assert_msg)
1581 }
1582 }
1583 }
1584 }
1585
1586 macro_rules! validate_against_rust {
1587 ($offset:literal, $align:literal $(, $elem_size:literal)?) => {{
1588 #[repr(C, align($align))]
1589 struct Foo([u8; $offset]$(, [[u8; $elem_size]])?);
1590
1591 let args = MacroArgs {
1592 offset: $offset,
1593 align: $align.try_into().unwrap(),
1594 elem_size: {
1595 #[allow(unused)]
1596 let ret = None::<usize>;
1597 $(let ret = Some($elem_size);)?
1598 ret
1599 }
1600 };
1601
1602 #[repr(C, align($align))]
1603 struct FooAlign;
1604 // Create an aligned buffer to use in order to synthesize
1605 // pointers to `Foo`. We don't ever load values from these
1606 // pointers - we just do arithmetic on them - so having a "real"
1607 // block of memory as opposed to a validly-aligned-but-dangling
1608 // pointer is only necessary to make Miri happy since we run it
1609 // with "strict provenance" checking enabled.
1610 let aligned_buf = Align::<_, FooAlign>::new([0u8; 1024]);
1611 let with_elems = |elems| {
1612 let slc = NonNull::slice_from_raw_parts(NonNull::from(&aligned_buf.t), elems);
1613 #[allow(clippy::as_conversions)]
1614 NonNull::new(slc.as_ptr() as *mut Foo).unwrap()
1615 };
1616 let addr_of_slice_field = {
1617 #[allow(unused)]
1618 let f = None::<fn(NonNull<Foo>) -> NonNull<u8>>;
1619 $(
1620 // SAFETY: `test` promises to only call `f` with a `ptr`
1621 // to a valid `Foo`.
1622 let f: Option<fn(NonNull<Foo>) -> NonNull<u8>> = Some(|ptr: NonNull<Foo>| unsafe {
1623 NonNull::new(ptr::addr_of_mut!((*ptr.as_ptr()).1)).unwrap().cast::<u8>()
1624 });
1625 let _ = $elem_size;
1626 )?
1627 f
1628 };
1629
1630 test::<Foo, _>(args, with_elems, addr_of_slice_field);
1631 }};
1632 }
1633
1634 // Every permutation of:
1635 // - offset in [0, 4]
1636 // - align in [1, 16]
1637 // - elem_size in [0, 4] (plus no elem_size)
1638 validate_against_rust!(0, 1);
1639 validate_against_rust!(0, 1, 0);
1640 validate_against_rust!(0, 1, 1);
1641 validate_against_rust!(0, 1, 2);
1642 validate_against_rust!(0, 1, 3);
1643 validate_against_rust!(0, 1, 4);
1644 validate_against_rust!(0, 2);
1645 validate_against_rust!(0, 2, 0);
1646 validate_against_rust!(0, 2, 1);
1647 validate_against_rust!(0, 2, 2);
1648 validate_against_rust!(0, 2, 3);
1649 validate_against_rust!(0, 2, 4);
1650 validate_against_rust!(0, 4);
1651 validate_against_rust!(0, 4, 0);
1652 validate_against_rust!(0, 4, 1);
1653 validate_against_rust!(0, 4, 2);
1654 validate_against_rust!(0, 4, 3);
1655 validate_against_rust!(0, 4, 4);
1656 validate_against_rust!(0, 8);
1657 validate_against_rust!(0, 8, 0);
1658 validate_against_rust!(0, 8, 1);
1659 validate_against_rust!(0, 8, 2);
1660 validate_against_rust!(0, 8, 3);
1661 validate_against_rust!(0, 8, 4);
1662 validate_against_rust!(0, 16);
1663 validate_against_rust!(0, 16, 0);
1664 validate_against_rust!(0, 16, 1);
1665 validate_against_rust!(0, 16, 2);
1666 validate_against_rust!(0, 16, 3);
1667 validate_against_rust!(0, 16, 4);
1668 validate_against_rust!(1, 1);
1669 validate_against_rust!(1, 1, 0);
1670 validate_against_rust!(1, 1, 1);
1671 validate_against_rust!(1, 1, 2);
1672 validate_against_rust!(1, 1, 3);
1673 validate_against_rust!(1, 1, 4);
1674 validate_against_rust!(1, 2);
1675 validate_against_rust!(1, 2, 0);
1676 validate_against_rust!(1, 2, 1);
1677 validate_against_rust!(1, 2, 2);
1678 validate_against_rust!(1, 2, 3);
1679 validate_against_rust!(1, 2, 4);
1680 validate_against_rust!(1, 4);
1681 validate_against_rust!(1, 4, 0);
1682 validate_against_rust!(1, 4, 1);
1683 validate_against_rust!(1, 4, 2);
1684 validate_against_rust!(1, 4, 3);
1685 validate_against_rust!(1, 4, 4);
1686 validate_against_rust!(1, 8);
1687 validate_against_rust!(1, 8, 0);
1688 validate_against_rust!(1, 8, 1);
1689 validate_against_rust!(1, 8, 2);
1690 validate_against_rust!(1, 8, 3);
1691 validate_against_rust!(1, 8, 4);
1692 validate_against_rust!(1, 16);
1693 validate_against_rust!(1, 16, 0);
1694 validate_against_rust!(1, 16, 1);
1695 validate_against_rust!(1, 16, 2);
1696 validate_against_rust!(1, 16, 3);
1697 validate_against_rust!(1, 16, 4);
1698 validate_against_rust!(2, 1);
1699 validate_against_rust!(2, 1, 0);
1700 validate_against_rust!(2, 1, 1);
1701 validate_against_rust!(2, 1, 2);
1702 validate_against_rust!(2, 1, 3);
1703 validate_against_rust!(2, 1, 4);
1704 validate_against_rust!(2, 2);
1705 validate_against_rust!(2, 2, 0);
1706 validate_against_rust!(2, 2, 1);
1707 validate_against_rust!(2, 2, 2);
1708 validate_against_rust!(2, 2, 3);
1709 validate_against_rust!(2, 2, 4);
1710 validate_against_rust!(2, 4);
1711 validate_against_rust!(2, 4, 0);
1712 validate_against_rust!(2, 4, 1);
1713 validate_against_rust!(2, 4, 2);
1714 validate_against_rust!(2, 4, 3);
1715 validate_against_rust!(2, 4, 4);
1716 validate_against_rust!(2, 8);
1717 validate_against_rust!(2, 8, 0);
1718 validate_against_rust!(2, 8, 1);
1719 validate_against_rust!(2, 8, 2);
1720 validate_against_rust!(2, 8, 3);
1721 validate_against_rust!(2, 8, 4);
1722 validate_against_rust!(2, 16);
1723 validate_against_rust!(2, 16, 0);
1724 validate_against_rust!(2, 16, 1);
1725 validate_against_rust!(2, 16, 2);
1726 validate_against_rust!(2, 16, 3);
1727 validate_against_rust!(2, 16, 4);
1728 validate_against_rust!(3, 1);
1729 validate_against_rust!(3, 1, 0);
1730 validate_against_rust!(3, 1, 1);
1731 validate_against_rust!(3, 1, 2);
1732 validate_against_rust!(3, 1, 3);
1733 validate_against_rust!(3, 1, 4);
1734 validate_against_rust!(3, 2);
1735 validate_against_rust!(3, 2, 0);
1736 validate_against_rust!(3, 2, 1);
1737 validate_against_rust!(3, 2, 2);
1738 validate_against_rust!(3, 2, 3);
1739 validate_against_rust!(3, 2, 4);
1740 validate_against_rust!(3, 4);
1741 validate_against_rust!(3, 4, 0);
1742 validate_against_rust!(3, 4, 1);
1743 validate_against_rust!(3, 4, 2);
1744 validate_against_rust!(3, 4, 3);
1745 validate_against_rust!(3, 4, 4);
1746 validate_against_rust!(3, 8);
1747 validate_against_rust!(3, 8, 0);
1748 validate_against_rust!(3, 8, 1);
1749 validate_against_rust!(3, 8, 2);
1750 validate_against_rust!(3, 8, 3);
1751 validate_against_rust!(3, 8, 4);
1752 validate_against_rust!(3, 16);
1753 validate_against_rust!(3, 16, 0);
1754 validate_against_rust!(3, 16, 1);
1755 validate_against_rust!(3, 16, 2);
1756 validate_against_rust!(3, 16, 3);
1757 validate_against_rust!(3, 16, 4);
1758 validate_against_rust!(4, 1);
1759 validate_against_rust!(4, 1, 0);
1760 validate_against_rust!(4, 1, 1);
1761 validate_against_rust!(4, 1, 2);
1762 validate_against_rust!(4, 1, 3);
1763 validate_against_rust!(4, 1, 4);
1764 validate_against_rust!(4, 2);
1765 validate_against_rust!(4, 2, 0);
1766 validate_against_rust!(4, 2, 1);
1767 validate_against_rust!(4, 2, 2);
1768 validate_against_rust!(4, 2, 3);
1769 validate_against_rust!(4, 2, 4);
1770 validate_against_rust!(4, 4);
1771 validate_against_rust!(4, 4, 0);
1772 validate_against_rust!(4, 4, 1);
1773 validate_against_rust!(4, 4, 2);
1774 validate_against_rust!(4, 4, 3);
1775 validate_against_rust!(4, 4, 4);
1776 validate_against_rust!(4, 8);
1777 validate_against_rust!(4, 8, 0);
1778 validate_against_rust!(4, 8, 1);
1779 validate_against_rust!(4, 8, 2);
1780 validate_against_rust!(4, 8, 3);
1781 validate_against_rust!(4, 8, 4);
1782 validate_against_rust!(4, 16);
1783 validate_against_rust!(4, 16, 0);
1784 validate_against_rust!(4, 16, 1);
1785 validate_against_rust!(4, 16, 2);
1786 validate_against_rust!(4, 16, 3);
1787 validate_against_rust!(4, 16, 4);
1788 }
1789}
1790
1791#[cfg(kani)]
1792mod proofs {
1793 use core::alloc::Layout;
1794
1795 use super::*;
1796
1797 impl kani::Arbitrary for DstLayout {
1798 fn any() -> Self {
1799 let align: NonZeroUsize = kani::any();
1800 let size_info: SizeInfo = kani::any();
1801
1802 kani::assume(align.is_power_of_two());
1803 kani::assume(align < DstLayout::THEORETICAL_MAX_ALIGN);
1804
1805 // For testing purposes, we most care about instantiations of
1806 // `DstLayout` that can correspond to actual Rust types. We use
1807 // `Layout` to verify that our `DstLayout` satisfies the validity
1808 // conditions of Rust layouts.
1809 kani::assume(
1810 match size_info {
1811 SizeInfo::Sized { size } => Layout::from_size_align(size, align.get()),
1812 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size: _ }) => {
1813 // `SliceDst` cannot encode an exact size, but we know
1814 // it is at least `offset` bytes.
1815 Layout::from_size_align(offset, align.get())
1816 }
1817 }
1818 .is_ok(),
1819 );
1820
1821 Self { align: align, size_info: size_info, statically_shallow_unpadded: kani::any() }
1822 }
1823 }
1824
1825 impl kani::Arbitrary for SizeInfo {
1826 fn any() -> Self {
1827 let is_sized: bool = kani::any();
1828
1829 match is_sized {
1830 true => {
1831 let size: usize = kani::any();
1832
1833 kani::assume(size <= isize::MAX as _);
1834
1835 SizeInfo::Sized { size }
1836 }
1837 false => SizeInfo::SliceDst(kani::any()),
1838 }
1839 }
1840 }
1841
1842 impl kani::Arbitrary for TrailingSliceLayout {
1843 fn any() -> Self {
1844 let elem_size: usize = kani::any();
1845 let offset: usize = kani::any();
1846
1847 kani::assume(elem_size < isize::MAX as _);
1848 kani::assume(offset < isize::MAX as _);
1849
1850 TrailingSliceLayout { elem_size, offset }
1851 }
1852 }
1853
1854 #[kani::proof]
1855 fn prove_requires_dynamic_padding() {
1856 let layout: DstLayout = kani::any();
1857
1858 let SizeInfo::SliceDst(size_info) = layout.size_info else {
1859 kani::assume(false);
1860 loop {}
1861 };
1862
1863 let meta: usize = kani::any();
1864
1865 let Some(trailing_slice_size) = size_info.elem_size.checked_mul(meta) else {
1866 // The `trailing_slice_size` exceeds `usize::MAX`; `meta` is invalid.
1867 kani::assume(false);
1868 loop {}
1869 };
1870
1871 let Some(unpadded_size) = size_info.offset.checked_add(trailing_slice_size) else {
1872 // The `unpadded_size` exceeds `usize::MAX`; `meta`` is invalid.
1873 kani::assume(false);
1874 loop {}
1875 };
1876
1877 if unpadded_size >= isize::MAX as usize {
1878 // The `unpadded_size` exceeds `isize::MAX`; `meta` is invalid.
1879 kani::assume(false);
1880 loop {}
1881 }
1882
1883 let trailing_padding = util::padding_needed_for(unpadded_size, layout.align);
1884
1885 if !layout.requires_dynamic_padding() {
1886 assert!(trailing_padding == 0);
1887 }
1888 }
1889
1890 #[kani::proof]
1891 fn prove_dst_layout_extend() {
1892 use crate::util::{max, min, padding_needed_for};
1893
1894 let base: DstLayout = kani::any();
1895 let field: DstLayout = kani::any();
1896 let packed: Option<NonZeroUsize> = kani::any();
1897
1898 if let Some(max_align) = packed {
1899 kani::assume(max_align.is_power_of_two());
1900 kani::assume(base.align <= max_align);
1901 }
1902
1903 // The base can only be extended if it's sized.
1904 kani::assume(matches!(base.size_info, SizeInfo::Sized { .. }));
1905 let base_size = if let SizeInfo::Sized { size } = base.size_info {
1906 size
1907 } else {
1908 unreachable!();
1909 };
1910
1911 // Under the above conditions, `DstLayout::extend` will not panic.
1912 let composite = base.extend(field, packed);
1913
1914 // The field's alignment is clamped by `max_align` (i.e., the
1915 // `packed` attribute, if any) [1].
1916 //
1917 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1918 //
1919 // The alignments of each field, for the purpose of positioning
1920 // fields, is the smaller of the specified alignment and the
1921 // alignment of the field's type.
1922 let field_align = min(field.align, packed.unwrap_or(DstLayout::THEORETICAL_MAX_ALIGN));
1923
1924 // The struct's alignment is the maximum of its previous alignment and
1925 // `field_align`.
1926 assert_eq!(composite.align, max(base.align, field_align));
1927
1928 // Compute the minimum amount of inter-field padding needed to
1929 // satisfy the field's alignment, and offset of the trailing field.
1930 // [1]
1931 //
1932 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1933 //
1934 // Inter-field padding is guaranteed to be the minimum required in
1935 // order to satisfy each field's (possibly altered) alignment.
1936 let padding = padding_needed_for(base_size, field_align);
1937 let offset = base_size + padding;
1938
1939 // For testing purposes, we'll also construct `alloc::Layout`
1940 // stand-ins for `DstLayout`, and show that `extend` behaves
1941 // comparably on both types.
1942 let base_analog = Layout::from_size_align(base_size, base.align.get()).unwrap();
1943
1944 match field.size_info {
1945 SizeInfo::Sized { size: field_size } => {
1946 if let SizeInfo::Sized { size: composite_size } = composite.size_info {
1947 // If the trailing field is sized, the resulting layout will
1948 // be sized. Its size will be the sum of the preceding
1949 // layout, the size of the new field, and the size of
1950 // inter-field padding between the two.
1951 assert_eq!(composite_size, offset + field_size);
1952
1953 let field_analog =
1954 Layout::from_size_align(field_size, field_align.get()).unwrap();
1955
1956 if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog)
1957 {
1958 assert_eq!(actual_offset, offset);
1959 assert_eq!(actual_composite.size(), composite_size);
1960 assert_eq!(actual_composite.align(), composite.align.get());
1961 } else {
1962 // An error here reflects that composite of `base`
1963 // and `field` cannot correspond to a real Rust type
1964 // fragment, because such a fragment would violate
1965 // the basic invariants of a valid Rust layout. At
1966 // the time of writing, `DstLayout` is a little more
1967 // permissive than `Layout`, so we don't assert
1968 // anything in this branch (e.g., unreachability).
1969 }
1970 } else {
1971 panic!("The composite of two sized layouts must be sized.")
1972 }
1973 }
1974 SizeInfo::SliceDst(TrailingSliceLayout {
1975 offset: field_offset,
1976 elem_size: field_elem_size,
1977 }) => {
1978 if let SizeInfo::SliceDst(TrailingSliceLayout {
1979 offset: composite_offset,
1980 elem_size: composite_elem_size,
1981 }) = composite.size_info
1982 {
1983 // The offset of the trailing slice component is the sum
1984 // of the offset of the trailing field and the trailing
1985 // slice offset within that field.
1986 assert_eq!(composite_offset, offset + field_offset);
1987 // The elem size is unchanged.
1988 assert_eq!(composite_elem_size, field_elem_size);
1989
1990 let field_analog =
1991 Layout::from_size_align(field_offset, field_align.get()).unwrap();
1992
1993 if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog)
1994 {
1995 assert_eq!(actual_offset, offset);
1996 assert_eq!(actual_composite.size(), composite_offset);
1997 assert_eq!(actual_composite.align(), composite.align.get());
1998 } else {
1999 // An error here reflects that composite of `base`
2000 // and `field` cannot correspond to a real Rust type
2001 // fragment, because such a fragment would violate
2002 // the basic invariants of a valid Rust layout. At
2003 // the time of writing, `DstLayout` is a little more
2004 // permissive than `Layout`, so we don't assert
2005 // anything in this branch (e.g., unreachability).
2006 }
2007 } else {
2008 panic!("The extension of a layout with a DST must result in a DST.")
2009 }
2010 }
2011 }
2012 }
2013
2014 #[kani::proof]
2015 #[kani::should_panic]
2016 fn prove_dst_layout_extend_dst_panics() {
2017 let base: DstLayout = kani::any();
2018 let field: DstLayout = kani::any();
2019 let packed: Option<NonZeroUsize> = kani::any();
2020
2021 if let Some(max_align) = packed {
2022 kani::assume(max_align.is_power_of_two());
2023 kani::assume(base.align <= max_align);
2024 }
2025
2026 kani::assume(matches!(base.size_info, SizeInfo::SliceDst(..)));
2027
2028 let _ = base.extend(field, packed);
2029 }
2030
2031 #[kani::proof]
2032 fn prove_dst_layout_pad_to_align() {
2033 use crate::util::padding_needed_for;
2034
2035 let layout: DstLayout = kani::any();
2036
2037 let padded = layout.pad_to_align();
2038
2039 // Calling `pad_to_align` does not alter the `DstLayout`'s alignment.
2040 assert_eq!(padded.align, layout.align);
2041
2042 if let SizeInfo::Sized { size: unpadded_size } = layout.size_info {
2043 if let SizeInfo::Sized { size: padded_size } = padded.size_info {
2044 // If the layout is sized, it will remain sized after padding is
2045 // added. Its sum will be its unpadded size and the size of the
2046 // trailing padding needed to satisfy its alignment
2047 // requirements.
2048 let padding = padding_needed_for(unpadded_size, layout.align);
2049 assert_eq!(padded_size, unpadded_size + padding);
2050
2051 // Prove that calling `DstLayout::pad_to_align` behaves
2052 // identically to `Layout::pad_to_align`.
2053 let layout_analog =
2054 Layout::from_size_align(unpadded_size, layout.align.get()).unwrap();
2055 let padded_analog = layout_analog.pad_to_align();
2056 assert_eq!(padded_analog.align(), layout.align.get());
2057 assert_eq!(padded_analog.size(), padded_size);
2058 } else {
2059 panic!("The padding of a sized layout must result in a sized layout.")
2060 }
2061 } else {
2062 // If the layout is a DST, padding cannot be statically added.
2063 assert_eq!(padded.size_info, layout.size_info);
2064 }
2065 }
2066}