add misalignment const-eval test

and some other raw pointer shenanigans while we are at it
This commit is contained in:
Ralf Jung 2023-09-04 09:32:23 +02:00
parent a993a8bf3f
commit bd33846253
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,36 @@
// normalize-stderr-test "alloc\d+" -> "allocN"
#![feature(const_pointer_byte_offsets)]
#![feature(pointer_byte_offsets)]
#![feature(const_mut_refs)]
const MISALIGNED_LOAD: () = unsafe {
let mem = [0u32; 8];
let ptr = mem.as_ptr().byte_add(1);
let _val = *ptr; //~ERROR: evaluation of constant value failed
//~^NOTE: accessing memory with alignment 1, but alignment 4 is required
};
const MISALIGNED_STORE: () = unsafe {
let mut mem = [0u32; 8];
let ptr = mem.as_mut_ptr().byte_add(1);
*ptr = 0; //~ERROR: evaluation of constant value failed
//~^NOTE: accessing memory with alignment 1, but alignment 4 is required
};
const MISALIGNED_COPY: () = unsafe {
let x = &[0_u8; 4];
let y = x.as_ptr().cast::<u32>();
let mut z = 123;
y.copy_to_nonoverlapping(&mut z, 1);
//~^NOTE
// The actual error points into the implementation of `copy_to_nonoverlapping`.
};
const OOB: () = unsafe {
let mem = [0u32; 1];
let ptr = mem.as_ptr().cast::<u64>();
let _val = *ptr; //~ERROR: evaluation of constant value failed
//~^NOTE: size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds
};
fn main() {}

View File

@ -0,0 +1,36 @@
error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:9:16
|
LL | let _val = *ptr;
| ^^^^ accessing memory with alignment 1, but alignment 4 is required
error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:16:5
|
LL | *ptr = 0;
| ^^^^^^^^ accessing memory with alignment 1, but alignment 4 is required
error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
= note: accessing memory with alignment 1, but alignment 4 is required
|
note: inside `copy_nonoverlapping::<u32>`
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
note: inside `ptr::const_ptr::<impl *const u32>::copy_to_nonoverlapping`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `MISALIGNED_COPY`
--> $DIR/raw-pointer-ub.rs:24:5
|
LL | y.copy_to_nonoverlapping(&mut z, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:32:16
|
LL | let _val = *ptr;
| ^^^^ dereferencing pointer failed: allocN has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0080`.