Skip to content

Commit

Permalink
add misalignment const-eval test
Browse files Browse the repository at this point in the history
and some other raw pointer shenanigans while we are at it
  • Loading branch information
RalfJung committed Sep 26, 2023
1 parent a993a8b commit bd33846
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/ui/consts/const-eval/raw-pointer-ub.rs
Original file line number Diff line number Diff line change
@@ -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() {}
36 changes: 36 additions & 0 deletions tests/ui/consts/const-eval/raw-pointer-ub.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit bd33846

Please sign in to comment.