Skip to content

Commit

Permalink
Auto merge of rust-lang#2193 - RalfJung:strict, r=RalfJung
Browse files Browse the repository at this point in the history
do not use int2ptr casts in strict provenance tests
  • Loading branch information
bors committed Jun 5, 2022
2 parents 5f988ab + 4a85212 commit d312b34
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// compile-flags: -Zmiri-strict-provenance
// error-pattern: not a valid pointer
#![feature(strict_provenance)]

fn main() {
let x = 22;
let ptr = &x as *const _ as *const u8;
let roundtrip = ptr as usize as *const u8;
let roundtrip = std::ptr::invalid::<u8>(ptr as usize);
// Not even offsetting this is allowed.
let _ = unsafe { roundtrip.offset(1) };
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ LL | unsafe { intrinsics::offset(self, count) }
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

= note: inside `std::ptr::const_ptr::<impl *const u8>::offset` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
note: inside `main` at $DIR/strict-provenance-offset.rs:LL:CC
--> $DIR/strict-provenance-offset.rs:LL:CC
note: inside `main` at $DIR/ptr_invalid_offset.rs:LL:CC
--> $DIR/ptr_invalid_offset.rs:LL:CC
|
LL | let _ = unsafe { roundtrip.offset(1) };
| ^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 5 additions & 3 deletions tests/pass/slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#![feature(slice_as_chunks)]
#![feature(slice_partition_dedup)]
#![feature(layout_for_ptr)]
#![feature(strict_provenance)]

use std::slice;
use std::ptr;

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

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

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

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

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

Expand Down

0 comments on commit d312b34

Please sign in to comment.