Skip to main content

zerocopy/pointer/
invariant.rs

1// Copyright 2024 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#![allow(missing_copy_implementations, missing_debug_implementations)]
10
11//! The parameterized invariants of a [`Ptr`][super::Ptr].
12//!
13//! Invariants are encoded as ([`Aliasing`], [`Alignment`], [`Validity`])
14//! triples implementing the [`Invariants`] trait.
15
16/// The invariants of a [`Ptr`][super::Ptr].
17pub trait Invariants: Sealed {
18    type Aliasing: Aliasing;
19    type Alignment: Alignment;
20    type Validity: Validity;
21}
22
23impl<A: Aliasing, AA: Alignment, V: Validity> Invariants for (A, AA, V) {
24    type Aliasing = A;
25    type Alignment = AA;
26    type Validity = V;
27}
28
29/// The aliasing invariant of a [`Ptr`][super::Ptr].
30///
31/// All aliasing invariants must permit reading from the bytes of a pointer's
32/// referent which are not covered by [`UnsafeCell`]s.
33///
34/// [`UnsafeCell`]: core::cell::UnsafeCell
35pub trait Aliasing: Sealed {
36    /// Is `Self` [`Exclusive`]?
37    #[doc(hidden)]
38    const IS_EXCLUSIVE: bool;
39}
40
41/// The alignment invariant of a [`Ptr`][super::Ptr].
42pub trait Alignment: Sealed {}
43
44/// The validity invariant of a [`Ptr`][super::Ptr].
45///
46/// # Safety
47///
48/// In this section, we will use `Ptr<T, V>` as a shorthand for `Ptr<T, I:
49/// Invariants<Validity = V>>` for brevity.
50///
51/// Each `V: Validity` defines a set of bit values which may appear in the
52/// referent of a `Ptr<T, V>`, denoted `S(T, V)`. Each `V: Validity`, in its
53/// documentation, provides a definition of `S(T, V)` which must be valid for
54/// all `T: ?Sized`. Any `V: Validity` must guarantee that this set is only a
55/// function of the *bit validity* of the referent type, `T`, and not of any
56/// other property of `T`. As a consequence, given `V: Validity`, `T`, and `U`
57/// where `T` and `U` have the same bit validity, `S(V, T) = S(V, U)`.
58///
59/// It is guaranteed that the referent of any `ptr: Ptr<T, V>` is a member of
60/// `S(T, V)`. Unsafe code must ensure that this guarantee will be upheld for
61/// any existing `Ptr`s or any `Ptr`s that that code creates.
62///
63/// An important implication of this guarantee is that it restricts what
64/// transmutes are sound, where "transmute" is used in this context to refer to
65/// changing the referent type or validity invariant of a `Ptr`, as either
66/// change may change the set of bit values permitted to appear in the referent.
67/// In particular, the following are necessary (but not sufficient) conditions
68/// in order for a transmute from `src: Ptr<T, V>` to `dst: Ptr<U, W>` to be
69/// sound:
70/// - If `S(T, V) = S(U, W)`, then no restrictions apply; otherwise,
71/// - If `dst` permits mutation of its referent (e.g. via `Exclusive` aliasing
72///   or interior mutation under `Shared` aliasing), then it must hold that
73///   `S(T, V) ⊇ S(U, W)` - in other words, the transmute must not expand the
74///   set of allowed referent bit patterns. A violation of this requirement
75///   would permit using `dst` to write `x` where `x ∈ S(U, W)` but `x ∉ S(T,
76///   V)`, which would violate the guarantee that `src`'s referent may only
77///   contain values in `S(T, V)`.
78/// - If the referent may be mutated without going through `dst` while `dst` is
79///   live (e.g. via interior mutation on a `Shared`-aliased `Ptr` or `&`
80///   reference), then it must hold that `S(T, V) ⊆ S(U, W)` - in other words,
81///   the transmute must not shrink the set of allowed referent bit patterns. A
82///   violation of this requirement would permit using `src` or another
83///   mechanism (e.g. a `&` reference used to derive `src`) to write `x` where
84///   `x ∈ S(T, V)` but `x ∉ S(U, W)`, which would violate the guarantee that
85///   `dst`'s referent may only contain values in `S(U, W)`.
86pub unsafe trait Validity: Sealed {
87    const KIND: ValidityKind;
88}
89
90pub enum ValidityKind {
91    Uninit,
92    AsInitialized,
93    Initialized,
94    Valid,
95}
96
97/// An [`Aliasing`] invariant which is either [`Shared`] or [`Exclusive`].
98///
99/// # Safety
100///
101/// Given `A: Reference`, callers may assume that either `A = Shared` or `A =
102/// Exclusive`.
103pub trait Reference: Aliasing + Sealed {}
104
105/// The `Ptr<'a, T>` adheres to the aliasing rules of a `&'a T`.
106///
107/// The referent of a shared-aliased `Ptr` may be concurrently referenced by any
108/// number of shared-aliased `Ptr` or `&T` references, or by any number of
109/// `Ptr<U>` or `&U` references as permitted by `T`'s library safety invariants,
110/// and may not be concurrently referenced by any exclusively-aliased `Ptr`s or
111/// `&mut` references. The referent must not be mutated, except via
112/// [`UnsafeCell`]s, and only when permitted by `T`'s library safety invariants.
113///
114/// [`UnsafeCell`]: core::cell::UnsafeCell
115pub enum Shared {}
116impl Aliasing for Shared {
117    const IS_EXCLUSIVE: bool = false;
118}
119impl Reference for Shared {}
120
121/// The `Ptr<'a, T>` adheres to the aliasing rules of a `&'a mut T`.
122///
123/// The referent of an exclusively-aliased `Ptr` may not be concurrently
124/// referenced by any other `Ptr`s or references, and may not be accessed (read
125/// or written) other than via this `Ptr`.
126pub enum Exclusive {}
127impl Aliasing for Exclusive {
128    const IS_EXCLUSIVE: bool = true;
129}
130impl Reference for Exclusive {}
131
132/// It is unknown whether the pointer is aligned.
133pub enum Unaligned {}
134
135impl Alignment for Unaligned {}
136
137/// The referent is aligned: for `Ptr<T>`, the referent's address is a multiple
138/// of the `T`'s alignment.
139pub enum Aligned {}
140impl Alignment for Aligned {}
141
142/// Any bit pattern is allowed in the `Ptr`'s referent, including uninitialized
143/// bytes.
144pub enum Uninit {}
145// SAFETY: `Uninit`'s validity is well-defined for all `T: ?Sized`, and is not a
146// function of any property of `T` other than its bit validity (in fact, it's
147// not even a property of `T`'s bit validity, but this is more than we are
148// required to uphold).
149unsafe impl Validity for Uninit {
150    const KIND: ValidityKind = ValidityKind::Uninit;
151}
152
153/// The byte ranges initialized in `T` are also initialized in the referent of a
154/// `Ptr<T>`.
155///
156/// Formally: uninitialized bytes may only be present in `Ptr<T>`'s referent
157/// where they are guaranteed to be present in `T`. This is a dynamic property:
158/// if, at a particular byte offset, a valid enum discriminant is set, the
159/// subsequent bytes may only have uninitialized bytes as specified by the
160/// corresponding enum.
161///
162/// Formally, given `len = size_of_val_raw(ptr)`, at every byte offset, `b`, in
163/// the range `[0, len)`:
164/// - If, in any instance `t: T` of length `len`, the byte at offset `b` in `t`
165///   is initialized, then the byte at offset `b` within `*ptr` must be
166///   initialized.
167/// - Let `c` be the contents of the byte range `[0, b)` in `*ptr`. Let `S` be
168///   the subset of valid instances of `T` of length `len` which contain `c` in
169///   the offset range `[0, b)`. If, in any instance of `t: T` in `S`, the byte
170///   at offset `b` in `t` is initialized, then the byte at offset `b` in `*ptr`
171///   must be initialized.
172///
173///   Pragmatically, this means that if `*ptr` is guaranteed to contain an enum
174///   type at a particular offset, and the enum discriminant stored in `*ptr`
175///   corresponds to a valid variant of that enum type, then it is guaranteed
176///   that the appropriate bytes of `*ptr` are initialized as defined by that
177///   variant's bit validity (although note that the variant may contain another
178///   enum type, in which case the same rules apply depending on the state of
179///   its discriminant, and so on recursively).
180pub enum AsInitialized {}
181// SAFETY: `AsInitialized`'s validity is well-defined for all `T: ?Sized`, and
182// is not a function of any property of `T` other than its bit validity.
183unsafe impl Validity for AsInitialized {
184    const KIND: ValidityKind = ValidityKind::AsInitialized;
185}
186
187/// The byte ranges in the referent are fully initialized. In other words, if
188/// the referent is `N` bytes long, then it contains a bit-valid `[u8; N]`.
189pub enum Initialized {}
190// SAFETY: `Initialized`'s validity is well-defined for all `T: ?Sized`, and is
191// not a function of any property of `T` other than its bit validity (in fact,
192// it's not even a property of `T`'s bit validity, but this is more than we are
193// required to uphold).
194unsafe impl Validity for Initialized {
195    const KIND: ValidityKind = ValidityKind::Initialized;
196}
197
198/// The referent of a `Ptr<T>` is valid for `T`, upholding bit validity and any
199/// library safety invariants.
200pub enum Valid {}
201// SAFETY: `Valid`'s validity is well-defined for all `T: ?Sized`, and is not a
202// function of any property of `T` other than its bit validity.
203unsafe impl Validity for Valid {
204    const KIND: ValidityKind = ValidityKind::Valid;
205}
206
207/// # Safety
208///
209/// `DT: CastableFrom<ST, SV, DV>` is sound if `SV = DV = Uninit` or `SV = DV =
210/// Initialized`.
211pub unsafe trait CastableFrom<ST: ?Sized, SV, DV> {}
212
213// SAFETY: `SV = DV = Uninit`.
214unsafe impl<ST: ?Sized, DT: ?Sized> CastableFrom<ST, Uninit, Uninit> for DT {}
215// SAFETY: `SV = DV = Initialized`.
216unsafe impl<ST: ?Sized, DT: ?Sized> CastableFrom<ST, Initialized, Initialized> for DT {}
217
218/// [`Ptr`](crate::Ptr) referents that permit unsynchronized read operations.
219///
220/// `T: Read<A, R>` implies that a pointer to `T` with aliasing `A` permits
221/// unsynchronized read operations. This can be because `A` is [`Exclusive`] or
222/// because `T` does not permit interior mutation.
223///
224/// # Safety
225///
226/// `T: Read<A, R>` if either of the following conditions holds:
227/// - `A` is [`Exclusive`]
228/// - `T` implements [`Immutable`](crate::Immutable)
229///
230/// As a consequence, if `T: Read<A, R>`, then any `Ptr<T, (A, ...)>` is
231/// permitted to perform unsynchronized reads from its referent.
232pub trait Read<A: Aliasing, R> {}
233
234impl<A: Aliasing, T: ?Sized + crate::Immutable> Read<A, BecauseImmutable> for T {}
235impl<T: ?Sized> Read<Exclusive, BecauseExclusive> for T {}
236
237/// Unsynchronized reads are permitted because only one live [`Ptr`](crate::Ptr)
238/// or reference may exist to the referent bytes at a time.
239#[derive(Copy, Clone, Debug)]
240#[doc(hidden)]
241pub enum BecauseExclusive {}
242
243/// Unsynchronized reads are permitted because no live [`Ptr`](crate::Ptr)s or
244/// references permit interior mutation.
245#[derive(Copy, Clone, Debug)]
246#[doc(hidden)]
247pub enum BecauseImmutable {}
248
249use sealed::Sealed;
250mod sealed {
251    use super::*;
252
253    pub trait Sealed {}
254
255    impl Sealed for Shared {}
256    impl Sealed for Exclusive {}
257
258    impl Sealed for Unaligned {}
259    impl Sealed for Aligned {}
260
261    impl Sealed for Uninit {}
262    impl Sealed for AsInitialized {}
263    impl Sealed for Initialized {}
264    impl Sealed for Valid {}
265
266    impl<A: Sealed, AA: Sealed, V: Sealed> Sealed for (A, AA, V) {}
267
268    impl Sealed for BecauseImmutable {}
269    impl Sealed for BecauseExclusive {}
270}