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 , about , 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 and can hold only the pairs and . The interval domain keeps one range per variable: lies in and lies in . That summary is enough to prove . It has forgotten that and are always equal, so it cannot prove . A relational domain §15.12, one that records facts linking two or more variables, can keep 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.
Abstract values: the summaries themselves. Two of them are special. Bottom, written , stands for no state at all and marks a point that no run reaches. Top, written , stands for every possible state and means that nothing is known.
Concretization §19.2.7: the meaning of each abstract value , written , the set of concrete values that stands for. For intervals, .
Precision order: means that is at least as precise as : every concrete value that stands for, also stands for. The interval is more precise than .
Join, written : the most precise abstract value that covers two given ones. The analysis uses it where two paths meet. For intervals, .
Transfer functions: one rule for each kind of program operation, which computes the abstract value after the operation from the abstract value before it. The rule must cover every result that the real operation can produce.
Widening: an operator, needed by domains such as intervals in which a summary can keep growing, that forces the analysis of a loop to stop. The Widening and narrowing §3.23 entry explains it.
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.
A non-relational domain keeps one summary per variable, independently of the others. Examples are signs (negative, zero or positive), constants (one known value, or unknown), intervals, congruences ( leaves remainder when divided by ) and known bits, which record for each bit whether it is known to be 0, known to be 1 or unknown (the Bitmask abstract value §3.10 entry). The cost grows in proportion to the number of variables.
A relational domain records constraints that link variables. Linear equalities record facts such as . Octagons, the domain introduced by Antoine Miné [254], record every constraint of the form for a constant , which includes and . Convex polyhedra, introduced by Patrick Cousot and Nicolas Halbwachs in 1978 [255], record any linear inequality over any number of variables. For variables an octagon stores a number of bounds proportional to and needs a number of steps proportional to to make all implied bounds explicit; operations on polyhedra can take time exponential in .
A product domain runs two domains side by side, such as intervals and known bits. A reduced product §15.11 also passes facts between them. If known bits say that the two lowest bits of are 0 and the interval says , then is a multiple of 4 between 1 and 6, so .
Worked example
Take the two states from above, and , and compare two summaries of them.
Intervals: and . The summary stands for every combination of the two ranges, pairs, of which 2 really occur.
Octagon: , , and . The last two constraints together say , so the summary stands for the 4 pairs , , and , of which 2 really occur.
Now the program computes and reads element 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 minus the largest value of , and the largest value of minus the smallest value of : and , so . 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 or . The octagon knows and , so , and the read is proved safe.
Precision never excuses a wrong summary. The interval for would be more precise than , but it is wrong, because 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 be the set of concrete values (or concrete states) and let be the set of all subsets of . An abstract domain is a set of abstract values together with a partial order §19.2.6 (the precision order), a concretization function , a join , and a transfer function for each concrete operation . For all abstract values and , the parts must satisfy three conditions.
Order: if , then .
Join: .
Transfer: .
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 , which gives the most precise abstract value covering a set of concrete values; for intervals, . When holds exactly when , for every set and every abstract value , the pair is a Galois connection, and the most precise sound transfer function for is .
The interval domain over the integers is , where is an integer or and is an integer or . Its meaning is and , the empty set.
Limits and common mistakes
An analysis proves only what its domain can express. Intervals cannot express , or for a variable , or when can be both negative and positive. Known bits cannot express the set : 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 and into , 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 , , , or where 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 with , intervals record and , a summary that also admits the pair , . Interval subtraction then gives instead of exactly 0.
A relational domain stores constraints over several variables: octagons store every bound of the form for a constant , and convex polyhedra store any linear inequality. An octagon keeps and , which together say , and so proves . The case that matters most in practice is a bounds check against a length held in another variable: after the test , a relational domain records and can prove that a later access at index stays below , while intervals keep only the separate ranges of and . 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].