1*c3739801SMiguel Ojeda // Copyright 2023 The Fuchsia Authors 2*c3739801SMiguel Ojeda // 3*c3739801SMiguel Ojeda // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 4*c3739801SMiguel Ojeda // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT 5*c3739801SMiguel Ojeda // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. 6*c3739801SMiguel Ojeda // This file may not be copied, modified, or distributed except according to 7*c3739801SMiguel Ojeda // those terms. 8*c3739801SMiguel Ojeda 9*c3739801SMiguel Ojeda //! Abstractions over raw pointers. 10*c3739801SMiguel Ojeda 11*c3739801SMiguel Ojeda #![allow(missing_docs)] 12*c3739801SMiguel Ojeda 13*c3739801SMiguel Ojeda mod inner; 14*c3739801SMiguel Ojeda pub mod invariant; 15*c3739801SMiguel Ojeda mod ptr; 16*c3739801SMiguel Ojeda pub mod transmute; 17*c3739801SMiguel Ojeda 18*c3739801SMiguel Ojeda pub use inner::PtrInner; 19*c3739801SMiguel Ojeda pub use invariant::{BecauseExclusive, BecauseImmutable, Read}; 20*c3739801SMiguel Ojeda pub use ptr::{Ptr, TryWithError}; 21*c3739801SMiguel Ojeda pub use transmute::*; 22*c3739801SMiguel Ojeda 23*c3739801SMiguel Ojeda use crate::wrappers::ReadOnly; 24*c3739801SMiguel Ojeda 25*c3739801SMiguel Ojeda /// A shorthand for a maybe-valid, maybe-aligned reference. Used as the argument 26*c3739801SMiguel Ojeda /// to [`TryFromBytes::is_bit_valid`]. 27*c3739801SMiguel Ojeda /// 28*c3739801SMiguel Ojeda /// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid 29*c3739801SMiguel Ojeda pub type Maybe<'a, T, Alignment = invariant::Unaligned> = 30*c3739801SMiguel Ojeda Ptr<'a, ReadOnly<T>, (invariant::Shared, Alignment, invariant::Initialized)>; 31*c3739801SMiguel Ojeda 32*c3739801SMiguel Ojeda /// Checks if the referent is zeroed. 33*c3739801SMiguel Ojeda pub(crate) fn is_zeroed<T, I>(ptr: Ptr<'_, T, I>) -> bool 34*c3739801SMiguel Ojeda where 35*c3739801SMiguel Ojeda T: crate::Immutable + crate::KnownLayout, 36*c3739801SMiguel Ojeda I: invariant::Invariants<Validity = invariant::Initialized>, 37*c3739801SMiguel Ojeda I::Aliasing: invariant::Reference, 38*c3739801SMiguel Ojeda { 39*c3739801SMiguel Ojeda ptr.as_bytes().as_ref().iter().all( 40*c3739801SMiguel Ojeda #[inline(always)] 41*c3739801SMiguel Ojeda |&byte| byte == 0, 42*c3739801SMiguel Ojeda ) 43*c3739801SMiguel Ojeda } 44*c3739801SMiguel Ojeda 45*c3739801SMiguel Ojeda pub mod cast { 46*c3739801SMiguel Ojeda use core::{marker::PhantomData, mem}; 47*c3739801SMiguel Ojeda 48*c3739801SMiguel Ojeda use crate::{ 49*c3739801SMiguel Ojeda layout::{SizeInfo, TrailingSliceLayout}, 50*c3739801SMiguel Ojeda HasField, KnownLayout, PtrInner, 51*c3739801SMiguel Ojeda }; 52*c3739801SMiguel Ojeda 53*c3739801SMiguel Ojeda /// A pointer cast or projection. 54*c3739801SMiguel Ojeda /// 55*c3739801SMiguel Ojeda /// # Safety 56*c3739801SMiguel Ojeda /// 57*c3739801SMiguel Ojeda /// The implementation of `project` must satisfy its safety post-condition. 58*c3739801SMiguel Ojeda pub unsafe trait Project<Src: ?Sized, Dst: ?Sized> { 59*c3739801SMiguel Ojeda /// Projects a pointer from `Src` to `Dst`. 60*c3739801SMiguel Ojeda /// 61*c3739801SMiguel Ojeda /// Users should generally not call `project` directly, and instead 62*c3739801SMiguel Ojeda /// should use high-level APIs like [`PtrInner::project`] or 63*c3739801SMiguel Ojeda /// [`Ptr::project`]. 64*c3739801SMiguel Ojeda /// 65*c3739801SMiguel Ojeda /// [`Ptr::project`]: crate::pointer::Ptr::project 66*c3739801SMiguel Ojeda /// 67*c3739801SMiguel Ojeda /// # Safety 68*c3739801SMiguel Ojeda /// 69*c3739801SMiguel Ojeda /// The returned pointer refers to a non-strict subset of the bytes of 70*c3739801SMiguel Ojeda /// `src`'s referent, and has the same provenance as `src`. 71*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, Src>) -> *mut Dst; 72*c3739801SMiguel Ojeda } 73*c3739801SMiguel Ojeda 74*c3739801SMiguel Ojeda /// A [`Project`] which preserves the address of the referent – a pointer 75*c3739801SMiguel Ojeda /// cast. 76*c3739801SMiguel Ojeda /// 77*c3739801SMiguel Ojeda /// # Safety 78*c3739801SMiguel Ojeda /// 79*c3739801SMiguel Ojeda /// A `Cast` projection must preserve the address of the referent. It may 80*c3739801SMiguel Ojeda /// shrink the set of referent bytes, and it may change the referent's type. 81*c3739801SMiguel Ojeda pub unsafe trait Cast<Src: ?Sized, Dst: ?Sized>: Project<Src, Dst> {} 82*c3739801SMiguel Ojeda 83*c3739801SMiguel Ojeda /// A [`Cast`] which does not shrink the set of referent bytes. 84*c3739801SMiguel Ojeda /// 85*c3739801SMiguel Ojeda /// # Safety 86*c3739801SMiguel Ojeda /// 87*c3739801SMiguel Ojeda /// A `CastExact` projection must preserve the set of referent bytes. 88*c3739801SMiguel Ojeda pub unsafe trait CastExact<Src: ?Sized, Dst: ?Sized>: Cast<Src, Dst> {} 89*c3739801SMiguel Ojeda 90*c3739801SMiguel Ojeda /// A no-op pointer cast. 91*c3739801SMiguel Ojeda #[derive(Default, Copy, Clone)] 92*c3739801SMiguel Ojeda #[allow(missing_debug_implementations)] 93*c3739801SMiguel Ojeda pub struct IdCast; 94*c3739801SMiguel Ojeda 95*c3739801SMiguel Ojeda // SAFETY: `project` returns its argument unchanged, and so it is a 96*c3739801SMiguel Ojeda // provenance-preserving projection which preserves the set of referent 97*c3739801SMiguel Ojeda // bytes. 98*c3739801SMiguel Ojeda unsafe impl<T: ?Sized> Project<T, T> for IdCast { 99*c3739801SMiguel Ojeda #[inline(always)] 100*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, T>) -> *mut T { 101*c3739801SMiguel Ojeda src.as_ptr() 102*c3739801SMiguel Ojeda } 103*c3739801SMiguel Ojeda } 104*c3739801SMiguel Ojeda 105*c3739801SMiguel Ojeda // SAFETY: The `Project::project` impl preserves referent address. 106*c3739801SMiguel Ojeda unsafe impl<T: ?Sized> Cast<T, T> for IdCast {} 107*c3739801SMiguel Ojeda 108*c3739801SMiguel Ojeda // SAFETY: The `Project::project` impl preserves referent size. 109*c3739801SMiguel Ojeda unsafe impl<T: ?Sized> CastExact<T, T> for IdCast {} 110*c3739801SMiguel Ojeda 111*c3739801SMiguel Ojeda /// A pointer cast which preserves or shrinks the set of referent bytes of 112*c3739801SMiguel Ojeda /// a statically-sized referent. 113*c3739801SMiguel Ojeda /// 114*c3739801SMiguel Ojeda /// # Safety 115*c3739801SMiguel Ojeda /// 116*c3739801SMiguel Ojeda /// The implementation of [`Project`] uses a compile-time assertion to 117*c3739801SMiguel Ojeda /// guarantee that `Dst` is no larger than `Src`. Thus, `CastSized` has a 118*c3739801SMiguel Ojeda /// sound implementation of [`Project`] for all `Src` and `Dst` – the caller 119*c3739801SMiguel Ojeda /// may pass any `Src` and `Dst` without being responsible for soundness. 120*c3739801SMiguel Ojeda #[allow(missing_debug_implementations, missing_copy_implementations)] 121*c3739801SMiguel Ojeda pub enum CastSized {} 122*c3739801SMiguel Ojeda 123*c3739801SMiguel Ojeda // SAFETY: By the `static_assert!`, `Dst` is no larger than `Src`, 124*c3739801SMiguel Ojeda // and so all casts preserve or shrink the set of referent bytes. All 125*c3739801SMiguel Ojeda // operations preserve provenance. 126*c3739801SMiguel Ojeda unsafe impl<Src, Dst> Project<Src, Dst> for CastSized { 127*c3739801SMiguel Ojeda #[inline(always)] 128*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, Src>) -> *mut Dst { 129*c3739801SMiguel Ojeda static_assert!(Src, Dst => mem::size_of::<Src>() >= mem::size_of::<Dst>()); 130*c3739801SMiguel Ojeda src.as_ptr().cast::<Dst>() 131*c3739801SMiguel Ojeda } 132*c3739801SMiguel Ojeda } 133*c3739801SMiguel Ojeda 134*c3739801SMiguel Ojeda // SAFETY: The `Project::project` impl preserves referent address. 135*c3739801SMiguel Ojeda unsafe impl<Src, Dst> Cast<Src, Dst> for CastSized {} 136*c3739801SMiguel Ojeda 137*c3739801SMiguel Ojeda /// A pointer cast which preserves the set of referent bytes of a 138*c3739801SMiguel Ojeda /// statically-sized referent. 139*c3739801SMiguel Ojeda /// 140*c3739801SMiguel Ojeda /// # Safety 141*c3739801SMiguel Ojeda /// 142*c3739801SMiguel Ojeda /// The implementation of [`Project`] uses a compile-time assertion to 143*c3739801SMiguel Ojeda /// guarantee that `Dst` has the same size as `Src`. Thus, `CastSizedExact` 144*c3739801SMiguel Ojeda /// has a sound implementation of [`Project`] for all `Src` and `Dst` – the 145*c3739801SMiguel Ojeda /// caller may pass any `Src` and `Dst` without being responsible for 146*c3739801SMiguel Ojeda /// soundness. 147*c3739801SMiguel Ojeda #[allow(missing_debug_implementations, missing_copy_implementations)] 148*c3739801SMiguel Ojeda pub enum CastSizedExact {} 149*c3739801SMiguel Ojeda 150*c3739801SMiguel Ojeda // SAFETY: By the `static_assert!`, `Dst` has the same size as `Src`, 151*c3739801SMiguel Ojeda // and so all casts preserve the set of referent bytes. All operations 152*c3739801SMiguel Ojeda // preserve provenance. 153*c3739801SMiguel Ojeda unsafe impl<Src, Dst> Project<Src, Dst> for CastSizedExact { 154*c3739801SMiguel Ojeda #[inline(always)] 155*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, Src>) -> *mut Dst { 156*c3739801SMiguel Ojeda static_assert!(Src, Dst => mem::size_of::<Src>() == mem::size_of::<Dst>()); 157*c3739801SMiguel Ojeda src.as_ptr().cast::<Dst>() 158*c3739801SMiguel Ojeda } 159*c3739801SMiguel Ojeda } 160*c3739801SMiguel Ojeda 161*c3739801SMiguel Ojeda // SAFETY: The `Project::project_raw` impl preserves referent address. 162*c3739801SMiguel Ojeda unsafe impl<Src, Dst> Cast<Src, Dst> for CastSizedExact {} 163*c3739801SMiguel Ojeda 164*c3739801SMiguel Ojeda // SAFETY: By the `static_assert!`, `Project::project_raw` impl preserves 165*c3739801SMiguel Ojeda // referent size. 166*c3739801SMiguel Ojeda unsafe impl<Src, Dst> CastExact<Src, Dst> for CastSizedExact {} 167*c3739801SMiguel Ojeda 168*c3739801SMiguel Ojeda /// A pointer cast which preserves or shrinks the set of referent bytes of 169*c3739801SMiguel Ojeda /// a dynamically-sized referent. 170*c3739801SMiguel Ojeda /// 171*c3739801SMiguel Ojeda /// # Safety 172*c3739801SMiguel Ojeda /// 173*c3739801SMiguel Ojeda /// The implementation of [`Project`] uses a compile-time assertion to 174*c3739801SMiguel Ojeda /// guarantee that the cast preserves the set of referent bytes. Thus, 175*c3739801SMiguel Ojeda /// `CastUnsized` has a sound implementation of [`Project`] for all `Src` 176*c3739801SMiguel Ojeda /// and `Dst` – the caller may pass any `Src` and `Dst` without being 177*c3739801SMiguel Ojeda /// responsible for soundness. 178*c3739801SMiguel Ojeda #[allow(missing_debug_implementations, missing_copy_implementations)] 179*c3739801SMiguel Ojeda pub enum CastUnsized {} 180*c3739801SMiguel Ojeda 181*c3739801SMiguel Ojeda // SAFETY: By the `static_assert!`, `Src` and `Dst` are either: 182*c3739801SMiguel Ojeda // - Both sized and equal in size 183*c3739801SMiguel Ojeda // - Both slice DSTs with the same trailing slice offset and element size 184*c3739801SMiguel Ojeda // and with align_of::<Src>() == align_of::<Dst>(). These ensure that any 185*c3739801SMiguel Ojeda // given pointer metadata encodes the same size for both `Src` and `Dst` 186*c3739801SMiguel Ojeda // (note that the alignment is required as it affects the amount of 187*c3739801SMiguel Ojeda // trailing padding). Thus, `project` preserves the set of referent bytes. 188*c3739801SMiguel Ojeda unsafe impl<Src, Dst> Project<Src, Dst> for CastUnsized 189*c3739801SMiguel Ojeda where 190*c3739801SMiguel Ojeda Src: ?Sized + KnownLayout, 191*c3739801SMiguel Ojeda Dst: ?Sized + KnownLayout<PointerMetadata = Src::PointerMetadata>, 192*c3739801SMiguel Ojeda { 193*c3739801SMiguel Ojeda #[inline(always)] 194*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, Src>) -> *mut Dst { 195*c3739801SMiguel Ojeda // FIXME: Do we want this to support shrinking casts as well? If so, 196*c3739801SMiguel Ojeda // we'll need to remove the `CastExact` impl. 197*c3739801SMiguel Ojeda static_assert!(Src: ?Sized + KnownLayout, Dst: ?Sized + KnownLayout => { 198*c3739801SMiguel Ojeda let src = <Src as KnownLayout>::LAYOUT; 199*c3739801SMiguel Ojeda let dst = <Dst as KnownLayout>::LAYOUT; 200*c3739801SMiguel Ojeda match (src.size_info, dst.size_info) { 201*c3739801SMiguel Ojeda (SizeInfo::Sized { size: src_size }, SizeInfo::Sized { size: dst_size }) => src_size == dst_size, 202*c3739801SMiguel Ojeda ( 203*c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset: src_offset, elem_size: src_elem_size }), 204*c3739801SMiguel Ojeda SizeInfo::SliceDst(TrailingSliceLayout { offset: dst_offset, elem_size: dst_elem_size }) 205*c3739801SMiguel Ojeda ) => src.align.get() == dst.align.get() && src_offset == dst_offset && src_elem_size == dst_elem_size, 206*c3739801SMiguel Ojeda _ => false, 207*c3739801SMiguel Ojeda } 208*c3739801SMiguel Ojeda }); 209*c3739801SMiguel Ojeda 210*c3739801SMiguel Ojeda let metadata = Src::pointer_to_metadata(src.as_ptr()); 211*c3739801SMiguel Ojeda Dst::raw_from_ptr_len(src.as_non_null().cast::<u8>(), metadata).as_ptr() 212*c3739801SMiguel Ojeda } 213*c3739801SMiguel Ojeda } 214*c3739801SMiguel Ojeda 215*c3739801SMiguel Ojeda // SAFETY: The `Project::project` impl preserves referent address. 216*c3739801SMiguel Ojeda unsafe impl<Src, Dst> Cast<Src, Dst> for CastUnsized 217*c3739801SMiguel Ojeda where 218*c3739801SMiguel Ojeda Src: ?Sized + KnownLayout, 219*c3739801SMiguel Ojeda Dst: ?Sized + KnownLayout<PointerMetadata = Src::PointerMetadata>, 220*c3739801SMiguel Ojeda { 221*c3739801SMiguel Ojeda } 222*c3739801SMiguel Ojeda 223*c3739801SMiguel Ojeda // SAFETY: By the `static_assert!` in `Project::project`, `Src` and `Dst` 224*c3739801SMiguel Ojeda // are either: 225*c3739801SMiguel Ojeda // - Both sized and equal in size 226*c3739801SMiguel Ojeda // - Both slice DSTs with the same alignment, trailing slice offset, and 227*c3739801SMiguel Ojeda // element size. These ensure that any given pointer metadata encodes the 228*c3739801SMiguel Ojeda // same size for both `Src` and `Dst` (note that the alignment is required 229*c3739801SMiguel Ojeda // as it affects the amount of trailing padding). 230*c3739801SMiguel Ojeda unsafe impl<Src, Dst> CastExact<Src, Dst> for CastUnsized 231*c3739801SMiguel Ojeda where 232*c3739801SMiguel Ojeda Src: ?Sized + KnownLayout, 233*c3739801SMiguel Ojeda Dst: ?Sized + KnownLayout<PointerMetadata = Src::PointerMetadata>, 234*c3739801SMiguel Ojeda { 235*c3739801SMiguel Ojeda } 236*c3739801SMiguel Ojeda 237*c3739801SMiguel Ojeda /// A field projection 238*c3739801SMiguel Ojeda /// 239*c3739801SMiguel Ojeda /// A `Projection` is a [`Project`] which implements projection by 240*c3739801SMiguel Ojeda /// delegating to an implementation of [`HasField::project`]. 241*c3739801SMiguel Ojeda #[allow(missing_debug_implementations, missing_copy_implementations)] 242*c3739801SMiguel Ojeda pub struct Projection<F: ?Sized, const VARIANT_ID: i128, const FIELD_ID: i128> { 243*c3739801SMiguel Ojeda _never: core::convert::Infallible, 244*c3739801SMiguel Ojeda _phantom: PhantomData<F>, 245*c3739801SMiguel Ojeda } 246*c3739801SMiguel Ojeda 247*c3739801SMiguel Ojeda // SAFETY: `HasField::project` has the same safety post-conditions as 248*c3739801SMiguel Ojeda // `Project::project`. 249*c3739801SMiguel Ojeda unsafe impl<T: ?Sized, F, const VARIANT_ID: i128, const FIELD_ID: i128> Project<T, T::Type> 250*c3739801SMiguel Ojeda for Projection<F, VARIANT_ID, FIELD_ID> 251*c3739801SMiguel Ojeda where 252*c3739801SMiguel Ojeda T: HasField<F, VARIANT_ID, FIELD_ID>, 253*c3739801SMiguel Ojeda { 254*c3739801SMiguel Ojeda #[inline(always)] 255*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, T>) -> *mut T::Type { 256*c3739801SMiguel Ojeda T::project(src) 257*c3739801SMiguel Ojeda } 258*c3739801SMiguel Ojeda } 259*c3739801SMiguel Ojeda 260*c3739801SMiguel Ojeda // SAFETY: All `repr(C)` union fields exist at offset 0 within the union [1], 261*c3739801SMiguel Ojeda // and so any union projection is actually a cast (ie, preserves address). 262*c3739801SMiguel Ojeda // 263*c3739801SMiguel Ojeda // [1] Per 264*c3739801SMiguel Ojeda // https://doc.rust-lang.org/1.92.0/reference/type-layout.html#reprc-unions, 265*c3739801SMiguel Ojeda // it's not *technically* guaranteed that non-maximally-sized fields 266*c3739801SMiguel Ojeda // are at offset 0, but it's clear that this is the intention of `repr(C)` 267*c3739801SMiguel Ojeda // unions. It says: 268*c3739801SMiguel Ojeda // 269*c3739801SMiguel Ojeda // > A union declared with `#[repr(C)]` will have the same size and 270*c3739801SMiguel Ojeda // > alignment as an equivalent C union declaration in the C language for 271*c3739801SMiguel Ojeda // > the target platform. 272*c3739801SMiguel Ojeda // 273*c3739801SMiguel Ojeda // Note that this only mentions size and alignment, not layout. However, 274*c3739801SMiguel Ojeda // C unions *do* guarantee that all fields start at offset 0. [2] 275*c3739801SMiguel Ojeda // 276*c3739801SMiguel Ojeda // This is also reinforced by 277*c3739801SMiguel Ojeda // https://doc.rust-lang.org/1.92.0/reference/items/unions.html#r-items.union.fields.offset: 278*c3739801SMiguel Ojeda // 279*c3739801SMiguel Ojeda // > Fields might have a non-zero offset (except when the C 280*c3739801SMiguel Ojeda // > representation is used); in that case the bits starting at the 281*c3739801SMiguel Ojeda // > offset of the fields are read 282*c3739801SMiguel Ojeda // 283*c3739801SMiguel Ojeda // [2] Per https://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p16: 284*c3739801SMiguel Ojeda // 285*c3739801SMiguel Ojeda // > The size of a union is sufficient to contain the largest of its 286*c3739801SMiguel Ojeda // > members. The value of at most one of the members can be stored in a 287*c3739801SMiguel Ojeda // > union object at any time. A pointer to a union object, suitably 288*c3739801SMiguel Ojeda // > converted, points to each of its members (or if a member is a 289*c3739801SMiguel Ojeda // > bit-field, then to the unit in which it resides), and vice versa. 290*c3739801SMiguel Ojeda // 291*c3739801SMiguel Ojeda // FIXME(https://github.com/rust-lang/unsafe-code-guidelines/issues/595): 292*c3739801SMiguel Ojeda // Cite the documentation once it's updated. 293*c3739801SMiguel Ojeda unsafe impl<T: ?Sized, F, const FIELD_ID: i128> Cast<T, T::Type> 294*c3739801SMiguel Ojeda for Projection<F, { crate::REPR_C_UNION_VARIANT_ID }, FIELD_ID> 295*c3739801SMiguel Ojeda where 296*c3739801SMiguel Ojeda T: HasField<F, { crate::REPR_C_UNION_VARIANT_ID }, FIELD_ID>, 297*c3739801SMiguel Ojeda { 298*c3739801SMiguel Ojeda } 299*c3739801SMiguel Ojeda 300*c3739801SMiguel Ojeda /// A transitive sequence of projections. 301*c3739801SMiguel Ojeda /// 302*c3739801SMiguel Ojeda /// Given `TU: Project` and `UV: Project`, `TransitiveProject<_, TU, UV>` is 303*c3739801SMiguel Ojeda /// a [`Project`] which projects by applying `TU` followed by `UV`. 304*c3739801SMiguel Ojeda /// 305*c3739801SMiguel Ojeda /// If `TU: Cast` and `UV: Cast`, then `TransitiveProject<_, TU, UV>: Cast`. 306*c3739801SMiguel Ojeda #[allow(missing_debug_implementations)] 307*c3739801SMiguel Ojeda pub struct TransitiveProject<U: ?Sized, TU, UV> { 308*c3739801SMiguel Ojeda _never: core::convert::Infallible, 309*c3739801SMiguel Ojeda _projections: PhantomData<(TU, UV)>, 310*c3739801SMiguel Ojeda // On our MSRV (1.56), the debuginfo for a tuple containing both an 311*c3739801SMiguel Ojeda // uninhabited type and a DST causes an ICE. We split `U` from `TU` and 312*c3739801SMiguel Ojeda // `UV` to avoid this situation. 313*c3739801SMiguel Ojeda _u: PhantomData<U>, 314*c3739801SMiguel Ojeda } 315*c3739801SMiguel Ojeda 316*c3739801SMiguel Ojeda // SAFETY: Since `TU::project` and `UV::project` are each 317*c3739801SMiguel Ojeda // provenance-preserving operations which preserve or shrink the set of 318*c3739801SMiguel Ojeda // referent bytes, so is their composition. 319*c3739801SMiguel Ojeda unsafe impl<T, U, V, TU, UV> Project<T, V> for TransitiveProject<U, TU, UV> 320*c3739801SMiguel Ojeda where 321*c3739801SMiguel Ojeda T: ?Sized, 322*c3739801SMiguel Ojeda U: ?Sized, 323*c3739801SMiguel Ojeda V: ?Sized, 324*c3739801SMiguel Ojeda TU: Project<T, U>, 325*c3739801SMiguel Ojeda UV: Project<U, V>, 326*c3739801SMiguel Ojeda { 327*c3739801SMiguel Ojeda #[inline(always)] 328*c3739801SMiguel Ojeda fn project(t: PtrInner<'_, T>) -> *mut V { 329*c3739801SMiguel Ojeda t.project::<_, TU>().project::<_, UV>().as_ptr() 330*c3739801SMiguel Ojeda } 331*c3739801SMiguel Ojeda } 332*c3739801SMiguel Ojeda 333*c3739801SMiguel Ojeda // SAFETY: Since the `Project::project` impl delegates to `TU::project` and 334*c3739801SMiguel Ojeda // `UV::project`, and since `TU` and `UV` are `Cast`, the `Project::project` 335*c3739801SMiguel Ojeda // impl preserves the address of the referent. 336*c3739801SMiguel Ojeda unsafe impl<T, U, V, TU, UV> Cast<T, V> for TransitiveProject<U, TU, UV> 337*c3739801SMiguel Ojeda where 338*c3739801SMiguel Ojeda T: ?Sized, 339*c3739801SMiguel Ojeda U: ?Sized, 340*c3739801SMiguel Ojeda V: ?Sized, 341*c3739801SMiguel Ojeda TU: Cast<T, U>, 342*c3739801SMiguel Ojeda UV: Cast<U, V>, 343*c3739801SMiguel Ojeda { 344*c3739801SMiguel Ojeda } 345*c3739801SMiguel Ojeda 346*c3739801SMiguel Ojeda // SAFETY: Since the `Project::project` impl delegates to `TU::project` and 347*c3739801SMiguel Ojeda // `UV::project`, and since `TU` and `UV` are `CastExact`, the `Project::project` 348*c3739801SMiguel Ojeda // impl preserves the set of referent bytes. 349*c3739801SMiguel Ojeda unsafe impl<T, U, V, TU, UV> CastExact<T, V> for TransitiveProject<U, TU, UV> 350*c3739801SMiguel Ojeda where 351*c3739801SMiguel Ojeda T: ?Sized, 352*c3739801SMiguel Ojeda U: ?Sized, 353*c3739801SMiguel Ojeda V: ?Sized, 354*c3739801SMiguel Ojeda TU: CastExact<T, U>, 355*c3739801SMiguel Ojeda UV: CastExact<U, V>, 356*c3739801SMiguel Ojeda { 357*c3739801SMiguel Ojeda } 358*c3739801SMiguel Ojeda 359*c3739801SMiguel Ojeda /// A cast from `T` to `[u8]`. 360*c3739801SMiguel Ojeda #[allow(missing_copy_implementations, missing_debug_implementations)] 361*c3739801SMiguel Ojeda pub struct AsBytesCast; 362*c3739801SMiguel Ojeda 363*c3739801SMiguel Ojeda // SAFETY: `project` constructs a pointer with the same address as `src` 364*c3739801SMiguel Ojeda // and with a referent of the same size as `*src`. It does this using 365*c3739801SMiguel Ojeda // provenance-preserving operations. 366*c3739801SMiguel Ojeda // 367*c3739801SMiguel Ojeda // FIXME(https://github.com/rust-lang/unsafe-code-guidelines/issues/594): 368*c3739801SMiguel Ojeda // Technically, this proof assumes that `*src` is contiguous (the same is 369*c3739801SMiguel Ojeda // true of other proofs in this codebase). Is this guaranteed anywhere? 370*c3739801SMiguel Ojeda unsafe impl<T: ?Sized + KnownLayout> Project<T, [u8]> for AsBytesCast { 371*c3739801SMiguel Ojeda #[inline(always)] 372*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, T>) -> *mut [u8] { 373*c3739801SMiguel Ojeda let bytes = match T::size_of_val_raw(src.as_non_null()) { 374*c3739801SMiguel Ojeda Some(bytes) => bytes, 375*c3739801SMiguel Ojeda // SAFETY: `KnownLayout::size_of_val_raw` promises to always 376*c3739801SMiguel Ojeda // return `Some` so long as the resulting size fits in a 377*c3739801SMiguel Ojeda // `usize`. By invariant on `PtrInner`, `src` refers to a range 378*c3739801SMiguel Ojeda // of bytes whose size fits in an `isize`, which implies that it 379*c3739801SMiguel Ojeda // also fits in a `usize`. 380*c3739801SMiguel Ojeda None => unsafe { core::hint::unreachable_unchecked() }, 381*c3739801SMiguel Ojeda }; 382*c3739801SMiguel Ojeda 383*c3739801SMiguel Ojeda core::ptr::slice_from_raw_parts_mut(src.as_ptr().cast::<u8>(), bytes) 384*c3739801SMiguel Ojeda } 385*c3739801SMiguel Ojeda } 386*c3739801SMiguel Ojeda 387*c3739801SMiguel Ojeda // SAFETY: The `Project::project` impl preserves referent address. 388*c3739801SMiguel Ojeda unsafe impl<T: ?Sized + KnownLayout> Cast<T, [u8]> for AsBytesCast {} 389*c3739801SMiguel Ojeda 390*c3739801SMiguel Ojeda // SAFETY: The `Project::project` impl preserves the set of referent bytes. 391*c3739801SMiguel Ojeda unsafe impl<T: ?Sized + KnownLayout> CastExact<T, [u8]> for AsBytesCast {} 392*c3739801SMiguel Ojeda 393*c3739801SMiguel Ojeda /// A cast from any type to `()`. 394*c3739801SMiguel Ojeda #[allow(missing_copy_implementations, missing_debug_implementations)] 395*c3739801SMiguel Ojeda pub struct CastToUnit; 396*c3739801SMiguel Ojeda 397*c3739801SMiguel Ojeda // SAFETY: The `project` implementation projects to a subset of its 398*c3739801SMiguel Ojeda // argument's referent using provenance-preserving operations. 399*c3739801SMiguel Ojeda unsafe impl<T: ?Sized> Project<T, ()> for CastToUnit { 400*c3739801SMiguel Ojeda #[inline(always)] 401*c3739801SMiguel Ojeda fn project(src: PtrInner<'_, T>) -> *mut () { 402*c3739801SMiguel Ojeda src.as_ptr().cast::<()>() 403*c3739801SMiguel Ojeda } 404*c3739801SMiguel Ojeda } 405*c3739801SMiguel Ojeda 406*c3739801SMiguel Ojeda // SAFETY: The `project` implementation preserves referent address. 407*c3739801SMiguel Ojeda unsafe impl<T: ?Sized> Cast<T, ()> for CastToUnit {} 408*c3739801SMiguel Ojeda } 409