Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verifying unit type #5

Open
wants to merge 2 commits into
base: verify/ptr_const
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 131 additions & 7 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,10 @@ impl<T: ?Sized> *const T {
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[requires(kani::mem::can_dereference(self))]
// TODO: Determine the valid value range for 'count' and update the precondition accordingly.
#[requires(count == 0)] // This precondition is currently a placeholder.
#[ensures(|result| kani::mem::can_dereference(result))]
pub const unsafe fn add(self, count: usize) -> Self
where
T: Sized,
Expand Down Expand Up @@ -932,6 +936,10 @@ impl<T: ?Sized> *const T {
#[rustc_allow_const_fn_unstable(unchecked_neg)]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[requires(kani::mem::can_dereference(self))]
// TODO: Determine the valid value range for 'count' and update the precondition accordingly.
#[requires(count == 0)] // This precondition is currently a placeholder.
#[ensures(|result| kani::mem::can_dereference(result))]
pub const unsafe fn sub(self, count: usize) -> Self
where
T: Sized,
Expand Down Expand Up @@ -1787,13 +1795,129 @@ impl<T: ?Sized> PartialOrd for *const T {
#[unstable(feature = "kani", issue = "none")]
mod verify {
use crate::kani;

// fn <*const T>::add verification begin
macro_rules! generate_add_harness {
($type:ty, $proof_name:ident) => {
#[allow(unused)]
#[kani::proof_for_contract(<*const $type>::add)]
pub fn $proof_name() {
let mut test_val: $type = kani::any::<$type>();
let test_ptr: *const $type = &test_val;
let count: usize = kani::any();
unsafe {
test_ptr.add(count);
}
}
};
}

generate_add_harness!(i8, check_add_i8);
generate_add_harness!(i16, check_add_i16);
generate_add_harness!(i32, check_add_i32);
generate_add_harness!(i64, check_add_i64);
generate_add_harness!(i128, check_add_i128);
generate_add_harness!(isize, check_add_isize);
generate_add_harness!(u8, check_add_u8);
generate_add_harness!(u16, check_add_u16);
generate_add_harness!(u32, check_add_u32);
generate_add_harness!(u64, check_add_u64);
generate_add_harness!(u128, check_add_u128);
generate_add_harness!(usize, check_add_usize);
// fn <*const T>::add verification end

// fn <*const T>::sub verification begin
macro_rules! generate_sub_harness {
($type:ty, $proof_name:ident) => {
#[allow(unused)]
#[kani::proof_for_contract(<*const $type>::sub)]
pub fn $proof_name() {
let mut test_val: $type = kani::any::<$type>();
let test_ptr: *const $type = &test_val;
let count: usize = kani::any();
unsafe {
test_ptr.sub(count);
}
}
};
}

generate_sub_harness!(i8, check_sub_i8);
generate_sub_harness!(i16, check_sub_i16);
generate_sub_harness!(i32, check_sub_i32);
generate_sub_harness!(i64, check_sub_i64);
generate_sub_harness!(i128, check_sub_i128);
generate_sub_harness!(isize, check_sub_isize);
generate_sub_harness!(u8, check_sub_u8);
generate_sub_harness!(u16, check_sub_u16);
generate_sub_harness!(u32, check_sub_u32);
generate_sub_harness!(u64, check_sub_u64);
generate_sub_harness!(u128, check_sub_u128);
generate_sub_harness!(usize, check_sub_usize);
// fn <*const T>::sub verification end

// fn <*const T>::offset verification begin
macro_rules! generate_offset_harness {
($type:ty, $proof_name:ident) => {
#[allow(unused)]
#[kani::proof_for_contract(<*const $type>::offset)]
pub fn $proof_name() {
let mut test_val: $type = kani::any::<$type>();
let test_ptr: *const $type = &test_val;
let count: isize = kani::any();
unsafe {
test_ptr.offset(count);
}
}
};
}

generate_offset_harness!(i8, check_offset_i8);
generate_offset_harness!(i16, check_offset_i16);
generate_offset_harness!(i32, check_offset_i32);
generate_offset_harness!(i64, check_offset_i64);
generate_offset_harness!(i128, check_offset_i128);
generate_offset_harness!(isize, check_offset_isize);
generate_offset_harness!(u8, check_offset_u8);
generate_offset_harness!(u16, check_offset_u16);
generate_offset_harness!(u32, check_offset_u32);
generate_offset_harness!(u64, check_offset_u64);
generate_offset_harness!(u128, check_offset_u128);
generate_offset_harness!(usize, check_offset_usize);
// fn <*const T>::offset verification end

// Unit type proofs start


macro_rules! generate_const_unit_harness {
($fn_name:ident, $proof_name:ident) => {
#[allow(unused)]
#[kani::proof_for_contract(<*const ()>::$fn_name)]
pub fn $proof_name() {
let test_val: () = ();
let test_ptr: *const () = &test_val;
let count: usize = kani::any();
unsafe {
test_ptr.$fn_name(count);
}
}
};
}

generate_const_unit_harness!(add, check_const_add_unit);
generate_const_unit_harness!(sub, check_const_sub_unit);

#[allow(unused)]
#[kani::proof_for_contract(<*const i32>::offset)]
fn check_offset_i32() {
let mut test_val: i32 = kani::any();
let test_ptr: *const i32 = &test_val;
let offset: isize = kani::any();
unsafe { test_ptr.offset(offset) };
}
#[kani::proof_for_contract(<*const ()>::offset)]
pub fn check_const_offset_unit() {
let test_val: () = ();
let test_ptr: *const () = &test_val;
let count: isize = kani::any();
unsafe {
test_ptr.offset(count);
}
}
// Unit type proofs end


}