forked from rust-lang/rust
-
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
Open
Dhvani-Kapadia
wants to merge
15
commits into
model-checking:main
Choose a base branch
from
danielhumanmod:dhvani_ptr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d7e9106
Contract and harness for as_ptr, cast, as_mut_ptr, and as_non_null_ptr
Dhvani-Kapadia 5c5904f
Resolved feedbacks
Dhvani-Kapadia 8a21bc6
Delete verify-error.log
Dhvani-Kapadia 0a9fc55
Deleting Kani
Dhvani-Kapadia ec1d94f
Merge branch 'dhvani_ptr' of https://github.com/danielhumanmod/verify…
Dhvani-Kapadia 8669d9f
Update non_null.rs
Dhvani-Kapadia a393bb1
editing non_null.rs
Dhvani-Kapadia 0116189
Update non_null.rs
Dhvani-Kapadia a844700
Update non_null.rs
Dhvani-Kapadia dbf1e32
Update non_null.rs
Dhvani-Kapadia 20652aa
Update non_null.rs
Dhvani-Kapadia 17d509e
Update non_null.rs
Dhvani-Kapadia 9b1cca6
Update non_null.rs
Dhvani-Kapadia 9bd3888
Update non_null.rs
Dhvani-Kapadia e9d741c
worked on feedback
Dhvani-Kapadia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,6 +334,8 @@ impl<T: ?Sized> NonNull<T> { | |
#[rustc_never_returns_null_ptr] | ||
#[must_use] | ||
#[inline(always)] | ||
//Ensures address of resulting pointer is same as original | ||
#[ensures(|result: &*mut T| *result == self.pointer as *mut T)] | ||
pub const fn as_ptr(self) -> *mut T { | ||
self.pointer as *mut T | ||
} | ||
|
@@ -430,6 +432,10 @@ impl<T: ?Sized> NonNull<T> { | |
#[must_use = "this returns the result of the operation, \ | ||
without modifying the original"] | ||
#[inline] | ||
// Address preservation | ||
#[ensures(|result: &NonNull<U>| result.as_ptr() as *const () as usize == self.as_ptr() as *const () as usize )] | ||
// Ensures pointer is properly aligned for new type 'U' | ||
#[ensures(|result: &NonNull<U>| (self.as_ptr() as *const () as usize) % core::mem::align_of::<U>() == 0)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't assume that. |
||
pub const fn cast<U>(self) -> NonNull<U> { | ||
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null | ||
unsafe { NonNull { pointer: self.as_ptr() as *mut U } } | ||
|
@@ -1508,6 +1514,8 @@ impl<T> NonNull<[T]> { | |
#[must_use] | ||
#[unstable(feature = "slice_ptr_get", issue = "74265")] | ||
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")] | ||
// Address preservation | ||
#[ensures(|result: &NonNull<T>| result.as_ptr() as *const () as usize == self.pointer as *const () as usize)] | ||
pub const fn as_non_null_ptr(self) -> NonNull<T> { | ||
self.cast() | ||
} | ||
|
@@ -1528,6 +1536,8 @@ impl<T> NonNull<[T]> { | |
#[unstable(feature = "slice_ptr_get", issue = "74265")] | ||
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")] | ||
#[rustc_never_returns_null_ptr] | ||
// Address preservation | ||
#[ensures(|result: &*mut T| *result == self.pointer as *mut T)] | ||
pub const fn as_mut_ptr(self) -> *mut T { | ||
self.as_non_null_ptr().as_ptr() | ||
} | ||
|
@@ -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() { | ||
Dhvani-Kapadia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Create a non-null pointer to a random value | ||
let mut x: i32 = kani::any(); | ||
let ptr = NonNull::new(&mut x as *mut i32).unwrap(); | ||
// Call as_ptr | ||
let raw_ptr = ptr.as_ptr(); | ||
} | ||
#[kani::proof_for_contract(NonNull::<[T]>::as_mut_ptr)] | ||
pub fn non_null_check_as_mut_ptr() { | ||
// 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); | ||
Dhvani-Kapadia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Call as_mut_ptr | ||
let raw_ptr = slice_ptr.as_mut_ptr(); | ||
} | ||
|
||
#[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(); | ||
Dhvani-Kapadia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let ptr = NonNull::new(&mut x as *mut i32).unwrap(); | ||
// Perform the cast | ||
let casted_ptr: NonNull<u8> = ptr.cast(); | ||
} | ||
#[kani::proof_for_contract(NonNull::<[T]>::as_non_null_ptr)] | ||
pub fn non_null_check_as_non_null_ptr() { | ||
// Create a non-null pointer to a random value | ||
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); | ||
Dhvani-Kapadia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Call as_non_null_ptr | ||
let result = slice_ptr.as_non_null_ptr(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please use
addr()
instead wherever you are casting tousize
.