Definition
A relational abstract domain is an abstract domain §3.6 whose summaries record constraints that link two or more variables, such as , or . An abstract domain is the set of summaries that a static analysis §3.1, a tool that reasons about every run of a program without running it, uses to describe the program’s values. A relational summary stands for every combination of values that satisfies all its constraints at once. A non-relational domain, such as intervals, describes each variable separately and cannot exclude a combination of individually possible values.
The idea
A program reads a number between 0 and 100, copies it with , and later uses as an index into a table that has a single entry, at index 0. Every run computes , so every access is safe. Interval analysis §3.9 keeps a lower and an upper bound for each variable on its own: and . From these two ranges alone, can be anything from to , so the analysis reports a possible out-of-bounds access that no run can make. Such a report is a false alarm §1.22. Both ranges are correct; what the analysis lost is the fact that links them.
Draw the possible values of the pair as points in a plane. An interval summary can describe only a rectangle with sides parallel to the axes: one range for combined with one range for . Here the smallest such rectangle is the square with corners and , which holds integer points. The real runs lie on its diagonal, the 101 points with . A relational domain can describe the diagonal itself, or a slanted band such as , and so it excludes the corner that caused the false alarm. Slanted shapes cost more to store and to update than rectangles; that cost is the price of the extra precision.
How it works
A relational domain fixes in advance the shape of constraint it can record. The shape sets both what the domain can express and what it costs. The common shapes are these.
Linear equalities, introduced by Michael Karr in 1976 [383], record facts such as over any number of variables, but no inequalities.
Zones record for pairs of variables and a constant , and bounds and on single variables. An analysis stores a zone as a square table of these bounds, called a difference-bound matrix.
Octagons, introduced by Antoine Miné in 2001 [254], record every constraint of the form . Besides the zone constraints, they include sums such as .
Convex polyhedra, introduced by Patrick Cousot and Nicolas Halbwachs in 1978 [255], record any linear inequality over any number of variables, such as .
For variables, a zone or an octagon stores a number of bounds proportional to , and making all implied bounds explicit takes a number of steps proportional to . Some operations on polyhedra take time exponential in .
The analysis runs over the program as in any abstract interpretation §3.4: for each statement it applies a transfer function, a rule that computes the summary after the statement from the summary before it. For zones the rules are these.
Closure. Constraints imply further constraints: and add up to . Closure adds every implied constraint and keeps the smallest bound for each pair of variables. For zones and octagons, closure is a shortest-path computation over the variables, which is where the cost comes from. If closure derives for a negative , the summary is contradictory, because is 0: it describes no state, and the program point it belongs to cannot be reached.
Copy and shift. For with a constant , close the summary, delete every constraint that mentions , because the old value of is gone, and add and , which together say . Closing first keeps the facts about other variables that were known only through . For , shift every constraint on : becomes .
Other assignments. For an assignment the domain cannot express, such as , delete every constraint that mentions and keep only a range for , if one can be computed. Deleting constraints is always sound, meaning that it never excludes a state that can occur, because fewer constraints describe more states.
Tests. On the edge where an integer test holds, add ; on the other edge, add . If closure then finds a contradiction, that edge cannot be taken.
Joins. Where two paths meet, keep what both paths guarantee: after closing both summaries, take for each pair of variables the larger of the two bounds. A constraint known on only one path is dropped. For polyhedra the join is the convex hull, the smallest convex shape that contains both.
Loops. The analysis repeats a loop body until the summary stops changing, which is a fixed point §3.22. A widening operator §3.23 forces this to happen after a bounded number of rounds by dropping bounds that keep growing.
Checks. An operation with a precondition, such as reading element of an array §14.5 with elements, is proved safe when the summary implies the precondition, here and .
Relational domains are often combined with cheaper domains in a reduced product §15.11, where each domain sharpens the facts of the other.
Worked example
In the first three cases the variables are mathematical integers. Copy. The program reads with , sets , sets and reads table[d], where table has one entry, at index 0.
Intervals: and . Interval subtraction gives . This range holds 201 integers, 200 of them invalid indices, so the analysis reports a possible out-of-bounds read §4.5.
Zones: after the summary is , and . For , the constraint gives , and gives . So , and the read is proved safe.
Closure. A summary holds , , and . The program then reads element of a 16-element array, which needs . No single constraint bounds from above. Closure adds the two differences: . Adding gives . Together with , this proves the read safe.
Join. One path sets ; the other sets . The first path gives and . The second gives and . The join keeps the larger bound of each pair: and , that is, . For integers this is exact, because the only integers from 0 to 1 are the two real outcomes, 0 and 1.
Wrap-around. Now let and be unsigned 8-bit integers. They hold the values 0 to 255, and the machine keeps only the remainder of every result after division by 256. The statement would add to the summary. For the machine computes , and then , not 1. A sound analysis therefore adds only after proving ; otherwise it deletes the constraints on and keeps the range . The copy computes nothing, so the equality it records holds for machine integers too.
Stated precisely
Let be the set of the program’s integer variables, and let a state give each variable a value . A summary is a finite set of constraints of the shape the domain allows. Its concretization §19.2.7 is the set of all states that satisfy every constraint in . A transfer function for a statement is sound when, for every summary , every state that can produce from a state in lies in . A condition is proved at a program point when every state in the concretization of the summary at that point satisfies the condition.
An interval summary contains only constraints on one variable each. Its concretization is therefore a box: a set of the form , in which every value in is paired with every value in . A box that contains the states and with also contains , whose difference is not 0. So no interval summary that admits two such states can prove . The zone summary proves it, because every state it admits satisfies both and .
A proof from a relational summary holds for the real program under three assumptions.
Sound rules for the real arithmetic. Every transfer function computes with the program’s own arithmetic, including the wrap-around of machine integers shown in the last example.
Every write is seen. A write to through a pointer that may point to must delete the constraints on ; finding such writes is the task of alias analysis §3.18.
No hidden writers. Nothing outside the analyzed code, such as another thread or an interrupt handler §6.12, changes the variables between the analyzed statements.
The implication itself needs no assumption about width: for mathematical integers and for -bit machine integers alike, subtracting equal values gives 0.
Limits and common mistakes
Zones, octagons and polyhedra are convex: with any two states they contain every integer state on the straight segment between them. They therefore cannot record a gap or a choice. They cannot record , and the join of and is , which forgets that 5 never occurs. So they cannot prove a division by safe from the fact .
Linear domains do not express products. After they record no relation between and or .
Precision costs time and memory. For 1,000 variables an octagon stores a number of bounds proportional to , and its closure takes a number of steps proportional to . Analyzers therefore often relate only small groups of variables that the program uses together, called packs, instead of all variables at once.
A stored relation is only as sound as the rules that maintain it. An operation the analysis does not understand must delete constraints, never leave them unchanged. Keeping after an unseen write to makes the analysis unsound: it can then accept a program that fails.
Relations computed over mathematical integers can be false for machine integers, as the wrap-around example shows. In C, signed integer overflow §4.15 is undefined behavior §4.12, so an analyzer for C reports a possible signed overflow instead of computing a wrapped value.
A join keeps only the constraints that both paths share. A relational domain does not replace path sensitivity §3.11, which keeps the paths apart.
More precision does not make an analysis complete. For any fixed domain, some safe programs need a fact that the domain cannot express, and the analysis rejects them or raises a false alarm.
Questions answered
Which property can be proved from x=y that separate wide intervals cannot show?
From an analysis can prove that , together with every other consequence of the equality: and hold, a test never succeeds, and a bound checked on , such as , holds for as well. Separate wide intervals cannot show any of these. Two intervals such as and admit every pair of values, including with , so interval subtraction gives . A relational abstract domain, whose summaries record constraints that link variables, keeps and , which exclude such pairs and give . The conclusion also holds for machine integers that wrap around, because subtracting equal values gives 0 at every width.
Related entries: Abstract interpretation (§3.4), Abstract domain (§3.6), Interval analysis (§3.9), Bit-vector logic (§15.15), Reduced product abstract domain (§15.11), Widening and narrowing (§3.23), Alias analysis (§3.18). Further reading: [68, 254, 255, 383].