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

Contract and harness for copy_to, copy_to_nonoverlapping, copy_from, and copy_from_nonoverlapping #149

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Dhvani-Kapadia
Copy link

@Dhvani-Kapadia Dhvani-Kapadia commented Nov 2, 2024

Description

This PR includes contracts and proof harnesses for the four APIs copy_to, copy_to_nonoverlapping, copy_from, and copy_from_nonoverlapping which are part of the NonNull library in Rust.

Changes Overview:

Covered APIs:
NonNull::copy_to
NonNull::copy_to_nonoverlapping
NonNull::copy_from
NonNull::opy_from_nonoverlapping

Proof harness:
non_null_check_copy_to
non_null_check_copy_to_nonoverlapping
non_null_check_copy_from
non_null_check_copy_from_nonoverlapping,

Revalidation

To revalidate the verification results, run path_to/kani/scripts/kani verify-std -Z unstable-options "path/to/library" -Z function-contracts -Z mem-predicates --harness ptr::non_null::verify. This will run all four harnesses in the module. All default checks should pass:

SUMMARY:
** 0 of 141 failed

VERIFICATION:- SUCCESSFUL
Verification Time: 0.62114185s

Complete - 6 successfully verified harnesses, 0 failures, 6 total.

Resolves #53

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@Dhvani-Kapadia Dhvani-Kapadia requested a review from a team as a code owner November 2, 2024 19:32
Copy link

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -934,6 +934,12 @@ impl<T: ?Sized> NonNull<T> {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[stable(feature = "non_null_convenience", since = "1.80.0")]
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
#[kani::requires(count * core::mem::size_of::<T>() <= isize::MAX as usize)]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation can overflow. Consider using checked_mul or overflowing_mul instead

@@ -934,6 +934,12 @@ impl<T: ?Sized> NonNull<T> {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[stable(feature = "non_null_convenience", since = "1.80.0")]
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
#[kani::requires(count * core::mem::size_of::<T>() <= isize::MAX as usize)]
#[requires(ub_checks::can_dereference(self.as_ptr()))] //The pointer is valid
#[requires(ub_checks::can_dereference(dest.as_ptr()))]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to be able to dereference the destination. You should check if you can write to it for the given count.

@@ -934,6 +934,12 @@ impl<T: ?Sized> NonNull<T> {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[stable(feature = "non_null_convenience", since = "1.80.0")]
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
#[kani::requires(count * core::mem::size_of::<T>() <= isize::MAX as usize)]
#[requires(ub_checks::can_dereference(self.as_ptr()))] //The pointer is valid
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think pointer requires a valid value of type T. I.e., I think you need to cast this to *const MaybeUninit<T>.

Comment on lines +940 to +941
#[ensures(|result: &()| ub_checks::can_dereference(self.as_ptr() as *const T))]
#[ensures(|result: &()| ub_checks::can_dereference(dest.as_ptr() as *const T))]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also not necessarily true since the copy is untyped.

Comment on lines +963 to +968
#[kani::requires(count * core::mem::size_of::<T>() <= isize::MAX as usize)]
#[requires(ub_checks::can_dereference(self.as_ptr()))]
#[requires(ub_checks::can_dereference(dest.as_ptr()))]
#[requires((self.as_ptr() as usize).abs_diff(dest.as_ptr() as usize) >= count * core::mem::size_of::<T>())]
#[ensures(|result: &()| ub_checks::can_dereference(self.as_ptr()))]
#[ensures(|result: &()| ub_checks::can_dereference(dest.as_ptr()))]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a function to allow you to reuse the requires and ensures clauses of all these predicates. Also, the ub_checks module contains a function is_nonoverlapping that you can use

Comment on lines +1926 to +1933
kani::assume(kani::mem::can_dereference(src_u8));
kani::assume(kani::mem::can_dereference(
src_u8.add(core::mem::size_of::<i32>() - 1),
));
kani::assume(kani::mem::can_dereference(dest_u8));
kani::assume(kani::mem::can_dereference(
dest_u8.add(core::mem::size_of::<i32>() - 1),
));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these assumptions. As a rule of thumb, you should minimize assumptions in the harnesses themselves.

Comment on lines +1917 to +1918
let src = NonNull::new(&mut src_value as *mut i32).unwrap();
let dest = NonNull::new(&mut dest_value as *mut i32).unwrap();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please try to create a few more combinations of pointers? You can try using the PointerGenerator or some other code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Challenge 6: Safety of NonNull
2 participants