Skip to content

Commit 3cbd281

Browse files
committed
resolve read_unaligned correctness
1 parent d8c1241 commit 3cbd281

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

library/core/src/ptr/non_null.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,8 @@ impl<T: ?Sized> NonNull<T> {
875875
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
876876
#[stable(feature = "non_null_convenience", since = "1.80.0")]
877877
#[rustc_const_stable(feature = "non_null_convenience", since = "1.80.0")]
878-
#[requires(self.pointer.is_aligned() && ub_checks::can_dereference(self.pointer))] // TODO: is can_dereference the best API to check if self is valid for read?
879-
#[ensures(|result| self.pointer.is_aligned())] //unsafe { *result == *self.pointer } can't be used due to unsized and lack of PartialEq implementation, moved to harness
878+
#[requires(self.pointer.is_aligned() && ub_checks::can_dereference(self.pointer))]
879+
#[ensures(|result| self.pointer.is_aligned())] // unsafe { *result == *self.pointer } can't be used due to unsized and lack of PartialEq implementation, moved to harness
880880
pub const unsafe fn read(self) -> T
881881
where
882882
T: Sized,
@@ -1857,15 +1857,16 @@ mod verify {
18571857
const SIZE: usize = 100;
18581858
// cast a random offset of an u8 array to usize
18591859
let offset: usize = kani::any();
1860-
kani::assume(offset >= 0 && offset < SIZE);
1860+
//const bound: usize = core::mem::size_of::<usize>();
1861+
kani::assume(offset >= 0 && offset < SIZE - core::mem::size_of::<usize>());
18611862
let arr: [u8; SIZE] = kani::any();
18621863
let unaligned_ptr = &arr[offset] as *const u8 as *const usize;
18631864
// create a NonNull pointer from the unaligned pointer
18641865
let nonnull_ptr = NonNull::new(unaligned_ptr as *mut usize).unwrap();
18651866
unsafe {
18661867
let result = nonnull_ptr.read_unaligned();
1867-
/*
1868-
let raw_result_ptr = result as *const u8;
1868+
1869+
let raw_result_ptr = &result as *const usize as *const u8;
18691870
let raw_original_ptr = &arr[offset] as *const u8;
18701871
for i in 0..core::mem::size_of::<usize>() {
18711872
unsafe {
@@ -1874,8 +1875,6 @@ mod verify {
18741875
}
18751876

18761877
}
1877-
// TODO: check result matches expectation(how to?) compare slice of bytes
1878-
*/
18791878
}
18801879

18811880
// read an unaligned value from a packed struct

0 commit comments

Comments
 (0)