forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
and some other raw pointer shenanigans while we are at it
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |