Section 15.13

Counterexample-guided abstraction refinement (CEGAR)

Definition

CEGAR (counterexample-guided abstraction refinement) is a verification method that proves a program cannot reach an error state or finds an execution that does. It checks an abstraction: a simplified model that allows every behavior of the program, and possibly more. If the abstraction cannot reach an error, neither can the program. Otherwise the check returns a counterexample path, and CEGAR tests whether the program can follow it. A feasible path is a real bug. An infeasible path is spurious; CEGAR adds the facts that exclude it and checks again.

The idea

A precise model of a program is expensive to check, and a coarse one raises false alarms §1.22: it reports paths to an error that need combinations of values the real program never produces. CEGAR starts coarse and adds precision only where a false alarm shows that precision is missing. Take a program that sets y=xy = x and then goes to an error state if x≠yx \ne y. A model that tracks nothing about xx and yy believes that the test can succeed, so it reports the path to the error. Checking that one path against the program’s real statements shows that the path is impossible, and the reason, x=yx = y, is exactly the fact the model was missing.

The method relies on the abstraction erring in one direction only. The abstraction may contain behaviors that the program lacks, but it never lacks a behavior that the program has. So an answer of “safe” from the abstraction holds for the program, while an answer of “unsafe” is only a lead, to be confirmed or refuted on the real statements. Edmund Clarke, Orna Grumberg, Somesh Jha, Yuan Lu and Helmut Veith described the method under this name in 2000 [384], and many software verifiers are built around it.

How it works

The method uses four notions.

The loop runs as follows.

  1. Abstract. Build the abstraction from the current predicates. The first abstraction usually has none.

  2. Check. Search the abstraction for a reachable error state, by model checking §3.30, an exhaustive search of the states the abstraction can reach. If none is reachable, stop: the program is safe, because every real execution is also an execution of the abstraction.

  3. Build the path formula. Translate the counterexample into one logical formula. Each assignment becomes an equation over a fresh copy of the variable it writes, as in static single assignment §3.38 (SSA) form, so that x=x+1x = x + 1 becomes x2=x1+1x_2 = x_1 + 1. Each branch outcome becomes its condition.

  4. Decide feasibility. Give the formula to an SMT solver §3.28 (satisfiability modulo theories solver), a program that decides whether some values of the variables make a formula true.

  5. Real bug. If the solver answers sat (satisfiable), the values it returns are inputs that drive the program along the path to the error. Stop and report them.

  6. Spurious counterexample. If the solver answers unsat (unsatisfiable), no execution follows the path. Find new predicates that explain why, add them, and go back to step 1.

  7. No decision. If the solver answers unknown, or a time or memory limit runs out, stop with the result unknown.

The new predicates in step 6 must make the spurious path impossible in the next abstraction. A standard way to find them uses Craig interpolants. Cut the unsatisfiable path formula at a program point into a part AA, the constraints from the statements before the point, and a part BB, the constraints from the statements after it. An interpolant is a formula II that AA implies, that contradicts BB, and that mentions only variables occurring in both AA and BB. The interpolant states what the program has established at that point that makes the rest of the path impossible, and it becomes a predicate tracked at that point. William Craig proved in 1957 [385] that such a formula exists for every unsatisfiable conjunction A∧BA \wedge B in first-order logic, and some SMT solvers compute interpolants for the theories they support.

Worked example

The program has an integer input xx.

Round 1. The abstraction tracks no predicates, so at line 3 it allows both outcomes of the test. The check finds the counterexample: line 1, line 2, line 3 with x≠yx \ne y true, error state. The path formula is (0≤x1≤10)∧(y1=x1)∧(x1≠y1).(0 \le x_1 \le 10) \wedge (y_1 = x_1) \wedge (x_1 \ne y_1). Putting y1=x1y_1 = x_1 into the third part gives x1≠x1x_1 \ne x_1, which is false for every x1x_1. The solver answers unsat: the counterexample is spurious.

New predicate. Cut the formula after line 2. Then A=(0≤x1≤10)∧(y1=x1)A = (0 \le x_1 \le 10) \wedge (y_1 = x_1) and B=(x1≠y1)B = (x_1 \ne y_1). The formula I=(y1=x1)I = (y_1 = x_1) is an interpolant: AA implies II, II contradicts BB, and II mentions only x1x_1 and y1y_1, which occur in both. Written over the program’s variables, the new predicate is x=yx = y, tracked at line 3.

Round 2. Line 2 makes the predicate x=yx = y true, so at line 3 the abstraction allows only the outcome in which the test x≠yx \ne y fails. No error state is reachable. The program is proved safe for every input from 0 to 10.

A real bug. Change line 2 to y=x+1y = x + 1. Round 1 finds the same counterexample, now with the path formula (0≤x1≤10)∧(y1=x1+1)∧(x1≠y1)(0 \le x_1 \le 10) \wedge (y_1 = x_1 + 1) \wedge (x_1 \ne y_1). The solver answers sat with, for example, x1=0x_1 = 0 and y1=1y_1 = 1. Check the values: 0≤0≤100 \le 0 \le 10 holds, 1=0+11 = 0 + 1 holds, and 0≠10 \ne 1 holds. The input x=0x = 0 drives the program into the error state, and so does every other input. Interval analysis §3.9 admits the pair x=0x = 0, y=1y = 1 in both versions of the program; only the check against the statements tells the two versions apart.

Stated precisely

Model the program as a set of initial states Init\mathrm{Init}, a set of error states Err\mathrm{Err}, and a transition relation TT, where (s,s′)∈T(s, s') \in T means that one step can lead from state ss to state s′s'. An abstraction maps each concrete state §3.5 ss to an abstract state h(s)h(s) and has its own transition relation T̂\hat{T}. The abstraction over-approximates the program when three conditions hold: h(s)h(s) is an abstract initial state for every s∈Inits \in \mathrm{Init}; h(s)h(s) is an abstract error state for every s∈Errs \in \mathrm{Err}; and (h(s),h(s′))∈T̂(h(s), h(s')) \in \hat{T} for every (s,s′)∈T(s, s') \in T. Then, by induction on the length of an execution, if no abstract error state is reachable from an abstract initial state, no state in Err\mathrm{Err} is reachable from Init\mathrm{Init}.

For a counterexample π\pi with statements s1,…,sks_1, \ldots, s_k, let the path formula φπ\varphi_\pi be the conjunction of the initial condition and one constraint for each sjs_j, written over fresh variable copies as above. Then π\pi is feasible exactly when φπ\varphi_\pi is satisfiable, judged in the theory that gives the program’s operations their real meaning: bit-vector arithmetic for machine integers, not the arithmetic of unbounded integers. The three solver answers mean the following: sat, a real counterexample, with the returned values as an input that reproduces it; unsat, a spurious counterexample; unknown, no conclusion.

The verdicts rest on three assumptions.

  1. The abstraction over-approximates the program, and every addition of predicates keeps that property.

  2. The path formula encodes the real semantics of each statement, including machine widths and the effects of called functions.

  3. The initial condition admits every input that the deployed program can receive. A real counterexample is real relative to the inputs that this condition admits.

The loop need not terminate. Whether a program with unbounded integer variables can reach an error state is undecidable §19.2.1 in general, so no method decides every case.

Limits and common mistakes

A spurious counterexample does not show that the error is unreachable. It excludes one path; a different path to the same error may be feasible, so the loop continues until the abstraction has no path to an error state at all.

The loop can run forever. For a loop that counts ii up from 0, the new predicates may be i=0i = 0, then i=1i = 1, then i=2i = 2, one per iteration, without ever reaching the loop invariant §3.24 i≥0i \ge 0 that would complete the proof. Tools therefore set time and memory limits.

Unknown is not safe. A solver answer of unknown, or an exhausted time or memory limit, leaves the program unproved. A checker that must decide then refuses the program, the rule called conservative refusal §3.36.

A feasibility check is only as faithful as its semantics. Over unbounded integers, the condition x+1<xx + 1 < x is unsatisfiable, so a path that needs it is dismissed as spurious. For a 32-bit unsigned xx, the value x=4,294,967,295x = 4{,}294{,}967{,}295 satisfies it, because x+1x + 1 wraps to 0. The integer encoding has hidden a real bug. Leaving out the effects of called functions, other threads or hardware causes the same kind of error.

A real counterexample is real for the model, not automatically for deployment. If the initial condition admits inputs that the deployed system can never receive, the bug may be unreachable in practice. Running the program on the solver’s input confirms it.

Basic CEGAR checks safety properties. Proving that something good eventually happens, a liveness property §1.12, needs further tools, such as a ranking function §3.25, which shows that a loop cannot run forever.

Questions answered

What distinguishes a real bug trace from an abstraction artifact?

Feasibility distinguishes them: a real bug trace is one that some execution of the program, for some allowed input, actually follows, while an abstraction artifact is a trace that only the simplified model allows. To decide which, translate the trace into a path formula. Each assignment becomes an equation over a fresh copy of the variable it writes, each branch outcome becomes its condition, and the allowed inputs become a constraint. Then ask an SMT solver (satisfiability modulo theories solver) whether some values satisfy the formula.

If the solver returns values, they form an input that drives the program along the trace to the error: the trace is a real bug, relative to the semantics and the input range that the formula encodes. If the solver proves that no values exist, the trace is a spurious artifact of the abstraction. The reason, such as the fact x=yx = y, is what counterexample-guided abstraction refinement (CEGAR) adds to the abstraction before checking again. If the solver answers unknown or runs out of time, the trace is undecided and nothing has been proved. The verdict is only as good as the encoding: a formula over unbounded integers can dismiss a real bug that depends on wrap-around.

Related entries: Abstract interpretation (§3.4), SMT solver (§3.28), Bounded model checking (§3.29), Relational abstract domain (§15.12), Model checking (§3.30), Static single assignment (SSA) (§3.38), Bit-vector logic (§15.15), Safety property (§1.11), Conservative refusal (§3.36). Further reading: [384, 385].