Skip to content

Commit 4a85212

Browse files
committed
do not use int2ptr casts in strict provenance tests
1 parent 5a1b09e commit 4a85212

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// compile-flags: -Zmiri-strict-provenance
22
// error-pattern: not a valid pointer
3+
#![feature(strict_provenance)]
34

45
fn main() {
56
let x = 22;
67
let ptr = &x as *const _ as *const u8;
7-
let roundtrip = ptr as usize as *const u8;
8+
let roundtrip = std::ptr::invalid::<u8>(ptr as usize);
9+
// Not even offsetting this is allowed.
810
let _ = unsafe { roundtrip.offset(1) };
911
}

tests/fail/provenance/strict-provenance-offset.stderr renamed to tests/fail/provenance/ptr_invalid_offset.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | unsafe { intrinsics::offset(self, count) }
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99

1010
= note: inside `std::ptr::const_ptr::<impl *const u8>::offset` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11-
note: inside `main` at $DIR/strict-provenance-offset.rs:LL:CC
12-
--> $DIR/strict-provenance-offset.rs:LL:CC
11+
note: inside `main` at $DIR/ptr_invalid_offset.rs:LL:CC
12+
--> $DIR/ptr_invalid_offset.rs:LL:CC
1313
|
1414
LL | let _ = unsafe { roundtrip.offset(1) };
1515
| ^^^^^^^^^^^^^^^^^^^

tests/pass/slices.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#![feature(slice_as_chunks)]
44
#![feature(slice_partition_dedup)]
55
#![feature(layout_for_ptr)]
6+
#![feature(strict_provenance)]
67

78
use std::slice;
9+
use std::ptr;
810

911
fn slice_of_zst() {
1012
fn foo<T>(v: &[T]) -> Option<&[T]> {
@@ -25,7 +27,7 @@ fn slice_of_zst() {
2527

2628
// In a slice of zero-size elements the pointer is meaningless.
2729
// Ensure iteration still works even if the pointer is at the end of the address space.
28-
let slice: &[()] = unsafe { slice::from_raw_parts(-5isize as *const (), 10) };
30+
let slice: &[()] = unsafe { slice::from_raw_parts(ptr::invalid(-5isize as usize), 10) };
2931
assert_eq!(slice.len(), 10);
3032
assert_eq!(slice.iter().count(), 10);
3133

@@ -38,7 +40,7 @@ fn slice_of_zst() {
3840
assert!(foo(slice).is_some());
3941

4042
// Test mutable iterators as well
41-
let slice: &mut [()] = unsafe { slice::from_raw_parts_mut(-5isize as *mut (), 10) };
43+
let slice: &mut [()] = unsafe { slice::from_raw_parts_mut(ptr::invalid_mut(-5isize as usize), 10) };
4244
assert_eq!(slice.len(), 10);
4345
assert_eq!(slice.iter_mut().count(), 10);
4446

@@ -254,7 +256,7 @@ fn test_for_invalidated_pointers() {
254256
fn large_raw_slice() {
255257
let size = isize::MAX as usize;
256258
// Creating a raw slice of size isize::MAX and asking for its size is okay.
257-
let s = std::ptr::slice_from_raw_parts(1usize as *const u8, size);
259+
let s = std::ptr::slice_from_raw_parts(ptr::invalid::<u8>(1), size);
258260
assert_eq!(size, unsafe { std::mem::size_of_val_raw(s) });
259261
}
260262

0 commit comments

Comments
 (0)