Prove code with CBMC and Lean 4#
A test checks the inputs someone thought of, and a fuzz target the ones it happens to find. shirudo’s proofs check all of them, in two ways that complement each other:
CBMC proves properties of the C itself. The proofs under
testing/proofs/cbmc/turn shirudo’s C and a harness into a formula and ask a SAT solver for an input that breaks a property; when none exists, the property holds for every input within the harness’s bounds.Lean 4 proves properties of a model of an algorithm. The proofs under
testing/proofs/lean4/state the algorithm in Lean and prove it meets its specification for every input, of any length, without a bound. Lean does not read shirudo’s C: a CBMC proof checks the C against the model within its bounds, where one does.
A CBMC proof states two kinds of property. CBMC adds the first to every proof: no read or write outside an object, no null or dangling pointer, no signed overflow, no shift past the width of a type, no division by zero, no conversion that changes a value, and no loop that runs longer than the harness allows. The harness states the second: what the code must do, such as that a parser accepts exactly the well-formed inputs and that every offset it returns lies inside the input. A CBMC proof covers every input up to the bound its harness names. Where the format itself bounds what the code reads, as a fixed-size header does, the proof covers every input of any size; elsewhere it covers every input up to the stated length, and Proof coverage says which is which.
Before you begin#
CBMC 5.95 or newer, packaged as
cbmcby Debian and Ubuntu. CI uses 5.95.1, as Run the proofs as CI does shows.For the Lean 4 proofs, elan, packaged as
elan, and the toolchaintesting/proofs/lean4/lean-toolchainnames:$ elan toolchain install "$(cat testing/proofs/lean4/lean-toolchain)"
Memory: most CBMC proofs need little, but a large one can take several gigabytes.
Run the proofs#
Run all of them from the checkout:
$ make proofs
Each CBMC line names a proof, the number of checks CBMC made and the time it took, and
proofs: <n> holdends a run in which all of them held.proofs: <n> Lean 4 modules hold, checked again by two kernelsends the Lean 4 run. The proofs take hours of CPU, a few of them minutes and gigabytes each, and CI runs them only once a change is on main. Before you send a change, run the ones that cover the code you change, as the next step shows, and all of them after a change toproof.horrun.sh.Run one kind, some CBMC proofs by name, or the CBMC proofs that read a file you changed:
$ make proofs-lean4 $ make proofs-cbmc PROOFS="string-number upatch-header" $ make proofs-cbmc CHANGED=origin/main..HEAD
CHANGEDtakes whatgit difftakes, a range of commits or one commit to compare the working tree with. The compiler lists the files each harness includes, and a proof runs when one of them changed; a change torun.shorproofs.listruns every proof. A run that finds none printsproofs: none reads a file changed in <range>. No run checks a Lean 4 model against the C it follows by review: read the model against the change.Read why a CBMC proof failed:
$ testing/proofs/cbmc/run.sh -v string-number
-vprints CBMC’s whole output, with the trace of each failed check: the input that breaks it and the steps that lead there. Lean prints the theorem and the goal it could not close.
Run the proofs as CI does#
The Proofs workflow, .github/workflows/proofs.yml, runs after
every push to main, never on a pull request, so the run before you send
a change is the only one it gets before it lands. Its Lean4 job runs
make proofs-lean4 with the toolchain lean-toolchain names. Its
CBMC job runs in eight shards: shard <k> takes every eighth proof
of testing/proofs/cbmc/proofs.list from the <k>-th and runs
each alone, under a cap of 6 GB and 60 minutes, so a proof you add has
to fit both on a runner. CI pins CBMC 5.95.1 and elan in
.github/scripts/proofs.sh, and installs the headers of libcurl,
OpenSSL and zlib that the harnesses include with its
install-headers, which a host that builds shirudo already has.
Install the same CBMC into
build/cbmc, without root, and put it on yourPATH:$ export PATH="$(.github/scripts/proofs.sh install-cbmc):$PATH"
List the proofs of a shard, or run them:
$ .github/scripts/proofs.sh list 3 8 $ .github/scripts/proofs.sh run 3 8
A proof that holds prints its
okline. One that fails printsFAILED <proof>:and why, then CBMC’s whole output with the trace, whichrunkeeps inbuild/proofs/<proof>.log. In CI that output is in the shard’s job log and in itscbmc-logs-<k>artifact.
CI leaves out the proofs testing/proofs/cbmc/ci-skip.list names,
each with why: a proof that needs more memory or time than CI gives one.
No run checks them but one you start. After a change to a file one of
them reads, which CHANGED finds, run it by hand with its memory
capped:
$ ulimit -v 24000000
$ make proofs-cbmc PROOFS=<proof>
Write a CBMC proof#
Write
testing/proofs/cbmc/<name>.c. It includesproof.hand the source file it proves, so that it reaches static functions too, and definesharness(), which CBMC runs with every input at once.any_bytes(n)givesnbytes of any contents,any_input(len, max)an input of any length for code that reads at mostmaxbytes of it, and a variable left uninitialized takes any value;__CPROVER_assume()narrows the inputs to the bound, and__CPROVER_assert()states each property with a message that says what holds.State what the code must do from its contract, not from its code: an accepted string is a number of the right digits, an accepted file has every section inside it. A check that restates the implementation proves nothing about it.
Add the proof to
testing/proofs/cbmc/proofs.listwith its CBMC options:--unwind <n>for loops,--unwindset <loop>:<n>for one loop, withgoto-instrument --show-loopsnaming them. The unwinding assertionsrun.shadds fail when an input needs more iterations, so a bound too low fails the proof rather than weakening it.LEN=<a>-<b>, or a list of lengths and ranges such asLEN=0-24,28,32, runs the proof once for each length, withLENdefined: CBMC takes far longer over an input whose length it has to keep symbolic.--object-bits <n>when CBMC reports too many addressed objects.
Check that the proof can fail: break the code it proves, for instance a loop that reads one byte too many, run it and see it fail, then restore the code.
Add a row to Proof coverage, and take a file the harness is the first to read off that page’s list of files without a proof.
A function without a body in the proof, a libc one CBMC does not model or
a system call, returns any value, and run.sh fails a proof that calls
one. Give the function a model in proof.h, or keep the proof away from
the code that calls it. CBMC 5.95’s own memcpy() reads wrong bytes from
a string literal that symbolic data picks among several, and fails a proof
the code meets; a harness that copies from such a pointer defines
PROOF_MEMCPY_BYTEWISE before it includes proof.h. A harness whose
code searches thousands of bytes with memchr() only to learn whether
a byte is there defines PROOF_MEMCHR_QUANTIFIED for a model that
answers without a loop. PROOF_FILE models a file of that many bytes
that getline() and pread() read, and an errno that keeps what
was stored, where CBMC’s own takes any value and lets a loop that
retries on EINTR run for ever; PROOF_DEVICE_NUMBERS gives
makedev(), major() and minor() bodies.
Write a Lean 4 proof#
Write the model in
testing/proofs/lean4/Shirudo/<Name>.leanas close to the C as Lean allows: the same steps, the same checks, the values the C can hold. State each theorem as the specification the algorithm must meet for every input, and assume what the C cannot prove, such as a hash being collision resistant, as an explicit hypothesis of the theorem.Import the module in
testing/proofs/lean4/Shirudo.lean. The build checks every module underShirudo/whether imported or not, and asorryor any other warning fails it, so every theorem is proved in full.testing/proofs/lean4/run.shthen checks each proof again, with Lean’s kernel and with nanoda, a second kernel written apart from Lean. Neither trusts the tactics that built the proof or a build CI restored from its cache, and nanoda admits only the axiomstesting/proofs/lean4/nanoda.jsonpermits:propext,Classical.choiceandQuot.sound. Anative_decide, which rests on the compiler, or anaxiomof the model’s own stops nanoda withdeclaration not foundand its name; prove the step withdecide,omegaorsimp.Where CBMC can, add a CBMC proof that the C computes what the model computes, for every input up to a bound, as
testing/proofs/cbmc/string-number-model.cdoes for the number parser; elsewhere the model follows the C by review, and the coverage page says so.Add a row to Proof coverage.
Limits#
A CBMC proof is about the code CBMC sees: the C of the files the harness
includes, with -funsigned-char as the build sets it, against a model
of the libc functions it calls. It says nothing about the kernel, the
compiler’s code generation, time, or other threads, and it holds only
within its bounds. A Lean 4 proof holds for every input, but of the model:
it carries over to the C as far as the model matches the C, and its
assumptions hold.