Skip to content

Commit 0741fde

Browse files
authored
Rollup merge of rust-lang#122964 - joboet:pointer_expose, r=Amanieu
Rename `expose_addr` to `expose_provenance` `expose_addr` is a bad name, an address is just a number and cannot be exposed. The operation is actually about the provenance of the pointer. This PR thus changes the name of the method to `expose_provenance` without changing its return type. There is sufficient precedence for returning a useful value from an operation that does something else without the name indicating such, e.g. [`Option::insert`](https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.insert) and [`MaybeUninit::write`](https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write). Returning the address is merely convenient, not a fundamental part of the operation. This is implied by the fact that integers do not have provenance since ```rust let addr = ptr.addr(); ptr.expose_provenance(); let new = ptr::with_exposed_provenance(addr); ``` must behave exactly like ```rust let addr = ptr.expose_provenance(); let new = ptr::with_exposed_provenance(addr); ``` as the result of `ptr.expose_provenance()` and `ptr.addr()` is the same integer. Therefore, this PR removes the `#[must_use]` annotation on the function and updates the documentation to reflect the important part. ~~An alternative name would be `expose_provenance`. I'm not at all opposed to that, but it makes a stronger implication than we might want that the provenance of the pointer returned by `ptr::with_exposed_provenance`[^1] is the same as that what was exposed, which is not yet specified as such IIUC. IMHO `expose` does not make that connection.~~ A previous version of this PR suggested `expose` as name, libs-api [decided on](rust-lang#122964 (comment)) `expose_provenance` to keep the symmetry with `with_exposed_provenance`. CC `@RalfJung` r? libs-api [^1]: I'm using the new name for `from_exposed_addr` suggested by rust-lang#122935 here.
2 parents 11c572a + 8c4df2f commit 0741fde

File tree

10 files changed

+40
-35
lines changed

10 files changed

+40
-35
lines changed

core/src/fmt/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2438,8 +2438,8 @@ impl Display for char {
24382438
#[stable(feature = "rust1", since = "1.0.0")]
24392439
impl<T: ?Sized> Pointer for *const T {
24402440
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2441-
// Cast is needed here because `.expose_addr()` requires `T: Sized`.
2442-
pointer_fmt_inner((*self as *const ()).expose_addr(), f)
2441+
// Cast is needed here because `.expose_provenance()` requires `T: Sized`.
2442+
pointer_fmt_inner((*self as *const ()).expose_provenance(), f)
24432443
}
24442444
}
24452445

core/src/intrinsics/simd.rs

+6
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ extern "rust-intrinsic" {
540540
/// `T` must be a vector of pointers.
541541
///
542542
/// `U` must be a vector of `usize` with the same length as `T`.
543+
#[cfg(not(bootstrap))]
544+
#[rustc_nounwind]
545+
pub fn simd_expose_provenance<T, U>(ptr: T) -> U;
546+
#[cfg(bootstrap)]
543547
#[rustc_nounwind]
544548
pub fn simd_expose_addr<T, U>(ptr: T) -> U;
545549

@@ -660,5 +664,7 @@ extern "rust-intrinsic" {
660664
pub fn simd_flog<T>(a: T) -> T;
661665
}
662666

667+
#[cfg(bootstrap)]
668+
pub use simd_expose_addr as simd_expose_provenance;
663669
#[cfg(bootstrap)]
664670
pub use simd_from_exposed_addr as simd_with_exposed_provenance;

core/src/ptr/const_ptr.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<T: ?Sized> *const T {
136136
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
137137
#[deprecated(
138138
since = "1.67.0",
139-
note = "replaced by the `expose_addr` method, or update your code \
139+
note = "replaced by the `expose_provenance` method, or update your code \
140140
to follow the strict provenance rules using its APIs"
141141
)]
142142
#[inline(always)]
@@ -187,7 +187,7 @@ impl<T: ?Sized> *const T {
187187
///
188188
/// If using those APIs is not possible because there is no way to preserve a pointer with the
189189
/// required provenance, then Strict Provenance might not be for you. Use pointer-integer casts
190-
/// or [`expose_addr`][pointer::expose_addr] and [`with_exposed_provenance`][with_exposed_provenance]
190+
/// or [`expose_provenance`][pointer::expose_provenance] and [`with_exposed_provenance`][with_exposed_provenance]
191191
/// instead. However, note that this makes your code less portable and less amenable to tools
192192
/// that check for compliance with the Rust memory model.
193193
///
@@ -210,8 +210,8 @@ impl<T: ?Sized> *const T {
210210
unsafe { mem::transmute(self.cast::<()>()) }
211211
}
212212

213-
/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
214-
/// use in [`with_exposed_provenance`][].
213+
/// Exposes the "provenance" part of the pointer for future use in
214+
/// [`with_exposed_provenance`][] and returns the "address" portion.
215215
///
216216
/// This is equivalent to `self as usize`, which semantically discards *provenance* and
217217
/// *address-space* information. Furthermore, this (like the `as` cast) has the implicit
@@ -238,7 +238,7 @@ impl<T: ?Sized> *const T {
238238
#[must_use]
239239
#[inline(always)]
240240
#[unstable(feature = "exposed_provenance", issue = "95228")]
241-
pub fn expose_addr(self) -> usize {
241+
pub fn expose_provenance(self) -> usize {
242242
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
243243
self.cast::<()>() as usize
244244
}

core/src/ptr/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@
340340
//! clear where a satisfying unambiguous semantics can be defined for Exposed Provenance.
341341
//! Furthermore, Exposed Provenance will not work (well) with tools like [Miri] and [CHERI].
342342
//!
343-
//! Exposed Provenance is provided by the [`expose_addr`] and [`with_exposed_provenance`] methods, which
344-
//! are meant to replace `as` casts between pointers and integers. [`expose_addr`] is a lot like
343+
//! Exposed Provenance is provided by the [`expose_provenance`] and [`with_exposed_provenance`] methods,
344+
//! which are meant to replace `as` casts between pointers and integers. [`expose_provenance`] is a lot like
345345
//! [`addr`], but additionally adds the provenance of the pointer to a global list of 'exposed'
346346
//! provenances. (This list is purely conceptual, it exists for the purpose of specifying Rust but
347347
//! is not materialized in actual executions, except in tools like [Miri].) [`with_exposed_provenance`]
@@ -355,9 +355,9 @@
355355
//! there is *no* previously 'exposed' provenance that justifies the way the returned pointer will
356356
//! be used, the program has undefined behavior.
357357
//!
358-
//! Using [`expose_addr`] or [`with_exposed_provenance`] (or the `as` casts) means that code is
358+
//! Using [`expose_provenance`] or [`with_exposed_provenance`] (or the `as` casts) means that code is
359359
//! *not* following Strict Provenance rules. The goal of the Strict Provenance experiment is to
360-
//! determine how far one can get in Rust without the use of [`expose_addr`] and
360+
//! determine how far one can get in Rust without the use of [`expose_provenance`] and
361361
//! [`with_exposed_provenance`], and to encourage code to be written with Strict Provenance APIs only.
362362
//! Maximizing the amount of such code is a major win for avoiding specification complexity and to
363363
//! facilitate adoption of tools like [CHERI] and [Miri] that can be a big help in increasing the
@@ -374,7 +374,7 @@
374374
//! [`map_addr`]: pointer::map_addr
375375
//! [`addr`]: pointer::addr
376376
//! [`ptr::dangling`]: core::ptr::dangling
377-
//! [`expose_addr`]: pointer::expose_addr
377+
//! [`expose_provenance`]: pointer::expose_provenance
378378
//! [`with_exposed_provenance`]: with_exposed_provenance
379379
//! [Miri]: https://github.com/rust-lang/miri
380380
//! [CHERI]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
@@ -663,7 +663,7 @@ pub const fn dangling_mut<T>() -> *mut T {
663663
///
664664
/// This is a more rigorously specified alternative to `addr as *const T`. The provenance of the
665665
/// returned pointer is that of *any* pointer that was previously exposed by passing it to
666-
/// [`expose_addr`][pointer::expose_addr], or a `ptr as usize` cast. In addition, memory which is
666+
/// [`expose_provenance`][pointer::expose_provenance], or a `ptr as usize` cast. In addition, memory which is
667667
/// outside the control of the Rust abstract machine (MMIO registers, for example) is always
668668
/// considered to be exposed, so long as this memory is disjoint from memory that will be used by
669669
/// the abstract machine such as the stack, heap, and statics.
@@ -711,7 +711,7 @@ where
711711
///
712712
/// This is a more rigorously specified alternative to `addr as *mut T`. The provenance of the
713713
/// returned pointer is that of *any* pointer that was previously passed to
714-
/// [`expose_addr`][pointer::expose_addr] or a `ptr as usize` cast. If there is no previously
714+
/// [`expose_provenance`][pointer::expose_provenance] or a `ptr as usize` cast. If there is no previously
715715
/// 'exposed' provenance that justifies the way this pointer will be used, the program has undefined
716716
/// behavior. Note that there is no algorithm that decides which provenance will be used. You can
717717
/// think of this as "guessing" the right provenance, and the guess will be "maximally in your

core/src/ptr/mut_ptr.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<T: ?Sized> *mut T {
142142
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
143143
#[deprecated(
144144
since = "1.67.0",
145-
note = "replaced by the `expose_addr` method, or update your code \
145+
note = "replaced by the `expose_provenance` method, or update your code \
146146
to follow the strict provenance rules using its APIs"
147147
)]
148148
#[inline(always)]
@@ -194,7 +194,7 @@ impl<T: ?Sized> *mut T {
194194
///
195195
/// If using those APIs is not possible because there is no way to preserve a pointer with the
196196
/// required provenance, then Strict Provenance might not be for you. Use pointer-integer casts
197-
/// or [`expose_addr`][pointer::expose_addr] and [`with_exposed_provenance`][with_exposed_provenance]
197+
/// or [`expose_provenance`][pointer::expose_provenance] and [`with_exposed_provenance`][with_exposed_provenance]
198198
/// instead. However, note that this makes your code less portable and less amenable to tools
199199
/// that check for compliance with the Rust memory model.
200200
///
@@ -217,8 +217,8 @@ impl<T: ?Sized> *mut T {
217217
unsafe { mem::transmute(self.cast::<()>()) }
218218
}
219219

220-
/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
221-
/// use in [`with_exposed_provenance`][].
220+
/// Exposes the "provenance" part of the pointer for future use in
221+
/// [`with_exposed_provenance`][] and returns the "address" portion.
222222
///
223223
/// This is equivalent to `self as usize`, which semantically discards *provenance* and
224224
/// *address-space* information. Furthermore, this (like the `as` cast) has the implicit
@@ -242,10 +242,9 @@ impl<T: ?Sized> *mut T {
242242
/// API and its claimed semantics are part of [Exposed Provenance][super#exposed-provenance].
243243
///
244244
/// [`with_exposed_provenance_mut`]: with_exposed_provenance_mut
245-
#[must_use]
246245
#[inline(always)]
247246
#[unstable(feature = "exposed_provenance", issue = "95228")]
248-
pub fn expose_addr(self) -> usize {
247+
pub fn expose_provenance(self) -> usize {
249248
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
250249
self.cast::<()>() as usize
251250
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ pub trait SimdConstPtr: Copy + Sealed {
5050
/// Equivalent to calling [`pointer::with_addr`] on each element.
5151
fn with_addr(self, addr: Self::Usize) -> Self;
5252

53-
/// Gets the "address" portion of the pointer, and "exposes" the provenance part for future use
54-
/// in [`Self::with_exposed_provenance`].
55-
fn expose_addr(self) -> Self::Usize;
53+
/// Exposes the "provenance" part of the pointer for future use in
54+
/// [`Self::with_exposed_provenance`] and returns the "address" portion.
55+
fn expose_provenance(self) -> Self::Usize;
5656

5757
/// Convert an address back to a pointer, picking up a previously "exposed" provenance.
5858
///
@@ -131,9 +131,9 @@ where
131131
}
132132

133133
#[inline]
134-
fn expose_addr(self) -> Self::Usize {
134+
fn expose_provenance(self) -> Self::Usize {
135135
// Safety: `self` is a pointer vector
136-
unsafe { core::intrinsics::simd::simd_expose_addr(self) }
136+
unsafe { core::intrinsics::simd::simd_expose_provenance(self) }
137137
}
138138

139139
#[inline]

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub trait SimdMutPtr: Copy + Sealed {
4747
/// Equivalent to calling [`pointer::with_addr`] on each element.
4848
fn with_addr(self, addr: Self::Usize) -> Self;
4949

50-
/// Gets the "address" portion of the pointer, and "exposes" the provenance part for future use
51-
/// in [`Self::with_exposed_provenance`].
52-
fn expose_addr(self) -> Self::Usize;
50+
/// Exposes the "provenance" part of the pointer for future use in
51+
/// [`Self::with_exposed_provenance`] and returns the "address" portion.
52+
fn expose_provenance(self) -> Self::Usize;
5353

5454
/// Convert an address back to a pointer, picking up a previously "exposed" provenance.
5555
///
@@ -128,9 +128,9 @@ where
128128
}
129129

130130
#[inline]
131-
fn expose_addr(self) -> Self::Usize {
131+
fn expose_provenance(self) -> Self::Usize {
132132
// Safety: `self` is a pointer vector
133-
unsafe { core::intrinsics::simd::simd_expose_addr(self) }
133+
unsafe { core::intrinsics::simd::simd_expose_provenance(self) }
134134
}
135135

136136
#[inline]

portable-simd/crates/core_simd/tests/pointers.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ macro_rules! common_tests {
3232
);
3333
}
3434

35-
fn expose_addr<const LANES: usize>() {
35+
fn expose_provenance<const LANES: usize>() {
3636
test_helpers::test_unary_elementwise(
37-
&Simd::<*$constness u32, LANES>::expose_addr,
38-
&<*$constness u32>::expose_addr,
37+
&Simd::<*$constness u32, LANES>::expose_provenance,
38+
&<*$constness u32>::expose_provenance,
3939
&|_| true,
4040
);
4141
}

std/src/sys/pal/hermit/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Thread {
2929
let p = Box::into_raw(Box::new(p));
3030
let tid = abi::spawn2(
3131
thread_start,
32-
p.expose_addr(),
32+
p.expose_provenance(),
3333
abi::Priority::into(abi::NORMAL_PRIO),
3434
stack,
3535
core_id,

std/src/sys/pal/itron/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl Thread {
181181
abi::acre_tsk(&abi::T_CTSK {
182182
// Activate this task immediately
183183
tskatr: abi::TA_ACT,
184-
exinf: p_inner.as_ptr().expose_addr() as abi::EXINF,
184+
exinf: p_inner.as_ptr().expose_provenance() as abi::EXINF,
185185
// The entry point
186186
task: Some(trampoline),
187187
// Inherit the calling task's base priority

0 commit comments

Comments
 (0)