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 and harnesses for dangling, from_raw_parts, slice_from_raw_parts, to_raw_parts in NonNull #127

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

QinyuanWu
Copy link

@QinyuanWu QinyuanWu commented Oct 22, 2024

Towards #53

Contracts and harnesses for dangling, from_raw_parts, slice_from_raw_parts, to_raw_parts in NonNull

Discussion

  1. NonNull::slice_from_raw_parts: requested new Kani API to compare byte by byte
  2. NonNull::to_raw_parts: unstable vtable comparison 'Eq'

Verification Result

SUMMARY:
 ** 0 of 141 failed

VERIFICATION:- SUCCESSFUL
Verification Time: 0.17378491s

Complete - 6 successfully verified harnesses, 0 failures, 6 total.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@QinyuanWu QinyuanWu marked this pull request as ready for review November 7, 2024 19:54
@QinyuanWu QinyuanWu requested a review from a team as a code owner November 7, 2024 19:54
#[kani::proof_for_contract(NonNull::from_raw_parts)]
pub fn non_null_check_from_raw_parts() {

const arr_len: usize = 100;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names of consts should be upper case (see https://doc.rust-lang.org/1.0.0/style/style/naming/README.html).

#[kani::proof_for_contract(NonNull::slice_from_raw_parts)]
#[kani::unwind(11)]
pub fn non_null_check_slice_from_raw_parts() {
const arr_len: usize = 10;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#[stable(feature = "nonnull_slice_from_raw_parts", since = "1.70.0")]
#[rustc_const_stable(feature = "const_slice_from_raw_parts_mut", since = "1.83.0")]
#[must_use]
#[inline]
#[requires(data.pointer.is_aligned()
&& len as isize * core::mem::size_of::<T>() as isize <= isize::MAX

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this subsumed by the next conjunct?

&& len as isize * core::mem::size_of::<T>() as isize <= isize::MAX
&& (len as isize).checked_mul(core::mem::size_of::<T>() as isize).is_some()
&& (data.pointer as isize).checked_add(len as isize * core::mem::size_of::<T>() as isize).is_some() // adding len must not “wrap around” the address space
&& unsafe { kani::mem::same_allocation(data.pointer, data.pointer.add(len)) })]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another safety requirement is that data must be valid for reads. This can be specified using can_dereference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants