Skip to content

Challenge 13: unbounded verification of strlen's loop - #798

Draft
larkindaniel wants to merge 2 commits into
model-checking:mainfrom
larkindaniel:cstr-strlen-real-loop
Draft

larkindaniel wants to merge 2 commits into
model-checking:mainfrom
larkindaniel:cstr-strlen-real-loop

Conversation

@larkindaniel

@larkindaniel larkindaniel commented Nov 27, 2026 •

Copy link
Copy Markdown

Towards #150. Follow-up to #638, which verifies strlen with bounded harnesses (strings of up to 32 bytes). This adds a proof of the loop in strlen's if const arm 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.

Commit Change
1 Moves the loop, unchanged, into const unsafe fn strlen_loop, which the if const arm calls. Items inside the arms of const_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's strlen) is untouched. git show --color-moved shows the loop as moved lines
2 Kani only: a #[safety::loop_invariant] on the loop (len hasn't passed the nul the harness chose, and no byte before len is nul), and the harness check_strlen_loop_unbounded in the existing mod verify

strlen's contract and the existing CStr harnesses are unchanged.

Harness

Harness What it shows
check_strlen_loop_unbounded For a buffer of any size up to 2^48 bytes and a nul anywhere in it, no kani::unwind: assuming strlen's safety precondition (a nul within isize::MAX bytes, readable up to it), strlen_loop returns the offset of the first nul (strlen's postcondition)
Covers An empty string, and the longest string in the largest buffer, are both reachable

The string starts at the start of the buffer. This loses no generality: the loop only reads forward from s, so a string at offset k of an n-byte buffer reads the same bytes as one at the start of an n - k-byte buffer.

Mutation testing

Each of these bugs, planted locally (they aren't part of this PR), makes the harness fail:

Where Planted bug
Loop len += 2 instead of len += 1
Loop != 1 instead of != 0 in the loop test
Loop Returns len + 1
Invariant || instead of &&
Invariant The bound on len removed
Harness The ghost nul offset never set

No change in behaviour

  • Commit 2 adds nothing that a normal build compiles: #[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.
  • Commit 1 is a pure move. As a check, a crate that uses CStr (from_ptr, count_bytes, to_bytes, and a const evaluated through from_ptr) compiles to byte-identical object files against core from main, from commit 1 and from commit 2, at -O0 and -O3 (built with -Zlocation-detail=none, so panic line numbers don't differ).

Not proven

What Why
Buffers over 2^48 bytes The harness caps the buffer there, so that it fits in the largest object CBMC can address with the --object-bits 12 that CI passes. #681 and #758 use the same cap
Termination Loop invariants show that every run of the loop that ends returns the right value, not that it ends
Uninitialized reads The buffer from __rust_alloc is uninitialized, and CI doesn't enable -Z uninit-checks. Under that flag the harness would need an initialized buffer

All 15 CStr harnesses pass with the flags in scripts/run-kani.sh and 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.

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.
@patricklam

patricklam commented Nov 27, 2026 •

Copy link
Copy Markdown

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.

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.

2 participants