test/ui/consts: Add tests for const ptr offsets

Signed-off-by: Joe Richey <joerichey@google.com>
This commit is contained in:
Joe Richey 2020-05-15 01:28:33 -07:00
parent 9b3dfd8ea9
commit 88a37a2086
3 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,115 @@
// run-pass
#![feature(const_ptr_offset)]
#![feature(const_ptr_offset_from)]
#![feature(ptr_offset_from)]
use std::ptr;
#[repr(C)]
struct Struct {
a: u32,
b: u32,
c: u32,
}
static S: Struct = Struct { a: 0, b: 0, c: 0 };
// For these tests we use offset_from to check that two pointers are equal.
// Rust doesn't currently support comparing pointers in const fn.
static OFFSET_NO_CHANGE: bool = unsafe {
let p1 = &S.b as *const u32;
let p2 = p1.offset(2).offset(-2);
p1.offset_from(p2) == 0
};
static OFFSET_MIDDLE: bool = unsafe {
let p1 = (&S.a as *const u32).offset(1);
let p2 = (&S.c as *const u32).offset(-1);
p1.offset_from(p2) == 0
};
// Pointing to the end of the allocation is OK
static OFFSET_END: bool = unsafe {
let p1 = (&S.a as *const u32).offset(3);
let p2 = (&S.c as *const u32).offset(1);
p1.offset_from(p2) == 0
};
// Casting though a differently sized type is OK
static OFFSET_U8_PTR: bool = unsafe {
let p1 = (&S.a as *const u32 as *const u8).offset(5);
let p2 = (&S.c as *const u32 as *const u8).offset(-3);
p1.offset_from(p2) == 0
};
// Any offset with a ZST does nothing
const OFFSET_ZST: bool = unsafe {
let pz = &() as *const ();
// offset_from can't work with ZSTs, so cast to u8 ptr
let p1 = pz.offset(5) as *const u8;
let p2 = pz.offset(isize::MIN) as *const u8;
p1.offset_from(p2) == 0
};
const OFFSET_ZERO: bool = unsafe {
let p = [0u8; 0].as_ptr();
p.offset(0).offset_from(p) == 0
};
const OFFSET_ONE: bool = unsafe {
let p = &42u32 as *const u32;
p.offset(1).offset_from(p) == 1
};
const OFFSET_DANGLING: bool = unsafe {
let p = ptr::NonNull::<u8>::dangling().as_ptr();
p.offset(0).offset_from(p) == 0
};
const OFFSET_UNALIGNED: bool = unsafe {
let arr = [0u8; 32];
let p1 = arr.as_ptr();
let p2 = (p1.offset(2) as *const u32).offset(1);
(p2 as *const u8).offset_from(p1) == 6
};
const WRAP_OFFSET_NO_CHANGE: bool = unsafe {
let p1 = &42u32 as *const u32;
let p2 = p1.wrapping_offset(1000).wrapping_offset(-1000);
let p3 = p1.wrapping_offset(-1000).wrapping_offset(1000);
(p1.offset_from(p2) == 0) & (p1.offset_from(p3) == 0)
};
const WRAP_ADDRESS_SPACE: bool = unsafe {
let p1 = &42u8 as *const u8;
let p2 = p1.wrapping_offset(isize::MIN).wrapping_offset(isize::MIN);
p1.offset_from(p2) == 0
};
// Wrap on the count*size_of::<T>() calculation.
const WRAP_SIZE_OF: bool = unsafe {
// Make sure that if p1 moves backwards, we are still in range
let arr = [0u32; 2];
let p = &arr[1] as *const u32;
// With wrapping arithmetic, isize::MAX * 4 == -4
let wrapped = p.wrapping_offset(isize::MAX);
let backward = p.wrapping_offset(-1);
wrapped.offset_from(backward) == 0
};
const WRAP_INTEGER_POINTER: bool = unsafe {
let p1 = (0x42 as *const u32).wrapping_offset(4);
let p2 = 0x52 as *const u32;
p1.offset_from(p2) == 0
};
const WRAP_NULL: bool = unsafe {
let p1 = ptr::null::<u32>().wrapping_offset(1);
let p2 = 0x4 as *const u32;
p1.offset_from(p2) == 0
};
fn main() {
assert!(OFFSET_NO_CHANGE);
assert!(OFFSET_MIDDLE);
assert!(OFFSET_END);
assert!(OFFSET_U8_PTR);
assert!(OFFSET_ZST);
assert!(OFFSET_ZERO);
assert!(OFFSET_ONE);
assert!(OFFSET_DANGLING);
assert!(OFFSET_UNALIGNED);
assert!(WRAP_OFFSET_NO_CHANGE);
assert!(WRAP_ADDRESS_SPACE);
assert!(WRAP_SIZE_OF);
assert!(WRAP_INTEGER_POINTER);
assert!(WRAP_NULL);
}

View File

@ -0,0 +1,22 @@
// ignore-tidy-linelength
#![feature(const_ptr_offset)]
use std::ptr;
// normalize-stderr-test "alloc\d+" -> "allocN"
pub const BEFORE_START: *const u8 = unsafe { (&0u8 as *const u8).offset(-1) }; //~NOTE
pub const AFTER_END: *const u8 = unsafe { (&0u8 as *const u8).offset(2) }; //~NOTE
pub const AFTER_ARRAY: *const u8 = unsafe { [0u8; 100].as_ptr().offset(101) }; //~NOTE
pub const OVERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize::MAX) }; //~NOTE
pub const UNDERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize::MIN) }; //~NOTE
pub const OVERFLOW_ADDRESS_SPACE: *const u8 = unsafe { (usize::MAX as *const u8).offset(2) }; //~NOTE
pub const UNDERFLOW_ADDRESS_SPACE: *const u8 = unsafe { (1 as *const u8).offset(-2) }; //~NOTE
pub const ZERO_SIZED_ALLOC: *const u8 = unsafe { [0u8; 0].as_ptr().offset(1) }; //~NOTE
pub const DANGLING: *const u8 = unsafe { ptr::NonNull::<u8>::dangling().as_ptr().offset(4) }; //~NOTE
// Right now, a zero offset from null is UB
pub const NULL_OFFSET_ZERO: *const u8 = unsafe { ptr::null::<u8>().offset(0) }; //~NOTE
fn main() {}

View File

@ -0,0 +1,154 @@
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| overflowing in-bounds pointer arithmetic
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `BEFORE_START` at $DIR/offset_ub.rs:7:46
|
::: $DIR/offset_ub.rs:7:1
|
LL | pub const BEFORE_START: *const u8 = unsafe { (&0u8 as *const u8).offset(-1) };
| ------------------------------------------------------------------------------
|
= note: `#[deny(const_err)]` on by default
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| inbounds test failed: pointer must be in-bounds at offset 2, but is outside bounds of allocN which has size 1
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `AFTER_END` at $DIR/offset_ub.rs:8:43
|
::: $DIR/offset_ub.rs:8:1
|
LL | pub const AFTER_END: *const u8 = unsafe { (&0u8 as *const u8).offset(2) };
| --------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| inbounds test failed: pointer must be in-bounds at offset 101, but is outside bounds of allocN which has size 100
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `AFTER_ARRAY` at $DIR/offset_ub.rs:9:45
|
::: $DIR/offset_ub.rs:9:1
|
LL | pub const AFTER_ARRAY: *const u8 = unsafe { [0u8; 100].as_ptr().offset(101) };
| ------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| inbounds pointer arithmetic: overflow computing offset
| inside `std::ptr::const_ptr::<impl *const u16>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `OVERFLOW` at $DIR/offset_ub.rs:11:43
|
::: $DIR/offset_ub.rs:11:1
|
LL | pub const OVERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize::MAX) };
| ----------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| inbounds pointer arithmetic: overflow computing offset
| inside `std::ptr::const_ptr::<impl *const u16>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `UNDERFLOW` at $DIR/offset_ub.rs:12:44
|
::: $DIR/offset_ub.rs:12:1
|
LL | pub const UNDERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize::MIN) };
| -----------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| overflowing in-bounds pointer arithmetic
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `OVERFLOW_ADDRESS_SPACE` at $DIR/offset_ub.rs:13:56
|
::: $DIR/offset_ub.rs:13:1
|
LL | pub const OVERFLOW_ADDRESS_SPACE: *const u8 = unsafe { (usize::MAX as *const u8).offset(2) };
| ---------------------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| overflowing in-bounds pointer arithmetic
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `UNDERFLOW_ADDRESS_SPACE` at $DIR/offset_ub.rs:14:57
|
::: $DIR/offset_ub.rs:14:1
|
LL | pub const UNDERFLOW_ADDRESS_SPACE: *const u8 = unsafe { (1 as *const u8).offset(-2) };
| --------------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| inbounds test failed: pointer must be in-bounds at offset 1, but is outside bounds of allocN which has size 0
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `ZERO_SIZED_ALLOC` at $DIR/offset_ub.rs:16:50
|
::: $DIR/offset_ub.rs:16:1
|
LL | pub const ZERO_SIZED_ALLOC: *const u8 = unsafe { [0u8; 0].as_ptr().offset(1) };
| -------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/mut_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count) as *mut T
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| unable to turn bytes into a pointer
| inside `std::ptr::mut_ptr::<impl *mut u8>::offset` at $SRC_DIR/libcore/ptr/mut_ptr.rs:LL:COL
| inside `DANGLING` at $DIR/offset_ub.rs:17:42
|
::: $DIR/offset_ub.rs:17:1
|
LL | pub const DANGLING: *const u8 = unsafe { ptr::NonNull::<u8>::dangling().as_ptr().offset(4) };
| ---------------------------------------------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
|
LL | intrinsics::offset(self, count)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| inbounds test failed: 0x0 is not a valid pointer
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
| inside `NULL_OFFSET_ZERO` at $DIR/offset_ub.rs:20:50
|
::: $DIR/offset_ub.rs:20:1
|
LL | pub const NULL_OFFSET_ZERO: *const u8 = unsafe { ptr::null::<u8>().offset(0) };
| -------------------------------------------------------------------------------
error: aborting due to 10 previous errors