Skip to content

Contract & Harnesses for byte_sub, offset, map_addr and with_addr #107

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

Merged
merged 52 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
0bb80ab
byte_sub and offset
Dhvani-Kapadia Oct 5, 2024
0a2913f
contract and harness for byte_sub and offset
Dhvani-Kapadia Oct 7, 2024
77e8865
editing the mod.rs file(#91)
Dhvani-Kapadia Oct 7, 2024
bcfca87
Delete library/target/.rustc_info.json
Dhvani-Kapadia Oct 11, 2024
a1fc1ae
Update non_null.rs
Dhvani-Kapadia Oct 11, 2024
29f740c
Update mod.rs
Dhvani-Kapadia Oct 11, 2024
c03befa
Delete library/core/src/ptr/target/kani_verify_std/Cargo.toml
Dhvani-Kapadia Oct 11, 2024
bf29680
Delete library/core/src/ptr/target directory
Dhvani-Kapadia Oct 11, 2024
99d4f9e
Delete library/target/kani_verify_std directory
Dhvani-Kapadia Oct 11, 2024
bea518b
Contract and harness for map_addr and with_addr
Dhvani-Kapadia Oct 13, 2024
6d18084
removed kani::requires and self is non null
Dhvani-Kapadia Oct 16, 2024
73eb7cc
resolving conflict
Dhvani-Kapadia Nov 14, 2024
1df5f54
Delete verify-error.log
Dhvani-Kapadia Nov 14, 2024
5972e04
Revised contracts
Dhvani-Kapadia Nov 15, 2024
51c2336
Merge branch 'dhvani_develop' of https://github.com/danielhumanmod/ve…
Dhvani-Kapadia Nov 15, 2024
f5538fd
removing comments
Dhvani-Kapadia Nov 15, 2024
bcb17b1
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 15, 2024
7e5584b
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 15, 2024
bc40a6b
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 16, 2024
a27cf5e
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 18, 2024
7b7ec2f
local changes
Dhvani-Kapadia Nov 19, 2024
78d7894
conflicts merged
Dhvani-Kapadia Nov 19, 2024
3c7e711
removing comments from harness
Dhvani-Kapadia Nov 19, 2024
a61ce84
resolved conflicts with main
Dhvani-Kapadia Nov 19, 2024
dcd0890
resolving merge from main conflicts
Dhvani-Kapadia Nov 19, 2024
4818616
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 22, 2024
2253393
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 22, 2024
5e2ac1a
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 22, 2024
105c2ac
Update library/core/src/ptr/non_null.rs
Dhvani-Kapadia Nov 22, 2024
0cb1044
merged with main
Dhvani-Kapadia Nov 25, 2024
6d0e284
ran rustfmt on the file
Dhvani-Kapadia Nov 25, 2024
59c349c
update
Dhvani-Kapadia Nov 25, 2024
37784bb
Reverted to commit 0cb1044
Dhvani-Kapadia Nov 26, 2024
e9ccbd3
Updated submodule reference for library/stdarch
Dhvani-Kapadia Nov 26, 2024
4f5a3a1
reverting to 37784bb
Dhvani-Kapadia Nov 26, 2024
ceb2c52
modified stdarch
Dhvani-Kapadia Nov 26, 2024
2df925e
merged from main
Dhvani-Kapadia Nov 26, 2024
fe0eb58
Revert change to stdarch
zhassan-aws Nov 27, 2024
9196acb
Merge branch 'main' into dhvani_develop
Dhvani-Kapadia Nov 27, 2024
fda97db
Update non_null.rs
Dhvani-Kapadia Nov 27, 2024
6898d9f
Update non_null.rs
Dhvani-Kapadia Nov 27, 2024
172f150
Update non_null.rs
Dhvani-Kapadia Nov 27, 2024
d743938
Update non_null.rs
Dhvani-Kapadia Nov 27, 2024
3f9f593
Update non_null.rs
Dhvani-Kapadia Nov 27, 2024
5e23b52
Merge branch 'main' into dhvani_develop
Dhvani-Kapadia Dec 4, 2024
1246d25
same_allocation offset
Dhvani-Kapadia Dec 4, 2024
8b34f00
Merge branch 'dhvani_develop' of https://github.com/danielhumanmod/ve…
Dhvani-Kapadia Dec 4, 2024
4ce3119
using ub_checks
Dhvani-Kapadia Dec 6, 2024
bcf4586
Merge branch 'main' into dhvani_develop
tautschnig Dec 10, 2024
6268a5f
Merge branch 'main' into dhvani_develop
tautschnig Dec 10, 2024
659b104
Merge branch 'main' into dhvani_develop
carolynzech Dec 11, 2024
d771b0c
Merge branch 'main' into dhvani_develop
tautschnig Dec 11, 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
67 changes: 65 additions & 2 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ impl<T: ?Sized> NonNull<T> {
#[must_use]
#[inline]
#[stable(feature = "strict_provenance", since = "CURRENT_RUSTC_VERSION")]
#[ensures(|result: &Self| !result.as_ptr().is_null() && result.addr() == addr)]
pub fn with_addr(self, addr: NonZero<usize>) -> Self {
// SAFETY: The result of `ptr::from::with_addr` is non-null because `addr` is guaranteed to be non-zero.
unsafe { NonNull::new_unchecked(self.pointer.with_addr(addr.get()) as *mut _) }
Expand All @@ -327,6 +328,7 @@ impl<T: ?Sized> NonNull<T> {
#[must_use]
#[inline]
#[stable(feature = "strict_provenance", since = "CURRENT_RUSTC_VERSION")]
#[ensures(|result: &Self| !result.as_ptr().is_null())]
pub fn map_addr(self, f: impl FnOnce(NonZero<usize>) -> NonZero<usize>) -> Self {
self.with_addr(f(self.addr()))
}
Expand Down Expand Up @@ -504,6 +506,12 @@ impl<T: ?Sized> NonNull<T> {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[stable(feature = "non_null_convenience", since = "1.80.0")]
#[rustc_const_stable(feature = "non_null_convenience", since = "1.80.0")]
#[requires(
count.checked_mul(core::mem::size_of::<T>() as isize).is_some() &&
(self.as_ptr() as isize).checked_add(count.wrapping_mul(core::mem::size_of::<T>() as isize)).is_some() &&
(count == 0 || ub_checks::same_allocation(self.as_ptr() as *const (), self.as_ptr().wrapping_offset(count) as *const ()))
)]
#[ensures(|result: &Self| result.as_ptr() == self.as_ptr().wrapping_offset(count))]
pub const unsafe fn offset(self, count: isize) -> Self
where
T: Sized,
Expand Down Expand Up @@ -709,6 +717,14 @@ 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_stable(feature = "non_null_convenience", since = "1.80.0")]
#[requires(
count <= (isize::MAX as usize) &&
self.as_ptr().addr().checked_sub(count).is_some() &&
ub_checks::same_allocation(self.as_ptr() as *const (), (self.as_ptr().addr() - count) as *const ())
)]
#[ensures(
|result: &NonNull<T>| result.as_ptr().addr() == (self.as_ptr().addr() - count)
)]
pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub` and `byte_sub` has the same
// safety contract.
Expand Down Expand Up @@ -1760,8 +1776,10 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
#[unstable(feature="kani", issue="none")]
mod verify {
use super::*;
use crate::ptr::null_mut;
use crate::mem;
use crate::ptr::null_mut;
use core::num::NonZeroUsize;
use core::ptr::NonNull;
use kani::PointerGenerator;

trait SampleTrait {
Expand Down Expand Up @@ -2161,7 +2179,6 @@ mod verify {
struct Droppable {
value: i32,
}

impl Drop for Droppable {
fn drop(&mut self) {
}
Expand Down Expand Up @@ -2296,6 +2313,52 @@ mod verify {
let result = non_null_ptr.is_aligned_to(align);
}

#[kani::proof_for_contract(NonNull::byte_sub)]
pub fn non_null_check_byte_sub() {
const SIZE: usize = mem::size_of::<i32>() * 10000;
let mut generator = PointerGenerator::<SIZE>::new();
let count: usize = kani::any();
let raw_ptr: *mut i32 = generator.any_in_bounds().ptr as *mut i32;
let ptr = NonNull::new(raw_ptr).unwrap();
unsafe {
let result = ptr.byte_sub(count);
}
}

#[kani::proof_for_contract(NonNull::offset)]
pub fn non_null_check_offset() {
const SIZE: usize = mem::size_of::<i32>() * 10000;
let mut generator = PointerGenerator::<SIZE>::new();
let start_ptr = generator.any_in_bounds().ptr as *mut i32;
let ptr_nonnull = NonNull::new(start_ptr).unwrap();
let count: isize = kani::any();
unsafe {
let result = ptr_nonnull.offset(count);
}
}

#[kani::proof_for_contract(NonNull::map_addr)]
pub fn non_null_check_map_addr() {
const SIZE: usize = 10000;
let arr: [i32; SIZE] = kani::any();
let ptr = NonNull::new(arr.as_ptr() as *mut i32).unwrap();
let new_offset: usize = kani::any_where(|&x| x <= SIZE);
let f = |addr: NonZeroUsize| -> NonZeroUsize {
NonZeroUsize::new(addr.get().wrapping_add(new_offset)).unwrap()
};
let result = ptr.map_addr(f);
}

#[kani::proof_for_contract(NonNull::with_addr)]
pub fn non_null_check_with_addr() {
const SIZE: usize = 10000;
let arr: [i32; SIZE] = kani::any();
let ptr = NonNull::new(arr.as_ptr() as *mut i32).unwrap();
let new_offset: usize = kani::any_where(|&x| x <= SIZE);
let new_addr = NonZeroUsize::new(ptr.as_ptr().addr() + new_offset).unwrap();
let result = ptr.with_addr(new_addr);
}

#[kani::proof_for_contract(NonNull::sub)]
pub fn non_null_check_sub() {
const SIZE: usize = 10000;
Expand Down
Loading