Skip to content

Commit 3eaf765

Browse files
authored
Rollup merge of rust-lang#122935 - RalfJung:with-exposed-provenance, r=Amanieu
rename ptr::from_exposed_addr -> ptr::with_exposed_provenance As discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/To.20expose.20or.20not.20to.20expose/near/427757066). The old name, `from_exposed_addr`, makes little sense as it's not the address that is exposed, it's the provenance. (`ptr.expose_addr()` stays unchanged as we haven't found a better option yet. The intended interpretation is "expose the provenance and return the address".) The new name nicely matches `ptr::without_provenance`.
2 parents 15795ee + 3eda213 commit 3eaf765

File tree

13 files changed

+56
-49
lines changed

13 files changed

+56
-49
lines changed

core/src/intrinsics/simd.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ extern "rust-intrinsic" {
549549
///
550550
/// `U` must be a vector of pointers, with the same length as `T`.
551551
#[rustc_nounwind]
552+
#[cfg(not(bootstrap))]
553+
pub fn simd_with_exposed_provenance<T, U>(addr: T) -> U;
554+
#[rustc_nounwind]
555+
#[cfg(bootstrap)]
552556
pub fn simd_from_exposed_addr<T, U>(addr: T) -> U;
553557

554558
/// Swap bytes of each element.
@@ -655,3 +659,6 @@ extern "rust-intrinsic" {
655659
#[rustc_nounwind]
656660
pub fn simd_flog<T>(a: T) -> T;
657661
}
662+
663+
#[cfg(bootstrap)]
664+
pub use simd_from_exposed_addr as simd_with_exposed_provenance;

core/src/ptr/const_ptr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<T: ?Sized> *const T {
165165
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
166166
#[deprecated(
167167
since = "1.67.0",
168-
note = "replaced by the `ptr::from_exposed_addr` function, or update \
168+
note = "replaced by the `ptr::with_exposed_provenance` function, or update \
169169
your code to follow the strict provenance rules using its APIs"
170170
)]
171171
#[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function
@@ -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 [`from_exposed_addr`][from_exposed_addr]
190+
/// or [`expose_addr`][pointer::expose_addr] 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
///
@@ -211,30 +211,30 @@ impl<T: ?Sized> *const T {
211211
}
212212

213213
/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
214-
/// use in [`from_exposed_addr`][].
214+
/// use in [`with_exposed_provenance`][].
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
218218
/// side-effect of marking the provenance as 'exposed', so on platforms that support it you can
219-
/// later call [`from_exposed_addr`][] to reconstitute the original pointer including its
219+
/// later call [`with_exposed_provenance`][] to reconstitute the original pointer including its
220220
/// provenance. (Reconstructing address space information, if required, is your responsibility.)
221221
///
222222
/// Using this method means that code is *not* following [Strict
223223
/// Provenance][super#strict-provenance] rules. Supporting
224-
/// [`from_exposed_addr`][] complicates specification and reasoning and may not be supported by
224+
/// [`with_exposed_provenance`][] complicates specification and reasoning and may not be supported by
225225
/// tools that help you to stay conformant with the Rust memory model, so it is recommended to
226226
/// use [`addr`][pointer::addr] wherever possible.
227227
///
228228
/// On most platforms this will produce a value with the same bytes as the original pointer,
229229
/// because all the bytes are dedicated to describing the address. Platforms which need to store
230230
/// additional information in the pointer may not support this operation, since the 'expose'
231-
/// side-effect which is required for [`from_exposed_addr`][] to work is typically not
231+
/// side-effect which is required for [`with_exposed_provenance`][] to work is typically not
232232
/// available.
233233
///
234234
/// It is unclear whether this method can be given a satisfying unambiguous specification. This
235235
/// API and its claimed semantics are part of [Exposed Provenance][super#exposed-provenance].
236236
///
237-
/// [`from_exposed_addr`]: from_exposed_addr
237+
/// [`with_exposed_provenance`]: with_exposed_provenance
238238
#[must_use]
239239
#[inline(always)]
240240
#[unstable(feature = "exposed_provenance", issue = "95228")]

core/src/ptr/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,13 @@
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 [`from_exposed_addr`] methods, which
343+
//! Exposed Provenance is provided by the [`expose_addr`] and [`with_exposed_provenance`] methods, which
344344
//! are meant to replace `as` casts between pointers and integers. [`expose_addr`] 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
347-
//! is not materialized in actual executions, except in tools like [Miri].) [`from_exposed_addr`]
347+
//! is not materialized in actual executions, except in tools like [Miri].) [`with_exposed_provenance`]
348348
//! can be used to construct a pointer with one of these previously 'exposed' provenances.
349-
//! [`from_exposed_addr`] takes only `addr: usize` as arguments, so unlike in [`with_addr`] there is
349+
//! [`with_exposed_provenance`] takes only `addr: usize` as arguments, so unlike in [`with_addr`] there is
350350
//! no indication of what the correct provenance for the returned pointer is -- and that is exactly
351351
//! what makes pointer-usize-pointer roundtrips so tricky to rigorously specify! There is no
352352
//! algorithm that decides which provenance will be used. You can think of this as "guessing" the
@@ -355,10 +355,10 @@
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 [`from_exposed_addr`] (or the `as` casts) means that code is
358+
//! Using [`expose_addr`] 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
360360
//! determine how far one can get in Rust without the use of [`expose_addr`] and
361-
//! [`from_exposed_addr`], and to encourage code to be written with Strict Provenance APIs only.
361+
//! [`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
364364
//! confidence in (unsafe) Rust code.
@@ -375,7 +375,7 @@
375375
//! [`addr`]: pointer::addr
376376
//! [`ptr::dangling`]: core::ptr::dangling
377377
//! [`expose_addr`]: pointer::expose_addr
378-
//! [`from_exposed_addr`]: from_exposed_addr
378+
//! [`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/
381381
//! [Strict Provenance]: https://github.com/rust-lang/rust/issues/95228
@@ -581,7 +581,7 @@ pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
581581
/// little more than a usize address in disguise.
582582
///
583583
/// This is different from `addr as *const T`, which creates a pointer that picks up a previously
584-
/// exposed provenance. See [`from_exposed_addr`] for more details on that operation.
584+
/// exposed provenance. See [`with_exposed_provenance`] for more details on that operation.
585585
///
586586
/// This API and its claimed semantics are part of the Strict Provenance experiment,
587587
/// see the [module documentation][crate::ptr] for details.
@@ -592,7 +592,7 @@ pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
592592
pub const fn without_provenance<T>(addr: usize) -> *const T {
593593
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
594594
// We use transmute rather than a cast so tools like Miri can tell that this
595-
// is *not* the same as from_exposed_addr.
595+
// is *not* the same as with_exposed_provenance.
596596
// SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
597597
// pointer).
598598
unsafe { mem::transmute(addr) }
@@ -625,7 +625,7 @@ pub const fn dangling<T>() -> *const T {
625625
/// little more than a usize address in disguise.
626626
///
627627
/// This is different from `addr as *mut T`, which creates a pointer that picks up a previously
628-
/// exposed provenance. See [`from_exposed_addr_mut`] for more details on that operation.
628+
/// exposed provenance. See [`with_exposed_provenance_mut`] for more details on that operation.
629629
///
630630
/// This API and its claimed semantics are part of the Strict Provenance experiment,
631631
/// see the [module documentation][crate::ptr] for details.
@@ -636,7 +636,7 @@ pub const fn dangling<T>() -> *const T {
636636
pub const fn without_provenance_mut<T>(addr: usize) -> *mut T {
637637
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
638638
// We use transmute rather than a cast so tools like Miri can tell that this
639-
// is *not* the same as from_exposed_addr.
639+
// is *not* the same as with_exposed_provenance.
640640
// SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
641641
// pointer).
642642
unsafe { mem::transmute(addr) }
@@ -699,7 +699,7 @@ pub const fn dangling_mut<T>() -> *mut T {
699699
#[unstable(feature = "exposed_provenance", issue = "95228")]
700700
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
701701
#[allow(fuzzy_provenance_casts)] // this *is* the explicit provenance API one should use instead
702-
pub fn from_exposed_addr<T>(addr: usize) -> *const T
702+
pub fn with_exposed_provenance<T>(addr: usize) -> *const T
703703
where
704704
T: Sized,
705705
{
@@ -739,7 +739,7 @@ where
739739
#[unstable(feature = "exposed_provenance", issue = "95228")]
740740
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
741741
#[allow(fuzzy_provenance_casts)] // this *is* the explicit provenance API one should use instead
742-
pub fn from_exposed_addr_mut<T>(addr: usize) -> *mut T
742+
pub fn with_exposed_provenance_mut<T>(addr: usize) -> *mut T
743743
where
744744
T: Sized,
745745
{

core/src/ptr/mut_ptr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<T: ?Sized> *mut T {
171171
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
172172
#[deprecated(
173173
since = "1.67.0",
174-
note = "replaced by the `ptr::from_exposed_addr_mut` function, or \
174+
note = "replaced by the `ptr::with_exposed_provenance_mut` function, or \
175175
update your code to follow the strict provenance rules using its APIs"
176176
)]
177177
#[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function
@@ -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 [`from_exposed_addr`][from_exposed_addr]
197+
/// or [`expose_addr`][pointer::expose_addr] 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
///
@@ -218,30 +218,30 @@ impl<T: ?Sized> *mut T {
218218
}
219219

220220
/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
221-
/// use in [`from_exposed_addr`][].
221+
/// use in [`with_exposed_provenance`][].
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
225225
/// side-effect of marking the provenance as 'exposed', so on platforms that support it you can
226-
/// later call [`from_exposed_addr_mut`][] to reconstitute the original pointer including its
226+
/// later call [`with_exposed_provenance_mut`][] to reconstitute the original pointer including its
227227
/// provenance. (Reconstructing address space information, if required, is your responsibility.)
228228
///
229229
/// Using this method means that code is *not* following [Strict
230230
/// Provenance][super#strict-provenance] rules. Supporting
231-
/// [`from_exposed_addr_mut`][] complicates specification and reasoning and may not be supported
231+
/// [`with_exposed_provenance_mut`][] complicates specification and reasoning and may not be supported
232232
/// by tools that help you to stay conformant with the Rust memory model, so it is recommended
233233
/// to use [`addr`][pointer::addr] wherever possible.
234234
///
235235
/// On most platforms this will produce a value with the same bytes as the original pointer,
236236
/// because all the bytes are dedicated to describing the address. Platforms which need to store
237237
/// additional information in the pointer may not support this operation, since the 'expose'
238-
/// side-effect which is required for [`from_exposed_addr_mut`][] to work is typically not
238+
/// side-effect which is required for [`with_exposed_provenance_mut`][] to work is typically not
239239
/// available.
240240
///
241241
/// It is unclear whether this method can be given a satisfying unambiguous specification. This
242242
/// API and its claimed semantics are part of [Exposed Provenance][super#exposed-provenance].
243243
///
244-
/// [`from_exposed_addr_mut`]: from_exposed_addr_mut
244+
/// [`with_exposed_provenance_mut`]: with_exposed_provenance_mut
245245
#[must_use]
246246
#[inline(always)]
247247
#[unstable(feature = "exposed_provenance", issue = "95228")]

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ pub trait SimdConstPtr: Copy + Sealed {
5151
fn with_addr(self, addr: Self::Usize) -> Self;
5252

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

5757
/// Convert an address back to a pointer, picking up a previously "exposed" provenance.
5858
///
59-
/// Equivalent to calling [`core::ptr::from_exposed_addr`] on each element.
60-
fn from_exposed_addr(addr: Self::Usize) -> Self;
59+
/// Equivalent to calling [`core::ptr::with_exposed_provenance`] on each element.
60+
fn with_exposed_provenance(addr: Self::Usize) -> Self;
6161

6262
/// Calculates the offset from a pointer using wrapping arithmetic.
6363
///
@@ -137,9 +137,9 @@ where
137137
}
138138

139139
#[inline]
140-
fn from_exposed_addr(addr: Self::Usize) -> Self {
140+
fn with_exposed_provenance(addr: Self::Usize) -> Self {
141141
// Safety: `self` is a pointer vector
142-
unsafe { core::intrinsics::simd::simd_from_exposed_addr(addr) }
142+
unsafe { core::intrinsics::simd::simd_with_exposed_provenance(addr) }
143143
}
144144

145145
#[inline]

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ pub trait SimdMutPtr: Copy + Sealed {
4848
fn with_addr(self, addr: Self::Usize) -> Self;
4949

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

5454
/// Convert an address back to a pointer, picking up a previously "exposed" provenance.
5555
///
56-
/// Equivalent to calling [`core::ptr::from_exposed_addr_mut`] on each element.
57-
fn from_exposed_addr(addr: Self::Usize) -> Self;
56+
/// Equivalent to calling [`core::ptr::with_exposed_provenance_mut`] on each element.
57+
fn with_exposed_provenance(addr: Self::Usize) -> Self;
5858

5959
/// Calculates the offset from a pointer using wrapping arithmetic.
6060
///
@@ -134,9 +134,9 @@ where
134134
}
135135

136136
#[inline]
137-
fn from_exposed_addr(addr: Self::Usize) -> Self {
137+
fn with_exposed_provenance(addr: Self::Usize) -> Self {
138138
// Safety: `self` is a pointer vector
139-
unsafe { core::intrinsics::simd::simd_from_exposed_addr(addr) }
139+
unsafe { core::intrinsics::simd::simd_with_exposed_provenance(addr) }
140140
}
141141

142142
#[inline]

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ mod const_ptr {
8080
);
8181
}
8282

83-
fn from_exposed_addr<const LANES: usize>() {
83+
fn with_exposed_provenance<const LANES: usize>() {
8484
test_helpers::test_unary_elementwise(
85-
&Simd::<*const u32, LANES>::from_exposed_addr,
86-
&core::ptr::from_exposed_addr::<u32>,
85+
&Simd::<*const u32, LANES>::with_exposed_provenance,
86+
&core::ptr::with_exposed_provenance::<u32>,
8787
&|_| true,
8888
);
8989
}
@@ -103,10 +103,10 @@ mod mut_ptr {
103103
);
104104
}
105105

106-
fn from_exposed_addr<const LANES: usize>() {
106+
fn with_exposed_provenance<const LANES: usize>() {
107107
test_helpers::test_unary_elementwise(
108-
&Simd::<*mut u32, LANES>::from_exposed_addr,
109-
&core::ptr::from_exposed_addr_mut::<u32>,
108+
&Simd::<*mut u32, LANES>::with_exposed_provenance,
109+
&core::ptr::with_exposed_provenance_mut::<u32>,
110110
&|_| true,
111111
);
112112
}

std/src/os/xous/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ pub(crate) unsafe fn map_memory<T>(
389389
let result = a0;
390390

391391
if result == SyscallResult::MemoryRange as usize {
392-
let start = core::ptr::from_exposed_addr_mut::<T>(a1);
392+
let start = core::ptr::with_exposed_provenance_mut::<T>(a1);
393393
let len = a2 / core::mem::size_of::<T>();
394394
let end = unsafe { start.add(len) };
395395
Ok(unsafe { core::slice::from_raw_parts_mut(start, len) })

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Thread {
4747
extern "C" fn thread_start(main: usize) {
4848
unsafe {
4949
// Finally, let's run some code.
50-
Box::from_raw(ptr::from_exposed_addr::<Box<dyn FnOnce()>>(main).cast_mut())();
50+
Box::from_raw(ptr::with_exposed_provenance::<Box<dyn FnOnce()>>(main).cast_mut())();
5151

5252
// run all destructors
5353
run_dtors();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Thread {
9898
});
9999

100100
unsafe extern "C" fn trampoline(exinf: isize) {
101-
let p_inner: *mut ThreadInner = crate::ptr::from_exposed_addr_mut(exinf as usize);
101+
let p_inner: *mut ThreadInner = crate::ptr::with_exposed_provenance_mut(exinf as usize);
102102
// Safety: `ThreadInner` is alive at this point
103103
let inner = unsafe { &*p_inner };
104104

std/src/sys/pal/xous/thread_local_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn tls_ptr_addr() -> *mut *mut u8 {
4949
out(reg) tp,
5050
);
5151
}
52-
core::ptr::from_exposed_addr_mut::<*mut u8>(tp)
52+
core::ptr::with_exposed_provenance_mut::<*mut u8>(tp)
5353
}
5454

5555
/// Create an area of memory that's unique per thread. This area will

std/src/sys/pal/zkvm/thread_local_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ pub unsafe fn create(_dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key {
99

1010
#[inline]
1111
pub unsafe fn set(key: Key, value: *mut u8) {
12-
let key: *mut *mut u8 = core::ptr::from_exposed_addr_mut(key);
12+
let key: *mut *mut u8 = core::ptr::with_exposed_provenance_mut(key);
1313
*key = value;
1414
}
1515

1616
#[inline]
1717
pub unsafe fn get(key: Key) -> *mut u8 {
18-
let key: *mut *mut u8 = core::ptr::from_exposed_addr_mut(key);
18+
let key: *mut *mut u8 = core::ptr::with_exposed_provenance_mut(key);
1919
*key
2020
}
2121

std/src/sys/personality/dwarf/eh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result
125125
// Can never have null landing pad for sjlj -- that would have
126126
// been indicated by a -1 call site index.
127127
// FIXME(strict provenance)
128-
let lpad = ptr::from_exposed_addr((cs_lpad + 1) as usize);
128+
let lpad = ptr::with_exposed_provenance((cs_lpad + 1) as usize);
129129
return Ok(interpret_cs_action(action_table, cs_action_entry, lpad));
130130
}
131131
}

0 commit comments

Comments
 (0)