Skip to content

Challenge 8: VeriFast solution for smallsort - #762

Open
haoran-ding wants to merge 4 commits into
model-checking:mainfrom
haoran-ding:challenge-8-smallsort-verifast
Open

haoran-ding wants to merge 4 commits into
model-checking:mainfrom
haoran-ding:challenge-8-smallsort-verifast

Conversation

@haoran-ding

@haoran-ding haoran-ding commented Nov 16, 2026 •

Copy link
Copy Markdown

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_sort
    • swap_if_less
    • insertion_sort_shift_left
    • sort4_stable
    • has_efficient_in_place_swap

    All 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_down and panic_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_less swaps exactly when v[b] < v[a], and has_efficient_in_place_swap returns size_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 under verified/: 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/, and rewritten/ in the role of RawVec's with-directives/). verify.sh checks the following:

  1. VeriFast reports no errors for each crate.
  2. Every crate refines rewritten/ according to refinement-checker.
  3. original/smallsort.rs is identical to library/core/src/slice/sort/shared/smallsort.rs.
  4. The trusted blocks (marked // ===== BEGIN TRUSTED ... =====) are the same in every crate, and assume and assume_correct appear nowhere else.
  5. The spec of the freeze small-sort wrapper is textually identical to the spec that both impls of UnstableSmallSortFreezeTypeImpl::small_sort are verified against.

update.sh is hooked into patch-verifast-proofs.sh, as for the other proofs.

Code changes

  1. Calls through wrapper functions. refinement-checker checks these, as in the LinkedList proof. They cover:
    • comparator calls;
    • as_mut_ptr;
    • wrapping_sub/wrapping_add;
    • ptr::copy(.., .., 1);
    • MaybeUninit::uninit;
    • len + 16;
    • the generic call to UnstableSmallSortFreezeTypeImpl::small_sort.
  2. Two for loops written as their own desugaring, so that the loop invariant can name the iterator. refinement-checker checks these too.
  3. One change that refinement-checker cannot check. It is the entire difference between original/ and rewritten/ (4 lines; the diff is in the README).
    • The change: at the end of insert_tail, the implicit drop of the CopyOnDrop guard is written out as its body (ptr::copy_nonoverlapping(src, dst, len)) followed by mem::forget(gap_guard).
    • Why it is needed: VeriFast 26.10 models implicit drops through the generic spec of drop_in_place, which loses the write that completes the insertion.
    • Why it is runtime-equivalent: on the normal path the drop runs exactly that body on the current field values; CopyOnDrop has 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 TRUSTED blocks and std_extra.rsspec)

  • Comparator model. Two uninterpreted functions: is_less_models(f) ("f is well-behaved") and is_less_model_of(f) (the order f computes). 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.
  • Specs or wrappers for std functions that VeriFast 26.10 does not specify. These are select_unpredictable, abort, is_multiple_of, slice::from_raw_parts, as_mut_ptr, wrapping_*, MaybeUninit::uninit and the [0, b] array iterator.
  • A ptr::copy wrapper. VeriFast 26.10's own ptr::copy spec does not bound the offset in its overlapping case, which lets a proof derive false. The wrapper's spec bounds the offset.
  • Model axioms. These cover the layout of MaybeUninit<[T; N]>, the ownership predicate of MaybeUninit<T>, and 48 * size_of::<T>() <= isize::MAX for the Ts that instantiate [T; 48].
  • The wrapper for the generic freeze small_sort call. Its spec is checked to be textually identical to the spec that both impls of UnstableSmallSortFreezeTypeImpl are verified against.

Caveats

  • Unwind paths are not verified. This is -ignore_unwind_paths, as in the accepted LinkedList and RawVec proofs. In particular, the panic-safety logic (the CopyOnDrop guards restoring the slice when a comparator panics) is not verified; the results hold for panic=abort.
  • No Tree Borrows checks at reference creation. This is -ignore_ref_creation: VeriFast 26.10 cannot produce the ref_initialized fact needed for &local. Accesses are still checked for dangling pointers, alignment, initialization and write permission.
  • Correctness assumes a well-behaved comparator, but memory safety does not.
  • Termination is not proven (partial correctness only).
  • Stability is not specified.
  • VeriFast is not foundational and has known unsoundnesses.

Findings

The following are not UB, and are unreachable from the public sort APIs, which return early for zero-sized T:

  • For zero-sized T with len > usize::MAX - 16, the len + 16 in small_sort_general_with_scratch overflows.
  • For zero-sized T with 18 <= len <= 32, small_sort_network does 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.

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
@haoran-ding
haoran-ding requested a review from a team as a code owner November 16, 2026 03:27
@haoran-ding haoran-ding changed the title VeriFast solution for Challenge 8 (smallsort) Challenge 8: VeriFast solution for smallsort Nov 16, 2026
…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.
@feliperodri feliperodri added the Challenge Used to tag a challenge label Nov 16, 2026
@feliperodri feliperodri self-assigned this Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Challenge Used to tag a challenge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Challenge 8: Contracts for SmallSort

2 participants