Skip to content

Commit 5e27765

Browse files
committed
Add tests
1 parent 1975a6e commit 5e27765

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

library/core/tests/const_ptr.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Aligned to two bytes
2+
const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x45, 0x67])];
3+
4+
const fn unaligned_ptr() -> *const u16 {
5+
// Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16
6+
unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 }
7+
}
8+
9+
#[test]
10+
fn read() {
11+
use core::ptr;
12+
13+
const FOO: i32 = unsafe { ptr::read(&42 as *const i32) };
14+
assert_eq!(FOO, 42);
15+
16+
const ALIGNED: i32 = unsafe { ptr::read_unaligned(&42 as *const i32) };
17+
assert_eq!(ALIGNED, 42);
18+
19+
const UNALIGNED_PTR: *const u16 = unaligned_ptr();
20+
21+
const UNALIGNED: u16 = unsafe { ptr::read_unaligned(UNALIGNED_PTR) };
22+
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
23+
}
24+
25+
#[test]
26+
fn const_ptr_read() {
27+
const FOO: i32 = unsafe { (&42 as *const i32).read() };
28+
assert_eq!(FOO, 42);
29+
30+
const ALIGNED: i32 = unsafe { (&42 as *const i32).read_unaligned() };
31+
assert_eq!(ALIGNED, 42);
32+
33+
const UNALIGNED_PTR: *const u16 = unaligned_ptr();
34+
35+
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
36+
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
37+
}
38+
39+
#[test]
40+
fn mut_ptr_read() {
41+
const FOO: i32 = unsafe { (&42 as *const i32 as *mut i32).read() };
42+
assert_eq!(FOO, 42);
43+
44+
const ALIGNED: i32 = unsafe { (&42 as *const i32 as *mut i32).read_unaligned() };
45+
assert_eq!(ALIGNED, 42);
46+
47+
const UNALIGNED_PTR: *mut u16 = unaligned_ptr() as *mut u16;
48+
49+
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
50+
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
51+
}

library/core/tests/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#![feature(const_assume)]
1414
#![feature(const_cell_into_inner)]
1515
#![feature(const_maybe_uninit_assume_init)]
16+
#![feature(const_ptr_read)]
17+
#![feature(const_ptr_offset)]
1618
#![feature(core_intrinsics)]
1719
#![feature(core_private_bignum)]
1820
#![feature(core_private_diy_float)]
@@ -34,6 +36,7 @@
3436
#![feature(raw)]
3537
#![feature(sort_internals)]
3638
#![feature(slice_partition_at_index)]
39+
#![feature(maybe_uninit_extra)]
3740
#![feature(maybe_uninit_write_slice)]
3841
#![feature(min_specialization)]
3942
#![feature(step_trait)]
@@ -82,6 +85,10 @@ mod cell;
8285
mod char;
8386
mod clone;
8487
mod cmp;
88+
89+
#[cfg(not(bootstrap))]
90+
mod const_ptr;
91+
8592
mod fmt;
8693
mod hash;
8794
mod intrinsics;

library/core/tests/mem.rs

+7
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,10 @@ fn uninit_write_slice_cloned_no_drop() {
267267

268268
forget(src);
269269
}
270+
271+
#[test]
272+
#[cfg(not(bootstrap))]
273+
fn uninit_const_assume_init_read() {
274+
const FOO: u32 = unsafe { MaybeUninit::new(42).assume_init_read() };
275+
assert_eq!(FOO, 42);
276+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// error-pattern: any use of this value will cause an error
2+
3+
#![feature(const_ptr_read)]
4+
#![feature(const_ptr_offset)]
5+
6+
fn main() {
7+
use std::ptr;
8+
9+
const DATA: [u32; 1] = [42];
10+
11+
const PAST_END_PTR: *const u32 = unsafe { DATA.as_ptr().add(1) };
12+
13+
const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
14+
const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
15+
const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: any use of this value will cause an error
2+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
3+
|
4+
LL | unsafe { copy_nonoverlapping(src, dst, count) }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
8+
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
9+
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
10+
| inside `_READ` at $DIR/out_of_bounds_read.rs:13:33
11+
|
12+
::: $DIR/out_of_bounds_read.rs:13:5
13+
|
14+
LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
15+
| ------------------------------------------------------
16+
|
17+
= note: `#[deny(const_err)]` on by default
18+
19+
error: any use of this value will cause an error
20+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
21+
|
22+
LL | unsafe { copy_nonoverlapping(src, dst, count) }
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
| |
25+
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
26+
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
27+
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
28+
| inside `ptr::const_ptr::<impl *const u32>::read` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
29+
| inside `_CONST_READ` at $DIR/out_of_bounds_read.rs:14:39
30+
|
31+
::: $DIR/out_of_bounds_read.rs:14:5
32+
|
33+
LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
34+
| --------------------------------------------------------
35+
36+
error: any use of this value will cause an error
37+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
38+
|
39+
LL | unsafe { copy_nonoverlapping(src, dst, count) }
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
| |
42+
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
43+
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
44+
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
45+
| inside `ptr::mut_ptr::<impl *mut u32>::read` at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
46+
| inside `_MUT_READ` at $DIR/out_of_bounds_read.rs:15:37
47+
|
48+
::: $DIR/out_of_bounds_read.rs:15:5
49+
|
50+
LL | const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
51+
| --------------------------------------------------------------------
52+
53+
error: aborting due to 3 previous errors
54+

0 commit comments

Comments
 (0)