Skip to content

Commit d8c1241

Browse files
committed
read_unaligned byte by byte compare issue
1 parent 6396bb3 commit d8c1241

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

library/core/src/ptr/non_null.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,21 +1820,18 @@ mod verify {
18201820
let result = nonnull_ptr_u8.read();
18211821
kani::assert(*ptr_u8 == result, "read returns the correct value");
18221822
}
1823-
/*
1823+
1824+
// array example
18241825
const arr_len: usize = 10;
18251826
let offset = kani::any_where(|x| * x < arr_len);
18261827
kani::assume(offset >= 0);
18271828
let arr: [i8; arr_len] = kani::any();
18281829
let raw_ptr: *mut i8 = arr.as_ptr() as *mut i8;
1829-
let nonnull_ptr = unsafe { NonNull::new(raw_ptr.add(offset)).unwrap()};
1830-
let slice_len = arr_len - offset;
1831-
let slice_ptr = NonNull::slice_from_raw_parts(nonnull_ptr, slice_len);
1830+
let nonnull_ptr = unsafe { NonNull::new(raw_ptr).unwrap()};
18321831
unsafe {
1833-
let result = slice_ptr.read(); //TODO: slice is unsized and cannot call read
1834-
kani::assert(*nonnull_ptr.pointer == result, "read returns the correct value");
1832+
let result = nonnull_ptr.add(offset).read();
1833+
kani::assert( *nonnull_ptr.as_ptr().add(offset) == result, "read returns the correct value");
18351834
}
1836-
kani::assert(slice_ptr.as_ref().len() == slice_len, "read preserves slice length");
1837-
*/
18381835
}
18391836

18401837
// pub unsafe fn read_volatile(self) -> T where T: Sized
@@ -1867,7 +1864,18 @@ mod verify {
18671864
let nonnull_ptr = NonNull::new(unaligned_ptr as *mut usize).unwrap();
18681865
unsafe {
18691866
let result = nonnull_ptr.read_unaligned();
1870-
// TODO: check result matches expectation(how to?)
1867+
/*
1868+
let raw_result_ptr = result as *const u8;
1869+
let raw_original_ptr = &arr[offset] as *const u8;
1870+
for i in 0..core::mem::size_of::<usize>() {
1871+
unsafe {
1872+
// Dereference and compare each byte
1873+
assert_eq!(*raw_original_ptr.add(i), *raw_result_ptr.add(i));
1874+
}
1875+
1876+
}
1877+
// TODO: check result matches expectation(how to?) compare slice of bytes
1878+
*/
18711879
}
18721880

18731881
// read an unaligned value from a packed struct

0 commit comments

Comments
 (0)