Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add without_provenance to pointer types #408

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [`core::ptr::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 [`core::ptr::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
Loading