Repository navigation
Challenge 13: unbounded verification of strlen's loop - #798
larkindaniel wants to merge 2 commits into
Conversation
The `if const` arm of `strlen` contains the loop that computes a C string's length during const evaluation. Items inside the arms of `const_eval_select!` cannot be named from outside the macro, so a verification harness cannot reach that loop. Move the loop, unchanged, into a private `const unsafe fn strlen_loop`, and call it from the `if const` arm. The runtime arm, which calls C's `strlen`, is untouched. This does not change behaviour: the loop is the same code, and it still runs only during const evaluation. Code that uses `CStr` compiles to byte-identical object files before and after this commit, at -O0 and -O3 (runtime calls and const-evaluated lengths both).
Add a loop invariant to `strlen_loop` and a harness, `check_strlen_loop_unbounded`, that verifies it for strings of any length: a buffer of symbolic size (up to 2^48 bytes), with no `kani::unwind` bound. The harness assumes `strlen`'s safety precondition (a nul within `isize::MAX` bytes, readable up to it) and asserts its postcondition (the result is the offset of the first nul). The invariant bounds `len` by a ghost nul offset that the harness chooses, which keeps every read in bounds. Everything added is Kani-only: a `#[safety::loop_invariant]` on the loop, which expands to nothing outside Kani, a `#[cfg(kani)]` import, and items in the existing `#[cfg(kani)] mod verify`. Normal builds are unaffected.
|
Some context here: @larkindaniel is working with me. I was wanting to see whether strlen could be unboundedly verified (I thought it could be), even if that wasn't a condition for the original challenge as stated. However, this PR changes the upstream code; perhaps the relevant changes could be mergeable upstream. We talked about the approach in #681, and could follow it more closely as well. In terms of improving the verification, this PR verifies more than we had collectively verified in the past, so I would say that it's a step forward, but not unmitigated, because of the upstream change. And, to be fair, it also doesn't verify the FFI branch. That's out of scope of verify-std in general, even if it is the more likely threat to actual safety. Would welcome thoughts by @feliperodri and others as well. |
Towards #150. Follow-up to #638, which verifies
strlenwith bounded harnesses (strings of up to 32 bytes). This adds a proof of the loop instrlen'sif constarm for strings of any length, with a loop invariant instead of an unwind bound. The proof runs the shipped loop, not a copy of it.Changes
All changes are in
library/core/src/ffi/c_str.rs. They're in two commits so the first can be checked on its own.const unsafe fn strlen_loop, which theif constarm calls. Items inside the arms ofconst_eval_select!can't be named from outside the macro, so a harness can't reach the loop where it was. The runtime arm (C'sstrlen) is untouched.git show --color-movedshows the loop as moved lines#[safety::loop_invariant]on the loop (lenhasn't passed the nul the harness chose, and no byte beforelenis nul), and the harnesscheck_strlen_loop_unboundedin the existingmod verifystrlen's contract and the existing CStr harnesses are unchanged.Harness
check_strlen_loop_unboundedkani::unwind: assumingstrlen's safety precondition (a nul withinisize::MAXbytes, readable up to it),strlen_loopreturns the offset of the first nul (strlen's postcondition)The string starts at the start of the buffer. This loses no generality: the loop only reads forward from
s, so a string at offsetkof ann-byte buffer reads the same bytes as one at the start of ann - k-byte buffer.Mutation testing
Each of these bugs, planted locally (they aren't part of this PR), makes the harness fail:
len += 2instead oflen += 1!= 1instead of!= 0in the loop testlen + 1||instead of&&lenremovedNo change in behaviour
#[safety::loop_invariant]expands to nothing outside Kani, and the rest is a#[cfg(kani)]import and items inside the existing#[cfg(kani)] mod verify.CStr(from_ptr,count_bytes,to_bytes, and aconstevaluated throughfrom_ptr) compiles to byte-identical object files againstcorefrommain, from commit 1 and from commit 2, at-O0and-O3(built with-Zlocation-detail=none, so panic line numbers don't differ).Not proven
--object-bits 12that CI passes. #681 and #758 use the same cap__rust_allocis uninitialized, and CI doesn't enable-Z uninit-checks. Under that flag the harness would need an initialized bufferAll 15 CStr harnesses pass with the flags in
scripts/run-kani.shand the pinned Kani (87c8701), after each commit (14 after commit 1).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.