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 and then goes to an error state if . A model that tracks nothing about and 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, , 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.
An error state is a state that the property forbids, such as a failed assertion or a read outside an array §14.5. CEGAR in its basic form checks safety properties §1.11, which say that something bad never happens.
An abstraction over-approximates the program: every execution of the program corresponds to an execution of the abstraction. A common kind is predicate abstraction. It tracks the program location and the truth values of a chosen set of predicates, conditions such as or , and forgets everything else about the data.
A counterexample is a path in the abstraction from the start to an error state, given as the sequence of statements and branch outcomes along it.
A path is feasible when some execution of the real program, for some input, follows it.
The loop runs as follows.
Abstract. Build the abstraction from the current predicates. The first abstraction usually has none.
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.
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 becomes . Each branch outcome becomes its condition.
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.
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.
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.
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 , the constraints from the statements before the point, and a part , the constraints from the statements after it. An interpolant is a formula that implies, that contradicts , and that mentions only variables occurring in both and . 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 in first-order logic, and some SMT solvers compute interpolants for the theories they support.
Worked example
The program has an integer input .
Line 1: read , with .
Line 2: set .
Line 3: if , go to the error state.
Line 4: return.
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 true, error state. The path formula is Putting into the third part gives , which is false for every . The solver answers unsat: the counterexample is spurious.
New predicate. Cut the formula after line 2. Then and . The formula is an interpolant: implies , contradicts , and mentions only and , which occur in both. Written over the program’s variables, the new predicate is , tracked at line 3.
Round 2. Line 2 makes the predicate true, so at line 3 the abstraction allows only the outcome in which the test 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 . Round 1 finds the same counterexample, now with the path formula . The solver answers sat with, for example, and . Check the values: holds, holds, and holds. The input drives the program into the error state, and so does every other input. Interval analysis §3.9 admits the pair , 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 , a set of error states , and a transition relation , where means that one step can lead from state to state . An abstraction maps each concrete state §3.5 to an abstract state and has its own transition relation . The abstraction over-approximates the program when three conditions hold: is an abstract initial state for every ; is an abstract error state for every ; and for every . Then, by induction on the length of an execution, if no abstract error state is reachable from an abstract initial state, no state in is reachable from .
For a counterexample with statements , let the path formula be the conjunction of the initial condition and one constraint for each , written over fresh variable copies as above. Then is feasible exactly when 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.
The abstraction over-approximates the program, and every addition of predicates keeps that property.
The path formula encodes the real semantics of each statement, including machine widths and the effects of called functions.
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 up from 0, the new predicates may be , then , then , one per iteration, without ever reaching the loop invariant §3.24 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 is unsatisfiable, so a path that needs it is dismissed as spurious. For a 32-bit unsigned , the value satisfies it, because 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 , 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].