Add tests

This commit is contained in:
Albin Hedman 2020-12-26 01:59:08 +01:00
parent 1975a6e710
commit 5e27765ddf
5 changed files with 135 additions and 0 deletions

View file

@ -0,0 +1,51 @@
// Aligned to two bytes
const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x45, 0x67])];
const fn unaligned_ptr() -> *const u16 {
// Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16
unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 }
}
#[test]
fn read() {
use core::ptr;
const FOO: i32 = unsafe { ptr::read(&42 as *const i32) };
assert_eq!(FOO, 42);
const ALIGNED: i32 = unsafe { ptr::read_unaligned(&42 as *const i32) };
assert_eq!(ALIGNED, 42);
const UNALIGNED_PTR: *const u16 = unaligned_ptr();
const UNALIGNED: u16 = unsafe { ptr::read_unaligned(UNALIGNED_PTR) };
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
}
#[test]
fn const_ptr_read() {
const FOO: i32 = unsafe { (&42 as *const i32).read() };
assert_eq!(FOO, 42);
const ALIGNED: i32 = unsafe { (&42 as *const i32).read_unaligned() };
assert_eq!(ALIGNED, 42);
const UNALIGNED_PTR: *const u16 = unaligned_ptr();
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
}
#[test]
fn mut_ptr_read() {
const FOO: i32 = unsafe { (&42 as *const i32 as *mut i32).read() };
assert_eq!(FOO, 42);
const ALIGNED: i32 = unsafe { (&42 as *const i32 as *mut i32).read_unaligned() };
assert_eq!(ALIGNED, 42);
const UNALIGNED_PTR: *mut u16 = unaligned_ptr() as *mut u16;
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
}

View file

@ -13,6 +13,8 @@
#![feature(const_assume)]
#![feature(const_cell_into_inner)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(const_ptr_read)]
#![feature(const_ptr_offset)]
#![feature(core_intrinsics)]
#![feature(core_private_bignum)]
#![feature(core_private_diy_float)]
@ -34,6 +36,7 @@
#![feature(raw)]
#![feature(sort_internals)]
#![feature(slice_partition_at_index)]
#![feature(maybe_uninit_extra)]
#![feature(maybe_uninit_write_slice)]
#![feature(min_specialization)]
#![feature(step_trait)]
@ -82,6 +85,10 @@
mod char;
mod clone;
mod cmp;
#[cfg(not(bootstrap))]
mod const_ptr;
mod fmt;
mod hash;
mod intrinsics;

View file

@ -267,3 +267,10 @@ fn drop(&mut self) {
forget(src);
}
#[test]
#[cfg(not(bootstrap))]
fn uninit_const_assume_init_read() {
const FOO: u32 = unsafe { MaybeUninit::new(42).assume_init_read() };
assert_eq!(FOO, 42);
}

View file

@ -0,0 +1,16 @@
// error-pattern: any use of this value will cause an error
#![feature(const_ptr_read)]
#![feature(const_ptr_offset)]
fn main() {
use std::ptr;
const DATA: [u32; 1] = [42];
const PAST_END_PTR: *const u32 = unsafe { DATA.as_ptr().add(1) };
const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
}

View file

@ -0,0 +1,54 @@
error: any use of this value will cause an error
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
LL | unsafe { copy_nonoverlapping(src, dst, count) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
| inside `_READ` at $DIR/out_of_bounds_read.rs:13:33
|
::: $DIR/out_of_bounds_read.rs:13:5
|
LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
| ------------------------------------------------------
|
= note: `#[deny(const_err)]` on by default
error: any use of this value will cause an error
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
LL | unsafe { copy_nonoverlapping(src, dst, count) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
| inside `ptr::const_ptr::<impl *const u32>::read` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
| inside `_CONST_READ` at $DIR/out_of_bounds_read.rs:14:39
|
::: $DIR/out_of_bounds_read.rs:14:5
|
LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
| --------------------------------------------------------
error: any use of this value will cause an error
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
LL | unsafe { copy_nonoverlapping(src, dst, count) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
| inside `ptr::mut_ptr::<impl *mut u32>::read` at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
| inside `_MUT_READ` at $DIR/out_of_bounds_read.rs:15:37
|
::: $DIR/out_of_bounds_read.rs:15:5
|
LL | const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
| --------------------------------------------------------------------
error: aborting due to 3 previous errors