Skip to content

Challenge 27: Verify Arc and Weak safety with VeriFast and Kani - #817

Open
YuguangTong wants to merge 2 commits into
model-checking:mainfrom
YuguangTong:challenge-27-arc-verifast
Open

YuguangTong wants to merge 2 commits into
model-checking:mainfrom
YuguangTong:challenge-27-arc-verifast

Conversation

@YuguangTong

Copy link
Copy Markdown

Addresses #383 (Challenge 27: Verify atomically reference-counted Arc / Weak implementation in library/alloc/src/rcs/arc.rs).

Summary & Coverage

This PR verifies Arc, Weak, UniqueArc, and UniqueArcUninit in library/alloc/src/rcs/arc.rs using VeriFast 26.10 (concurrent separation logic + RustBelt semantic well-typedness) complemented by Kani 0.68.0 for the two dyn Any downcast methods currently unsupported by VeriFast's frontend (Dynamic types are not yet supported).

Challenge 27 Criterion (doc/src/challenges/0027-arc.md) VeriFast 26.10 Kani 0.68.0 Combined
Required pub unsafe fns (12 total) 11 / 12 1 / 12 (downcast_unchecked) 12 / 12 (100%)
Safe-with-unsafe functions (58 total; >= 75% / 44 required) 46 / 58 (79.3%) 1 / 58 (downcast) 47 / 58 (81.0%)
Total verified functions 107 (101 non-wrapper fns + 5 verified non-assume_correct allocation helpers + rc::is_dangling) 2 functions (4 harnesses) 109
Challenge 27 UB categories 5 / 5 (data races, dangling/misaligned, intrinsic UB, mutating immutable bytes, invalid values) Memory safety + non-vacuity (kani::cover) 5 / 5

Tool Split & Key Artifacts

  • VeriFast 26.10 (verifast-proofs/alloc/rcs/arc.rs/):
    • atomic_model/: Trusted -extern_spec for core::sync::atomic (Relaxed/Acquire/Release/AcqRel + fence(Acquire) pending_acquire + adversarial spurious CAS failure) + standalone 6-state Arc_inv protocol test (arc_weak_test.rs: 1,170 statements verified, 0 errors).
    • verified/arc.rs: 3,634 statements verified, 0 errors across 11/12 pub unsafe fns (full concurrent separation-logic contracts Arc_own, Arc_own_in, Arc_raw_own, Weak_own, Weak_raw_own, full_borrow_content), 46/58 safe-with-unsafe fns (proven unconditionally safe under RustBelt semantic well-typedness), and 50 additional concurrent/helper fns (Arc::{downgrade, is_unique, strong_count, weak_count, deref}, Weak::{new*, clone, strong_count, weak_count}, UniqueArc::{new*, try_new*, weak_count, assume_init}, etc.).
    • verify.sh: Runs VeriFast, refinement-checker (with exact full-body token equality enforced on BOTH verified/arc.rs and with-directives/arc.rs for the 23 frontend-limited functions), compat.patch check, and diff original/arc.rs ../../../../library/alloc/src/rcs/arc.rs (0 diff).
    • Hooked into verifast-proofs/check-verifast-proofs.sh, verifast-proofs/patch-verifast-proofs.sh (update.sh), and verifast-proofs/README.md. Detailed proof tour & caveats in verifast-proofs/alloc/rcs/arc.rs/README.md.
  • Kani 0.68.0 (library/alloc/src/rcs/arc.rs, +92/-0 lines, 100% #[cfg(kani)] / #[cfg_attr(kani, ...)]):
    • Contract on Arc<dyn Any + Send + Sync, A>::downcast_unchecked (#[cfg_attr(kani, requires((*self).is::<T>()))], #[cfg_attr(kani, ensures(|result| ub_checks::can_dereference(&**result as *const T)))], matching library/alloc/src/boxed/convert.rs).
    • mod verify: 3 #[kani::proof_for_contract] harnesses (i32 with symbolic value + refcount 1 & 2, bool, ZST ()), each passing a symbolic match/mismatch (if kani::any() { ... } else { Arc::new(0u16) }) so requires actively excludes invalid types while kani::cover witnesses non-vacuity, plus 1 #[kani::proof] harness check_downcast verifying both Ok and Err branches (2/2 kani::cover). Zero runtime code changes outside cfg(kani).

Trust Base & Audit Transparency

Full details are documented in verifast-proofs/alloc/rcs/arc.rs/README.md and verifast-proofs/alloc/rcs/arc.rs/atomic_model/TRUSTED-MODEL.md:

  • Weak-memory atomic model (atomic_model/TRUSTED-MODEL.md): Justified under C++20/Rust weak memory via RustBelt Relaxed (Dang et al., POPL 2020), enforcing has_release / has_acquire / pending_acquire + dual syntactic audit rules, backed by 6 negative soundness tests.
  • Localized assume inventory: Zero assume(false) in any executable function body and zero vacuous preconditions. Only 3 localized runtime assumes, strictly inside aborting > MAX_REFCOUNT overflow branches (Arc::clone, Weak::clone, UniqueArc::downgrade) matching Rust's documented < isize::MAX - 1 concurrent-thread assumption. Ghost lemma assumes are limited to VeriFast 26.10 prelude/?Sized/issue-Update Kani Metrics #610/alloc_id.lft == 'static gaps.
  • Extern-spec shims: 44 assume_correct __VeriFast_wrapper stdlib/intrinsic shims in verified/arc.rs (including Rust-trait-gated TrivialClone_slice_identity__VeriFast_wrapper<T: TrivialClone> and unforgeable ValidValueMetadata<T>), 2 in verified/lib.rs, and 2 in verified/arc_pin.rsspec (intrinsics::abort, Pin::new_unchecked — pinning-invariant caveat documented in README.md).
  • compat.patch: Minimal 6-line nightly-2026-09-25 shim (checked against original/arc.rs on every run).

Local Verification & Mutation Stress Testing

  • bash verifast-proofs/check-verifast-proofs.sh: exit 0 (linked_list.rs + raw_vec/mod.rs + rcs/arc.rs/atomic_model 1,170 stmts + rcs/arc.rs 3,634 stmts + refinement-checker + all 3 diffs).
  • bash verifast-proofs/check-verifast-proofs-negative.sh: exit 0.
  • ./scripts/run-kani.sh on all 4 rcs::arc::verify::* harnesses: exit 0 (4/4 verified, 0 failures, all kani::cover properties satisfied).
  • 10/10 mutation stress tests passed (on temporary copies): 7/7 VeriFast concurrency/safety mutations rejected (Arc::drop Release->Relaxed, missing fence(Acquire), Arc::is_unique strong.load Acquire->Relaxed & weak.store Release->Relaxed, Weak::inner skipping is_dangling, Arc::get_mut skipping is_unique, Weak::upgrade allowing n == 0) and 3/3 Kani contract/vacuity mutations caught (mismatched concrete type -> 0/1 cover, removed requires -> verification failure, negated ensures -> verification failure).

@YuguangTong
YuguangTong marked this pull request as ready for review December 3, 2026 00:18
@YuguangTong
YuguangTong requested a review from a team as a code owner December 3, 2026 00:18

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant