ub_checks::can_dereference does not support reasoning about pointer to unallocated memory #117
-
@zhassan-aws As part of the attempt to constrain #[requires(count.checked_mul(core::mem::size_of::<T>()).is_some()
&& count * core::mem::size_of::<T>() <= isize::MAX as usize
&& ub_checks::can_dereference(self.pointer)
&& ub_checks::can_dereference(self.pointer.wrapping_offset(count as size)))]
#[ensures(|result: &NonNull<T>| result.as_ptr() == self.as_ptr().offset(count as isize))]
pub const unsafe fn add(self, count: usize) -> Self
where
T: Sized,
{
unsafe { NonNull { pointer: intrinsics::offset(self.pointer, count) } }
} Harness: #[kani::proof_for_contract(NonNull::add)]
pub fn non_null_check_add() {
const SIZE: usize = 100000;
// Randomize pointer offset within array bound
let offset = kani::any_where(|x| * x <= SIZE as isize);
kani::assume(offset >= 0);
// Create a non-deterministic array of size SIZE
let arr: [i8; SIZE] = kani::any();
// Get a raw pointer to the array
let raw_ptr: *mut i8 = arr.as_ptr() as *mut i8;
// NonNUll pointer to the random offset
let ptr = unsafe { NonNull::new(raw_ptr.offset(offset)).unwrap()};
// Create a non-deterministic count value
let count: usize = kani::any();
unsafe {
// Add a positive offset to pointer
let result = ptr.add(count);
}
} Note
Is this expected and is it true that we cannot use
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @QinyuanWu. Yes, unfortunately, this is currently a limitation with The PR that adds an API to check if two pointers are within the same allocation has been merged: model-checking/kani#3583 Once this is available in this repo, you can start using it. |
Beta Was this translation helpful? Give feedback.
Hi @QinyuanWu. Yes, unfortunately, this is currently a limitation with
can_dereference
. Unless the offset is restricted in the harness (but it shouldn't be), the new pointer can go out-of-bounds, and Kani will fail.The PR that adds an API to check if two pointers are within the same allocation has been merged: model-checking/kani#3583
Once this is available in this repo, you can start using it.