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

Contracts & Harnesses for Reference Conversion APIs at std::NonNull #116

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
114 changes: 114 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::slice::{self, SliceIndex};
use crate::ub_checks::assert_unsafe_precondition;
use crate::{fmt, hash, intrinsics, ptr};
use safety::{ensures, requires};
use crate::{ub_checks};


#[cfg(kani)]
Expand Down Expand Up @@ -139,6 +140,8 @@ impl<T: Sized> NonNull<T> {
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[requires(ub_checks::can_dereference(self.as_ptr()))] // Ensure the pointer is valid to create a reference.
#[ensures(|result: &&MaybeUninit<T>| core::ptr::eq(*result, self.cast().as_ptr()))] // Ensure returned reference points to the correct memory location.
pub const unsafe fn as_uninit_ref<'a>(self) -> &'a MaybeUninit<T> {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
Expand All @@ -163,6 +166,8 @@ impl<T: Sized> NonNull<T> {
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[requires(ub_checks::can_dereference(self.as_ptr()))] // Ensure pointer is valid to create a mutable reference.
#[ensures(|result: &&mut MaybeUninit<T>| core::ptr::eq(*result, self.cast().as_ptr()))] // Ensure the returned reference points to the correct memory.
pub const unsafe fn as_uninit_mut<'a>(self) -> &'a mut MaybeUninit<T> {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
Expand Down Expand Up @@ -368,6 +373,7 @@ impl<T: ?Sized> NonNull<T> {
#[rustc_const_stable(feature = "const_nonnull_as_ref", since = "1.73.0")]
#[must_use]
#[inline(always)]
#[ensures(|result: &&T| core::ptr::eq(*result, self.as_ptr()))] // Ensure returned reference matches pointer
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
Expand Down Expand Up @@ -406,6 +412,8 @@ impl<T: ?Sized> NonNull<T> {
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[must_use]
#[inline(always)]
// verify result (a mutable reference) is still associated with the same memory address as the raw pointer stored in self
#[ensures(|result: &&mut T| core::ptr::eq(*result, self.as_ptr()))]
pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a mutable reference.
Expand Down Expand Up @@ -1573,6 +1581,10 @@ impl<T> NonNull<[T]> {
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[requires(self.as_ptr().cast::<T>().align_offset(core::mem::align_of::<T>()) == 0)] // Ensure the pointer is properly aligned
#[requires(self.len().checked_mul(core::mem::size_of::<T>()).is_some() && self.len() * core::mem::size_of::<T>() <= isize::MAX as usize)] // Ensure the slice size does not exceed isize::MAX
// TODO: add a require to check the slice belong to same allocated object with `same_allocation`
#[ensures(|result: &&[MaybeUninit<T>]| core::ptr::eq(result.as_ptr(), self.cast().as_ptr()))] // Ensure the memory addresses match.
pub const unsafe fn as_uninit_slice<'a>(self) -> &'a [MaybeUninit<T>] {
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
unsafe { slice::from_raw_parts(self.cast().as_ptr(), self.len()) }
Expand Down Expand Up @@ -1638,6 +1650,11 @@ impl<T> NonNull<[T]> {
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[requires(self.as_ptr().cast::<T>().align_offset(core::mem::align_of::<T>()) == 0)] // Ensure the pointer is properly aligned
#[requires(self.len().checked_mul(core::mem::size_of::<T>()).is_some() && self.len() * core::mem::size_of::<T>() <= isize::MAX as usize)] // Ensure the slice size does not exceed isize::MAX
// TODO: add a require to check the slice belong to same allocated object with `same_allocation`
#[ensures(|result: &&mut [MaybeUninit<T>]| result.len() == self.len())] // Length check
#[ensures(|result: &&mut [MaybeUninit<T>]| core::ptr::eq(result.as_ptr(), self.cast().as_ptr()))] // Address check
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> &'a mut [MaybeUninit<T>] {
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
Expand Down Expand Up @@ -1666,6 +1683,8 @@ impl<T> NonNull<[T]> {
/// ```
#[unstable(feature = "slice_ptr_get", issue = "74265")]
#[inline]
#[requires(ub_checks::can_dereference(self.as_ptr()))] // Ensure self is dereferenceable
#[ensures(|result: &NonNull<I::Output>| result.as_ptr() as *mut () != core::ptr::null_mut())] // Ensure valid non-null pointer
pub unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output>
where
I: SliceIndex<[T]>,
Expand Down Expand Up @@ -1803,4 +1822,99 @@ 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_mut)]
pub fn non_null_check_as_mut() {
let mut x: i32 = kani::any();
if let Some(mut ptr) = NonNull::new(&mut x as *mut i32) {
kani::assume(ptr.as_ptr().align_offset(core::mem::align_of::<i32>()) == 0);
unsafe {
let result = ptr.as_mut();
}
}
}

#[kani::proof_for_contract(NonNull::as_ref)]
pub fn non_null_check_as_ref() {
let mut x: i32 = kani::any();
if let Some(ptr) = NonNull::new(&mut x as *mut i32) {
kani::assume(ptr.as_ptr().align_offset(core::mem::align_of::<i32>()) == 0);
unsafe {
let _ = ptr.as_ref();
}
}
}

#[kani::proof_for_contract(NonNull::as_uninit_mut)]
pub fn non_null_check_as_uninit_mut() {
use core::mem::MaybeUninit;

// Create an uninitialized MaybeUninit value
let mut uninit: MaybeUninit<i32> = MaybeUninit::uninit();
let mut ptr = NonNull::new(uninit.as_mut_ptr()).unwrap();

unsafe {
let _ = ptr.as_uninit_mut();
}
}

#[kani::proof_for_contract(NonNull::as_uninit_ref)]
pub fn non_null_check_as_uninit_ref() {
use core::mem::MaybeUninit;

// Create an uninitialized MaybeUninit value
let mut uninit: MaybeUninit<i32> = MaybeUninit::uninit();
let ptr = NonNull::new(uninit.as_mut_ptr()).unwrap();

unsafe {
let uninit_ref = ptr.as_uninit_ref();
}
}

#[kani::proof_for_contract(NonNull::as_uninit_slice)]
pub fn non_null_check_as_uninit_slice() {
use core::mem::MaybeUninit;

const SIZE: usize = 100000;
let mut arr: [MaybeUninit<i32>; SIZE] = MaybeUninit::uninit_array();
let ptr = NonNull::slice_from_raw_parts(
NonNull::new(arr.as_mut_ptr()).unwrap(),
SIZE,
);

unsafe {
let _ = ptr.as_uninit_slice();
}
}

#[kani::proof_for_contract(NonNull::as_uninit_slice_mut)]
pub fn non_null_check_as_uninit_slice_mut() {
use core::mem::MaybeUninit;

const SIZE: usize = 100000;
let mut arr: [MaybeUninit<i32>; SIZE] = MaybeUninit::uninit_array();
let ptr = NonNull::slice_from_raw_parts(
NonNull::new(arr.as_mut_ptr()).unwrap(),
SIZE,
);

unsafe {
let _ = ptr.as_uninit_slice_mut();
}
}

#[kani::proof_for_contract(NonNull::get_unchecked_mut)]
pub fn non_null_check_get_unchecked_mut() {
const ARR_SIZE: usize = 100000;
let mut arr: [i32; ARR_SIZE] = kani::any();
let raw_ptr = arr.as_mut_ptr();
let ptr = NonNull::slice_from_raw_parts(
NonNull::new(raw_ptr).unwrap(),
ARR_SIZE,
);
let index = kani::any_where(|x| *x < ARR_SIZE - 1);
unsafe {
let _ = ptr.get_unchecked_mut(index..index + 1);
}
}
}