Section 15.12

Relational abstract domain

Definition

A relational abstract domain is an abstract domain §3.6 whose summaries record constraints that link two or more variables, such as x=yx = y, x−y≤3x - y \le 3 or i<ni < n. 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 xx between 0 and 100, copies it with y=xy = x, and later uses d=x−yd = x - y as an index into a table that has a single entry, at index 0. Every run computes d=0d = 0, so every access is safe. Interval analysis §3.9 keeps a lower and an upper bound for each variable on its own: x∈[0,100]x \in [0, 100] and y∈[0,100]y \in [0, 100]. From these two ranges alone, dd can be anything from 0−100=−1000 - 100 = -100 to 100−0=100100 - 0 = 100, 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 (x,y)(x, y) as points in a plane. An interval summary can describe only a rectangle with sides parallel to the axes: one range for xx combined with one range for yy. Here the smallest such rectangle is the square with corners (0,0)(0, 0) and (100,100)(100, 100), which holds 101×101=10,201101 \times 101 = 10{,}201 integer points. The real runs lie on its diagonal, the 101 points with x=yx = y. A relational domain can describe the diagonal itself, or a slanted band such as 0≤x−y≤10 \le x - y \le 1, and so it excludes the corner (0,100)(0, 100) 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.

For nn variables, a zone or an octagon stores a number of bounds proportional to n2n^2, and making all implied bounds explicit takes a number of steps proportional to n3n^3. Some operations on polyhedra take time exponential in nn.

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.

  1. Closure. Constraints imply further constraints: x−y≤ax - y \le a and y−z≤by - z \le b add up to x−z≤a+bx - z \le a + b. 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 n3n^3 cost comes from. If closure derives x−x≤cx - x \le c for a negative cc, the summary is contradictory, because x−xx - x is 0: it describes no state, and the program point it belongs to cannot be reached.

  2. Copy and shift. For y=x+cy = x + c with a constant cc, close the summary, delete every constraint that mentions yy, because the old value of yy is gone, and add y−x≤cy - x \le c and x−y≤−cx - y \le -c, which together say y−x=cy - x = c. Closing first keeps the facts about other variables that were known only through yy. For x=x+cx = x + c, shift every constraint on xx: x−z≤kx - z \le k becomes x−z≤k+cx - z \le k + c.

  3. Other assignments. For an assignment the domain cannot express, such as y=x⋅zy = x \cdot z, delete every constraint that mentions yy and keep only a range for yy, 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.

  4. Tests. On the edge where an integer test x<yx < y holds, add x−y≤−1x - y \le -1; on the other edge, add y−x≤0y - x \le 0. If closure then finds a contradiction, that edge cannot be taken.

  5. 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.

  6. 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.

  7. Checks. An operation with a precondition, such as reading element ii of an array §14.5 with nn elements, is proved safe when the summary implies the precondition, here −i≤0-i \le 0 and i−n≤−1i - n \le -1.

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 xx with 0≤x≤1000 \le x \le 100, sets y=xy = x, sets d=x−yd = x - y and reads table[d], where table has one entry, at index 0.

Closure. A summary holds x≥0x \ge 0, x−y≤2x - y \le 2, y−z≤3y - z \le 3 and z≤10z \le 10. The program then reads element xx of a 16-element array, which needs 0≤x≤150 \le x \le 15. No single constraint bounds xx from above. Closure adds the two differences: (x−y)+(y−z)=x−z≤2+3=5(x - y) + (y - z) = x - z \le 2 + 3 = 5. Adding z≤10z \le 10 gives x=(x−z)+z≤5+10=15x = (x - z) + z \le 5 + 10 = 15. Together with x≥0x \ge 0, this proves the read safe.

Join. One path sets y=xy = x; the other sets y=x−1y = x - 1. The first path gives x−y≤0x - y \le 0 and y−x≤0y - x \le 0. The second gives x−y≤1x - y \le 1 and y−x≤−1y - x \le -1. The join keeps the larger bound of each pair: x−y≤max(0,1)=1x - y \le \max(0, 1) = 1 and y−x≤max(0,−1)=0y - x \le \max(0, -1) = 0, that is, 0≤x−y≤10 \le x - y \le 1. 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 xx and yy 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 y=x+1y = x + 1 would add y−x=1y - x = 1 to the summary. For x=255x = 255 the machine computes y=256−256=0y = 256 - 256 = 0, and then y−x=0−255=−255y - x = 0 - 255 = -255, not 1. A sound analysis therefore adds y−x=1y - x = 1 only after proving x≤254x \le 254; otherwise it deletes the constraints on yy and keeps the range y∈[0,255]y \in [0, 255]. The copy y=xy = x computes nothing, so the equality it records holds for machine integers too.

Stated precisely

Let VV be the set of the program’s integer variables, and let a state σ\sigma give each variable v∈Vv \in V a value σ(v)\sigma(v). A summary CC is a finite set of constraints of the shape the domain allows. Its concretization §19.2.7 γ(C)\gamma(C) is the set of all states that satisfy every constraint in CC. A transfer function f♯f^\sharp for a statement ss is sound when, for every summary CC, every state that ss can produce from a state in γ(C)\gamma(C) lies in γ(f♯(C))\gamma(f^\sharp(C)). 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 Bx×ByB_x \times B_y, in which every value in BxB_x is paired with every value in ByB_y. A box that contains the states (x,y)=(a,a)(x, y) = (a, a) and (x,y)=(b,b)(x, y) = (b, b) with a≠ba \ne b also contains (a,b)(a, b), whose difference a−ba - b is not 0. So no interval summary that admits two such states can prove x−y=0x - y = 0. The zone summary {x−y≤0,y−x≤0}\{x - y \le 0,\ y - x \le 0\} proves it, because every state it admits satisfies both x−y≤0x - y \le 0 and x−y≥0x - y \ge 0.

A proof from a relational summary holds for the real program under three assumptions.

  1. 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.

  2. Every write is seen. A write to xx through a pointer that may point to xx must delete the constraints on xx; finding such writes is the task of alias analysis §3.18.

  3. 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 x=y⇒x−y=0x = y \Rightarrow x - y = 0 itself needs no assumption about width: for mathematical integers and for ww-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 x≠yx \ne y, and the join of x=0x = 0 and x=10x = 10 is 0≤x≤100 \le x \le 10, which forgets that 5 never occurs. So they cannot prove a division by x−yx - y safe from the fact x≠yx \ne y.

Linear domains do not express products. After z=x⋅yz = x \cdot y they record no relation between zz and xx or yy.

Precision costs time and memory. For 1,000 variables an octagon stores a number of bounds proportional to 1,0002=1061{,}000^2 = 10^6, and its closure takes a number of steps proportional to 1,0003=1091{,}000^3 = 10^9. 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 x=yx = y after an unseen write to xx 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 x=yx = y an analysis can prove that x−y=0x - y = 0, together with every other consequence of the equality: x≤yx \le y and y≤xy \le x hold, a test x≠yx \ne y never succeeds, and a bound checked on yy, such as y<ny < n, holds for xx as well. Separate wide intervals cannot show any of these. Two intervals such as x∈[0,100]x \in [0, 100] and y∈[0,100]y \in [0, 100] admit every pair of values, including x=0x = 0 with y=100y = 100, so interval subtraction gives x−y∈[−100,100]x - y \in [-100, 100]. A relational abstract domain, whose summaries record constraints that link variables, keeps x−y≤0x - y \le 0 and y−x≤0y - x \le 0, which exclude such pairs and give x−y=0x - y = 0. 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].