Challenge 13: contracts and proofs for CStr trait implementations and safe methods - #670
Open
kasimte wants to merge 1 commit into
Open
Challenge 13: contracts and proofs for CStr trait implementations and safe methods#670kasimte wants to merge 1 commit into
kasimte wants to merge 1 commit into
Conversation
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.
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.