Section 3.4

Abstract interpretation

Definition

Abstract interpretation is a theory, introduced by Patrick Cousot and Radhia Cousot in 1977, for building static analyses whose results cover every real execution. The analysis runs the program on abstract values: summaries, such as “xx is between 1 and 3”, that each stand for a set of concrete states §3.5 (complete snapshots of the running program). Each program operation gets an abstract transfer function, its counterpart on summaries. The theory states the condition these functions must meet, soundness §3.7, for the final summaries to cover every reachable state §3.21.

The idea

The rule of signs is abstract interpretation in miniature. A negative number times a negative number is positive, whatever the numbers are, so the sign of −17×(−4)-17 \times (-4) is known without multiplying. The sign is a summary of a number, and the rule of signs computes on summaries with a result that is right for every pair of numbers the summaries stand for. Sometimes the summary is too coarse: a positive number plus a negative number can be positive, zero or negative, so the rule must answer “sign unknown”. That answer is still correct, only less useful.

Program analysis needs the same trick, because the set of real states is too large to compute. One 64-bit variable can hold 2642^{64} values at each point of the program, and a loop may run any number of times. Abstract interpretation replaces each set of states by a summary drawn from a chosen family, the abstract domain §3.6: signs, intervals, known bits, or relations such as x≤yx \le y. Each instruction and branch gets a rule on summaries. Running the rules over the whole program, around each loop until no summary changes, gives a summary at each program point that includes every state that can really occur there.

How it works

The parts, in plain words:

The analysis runs in this order:

  1. Give the entry point the summary of all possible starting states, and every other program point ⊥\bot.

  2. Apply each instruction’s F#F^{\#} to the summary before it. At a branch, refine the summary on each side by the branch condition. Where paths meet, join their summaries.

  3. Repeat until no summary changes, a fixed point §3.22. Around a loop a range can grow by one step per round; widening §3.23 jumps a growing bound straight to infinity so that the analysis finishes, and narrowing then recovers part of the lost precision.

  4. Check the property against the summary at each point where it must hold.

Worked example

The domain is intervals over mathematical integers, so no value wraps around. Write [a,b][a, b] for the integers from aa to bb inclusive.

  1. One instruction. The summary [1,3][1, 3] reaches x=x+2x = x + 2. The real results are 1+2=31 + 2 = 3, 2+2=42 + 2 = 4 and 3+2=53 + 2 = 5. The output [3,5][3, 5] covers exactly these. The output [3,4][3, 4] misses 5; a later test x<5x < 5 would then look always true, a false proof. The output [0,10][0, 10] is correct but less precise.

  2. A branch. The summary [1,3][1, 3] reaches the test x<3x < 3. The true side gets [1,2][1, 2], the false side [3,3][3, 3], and where the sides meet again the join gives [1,2]⊔[3,3]=[1,3][1, 2] \sqcup [3, 3] = [1, 3].

  3. A loop. The program is x = 0; while (x < 100) x = x + 1;. The summary at the loop test is the join of the entry value with the value coming back from the body. Without widening it grows [0,0][0, 0], [0,1][0, 1], [0,2][0, 2], and needs 101 rounds to reach [0,100][0, 100]. With widening, the second round sees the upper bound grow from 0 to 1 and jumps it to +∞+\infty: [0,+∞][0, +\infty]. Check stability: the body receives [0,+∞][0, +\infty] refined by the test x<100x < 100 to [0,99][0, 99], and adds 1, giving [1,100][1, 100]; joined with the entry value [0,0][0, 0] this is [0,100][0, 100], inside [0,+∞][0, +\infty], so the summary is stable. One narrowing round replaces [0,+∞][0, +\infty] by the computed [0,100][0, 100]. At the loop exit, the condition x≥100x \ge 100 refines [0,100][0, 100] to [100,100][100, 100]: exactly the real final value.

A domain cannot keep what it does not record. If (x,y)(x, y) is either (0,0)(0, 0) or (3,3)(3, 3), intervals keep x∈[0,3]x \in [0, 3] and y∈[0,3]y \in [0, 3] and lose the fact x=yx = y. A relational abstract domain §15.12, one that records relations between variables, keeps it.

Stated precisely

Let Σ\Sigma (sigma) be the set of concrete states and 𝒫(Σ)\mathcal{P}(\Sigma) the set of all its subsets. For one instruction, let F:𝒫(Σ)→𝒫(Σ)F : \mathcal{P}(\Sigma) \to \mathcal{P}(\Sigma) map a set SS of states to the set of states that the instruction can produce from members of SS. Let AA be the abstract domain, with the order a⊑ba \sqsubseteq b meaning “aa is at least as precise as bb”, and let γ:A→𝒫(Σ)\gamma : A \to \mathcal{P}(\Sigma) be monotone: a⊑ba \sqsubseteq b implies γ(a)⊆γ(b)\gamma(a) \subseteq \gamma(b). An abstract transfer function F#:A→AF^{\#} : A \to A is sound when F(γ(a))⊆γ(F#(a))for every a∈A.F(\gamma(a)) \subseteq \gamma(F^{\#}(a)) \quad \text{for every } a \in A. For a=[1,3]a = [1, 3] and x=x+2x = x + 2: F(γ(a))={3,4,5}F(\gamma(a)) = \{3, 4, 5\} and γ([3,5])={3,4,5}\gamma([3, 5]) = \{3, 4, 5\}, so the inclusion holds; γ([3,4])={3,4}\gamma([3, 4]) = \{3, 4\} lacks 5, so it fails. When α\alpha and γ\gamma form a Galois connection, the most precise sound choice is F#=α∘F∘γF^{\#} = \alpha \circ F \circ \gamma: concretize, apply the real instruction, abstract again.

Soundness carries over to the whole program. Now let FF be one step of the whole program and F#F^{\#} a sound abstract version of it, let II be the set of starting states, and let a*a^{*} be a summary with I⊆γ(a*)I \subseteq \gamma(a^{*}) and F#(a*)⊑a*F^{\#}(a^{*}) \sqsubseteq a^{*}, so that one more step does not enlarge it. Then every reachable state lies in γ(a*)\gamma(a^{*}). The reason: F(γ(a*))⊆γ(F#(a*))⊆γ(a*)F(\gamma(a^{*})) \subseteq \gamma(F^{\#}(a^{*})) \subseteq \gamma(a^{*}), by soundness and then monotonicity. So γ(a*)\gamma(a^{*}) contains the starting states and is closed under steps, and every reachable state is reached from a starting state in finitely many steps.

Limits and common mistakes

Soundness is relative to the concrete semantics chosen. The rule [l,u]+2=[l+2,u+2][l, u] + 2 = [l + 2, u + 2] is sound for mathematical integers. On a machine with 64-bit unsigned wraparound §4.16, x=264−1x = 2^{64} - 1 plus 2 gives 1. The same rule claims the result 264+12^{64} + 1, so an analyzer using it would conclude, for example, that x>5x > 5 afterwards, while the real value is 1. An analysis is only as good as the match between its concrete semantics and the real machine: arithmetic, memory and concurrency §9.1.

Sound is not useful by itself. The function that returns ⊤\top for every input is sound and proves nothing. Precision is a separate goal, and more precise domains cost more. The octagon domain, which records constraints of the form ±x±y≤c\pm x \pm y \le c, needs, in the worst case, memory proportional to the square and time proportional to the cube of the number of variables. Intervals need both only in proportion to the number of variables.

Widening trades precision for termination. It can jump past the true bound, and narrowing recovers only part of what was lost.

Abstract interpretation is a theory, not a tool. A tool built on it is sound only if every one of its transfer functions has been shown sound against the real semantics; one wrong rule voids the guarantee.

Questions answered

What does it mean for an abstract transfer function to be sound?

An abstract transfer function is sound when its output summary covers every state that the real instruction can produce from any state its input summary stands for. In abstract interpretation a summary aa stands for a set of concrete program states γ(a)\gamma(a); the real instruction maps a set SS of states to the set F(S)F(S) of possible results; the abstract transfer function F#F^{\#} maps a summary to a summary. Soundness is the condition F(γ(a))⊆γ(F#(a))F(\gamma(a)) \subseteq \gamma(F^{\#}(a)) for every summary aa.

For the instruction x=x+2x = x + 2 and the input interval [1,3][1, 3], the real results are 3, 4 and 5. The output [3,5][3, 5] is sound and exact; [0,10][0, 10] is sound but imprecise; [3,4][3, 4] is unsound because it omits the real result 5. A sound function may add states that cannot occur, which can only cause false warnings. It never omits a state that can occur, which could cause a false proof. Soundness is defined against a chosen concrete semantics, so a rule that is sound for mathematical integers can be unsound for 64-bit machine arithmetic.

Related entries: Concrete state (§3.5), Abstract domain (§3.6), Interval analysis (§3.9), Fixed point (§3.22), Soundness (§3.7), Widening and narrowing (§3.23), Abstraction, concretization, and Galois connection (§19.2.7). Further reading: [68, 69].