Skip to content

Commit bd33846

Browse files
committed
add misalignment const-eval test
and some other raw pointer shenanigans while we are at it
1 parent a993a8b commit bd33846

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// normalize-stderr-test "alloc\d+" -> "allocN"
2+
#![feature(const_pointer_byte_offsets)]
3+
#![feature(pointer_byte_offsets)]
4+
#![feature(const_mut_refs)]
5+
6+
const MISALIGNED_LOAD: () = unsafe {
7+
let mem = [0u32; 8];
8+
let ptr = mem.as_ptr().byte_add(1);
9+
let _val = *ptr; //~ERROR: evaluation of constant value failed
10+
//~^NOTE: accessing memory with alignment 1, but alignment 4 is required
11+
};
12+
13+
const MISALIGNED_STORE: () = unsafe {
14+
let mut mem = [0u32; 8];
15+
let ptr = mem.as_mut_ptr().byte_add(1);
16+
*ptr = 0; //~ERROR: evaluation of constant value failed
17+
//~^NOTE: accessing memory with alignment 1, but alignment 4 is required
18+
};
19+
20+
const MISALIGNED_COPY: () = unsafe {
21+
let x = &[0_u8; 4];
22+
let y = x.as_ptr().cast::<u32>();
23+
let mut z = 123;
24+
y.copy_to_nonoverlapping(&mut z, 1);
25+
//~^NOTE
26+
// The actual error points into the implementation of `copy_to_nonoverlapping`.
27+
};
28+
29+
const OOB: () = unsafe {
30+
let mem = [0u32; 1];
31+
let ptr = mem.as_ptr().cast::<u64>();
32+
let _val = *ptr; //~ERROR: evaluation of constant value failed
33+
//~^NOTE: size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds
34+
};
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/raw-pointer-ub.rs:9:16
3+
|
4+
LL | let _val = *ptr;
5+
| ^^^^ accessing memory with alignment 1, but alignment 4 is required
6+
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/raw-pointer-ub.rs:16:5
9+
|
10+
LL | *ptr = 0;
11+
| ^^^^^^^^ accessing memory with alignment 1, but alignment 4 is required
12+
13+
error[E0080]: evaluation of constant value failed
14+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
15+
|
16+
= note: accessing memory with alignment 1, but alignment 4 is required
17+
|
18+
note: inside `copy_nonoverlapping::<u32>`
19+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
20+
note: inside `ptr::const_ptr::<impl *const u32>::copy_to_nonoverlapping`
21+
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
22+
note: inside `MISALIGNED_COPY`
23+
--> $DIR/raw-pointer-ub.rs:24:5
24+
|
25+
LL | y.copy_to_nonoverlapping(&mut z, 1);
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error[E0080]: evaluation of constant value failed
29+
--> $DIR/raw-pointer-ub.rs:32:16
30+
|
31+
LL | let _val = *ptr;
32+
| ^^^^ dereferencing pointer failed: allocN has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds
33+
34+
error: aborting due to 4 previous errors
35+
36+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)