zerocopy/pointer/
transmute.rs

1// Copyright 2025 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
9use core::{
10    cell::{Cell, UnsafeCell},
11    mem::{ManuallyDrop, MaybeUninit},
12    num::Wrapping,
13    ptr::NonNull,
14};
15
16use crate::{pointer::invariant::*, FromBytes, Immutable, IntoBytes, Unalign};
17
18/// Transmutations which are sound to attempt, conditional on validating the bit
19/// validity of the destination type.
20///
21/// If a `Ptr` transmutation is `TryTransmuteFromPtr`, then it is sound to
22/// perform that transmutation so long as some additional mechanism is used to
23/// validate that the referent is bit-valid for the destination type. That
24/// validation mechanism could be a type bound (such as `TransmuteFrom`) or a
25/// runtime validity check.
26///
27/// # Safety
28///
29/// ## Post-conditions
30///
31/// Given `Dst: TryTransmuteFromPtr<Src, A, SV, DV, _>`, callers may assume the
32/// following:
33///
34/// Given `src: Ptr<'a, Src, (A, _, SV)>`, if the referent of `src` is
35/// `DV`-valid for `Dst`, then it is sound to transmute `src` into `dst: Ptr<'a,
36/// Dst, (A, Unaligned, DV)>` by preserving pointer address and metadata.
37///
38/// ## Pre-conditions
39///
40/// Given `src: Ptr<Src, (A, _, SV)>` and `dst: Ptr<Dst, (A, Unaligned, DV)>`,
41/// `Dst: TryTransmuteFromPtr<Src, A, SV, DV, _>` is sound if all of the
42/// following hold:
43/// - Forwards transmutation: Either of the following hold:
44///   - So long as `dst` is active, no mutation of `dst`'s referent is allowed
45///     except via `dst` itself
46///   - The set of `DV`-valid `Dst`s is a superset of the set of `SV`-valid
47///     `Src`s
48/// - Reverse transmutation: Either of the following hold:
49///   - `dst` does not permit mutation of its referent
50///   - The set of `DV`-valid `Dst`s is a subset of the set of `SV`-valid `Src`s
51/// - No safe code, given access to `src` and `dst`, can cause undefined
52///   behavior: Any of the following hold:
53///   - `A` is `Exclusive`
54///   - `Src: Immutable` and `Dst: Immutable`
55///   - It is sound for shared code to operate on a `&Src` and `&Dst` which
56///     reference the same byte range at the same time
57///
58/// ## Proof
59///
60/// Given:
61/// - `src: Ptr<'a, Src, (A, _, SV)>`
62/// - `src`'s referent is `DV`-valid for `Dst`
63/// - `Dst: SizeEq<Src>`
64///
65/// We are trying to prove that it is sound to perform a pointer address- and
66/// metadata-preserving transmute from `src` to a `dst: Ptr<'a, Dst, (A,
67/// Unaligned, DV)>`. We need to prove that such a transmute does not violate
68/// any of `src`'s invariants, and that it satisfies all invariants of the
69/// destination `Ptr` type.
70///
71/// First, all of `src`'s `PtrInner` invariants are upheld. `src`'s address and
72/// metadata are unchanged, so:
73/// - If its referent is not zero sized, then it still has valid provenance for
74///   its referent, which is still entirely contained in some Rust allocation,
75///   `A`
76/// - If its referent is not zero sized, `A` is guaranteed to live for at least
77///   `'a`
78///
79/// Since `Dst: SizeEq<Src>`, and since `dst` has the same address and metadata
80/// as `src`, `dst` addresses the same byte range as `src`. `dst` also has the
81/// same lifetime as `src`. Therefore, all of the `PtrInner` invariants
82/// mentioned above also hold for `dst`.
83///
84/// Second, since `src`'s address is unchanged, it still satisfies its
85/// alignment. Since `dst`'s alignment is `Unaligned`, it trivially satisfies
86/// its alignment.
87///
88/// Third, aliasing is either `Exclusive` or `Shared`:
89/// - If it is `Exclusive`, then both `src` and `dst` satisfy `Exclusive`
90///   aliasing trivially: since `src` and `dst` have the same lifetime, `src` is
91///   inaccessible so long as `dst` is alive, and no other live `Ptr`s or
92///   references may reference the same referent.
93/// - If it is `Shared`, then either:
94///   - `Src: Immutable` and `Dst: Immutable`, and so `UnsafeCell`s trivially
95///     cover the same byte ranges in both types.
96///   - It is explicitly sound for safe code to operate on a `&Src` and a `&Dst`
97///     pointing to the same byte range at the same time.
98///
99/// Fourth, `src`'s validity is satisfied. By invariant, `src`'s referent began
100/// as an `SV`-valid `Src`. It is guaranteed to remain so, as either of the
101/// following hold:
102/// - `dst` does not permit mutation of its referent.
103/// - The set of `DV`-valid `Dst`s is a superset of the set of `SV`-valid
104///   `Src`s. Thus, any value written via `dst` is guaranteed to be `SV`-valid
105///   for `Src`.
106///
107/// Fifth, `dst`'s validity is satisfied. It is a given of this proof that the
108/// referent is `DV`-valid for `Dst`. It is guaranteed to remain so, as either
109/// of the following hold:
110/// - So long as `dst` is active, no mutation of the referent is allowed except
111///   via `dst` itself.
112/// - The set of `DV`-valid `Dst`s is a superset of the set of `SV`-valid
113///   `Src`s. Thus, any value written via `src` is guaranteed to be a `DV`-valid
114///   `Dst`.
115pub unsafe trait TryTransmuteFromPtr<Src: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R>:
116    SizeEq<Src>
117{
118}
119
120#[allow(missing_copy_implementations, missing_debug_implementations)]
121pub enum BecauseMutationCompatible {}
122
123// SAFETY:
124// - Forwards transmutation: By `Dst: MutationCompatible<Src, A, SV, DV, _>`, we
125//   know that at least one of the following holds:
126//   - So long as `dst: Ptr<Dst>` is active, no mutation of its referent is
127//     allowed except via `dst` itself if either of the following hold:
128//     - Aliasing is `Exclusive`, in which case, so long as the `Dst` `Ptr`
129//       exists, no mutation is permitted except via that `Ptr`
130//     - Aliasing is `Shared`, `Src: Immutable`, and `Dst: Immutable`, in which
131//       case no mutation is possible via either `Ptr`
132//   - `Dst: TransmuteFrom<Src, SV, DV>`, and so the set of `DV`-valid `Dst`s is
133//     a supserset of the set of `SV`-valid `Src`s
134// - Reverse transmutation: `Src: TransmuteFrom<Dst, DV, SV>`, and so the set of
135//   `DV`-valid `Dst`s is a subset of the set of `SV`-valid `Src`s
136// - No safe code, given access to `src` and `dst`, can cause undefined
137//   behavior: By `Dst: MutationCompatible<Src, A, SV, DV, _>`, at least one of
138//   the following holds:
139//   - `A` is `Exclusive`
140//   - `Src: Immutable` and `Dst: Immutable`
141//   - `Dst: InvariantsEq<Src>`, which guarantees that `Src` and `Dst` have the
142//     same invariants, and have `UnsafeCell`s covering the same byte ranges
143unsafe impl<Src, Dst, SV, DV, A, R>
144    TryTransmuteFromPtr<Src, A, SV, DV, (BecauseMutationCompatible, R)> for Dst
145where
146    A: Aliasing,
147    SV: Validity,
148    DV: Validity,
149    Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
150    Dst: MutationCompatible<Src, A, SV, DV, R> + SizeEq<Src> + ?Sized,
151{
152}
153
154// SAFETY:
155// - Forwards transmutation: Since aliasing is `Shared` and `Src: Immutable`,
156//   `src` does not permit mutation of its referent.
157// - Reverse transmutation: Since aliasing is `Shared` and `Dst: Immutable`,
158//   `dst` does not permit mutation of its referent.
159// - No safe code, given access to `src` and `dst`, can cause undefined
160//   behavior: `Src: Immutable` and `Dst: Immutable`
161unsafe impl<Src, Dst, SV, DV> TryTransmuteFromPtr<Src, Shared, SV, DV, BecauseImmutable> for Dst
162where
163    SV: Validity,
164    DV: Validity,
165    Src: Immutable + ?Sized,
166    Dst: Immutable + SizeEq<Src> + ?Sized,
167{
168}
169
170/// Denotes that `src: Ptr<Src, (A, _, SV)>` and `dst: Ptr<Self, (A, _, DV)>`,
171/// referencing the same referent at the same time, cannot be used by safe code
172/// to break library safety invariants of `Src` or `Self`.
173///
174/// # Safety
175///
176/// At least one of the following must hold:
177/// - `Src: Read<A, _>` and `Self: Read<A, _>`
178/// - `Self: InvariantsEq<Src>`, and, for some `V`:
179///   - `Dst: TransmuteFrom<Src, V, V>`
180///   - `Src: TransmuteFrom<Dst, V, V>`
181pub unsafe trait MutationCompatible<Src: ?Sized, A: Aliasing, SV, DV, R> {}
182
183#[allow(missing_copy_implementations, missing_debug_implementations)]
184pub enum BecauseRead {}
185
186// SAFETY: `Src: Read<A, _>` and `Dst: Read<A, _>`.
187unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R, S>
188    MutationCompatible<Src, A, SV, DV, (BecauseRead, (R, S))> for Dst
189where
190    Src: Read<A, R>,
191    Dst: Read<A, S>,
192{
193}
194
195/// Denotes that two types have the same invariants.
196///
197/// # Safety
198///
199/// It is sound for safe code to operate on a `&T` and a `&Self` pointing to the
200/// same referent at the same time - no such safe code can cause undefined
201/// behavior.
202pub unsafe trait InvariantsEq<T: ?Sized> {}
203
204// SAFETY: Trivially sound to have multiple `&T` pointing to the same referent.
205unsafe impl<T: ?Sized> InvariantsEq<T> for T {}
206
207// SAFETY: `Dst: InvariantsEq<Src> + TransmuteFrom<Src, V, V>`, and `Src:
208// TransmuteFrom<Dst, V, V>`.
209unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, V: Validity>
210    MutationCompatible<Src, A, V, V, BecauseInvariantsEq> for Dst
211where
212    Src: TransmuteFrom<Dst, V, V>,
213    Dst: TransmuteFrom<Src, V, V> + InvariantsEq<Src>,
214{
215}
216
217pub(crate) enum BecauseInvariantsEq {}
218
219macro_rules! unsafe_impl_invariants_eq {
220    ($tyvar:ident => $t:ty, $u:ty) => {{
221        crate::util::macros::__unsafe();
222        // SAFETY: The caller promises that this is sound.
223        unsafe impl<$tyvar> InvariantsEq<$t> for $u {}
224        // SAFETY: The caller promises that this is sound.
225        unsafe impl<$tyvar> InvariantsEq<$u> for $t {}
226    }};
227}
228
229impl_transitive_transmute_from!(T => MaybeUninit<T> => T => Wrapping<T>);
230impl_transitive_transmute_from!(T => Wrapping<T> => T => MaybeUninit<T>);
231
232// SAFETY: `ManuallyDrop<T>` has the same size and bit validity as `T` [1], and
233// implements `Deref<Target = T>` [2]. Thus, it is already possible for safe
234// code to obtain a `&T` and a `&ManuallyDrop<T>` to the same referent at the
235// same time.
236//
237// [1] Per https://doc.rust-lang.org/1.81.0/std/mem/struct.ManuallyDrop.html:
238//
239//   `ManuallyDrop<T>` is guaranteed to have the same layout and bit
240//   validity as `T`
241//
242// [2] https://doc.rust-lang.org/1.81.0/std/mem/struct.ManuallyDrop.html#impl-Deref-for-ManuallyDrop%3CT%3E
243unsafe impl<T: ?Sized> InvariantsEq<T> for ManuallyDrop<T> {}
244// SAFETY: See previous safety comment.
245unsafe impl<T: ?Sized> InvariantsEq<ManuallyDrop<T>> for T {}
246
247/// Transmutations which are always sound.
248///
249/// `TransmuteFromPtr` is a shorthand for [`TryTransmuteFromPtr`] and
250/// [`TransmuteFrom`].
251///
252/// # Safety
253///
254/// `Dst: TransmuteFromPtr<Src, A, SV, DV, _>` is equivalent to `Dst:
255/// TryTransmuteFromPtr<Src, A, SV, DV, _> + TransmuteFrom<Src, SV, DV>`.
256pub unsafe trait TransmuteFromPtr<Src: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R>:
257    TryTransmuteFromPtr<Src, A, SV, DV, R> + TransmuteFrom<Src, SV, DV>
258{
259}
260
261// SAFETY: The `where` bounds are equivalent to the safety invariant on
262// `TransmuteFromPtr`.
263unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R>
264    TransmuteFromPtr<Src, A, SV, DV, R> for Dst
265where
266    Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, R>,
267{
268}
269
270/// Denotes that any `SV`-valid `Src` may soundly be transmuted into a
271/// `DV`-valid `Self`.
272///
273/// # Safety
274///
275/// The set of bit patterns allowed to appear in the referent of a `Ptr<Src, (_,
276/// _, SV)>` must be a subset of the set allowed to appear in the referent of a
277/// `Ptr<Self, (_, _, DV)>`.
278pub unsafe trait TransmuteFrom<Src: ?Sized, SV, DV>: SizeEq<Src> {}
279
280/// # Safety
281///
282/// `T` and `Self` must have the same vtable kind (`Sized`, slice DST, `dyn`,
283/// etc) and have the same size. In particular:
284/// - If `T: Sized` and `Self: Sized`, then their sizes must be equal
285/// - If `T: ?Sized` and `Self: ?Sized`, then it must be the case that, given
286///   any `t: *mut T`, `t as *mut Self` produces a pointer which addresses the
287///   same number of bytes as `t`.
288pub unsafe trait SizeEq<T: ?Sized> {
289    fn cast_from_raw(t: NonNull<T>) -> NonNull<Self>;
290}
291
292// SAFETY: `T` trivially has the same size and vtable kind as `T`, and since
293// pointer `*mut T -> *mut T` pointer casts are no-ops, this cast trivially
294// preserves referent size (when `T: ?Sized`).
295unsafe impl<T: ?Sized> SizeEq<T> for T {
296    fn cast_from_raw(t: NonNull<T>) -> NonNull<T> {
297        t
298    }
299}
300
301// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
302// initialized bit patterns, which is exactly the set allowed in the referent of
303// any `Initialized` `Ptr`.
304unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
305where
306    Src: IntoBytes + ?Sized,
307    Dst: SizeEq<Src> + ?Sized,
308{
309}
310
311// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
312// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
313// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
314unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
315where
316    Src: ?Sized,
317    Dst: FromBytes + SizeEq<Src> + ?Sized,
318{
319}
320
321// FIXME(#2354): This seems like a smell - the soundness of this bound has
322// nothing to do with `Src` or `Dst` - we're basically just saying `[u8; N]` is
323// transmutable into `[u8; N]`.
324
325// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
326// `Ptr` is the same regardless of referent type.
327unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
328where
329    Src: ?Sized,
330    Dst: SizeEq<Src> + ?Sized,
331{
332}
333
334// FIXME(#2354): This seems like a smell - the soundness of this bound has
335// nothing to do with `Dst` - we're basically just saying that any type is
336// transmutable into `MaybeUninit<[u8; N]>`.
337
338// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
339// therefore can be transmuted from any value.
340unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
341where
342    Src: ?Sized,
343    Dst: SizeEq<Src> + ?Sized,
344    V: Validity,
345{
346}
347
348// SAFETY:
349// - `ManuallyDrop<T>` has the same size as `T` [1]
350// - `ManuallyDrop<T>` has the same validity as `T` [1]
351//
352// [1] Per https://doc.rust-lang.org/1.81.0/std/mem/struct.ManuallyDrop.html:
353//
354//   `ManuallyDrop<T>` is guaranteed to have the same layout and bit validity as
355//   `T`
356const _: () = unsafe { unsafe_impl_for_transparent_wrapper!(T: ?Sized => ManuallyDrop<T>) };
357
358// SAFETY:
359// - `Unalign<T>` promises to have the same size as `T`.
360// - `Unalign<T>` promises to have the same validity as `T`.
361const _: () = unsafe { unsafe_impl_for_transparent_wrapper!(T => Unalign<T>) };
362// SAFETY: `Unalign<T>` promises to have the same size and validity as `T`.
363// Given `u: &Unalign<T>`, it is already possible to obtain `let t =
364// u.try_deref().unwrap()`. Because `Unalign<T>` has the same size as `T`, the
365// returned `&T` must point to the same referent as `u`, and thus it must be
366// sound for these two references to exist at the same time since it's already
367// possible for safe code to get into this state.
368const _: () = unsafe { unsafe_impl_invariants_eq!(T => T, Unalign<T>) };
369
370// SAFETY:
371// - `Wrapping<T>` has the same size as `T` [1].
372// - `Wrapping<T>` has only one field, which is `pub` [2]. We are also
373//   guaranteed per that `Wrapping<T>` has the same layout as `T` [1]. The only
374//   way for both of these to be true simultaneously is for `Wrapping<T>` to
375//   have the same bit validity as `T`. In particular, in order to change the
376//   bit validity, one of the following would need to happen:
377//   - `Wrapping` could change its `repr`, but this would violate the layout
378//     guarantee.
379//   - `Wrapping` could add or change its fields, but this would be a
380//     stability-breaking change.
381//
382// [1] Per https://doc.rust-lang.org/1.85.0/core/num/struct.Wrapping.html#layout-1:
383//
384//   `Wrapping<T>` is guaranteed to have the same layout and ABI as `T`.
385//
386// [2] Definition from https://doc.rust-lang.org/1.85.0/core/num/struct.Wrapping.html:
387//
388//   ```
389//   #[repr(transparent)]
390//   pub struct Wrapping<T>(pub T);
391//   ```
392const _: () = unsafe { unsafe_impl_for_transparent_wrapper!(T => Wrapping<T>) };
393
394// SAFETY: By the preceding safety proof, `Wrapping<T>` and `T` have the same
395// layout and bit validity. Since a `Wrapping<T>`'s `T` field is `pub`, given
396// `w: &Wrapping<T>`, it's possible to do `let t = &w.t`, which means that it's
397// already possible for safe code to obtain a `&Wrapping<T>` and a `&T` pointing
398// to the same referent at the same time. Thus, this must be sound.
399const _: () = unsafe { unsafe_impl_invariants_eq!(T => T, Wrapping<T>) };
400
401// SAFETY:
402// - `UnsafeCell<T>` has the same size as `T` [1].
403// - Per [1], `UnsafeCell<T>` has the same bit validity as `T`. Technically the
404//   term "representation" doesn't guarantee this, but the subsequent sentence
405//   in the documentation makes it clear that this is the intention.
406//
407// [1] Per https://doc.rust-lang.org/1.81.0/core/cell/struct.UnsafeCell.html#memory-layout:
408//
409//   `UnsafeCell<T>` has the same in-memory representation as its inner type
410//   `T`. A consequence of this guarantee is that it is possible to convert
411//   between `T` and `UnsafeCell<T>`.
412const _: () = unsafe { unsafe_impl_for_transparent_wrapper!(T: ?Sized => UnsafeCell<T>) };
413
414// SAFETY:
415// - `Cell<T>` has the same size as `T` [1].
416// - Per [1], `Cell<T>` has the same bit validity as `T`. Technically the term
417//   "representation" doesn't guarantee this, but it does promise to have the
418//   "same memory layout and caveats as `UnsafeCell<T>`." The `UnsafeCell` docs
419//   [2] make it clear that bit validity is the intention even if that phrase
420//   isn't used.
421//
422// [1] Per https://doc.rust-lang.org/1.85.0/std/cell/struct.Cell.html#memory-layout:
423//
424//   `Cell<T>` has the same memory layout and caveats as `UnsafeCell<T>`. In
425//   particular, this means that `Cell<T>` has the same in-memory representation
426//   as its inner type `T`.
427//
428// [2] Per https://doc.rust-lang.org/1.81.0/core/cell/struct.UnsafeCell.html#memory-layout:
429//
430//   `UnsafeCell<T>` has the same in-memory representation as its inner type
431//   `T`. A consequence of this guarantee is that it is possible to convert
432//   between `T` and `UnsafeCell<T>`.
433const _: () = unsafe { unsafe_impl_for_transparent_wrapper!(T: ?Sized => Cell<T>) };
434
435impl_transitive_transmute_from!(T: ?Sized => Cell<T> => T => UnsafeCell<T>);
436impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
437
438// SAFETY: `MaybeUninit<T>` has no validity requirements. Currently this is not
439// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
440// that this is the intention:
441// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
442unsafe impl<T> TransmuteFrom<T, Uninit, Valid> for MaybeUninit<T> {}
443
444// SAFETY: `MaybeUninit<T>` has the same size as `T` [1].
445//
446// [1] Per https://doc.rust-lang.org/1.81.0/std/mem/union.MaybeUninit.html#layout-1:
447//
448//   `MaybeUninit<T>` is guaranteed to have the same size, alignment, and ABI as
449//   `T`
450unsafe impl<T> SizeEq<T> for MaybeUninit<T> {
451    fn cast_from_raw(t: NonNull<T>) -> NonNull<MaybeUninit<T>> {
452        cast!(t)
453    }
454}
455
456// SAFETY: See previous safety comment.
457unsafe impl<T> SizeEq<MaybeUninit<T>> for T {
458    fn cast_from_raw(t: NonNull<MaybeUninit<T>>) -> NonNull<T> {
459        cast!(t)
460    }
461}