Skip to content

Commit

Permalink
add without_provenance to pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 23, 2024
1 parent cff979e commit 9620504
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/core_simd/src/simd/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ pub trait SimdConstPtr: Copy + Sealed {
/// Equivalent to calling [`pointer::addr`] on each element.
fn addr(self) -> Self::Usize;

/// Convert an address to a pointer without giving it any provenance.
///
/// Without provenance, this pointer is not associated with any actual allocation. Such a
/// no-provenance pointer may be used for zero-sized memory accesses (if suitably aligned), but
/// non-zero-sized memory accesses with a no-provenance pointer are UB. No-provenance pointers
/// are little more than a usize address in disguise.
///
/// This is different from [`Self::from_exposed_addr`], which creates a pointer that picks up a
/// previously exposed provenance.
///
/// Equivalent to calling [`pointer::without_provenance`] on each element.
fn without_provenance(addr: Self::Usize) -> Self;

/// Creates a new pointer with the given address.
///
/// This performs the same operation as a cast, but copies the *address-space* and
Expand Down Expand Up @@ -118,6 +131,14 @@ where
unsafe { core::mem::transmute_copy(&self) }
}

#[inline]
fn without_provenance(addr: Self::Usize) -> Self {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// SAFETY: Integer-to-pointer transmutes are valid (if you are okay with not getting any
// provenance).
unsafe { core::mem::transmute_copy(&addr) }
}

#[inline]
fn with_addr(self, addr: Self::Usize) -> Self {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
Expand Down
21 changes: 21 additions & 0 deletions crates/core_simd/src/simd/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ pub trait SimdMutPtr: Copy + Sealed {
/// Equivalent to calling [`pointer::addr`] on each element.
fn addr(self) -> Self::Usize;

/// Convert an address to a pointer without giving it any provenance.
///
/// Without provenance, this pointer is not associated with any actual allocation. Such a
/// no-provenance pointer may be used for zero-sized memory accesses (if suitably aligned), but
/// non-zero-sized memory accesses with a no-provenance pointer are UB. No-provenance pointers
/// are little more than a usize address in disguise.
///
/// This is different from [`Self::from_exposed_addr`], which creates a pointer that picks up a
/// previously exposed provenance.
///
/// Equivalent to calling [`pointer::without_provenance`] on each element.
fn without_provenance(addr: Self::Usize) -> Self;

/// Creates a new pointer with the given address.
///
/// This performs the same operation as a cast, but copies the *address-space* and
Expand Down Expand Up @@ -115,6 +128,14 @@ where
unsafe { core::mem::transmute_copy(&self) }
}

#[inline]
fn without_provenance(addr: Self::Usize) -> Self {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// SAFETY: Integer-to-pointer transmutes are valid (if you are okay with not getting any
// provenance).
unsafe { core::mem::transmute_copy(&addr) }
}

#[inline]
fn with_addr(self, addr: Self::Usize) -> Self {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
Expand Down

0 comments on commit 9620504

Please sign in to comment.