Skip to content

Commit fb82ee0

Browse files
committed
rename ptr::invalid -> ptr::without_provenance
also introduce ptr::dangling matching NonNull::dangling
1 parent 3e52033 commit fb82ee0

11 files changed

+24
-24
lines changed

Diff for: tests/fail/dangling_pointers/deref_dangling_box.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ptr::{self, addr_of_mut};
88
// (This test relies on the `deref_copy` pass that lowers `**ptr` to materialize the intermediate pointer.)
99

1010
fn main() {
11-
let mut inner = ptr::invalid::<i32>(24);
11+
let mut inner = ptr::without_provenance::<i32>(24);
1212
let outer = addr_of_mut!(inner).cast::<Box<i32>>();
1313
// Now `outer` is a pointer to a dangling reference.
1414
// Deref'ing that should be UB.

Diff for: tests/fail/dangling_pointers/deref_dangling_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ptr::{self, addr_of_mut};
88
// (This test relies on the `deref_copy` pass that lowers `**ptr` to materialize the intermediate pointer.)
99

1010
fn main() {
11-
let mut inner = ptr::invalid::<i32>(24);
11+
let mut inner = ptr::without_provenance::<i32>(24);
1212
let outer = addr_of_mut!(inner).cast::<&'static mut i32>();
1313
// Now `outer` is a pointer to a dangling reference.
1414
// Deref'ing that should be UB.

Diff for: tests/fail/provenance/ptr_invalid.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(strict_provenance, exposed_provenance)]
22

3-
// Ensure that a `ptr::invalid` ptr is truly invalid.
3+
// Ensure that a `ptr::without_provenance` ptr is truly invalid.
44
fn main() {
55
let x = 42;
66
let xptr = &x as *const i32;
7-
let xptr_invalid = std::ptr::invalid::<i32>(xptr.expose_addr());
7+
let xptr_invalid = std::ptr::without_provenance::<i32>(xptr.expose_addr());
88
let _val = unsafe { *xptr_invalid }; //~ ERROR: is a dangling pointer
99
}

Diff for: tests/fail/provenance/ptr_invalid_offset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
fn main() {
55
let x = 22;
66
let ptr = &x as *const _ as *const u8;
7-
let roundtrip = std::ptr::invalid::<u8>(ptr as usize);
7+
let roundtrip = std::ptr::without_provenance::<u8>(ptr as usize);
88
// Not even offsetting this is allowed.
99
let _ = unsafe { roundtrip.offset(1) }; //~ERROR: is a dangling pointer
1010
}

Diff for: tests/pass-dep/shims/mmap.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn test_mmap<Offset: Default>(
7171

7272
let ptr = unsafe {
7373
mmap(
74-
ptr::invalid_mut(page_size * 64),
74+
ptr::without_provenance_mut(page_size * 64),
7575
page_size,
7676
libc::PROT_READ | libc::PROT_WRITE,
7777
// We don't support MAP_FIXED
@@ -114,13 +114,13 @@ fn test_mmap<Offset: Default>(
114114
assert_eq!(ptr, libc::MAP_FAILED);
115115

116116
// We report an error when trying to munmap an address which is not a multiple of the page size
117-
let res = unsafe { libc::munmap(ptr::invalid_mut(1), page_size) };
117+
let res = unsafe { libc::munmap(ptr::without_provenance_mut(1), page_size) };
118118
assert_eq!(res, -1);
119119
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
120120

121121
// We report an error when trying to munmap a length that cannot be rounded up to a multiple of
122122
// the page size.
123-
let res = unsafe { libc::munmap(ptr::invalid_mut(page_size), usize::MAX - 1) };
123+
let res = unsafe { libc::munmap(ptr::without_provenance_mut(page_size), usize::MAX - 1) };
124124
assert_eq!(res, -1);
125125
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
126126
}
@@ -156,7 +156,7 @@ fn test_mremap() {
156156
// Test all of our error conditions
157157
// Not aligned
158158
let ptr =
159-
unsafe { libc::mremap(ptr::invalid_mut(1), page_size, page_size, libc::MREMAP_MAYMOVE) };
159+
unsafe { libc::mremap(ptr::without_provenance_mut(1), page_size, page_size, libc::MREMAP_MAYMOVE) };
160160
assert_eq!(ptr, libc::MAP_FAILED);
161161
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
162162

Diff for: tests/pass-dep/shims/posix_memalign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn main() {
5858

5959
// Non-power of 2 align
6060
unsafe {
61-
let mut ptr: *mut libc::c_void = ptr::invalid_mut(0x1234567);
61+
let mut ptr: *mut libc::c_void = ptr::without_provenance_mut(0x1234567);
6262
let align = 15;
6363
let size = 8;
6464
assert_eq!(libc::posix_memalign(&mut ptr, align, size), libc::EINVAL);
@@ -70,7 +70,7 @@ fn main() {
7070

7171
// Too small align (smaller than ptr)
7272
unsafe {
73-
let mut ptr: *mut libc::c_void = ptr::invalid_mut(0x1234567);
73+
let mut ptr: *mut libc::c_void = ptr::without_provenance_mut(0x1234567);
7474
let align = std::mem::size_of::<usize>() / 2;
7575
let size = 8;
7676
assert_eq!(libc::posix_memalign(&mut ptr, align, size), libc::EINVAL);

Diff for: tests/pass/align_offset_symbolic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn huge_align() {
100100
#[cfg(target_pointer_width = "16")]
101101
const SIZE: usize = 1 << 13;
102102
struct HugeSize(#[allow(dead_code)] [u8; SIZE - 1]);
103-
let _ = std::ptr::invalid::<HugeSize>(SIZE).align_offset(SIZE);
103+
let _ = std::ptr::without_provenance::<HugeSize>(SIZE).align_offset(SIZE);
104104
}
105105

106106
// This shows that we cannot store the promised alignment info in `AllocExtra`,

Diff for: tests/pass/atomic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ fn atomic_ptr() {
137137

138138
let ptr = AtomicPtr::<i32>::new(ptr::null_mut());
139139
assert!(ptr.load(Relaxed).addr() == 0);
140-
ptr.store(ptr::invalid_mut(13), SeqCst);
140+
ptr.store(ptr::without_provenance_mut(13), SeqCst);
141141
assert!(ptr.swap(x, Relaxed).addr() == 13);
142142
unsafe { assert!(*ptr.load(Acquire) == 0) };
143143

144144
// comparison ignores provenance
145145
assert_eq!(
146146
ptr.compare_exchange(
147147
(&mut 0 as *mut i32).with_addr(x.addr()),
148-
ptr::invalid_mut(0),
148+
ptr::without_provenance_mut(0),
149149
SeqCst,
150150
SeqCst
151151
)
@@ -156,7 +156,7 @@ fn atomic_ptr() {
156156
assert_eq!(
157157
ptr.compare_exchange(
158158
(&mut 0 as *mut i32).with_addr(x.addr()),
159-
ptr::invalid_mut(0),
159+
ptr::without_provenance_mut(0),
160160
SeqCst,
161161
SeqCst
162162
)

Diff for: tests/pass/ptr_raw.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ fn assign_overlapping() {
3535
fn deref_invalid() {
3636
unsafe {
3737
// `addr_of!(*ptr)` is never UB.
38-
let _val = addr_of!(*ptr::invalid::<i32>(0));
39-
let _val = addr_of!(*ptr::invalid::<i32>(1)); // not aligned
38+
let _val = addr_of!(*ptr::without_provenance::<i32>(0));
39+
let _val = addr_of!(*ptr::without_provenance::<i32>(1)); // not aligned
4040

4141
// Similarly, just mentioning the place is fine.
42-
let _ = *ptr::invalid::<i32>(0);
43-
let _ = *ptr::invalid::<i32>(1);
42+
let _ = *ptr::without_provenance::<i32>(0);
43+
let _ = *ptr::without_provenance::<i32>(1);
4444
}
4545
}
4646

Diff for: tests/pass/slices.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn slice_of_zst() {
2929

3030
// In a slice of zero-size elements the pointer is meaningless.
3131
// Ensure iteration still works even if the pointer is at the end of the address space.
32-
let slice: &[()] = unsafe { slice::from_raw_parts(ptr::invalid(-5isize as usize), 10) };
32+
let slice: &[()] = unsafe { slice::from_raw_parts(ptr::without_provenance(-5isize as usize), 10) };
3333
assert_eq!(slice.len(), 10);
3434
assert_eq!(slice.iter().count(), 10);
3535

@@ -43,7 +43,7 @@ fn slice_of_zst() {
4343

4444
// Test mutable iterators as well
4545
let slice: &mut [()] =
46-
unsafe { slice::from_raw_parts_mut(ptr::invalid_mut(-5isize as usize), 10) };
46+
unsafe { slice::from_raw_parts_mut(ptr::without_provenance_mut(-5isize as usize), 10) };
4747
assert_eq!(slice.len(), 10);
4848
assert_eq!(slice.iter_mut().count(), 10);
4949

@@ -263,7 +263,7 @@ fn test_for_invalidated_pointers() {
263263
fn large_raw_slice() {
264264
let size = isize::MAX as usize;
265265
// Creating a raw slice of size isize::MAX and asking for its size is okay.
266-
let s = std::ptr::slice_from_raw_parts(ptr::invalid::<u8>(1), size);
266+
let s = std::ptr::slice_from_raw_parts(ptr::without_provenance::<u8>(1), size);
267267
assert_eq!(size, unsafe { std::mem::size_of_val_raw(s) });
268268
}
269269

Diff for: tests/pass/underscore_pattern.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn invalid_match() {
3838

3939
fn dangling_let() {
4040
unsafe {
41-
let ptr = ptr::invalid::<bool>(0x40);
41+
let ptr = ptr::without_provenance::<bool>(0x40);
4242
let _ = *ptr;
4343
}
4444
}
@@ -54,7 +54,7 @@ fn invalid_let() {
5454
// Adding a type annotation used to change how MIR is generated, make sure we cover both cases.
5555
fn dangling_let_type_annotation() {
5656
unsafe {
57-
let ptr = ptr::invalid::<bool>(0x40);
57+
let ptr = ptr::without_provenance::<bool>(0x40);
5858
let _: bool = *ptr;
5959
}
6060
}

0 commit comments

Comments
 (0)