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.

@QinyuanWu
Copy link
Author

QinyuanWu commented Nov 13, 2024

@zhassan-aws I just starting to get compiler errors when I run kani on this branch:

Kani Rust Verifier 0.56.0 (standalone)
error: cannot find a built-in macro with name `SmartPointer`
    --> /Users/owo/Desktop/verify-rust-std/library/core/src/marker.rs:1068:1
     |
1068 | / pub macro SmartPointer($item:item) {
1069 | |     /* compiler built-in */
1070 | | }
     | |_^

error: attributes starting with `rustc` are reserved for use by the `rustc` compiler
    --> /Users/owo/Desktop/verify-rust-std/library/core/src/marker.rs:1095:12
     |
1095 |     impl<#[rustc_runtime] const RUNTIME: bool> Compat<RUNT...
     |            ^^^^^^^^^^^^^

......

I just pulled the latest kani changes and re-built it but the error persists. Latest kani git log:

commit abd68b963c6e4a9508ce9f6950f3d26134631eba
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Nov 11 17:13:34 2024 -0600

    Bump tests/perf/s2n-quic from `65d55a4` to `cb41b35` (#3706)
    
    Bumps [tests/perf/s2n-quic](https://github.com/aws/s2n-quic) from
    `65d55a4` to `cb41b35`.
    <details>
    <summary>Commits</summary>
    <ul>
    <li><a
    href="https://github.com/aws/s2n-quic/commit/cb41b35a9bc0412435b70b5038df0681a881e414"><code>cb41b35</code></a>
    feat(s2n-quic-dc): mtu probing event subscriber (<a
    href="https://redirect.github.com/aws/s2n-quic/issues/2374">#2374</a>)</li>
    <li><a
    href="https://github.com/aws/s2n-quic/commit/56ce95a48db7dd2ed31110d9d408775793bbcb3a"><code>56ce95a</code></a>
    test(s2n-quic-core): add metric snapshot registry (<a

@danielhumanmode also reproduced this error on his local environment and found that Kani compiled the code successfully on commit 13e6695(before the latest merge from main).

@carolynzech
Copy link

@zhassan-aws I just starting to get compiler errors when I run kani on this branch:
I just pulled the latest kani changes and re-built it but the error persists. Latest kani git log:

commit abd68b963c6e4a9508ce9f6950f3d26134631eba
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Nov 11 17:13:34 2024 -0600

    Bump tests/perf/s2n-quic from `65d55a4` to `cb41b35` (#3706)
    
    Bumps [tests/perf/s2n-quic](https://github.com/aws/s2n-quic) from
    `65d55a4` to `cb41b35`.
    <details>
    <summary>Commits</summary>
    <ul>
    <li><a
    href="https://github.com/aws/s2n-quic/commit/cb41b35a9bc0412435b70b5038df0681a881e414"><code>cb41b35</code></a>
    feat(s2n-quic-dc): mtu probing event subscriber (<a
    href="https://redirect.github.com/aws/s2n-quic/issues/2374">#2374</a>)</li>
    <li><a
    href="https://github.com/aws/s2n-quic/commit/56ce95a48db7dd2ed31110d9d408775793bbcb3a"><code>56ce95a</code></a>
    test(s2n-quic-core): add metric snapshot registry (<a

@danielhumanmode also reproduced this error on his local environment and found that Kani compiled the code successfully on commit 13e6695(before the latest merge from main).

@QinyuanWu Is there a reason why you're running on Kani's main branch instead of the features/verify-rust-std branch? If not, please try running Kani from the features/verify-rust-std branch instead.

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.

3 participants