Challenge 13: contracts and proofs for CStr trait implementations and safe methods - #670
Challenge 13: contracts and proofs for CStr trait implementations and safe methods#670kasimte wants to merge 1 commit into
Conversation
|
Thanks @kasimte — this is a strong, sound submission for Challenge 13. It's complete and clean on our vacuity checks (no cfg(kani) body swaps, no trivial invariant, no decorative contracts, symbolic inputs), it's purely additive (+246/−0), it upgrades 7 of the 9 safe methods from plain proofs to real Heads-up for transparency: we reviewed all four open Challenge 13 solutions together, and we're prioritizing #638 in the review process as the front-runner — it's the most complete (it also contracts |
Towards #150.
This adds Challenge 13 work: the two trait implementations from criterion 4 had no verification, and the criterion 2 methods were exercised by test harnesses but carried no contracts stating what they guarantee.
This PR adds:
CloneToUninitandIndex<RangeFrom<usize>>, each proven withproof_for_contract. Slicing aCStrfrom an out-of-bounds position is documented to panic, and that panic path now has its own proof (should_panic).count_bytesagrees with the length of the byte view, and that the pointer fromas_ptrcovers the whole string including its final NUL byte.is_safeaccepts exactly the well-formed strings (non-empty, NUL at the end, no NUL in the middle) and rejects everything else — both outcomes proven reachable.kani::cover) next to every assumption, confirming the assumed input sets are non-empty.Invarianttrait forCStrCStrasserts it, and the fidelity check above comparesis_safeagainst its documented meaning (the trait implementation itself was already in-tree)from_bytes_until_nul,from_bytes_with_nul,count_bytes,is_empty,to_bytes,to_bytes_with_nul,as_ptr);bytesandto_strstay harness-proven, with the reason explained in a comment at each functionfrom_ptr,from_bytes_with_nul_unchecked,strlen)is_safepredicate their postconditions use (the contracts and proofs themselves were already in-tree, unchanged)CloneToUninit,Index<RangeFrom<usize>>)should_panicproofBounds: the challenge explicitly allows bounded harnesses ("Harnesses may be bounded"). Inputs go up to 32 bytes; the pre-existing
from_bytes_with_nulharness keeps its in-tree bound of 16, two new harnesses use 16, and one uses 8 to fit CI's per-harness time limit — each new bound explained in a comment where it appears.All 23 harnesses (22 in
ffi::c_str::verify, 1 inclone::verify) pass viascripts/run-kani.sh, with every cover property satisfied. TheUNREACHABLEproperties in the run output are library-internal helper checks and panic paths the proofs never reach. The change only adds code: 246 lines added, none removed.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.