Repository navigation
Challenge 8: VeriFast solution for smallsort - #762
Open
haoran-ding wants to merge 4 commits into
Open
haoran-ding wants to merge 4 commits into
haoran-ding wants to merge 4 commits into
Conversation
Adds a VeriFast 26.10 proof of absence of UB for the seven functions of Challenge 8, and of sortedness + permutation contracts for the three small_sort trait methods (all impls), in verifast-proofs/core/slice/sort/shared/smallsort.rs, and runs it in the VeriFast CI job. See that directory's README for the tour, trusted base and caveats. Resolves model-checking#56
…VeriFast proofs Keep only original/, rewritten/, verified/<crate>/, std_extra.rsspec, README.md, verify.sh and update.sh; drop the auxiliary tooling, audit scripts and logs. verify.sh keeps the checks that the trusted blocks are the same in every crate, that assume appears nowhere else, and that the freeze small-sort wrapper's spec is the one its two impls are verified against. Comments only in the crates.
…to challenge-8-smallsort-verifast
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a VeriFast 26.10 proof for Challenge 8: Contracts for SmallSort, in
verifast-proofs/core/slice/sort/shared/smallsort.rs/, and adds it to the VeriFast CI job (verifast-proofs/check-verifast-proofs.sh). A tour of the proof, its trusted base and its caveats is in that directory's README.What is proven
The success criteria are met, subject to the caveats below.
Absence of UB. For all slice lengths and every comparator (including comparators that are not orders or that mutate their own state), on all normal-return paths (what happens after a comparator panics is not verified, see Caveats), the proof covers:
<T as StableSmallSortTypeImpl>::small_sort<T as UnstableSmallSortTypeImpl>::small_sort<T as UnstableSmallSortFreezeTypeImpl>::small_sortswap_if_lessinsertion_sort_shift_leftsort4_stablehas_efficient_in_place_swapAll their callees in the module are verified against specs, and none is assumed. The callees are
insert_tail,small_sort_fallback,small_sort_general,small_sort_general_with_scratch,small_sort_network,sort8_stable,sort9_optimal,sort13_optimal,bidirectional_merge,merge_up,merge_downandpanic_on_ord_violation.Contracts that show the three
small_sorts sort. This holds for every impl of each trait (default and specialized). For every comparator that implements a strict weak order, the postcondition states that the slice afterwards is a permutation of the input, and that it is sorted by that order (is_perm(vs1, vs) && sorted_by(lt, vs1)). The other four functions get functional specs as well. For example,swap_if_lessswaps exactly whenv[b] < v[a], andhas_efficient_in_place_swapreturnssize_of::<T>() <= 8.Structure
VeriFast 26.10 gives the default and the specialized impl of a trait method the same name (
<T as Trait>::small_sort), so it cannot hold specs for both in one crate. The proof is therefore split into 10 crates underverified/: one per entry function, and one per impl for the three trait methods. Each crate carries the same spec for its impl. The layout otherwise follows the existing proofs (original/,verified/, andrewritten/in the role of RawVec'swith-directives/).verify.shchecks the following:rewritten/according torefinement-checker.original/smallsort.rsis identical tolibrary/core/src/slice/sort/shared/smallsort.rs.// ===== BEGIN TRUSTED ... =====) are the same in every crate, andassumeandassume_correctappear nowhere else.UnstableSmallSortFreezeTypeImpl::small_sortare verified against.update.shis hooked intopatch-verifast-proofs.sh, as for the other proofs.Code changes
refinement-checkerchecks these, as in the LinkedList proof. They cover:as_mut_ptr;wrapping_sub/wrapping_add;ptr::copy(.., .., 1);MaybeUninit::uninit;len + 16;UnstableSmallSortFreezeTypeImpl::small_sort.forloops written as their own desugaring, so that the loop invariant can name the iterator.refinement-checkerchecks these too.refinement-checkercannot check. It is the entire difference betweenoriginal/andrewritten/(4 lines; the diff is in the README).insert_tail, the implicit drop of theCopyOnDropguard is written out as its body (ptr::copy_nonoverlapping(src, dst, len)) followed bymem::forget(gap_guard).drop_in_place, which loses the write that completes the insertion.CopyOnDrophas no fields with drop glue; nothing in between can panic; and the unwind paths are unchanged. The full argument is in the README. We also compared both versions on 12000 random runs, including panicking and inconsistent comparators and element types with destructors, in all four combinations of overflow-checks and debug-assertions; the traces were identical.No change is made to
library/.Trusted base (the
TRUSTEDblocks andstd_extra.rsspec)is_less_models(f)("fis well-behaved") andis_less_model_of(f)(the orderfcomputes). Three axioms make that order a strict weak order. A wrapper links every real comparator call to the model; it has no free-argument lemma.select_unpredictable,abort,is_multiple_of,slice::from_raw_parts,as_mut_ptr,wrapping_*,MaybeUninit::uninitand the[0, b]array iterator.ptr::copywrapper. VeriFast 26.10's ownptr::copyspec does not bound the offset in its overlapping case, which lets a proof derivefalse. The wrapper's spec bounds the offset.MaybeUninit<[T; N]>, the ownership predicate ofMaybeUninit<T>, and48 * size_of::<T>() <= isize::MAXfor theTs that instantiate[T; 48].small_sortcall. Its spec is checked to be textually identical to the spec that both impls ofUnstableSmallSortFreezeTypeImplare verified against.Caveats
-ignore_unwind_paths, as in the accepted LinkedList and RawVec proofs. In particular, the panic-safety logic (theCopyOnDropguards restoring the slice when a comparator panics) is not verified; the results hold forpanic=abort.-ignore_ref_creation: VeriFast 26.10 cannot produce theref_initializedfact needed for&local. Accesses are still checked for dangling pointers, alignment, initialization and write permission.Findings
The following are not UB, and are unreachable from the public sort APIs, which return early for zero-sized
T:Twithlen > usize::MAX - 16, thelen + 16insmall_sort_general_with_scratchoverflows.Twith18 <= len <= 32,small_sort_networkdoes not terminate.CI cost
Seven crates take 5–10 s each. The three crates with the merge/network proofs take about 26–31 min each on one core, so the VeriFast job grows by about 1.5 h, which is within the default 6 h job limit. Each VeriFast run is capped by
VF_TIMEOUT(default 5400 s).Resolves #56
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.