Section 3.29

Bounded model checking

Definition

Bounded model checking (BMC) is an automatic verification method that decides whether a model of a system has an execution of at most kk steps, starting in an initial state, that violates a stated property. BMC writes this question as one logical formula that is satisfiable, meaning that some values of its variables make it true, exactly when such an execution exists, and a program called a solver searches for such values. Values found describe a counterexample, the violating execution; if none exist, no violation occurs within kk steps.

The idea

Many defects appear within a few steps of the start: a counter that wraps around on its eighth increment, or a protocol that accepts a message one step too early. Model checking §3.30 in full settles a property for executions of every length, which needs the set of all reachable states §3.21, and that set is often too large to compute; this is the problem of state explosion §3.26. Bounded model checking gives up the claim about every length. In return it gives an exact, automatic answer about every execution up to the bound, including every choice of input along the way. Armin Biere, Alessandro Cimatti, Edmund Clarke and Yunshan Zhu introduced the method in 1999. They used a solver for the Boolean satisfiability problem (SAT), which asks whether true-or-false values can be given to variables so that a formula becomes true.

The core idea is to unroll time. Make one copy of the system’s variables for each moment 0,1,…,k0, 1, \dots, k. Write one constraint saying that copy 0 is an initial state, one constraint per step saying that copy i+1i + 1 follows from copy ii by an allowed step, and one constraint saying that the property fails at some moment. Every solution of the whole formula is then a violating execution, so a solver that finds solutions finds bugs. The picture is a maze: instead of walking its corridors, the method writes the whole route of kk moves as a puzzle with unknowns and asks whether any way of filling them in ends in the forbidden room. The puzzle says nothing about routes longer than kk moves.

How it works

Bounded model checking needs three inputs.

The check runs as follows.

  1. Make k+1k + 1 copies s0,s1,…,sks_0, s_1, \dots, s_k of the state variables, one for each moment.

  2. Build the formula: s0s_0 is an initial state, each si+1s_{i+1} follows sis_i for ii from 0 to k−1k - 1, and PP fails in at least one of s0,…,sks_0, \dots, s_k.

  3. Translate the formula into the solver’s language. For hardware, every bit becomes a true-or-false variable and a SAT solver decides the formula. For software, integers become fixed-width bit vectors, so that wrap-around and overflow are modeled exactly, and an SMT solver §3.28 (satisfiability modulo theories solver, a SAT solver extended with such arithmetic) decides it.

  4. Read the verdict. Satisfiable: the solver’s values for s0,…,sks_0, \dots, s_k form a counterexample. Unsatisfiable: no execution of the model violates PP within kk steps. Unknown, because the solver timed out or ran out of memory: no conclusion either way.

  5. Usually repeat with k=0,1,2,…k = 0, 1, 2, \dots until a counterexample appears or the time budget ends. The first bound that gives a counterexample is the length of the shortest violating execution.

For a program, the unit of the bound is usually the number of loop iterations. The tool unrolls each loop kk times: it copies the loop body kk times, each copy guarded by the loop condition. It then rewrites the code in static single assignment §3.38 (SSA) form, in which every variable is assigned exactly once, so that each assignment becomes an equation. CBMC (C Bounded Model Checker) is a widely used tool of this kind. After the last copy, the tool must say what happens to executions that would run the loop again. An unwinding assertion asks the solver whether the loop condition can still be true at that point; if it can, the tool reports that the bound is too small. An unwinding assumption silently discards those executions, so a clean result then covers only executions that leave every loop within kk iterations. Other units in use are the depth of recursive calls and the number of switches between threads.

Worked example

A missed and a found violation. A 3-bit counter xx holds a whole number from 0 to 7. It starts at 0, and each step sets xx to (x+1)mod8(x + 1) \bmod 8, the remainder of x+1x + 1 after division by 8. The property is x≠5x \neq 5. Write xix_i for the value of xx at moment ii. With k=4k = 4 the formula is x0=0∧x1=x0+1∧x2=x1+1∧x3=x2+1∧x4=x3+1∧(x0=5∨x1=5∨…∨x4=5),x_0 = 0 \wedge x_1 = x_0 + 1 \wedge x_2 = x_1 + 1 \wedge x_3 = x_2 + 1 \wedge x_4 = x_3 + 1 \wedge (x_0 = 5 \vee x_1 = 5 \vee \dots \vee x_4 = 5), where ∧\wedge means and and ∨\vee means or; the remainder is left out because no value reaches 8. The equations force x0,…,x4x_0, \dots, x_4 to be 0, 1, 2, 3, 4. None of them is 5, so the formula is unsatisfiable: no violation within 4 steps. With k=5k = 5 the equations also force x5=4+1=5x_5 = 4 + 1 = 5, the formula is satisfiable, and the counterexample is the execution 0, 1, 2, 3, 4, 5. The answer at k=4k = 4 was true, and it still did not show that the property holds.

Why every moment is checked. With k=6k = 6 the forced values are 0, 1, 2, 3, 4, 5, 6. A formula that asked only whether the last value x6x_6 is 5 would be unsatisfiable, although the execution passed through 5 at moment 5. The disjunction, the chain of ors over all moments from 0 to kk, catches the visit.

The solver chooses the inputs. Now let each step set xx to (x+b)mod8(x + b) \bmod 8, where the environment supplies bb, either 1 or 2, afresh at every step. With k=2k = 2, x0=0x_0 = 0, x1x_1 is 1 or 2, and x2x_2 is 2, 3 or 4, so no value is 5 and the formula is unsatisfiable. With k=3k = 3, x3x_3 can be 3, 4, 5 or 6, and the solver returns one choice that reaches 5, for example b=1,2,2b = 1, 2, 2, which gives the execution 0, 1, 3, 5. A tester would have had to guess that input sequence.

Turning a bounded answer into a proof. Let each step add 2 instead: xx becomes (x+2)mod8(x + 2) \bmod 8, starting at 0, and the property is again x≠5x \neq 5. The executions visit 0, 2, 4, 6, 0, 2 and so on, so every bound gives unsatisfiable, and the question is when the check may stop. Two arguments end it.

Stated precisely

Let I(s)I(s), T(s,s′)T(s, s') and P(s)P(s) be as above, and let s0,…,sks_0, \dots, s_k be copies of the state variables. Write ∧\wedge for and, ∨\vee for or and ¬\neg for not; a large ⋀\bigwedge or ⋁\bigvee joins all its terms in the same way. Bounded model checking to depth kk asks whether BMCk=I(s0)∧⋀i=0k−1T(si,si+1)∧⋁i=0k¬P(si)\mathrm{BMC}_k = I(s_0) \wedge \bigwedge_{i=0}^{k-1} T(s_i, s_{i+1}) \wedge \bigvee_{i=0}^{k} \neg P(s_i) is satisfiable.

Assume that every state has at least one successor. Then BMCk\mathrm{BMC}_k is satisfiable exactly when some execution that starts in an initial state reaches a state violating PP within at most kk steps. In the notation of the Reachability entry, where RkR_k is the set of states reachable within kk steps, unsatisfiable means that every state in RkR_k satisfies PP. The assumption matters. If an execution can stop, for example in a program’s exit state or in a fault state, then a bad state reached at step 2 that has no successor is missed by BMC3\mathrm{BMC}_3, because the formula demands three steps. Tools avoid this by giving each final state a step to itself, or by checking each depth j≤kj \le k with ¬P(sj)\neg P(s_j) alone.

An unsatisfiable BMCk\mathrm{BMC}_k extends to every reachable state in two cases.

Every conclusion above holds only under four conditions.

  1. The solver returned unsatisfiable; unknown proves nothing.

  2. The formula encodes II, TT and PP exactly, including fixed-width arithmetic.

  3. The model contains every behavior of the environment that the claim covers: every input value, every failure and every interleaving of threads.

  4. For a program, every unwinding assertion held; otherwise the result covers only executions within the loop bounds.

Limits and common mistakes

A larger bound is more evidence, not a proof. Without a completeness threshold or an induction argument, a clean result at k=1000k = 1000 says nothing about step 1001. The formula grows in proportion to kk, but the solver’s running time can grow exponentially with it, so deep bounds are often out of reach.

Contradictory assumptions make every check pass. Constraints on the environment, such as assume(x > 5) together with assume(x < 3), can leave no execution at all, and with no execution there is no counterexample. Check that the assumptions admit executions, for example by asking the tool for one execution that reaches the checked statement.

The unit of the bound must be stated. Ten loop iterations are not ten instructions, and a loop that normally runs 64 times is not covered by a bound below 64.

A liveness property §1.12, which says that something good eventually happens, needs a different shape of counterexample. A violation of “every request is eventually answered” is an infinite execution. Bounded model checking finds it only as a lasso: a finite execution whose last state equals an earlier one, so that the loop between them can repeat forever without the answer. Finding no lasso within kk steps proves nothing about the liveness property.

A counterexample is real for the model, not automatically for the system. If the model lets an input take a value that the hardware never produces, the counterexample may be impossible in practice; check it against the real system.

Questions answered

What does no counterexample up to depth k establish?

No counterexample up to depth kk establishes that no execution of the model that starts in an initial state and takes at most kk steps reaches a state that violates the checked property. Three conditions must hold for even that bounded claim. The solver answered unsatisfiable, not unknown or timeout §9.34. The formula checked the property at every moment from 0 to kk, not only at moment kk, and it handled executions that stop early. The model, its assumptions about the environment and, for a program, its loop bounds admit every behavior the claim is about; if unwinding assumptions cut off longer runs of a loop, those runs were never examined.

It establishes nothing about step k+1k + 1 or later. The bounded result becomes a proof for all executions only with an extra argument. Either kk is at least a completeness threshold, for example because no execution of k+1k + 1 steps from an initial state has all its states different, or an induction proof such as kk-induction succeeds. Without one of these, a deeper search is more evidence, not a proof.

Related entries: Reachability (§3.21), SMT solver (§3.28), Model checking (§3.30), State explosion (§3.26), Safety property (§1.11). Further reading: [228].