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

Merged
merged 38 commits into from
Dec 5, 2024
Merged
Changes from 35 commits
Commits
Show all changes
38 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 Oct 22, 2024
5c5904f
Resolved feedbacks
Dhvani-Kapadia Oct 28, 2024
8a21bc6
Delete verify-error.log
Dhvani-Kapadia Oct 28, 2024
0a9fc55
Deleting Kani
Dhvani-Kapadia Oct 28, 2024
ec1d94f
Merge branch 'dhvani_ptr' of https://github.com/danielhumanmod/verify…
Dhvani-Kapadia Oct 28, 2024
8669d9f
Update non_null.rs
Dhvani-Kapadia Oct 28, 2024
a393bb1
editing non_null.rs
Dhvani-Kapadia Oct 28, 2024
0116189
Update non_null.rs
Dhvani-Kapadia Oct 28, 2024
a844700
Update non_null.rs
Dhvani-Kapadia Oct 28, 2024
dbf1e32
Update non_null.rs
Dhvani-Kapadia Oct 29, 2024
20652aa
Update non_null.rs
Dhvani-Kapadia Oct 29, 2024
17d509e
Update non_null.rs
Dhvani-Kapadia Oct 29, 2024
9b1cca6
Update non_null.rs
Dhvani-Kapadia Oct 29, 2024
9bd3888
Update non_null.rs
Dhvani-Kapadia Oct 29, 2024
e9d741c
worked on feedback
Dhvani-Kapadia Nov 13, 2024
065eb9f
Update non_null.rs
Dhvani-Kapadia Nov 16, 2024
5f0538c
Update non_null.rs
Dhvani-Kapadia Nov 16, 2024
f898e60
Update non_null.rs
Dhvani-Kapadia Nov 16, 2024
6bf025b
Merged with main
Dhvani-Kapadia Nov 21, 2024
c02caa9
Merge branch 'dhvani_ptr' of https://github.com/danielhumanmod/verify…
Dhvani-Kapadia Nov 21, 2024
9c3ae58
Delete verify-error1.log
Dhvani-Kapadia Nov 22, 2024
a92f07d
Update non_null.rs
Dhvani-Kapadia Nov 22, 2024
43e579e
fixing indentation
Dhvani-Kapadia Nov 22, 2024
87a482d
Revert "fixing indentation"
Dhvani-Kapadia Nov 22, 2024
32be6da
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 26, 2024
a9e4fda
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 26, 2024
76e717d
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 26, 2024
c1586f8
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 26, 2024
44cb515
Revert changes to stdarch
zhassan-aws Nov 27, 2024
905701d
Merge branch 'main' into dhvani_ptr
Dhvani-Kapadia Nov 27, 2024
d404097
Update non_null.rs
Dhvani-Kapadia Nov 27, 2024
417655e
Merge branch 'main' into dhvani_ptr
Dhvani-Kapadia Nov 27, 2024
408e0cf
Merge branch 'main' into dhvani_ptr
Dhvani-Kapadia Nov 27, 2024
e9b4b37
Remove blank lines
tautschnig Nov 28, 2024
0815ba2
Merge branch 'main' into dhvani_ptr
Dhvani-Kapadia Dec 2, 2024
cdeb735
Merge branch 'model-checking:main' into dhvani_ptr
QinyuanWu Dec 4, 2024
152d68e
Remove rustc_const_unstable attribute
zhassan-aws Dec 4, 2024
d4924e9
Merge branch 'main' into dhvani_ptr
zhassan-aws Dec 4, 2024
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
45 changes: 45 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,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
}
Expand Down Expand Up @@ -454,6 +456,8 @@ 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().addr() == self.as_ptr().addr())]
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 } }
Expand Down Expand Up @@ -1451,6 +1455,9 @@ impl<T> NonNull<[T]> {
#[inline]
#[must_use]
#[unstable(feature = "slice_ptr_get", issue = "74265")]
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
Dhvani-Kapadia marked this conversation as resolved.
Show resolved Hide resolved
// Address preservation
#[ensures(|result: &NonNull<T>| result.as_ptr().addr() == self.as_ptr().addr())]
pub const fn as_non_null_ptr(self) -> NonNull<T> {
self.cast()
}
Expand All @@ -1470,6 +1477,8 @@ impl<T> NonNull<[T]> {
#[must_use]
#[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()
}
Expand Down Expand Up @@ -2166,6 +2175,42 @@ mod verify {
}
}

#[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 non_null_ptr: *mut i32 = kani::any::<usize>() as *mut i32;
if let Some(ptr) = NonNull::new(non_null_ptr) {
let result = ptr.as_ptr();
}

}

#[kani::proof_for_contract(NonNull::<[T]>::as_mut_ptr)]
Dhvani-Kapadia marked this conversation as resolved.
Show resolved Hide resolved
pub fn non_null_check_as_mut_ptr() {
const ARR_LEN: usize = 100;
let mut values: [i32; ARR_LEN] = kani::any();
let slice = kani::slice::any_slice_of_array_mut(&mut values);
let non_null_ptr = NonNull::new(slice as *mut [i32]).unwrap();
let result = non_null_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 non_null_ptr: *mut i32 = kani::any::<usize>() as *mut i32;
if let Some(ptr) = NonNull::new(non_null_ptr) {
let result: NonNull<u8> = ptr.cast();
}
}

#[kani::proof_for_contract(NonNull::<[T]>::as_non_null_ptr)]
Dhvani-Kapadia marked this conversation as resolved.
Show resolved Hide resolved
pub fn non_null_check_as_non_null_ptr() {
const ARR_LEN: usize = 100;
let mut values: [i32; ARR_LEN] = kani::any();
let slice = kani::slice::any_slice_of_array_mut(&mut values);
let non_null_ptr = NonNull::new(slice as *mut [i32]).unwrap();
let result = non_null_ptr.as_non_null_ptr();
}

#[kani::proof]
pub fn non_null_check_len() {
// Create a non-deterministic NonNull pointer using kani::any()
Expand Down
Loading