From 07082c3a917e4099465419266155589c47a00a8b Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Thu, 2 Nov 2023 05:52:39 -0700 Subject: [PATCH] Implement KnownLayout, FromZeroes for raw pointers (#584) Makes progress on #170 --- src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index bff5c98580..0683a44c27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -707,6 +707,8 @@ impl_known_layout!( T: ?Sized => PhantomData, T => Wrapping, T => MaybeUninit, + T: ?Sized => *const T, + T: ?Sized => *mut T, ); impl_known_layout!(const N: usize, T => [T; N]); @@ -1865,6 +1867,22 @@ safety_comment! { unsafe_impl!(T: AsBytes => AsBytes for [T]); unsafe_impl!(T: Unaligned => Unaligned for [T]); } +safety_comment! { + /// SAFETY: + /// - `FromZeroes`: For thin pointers (note that `T: Sized`), the zero + /// pointer is considered "null". [1] No operations which require + /// provenance are legal on null pointers, so this is not a footgun. + /// + /// NOTE(#170): Implementing `FromBytes` and `AsBytes` for raw pointers + /// would be sound, but carries provenance footguns. We want to support + /// `FromBytes` and `AsBytes` for raw pointers eventually, but we are + /// holding off until we can figure out how to address those footguns. + /// + /// [1] TODO(https://github.com/rust-lang/rust/pull/116988): Cite the + /// documentation once this PR lands. + unsafe_impl!(T => FromZeroes for *const T); + unsafe_impl!(T => FromZeroes for *mut T); +} // SIMD support // @@ -5384,6 +5402,13 @@ mod tests { assert_impls!([u8; 1]: KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned); assert_impls!([NotZerocopy; 1]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); + assert_impls!(*const NotZerocopy: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned); + assert_impls!(*mut NotZerocopy: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned); + assert_impls!(*const [NotZerocopy]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); + assert_impls!(*mut [NotZerocopy]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); + assert_impls!(*const dyn Debug: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); + assert_impls!(*mut dyn Debug: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); + #[cfg(feature = "simd")] { #[allow(unused_macros)]