cverif 0.0.1

Verifies that compiled machine code faithfully matches its C source
Documentation

cverif

CI crates.io docs.rs License: MIT

cverif checks the work of a C compiler. Given a C source tree and a compiled binary, it proves that the machine code computes exactly what the source says — for every input, not for a sample of inputs — and reports how much of the source and of the binary the proofs cover.

It answers two questions at once:

  • Did the compiler do its job? A miscompilation shows up as a function whose machine code provably differs from the source, with a concrete input that demonstrates the difference.
  • Is this binary really built from this source? Code in the binary that no proof reaches (a patched function, an added trigger value, a changed table) lowers the binary coverage or is reported as a mismatch.

cverif does not contain or invoke a compiler and does not trust one. The first supported target is ARM Thumb (Cortex-M0/M0+/M3/M4/M35P), as produced by GCC (arm-none-eabi-gcc -mthumb) or clang (--target=arm*-none-eabi), including position-independent code built with -fropi and -frwpi.

$ arm-none-eabi-gcc -mthumb -mcpu=cortex-m3 -O2 -c control.c
$ cverif control.o control.c
PROVED       nested            28 bytes     7 paths       0 ms
PROVED       ternary_chain     28 bytes     4 paths       0 ms
PROVED       goto_cleanup      18 bytes     3 paths       0 ms
PROVED       classify          20 bytes     7 paths       0 ms
PROVED       dense_switch      50 bytes     8 paths       0 ms
PROVED       sum_to_10          4 bytes     1 paths       0 ms
PROVED       sum_squares_8     12 bytes     1 paths       0 ms
PROVED       popcount8         20 bytes     9 paths       1 ms
PROVED       first_set         30 bytes    33 paths       8 ms
PROVED       digits            32 bytes    10 paths      23 ms
BOUNDED      sum_n             24 bytes    40 paths      62 ms  loop bound (40 branches per path); 40 paths proved
BOUNDED      gcd               24 bytes     6 paths    7466 ms  time limit; 6 paths proved

functions:       10 proved, 2 bounded, 0 mismatched, 0 inconclusive, 0 unsupported
source coverage: 100.0%  (71 of 71 statements and branches proved)
binary coverage:  99.0%  (287 of 290 code bytes proved)
initialised data: 0 objects compared, 0 differ
compiler:        GCC: (Gentoo 16.2.1_p20260926 p1) 16.2.1 20260926
optimisation:    -O2 (70%)
score:           99.5 / 100
verdict:         no difference found

A binary built from a source with one extra line — if (key == 0x5ec2e7u) return 1; — is rejected, and the counterexample is the trigger value itself:

MISMATCH     m_backdoor     28 bytes     2 paths       0 ms  return value: source gives 0x0, binary gives 0x1 for r0=0x5ec2e7 r1=0xfffff000
...
score:           0.0 / 100
verdict:         MISMATCH - the binary does not match the source

Usage

cverif [OPTIONS] <BINARY> <SOURCE>...

<BINARY> is an ELF file with symbols — a relocatable object or a linked executable — or a raw binary together with --symbols <file> (the output of nm -S: address [size] type name per line) and --base <hex>. <SOURCE> is any number of C files or directories. -I and -D work as they do for a compiler. -v lists the source lines and binary functions that no proof covers. For linked or raw code built with -frwpi, give the data base address the firmware loads into r9 with --static-base <hex> (relocatable objects need nothing). cverif --help shows the remaining options.

Coverage output

Three optional files describe exactly what the proofs cover:

Option Format Contents
--lcov <file> LCOV tracefile Source side, for genhtml, CI coverage services and editor plugins. A line is hit only when every statement and branch condition on it lies on a proved path; lines with several coverage points also get one branch record per point.
--drcov <file> drcov (version 2) Binary side, for disassembler coverage plugins: one block per proved address range.
--json <file> cverif's own Everything: per-function status and counterexample, source line ranges (proved / partial / unproved), binary address ranges (proved / unproved / padding) with section, enclosing function and offsets, and the data comparison.

For a relocatable object, addresses are positions in cverif's own layout of the sections; use the section and symbol offsets in the JSON instead. For a linked ELF or a raw binary they are the real addresses.

The exit status is 0 when no difference was found, 1 when the binary provably differs from the source, and 2 for usage or input errors.

What the output means

Per function:

Status Meaning
PROVED Every path through the function was explored on both sides and every effect was proved equal.
BOUNDED No difference exists on the explored paths, but exploration stopped at a limit — typically a loop whose trip count depends on the input. The proof covers the explored trip counts only.
MISMATCH The machine code provably behaves differently. The message gives a concrete input.
INCONCLUSIVE The prover ran out of budget on some obligation. Nothing is claimed either way.
UNSUPPORTED The function uses something cverif does not model yet.

Overall:

  • Source coverage — the share of statements and branch conditions that lie on a proved path.
  • Binary coverage — the share of code bytes (instructions and literal pools, excluding alignment padding) that lie on a proved path. Bytes that no proof reaches are exactly the bytes nothing vouches for.
  • Initialised data — the initial contents of every object the source defines are compared with the image.
  • Score — the mean of the two coverages, or 0 as soon as anything provably differs.
  • Optimisation — a best guess at the -O level from the shape of the code, with ties shown when levels produce indistinguishable code. Flags recorded in debug info are reported separately as declared, because they are a claim made by the binary, not evidence.

How it works

Both the C function and the machine code are executed symbolically over the same symbolic inputs: the argument registers and stack slots defined by the AAPCS, the initial contents of memory, and the results of calls to functions defined elsewhere. Every value either side computes is a term in one shared, hash-consed bit-vector language whose constructors normalise aggressively, so the two sides usually arrive at the same term. What normalisation cannot settle goes to a built-in prover: terms are bit-blasted into an incremental CDCL SAT solver.

For each path through the C function, the machine code is executed under that path's condition; where the machine code branches on something the condition does not determine, both directions are explored. At the end of every path — and at every call to an external function — cverif proves that:

  • the return value is the same;
  • every memory location either side wrote holds the same value;
  • external calls happen in the same order with the same arguments;
  • callee-saved registers and the stack pointer are restored.

The C side records the conditions under which the program has defined behaviour (no signed overflow, no division by zero, in-range shifts, in-bounds accesses to known objects). The machine code only has to match on inputs that satisfy them, which is precisely the freedom the C standard gives a compiler.

A reported counterexample is always a real model from the solver; cverif never samples inputs or runs the code.

Some arithmetic that compilers produce is out of reach for a SAT solver. For those the normaliser carries rewrite rules that are theorems checked on each use — for example, division by a constant compiled into a multiplication by a reciprocal is recognised and turned back into a division only when the exact Granlund–Montgomery condition holds.

Current limits

cverif is at an early stage. Known gaps are reported as UNSUPPORTED, INCONCLUSIVE or BOUNDED rather than silently accepted, except where noted:

  • Loops with an input-dependent trip count are proved up to an exploration bound, not by induction.
  • Floating point, variadic function definitions, inline assembly, volatile bit-fields, and structures with padding passed by value to external functions.
  • A local variable whose address is stored in a global. (Passing the address of a local to an external function is supported.)
  • Volatile objects: every access the source makes must be reproduced in order, but an extra read by the binary of a location the source never touches is not detected.
  • After a local's address has been passed to an external function, pointers obtained later from unknown memory are still assumed not to alias it.
  • Type-based alias analysis and restrict: where a compiler relies on them, cverif may report a mismatch for an aliasing input that the C standard rules out.
  • Some compiler arithmetic is beyond the prover, for example 64-bit division by a constant expanded inline.
  • Targets other than Thumb (Cortex-M0, M3 and M4 are tested). Floating-point instructions are not decoded.
  • The C front end is its own implementation and uses built-in versions of the common standard headers; unusual GNU extensions may not parse.

Building and testing

cargo build --release
cargo test --release

The crate has no dependencies. The end-to-end tests compile the corpus in tests/corpus at every optimisation level, require every function to be proved, and require deliberately altered builds to be rejected. They cover:

  • GCC for Cortex-M3, M0 and M4 at -O0 -O1 -O2 -O3 -Os -Og;
  • clang and GCC with a size-optimised Cortex-M0+ firmware command line (-mcpu=cortex-m0plus -fropi -fno-jump-tables -mno-unaligned-access ...), clang for -mcpu=cortex-m3 -frwpi and -mcpu=cortex-m35p+nodsp -msoft-float -frwpi, and GCC for cortex-m35p+nodsp, each at -Oz -Os -O0 -O1 -O2 -O3.

Tests skip themselves when the compiler they need is not installed. tests/core_fuzz.rs checks the term normaliser and the prover against an independent reference evaluator.

License

MIT