Skip to content

Use dynamic allocation for non-deterministic slices - #1011

Merged
zhassan-aws merged 20 commits into
model-checking:mainfrom
zhassan-aws:iss1009
Apr 14, 2022
Merged

zhassan-aws merged 20 commits into
model-checking:mainfrom
zhassan-aws:iss1009

Conversation

@zhassan-aws

Copy link
Copy Markdown
Contributor

Description of changes:

Kani's API for non-deterministic slices (NonDetSlice) has unsoundness as it may hide failures due to out-of-bound accesses. This is due to statically allocating an array with the specified maximum length and then returning a slice of a nondet length of that. Accessing an element beyond the slice length should be a failure, but if the element is within the array length, no failure is reported.

Resolved issues:

Resolves #1009

Call-outs:

Testing:

  • How is this change tested? Added one test

  • Is this a refactor change? No

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

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

@zhassan-aws
zhassan-aws requested a review from a team as a code owner April 4, 2022 23:12

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sweet! I hope that doesn't impact performance.

One quick thing, since you are changing this struct, what do you think about renaming it too? Maybe AnySlice?

Comment thread library/kani/src/slice.rs Outdated
pub struct NonDetSlice<T, const MAX_SLICE_LENGTH: usize> {
arr: [T; MAX_SLICE_LENGTH],
layout: Layout,
ptr: *mut u8,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use ptr: *mut T?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea. This removed the need to have PhantomData.

// created using kani::slice::any_slice

#[kani::proof]
fn check_out_of_bounds() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you please add another test with a different T with size > 1 byte.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@celinval

celinval commented Apr 5, 2022

Copy link
Copy Markdown
Contributor

One more thing, I believe all these methods should either be marked as unsafe or T should implement Arbitrary type.

@zhassan-aws

Copy link
Copy Markdown
Contributor Author

While trying to check the impact of this change on performance using one of the s2n-quic proofs, I ran into a CBMC invariant violation:

--- begin invariant violation report ---
Invariant check failed
File: ../src/util/simplify_expr.cpp:2642 function: simplify_rec
Condition: as_const(tmp).type() == expr.type()
Reason: Postcondition
Backtrace:

I confirmed that without this change, verification passes on that particular proof. This is a blocker for this PR until we figure out a fix.

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks much better. Thanks for doing this.

Have you been able to narrow down the problem to see if it's an issue on our side or CBMC?

Comment thread library/kani/src/slice.rs Outdated

pub fn any_slice<T, const MAX_SLICE_LENGTH: usize>() -> NonDetSlice<T, MAX_SLICE_LENGTH> {
NonDetSlice::<T, MAX_SLICE_LENGTH>::new()
pub fn any_slice<T, const MAX_SLICE_LENGTH: usize>() -> AnySlice<T, MAX_SLICE_LENGTH> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just wanna make sure you've seen my comments about making this method unsafe or using the Arbitrary trait.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have seen it, but I haven't addressed it yet. I think we have discussed this before, but I don't recall what we concluded. What I'm thinking right now is to perhaps add Invariant as a trait bound for T in AnySlice and set every element in the slice to kani::any(). We can also provide an unsafe version, e.g. AnyRawSlice that does not require Invariant for T. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, changed T to require Arbitrary, and added an unsafe any_raw_slice version that doesn't require Arbitrary.

@camshaft

camshaft commented Apr 8, 2022

Copy link
Copy Markdown
Contributor

Nice catch! Do you know how much slower the dynamic version is?

@zhassan-aws

Copy link
Copy Markdown
Contributor Author

Nice catch! Do you know how much slower the dynamic version is?

I'm in the process of evaluating the impact of the change on performance. I don't have numbers yet because I ran into a CBMC crash while doing so.

@zhassan-aws

Copy link
Copy Markdown
Contributor Author

Performance does not seem to degrade with the switch to using dynamic allocation. In fact, on one of the s2n-quic proofs (vectored_copy), it is slightly faster with dynamic allocation:
Current:

SUMMARY:
 ** 0 of 820 failed

VERIFICATION:- SUCCESSFUL


        Command being timed: "cargo kani --function check_vectored_copy_u8 --unwind 5"
        User time (seconds): 1149.01
        Maximum resident set size (kbytes): 3733332

New:

SUMMARY:
 ** 0 of 910 failed

VERIFICATION:- SUCCESSFUL


        Command being timed: "cargo kani --function check_vectored_copy_u8 --unwind 5"
        User time (seconds): 1023.27
        Maximum resident set size (kbytes): 5487408

Memory usage seems to go up quite a bit though (3.7 GB to 5.5 GB).

Comment thread library/kani/src/slice.rs
impl<T, const MAX_SLICE_LENGTH: usize> Deref for NonDetSlice<T, MAX_SLICE_LENGTH> {
impl<T, const MAX_SLICE_LENGTH: usize> Drop for AnySlice<T, MAX_SLICE_LENGTH> {
fn drop(&mut self) {
if self.slice_len > 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm curious to know what happens if we remove this if. It's a bit counter intuitive that we are still calling alloc when slice_len = 0 but we don't call dealloc. I would expect some symmetry in those calls.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removing the if produces a failure of the form:

Check 35: std::alloc::dealloc.pointer_primitives.4
         - Status: FAILURE
         - Description: "pointer outside object bounds"
         - Location: ~/.rustup/toolchains/nightly-2022-03-23-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:105:14 in function std::alloc::dealloc

You're right about the asymmetry not looking correct: calling alloc with a zero size is actually undefined behavior (https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html#safety-1). I'll see if I can fix that. Might need to store a null pointer in the case of a zero slice!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Doesn't rust normally prefer to use a non-null but invalid value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added a commit that explicitly handles the slice_len == 0 case via a ptr::null().

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good! Thanks

Comment thread library/kani/src/slice.rs
assume(slice_len <= MAX_SLICE_LENGTH);
let layout = Layout::array::<T>(slice_len).unwrap();
let ptr = unsafe { alloc(layout) };
let any_slice = AnySlice::<T, MAX_SLICE_LENGTH>::alloc_slice();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

@zhassan-aws
zhassan-aws merged commit c65b3ec into model-checking:main Apr 14, 2022
@zhassan-aws
zhassan-aws deleted the iss1009 branch April 14, 2022 23:34
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 26, 2022
)

* Use dynamic allocation for non-deterministic slices

* Rename NonDetSlice to AnySlice
tedinski pushed a commit that referenced this pull request Apr 27, 2022
* Use dynamic allocation for non-deterministic slices

* Rename NonDetSlice to AnySlice
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.

AnySlice may yield unexpected results

4 participants