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