Section 15.11

Reduced product abstract domain

Definition

A reduced product is a combination of two abstract domains §3.6 in abstract interpretation §3.4, the method that proves facts about a program by computing with descriptions of sets of values instead of with the values. An abstract domain is one kind of description, such as an interval or a pattern of known bits. The product describes a set by one description from each domain and means the values that satisfy both. The reduction passes facts between the two descriptions, tightening each without excluding any value that both allow.

The idea

A verifier must decide whether a 4-byte read at offset xx of a 16-byte buffer stays inside the buffer. The read covers bytes xx to x+3x + 3, so it is safe when x+4≤16x + 4 \le 16, that is when x≤12x \le 12. The program first clears the two lowest bits of an unknown value with a bitwise AND, which makes xx a multiple of 4, and then rejects any xx greater than 15.

Two analyses run side by side. A known-bits analysis tracks, for each bit, whether it is known to be 0, known to be 1, or unknown; from the AND it knows that the two lowest bits of xx are 0 and nothing about the others. Interval analysis §3.9 tracks a lowest and a highest possible value; from the comparison it knows that 0≤x≤150 \le x \le 15. Alone, each fails. The interval allows x=15x = 15, and 15+4=19>1615 + 4 = 19 > 16. The bit pattern allows every multiple of 4 that fits in the register, including huge ones.

Together they settle the question: the values that both allow are 0, 4, 8 and 12, and all of them pass. Running the analyses side by side is not enough, because each keeps only its own kind of fact. The reduced product adds the step that tells each analysis what the other’s facts imply, so that the interval tightens to 0≤x≤120 \le x \le 12.

How it works

The parts, for values read as unsigned numbers of a fixed width:

The reduction runs after each operation of the analysis, or before a safety check:

  1. Bits to interval. The smallest value the bits allow is vv, with every unknown bit 0. The largest is v+μv + \mu, with every unknown bit 1; adding works because vv and μ\mu have no 1 bits in common. Raise 𝑙𝑜\mathit{lo} to vv if it is smaller, and lower h𝑖\mathit{hi} to v+μv + \mu if it is larger.

  2. Interval to bits. All values from 𝑙𝑜\mathit{lo} to h𝑖\mathit{hi} share the leading bits on which 𝑙𝑜\mathit{lo} and h𝑖\mathit{hi} agree, down to the first bit where they differ. Mark those leading bits as known.

  3. Empty check. If 𝑙𝑜>h𝑖\mathit{lo} > \mathit{hi}, or a known bit contradicts the interval, no value satisfies both components. The program point is then unreachable, and the analysis records the empty description, called bottom.

  4. Repeat steps 1 and 2 until neither component changes. Each step removes only values that one component already excludes, so the meaning of the pair never changes.

These simple rules do not always find the tightest interval. With the interval [1,12][1, 12] and the two lowest bits known to be 0, step 1 leaves 𝑙𝑜=1\mathit{lo} = 1, although the smallest allowed value is 4. A stronger rule for known low zero bits rounds 𝑙𝑜\mathit{lo} up and h𝑖\mathit{hi} down to the nearest multiples of the matching power of two. Both rules are sound; the stronger one is more precise.

Worked example

To keep the bit patterns short, the example uses 8-bit unsigned values; a verifier for 64-bit registers applies the same rules to 64 bits. In a bit pattern, ? marks an unknown bit. The starting facts are the interval [0,15][0, 15] and the pattern ??????00, that is v=𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶v = \mathtt{00000000} and μ=𝟷𝟷𝟷𝟷𝟷𝟷𝟶𝟶\mu = \mathtt{11111100}.

Round 1, step 1. The bits allow at least v=0v = 0 and at most v+μ=𝟷𝟷𝟷𝟷𝟷𝟷𝟶𝟶=252v + \mu = \mathtt{11111100} = 252. The interval becomes [max(0,0),min(15,252)]=[0,15][\max(0, 0), \min(15, 252)] = [0, 15]: no change.

Round 1, step 2. In binary, 𝑙𝑜=𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶\mathit{lo} = \mathtt{00000000} and h𝑖=𝟶𝟶𝟶𝟶𝟷𝟷𝟷𝟷\mathit{hi} = \mathtt{00001111}. They agree on the four leading bits, all 0, and first differ at the fifth. So the four leading bits are known to be 0, and the pattern becomes 0000??00: μ=𝟶𝟶𝟶𝟶𝟷𝟷𝟶𝟶\mu = \mathtt{00001100}.

Round 2, step 1. Now v+μ=𝟶𝟶𝟶𝟶𝟷𝟷𝟶𝟶=12v + \mu = \mathtt{00001100} = 12, so h𝑖\mathit{hi} drops to min(15,12)=12\min(15, 12) = 12 and the interval becomes [0,12][0, 12]. Round 2, step 2. 𝑙𝑜=𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶\mathit{lo} = \mathtt{00000000} and h𝑖=𝟶𝟶𝟶𝟶𝟷𝟷𝟶𝟶\mathit{hi} = \mathtt{00001100} agree on the same four leading bits, so nothing new is learned. Round 3 changes nothing, and the reduction stops.

Result. The interval [0,12][0, 12] with the pattern 0000??00 allows exactly 0, 4, 8 and 12, four values. The interval [0,15][0, 15] alone allowed 16 values, and the pattern ??????00 alone allowed 26=642^{6} = 64 values on 8 bits. The bounds check now passes, because 12+4=16≤1612 + 4 = 16 \le 16.

Alignment §4.22. Suppose the buffer starts at address 0x2000, which is 8192, a multiple of 4. The read address 8192+x8192 + x has its two lowest bits 0 for every allowed xx, because both terms have; the largest, 8192+12=8204=4×20518192 + 12 = 8204 = 4 \times 2051, is a multiple of 4. So the 4-byte read is aligned at every allowed offset. The alignment fact comes from the bits, and the bounds fact from the interval tightened by the bits.

Stated precisely

Let γ1\gamma_1 and γ2\gamma_2 be the concretization §19.2.7 functions of the two domains: γ1(a1)\gamma_1(a_1) is the set of concrete values that the description a1a_1 allows, and likewise for γ2\gamma_2. The product domain consists of pairs (a1,a2)(a_1, a_2) with the meaning γ(a1,a2)=γ1(a1)∩γ2(a2)\gamma(a_1, a_2) = \gamma_1(a_1) \cap \gamma_2(a_2). A reduction is a function ρ\rho on pairs such that, for every pair aa, γ(ρ(a))=γ(a)\gamma(\rho(a)) = \gamma(a), so no value is lost or added, and each component of ρ(a)\rho(a) allows no more values than the matching component of aa.

The best reduction maps a pair to the most precise pair with the same meaning: ρ*(a1,a2)=(α1(S),α2(S))\rho^{*}(a_1, a_2) = (\alpha_1(S), \alpha_2(S)) with S=γ(a1,a2)S = \gamma(a_1, a_2), where αi(S)\alpha_i(S), the abstraction function of domain ii, gives the most precise description of the set SS in that domain. Patrick and Radhia Cousot defined the reduced product in 1979 [382] as the product domain in which pairs with the same meaning are treated as one, which is what applying ρ*\rho^{*} achieves.

Low zero bits and intervals. For unsigned ww-bit values, if the lowest kk bits are known to be 0, every allowed value is a multiple of 2k2^{k}, and the interval [𝑙𝑜,h𝑖][\mathit{lo}, \mathit{hi}] can be replaced by [2k⌈𝑙𝑜/2k⌉,2k⌊h𝑖/2k⌋],\bigl[\, 2^{k} \lceil \mathit{lo} / 2^{k} \rceil,\ 2^{k} \lfloor \mathit{hi} / 2^{k} \rfloor \,\bigr], where ⌈y⌉\lceil y \rceil rounds yy up and ⌊y⌋\lfloor y \rfloor rounds it down to a whole number. For [0,15][0, 15] and k=2k = 2 this gives [4×0,4×3]=[0,12][4 \times 0, 4 \times 3] = [0, 12]; for [1,12][1, 12] it gives [4×1,4×3]=[4,12][4 \times 1, 4 \times 3] = [4, 12]. If a base address bb is a multiple of 2k2^{k}, then b+xb + x is a multiple of 2k2^{k} for every allowed xx, even if the addition wraps around modulo 2w2^{w}, because 2w2^{w} is itself a multiple of 2k2^{k}.

Soundness §3.7. A reduction step is sound when every value allowed by the pair before the step is still allowed after it. The steps above are sound because each removes only values that one component already excludes.

Limits and common mistakes

Reduction changes precision, not meaning. A rule that removes a value that both components allow is unsound, and a verifier that uses it can accept a program that reads outside its buffer.

Both components must describe the same bit width and the same reading of the bits. In two’s complement, the signed value −4-4 has the 64-bit pattern 0xfffffffffffffffc, which read as unsigned is 264−42^{64} - 4. Rules written for unsigned intervals must not be applied to signed ones without conversion; the eBPF verifier §3.3, for example, keeps signed and unsigned intervals separately and reduces among all of them.

A reduced product of domains that each describe one variable still describes each variable separately. It cannot record a relation such as x<yx < y between two variables; that needs a relational abstract domain §15.12.

Reduction costs time and can interfere with termination. An implementation can bound the number of rounds, or exchange facts only once. Reducing right after a widening operator §3.23, the step that forces a loop analysis to finish by giving up precision, can undo the widening and keep the analysis from finishing, so analyses avoid reducing at that point or use a widening designed for the product.

The best reduction can be expensive, or impossible to compute, for rich domains. Practical tools use partial reductions such as the simple rules above.

Questions answered

How can known low zero bits refine an interval’s alignment facts?

Known low zero bits refine an interval by ruling out every value in it that is not a multiple of the matching power of two, so the interval’s ends move inward to the nearest such multiples, and the bits themselves prove alignment. If the lowest kk bits of an unsigned value xx are known to be 0, then xx is a multiple of 2k2^{k}. An interval [𝑙𝑜,h𝑖][\mathit{lo}, \mathit{hi}] for xx can then be tightened to [2k⌈𝑙𝑜/2k⌉,2k⌊h𝑖/2k⌋][2^{k} \lceil \mathit{lo} / 2^{k} \rceil, 2^{k} \lfloor \mathit{hi} / 2^{k} \rfloor], where the brackets round up and down to whole numbers, without losing any possible value. With xx in [0,15][0, 15] and its two lowest bits 0, the interval becomes [0,12][0, 12], and the possible values are 0, 4, 8 and 12.

The refinement matters in two ways. The tighter upper end can prove a bounds check that the interval alone fails: a 4-byte read at offset xx of a 16-byte buffer is safe because 12+4=1612 + 4 = 16, while 15+4=1915 + 4 = 19 would run past the end. The bit fact, not the interval, carries the alignment: added to a base address that is a multiple of 4, every possible xx gives a multiple of 4, so the read is aligned. Combining an interval with a known-bits description, together with a step that passes such consequences between them, is a reduced product of the two abstract domains. The refinement is sound only if it removes no value that both descriptions allow, and only if both describe the same bit width and the same unsigned reading of the bits.

Related entries: Abstract interpretation (§3.4), Interval analysis (§3.9), Bitmask abstract value (tnum) (§3.10), Widening and narrowing (§3.23), Alignment (§4.22), Abstract domain (§3.6), Relational abstract domain (§15.12), Abstraction, concretization, and Galois connection (§19.2.7). Further reading: [2, 68, 69, 382].