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.

This proves the loop in strlen's if const arm for strings of any length, using a loop invariant instead of an unwind bound.

Changes

  • Commit 1 moves the loop, unchanged, into strlen_loop so a harness can call it. Items inside const_eval_select! can't be named from outside. The compiled code is identical.
  • Commit 2 adds a loop invariant and the harness check_strlen_loop_unbounded. Both are Kani-only.

How it works

The harness allocates a buffer of any size up to 2^48 bytes and picks any nul in it, nul_at. The loop invariant is len <= nul_at. Since nul_at can be any nul, len <= nul_at at the end means len is the first one.

Planting bugs in the loop (e.g. len += 2, or != 1 instead of != 0) makes the harness fail.

Not proven

  • Buffers over 2^48 bytes, the largest object CBMC can address with --object-bits 12
  • Termination
  • Uninitialized reads (-Z uninit-checks is off in CI)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

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

Items inside `const_eval_select!` can't be named from outside, so a
harness can't reach the loop. Move it, unchanged, into `strlen_loop`.
No change in behaviour.
Add a loop invariant to `strlen_loop` and a harness that checks it for
strings of any length. Both are Kani-only.
@larkindaniel
larkindaniel force-pushed the cstr-strlen-real-loop branch from f298b0a to 7b4315f Compare December 3, 2026 05:42

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