-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: main
Are you sure you want to change the base?
Conversation
@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() { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
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:
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.