Skip to content

Commit 00909fe

Browse files
authored
Rollup merge of #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/rust#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 #122935 here.
2 parents 8f012a3 + 34c6202 commit 00909fe

File tree

8 files changed

+16
-16
lines changed

8 files changed

+16
-16
lines changed

src/alloc_addresses/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use reuse_pool::ReusePool;
1818

1919
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2020
pub enum ProvenanceMode {
21-
/// We support `expose_addr`/`with_exposed_provenance` via "wildcard" provenance.
22-
/// However, we want on `with_exposed_provenance` to alert the user of the precision loss.
21+
/// We support `expose_provenance`/`with_exposed_provenance` via "wildcard" provenance.
22+
/// However, we warn on `with_exposed_provenance` to alert the user of the precision loss.
2323
Default,
2424
/// Like `Default`, but without the warning.
2525
Permissive,

src/shims/intrinsics/simd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
514514
dest.transmute(this.machine.layouts.uint(dest.layout.size).unwrap(), this)?;
515515
this.write_int(res, &dest)?;
516516
}
517-
"cast" | "as" | "cast_ptr" | "expose_addr" | "with_exposed_provenance" => {
517+
"cast" | "as" | "cast_ptr" | "expose_provenance" | "with_exposed_provenance" => {
518518
let [op] = check_arg_count(args)?;
519519
let (op, op_len) = this.operand_to_simd(op)?;
520520
let (dest, dest_len) = this.mplace_to_simd(dest)?;
@@ -524,7 +524,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
524524
let unsafe_cast = intrinsic_name == "cast";
525525
let safe_cast = intrinsic_name == "as";
526526
let ptr_cast = intrinsic_name == "cast_ptr";
527-
let expose_cast = intrinsic_name == "expose_addr";
527+
let expose_cast = intrinsic_name == "expose_provenance";
528528
let from_exposed_cast = intrinsic_name == "with_exposed_provenance";
529529

530530
for i in 0..dest_len {
@@ -557,7 +557,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
557557
this.ptr_to_ptr(&op, dest.layout)?,
558558
// Ptr/Int casts
559559
(ty::RawPtr(..), ty::Int(_) | ty::Uint(_)) if expose_cast =>
560-
this.pointer_expose_address_cast(&op, dest.layout)?,
560+
this.pointer_expose_provenance_cast(&op, dest.layout)?,
561561
(ty::Int(_) | ty::Uint(_), ty::RawPtr(..)) if from_exposed_cast =>
562562
this.pointer_with_exposed_provenance_cast(&op, dest.layout)?,
563563
// Error otherwise

tests/fail/provenance/ptr_invalid.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
fn main() {
55
let x = 42;
66
let xptr = &x as *const i32;
7-
let xptr_invalid = std::ptr::without_provenance::<i32>(xptr.expose_addr());
7+
let xptr_invalid = std::ptr::without_provenance::<i32>(xptr.expose_provenance());
88
let _val = unsafe { *xptr_invalid }; //~ ERROR: is a dangling pointer
99
}

tests/fail/stacked_borrows/exposed_only_ro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
fn main() {
77
let mut x = 0;
88
let _fool = &mut x as *mut i32; // this would have fooled the old untagged pointer logic
9-
let addr = (&x as *const i32).expose_addr();
9+
let addr = (&x as *const i32).expose_provenance();
1010
let ptr = std::ptr::with_exposed_provenance_mut::<i32>(addr);
1111
unsafe { *ptr = 0 }; //~ ERROR: /write access using <wildcard> .* no exposed tags have suitable permission in the borrow stack/
1212
}

tests/pass/portable-simd-ptrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ use std::simd::prelude::*;
77
fn main() {
88
// Pointer casts
99
let _val: Simd<*const u8, 4> = Simd::<*const i32, 4>::splat(ptr::null()).cast();
10-
let addrs = Simd::<*const i32, 4>::splat(ptr::null()).expose_addr();
10+
let addrs = Simd::<*const i32, 4>::splat(ptr::null()).expose_provenance();
1111
let _ptrs = Simd::<*const i32, 4>::with_exposed_provenance(addrs);
1212
}

tests/pass/ptr_int_from_exposed.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn ptr_roundtrip_out_of_bounds() {
1010
let x: i32 = 3;
1111
let x_ptr = &x as *const i32;
1212

13-
let x_usize = x_ptr.wrapping_offset(128).expose_addr();
13+
let x_usize = x_ptr.wrapping_offset(128).expose_provenance();
1414

1515
let ptr = ptr::with_exposed_provenance::<i32>(x_usize).wrapping_offset(-128);
1616
assert_eq!(unsafe { *ptr }, 3);
@@ -24,8 +24,8 @@ fn ptr_roundtrip_confusion() {
2424
let x_ptr = &x as *const i32;
2525
let y_ptr = &y as *const i32;
2626

27-
let x_usize = x_ptr.expose_addr();
28-
let y_usize = y_ptr.expose_addr();
27+
let x_usize = x_ptr.expose_provenance();
28+
let y_usize = y_ptr.expose_provenance();
2929

3030
let ptr = ptr::with_exposed_provenance::<i32>(y_usize);
3131
let ptr = ptr.with_addr(x_usize);
@@ -37,7 +37,7 @@ fn ptr_roundtrip_imperfect() {
3737
let x: u8 = 3;
3838
let x_ptr = &x as *const u8;
3939

40-
let x_usize = x_ptr.expose_addr() + 128;
40+
let x_usize = x_ptr.expose_provenance() + 128;
4141

4242
let ptr = ptr::with_exposed_provenance::<u8>(x_usize).wrapping_offset(-128);
4343
assert_eq!(unsafe { *ptr }, 3);
@@ -48,7 +48,7 @@ fn ptr_roundtrip_null() {
4848
let x = &42;
4949
let x_ptr = x as *const i32;
5050
let x_null_ptr = x_ptr.with_addr(0); // addr 0, but still the provenance of x
51-
let null = x_null_ptr.expose_addr();
51+
let null = x_null_ptr.expose_provenance();
5252
assert_eq!(null, 0);
5353

5454
let x_null_ptr_copy = ptr::with_exposed_provenance::<i32>(null); // just a roundtrip, so has provenance of x (angelically)

tests/pass/stacked-borrows/int-to-ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn example(variant: bool) {
1717
unsafe {
1818
fn not_so_innocent(x: &mut u32) -> usize {
1919
let x_raw4 = x as *mut u32;
20-
x_raw4.expose_addr()
20+
x_raw4.expose_provenance()
2121
}
2222

2323
let mut c = 42u32;
@@ -26,7 +26,7 @@ fn example(variant: bool) {
2626
// stack: [..., Unique(1)]
2727

2828
let x_raw2 = x_unique1 as *mut u32;
29-
let x_raw2_addr = x_raw2.expose_addr();
29+
let x_raw2_addr = x_raw2.expose_provenance();
3030
// stack: [..., Unique(1), SharedRW(2)]
3131

3232
let x_unique3 = &mut *x_raw2;

tests/pass/stacked-borrows/unknown-bottom-gc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99

1010
// Expose the allocation and use the exposed pointer, creating an unknown bottom
1111
unsafe {
12-
let p: *mut u8 = ptr::with_exposed_provenance::<u8>(ptr.expose_addr()) as *mut u8;
12+
let p: *mut u8 = ptr::with_exposed_provenance::<u8>(ptr.expose_provenance()) as *mut u8;
1313
*p = 1;
1414
}
1515

0 commit comments

Comments
 (0)