zerocopy/util/
macros.rs

1// Copyright 2023 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9/// Unsafely implements trait(s) for a type.
10///
11/// # Safety
12///
13/// The trait impl must be sound.
14///
15/// When implementing `TryFromBytes`:
16/// - If no `is_bit_valid` impl is provided, then it must be valid for
17///   `is_bit_valid` to unconditionally return `true`. In other words, it must
18///   be the case that any initialized sequence of bytes constitutes a valid
19///   instance of `$ty`.
20/// - If an `is_bit_valid` impl is provided, then the impl of `is_bit_valid`
21///   must only return `true` if its argument refers to a valid `$ty`.
22macro_rules! unsafe_impl {
23    // Implement `$trait` for `$ty` with no bounds.
24    ($(#[$attr:meta])* $ty:ty: $trait:ident $(; |$candidate:ident| $is_bit_valid:expr)?) => {{
25        crate::util::macros::__unsafe();
26
27        $(#[$attr])*
28        // SAFETY: The caller promises that this is sound.
29        unsafe impl $trait for $ty {
30            unsafe_impl!(@method $trait $(; |$candidate| $is_bit_valid)?);
31        }
32    }};
33
34    // Implement all `$traits` for `$ty` with no bounds.
35    //
36    // The 2 arms under this one are there so we can apply
37    // N attributes for each one of M trait implementations.
38    // The simple solution of:
39    //
40    // ($(#[$attrs:meta])* $ty:ty: $($traits:ident),*) => {
41    //     $( unsafe_impl!( $(#[$attrs])* $ty: $traits ) );*
42    // }
43    //
44    // Won't work. The macro processor sees that the outer repetition
45    // contains both $attrs and $traits and expects them to match the same
46    // amount of fragments.
47    //
48    // To solve this we must:
49    // 1. Pack the attributes into a single token tree fragment we can match over.
50    // 2. Expand the traits.
51    // 3. Unpack and expand the attributes.
52    ($(#[$attrs:meta])* $ty:ty: $($traits:ident),*) => {
53        unsafe_impl!(@impl_traits_with_packed_attrs { $(#[$attrs])* } $ty: $($traits),*)
54    };
55
56    (@impl_traits_with_packed_attrs $attrs:tt $ty:ty: $($traits:ident),*) => {{
57        $( unsafe_impl!(@unpack_attrs $attrs $ty: $traits); )*
58    }};
59
60    (@unpack_attrs { $(#[$attrs:meta])* } $ty:ty: $traits:ident) => {
61        unsafe_impl!($(#[$attrs])* $ty: $traits);
62    };
63
64    // This arm is identical to the following one, except it contains a
65    // preceding `const`. If we attempt to handle these with a single arm, there
66    // is an inherent ambiguity between `const` (the keyword) and `const` (the
67    // ident match for `$tyvar:ident`).
68    //
69    // To explain how this works, consider the following invocation:
70    //
71    //   unsafe_impl!(const N: usize, T: ?Sized + Copy => Clone for Foo<T>);
72    //
73    // In this invocation, here are the assignments to meta-variables:
74    //
75    //   |---------------|------------|
76    //   | Meta-variable | Assignment |
77    //   |---------------|------------|
78    //   | $constname    |  N         |
79    //   | $constty      |  usize     |
80    //   | $tyvar        |  T         |
81    //   | $optbound     |  Sized     |
82    //   | $bound        |  Copy      |
83    //   | $trait        |  Clone     |
84    //   | $ty           |  Foo<T>    |
85    //   |---------------|------------|
86    //
87    // The following arm has the same behavior with the exception of the lack of
88    // support for a leading `const` parameter.
89    (
90        $(#[$attr:meta])*
91        const $constname:ident : $constty:ident $(,)?
92        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
93        => $trait:ident for $ty:ty $(; |$candidate:ident| $is_bit_valid:expr)?
94    ) => {
95        unsafe_impl!(
96            @inner
97            $(#[$attr])*
98            @const $constname: $constty,
99            $($tyvar $(: $(? $optbound +)* + $($bound +)*)?,)*
100            => $trait for $ty $(; |$candidate| $is_bit_valid)?
101        );
102    };
103    (
104        $(#[$attr:meta])*
105        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
106        => $trait:ident for $ty:ty $(; |$candidate:ident| $is_bit_valid:expr)?
107    ) => {{
108        unsafe_impl!(
109            @inner
110            $(#[$attr])*
111            $($tyvar $(: $(? $optbound +)* + $($bound +)*)?,)*
112            => $trait for $ty $(; |$candidate| $is_bit_valid)?
113        );
114    }};
115    (
116        @inner
117        $(#[$attr:meta])*
118        $(@const $constname:ident : $constty:ident,)*
119        $($tyvar:ident $(: $(? $optbound:ident +)* + $($bound:ident +)* )?,)*
120        => $trait:ident for $ty:ty $(; |$candidate:ident| $is_bit_valid:expr)?
121    ) => {{
122        crate::util::macros::__unsafe();
123
124        $(#[$attr])*
125        #[allow(non_local_definitions)]
126        // SAFETY: The caller promises that this is sound.
127        unsafe impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),* $(, const $constname: $constty,)*> $trait for $ty {
128            unsafe_impl!(@method $trait $(; |$candidate| $is_bit_valid)?);
129        }
130    }};
131
132    (@method TryFromBytes ; |$candidate:ident| $is_bit_valid:expr) => {
133        #[allow(clippy::missing_inline_in_public_items, dead_code)]
134        #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
135        fn only_derive_is_allowed_to_implement_this_trait() {}
136
137        #[inline]
138        fn is_bit_valid<AA: crate::pointer::invariant::Reference>($candidate: Maybe<'_, Self, AA>) -> bool {
139            $is_bit_valid
140        }
141    };
142    (@method TryFromBytes) => {
143        #[allow(clippy::missing_inline_in_public_items)]
144        #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
145        fn only_derive_is_allowed_to_implement_this_trait() {}
146        #[inline(always)] fn is_bit_valid<AA: crate::pointer::invariant::Reference>(_: Maybe<'_, Self, AA>) -> bool { true }
147    };
148    (@method $trait:ident) => {
149        #[allow(clippy::missing_inline_in_public_items, dead_code)]
150        #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
151        fn only_derive_is_allowed_to_implement_this_trait() {}
152    };
153    (@method $trait:ident; |$_candidate:ident| $_is_bit_valid:expr) => {
154        compile_error!("Can't provide `is_bit_valid` impl for trait other than `TryFromBytes`");
155    };
156}
157
158/// Implements `$trait` for `$ty` where `$ty: TransmuteFrom<$repr>` (and
159/// vice-versa).
160///
161/// Calling this macro is safe; the internals of the macro emit appropriate
162/// trait bounds which ensure that the given impl is sound.
163macro_rules! impl_for_transmute_from {
164    (
165        $(#[$attr:meta])*
166        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?)?
167        => $trait:ident for $ty:ty [$($unsafe_cell:ident)? <$repr:ty>]
168    ) => {
169        const _: () = {
170            $(#[$attr])*
171            #[allow(non_local_definitions)]
172
173            // SAFETY: `is_trait<T, R>` (defined and used below) requires `T:
174            // TransmuteFrom<R>`, `R: TransmuteFrom<T>`, and `R: $trait`. It is
175            // called using `$ty` and `$repr`, ensuring that `$ty` and `$repr`
176            // have equivalent bit validity, and ensuring that `$repr: $trait`.
177            // The supported traits - `TryFromBytes`, `FromZeros`, `FromBytes`,
178            // and `IntoBytes` - are defined only in terms of the bit validity
179            // of a type. Therefore, `$repr: $trait` ensures that `$ty: $trait`
180            // is sound.
181            unsafe impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?)?> $trait for $ty {
182                #[allow(dead_code, clippy::missing_inline_in_public_items)]
183                #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
184                fn only_derive_is_allowed_to_implement_this_trait() {
185                    use crate::pointer::{*, invariant::Valid};
186
187                    impl_for_transmute_from!(@assert_is_supported_trait $trait);
188
189                    fn is_trait<T, R>()
190                    where
191                        T: TransmuteFrom<R, Valid, Valid> + ?Sized,
192                        R: TransmuteFrom<T, Valid, Valid> + ?Sized,
193                        R: $trait,
194                    {
195                    }
196
197                    #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
198                    fn f<$($tyvar $(: $(? $optbound +)* $($bound +)*)?)?>() {
199                        is_trait::<$ty, $repr>();
200                    }
201                }
202
203                impl_for_transmute_from!(
204                    @is_bit_valid
205                    $(<$tyvar $(: $(? $optbound +)* $($bound +)*)?>)?
206                    $trait for $ty [$($unsafe_cell)? <$repr>]
207                );
208            }
209        };
210    };
211    (@assert_is_supported_trait TryFromBytes) => {};
212    (@assert_is_supported_trait FromZeros) => {};
213    (@assert_is_supported_trait FromBytes) => {};
214    (@assert_is_supported_trait IntoBytes) => {};
215    (
216        @is_bit_valid
217        $(<$tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?>)?
218        TryFromBytes for $ty:ty [UnsafeCell<$repr:ty>]
219    ) => {
220        #[inline]
221        fn is_bit_valid<A: crate::pointer::invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool {
222            let c: Maybe<'_, Self, crate::pointer::invariant::Exclusive> = candidate.into_exclusive_or_pme();
223            let c: Maybe<'_, $repr, _> = c.transmute::<_, _, (_, (_, (BecauseExclusive, BecauseExclusive)))>();
224            // SAFETY: This macro ensures that `$repr` and `Self` have the same
225            // size and bit validity. Thus, a bit-valid instance of `$repr` is
226            // also a bit-valid instance of `Self`.
227            <$repr as TryFromBytes>::is_bit_valid(c)
228        }
229    };
230    (
231        @is_bit_valid
232        $(<$tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?>)?
233        TryFromBytes for $ty:ty [<$repr:ty>]
234    ) => {
235        #[inline]
236        fn is_bit_valid<A: crate::pointer::invariant::Reference>(candidate: $crate::Maybe<'_, Self, A>) -> bool {
237            // SAFETY: This macro ensures that `$repr` and `Self` have the same
238            // size and bit validity. Thus, a bit-valid instance of `$repr` is
239            // also a bit-valid instance of `Self`.
240            <$repr as TryFromBytes>::is_bit_valid(candidate.transmute())
241        }
242    };
243    (
244        @is_bit_valid
245        $(<$tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?>)?
246        $trait:ident for $ty:ty [$($unsafe_cell:ident)? <$repr:ty>]
247    ) => {
248        // Trait other than `TryFromBytes`; no `is_bit_valid` impl.
249    };
250}
251
252/// Implements a trait for a type, bounding on each member of the power set of
253/// a set of type variables. This is useful for implementing traits for tuples
254/// or `fn` types.
255///
256/// The last argument is the name of a macro which will be called in every
257/// `impl` block, and is expected to expand to the name of the type for which to
258/// implement the trait.
259///
260/// For example, the invocation:
261/// ```ignore
262/// unsafe_impl_for_power_set!(A, B => Foo for type!(...))
263/// ```
264/// ...expands to:
265/// ```ignore
266/// unsafe impl       Foo for type!()     { ... }
267/// unsafe impl<B>    Foo for type!(B)    { ... }
268/// unsafe impl<A, B> Foo for type!(A, B) { ... }
269/// ```
270macro_rules! unsafe_impl_for_power_set {
271    (
272        $first:ident $(, $rest:ident)* $(-> $ret:ident)? => $trait:ident for $macro:ident!(...)
273        $(; |$candidate:ident| $is_bit_valid:expr)?
274    ) => {
275        unsafe_impl_for_power_set!(
276            $($rest),* $(-> $ret)? => $trait for $macro!(...)
277            $(; |$candidate| $is_bit_valid)?
278        );
279        unsafe_impl_for_power_set!(
280            @impl $first $(, $rest)* $(-> $ret)? => $trait for $macro!(...)
281            $(; |$candidate| $is_bit_valid)?
282        );
283    };
284    (
285        $(-> $ret:ident)? => $trait:ident for $macro:ident!(...)
286        $(; |$candidate:ident| $is_bit_valid:expr)?
287    ) => {
288        unsafe_impl_for_power_set!(
289            @impl $(-> $ret)? => $trait for $macro!(...)
290            $(; |$candidate| $is_bit_valid)?
291        );
292    };
293    (
294        @impl $($vars:ident),* $(-> $ret:ident)? => $trait:ident for $macro:ident!(...)
295        $(; |$candidate:ident| $is_bit_valid:expr)?
296    ) => {
297        unsafe_impl!(
298            $($vars,)* $($ret)? => $trait for $macro!($($vars),* $(-> $ret)?)
299            $(; |$candidate| $is_bit_valid)?
300        );
301    };
302}
303
304/// Expands to an `Option<extern "C" fn>` type with the given argument types and
305/// return type. Designed for use with `unsafe_impl_for_power_set`.
306macro_rules! opt_extern_c_fn {
307    ($($args:ident),* -> $ret:ident) => { Option<extern "C" fn($($args),*) -> $ret> };
308}
309
310/// Expands to an `Option<unsafe extern "C" fn>` type with the given argument
311/// types and return type. Designed for use with `unsafe_impl_for_power_set`.
312macro_rules! opt_unsafe_extern_c_fn {
313    ($($args:ident),* -> $ret:ident) => { Option<unsafe extern "C" fn($($args),*) -> $ret> };
314}
315
316/// Expands to an `Option<fn>` type with the given argument types and return
317/// type. Designed for use with `unsafe_impl_for_power_set`.
318macro_rules! opt_fn {
319    ($($args:ident),* -> $ret:ident) => { Option<fn($($args),*) -> $ret> };
320}
321
322/// Expands to an `Option<unsafe fn>` type with the given argument types and
323/// return type. Designed for use with `unsafe_impl_for_power_set`.
324macro_rules! opt_unsafe_fn {
325    ($($args:ident),* -> $ret:ident) => { Option<unsafe fn($($args),*) -> $ret> };
326}
327
328/// Implements trait(s) for a type or verifies the given implementation by
329/// referencing an existing (derived) implementation.
330///
331/// This macro exists so that we can provide zerocopy-derive as an optional
332/// dependency and still get the benefit of using its derives to validate that
333/// our trait impls are sound.
334///
335/// When compiling without `--cfg 'feature = "derive"` and without `--cfg test`,
336/// `impl_or_verify!` emits the provided trait impl. When compiling with either
337/// of those cfgs, it is expected that the type in question is deriving the
338/// traits instead. In this case, `impl_or_verify!` emits code which validates
339/// that the given trait impl is at least as restrictive as the the impl emitted
340/// by the custom derive. This has the effect of confirming that the impl which
341/// is emitted when the `derive` feature is disabled is actually sound (on the
342/// assumption that the impl emitted by the custom derive is sound).
343///
344/// The caller is still required to provide a safety comment (e.g. using the
345/// `const _: () = unsafe` macro) . The reason for this restriction is that, while
346/// `impl_or_verify!` can guarantee that the provided impl is sound when it is
347/// compiled with the appropriate cfgs, there is no way to guarantee that it is
348/// ever compiled with those cfgs. In particular, it would be possible to
349/// accidentally place an `impl_or_verify!` call in a context that is only ever
350/// compiled when the `derive` feature is disabled. If that were to happen,
351/// there would be nothing to prevent an unsound trait impl from being emitted.
352/// Requiring a safety comment reduces the likelihood of emitting an unsound
353/// impl in this case, and also provides useful documentation for readers of the
354/// code.
355///
356/// Finally, if a `TryFromBytes::is_bit_valid` impl is provided, it must adhere
357/// to the safety preconditions of [`unsafe_impl!`].
358///
359/// ## Example
360///
361/// ```rust,ignore
362/// // Note that these derives are gated by `feature = "derive"`
363/// #[cfg_attr(any(feature = "derive", test), derive(FromZeros, FromBytes, IntoBytes, Unaligned))]
364/// #[repr(transparent)]
365/// struct Wrapper<T>(T);
366///
367/// const _: () = unsafe {
368///     /// SAFETY:
369///     /// `Wrapper<T>` is `repr(transparent)`, so it is sound to implement any
370///     /// zerocopy trait if `T` implements that trait.
371///     impl_or_verify!(T: FromZeros => FromZeros for Wrapper<T>);
372///     impl_or_verify!(T: FromBytes => FromBytes for Wrapper<T>);
373///     impl_or_verify!(T: IntoBytes => IntoBytes for Wrapper<T>);
374///     impl_or_verify!(T: Unaligned => Unaligned for Wrapper<T>);
375/// }
376/// ```
377macro_rules! impl_or_verify {
378    // The following two match arms follow the same pattern as their
379    // counterparts in `unsafe_impl!`; see the documentation on those arms for
380    // more details.
381    (
382        const $constname:ident : $constty:ident $(,)?
383        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
384        => $trait:ident for $ty:ty
385    ) => {
386        impl_or_verify!(@impl { unsafe_impl!(
387            const $constname: $constty, $($tyvar $(: $(? $optbound +)* $($bound +)*)?),* => $trait for $ty
388        ); });
389        impl_or_verify!(@verify $trait, {
390            impl<const $constname: $constty, $($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
391        });
392    };
393    (
394        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
395        => $trait:ident for $ty:ty $(; |$candidate:ident| $is_bit_valid:expr)?
396    ) => {
397        impl_or_verify!(@impl { unsafe_impl!(
398            $($tyvar $(: $(? $optbound +)* $($bound +)*)?),* => $trait for $ty
399            $(; |$candidate| $is_bit_valid)?
400        ); });
401        impl_or_verify!(@verify $trait, {
402            impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
403        });
404    };
405    (@impl $impl_block:tt) => {
406        #[cfg(not(any(feature = "derive", test)))]
407        { $impl_block };
408    };
409    (@verify $trait:ident, $impl_block:tt) => {
410        #[cfg(any(feature = "derive", test))]
411        {
412            // On some toolchains, `Subtrait` triggers the `dead_code` lint
413            // because it is implemented but never used.
414            #[allow(dead_code)]
415            trait Subtrait: $trait {}
416            $impl_block
417        };
418    };
419}
420
421/// Implements `KnownLayout` for a sized type.
422macro_rules! impl_known_layout {
423    ($(const $constvar:ident : $constty:ty, $tyvar:ident $(: ?$optbound:ident)? => $ty:ty),* $(,)?) => {
424        $(impl_known_layout!(@inner const $constvar: $constty, $tyvar $(: ?$optbound)? => $ty);)*
425    };
426    ($($tyvar:ident $(: ?$optbound:ident)? => $ty:ty),* $(,)?) => {
427        $(impl_known_layout!(@inner , $tyvar $(: ?$optbound)? => $ty);)*
428    };
429    ($($(#[$attrs:meta])* $ty:ty),*) => { $(impl_known_layout!(@inner , => $(#[$attrs])* $ty);)* };
430    (@inner $(const $constvar:ident : $constty:ty)? , $($tyvar:ident $(: ?$optbound:ident)?)? => $(#[$attrs:meta])* $ty:ty) => {
431        const _: () = {
432            use core::ptr::NonNull;
433
434            #[allow(non_local_definitions)]
435            $(#[$attrs])*
436            // SAFETY: Delegates safety to `DstLayout::for_type`.
437            unsafe impl<$($tyvar $(: ?$optbound)?)? $(, const $constvar : $constty)?> KnownLayout for $ty {
438                #[allow(clippy::missing_inline_in_public_items)]
439                #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
440                fn only_derive_is_allowed_to_implement_this_trait() where Self: Sized {}
441
442                type PointerMetadata = ();
443
444                // SAFETY: `CoreMaybeUninit<T>::LAYOUT` and `T::LAYOUT` are
445                // identical because `CoreMaybeUninit<T>` has the same size and
446                // alignment as `T` [1], and `CoreMaybeUninit` admits
447                // uninitialized bytes in all positions.
448                //
449                // [1] Per https://doc.rust-lang.org/1.81.0/std/mem/union.MaybeUninit.html#layout-1:
450                //
451                //   `MaybeUninit<T>` is guaranteed to have the same size,
452                //   alignment, and ABI as `T`
453                type MaybeUninit = core::mem::MaybeUninit<Self>;
454
455                const LAYOUT: crate::DstLayout = crate::DstLayout::for_type::<$ty>();
456
457                // SAFETY: `.cast` preserves address and provenance.
458                //
459                // FIXME(#429): Add documentation to `.cast` that promises that
460                // it preserves provenance.
461                #[inline(always)]
462                fn raw_from_ptr_len(bytes: NonNull<u8>, _meta: ()) -> NonNull<Self> {
463                    bytes.cast::<Self>()
464                }
465
466                #[inline(always)]
467                fn pointer_to_metadata(_ptr: *mut Self) -> () {
468                }
469            }
470        };
471    };
472}
473
474/// Implements `KnownLayout` for a type in terms of the implementation of
475/// another type with the same representation.
476///
477/// # Safety
478///
479/// - `$ty` and `$repr` must have the same:
480///   - Fixed prefix size
481///   - Alignment
482///   - (For DSTs) trailing slice element size
483/// - It must be valid to perform an `as` cast from `*mut $repr` to `*mut $ty`,
484///   and this operation must preserve referent size (ie, `size_of_val_raw`).
485macro_rules! unsafe_impl_known_layout {
486    ($($tyvar:ident: ?Sized + KnownLayout =>)? #[repr($repr:ty)] $ty:ty) => {{
487        use core::ptr::NonNull;
488
489        crate::util::macros::__unsafe();
490
491        #[allow(non_local_definitions)]
492        // SAFETY: The caller promises that this is sound.
493        unsafe impl<$($tyvar: ?Sized + KnownLayout)?> KnownLayout for $ty {
494            #[allow(clippy::missing_inline_in_public_items, dead_code)]
495            #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))]
496            fn only_derive_is_allowed_to_implement_this_trait() {}
497
498            type PointerMetadata = <$repr as KnownLayout>::PointerMetadata;
499            type MaybeUninit = <$repr as KnownLayout>::MaybeUninit;
500
501            const LAYOUT: DstLayout = <$repr as KnownLayout>::LAYOUT;
502
503            // SAFETY: All operations preserve address and provenance. Caller
504            // has promised that the `as` cast preserves size.
505            //
506            // FIXME(#429): Add documentation to `NonNull::new_unchecked` that
507            // it preserves provenance.
508            #[inline(always)]
509            fn raw_from_ptr_len(bytes: NonNull<u8>, meta: <$repr as KnownLayout>::PointerMetadata) -> NonNull<Self> {
510                #[allow(clippy::as_conversions)]
511                let ptr = <$repr>::raw_from_ptr_len(bytes, meta).as_ptr() as *mut Self;
512                // SAFETY: `ptr` was converted from `bytes`, which is non-null.
513                unsafe { NonNull::new_unchecked(ptr) }
514            }
515
516            #[inline(always)]
517            fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
518                #[allow(clippy::as_conversions)]
519                let ptr = ptr as *mut $repr;
520                <$repr>::pointer_to_metadata(ptr)
521            }
522        }
523    }};
524}
525
526/// Uses `align_of` to confirm that a type or set of types have alignment 1.
527///
528/// Note that `align_of<T>` requires `T: Sized`, so this macro doesn't work for
529/// unsized types.
530macro_rules! assert_unaligned {
531    ($($tys:ty),*) => {
532        $(
533            // We only compile this assertion under `cfg(test)` to avoid taking
534            // an extra non-dev dependency (and making this crate more expensive
535            // to compile for our dependents).
536            #[cfg(test)]
537            static_assertions::const_assert_eq!(core::mem::align_of::<$tys>(), 1);
538        )*
539    };
540}
541
542/// Emits a function definition as either `const fn` or `fn` depending on
543/// whether the current toolchain version supports `const fn` with generic trait
544/// bounds.
545macro_rules! maybe_const_trait_bounded_fn {
546    // This case handles both `self` methods (where `self` is by value) and
547    // non-method functions. Each `$args` may optionally be followed by `:
548    // $arg_tys:ty`, which can be omitted for `self`.
549    ($(#[$attr:meta])* $vis:vis const fn $name:ident($($args:ident $(: $arg_tys:ty)?),* $(,)?) $(-> $ret_ty:ty)? $body:block) => {
550        #[cfg(zerocopy_generic_bounds_in_const_fn_1_61_0)]
551        $(#[$attr])* $vis const fn $name($($args $(: $arg_tys)?),*) $(-> $ret_ty)? $body
552
553        #[cfg(not(zerocopy_generic_bounds_in_const_fn_1_61_0))]
554        $(#[$attr])* $vis fn $name($($args $(: $arg_tys)?),*) $(-> $ret_ty)? $body
555    };
556}
557
558/// Either panic (if the current Rust toolchain supports panicking in `const
559/// fn`) or evaluate a constant that will cause an array indexing error whose
560/// error message will include the format string.
561///
562/// The type that this expression evaluates to must be `Copy`, or else the
563/// non-panicking desugaring will fail to compile.
564macro_rules! const_panic {
565    (@non_panic $($_arg:tt)+) => {{
566        // This will type check to whatever type is expected based on the call
567        // site.
568        let panic: [_; 0] = [];
569        // This will always fail (since we're indexing into an array of size 0.
570        #[allow(unconditional_panic)]
571        panic[0]
572    }};
573    ($($arg:tt)+) => {{
574        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0)]
575        panic!($($arg)+);
576        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0))]
577        const_panic!(@non_panic $($arg)+)
578    }};
579}
580
581/// Either assert (if the current Rust toolchain supports panicking in `const
582/// fn`) or evaluate the expression and, if it evaluates to `false`, call
583/// `const_panic!`. This is used in place of `assert!` in const contexts to
584/// accommodate old toolchains.
585macro_rules! const_assert {
586    ($e:expr) => {{
587        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0)]
588        assert!($e);
589        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0))]
590        {
591            let e = $e;
592            if !e {
593                let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e)));
594            }
595        }
596    }};
597    ($e:expr, $($args:tt)+) => {{
598        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0)]
599        assert!($e, $($args)+);
600        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0))]
601        {
602            let e = $e;
603            if !e {
604                let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e), ": ", stringify!($arg)), $($args)*);
605            }
606        }
607    }};
608}
609
610/// Like `const_assert!`, but relative to `debug_assert!`.
611macro_rules! const_debug_assert {
612    ($e:expr $(, $msg:expr)?) => {{
613        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0)]
614        debug_assert!($e $(, $msg)?);
615        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0))]
616        {
617            // Use this (rather than `#[cfg(debug_assertions)]`) to ensure that
618            // `$e` is always compiled even if it will never be evaluated at
619            // runtime.
620            if cfg!(debug_assertions) {
621                let e = $e;
622                if !e {
623                    let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e) $(, ": ", $msg)?));
624                }
625            }
626        }
627    }}
628}
629
630/// Either invoke `unreachable!()` or `loop {}` depending on whether the Rust
631/// toolchain supports panicking in `const fn`.
632macro_rules! const_unreachable {
633    () => {{
634        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0)]
635        unreachable!();
636
637        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve_1_57_0))]
638        loop {}
639    }};
640}
641
642/// Asserts at compile time that `$condition` is true for `Self` or the given
643/// `$tyvar`s. Unlike `const_assert`, this is *strictly* a compile-time check;
644/// it cannot be evaluated in a runtime context. The condition is checked after
645/// monomorphization and, upon failure, emits a compile error.
646macro_rules! static_assert {
647    (Self $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )? => $condition:expr $(, $args:tt)*) => {{
648        trait StaticAssert {
649            const ASSERT: bool;
650        }
651
652        impl<T $(: $(? $optbound +)* $($bound +)*)?> StaticAssert for T {
653            const ASSERT: bool = {
654                const_assert!($condition $(, $args)*);
655                $condition
656            };
657        }
658
659        const_assert!(<Self as StaticAssert>::ASSERT);
660    }};
661    ($($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $condition:expr $(, $args:tt)*) => {{
662        trait StaticAssert {
663            const ASSERT: bool;
664        }
665
666        // NOTE: We use `PhantomData` so we can support unsized types.
667        impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?,)*> StaticAssert for ($(core::marker::PhantomData<$tyvar>,)*) {
668            const ASSERT: bool = {
669                const_assert!($condition $(, $args)*);
670                $condition
671            };
672        }
673
674        const_assert!(<($(core::marker::PhantomData<$tyvar>,)*) as StaticAssert>::ASSERT);
675    }};
676}
677
678/// Assert at compile time that `tyvar` does not have a zero-sized DST
679/// component.
680macro_rules! static_assert_dst_is_not_zst {
681    ($tyvar:ident) => {{
682        use crate::KnownLayout;
683        static_assert!($tyvar: ?Sized + KnownLayout => {
684            let dst_is_zst = match $tyvar::LAYOUT.size_info {
685                crate::SizeInfo::Sized { .. } => false,
686                crate::SizeInfo::SliceDst(TrailingSliceLayout { elem_size, .. }) => {
687                    elem_size == 0
688                }
689            };
690            !dst_is_zst
691        }, "cannot call this method on a dynamically-sized type whose trailing slice element is zero-sized");
692    }}
693}
694
695/// # Safety
696///
697/// The caller must ensure that the cast does not grow the size of the referent.
698/// Preserving or shrinking the size of the referent are both acceptable.
699macro_rules! cast {
700    ($p:expr) => {{
701        let ptr: crate::pointer::PtrInner<'_, _> = $p;
702        let ptr = ptr.as_non_null();
703        let ptr = ptr.as_ptr();
704        #[allow(clippy::as_conversions)]
705        let ptr = ptr as *mut _;
706        #[allow(unused_unsafe)]
707        // SAFETY: `NonNull::as_ptr` returns a non-null pointer, so the argument
708        // to `NonNull::new_unchecked` is also non-null.
709        let ptr = unsafe { core::ptr::NonNull::new_unchecked(ptr) };
710        // SAFETY: The caller promises that the cast preserves or shrinks
711        // referent size. By invariant on `$p: PtrInner` (guaranteed by type
712        // annotation above), `$p` refers to a byte range entirely contained
713        // inside of a single allocation, has provenance for that whole byte
714        // range, and will not outlive the allocation. All of these conditions
715        // are preserved when preserving or shrinking referent size.
716        crate::pointer::PtrInner::new(ptr)
717    }};
718}
719
720/// Implements `TransmuteFrom` and `SizeEq` for `T` and `$wrapper<T>`.
721///
722/// # Safety
723///
724/// `T` and `$wrapper<T>` must have the same bit validity, and must have the
725/// same size in the sense of `SizeEq`.
726macro_rules! unsafe_impl_for_transparent_wrapper {
727    (T $(: ?$optbound:ident)? => $wrapper:ident<T>) => {{
728        crate::util::macros::__unsafe();
729
730        use crate::pointer::{TransmuteFrom, PtrInner, SizeEq, invariant::Valid};
731
732        // SAFETY: The caller promises that `T` and `$wrapper<T>` have the same
733        // bit validity.
734        unsafe impl<T $(: ?$optbound)?> TransmuteFrom<T, Valid, Valid> for $wrapper<T> {}
735        // SAFETY: See previous safety comment.
736        unsafe impl<T $(: ?$optbound)?> TransmuteFrom<$wrapper<T>, Valid, Valid> for T {}
737        // SAFETY: The caller promises that `T` and `$wrapper<T>` satisfy
738        // `SizeEq`.
739        unsafe impl<T $(: ?$optbound)?> SizeEq<T> for $wrapper<T> {
740            #[inline(always)]
741            fn cast_from_raw(t: PtrInner<'_, T>) -> PtrInner<'_, $wrapper<T>> {
742                // SAFETY: See previous safety comment.
743                unsafe { cast!(t) }
744            }
745        }
746        // SAFETY: See previous safety comment.
747        unsafe impl<T $(: ?$optbound)?> SizeEq<$wrapper<T>> for T {
748            #[inline(always)]
749            fn cast_from_raw(t: PtrInner<'_, $wrapper<T>>) -> PtrInner<'_, T> {
750                // SAFETY: See previous safety comment.
751                unsafe { cast!(t) }
752            }
753        }
754    }};
755}
756
757macro_rules! impl_transitive_transmute_from {
758    ($($tyvar:ident $(: ?$optbound:ident)?)? => $t:ty => $u:ty => $v:ty) => {
759        const _: () = {
760            use crate::pointer::{TransmuteFrom, PtrInner, SizeEq, invariant::Valid};
761
762            // SAFETY: Since `$u: SizeEq<$t>` and `$v: SizeEq<U>`, this impl is
763            // transitively sound.
764            unsafe impl<$($tyvar $(: ?$optbound)?)?> SizeEq<$t> for $v
765            where
766                $u: SizeEq<$t>,
767                $v: SizeEq<$u>,
768            {
769                #[inline(always)]
770                fn cast_from_raw(t: PtrInner<'_, $t>) -> PtrInner<'_, $v> {
771                    let u = <$u as SizeEq<_>>::cast_from_raw(t);
772                    <$v as SizeEq<_>>::cast_from_raw(u)
773                }
774            }
775
776            // SAFETY: Since `$u: TransmuteFrom<$t, Valid, Valid>`, it is sound
777            // to transmute a bit-valid `$t` to a bit-valid `$u`. Since `$v:
778            // TransmuteFrom<$u, Valid, Valid>`, it is sound to transmute that
779            // bit-valid `$u` to a bit-valid `$v`.
780            unsafe impl<$($tyvar $(: ?$optbound)?)?> TransmuteFrom<$t, Valid, Valid> for $v
781            where
782                $u: TransmuteFrom<$t, Valid, Valid>,
783                $v: TransmuteFrom<$u, Valid, Valid>,
784            {}
785        };
786    };
787}
788
789#[rustfmt::skip]
790macro_rules! impl_size_eq {
791    ($t:ty, $u:ty) => {
792        const _: () = {
793            use crate::{KnownLayout, pointer::{PtrInner, SizeEq}};
794
795            static_assert!(=> {
796                let t = <$t as KnownLayout>::LAYOUT;
797                let u = <$u as KnownLayout>::LAYOUT;
798                t.align.get() >= u.align.get() && match (t.size_info, u.size_info) {
799                    (SizeInfo::Sized { size: t }, SizeInfo::Sized { size: u }) => t == u,
800                    (
801                        SizeInfo::SliceDst(TrailingSliceLayout { offset: t_offset, elem_size: t_elem_size }),
802                        SizeInfo::SliceDst(TrailingSliceLayout { offset: u_offset, elem_size: u_elem_size })
803                    ) => t_offset == u_offset && t_elem_size == u_elem_size,
804                    _ => false,
805                }
806            });
807
808            // SAFETY: See inline.
809            unsafe impl SizeEq<$t> for $u {
810                #[inline(always)]
811                fn cast_from_raw(t: PtrInner<'_, $t>) -> PtrInner<'_, $u> {
812                    // SAFETY: We've asserted that their
813                    // `KnownLayout::LAYOUT.size_info`s are equal, and so this
814                    // cast is guaranteed to preserve address and referent size.
815                    // It trivially preserves provenance.
816                    unsafe { cast!(t) }
817                }
818            }
819            // SAFETY: See previous safety comment.
820            unsafe impl SizeEq<$u> for $t {
821                #[inline(always)]
822                fn cast_from_raw(u: PtrInner<'_, $u>) -> PtrInner<'_, $t> {
823                    // SAFETY: See previous safety comment.
824                    unsafe { cast!(u) }
825                }
826            }
827        };
828    };
829}
830
831/// Invokes `$blk` in a context in which `$src<$t>` and `$dst<$u>` implement
832/// `SizeEq`.
833///
834/// This macro emits code which implements `SizeEq`, and ensures that the impl
835/// is sound via PME.
836///
837/// # Safety
838///
839/// Inside of `$blk`, the caller must only use `$src` and `$dst` as `$src<$t>`
840/// and `$dst<$u>`. The caller must not use `$src` or `$dst` to wrap any other
841/// types.
842macro_rules! unsafe_with_size_eq {
843    (<$src:ident<$t:ident>, $dst:ident<$u:ident>> $blk:expr) => {{
844        crate::util::macros::__unsafe();
845
846        use crate::{KnownLayout, pointer::PtrInner};
847
848        #[repr(transparent)]
849        struct $src<T: ?Sized>(T);
850
851        #[repr(transparent)]
852        struct $dst<U: ?Sized>(U);
853
854        // SAFETY: Since `$src<T>` is a `#[repr(transparent)]` wrapper around
855        // `T`, it has the same bit validity and size as `T`.
856        unsafe_impl_for_transparent_wrapper!(T: ?Sized => $src<T>);
857
858        // SAFETY: Since `$dst<T>` is a `#[repr(transparent)]` wrapper around
859        // `T`, it has the same bit validity and size as `T`.
860        unsafe_impl_for_transparent_wrapper!(T: ?Sized => $dst<T>);
861
862        // SAFETY: `$src<T>` is a `#[repr(transparent)]` wrapper around `T` with
863        // no added semantics.
864        unsafe impl<T: ?Sized> InvariantsEq<$src<T>> for T {}
865
866        // SAFETY: `$dst<T>` is a `#[repr(transparent)]` wrapper around `T` with
867        // no added semantics.
868        unsafe impl<T: ?Sized> InvariantsEq<$dst<T>> for T {}
869
870        // SAFETY: See inline for the soundness of this impl when
871        // `cast_from_raw` is actually instantiated (otherwise, PMEs may not be
872        // triggered).
873        //
874        // We manually instantiate `cast_from_raw` below to ensure that this PME
875        // can be triggered, and the caller promises not to use `$src` and
876        // `$dst` with any wrapped types other than `$t` and `$u` respectively.
877        unsafe impl<T: ?Sized, U: ?Sized> SizeEq<$src<T>> for $dst<U>
878        where
879            T: KnownLayout<PointerMetadata = usize>,
880            U: KnownLayout<PointerMetadata = usize>,
881        {
882            fn cast_from_raw(src: PtrInner<'_, $src<T>>) -> PtrInner<'_, Self> {
883                // SAFETY: `crate::layout::cast_from_raw` promises to satisfy
884                // the safety invariants of `SizeEq::cast_from_raw`, or to
885                // generate a PME. Since `$src<T>` and `$dst<U>` are
886                // `#[repr(transparent)]` wrappers around `T` and `U`
887                // respectively, a `cast_from_raw` impl which satisfies the
888                // conditions for casting from `NonNull<T>` to `NonNull<U>` also
889                // satisfies the conditions for casting from `NonNull<$src<T>>`
890                // to `NonNull<$dst<U>>`.
891
892                // SAFETY: By the preceding safety comment, this cast preserves
893                // referent size.
894                let src: PtrInner<'_, T> = unsafe { cast!(src) };
895                let dst: PtrInner<'_, U> = crate::layout::cast_from_raw(src);
896                // SAFETY: By the preceding safety comment, this cast preserves
897                // referent size.
898                unsafe { cast!(dst) }
899            }
900        }
901
902        // See safety comment on the preceding `unsafe impl` block for an
903        // explanation of why we need this block.
904        if 1 == 0 {
905            let ptr = <$t as KnownLayout>::raw_dangling();
906            #[allow(unused_unsafe)]
907            // SAFETY: This call is never executed.
908            let ptr = unsafe { crate::pointer::PtrInner::new(ptr) };
909            #[allow(unused_unsafe)]
910            // SAFETY: This call is never executed.
911            let ptr = unsafe { cast!(ptr) };
912            let _ = <$dst<$u> as SizeEq<$src<$t>>>::cast_from_raw(ptr);
913        }
914
915        impl_for_transmute_from!(T: ?Sized + TryFromBytes => TryFromBytes for $src<T>[<T>]);
916        impl_for_transmute_from!(T: ?Sized + FromBytes => FromBytes for $src<T>[<T>]);
917        impl_for_transmute_from!(T: ?Sized + FromZeros => FromZeros for $src<T>[<T>]);
918        impl_for_transmute_from!(T: ?Sized + IntoBytes => IntoBytes for $src<T>[<T>]);
919
920        impl_for_transmute_from!(U: ?Sized + TryFromBytes => TryFromBytes for $dst<U>[<U>]);
921        impl_for_transmute_from!(U: ?Sized + FromBytes => FromBytes for $dst<U>[<U>]);
922        impl_for_transmute_from!(U: ?Sized + FromZeros => FromZeros for $dst<U>[<U>]);
923        impl_for_transmute_from!(U: ?Sized + IntoBytes => IntoBytes for $dst<U>[<U>]);
924
925        // SAFETY: `$src<T>` is a `#[repr(transparent)]` wrapper around `T`, and
926        // so permits interior mutation exactly when `T` does.
927        unsafe_impl!(T: ?Sized + Immutable => Immutable for $src<T>);
928
929        // SAFETY: `$dst<T>` is a `#[repr(transparent)]` wrapper around `T`, and
930        // so permits interior mutation exactly when `T` does.
931        unsafe_impl!(T: ?Sized + Immutable => Immutable for $dst<T>);
932
933        $blk
934    }};
935}
936
937/// A no-op `unsafe fn` for use in macro expansions.
938///
939/// Calling this function in a macro expansion ensures that the macro's caller
940/// must wrap the call in `unsafe { ... }`.
941pub(crate) const unsafe fn __unsafe() {}