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 as_ptr, cast, as_mut_ptr, and as_non_null_ptr #126

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

Conversation

Dhvani-Kapadia
Copy link

Description

This PR includes contracts and proof harnesses for the four APIs as_ptr, cast, as_mut_ptr, and as_non_null_ptr which are part of the NonNull library in Rust.

Changes Overview:

Covered APIs:
NonNull::as_ptr: Acquires the underlying *mut pointer
NonNull::cast: Casts to a pointer of another type
NonNull:: as_mut_ptr: Returns raw pointer to array's buffer
NonNull::as_non_null_ptr: Returns a non-null pointer to slice's buffer

Proof harness:
non_null_check_as_ptr
non_null_check_cast
non_null_check_as_mut_ptr
non_null_check_as_non_null_ptr

Revalidation

To revalidate the verification results, run 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 128 failed

VERIFICATION:- SUCCESSFUL
Verification Time: 0.8232234s

Complete - 4 successfully verified harnesses, 0 failures, 4 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 October 22, 2024 01:55
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
@Dhvani-Kapadia
Copy link
Author

@zhassan-aws , I have made the required changes based on your feedback. Kindly review it again.

@@ -1803,4 +1813,41 @@ mod verify {
let maybe_null_ptr = if kani::any() { xptr as *mut i32 } else { null_mut() };
let _ = NonNull::new(maybe_null_ptr);
}

#[kani::proof_for_contract(NonNull::as_ptr)]
pub fn non_null_check_as_ptr() {

Choose a reason for hiding this comment

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

Since the API under verification (NonNull::as_ptr) has no requirements on the data, it is better to use a less-restrictive harness that passes any pointer value, e.g.

let non_null_ptr: *mut i32 = kani::any::<usize>() as *mut i32;
let ptr = NonNull::new(non_null_ptr).unwrap();
...

This guarantees that the API works for all possible pointer values.

// Create a non-null slice pointer
let mut value: i32 = kani::any();
let ptr = NonNull::new(&mut value as *mut i32).unwrap();
let slice_ptr = NonNull::slice_from_raw_parts(ptr, 1);

Choose a reason for hiding this comment

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

Can we use a bigger length? value will need to be an array.

#[kani::proof_for_contract(NonNull::<T>::cast)]
pub fn non_null_check_cast() {
// Create a non-null pointer to a random value
let mut x: i32 = kani::any();

Choose a reason for hiding this comment

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

Same comment about using any pointer value (using kani::any::<usize>())

let mut value: i32 = kani::any();
let ptr = NonNull::new(&mut value as *mut i32).unwrap();
// Create a slice pointer
let slice_ptr = NonNull::slice_from_raw_parts(ptr, 1);

Choose a reason for hiding this comment

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

Same comment about the slice length.

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
3 participants