Repository navigation
Use dynamic allocation for non-deterministic slices - #1011
Conversation
celinval
left a comment
There was a problem hiding this comment.
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?
| pub struct NonDetSlice<T, const MAX_SLICE_LENGTH: usize> { | ||
| arr: [T; MAX_SLICE_LENGTH], | ||
| layout: Layout, | ||
| ptr: *mut u8, |
There was a problem hiding this comment.
Good idea. This removed the need to have PhantomData.
| // created using kani::slice::any_slice | ||
|
|
||
| #[kani::proof] | ||
| fn check_out_of_bounds() { |
There was a problem hiding this comment.
Can you please add another test with a different T with size > 1 byte.
|
One more thing, I believe all these methods should either be marked as unsafe or T should implement Arbitrary type. |
|
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: 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
left a comment
There was a problem hiding this comment.
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?
|
|
||
| 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> { |
There was a problem hiding this comment.
Just wanna make sure you've seen my comments about making this method unsafe or using the Arbitrary trait.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
As discussed offline, changed T to require Arbitrary, and added an unsafe any_raw_slice version that doesn't require Arbitrary.
|
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. |
|
Performance does not seem to degrade with the switch to using dynamic allocation. In fact, on one of the s2n-quic proofs ( New: Memory usage seems to go up quite a bit though (3.7 GB to 5.5 GB). |
| 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Doesn't rust normally prefer to use a non-null but invalid value
There was a problem hiding this comment.
Indeed! NonNull::dangling() is the recommended approach on https://doc.rust-lang.org/core/slice/fn.from_raw_parts.html#safety.
There was a problem hiding this comment.
I added a commit that explicitly handles the slice_len == 0 case via a ptr::null().
| 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(); |
* Use dynamic allocation for non-deterministic slices * Rename NonDetSlice to AnySlice
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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.