Section 3.6

Abstract domain

Definition

An abstract domain is the set of summaries that a static analysis §3.1 uses to describe a program’s values without running it. Each summary, called an abstract value, stands for a set of concrete values, the values that real runs can produce. The domain orders abstract values by precision and supplies the operations the analysis computes with, such as merging the summaries from two paths. The interval domain, for example, describes each integer variable by a lower and an upper bound. The domain decides which facts the analysis can express.

The idea

A program’s possible states are far too many to list. One 64-bit register alone can hold 2642^{64}, about 1.8×10191.8 \times 10^{19}, different values, and a loop can run any number of times. An analysis that must say something true about every run therefore works with summaries, each of which covers a whole set of states at once. Every summary forgets something. The abstract domain is the decision about what to keep and what to forget.

Take a point in a program where the two variables xx and yy can hold only the pairs (0,0)(0, 0) and (3,3)(3, 3). The interval domain keeps one range per variable: xx lies in [0,3][0, 3] and yy lies in [0,3][0, 3]. That summary is enough to prove x≤3x \le 3. It has forgotten that xx and yy are always equal, so it cannot prove x−y=0x - y = 0. A relational domain §15.12, one that records facts linking two or more variables, can keep x=yx = y as well. The relational summary costs more to store and to compute with.

A map is a fair picture: a country map shows a city as a dot, a street map shows its streets, and the coarse map is cheaper but cannot say which street a house is on. The picture stops in one respect. A map may leave a street out, but an abstract value may never leave out a state that can really occur; it may only include extra states that never occur.

How it works

An abstract domain has the following parts.

With these parts the analysis runs the program on abstract values: it applies the transfer function of each instruction, narrows the abstract value at each branch to the states that satisfy the branch condition, joins where paths meet, and repeats the work around each loop until nothing changes. Because every step covers all real results, the final abstract value at each point covers every concrete state §3.5 that a run can reach there (the Abstract interpretation §3.4 entry).

Domains differ in what they can say.

Worked example

Take the two states from above, (x,y)=(0,0)(x, y) = (0, 0) and (x,y)=(3,3)(x, y) = (3, 3), and compare two summaries of them.

Now the program computes z=x−yz = x - y and reads element zz of an array §14.5 with one element, so the only valid index is 0. The interval transfer function for subtraction takes the smallest value of xx minus the largest value of yy, and the largest value of xx minus the smallest value of yy: 0−3=−30 - 3 = -3 and 3−0=33 - 0 = 3, so z∈[−3,3]z \in [-3, 3]. Six of these seven values lie outside the array, so the interval analysis §3.9 must report a possible out-of-bounds read §4.5. The report is a false alarm §1.22: in every real run z=0−0=0z = 0 - 0 = 0 or z=3−3=0z = 3 - 3 = 0. The octagon knows x−y≤0x - y \le 0 and y−x≤0y - x \le 0, so z∈[0,0]z \in [0, 0], and the read is proved safe.

Precision never excuses a wrong summary. The interval [1,2][1, 2] for xx would be more precise than [0,3][0, 3], but it is wrong, because xx can be 0 or 3. A summary must first cover every real value; only among summaries that do is the smaller one better.

Stated precisely

Let CC be the set of concrete values (or concrete states) and let 𝒫(C)\mathcal{P}(C) be the set of all subsets of CC. An abstract domain is a set AA of abstract values together with a partial order §19.2.6 ⊑\sqsubseteq (the precision order), a concretization function γ:A→𝒫(C)\gamma : A \to \mathcal{P}(C), a join ⊔\sqcup, and a transfer function f♯:A→Af^\sharp : A \to A for each concrete operation f:C→Cf : C \to C. For all abstract values aa and bb, the parts must satisfy three conditions.

  1. Order: if a⊑ba \sqsubseteq b, then γ(a)⊆γ(b)\gamma(a) \subseteq \gamma(b).

  2. Join: γ(a)∪γ(b)⊆γ(a⊔b)\gamma(a) \cup \gamma(b) \subseteq \gamma(a \sqcup b).

  3. Transfer: {f(c):c∈γ(a)}⊆γ(f♯(a))\{ f(c) : c \in \gamma(a) \} \subseteq \gamma(f^\sharp(a)).

Conditions 2 and 3 make an analysis built from the domain sound: the summary it computes at a point covers every state that a run can reach there, once the iteration over loops has reached a fixed point §3.22.

Many domains also have an abstraction function α:𝒫(C)→A\alpha : \mathcal{P}(C) \to A, which gives the most precise abstract value covering a set of concrete values; for intervals, α({0,3})=[0,3]\alpha(\{0, 3\}) = [0, 3]. When α(S)⊑a\alpha(S) \sqsubseteq a holds exactly when S⊆γ(a)S \subseteq \gamma(a), for every set S⊆CS \subseteq C and every abstract value aa, the pair α,γ\alpha, \gamma is a Galois connection, and the most precise sound transfer function for ff is f♯(a)=α({f(c):c∈γ(a)})f^\sharp(a) = \alpha(\{ f(c) : c \in \gamma(a) \}).

The interval domain over the integers ℤ\mathbb{Z} is A={⊥}∪{[l,u]:l≤u}A = \{\bot\} \cup \{ [l, u] : l \le u \}, where ll is an integer or −∞-\infty and uu is an integer or +∞+\infty. Its meaning is γ([l,u])={n∈ℤ:l≤n≤u}\gamma([l, u]) = \{ n \in \mathbb{Z} : l \le n \le u \} and γ(⊥)=∅\gamma(\bot) = \emptyset, the empty set.

Limits and common mistakes

An analysis proves only what its domain can express. Intervals cannot express x=yx = y, or i<ni < n for a variable nn, or x≠0x \ne 0 when xx can be both negative and positive. Known bits cannot express the set {0,1,…,10}\{0, 1, \dots, 10\}: the smallest known-bits summary that covers it leaves the four lowest bits unknown, which also admits 11 to 15.

Joins lose information in every domain. Intervals join [0,0][0, 0] and [10,10][10, 10] into [0,10][0, 10], which includes the nine values 1 to 9 that neither path produced. Keeping the paths apart is a different source of precision, described in the Path sensitivity §3.11 entry.

A domain describes values under one meaning of the operations. A domain that computes with mathematical integers is wrong for machine integers that wrap around at their width; the Interval analysis entry shows the repair.

Combining domains needs a correct rule for passing facts between them. Two sound domains connected by a wrong exchange rule give an unsound analysis.

Questions answered

Which relation can intervals not represent that a relational domain can?

Intervals cannot represent any relation between two or more variables, such as x=yx = y, x≤yx \le y, x−y≤5x - y \le 5, or i<ni < n where nn is another variable; a relational domain can. An interval domain keeps one range per variable, independently, so its summary stands for every combination of the ranges. After the assignment y=xy = x with x∈[0,3]x \in [0, 3], intervals record x∈[0,3]x \in [0, 3] and y∈[0,3]y \in [0, 3], a summary that also admits the pair x=0x = 0, y=3y = 3. Interval subtraction then gives x−y∈[0−3,3−0]=[−3,3]x - y \in [0 - 3, 3 - 0] = [-3, 3] instead of exactly 0.

A relational domain stores constraints over several variables: octagons store every bound of the form ±x±y≤c\pm x \pm y \le c for a constant cc, and convex polyhedra store any linear inequality. An octagon keeps x−y≤0x - y \le 0 and y−x≤0y - x \le 0, which together say x=yx = y, and so proves x−y=0x - y = 0. The case that matters most in practice is a bounds check against a length held in another variable: after the test i<ni < n, a relational domain records i−n≤−1i - n \le -1 and can prove that a later access at index ii stays below nn, while intervals keep only the separate ranges of ii and nn. The extra precision costs time and memory.

Related entries: Abstract interpretation (§3.4), Interval analysis (§3.9), Bitmask abstract value (tnum) (§3.10), Path sensitivity (§3.11), Widening and narrowing (§3.23), Partial order, lattice, join, and meet (§19.2.6), Abstraction, concretization, and Galois connection (§19.2.7). Further reading: [68, 254, 255].