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::CastFrom;
742mod cast_from {
743 use crate::*;
744
745 pub(crate) struct CastFrom<Dst: ?Sized> {
746 _never: core::convert::Infallible,
747 _marker: PhantomData<Dst>,
748 }
749
750 // SAFETY: The implementation of `Project::project` preserves the address
751 // of the referent – it only modifies pointer metadata.
752 unsafe impl<Src, Dst> crate::pointer::cast::Cast<Src, Dst> for CastFrom<Dst>
753 where
754 Src: KnownLayout<PointerMetadata = usize> + ?Sized,
755 Dst: KnownLayout<PointerMetadata = usize> + ?Sized,
756 {
757 }
758
759 // SAFETY: `project` produces a pointer which refers to the same referent
760 // bytes as its input, or to a subset of them (see inline comments for a
761 // more detailed proof of this). It does this using provenance-preserving
762 // operations.
763 unsafe impl<Src, Dst> crate::pointer::cast::Project<Src, Dst> for CastFrom<Dst>
764 where
765 Src: KnownLayout<PointerMetadata = usize> + ?Sized,
766 Dst: KnownLayout<PointerMetadata = usize> + ?Sized,
767 {
768 /// # PME
769 ///
770 /// Generates a post-monomorphization error if it is not possible to
771 /// implement soundly.
772 //
773 // FIXME(#1817): Support Sized->Unsized and Unsized->Sized casts
774 fn project(src: PtrInner<'_, Src>) -> *mut Dst {
775 // At compile time (specifically, post-monomorphization time), we
776 // need to compute two things:
777 // - Whether, given *any* `*Src`, it is possible to construct a
778 // `*Dst` which addresses the same number of bytes (ie, whether,
779 // for any `Src` pointer metadata, there exists `Dst` pointer
780 // metadata that addresses the same number of bytes)
781 // - If this is possible, any information necessary to perform the
782 // `Src`->`Dst` metadata conversion at runtime.
783 //
784 // Assume that `Src` and `Dst` are slice DSTs, and define:
785 // - `S_OFF = Src::LAYOUT.size_info.offset`
786 // - `S_ELEM = Src::LAYOUT.size_info.elem_size`
787 // - `D_OFF = Dst::LAYOUT.size_info.offset`
788 // - `D_ELEM = Dst::LAYOUT.size_info.elem_size`
789 //
790 // We are trying to solve the following equation:
791 //
792 // D_OFF + d_meta * D_ELEM = S_OFF + s_meta * S_ELEM
793 //
794 // At runtime, we will be attempting to compute `d_meta`, given
795 // `s_meta` (a runtime value) and all other parameters (which are
796 // compile-time values). We can solve like so:
797 //
798 // D_OFF + d_meta * D_ELEM = S_OFF + s_meta * S_ELEM
799 //
800 // d_meta * D_ELEM = S_OFF - D_OFF + s_meta * S_ELEM
801 //
802 // d_meta = (S_OFF - D_OFF + s_meta * S_ELEM)/D_ELEM
803 //
804 // Since `d_meta` will be a `usize`, we need the right-hand side to
805 // be an integer, and this needs to hold for *any* value of `s_meta`
806 // (in order for our conversion to be infallible - ie, to not have
807 // to reject certain values of `s_meta` at runtime). This means
808 // that:
809 // - `s_meta * S_ELEM` must be a multiple of `D_ELEM`
810 // - Since this must hold for any value of `s_meta`, `S_ELEM` must
811 // be a multiple of `D_ELEM`
812 // - `S_OFF - D_OFF` must be a multiple of `D_ELEM`
813 //
814 // Thus, let `OFFSET_DELTA_ELEMS = (S_OFF - D_OFF)/D_ELEM` and
815 // `ELEM_MULTIPLE = S_ELEM/D_ELEM`. We can rewrite the above
816 // expression as:
817 //
818 // d_meta = (S_OFF - D_OFF + s_meta * S_ELEM)/D_ELEM
819 //
820 // d_meta = OFFSET_DELTA_ELEMS + s_meta * ELEM_MULTIPLE
821 //
822 // Thus, we just need to compute the following and confirm that they
823 // have integer solutions in order to both a) determine whether
824 // infallible `Src` -> `Dst` casts are possible and, b) pre-compute
825 // the parameters necessary to perform those casts at runtime. These
826 // parameters are encapsulated in `CastParams`, which acts as a
827 // witness that such infallible casts are possible.
828
829 /// The parameters required in order to perform a pointer cast from
830 /// `Src` to `Dst` as described above.
831 ///
832 /// These are a compile-time function of the layouts of `Src` and
833 /// `Dst`.
834 ///
835 /// # Safety
836 ///
837 /// `offset_delta_elems` and `elem_multiple` must be valid as
838 /// described above.
839 ///
840 /// `Src`'s alignment must not be smaller than `Dst`'s alignment.
841 #[derive(Copy, Clone)]
842 struct CastParams {
843 offset_delta_elems: usize,
844 elem_multiple: usize,
845 }
846
847 impl CastParams {
848 const fn try_compute(src: &DstLayout, dst: &DstLayout) -> Option<CastParams> {
849 if src.align.get() < dst.align.get() {
850 return None;
851 }
852
853 let (src, dst) = if let (SizeInfo::SliceDst(src), SizeInfo::SliceDst(dst)) =
854 (src.size_info, dst.size_info)
855 {
856 (src, dst)
857 } else {
858 return None;
859 };
860
861 let offset_delta = if let Some(od) = src.offset.checked_sub(dst.offset) {
862 od
863 } else {
864 return None;
865 };
866
867 let dst_elem_size = if let Some(e) = NonZeroUsize::new(dst.elem_size) {
868 e
869 } else {
870 return None;
871 };
872
873 // PANICS: `dst_elem_size: NonZeroUsize`, so this won't
874 // divide by zero.
875 #[allow(clippy::arithmetic_side_effects)]
876 let delta_mod_other_elem = offset_delta % dst_elem_size.get();
877
878 // PANICS: `dst_elem_size: NonZeroUsize`, so this won't
879 // divide by zero.
880 #[allow(clippy::arithmetic_side_effects)]
881 let elem_remainder = src.elem_size % dst_elem_size.get();
882
883 if delta_mod_other_elem != 0
884 || src.elem_size < dst.elem_size
885 || elem_remainder != 0
886 {
887 return None;
888 }
889
890 // PANICS: `dst_elem_size: NonZeroUsize`, so this won't
891 // divide by zero.
892 #[allow(clippy::arithmetic_side_effects)]
893 let offset_delta_elems = offset_delta / dst_elem_size.get();
894
895 // PANICS: `dst_elem_size: NonZeroUsize`, so this won't
896 // divide by zero.
897 #[allow(clippy::arithmetic_side_effects)]
898 let elem_multiple = src.elem_size / dst_elem_size.get();
899
900 // SAFETY: We checked above that `src.align >= dst.align`.
901 Some(CastParams {
902 // SAFETY: We checked above that this is an exact ratio.
903 offset_delta_elems,
904 // SAFETY: We checked above that this is an exact ratio.
905 elem_multiple,
906 })
907 }
908
909 /// # Safety
910 ///
911 /// `src_meta` describes a `Src` whose size is no larger than
912 /// `isize::MAX`.
913 ///
914 /// The returned metadata describes a `Dst` of the same size as
915 /// the original `Src`.
916 unsafe fn cast_metadata(self, src_meta: usize) -> usize {
917 #[allow(unused)]
918 use crate::util::polyfills::*;
919
920 // SAFETY: `self` is a witness that the following equation
921 // holds:
922 //
923 // D_OFF + d_meta * D_ELEM = S_OFF + s_meta * S_ELEM
924 //
925 // Since the caller promises that `src_meta` is valid `Src`
926 // metadata, this math will not overflow, and the returned
927 // value will describe a `Dst` of the same size.
928 #[allow(unstable_name_collisions, clippy::multiple_unsafe_ops_per_block)]
929 unsafe {
930 self.offset_delta_elems
931 .unchecked_add(src_meta.unchecked_mul(self.elem_multiple))
932 }
933 }
934 }
935
936 trait Params<Src: ?Sized> {
937 const CAST_PARAMS: CastParams;
938 }
939
940 impl<Src, Dst> Params<Src> for Dst
941 where
942 Src: KnownLayout + ?Sized,
943 Dst: KnownLayout<PointerMetadata = usize> + ?Sized,
944 {
945 const CAST_PARAMS: CastParams =
946 match CastParams::try_compute(&Src::LAYOUT, &Dst::LAYOUT) {
947 Some(params) => params,
948 None => const_panic!(
949 "cannot `transmute_ref!` or `transmute_mut!` between incompatible types"
950 ),
951 };
952 }
953
954 let src_meta = <Src as KnownLayout>::pointer_to_metadata(src.as_ptr());
955 let params = <Dst as Params<Src>>::CAST_PARAMS;
956
957 // SAFETY: `src: PtrInner` guarantees that `src`'s referent is zero
958 // bytes or lives in a single allocation, which means that it is no
959 // larger than `isize::MAX` bytes [1].
960 //
961 // [1] https://doc.rust-lang.org/1.92.0/std/ptr/index.html#allocation
962 let dst_meta = unsafe { params.cast_metadata(src_meta) };
963
964 <Dst as KnownLayout>::raw_from_ptr_len(src.as_non_null().cast(), dst_meta).as_ptr()
965 }
966 }
967}
968
969// FIXME(#67): For some reason, on our MSRV toolchain, this `allow` isn't
970// enforced despite having `#![allow(unknown_lints)]` at the crate root, but
971// putting it here works. Once our MSRV is high enough that this bug has been
972// fixed, remove this `allow`.
973#[allow(unknown_lints)]
974#[cfg(test)]
975mod tests {
976 use super::*;
977
978 #[test]
979 fn test_dst_layout_for_slice() {
980 let layout = DstLayout::for_slice::<u32>();
981 match layout.size_info {
982 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => {
983 assert_eq!(offset, 0);
984 assert_eq!(elem_size, 4);
985 }
986 _ => panic!("Expected SliceDst"),
987 }
988 assert_eq!(layout.align.get(), 4);
989 }
990
991 /// Tests of when a sized `DstLayout` is extended with a sized field.
992 #[allow(clippy::decimal_literal_representation)]
993 #[test]
994 fn test_dst_layout_extend_sized_with_sized() {
995 // This macro constructs a layout corresponding to a `u8` and extends it
996 // with a zero-sized trailing field of given alignment `n`. The macro
997 // tests that the resulting layout has both size and alignment `min(n,
998 // P)` for all valid values of `repr(packed(P))`.
999 macro_rules! test_align_is_size {
1000 ($n:expr) => {
1001 let base = DstLayout::for_type::<u8>();
1002 let trailing_field = DstLayout::for_type::<elain::Align<$n>>();
1003
1004 let packs =
1005 core::iter::once(None).chain((0..29).map(|p| NonZeroUsize::new(2usize.pow(p))));
1006
1007 for pack in packs {
1008 let composite = base.extend(trailing_field, pack);
1009 let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN);
1010 let align = $n.min(max_align.get());
1011 assert_eq!(
1012 composite,
1013 DstLayout {
1014 align: NonZeroUsize::new(align).unwrap(),
1015 size_info: SizeInfo::Sized { size: align },
1016 statically_shallow_unpadded: false,
1017 }
1018 )
1019 }
1020 };
1021 }
1022
1023 test_align_is_size!(1);
1024 test_align_is_size!(2);
1025 test_align_is_size!(4);
1026 test_align_is_size!(8);
1027 test_align_is_size!(16);
1028 test_align_is_size!(32);
1029 test_align_is_size!(64);
1030 test_align_is_size!(128);
1031 test_align_is_size!(256);
1032 test_align_is_size!(512);
1033 test_align_is_size!(1024);
1034 test_align_is_size!(2048);
1035 test_align_is_size!(4096);
1036 test_align_is_size!(8192);
1037 test_align_is_size!(16384);
1038 test_align_is_size!(32768);
1039 test_align_is_size!(65536);
1040 test_align_is_size!(131072);
1041 test_align_is_size!(262144);
1042 test_align_is_size!(524288);
1043 test_align_is_size!(1048576);
1044 test_align_is_size!(2097152);
1045 test_align_is_size!(4194304);
1046 test_align_is_size!(8388608);
1047 test_align_is_size!(16777216);
1048 test_align_is_size!(33554432);
1049 test_align_is_size!(67108864);
1050 test_align_is_size!(33554432);
1051 test_align_is_size!(134217728);
1052 test_align_is_size!(268435456);
1053 }
1054
1055 /// Tests of when a sized `DstLayout` is extended with a DST field.
1056 #[test]
1057 fn test_dst_layout_extend_sized_with_dst() {
1058 // Test that for all combinations of real-world alignments and
1059 // `repr_packed` values, that the extension of a sized `DstLayout`` with
1060 // a DST field correctly computes the trailing offset in the composite
1061 // layout.
1062
1063 let aligns = (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap());
1064 let packs = core::iter::once(None).chain(aligns.clone().map(Some));
1065
1066 for align in aligns {
1067 for pack in packs.clone() {
1068 let base = DstLayout::for_type::<u8>();
1069 let elem_size = 42;
1070 let trailing_field_offset = 11;
1071
1072 let trailing_field = DstLayout {
1073 align,
1074 size_info: SizeInfo::SliceDst(TrailingSliceLayout { elem_size, offset: 11 }),
1075 statically_shallow_unpadded: false,
1076 };
1077
1078 let composite = base.extend(trailing_field, pack);
1079
1080 let max_align = pack.unwrap_or(DstLayout::CURRENT_MAX_ALIGN).get();
1081
1082 let align = align.get().min(max_align);
1083
1084 assert_eq!(
1085 composite,
1086 DstLayout {
1087 align: NonZeroUsize::new(align).unwrap(),
1088 size_info: SizeInfo::SliceDst(TrailingSliceLayout {
1089 elem_size,
1090 offset: align + trailing_field_offset,
1091 }),
1092 statically_shallow_unpadded: false,
1093 }
1094 )
1095 }
1096 }
1097 }
1098
1099 /// Tests that calling `pad_to_align` on a sized `DstLayout` adds the
1100 /// expected amount of trailing padding.
1101 #[test]
1102 fn test_dst_layout_pad_to_align_with_sized() {
1103 // For all valid alignments `align`, construct a one-byte layout aligned
1104 // to `align`, call `pad_to_align`, and assert that the size of the
1105 // resulting layout is equal to `align`.
1106 for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) {
1107 let layout = DstLayout {
1108 align,
1109 size_info: SizeInfo::Sized { size: 1 },
1110 statically_shallow_unpadded: true,
1111 };
1112
1113 assert_eq!(
1114 layout.pad_to_align(),
1115 DstLayout {
1116 align,
1117 size_info: SizeInfo::Sized { size: align.get() },
1118 statically_shallow_unpadded: align.get() == 1
1119 }
1120 );
1121 }
1122
1123 // Test explicitly-provided combinations of unpadded and padded
1124 // counterparts.
1125
1126 macro_rules! test {
1127 (unpadded { size: $unpadded_size:expr, align: $unpadded_align:expr }
1128 => padded { size: $padded_size:expr, align: $padded_align:expr }) => {
1129 let unpadded = DstLayout {
1130 align: NonZeroUsize::new($unpadded_align).unwrap(),
1131 size_info: SizeInfo::Sized { size: $unpadded_size },
1132 statically_shallow_unpadded: false,
1133 };
1134 let padded = unpadded.pad_to_align();
1135
1136 assert_eq!(
1137 padded,
1138 DstLayout {
1139 align: NonZeroUsize::new($padded_align).unwrap(),
1140 size_info: SizeInfo::Sized { size: $padded_size },
1141 statically_shallow_unpadded: false,
1142 }
1143 );
1144 };
1145 }
1146
1147 test!(unpadded { size: 0, align: 4 } => padded { size: 0, align: 4 });
1148 test!(unpadded { size: 1, align: 4 } => padded { size: 4, align: 4 });
1149 test!(unpadded { size: 2, align: 4 } => padded { size: 4, align: 4 });
1150 test!(unpadded { size: 3, align: 4 } => padded { size: 4, align: 4 });
1151 test!(unpadded { size: 4, align: 4 } => padded { size: 4, align: 4 });
1152 test!(unpadded { size: 5, align: 4 } => padded { size: 8, align: 4 });
1153 test!(unpadded { size: 6, align: 4 } => padded { size: 8, align: 4 });
1154 test!(unpadded { size: 7, align: 4 } => padded { size: 8, align: 4 });
1155 test!(unpadded { size: 8, align: 4 } => padded { size: 8, align: 4 });
1156
1157 let current_max_align = DstLayout::CURRENT_MAX_ALIGN.get();
1158
1159 test!(unpadded { size: 1, align: current_max_align }
1160 => padded { size: current_max_align, align: current_max_align });
1161
1162 test!(unpadded { size: current_max_align + 1, align: current_max_align }
1163 => padded { size: current_max_align * 2, align: current_max_align });
1164 }
1165
1166 /// Tests that calling `pad_to_align` on a DST `DstLayout` is a no-op.
1167 #[test]
1168 fn test_dst_layout_pad_to_align_with_dst() {
1169 for align in (0..29).map(|p| NonZeroUsize::new(2usize.pow(p)).unwrap()) {
1170 for offset in 0..10 {
1171 for elem_size in 0..10 {
1172 let layout = DstLayout {
1173 align,
1174 size_info: SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }),
1175 statically_shallow_unpadded: false,
1176 };
1177 assert_eq!(layout.pad_to_align(), layout);
1178 }
1179 }
1180 }
1181 }
1182
1183 // This test takes a long time when running under Miri, so we skip it in
1184 // that case. This is acceptable because this is a logic test that doesn't
1185 // attempt to expose UB.
1186 #[test]
1187 #[cfg_attr(miri, ignore)]
1188 fn test_validate_cast_and_convert_metadata() {
1189 #[allow(non_local_definitions)]
1190 impl From<usize> for SizeInfo {
1191 fn from(size: usize) -> SizeInfo {
1192 SizeInfo::Sized { size }
1193 }
1194 }
1195
1196 #[allow(non_local_definitions)]
1197 impl From<(usize, usize)> for SizeInfo {
1198 fn from((offset, elem_size): (usize, usize)) -> SizeInfo {
1199 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
1200 }
1201 }
1202
1203 fn layout<S: Into<SizeInfo>>(s: S, align: usize) -> DstLayout {
1204 DstLayout {
1205 size_info: s.into(),
1206 align: NonZeroUsize::new(align).unwrap(),
1207 statically_shallow_unpadded: false,
1208 }
1209 }
1210
1211 /// This macro accepts arguments in the form of:
1212 ///
1213 /// layout(_, _).validate(_, _, _), Ok(Some((_, _)))
1214 /// | | | | | | |
1215 /// size ---------+ | | | | | |
1216 /// align -----------+ | | | | |
1217 /// addr ------------------------+ | | | |
1218 /// bytes_len ----------------------+ | | |
1219 /// cast_type -------------------------+ | |
1220 /// elems ------------------------------------------+ |
1221 /// split_at ------------------------------------------+
1222 ///
1223 /// `.validate` is shorthand for `.validate_cast_and_convert_metadata`
1224 /// for brevity.
1225 ///
1226 /// Each argument can either be an iterator or a wildcard. Each
1227 /// wildcarded variable is implicitly replaced by an iterator over a
1228 /// representative sample of values for that variable. Each `test!`
1229 /// invocation iterates over every combination of values provided by
1230 /// each variable's iterator (ie, the cartesian product) and validates
1231 /// that the results are expected.
1232 ///
1233 /// The final argument uses the same syntax, but it has a different
1234 /// meaning:
1235 /// - If it is `Ok(pat)`, then the pattern `pat` is supplied to
1236 /// a matching assert to validate the computed result for each
1237 /// combination of input values.
1238 /// - If it is `Err(Some(msg) | None)`, then `test!` validates that the
1239 /// call to `validate_cast_and_convert_metadata` panics with the given
1240 /// panic message or, if the current Rust toolchain version is too
1241 /// early to support panicking in `const fn`s, panics with *some*
1242 /// message. In the latter case, the `const_panic!` macro is used,
1243 /// which emits code which causes a non-panicking error at const eval
1244 /// time, but which does panic when invoked at runtime. Thus, it is
1245 /// merely difficult to predict the *value* of this panic. We deem
1246 /// that testing against the real panic strings on stable and nightly
1247 /// toolchains is enough to ensure correctness.
1248 ///
1249 /// Note that the meta-variables that match these variables have the
1250 /// `tt` type, and some valid expressions are not valid `tt`s (such as
1251 /// `a..b`). In this case, wrap the expression in parentheses, and it
1252 /// will become valid `tt`.
1253 macro_rules! test {
1254 (
1255 layout($size:tt, $align:tt)
1256 .validate($addr:tt, $bytes_len:tt, $cast_type:tt), $expect:pat $(,)?
1257 ) => {
1258 itertools::iproduct!(
1259 test!(@generate_size $size),
1260 test!(@generate_align $align),
1261 test!(@generate_usize $addr),
1262 test!(@generate_usize $bytes_len),
1263 test!(@generate_cast_type $cast_type)
1264 ).for_each(|(size_info, align, addr, bytes_len, cast_type)| {
1265 // Temporarily disable the panic hook installed by the test
1266 // harness. If we don't do this, all panic messages will be
1267 // kept in an internal log. On its own, this isn't a
1268 // problem, but if a non-caught panic ever happens (ie, in
1269 // code later in this test not in this macro), all of the
1270 // previously-buffered messages will be dumped, hiding the
1271 // real culprit.
1272 let previous_hook = std::panic::take_hook();
1273 // I don't understand why, but this seems to be required in
1274 // addition to the previous line.
1275 std::panic::set_hook(Box::new(|_| {}));
1276 let actual = std::panic::catch_unwind(|| {
1277 layout(size_info, align).validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
1278 }).map_err(|d| {
1279 let msg = d.downcast::<&'static str>().ok().map(|s| *s.as_ref());
1280 assert!(msg.is_some() || cfg!(no_zerocopy_panic_in_const_and_vec_try_reserve_1_57_0), "non-string panic messages are not permitted when usage of panic in const fn is enabled");
1281 msg
1282 });
1283 std::panic::set_hook(previous_hook);
1284
1285 assert!(
1286 matches!(actual, $expect),
1287 "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?})" ,size_info, align, addr, bytes_len, cast_type
1288 );
1289 });
1290 };
1291 (@generate_usize _) => { 0..8 };
1292 // Generate sizes for both Sized and !Sized types.
1293 (@generate_size _) => {
1294 test!(@generate_size (_)).chain(test!(@generate_size (_, _)))
1295 };
1296 // Generate sizes for both Sized and !Sized types by chaining
1297 // specified iterators for each.
1298 (@generate_size ($sized_sizes:tt | $unsized_sizes:tt)) => {
1299 test!(@generate_size ($sized_sizes)).chain(test!(@generate_size $unsized_sizes))
1300 };
1301 // Generate sizes for Sized types.
1302 (@generate_size (_)) => { test!(@generate_size (0..8)) };
1303 (@generate_size ($sizes:expr)) => { $sizes.into_iter().map(Into::<SizeInfo>::into) };
1304 // Generate sizes for !Sized types.
1305 (@generate_size ($min_sizes:tt, $elem_sizes:tt)) => {
1306 itertools::iproduct!(
1307 test!(@generate_min_size $min_sizes),
1308 test!(@generate_elem_size $elem_sizes)
1309 ).map(Into::<SizeInfo>::into)
1310 };
1311 (@generate_fixed_size _) => { (0..8).into_iter().map(Into::<SizeInfo>::into) };
1312 (@generate_min_size _) => { 0..8 };
1313 (@generate_elem_size _) => { 1..8 };
1314 (@generate_align _) => { [1, 2, 4, 8, 16] };
1315 (@generate_opt_usize _) => { [None].into_iter().chain((0..8).map(Some).into_iter()) };
1316 (@generate_cast_type _) => { [CastType::Prefix, CastType::Suffix] };
1317 (@generate_cast_type $variant:ident) => { [CastType::$variant] };
1318 // Some expressions need to be wrapped in parentheses in order to be
1319 // valid `tt`s (required by the top match pattern). See the comment
1320 // below for more details. This arm removes these parentheses to
1321 // avoid generating an `unused_parens` warning.
1322 (@$_:ident ($vals:expr)) => { $vals };
1323 (@$_:ident $vals:expr) => { $vals };
1324 }
1325
1326 const EVENS: [usize; 8] = [0, 2, 4, 6, 8, 10, 12, 14];
1327 const ODDS: [usize; 8] = [1, 3, 5, 7, 9, 11, 13, 15];
1328
1329 // base_size is too big for the memory region.
1330 test!(
1331 layout(((1..8) | ((1..8), (1..8))), _).validate([0], [0], _),
1332 Ok(Err(MetadataCastError::Size))
1333 );
1334 test!(
1335 layout(((2..8) | ((2..8), (2..8))), _).validate([0], [1], Prefix),
1336 Ok(Err(MetadataCastError::Size))
1337 );
1338 test!(
1339 layout(((2..8) | ((2..8), (2..8))), _).validate([0x1000_0000 - 1], [1], Suffix),
1340 Ok(Err(MetadataCastError::Size))
1341 );
1342
1343 // addr is unaligned for prefix cast
1344 test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
1345 test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
1346
1347 // addr is aligned, but end of buffer is unaligned for suffix cast
1348 test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
1349 test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
1350
1351 // Unfortunately, these constants cannot easily be used in the
1352 // implementation of `validate_cast_and_convert_metadata`, since
1353 // `panic!` consumes a string literal, not an expression.
1354 //
1355 // It's important that these messages be in a separate module. If they
1356 // were at the function's top level, we'd pass them to `test!` as, e.g.,
1357 // `Err(TRAILING)`, which would run into a subtle Rust footgun - the
1358 // `TRAILING` identifier would be treated as a pattern to match rather
1359 // than a value to check for equality.
1360 mod msgs {
1361 pub(super) const TRAILING: &str =
1362 "attempted to cast to slice type with zero-sized element";
1363 pub(super) const OVERFLOW: &str = "`addr` + `bytes_len` > usize::MAX";
1364 }
1365
1366 // casts with ZST trailing element types are unsupported
1367 test!(layout((_, [0]), _).validate(_, _, _), Err(Some(msgs::TRAILING) | None),);
1368
1369 // addr + bytes_len must not overflow usize
1370 test!(layout(_, _).validate([usize::MAX], (1..100), _), Err(Some(msgs::OVERFLOW) | None));
1371 test!(layout(_, _).validate((1..100), [usize::MAX], _), Err(Some(msgs::OVERFLOW) | None));
1372 test!(
1373 layout(_, _).validate(
1374 [usize::MAX / 2 + 1, usize::MAX],
1375 [usize::MAX / 2 + 1, usize::MAX],
1376 _
1377 ),
1378 Err(Some(msgs::OVERFLOW) | None)
1379 );
1380
1381 // Validates that `validate_cast_and_convert_metadata` satisfies its own
1382 // documented safety postconditions, and also a few other properties
1383 // that aren't documented but we want to guarantee anyway.
1384 fn validate_behavior(
1385 (layout, addr, bytes_len, cast_type): (DstLayout, usize, usize, CastType),
1386 ) {
1387 if let Ok((elems, split_at)) =
1388 layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
1389 {
1390 let (size_info, align) = (layout.size_info, layout.align);
1391 let debug_str = format!(
1392 "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?}) => ({}, {})",
1393 size_info, align, addr, bytes_len, cast_type, elems, split_at
1394 );
1395
1396 // If this is a sized type (no trailing slice), then `elems` is
1397 // meaningless, but in practice we set it to 0. Callers are not
1398 // allowed to rely on this, but a lot of math is nicer if
1399 // they're able to, and some callers might accidentally do that.
1400 let sized = matches!(layout.size_info, SizeInfo::Sized { .. });
1401 assert!(!(sized && elems != 0), "{}", debug_str);
1402
1403 let resulting_size = match layout.size_info {
1404 SizeInfo::Sized { size } => size,
1405 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size }) => {
1406 let padded_size = |elems| {
1407 let without_padding = offset + elems * elem_size;
1408 without_padding + util::padding_needed_for(without_padding, align)
1409 };
1410
1411 let resulting_size = padded_size(elems);
1412 // Test that `validate_cast_and_convert_metadata`
1413 // computed the largest possible value that fits in the
1414 // given range.
1415 assert!(padded_size(elems + 1) > bytes_len, "{}", debug_str);
1416 resulting_size
1417 }
1418 };
1419
1420 // Test safety postconditions guaranteed by
1421 // `validate_cast_and_convert_metadata`.
1422 assert!(resulting_size <= bytes_len, "{}", debug_str);
1423 match cast_type {
1424 CastType::Prefix => {
1425 assert_eq!(addr % align, 0, "{}", debug_str);
1426 assert_eq!(resulting_size, split_at, "{}", debug_str);
1427 }
1428 CastType::Suffix => {
1429 assert_eq!(split_at, bytes_len - resulting_size, "{}", debug_str);
1430 assert_eq!((addr + split_at) % align, 0, "{}", debug_str);
1431 }
1432 }
1433 } else {
1434 let min_size = match layout.size_info {
1435 SizeInfo::Sized { size } => size,
1436 SizeInfo::SliceDst(TrailingSliceLayout { offset, .. }) => {
1437 offset + util::padding_needed_for(offset, layout.align)
1438 }
1439 };
1440
1441 // If a cast is invalid, it is either because...
1442 // 1. there are insufficient bytes at the given region for type:
1443 let insufficient_bytes = bytes_len < min_size;
1444 // 2. performing the cast would misalign type:
1445 let base = match cast_type {
1446 CastType::Prefix => 0,
1447 CastType::Suffix => bytes_len,
1448 };
1449 let misaligned = (base + addr) % layout.align != 0;
1450
1451 assert!(insufficient_bytes || misaligned);
1452 }
1453 }
1454
1455 let sizes = 0..8;
1456 let elem_sizes = 1..8;
1457 let size_infos = sizes
1458 .clone()
1459 .map(Into::<SizeInfo>::into)
1460 .chain(itertools::iproduct!(sizes, elem_sizes).map(Into::<SizeInfo>::into));
1461 let layouts = itertools::iproduct!(size_infos, [1, 2, 4, 8, 16, 32])
1462 .filter(|(size_info, align)| !matches!(size_info, SizeInfo::Sized { size } if size % align != 0))
1463 .map(|(size_info, align)| layout(size_info, align));
1464 itertools::iproduct!(layouts, 0..8, 0..8, [CastType::Prefix, CastType::Suffix])
1465 .for_each(validate_behavior);
1466 }
1467
1468 #[test]
1469 #[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
1470 fn test_validate_rust_layout() {
1471 use core::{
1472 convert::TryInto as _,
1473 ptr::{self, NonNull},
1474 };
1475
1476 use crate::util::testutil::*;
1477
1478 // This test synthesizes pointers with various metadata and uses Rust's
1479 // built-in APIs to confirm that Rust makes decisions about type layout
1480 // which are consistent with what we believe is guaranteed by the
1481 // language. If this test fails, it doesn't just mean our code is wrong
1482 // - it means we're misunderstanding the language's guarantees.
1483
1484 #[derive(Debug)]
1485 struct MacroArgs {
1486 offset: usize,
1487 align: NonZeroUsize,
1488 elem_size: Option<usize>,
1489 }
1490
1491 /// # Safety
1492 ///
1493 /// `test` promises to only call `addr_of_slice_field` on a `NonNull<T>`
1494 /// which points to a valid `T`.
1495 ///
1496 /// `with_elems` must produce a pointer which points to a valid `T`.
1497 fn test<T: ?Sized, W: Fn(usize) -> NonNull<T>>(
1498 args: MacroArgs,
1499 with_elems: W,
1500 addr_of_slice_field: Option<fn(NonNull<T>) -> NonNull<u8>>,
1501 ) {
1502 let dst = args.elem_size.is_some();
1503 let layout = {
1504 let size_info = match args.elem_size {
1505 Some(elem_size) => {
1506 SizeInfo::SliceDst(TrailingSliceLayout { offset: args.offset, elem_size })
1507 }
1508 None => SizeInfo::Sized {
1509 // Rust only supports types whose sizes are a multiple
1510 // of their alignment. If the macro created a type like
1511 // this:
1512 //
1513 // #[repr(C, align(2))]
1514 // struct Foo([u8; 1]);
1515 //
1516 // ...then Rust will automatically round the type's size
1517 // up to 2.
1518 size: args.offset + util::padding_needed_for(args.offset, args.align),
1519 },
1520 };
1521 DstLayout { size_info, align: args.align, statically_shallow_unpadded: false }
1522 };
1523
1524 for elems in 0..128 {
1525 let ptr = with_elems(elems);
1526
1527 if let Some(addr_of_slice_field) = addr_of_slice_field {
1528 let slc_field_ptr = addr_of_slice_field(ptr).as_ptr();
1529 // SAFETY: Both `slc_field_ptr` and `ptr` are pointers to
1530 // the same valid Rust object.
1531 // Work around https://github.com/rust-lang/rust-clippy/issues/12280
1532 let offset: usize =
1533 unsafe { slc_field_ptr.byte_offset_from(ptr.as_ptr()).try_into().unwrap() };
1534 assert_eq!(offset, args.offset);
1535 }
1536
1537 // SAFETY: `ptr` points to a valid `T`.
1538 #[allow(clippy::multiple_unsafe_ops_per_block)]
1539 let (size, align) = unsafe {
1540 (mem::size_of_val_raw(ptr.as_ptr()), mem::align_of_val_raw(ptr.as_ptr()))
1541 };
1542
1543 // Avoid expensive allocation when running under Miri.
1544 let assert_msg = if !cfg!(miri) {
1545 format!("\n{:?}\nsize:{}, align:{}", args, size, align)
1546 } else {
1547 String::new()
1548 };
1549
1550 let without_padding =
1551 args.offset + args.elem_size.map(|elem_size| elems * elem_size).unwrap_or(0);
1552 assert!(size >= without_padding, "{}", assert_msg);
1553 assert_eq!(align, args.align.get(), "{}", assert_msg);
1554
1555 // This encodes the most important part of the test: our
1556 // understanding of how Rust determines the layout of repr(C)
1557 // types. Sized repr(C) types are trivial, but DST types have
1558 // some subtlety. Note that:
1559 // - For sized types, `without_padding` is just the size of the
1560 // type that we constructed for `Foo`. Since we may have
1561 // requested a larger alignment, `Foo` may actually be larger
1562 // than this, hence `padding_needed_for`.
1563 // - For unsized types, `without_padding` is dynamically
1564 // computed from the offset, the element size, and element
1565 // count. We expect that the size of the object should be
1566 // `offset + elem_size * elems` rounded up to the next
1567 // alignment.
1568 let expected_size =
1569 without_padding + util::padding_needed_for(without_padding, args.align);
1570 assert_eq!(expected_size, size, "{}", assert_msg);
1571
1572 // For zero-sized element types,
1573 // `validate_cast_and_convert_metadata` just panics, so we skip
1574 // testing those types.
1575 if args.elem_size.map(|elem_size| elem_size > 0).unwrap_or(true) {
1576 let addr = ptr.addr().get();
1577 let (got_elems, got_split_at) = layout
1578 .validate_cast_and_convert_metadata(addr, size, CastType::Prefix)
1579 .unwrap();
1580 // Avoid expensive allocation when running under Miri.
1581 let assert_msg = if !cfg!(miri) {
1582 format!(
1583 "{}\nvalidate_cast_and_convert_metadata({}, {})",
1584 assert_msg, addr, size,
1585 )
1586 } else {
1587 String::new()
1588 };
1589 assert_eq!(got_split_at, size, "{}", assert_msg);
1590 if dst {
1591 assert!(got_elems >= elems, "{}", assert_msg);
1592 if got_elems != elems {
1593 // If `validate_cast_and_convert_metadata`
1594 // returned more elements than `elems`, that
1595 // means that `elems` is not the maximum number
1596 // of elements that can fit in `size` - in other
1597 // words, there is enough padding at the end of
1598 // the value to fit at least one more element.
1599 // If we use this metadata to synthesize a
1600 // pointer, despite having a different element
1601 // count, we still expect it to have the same
1602 // size.
1603 let got_ptr = with_elems(got_elems);
1604 // SAFETY: `got_ptr` is a pointer to a valid `T`.
1605 let size_of_got_ptr = unsafe { mem::size_of_val_raw(got_ptr.as_ptr()) };
1606 assert_eq!(size_of_got_ptr, size, "{}", assert_msg);
1607 }
1608 } else {
1609 // For sized casts, the returned element value is
1610 // technically meaningless, and we don't guarantee any
1611 // particular value. In practice, it's always zero.
1612 assert_eq!(got_elems, 0, "{}", assert_msg)
1613 }
1614 }
1615 }
1616 }
1617
1618 macro_rules! validate_against_rust {
1619 ($offset:literal, $align:literal $(, $elem_size:literal)?) => {{
1620 #[repr(C, align($align))]
1621 struct Foo([u8; $offset]$(, [[u8; $elem_size]])?);
1622
1623 let args = MacroArgs {
1624 offset: $offset,
1625 align: $align.try_into().unwrap(),
1626 elem_size: {
1627 #[allow(unused)]
1628 let ret = None::<usize>;
1629 $(let ret = Some($elem_size);)?
1630 ret
1631 }
1632 };
1633
1634 #[repr(C, align($align))]
1635 struct FooAlign;
1636 // Create an aligned buffer to use in order to synthesize
1637 // pointers to `Foo`. We don't ever load values from these
1638 // pointers - we just do arithmetic on them - so having a "real"
1639 // block of memory as opposed to a validly-aligned-but-dangling
1640 // pointer is only necessary to make Miri happy since we run it
1641 // with "strict provenance" checking enabled.
1642 let aligned_buf = Align::<_, FooAlign>::new([0u8; 1024]);
1643 let with_elems = |elems| {
1644 let slc = NonNull::slice_from_raw_parts(NonNull::from(&aligned_buf.t), elems);
1645 #[allow(clippy::as_conversions)]
1646 NonNull::new(slc.as_ptr() as *mut Foo).unwrap()
1647 };
1648 let addr_of_slice_field = {
1649 #[allow(unused)]
1650 let f = None::<fn(NonNull<Foo>) -> NonNull<u8>>;
1651 $(
1652 // SAFETY: `test` promises to only call `f` with a `ptr`
1653 // to a valid `Foo`.
1654 let f: Option<fn(NonNull<Foo>) -> NonNull<u8>> = Some(|ptr: NonNull<Foo>| unsafe {
1655 NonNull::new(ptr::addr_of_mut!((*ptr.as_ptr()).1)).unwrap().cast::<u8>()
1656 });
1657 let _ = $elem_size;
1658 )?
1659 f
1660 };
1661
1662 test::<Foo, _>(args, with_elems, addr_of_slice_field);
1663 }};
1664 }
1665
1666 // Every permutation of:
1667 // - offset in [0, 4]
1668 // - align in [1, 16]
1669 // - elem_size in [0, 4] (plus no elem_size)
1670 validate_against_rust!(0, 1);
1671 validate_against_rust!(0, 1, 0);
1672 validate_against_rust!(0, 1, 1);
1673 validate_against_rust!(0, 1, 2);
1674 validate_against_rust!(0, 1, 3);
1675 validate_against_rust!(0, 1, 4);
1676 validate_against_rust!(0, 2);
1677 validate_against_rust!(0, 2, 0);
1678 validate_against_rust!(0, 2, 1);
1679 validate_against_rust!(0, 2, 2);
1680 validate_against_rust!(0, 2, 3);
1681 validate_against_rust!(0, 2, 4);
1682 validate_against_rust!(0, 4);
1683 validate_against_rust!(0, 4, 0);
1684 validate_against_rust!(0, 4, 1);
1685 validate_against_rust!(0, 4, 2);
1686 validate_against_rust!(0, 4, 3);
1687 validate_against_rust!(0, 4, 4);
1688 validate_against_rust!(0, 8);
1689 validate_against_rust!(0, 8, 0);
1690 validate_against_rust!(0, 8, 1);
1691 validate_against_rust!(0, 8, 2);
1692 validate_against_rust!(0, 8, 3);
1693 validate_against_rust!(0, 8, 4);
1694 validate_against_rust!(0, 16);
1695 validate_against_rust!(0, 16, 0);
1696 validate_against_rust!(0, 16, 1);
1697 validate_against_rust!(0, 16, 2);
1698 validate_against_rust!(0, 16, 3);
1699 validate_against_rust!(0, 16, 4);
1700 validate_against_rust!(1, 1);
1701 validate_against_rust!(1, 1, 0);
1702 validate_against_rust!(1, 1, 1);
1703 validate_against_rust!(1, 1, 2);
1704 validate_against_rust!(1, 1, 3);
1705 validate_against_rust!(1, 1, 4);
1706 validate_against_rust!(1, 2);
1707 validate_against_rust!(1, 2, 0);
1708 validate_against_rust!(1, 2, 1);
1709 validate_against_rust!(1, 2, 2);
1710 validate_against_rust!(1, 2, 3);
1711 validate_against_rust!(1, 2, 4);
1712 validate_against_rust!(1, 4);
1713 validate_against_rust!(1, 4, 0);
1714 validate_against_rust!(1, 4, 1);
1715 validate_against_rust!(1, 4, 2);
1716 validate_against_rust!(1, 4, 3);
1717 validate_against_rust!(1, 4, 4);
1718 validate_against_rust!(1, 8);
1719 validate_against_rust!(1, 8, 0);
1720 validate_against_rust!(1, 8, 1);
1721 validate_against_rust!(1, 8, 2);
1722 validate_against_rust!(1, 8, 3);
1723 validate_against_rust!(1, 8, 4);
1724 validate_against_rust!(1, 16);
1725 validate_against_rust!(1, 16, 0);
1726 validate_against_rust!(1, 16, 1);
1727 validate_against_rust!(1, 16, 2);
1728 validate_against_rust!(1, 16, 3);
1729 validate_against_rust!(1, 16, 4);
1730 validate_against_rust!(2, 1);
1731 validate_against_rust!(2, 1, 0);
1732 validate_against_rust!(2, 1, 1);
1733 validate_against_rust!(2, 1, 2);
1734 validate_against_rust!(2, 1, 3);
1735 validate_against_rust!(2, 1, 4);
1736 validate_against_rust!(2, 2);
1737 validate_against_rust!(2, 2, 0);
1738 validate_against_rust!(2, 2, 1);
1739 validate_against_rust!(2, 2, 2);
1740 validate_against_rust!(2, 2, 3);
1741 validate_against_rust!(2, 2, 4);
1742 validate_against_rust!(2, 4);
1743 validate_against_rust!(2, 4, 0);
1744 validate_against_rust!(2, 4, 1);
1745 validate_against_rust!(2, 4, 2);
1746 validate_against_rust!(2, 4, 3);
1747 validate_against_rust!(2, 4, 4);
1748 validate_against_rust!(2, 8);
1749 validate_against_rust!(2, 8, 0);
1750 validate_against_rust!(2, 8, 1);
1751 validate_against_rust!(2, 8, 2);
1752 validate_against_rust!(2, 8, 3);
1753 validate_against_rust!(2, 8, 4);
1754 validate_against_rust!(2, 16);
1755 validate_against_rust!(2, 16, 0);
1756 validate_against_rust!(2, 16, 1);
1757 validate_against_rust!(2, 16, 2);
1758 validate_against_rust!(2, 16, 3);
1759 validate_against_rust!(2, 16, 4);
1760 validate_against_rust!(3, 1);
1761 validate_against_rust!(3, 1, 0);
1762 validate_against_rust!(3, 1, 1);
1763 validate_against_rust!(3, 1, 2);
1764 validate_against_rust!(3, 1, 3);
1765 validate_against_rust!(3, 1, 4);
1766 validate_against_rust!(3, 2);
1767 validate_against_rust!(3, 2, 0);
1768 validate_against_rust!(3, 2, 1);
1769 validate_against_rust!(3, 2, 2);
1770 validate_against_rust!(3, 2, 3);
1771 validate_against_rust!(3, 2, 4);
1772 validate_against_rust!(3, 4);
1773 validate_against_rust!(3, 4, 0);
1774 validate_against_rust!(3, 4, 1);
1775 validate_against_rust!(3, 4, 2);
1776 validate_against_rust!(3, 4, 3);
1777 validate_against_rust!(3, 4, 4);
1778 validate_against_rust!(3, 8);
1779 validate_against_rust!(3, 8, 0);
1780 validate_against_rust!(3, 8, 1);
1781 validate_against_rust!(3, 8, 2);
1782 validate_against_rust!(3, 8, 3);
1783 validate_against_rust!(3, 8, 4);
1784 validate_against_rust!(3, 16);
1785 validate_against_rust!(3, 16, 0);
1786 validate_against_rust!(3, 16, 1);
1787 validate_against_rust!(3, 16, 2);
1788 validate_against_rust!(3, 16, 3);
1789 validate_against_rust!(3, 16, 4);
1790 validate_against_rust!(4, 1);
1791 validate_against_rust!(4, 1, 0);
1792 validate_against_rust!(4, 1, 1);
1793 validate_against_rust!(4, 1, 2);
1794 validate_against_rust!(4, 1, 3);
1795 validate_against_rust!(4, 1, 4);
1796 validate_against_rust!(4, 2);
1797 validate_against_rust!(4, 2, 0);
1798 validate_against_rust!(4, 2, 1);
1799 validate_against_rust!(4, 2, 2);
1800 validate_against_rust!(4, 2, 3);
1801 validate_against_rust!(4, 2, 4);
1802 validate_against_rust!(4, 4);
1803 validate_against_rust!(4, 4, 0);
1804 validate_against_rust!(4, 4, 1);
1805 validate_against_rust!(4, 4, 2);
1806 validate_against_rust!(4, 4, 3);
1807 validate_against_rust!(4, 4, 4);
1808 validate_against_rust!(4, 8);
1809 validate_against_rust!(4, 8, 0);
1810 validate_against_rust!(4, 8, 1);
1811 validate_against_rust!(4, 8, 2);
1812 validate_against_rust!(4, 8, 3);
1813 validate_against_rust!(4, 8, 4);
1814 validate_against_rust!(4, 16);
1815 validate_against_rust!(4, 16, 0);
1816 validate_against_rust!(4, 16, 1);
1817 validate_against_rust!(4, 16, 2);
1818 validate_against_rust!(4, 16, 3);
1819 validate_against_rust!(4, 16, 4);
1820 }
1821}
1822
1823#[cfg(kani)]
1824mod proofs {
1825 use core::alloc::Layout;
1826
1827 use super::*;
1828
1829 impl kani::Arbitrary for DstLayout {
1830 fn any() -> Self {
1831 let align: NonZeroUsize = kani::any();
1832 let size_info: SizeInfo = kani::any();
1833
1834 kani::assume(align.is_power_of_two());
1835 kani::assume(align < DstLayout::THEORETICAL_MAX_ALIGN);
1836
1837 // For testing purposes, we most care about instantiations of
1838 // `DstLayout` that can correspond to actual Rust types. We use
1839 // `Layout` to verify that our `DstLayout` satisfies the validity
1840 // conditions of Rust layouts.
1841 kani::assume(
1842 match size_info {
1843 SizeInfo::Sized { size } => Layout::from_size_align(size, align.get()),
1844 SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size: _ }) => {
1845 // `SliceDst` cannot encode an exact size, but we know
1846 // it is at least `offset` bytes.
1847 Layout::from_size_align(offset, align.get())
1848 }
1849 }
1850 .is_ok(),
1851 );
1852
1853 Self { align: align, size_info: size_info, statically_shallow_unpadded: kani::any() }
1854 }
1855 }
1856
1857 impl kani::Arbitrary for SizeInfo {
1858 fn any() -> Self {
1859 let is_sized: bool = kani::any();
1860
1861 match is_sized {
1862 true => {
1863 let size: usize = kani::any();
1864
1865 kani::assume(size <= isize::MAX as _);
1866
1867 SizeInfo::Sized { size }
1868 }
1869 false => SizeInfo::SliceDst(kani::any()),
1870 }
1871 }
1872 }
1873
1874 impl kani::Arbitrary for TrailingSliceLayout {
1875 fn any() -> Self {
1876 let elem_size: usize = kani::any();
1877 let offset: usize = kani::any();
1878
1879 kani::assume(elem_size < isize::MAX as _);
1880 kani::assume(offset < isize::MAX as _);
1881
1882 TrailingSliceLayout { elem_size, offset }
1883 }
1884 }
1885
1886 #[kani::proof]
1887 fn prove_requires_dynamic_padding() {
1888 let layout: DstLayout = kani::any();
1889
1890 let SizeInfo::SliceDst(size_info) = layout.size_info else {
1891 kani::assume(false);
1892 loop {}
1893 };
1894
1895 let meta: usize = kani::any();
1896
1897 let Some(trailing_slice_size) = size_info.elem_size.checked_mul(meta) else {
1898 // The `trailing_slice_size` exceeds `usize::MAX`; `meta` is invalid.
1899 kani::assume(false);
1900 loop {}
1901 };
1902
1903 let Some(unpadded_size) = size_info.offset.checked_add(trailing_slice_size) else {
1904 // The `unpadded_size` exceeds `usize::MAX`; `meta`` is invalid.
1905 kani::assume(false);
1906 loop {}
1907 };
1908
1909 if unpadded_size >= isize::MAX as usize {
1910 // The `unpadded_size` exceeds `isize::MAX`; `meta` is invalid.
1911 kani::assume(false);
1912 loop {}
1913 }
1914
1915 let trailing_padding = util::padding_needed_for(unpadded_size, layout.align);
1916
1917 if !layout.requires_dynamic_padding() {
1918 assert!(trailing_padding == 0);
1919 }
1920 }
1921
1922 #[kani::proof]
1923 fn prove_dst_layout_extend() {
1924 use crate::util::{max, min, padding_needed_for};
1925
1926 let base: DstLayout = kani::any();
1927 let field: DstLayout = kani::any();
1928 let packed: Option<NonZeroUsize> = kani::any();
1929
1930 if let Some(max_align) = packed {
1931 kani::assume(max_align.is_power_of_two());
1932 kani::assume(base.align <= max_align);
1933 }
1934
1935 // The base can only be extended if it's sized.
1936 kani::assume(matches!(base.size_info, SizeInfo::Sized { .. }));
1937 let base_size = if let SizeInfo::Sized { size } = base.size_info {
1938 size
1939 } else {
1940 unreachable!();
1941 };
1942
1943 // Under the above conditions, `DstLayout::extend` will not panic.
1944 let composite = base.extend(field, packed);
1945
1946 // The field's alignment is clamped by `max_align` (i.e., the
1947 // `packed` attribute, if any) [1].
1948 //
1949 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1950 //
1951 // The alignments of each field, for the purpose of positioning
1952 // fields, is the smaller of the specified alignment and the
1953 // alignment of the field's type.
1954 let field_align = min(field.align, packed.unwrap_or(DstLayout::THEORETICAL_MAX_ALIGN));
1955
1956 // The struct's alignment is the maximum of its previous alignment and
1957 // `field_align`.
1958 assert_eq!(composite.align, max(base.align, field_align));
1959
1960 // Compute the minimum amount of inter-field padding needed to
1961 // satisfy the field's alignment, and offset of the trailing field.
1962 // [1]
1963 //
1964 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1965 //
1966 // Inter-field padding is guaranteed to be the minimum required in
1967 // order to satisfy each field's (possibly altered) alignment.
1968 let padding = padding_needed_for(base_size, field_align);
1969 let offset = base_size + padding;
1970
1971 // For testing purposes, we'll also construct `alloc::Layout`
1972 // stand-ins for `DstLayout`, and show that `extend` behaves
1973 // comparably on both types.
1974 let base_analog = Layout::from_size_align(base_size, base.align.get()).unwrap();
1975
1976 match field.size_info {
1977 SizeInfo::Sized { size: field_size } => {
1978 if let SizeInfo::Sized { size: composite_size } = composite.size_info {
1979 // If the trailing field is sized, the resulting layout will
1980 // be sized. Its size will be the sum of the preceding
1981 // layout, the size of the new field, and the size of
1982 // inter-field padding between the two.
1983 assert_eq!(composite_size, offset + field_size);
1984
1985 let field_analog =
1986 Layout::from_size_align(field_size, field_align.get()).unwrap();
1987
1988 if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog)
1989 {
1990 assert_eq!(actual_offset, offset);
1991 assert_eq!(actual_composite.size(), composite_size);
1992 assert_eq!(actual_composite.align(), composite.align.get());
1993 } else {
1994 // An error here reflects that composite of `base`
1995 // and `field` cannot correspond to a real Rust type
1996 // fragment, because such a fragment would violate
1997 // the basic invariants of a valid Rust layout. At
1998 // the time of writing, `DstLayout` is a little more
1999 // permissive than `Layout`, so we don't assert
2000 // anything in this branch (e.g., unreachability).
2001 }
2002 } else {
2003 panic!("The composite of two sized layouts must be sized.")
2004 }
2005 }
2006 SizeInfo::SliceDst(TrailingSliceLayout {
2007 offset: field_offset,
2008 elem_size: field_elem_size,
2009 }) => {
2010 if let SizeInfo::SliceDst(TrailingSliceLayout {
2011 offset: composite_offset,
2012 elem_size: composite_elem_size,
2013 }) = composite.size_info
2014 {
2015 // The offset of the trailing slice component is the sum
2016 // of the offset of the trailing field and the trailing
2017 // slice offset within that field.
2018 assert_eq!(composite_offset, offset + field_offset);
2019 // The elem size is unchanged.
2020 assert_eq!(composite_elem_size, field_elem_size);
2021
2022 let field_analog =
2023 Layout::from_size_align(field_offset, field_align.get()).unwrap();
2024
2025 if let Ok((actual_composite, actual_offset)) = base_analog.extend(field_analog)
2026 {
2027 assert_eq!(actual_offset, offset);
2028 assert_eq!(actual_composite.size(), composite_offset);
2029 assert_eq!(actual_composite.align(), composite.align.get());
2030 } else {
2031 // An error here reflects that composite of `base`
2032 // and `field` cannot correspond to a real Rust type
2033 // fragment, because such a fragment would violate
2034 // the basic invariants of a valid Rust layout. At
2035 // the time of writing, `DstLayout` is a little more
2036 // permissive than `Layout`, so we don't assert
2037 // anything in this branch (e.g., unreachability).
2038 }
2039 } else {
2040 panic!("The extension of a layout with a DST must result in a DST.")
2041 }
2042 }
2043 }
2044 }
2045
2046 #[kani::proof]
2047 #[kani::should_panic]
2048 fn prove_dst_layout_extend_dst_panics() {
2049 let base: DstLayout = kani::any();
2050 let field: DstLayout = kani::any();
2051 let packed: Option<NonZeroUsize> = kani::any();
2052
2053 if let Some(max_align) = packed {
2054 kani::assume(max_align.is_power_of_two());
2055 kani::assume(base.align <= max_align);
2056 }
2057
2058 kani::assume(matches!(base.size_info, SizeInfo::SliceDst(..)));
2059
2060 let _ = base.extend(field, packed);
2061 }
2062
2063 #[kani::proof]
2064 fn prove_dst_layout_pad_to_align() {
2065 use crate::util::padding_needed_for;
2066
2067 let layout: DstLayout = kani::any();
2068
2069 let padded = layout.pad_to_align();
2070
2071 // Calling `pad_to_align` does not alter the `DstLayout`'s alignment.
2072 assert_eq!(padded.align, layout.align);
2073
2074 if let SizeInfo::Sized { size: unpadded_size } = layout.size_info {
2075 if let SizeInfo::Sized { size: padded_size } = padded.size_info {
2076 // If the layout is sized, it will remain sized after padding is
2077 // added. Its sum will be its unpadded size and the size of the
2078 // trailing padding needed to satisfy its alignment
2079 // requirements.
2080 let padding = padding_needed_for(unpadded_size, layout.align);
2081 assert_eq!(padded_size, unpadded_size + padding);
2082
2083 // Prove that calling `DstLayout::pad_to_align` behaves
2084 // identically to `Layout::pad_to_align`.
2085 let layout_analog =
2086 Layout::from_size_align(unpadded_size, layout.align.get()).unwrap();
2087 let padded_analog = layout_analog.pad_to_align();
2088 assert_eq!(padded_analog.align(), layout.align.get());
2089 assert_eq!(padded_analog.size(), padded_size);
2090 } else {
2091 panic!("The padding of a sized layout must result in a sized layout.")
2092 }
2093 } else {
2094 // If the layout is a DST, padding cannot be statically added.
2095 assert_eq!(padded.size_info, layout.size_info);
2096 }
2097 }
2098}