about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlbin Hedman <albin9604@gmail.com>2020-12-26 01:59:08 +0100
committerAlbin Hedman <albin9604@gmail.com>2020-12-26 03:45:51 +0100
commit5e27765ddfbeb524629ecb78c430fbe9c765232b (patch)
tree81ef49d3c8af66f0a682948e5c8239b27af55263
parent1975a6e710d6fbb583a415edc3efed1e8970296a (diff)
downloadrust-5e27765ddfbeb524629ecb78c430fbe9c765232b.tar.gz
rust-5e27765ddfbeb524629ecb78c430fbe9c765232b.zip
Add tests
-rw-r--r--library/core/tests/const_ptr.rs51
-rw-r--r--library/core/tests/lib.rs7
-rw-r--r--library/core/tests/mem.rs7
-rw-r--r--src/test/ui/const-ptr/out_of_bounds_read.rs16
-rw-r--r--src/test/ui/const-ptr/out_of_bounds_read.stderr54
5 files changed, 135 insertions, 0 deletions
diff --git a/library/core/tests/const_ptr.rs b/library/core/tests/const_ptr.rs
new file mode 100644
index 00000000000..4acd059ab03
--- /dev/null
+++ b/library/core/tests/const_ptr.rs
@@ -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]));
+}
diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs
index 2828235c3e3..9e8ec706021 100644
--- a/library/core/tests/lib.rs
+++ b/library/core/tests/lib.rs
@@ -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 cell;
 mod char;
 mod clone;
 mod cmp;
+
+#[cfg(not(bootstrap))]
+mod const_ptr;
+
 mod fmt;
 mod hash;
 mod intrinsics;
diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs
index 5d0fedd4d9c..a09a2e91804 100644
--- a/library/core/tests/mem.rs
+++ b/library/core/tests/mem.rs
@@ -267,3 +267,10 @@ fn uninit_write_slice_cloned_no_drop() {
 
     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);
+}
diff --git a/src/test/ui/const-ptr/out_of_bounds_read.rs b/src/test/ui/const-ptr/out_of_bounds_read.rs
new file mode 100644
index 00000000000..183aa9e5122
--- /dev/null
+++ b/src/test/ui/const-ptr/out_of_bounds_read.rs
@@ -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() };
+}
diff --git a/src/test/ui/const-ptr/out_of_bounds_read.stderr b/src/test/ui/const-ptr/out_of_bounds_read.stderr
new file mode 100644
index 00000000000..ca65a079947
--- /dev/null
+++ b/src/test/ui/const-ptr/out_of_bounds_read.stderr
@@ -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
+