Prove correctness for NonNull::read_unaligned
#147
-
In the harness for pub fn non_null_check_read_unaligned() {
const SIZE: usize = 100;
// cast a random offset of an u8 array to usize
let offset: usize = kani::any();
kani::assume(offset >= 0 && offset < SIZE - 4);
let arr: [u8; SIZE] = kani::any();
let unaligned_ptr = &arr[offset] as *const u8 as *const usize;
// create a NonNull pointer from the unaligned pointer
let nonnull_ptr = NonNull::new(unaligned_ptr as *mut usize).unwrap();
unsafe {
let result = nonnull_ptr.read_unaligned();
let raw_result_ptr = result as *const u8;
let raw_original_ptr = &arr[offset] as *const u8;
for i in 0..core::mem::size_of::<usize>() {
// Dereference and compare each byte
assert_eq!(*raw_original_ptr.add(i), *raw_result_ptr.add(i));
}
} Coontract for #[requires(ub_checks::can_dereference(self.pointer))]
pub const unsafe fn read_unaligned(self) -> T
where
T: Sized,
{
// SAFETY: the caller must uphold the safety contract for `read_unaligned`.
unsafe { ptr::read_unaligned(self.pointer) }
} However the following checks failed:
Appreciate any guidance. @zhassan-aws I checked this example but in that proof we know the concrete value so here the value is non-deterministic so I have to use a loop to compare the bytes. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @QinyuanWu. One problem in the harness is that |
Beta Was this translation helpful? Give feedback.
-
Also, please file a feature request in https://github.com/model-checking/kani for an byte-by-byte comparison API. |
Beta Was this translation helpful? Give feedback.
The other issue in the harness is this line:
The
read_unaligned
method returns the value, and so we need to get the address of it, i.e.: