Skip to content

Commit f266821

Browse files
authored
Rollup merge of #98587 - RalfJung:core-tests, r=thomcc
libcore tests: avoid int2ptr casts We don't need any of these pointers to actually be dereferenceable so using `ptr::invalid` should be fine. And then we can run Miri with strict provenance enforcement on the tests.
2 parents 19a05b5 + 8c977cf commit f266821

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

library/core/tests/alloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::alloc::Layout;
2-
use core::ptr::NonNull;
2+
use core::ptr::{self, NonNull};
33

44
#[test]
55
fn const_unchecked_layout() {
@@ -9,7 +9,7 @@ fn const_unchecked_layout() {
99
const DANGLING: NonNull<u8> = LAYOUT.dangling();
1010
assert_eq!(LAYOUT.size(), SIZE);
1111
assert_eq!(LAYOUT.align(), ALIGN);
12-
assert_eq!(Some(DANGLING), NonNull::new(ALIGN as *mut u8));
12+
assert_eq!(Some(DANGLING), NonNull::new(ptr::invalid_mut(ALIGN)));
1313
}
1414

1515
#[test]

library/core/tests/hash/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod sip;
22

33
use std::default::Default;
44
use std::hash::{BuildHasher, Hash, Hasher};
5+
use std::ptr;
56
use std::rc::Rc;
67

78
struct MyHasher {
@@ -69,10 +70,10 @@ fn test_writer_hasher() {
6970
let cs: Rc<[u8]> = Rc::new([1, 2, 3]);
7071
assert_eq!(hash(&cs), 9);
7172

72-
let ptr = 5_usize as *const i32;
73+
let ptr = ptr::invalid::<i32>(5_usize);
7374
assert_eq!(hash(&ptr), 5);
7475

75-
let ptr = 5_usize as *mut i32;
76+
let ptr = ptr::invalid_mut::<i32>(5_usize);
7677
assert_eq!(hash(&ptr), 5);
7778

7879
if cfg!(miri) {

library/core/tests/ptr.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ fn align_offset_zst() {
353353
// all, because no amount of elements will align the pointer.
354354
let mut p = 1;
355355
while p < 1024 {
356-
assert_eq!((p as *const ()).align_offset(p), 0);
356+
assert_eq!(ptr::invalid::<()>(p).align_offset(p), 0);
357357
if p != 1 {
358-
assert_eq!(((p + 1) as *const ()).align_offset(p), !0);
358+
assert_eq!(ptr::invalid::<()>(p + 1).align_offset(p), !0);
359359
}
360360
p = (p + 1).next_power_of_two();
361361
}
@@ -371,7 +371,7 @@ fn align_offset_stride1() {
371371
let expected = ptr % align;
372372
let offset = if expected == 0 { 0 } else { align - expected };
373373
assert_eq!(
374-
(ptr as *const u8).align_offset(align),
374+
ptr::invalid::<u8>(ptr).align_offset(align),
375375
offset,
376376
"ptr = {}, align = {}, size = 1",
377377
ptr,
@@ -434,14 +434,14 @@ fn align_offset_weird_strides() {
434434
while align < limit {
435435
for ptr in 1usize..4 * align {
436436
unsafe {
437-
x |= test_weird_stride::<A3>(ptr as *const A3, align);
438-
x |= test_weird_stride::<A4>(ptr as *const A4, align);
439-
x |= test_weird_stride::<A5>(ptr as *const A5, align);
440-
x |= test_weird_stride::<A6>(ptr as *const A6, align);
441-
x |= test_weird_stride::<A7>(ptr as *const A7, align);
442-
x |= test_weird_stride::<A8>(ptr as *const A8, align);
443-
x |= test_weird_stride::<A9>(ptr as *const A9, align);
444-
x |= test_weird_stride::<A10>(ptr as *const A10, align);
437+
x |= test_weird_stride::<A3>(ptr::invalid::<A3>(ptr), align);
438+
x |= test_weird_stride::<A4>(ptr::invalid::<A4>(ptr), align);
439+
x |= test_weird_stride::<A5>(ptr::invalid::<A5>(ptr), align);
440+
x |= test_weird_stride::<A6>(ptr::invalid::<A6>(ptr), align);
441+
x |= test_weird_stride::<A7>(ptr::invalid::<A7>(ptr), align);
442+
x |= test_weird_stride::<A8>(ptr::invalid::<A8>(ptr), align);
443+
x |= test_weird_stride::<A9>(ptr::invalid::<A9>(ptr), align);
444+
x |= test_weird_stride::<A10>(ptr::invalid::<A10>(ptr), align);
445445
}
446446
}
447447
align = (align + 1).next_power_of_two();
@@ -479,8 +479,8 @@ fn ptr_metadata() {
479479
let () = metadata(&[4, 7]);
480480
let () = metadata(&(4, String::new()));
481481
let () = metadata(&Pair(4, String::new()));
482-
let () = metadata(0 as *const Extern);
483-
let () = metadata(0 as *const <&u32 as std::ops::Deref>::Target);
482+
let () = metadata(ptr::null::<()>() as *const Extern);
483+
let () = metadata(ptr::null::<()>() as *const <&u32 as std::ops::Deref>::Target);
484484

485485
assert_eq!(metadata("foo"), 3_usize);
486486
assert_eq!(metadata(&[4, 7][..]), 2_usize);

library/core/tests/waker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::task::{RawWaker, RawWakerVTable, Waker};
33

44
#[test]
55
fn test_waker_getters() {
6-
let raw_waker = RawWaker::new(42usize as *mut (), &WAKER_VTABLE);
6+
let raw_waker = RawWaker::new(ptr::invalid_mut(42usize), &WAKER_VTABLE);
77
assert_eq!(raw_waker.data() as usize, 42);
88
assert!(ptr::eq(raw_waker.vtable(), &WAKER_VTABLE));
99

@@ -15,7 +15,7 @@ fn test_waker_getters() {
1515
}
1616

1717
static WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
18-
|data| RawWaker::new((data as usize + 1) as *mut (), &WAKER_VTABLE),
18+
|data| RawWaker::new(ptr::invalid_mut(data as usize + 1), &WAKER_VTABLE),
1919
|_| {},
2020
|_| {},
2121
|_| {},

0 commit comments

Comments
 (0)