Auto merge of #2193 - RalfJung:strict, r=RalfJung

do not use int2ptr casts in strict provenance tests
This commit is contained in:
bors 2022-06-05 15:49:55 +00:00
commit d312b34feb
3 changed files with 10 additions and 6 deletions

View File

@ -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) };
}

View File

@ -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) };
| ^^^^^^^^^^^^^^^^^^^

View File

@ -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]> {
@ -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);
@ -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);
@ -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) });
}