Skip to content

Commit 5fa5358

Browse files
Merge pull request #408 from RalfJung/without_provenance
add without_provenance to pointer types
2 parents cff979e + 6c39a26 commit 5fa5358

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

crates/core_simd/src/simd/ptr/const_ptr.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ pub trait SimdConstPtr: Copy + Sealed {
4242
/// Equivalent to calling [`pointer::addr`] on each element.
4343
fn addr(self) -> Self::Usize;
4444

45+
/// Convert an address to a pointer without giving it any provenance.
46+
///
47+
/// Without provenance, this pointer is not associated with any actual allocation. Such a
48+
/// no-provenance pointer may be used for zero-sized memory accesses (if suitably aligned), but
49+
/// non-zero-sized memory accesses with a no-provenance pointer are UB. No-provenance pointers
50+
/// are little more than a usize address in disguise.
51+
///
52+
/// This is different from [`Self::from_exposed_addr`], which creates a pointer that picks up a
53+
/// previously exposed provenance.
54+
///
55+
/// Equivalent to calling [`core::ptr::without_provenance`] on each element.
56+
fn without_provenance(addr: Self::Usize) -> Self;
57+
4558
/// Creates a new pointer with the given address.
4659
///
4760
/// This performs the same operation as a cast, but copies the *address-space* and
@@ -118,6 +131,14 @@ where
118131
unsafe { core::mem::transmute_copy(&self) }
119132
}
120133

134+
#[inline]
135+
fn without_provenance(addr: Self::Usize) -> Self {
136+
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
137+
// SAFETY: Integer-to-pointer transmutes are valid (if you are okay with not getting any
138+
// provenance).
139+
unsafe { core::mem::transmute_copy(&addr) }
140+
}
141+
121142
#[inline]
122143
fn with_addr(self, addr: Self::Usize) -> Self {
123144
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.

crates/core_simd/src/simd/ptr/mut_ptr.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ pub trait SimdMutPtr: Copy + Sealed {
3939
/// Equivalent to calling [`pointer::addr`] on each element.
4040
fn addr(self) -> Self::Usize;
4141

42+
/// Convert an address to a pointer without giving it any provenance.
43+
///
44+
/// Without provenance, this pointer is not associated with any actual allocation. Such a
45+
/// no-provenance pointer may be used for zero-sized memory accesses (if suitably aligned), but
46+
/// non-zero-sized memory accesses with a no-provenance pointer are UB. No-provenance pointers
47+
/// are little more than a usize address in disguise.
48+
///
49+
/// This is different from [`Self::from_exposed_addr`], which creates a pointer that picks up a
50+
/// previously exposed provenance.
51+
///
52+
/// Equivalent to calling [`core::ptr::without_provenance`] on each element.
53+
fn without_provenance(addr: Self::Usize) -> Self;
54+
4255
/// Creates a new pointer with the given address.
4356
///
4457
/// This performs the same operation as a cast, but copies the *address-space* and
@@ -115,6 +128,14 @@ where
115128
unsafe { core::mem::transmute_copy(&self) }
116129
}
117130

131+
#[inline]
132+
fn without_provenance(addr: Self::Usize) -> Self {
133+
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
134+
// SAFETY: Integer-to-pointer transmutes are valid (if you are okay with not getting any
135+
// provenance).
136+
unsafe { core::mem::transmute_copy(&addr) }
137+
}
138+
118139
#[inline]
119140
fn with_addr(self, addr: Self::Usize) -> Self {
120141
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.

0 commit comments

Comments
 (0)