Section 3.9

Interval analysis

Definition

Interval analysis is a static analysis §3.1 that computes, for each integer variable at each point of a program, an interval [l,u][l, u]: a lower bound ll and an upper bound uu such that every value the variable can hold at that point lies between them. It is abstract interpretation §3.4, running a program on summaries of its values instead of on the values themselves, with intervals as the summaries. It is used to prove that array §14.5 indices stay in bounds and that arithmetic stays within the range of its type.

The idea

A program reads one byte bb from a message and uses i=⌊b/16⌋i = \lfloor b / 16 \rfloor, where ⌊⋅⌋\lfloor \cdot \rfloor means rounding down, as an index into a table of 16 entries. To show that the access is safe for every message, a checker could try all 256 byte values. With two input bytes there are already 256×256=65,536256 \times 256 = 65{,}536 combinations, and with a 64-bit input trying every value is impossible. Interval analysis keeps only the two ends: b∈[0,255]b \in [0, 255], so i∈[⌊0/16⌋,⌊255/16⌋]=[0,15]i \in [\lfloor 0 / 16 \rfloor, \lfloor 255 / 16 \rfloor] = [0, 15], and since 15<1615 < 16 the access is safe for every possible byte at once.

A thermometer that records only the day’s lowest and highest temperature is a fair picture. Like an interval, it cannot tell which temperatures in between occurred, and it cannot relate two readings, such as temperature and humidity, just as intervals cannot relate two variables (the Abstract domain §3.6 entry). One point has no counterpart in the picture: a computer’s integers have a fixed width and wrap around when a result does not fit, so the analysis must compute with the machine’s arithmetic, not with the integers of mathematics.

How it works

The analysis keeps one interval per variable at each point of the program.

  1. Start. A constant cc gets [c,c][c, c]. An input gets the full range of its type: an unsigned byte gets [0,255][0, 255].

  2. Arithmetic. Each operation has a transfer function, a rule that computes the result interval from the operand intervals. Addition adds the two lower ends and adds the two upper ends. Subtraction takes the smallest first value minus the largest second value, and the largest first value minus the smallest second value. Multiplication takes the smallest and the largest of the four products of the ends.

  3. Branches. At a test such as x<kx < k for a constant kk, the edge on which the test holds receives the interval of xx cut down to the values below kk, and the other edge receives the values at or above kk. If a cut leaves nothing, that edge cannot be reached.

  4. Merges. Where two paths meet, the analysis joins their intervals into the smallest interval that contains both.

  5. Loops. The analysis goes round each loop again until no interval changes, which is called reaching a fixed point §3.22. Because an interval can grow by one at every round, the analysis applies widening, which moves a bound that keeps growing straight to infinity or to the limit of its type, and then narrowing, which recovers some of the precision that widening gave up (the Widening and narrowing §3.23 entry).

  6. Checks. At each array access, division or other operation with a precondition, the analysis compares the interval with the precondition. An index interval inside the array’s valid range proves every run safe; otherwise the analysis reports a possible error.

Machine arithmetic changes step 2. An unsigned ww-bit integer holds the values 0 to 2w−12^w - 1 and keeps every result modulo 2w2^w: a sum of 2w2^w or more wraps around and loses 2w2^w (the Unsigned modular arithmetic §4.16 entry). A signed ww-bit integer in two’s complement, the usual encoding in which the top bit counts as −2w−1-2^{w-1}, holds −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1, and a result outside that range also wraps by 2w2^w. The same bit pattern means different numbers in the two readings, so an analyzer of machine code, whose comparisons can be signed or unsigned, keeps both a signed and an unsigned interval for each value (the Signed versus unsigned comparison §2.12 entry). Linux checks eBPF §2.1 (extended Berkeley Packet Filter) programs, small programs loaded into the kernel, with the eBPF verifier §3.3, which keeps both kinds of bounds for each 64-bit value and for its lower 32 bits.

Worked example

Take an unsigned 3-bit register, so w=3w = 3. It holds the 23=82^3 = 8 values 0 to 7, and every result is kept modulo 8.

  1. x∈[1,3]x \in [1, 3]; compute x+2x + 2. The mathematical result is [1+2,3+2]=[3,5][1 + 2, 3 + 2] = [3, 5]. Since 5≤75 \le 7, nothing wraps, and the result is [3,5][3, 5].

  2. x∈[6,7]x \in [6, 7]; compute x+2x + 2. The mathematical result is [8,9][8, 9]. Both ends are at least 8, so every sum wraps once: 8−8=08 - 8 = 0 and 9−8=19 - 8 = 1. The result is [0,1][0, 1], which is exact.

  3. x∈[5,7]x \in [5, 7]; compute x+1x + 1. The mathematical result is [6,8][6, 8]. The sums 6 and 7 fit, and 8 wraps to 8−8=08 - 8 = 0, so the stored results are 6, 7 and 0. They lie at both ends of the range, and the smallest single interval that contains them is [0,7][0, 7]: all 8 values, of which only 3 occur.

Now suppose the program sets y=x+1y = x + 1 as in case 3, then takes one branch when y≥6y \ge 6 and another, which performs an unsafe operation, when y<6y < 6. An analyzer that ignores wrapping computes y∈[6,8]y \in [6, 8], cuts the branch y<6y < 6 down to nothing, and declares that branch unreachable. On the real machine x=7x = 7 gives y=0y = 0, which takes that branch. The analyzer has missed a real execution, so it is unsound. With the wrapping rule, y∈[0,7]y \in [0, 7], the branch y<6y < 6 receives [0,5][0, 5], and the analyzer checks the unsafe operation and reports it.

The signed reading wraps at a different place. With 3 bits, two’s complement holds −4-4 to 3. For x∈[2,3]x \in [2, 3], x+1x + 1 gives the mathematical [3,4][3, 4]; 4 is above 3 and wraps to 4−8=−44 - 8 = -4, so the signed results are 3 and −4-4, and the smallest signed interval is [−4,3][-4, 3], every value. Read as unsigned, the same input bit patterns 010 and 011 are 2 and 3, the results 011 and 100 are 3 and 4, and the unsigned interval [3,4][3, 4] is exact. Keeping both intervals keeps the precise one.

Stated precisely

For intervals over the mathematical integers, with x∈[a,b]x \in [a, b] and y∈[c,d]y \in [c, d]: x+y∈[a+c,b+d],x−y∈[a−d,b−c],x×y∈[min(a⋅c,a⋅d,b⋅c,b⋅d),max(a⋅c,a⋅d,b⋅c,b⋅d)],\begin{aligned} x + y &\in [a + c,\ b + d], \\ x - y &\in [a - d,\ b - c], \\ x \times y &\in [\min(a \cdot c, a \cdot d, b \cdot c, b \cdot d),\ \max(a \cdot c, a \cdot d, b \cdot c, b \cdot d)], \end{aligned} and the join is [a,b]⊔[c,d]=[min(a,c),max(b,d)][a, b] \sqcup [c, d] = [\min(a, c), \max(b, d)]. On the edge where x<kx < k holds, x∈[a,min(b,k−1)]x \in [a, \min(b, k - 1)]; on the other edge, x∈[max(a,k),b]x \in [\max(a, k), b]. An interval whose lower end exceeds its upper end is empty, and the edge that receives it is unreachable.

For unsigned ww-bit addition, let M=2wM = 2^w, let both operands lie in [0,M−1][0, M - 1], and let s=a+cs = a + c and t=b+dt = b + d be the exact sums of the ends. Because t≤2M−2t \le 2M - 2, every sum wraps at most once, and (x+y)modM∈{[s,t]if t≤M−1 (no sum wraps),[s−M,t−M]if s≥M (every sum wraps),[0,M−1]if s≤M−1<t (some sums wrap).(x + y) \bmod M \in \begin{cases} [s,\ t] & \text{if } t \le M - 1 \text{ (no sum wraps)}, \\ [s - M,\ t - M] & \text{if } s \ge M \text{ (every sum wraps)}, \\ [0,\ M - 1] & \text{if } s \le M - 1 < t \text{ (some sums wrap)}. \end{cases} In the third case the true results are the two pieces [s,M−1][s, M - 1] and [0,t−M][0, t - M]. A single plain interval that holds both must contain 0 and M−1M - 1, so it is the whole range. An analyzer can keep the pieces exactly with a wrapped interval, one allowed to run past M−1M - 1 and continue from 0, or with a set of intervals.

A transfer function f♯f^\sharp for an operation ff is sound when, for all x∈[a,b]x \in [a, b] and y∈[c,d]y \in [c, d], the value that the machine actually stores for f(x,y)f(x, y) lies in f♯([a,b],[c,d])f^\sharp([a, b], [c, d]). A formula derived for mathematical integers fails this test as soon as a result can wrap.

Limits and common mistakes

Intervals lose relations between variables. After y=xy = x with x∈[0,3]x \in [0, 3], the analysis computes x−y∈[−3,3]x - y \in [-3, 3] instead of 0. The Abstract domain entry describes relational domains §15.12 that keep such facts.

Joins add values that no path produced: joining [0,0][0, 0] and [10,10][10, 10] gives [0,10][0, 10]. An interval also cannot express a gap. It cannot say that x∈[−1,1]x \in [-1, 1] is never 0, so it cannot prove that a division by xx is safe.

Each operation needs its own correct rule. Division must report a possible error when the divisor’s interval contains 0. A shift by an amount equal to or larger than the width is undefined behavior §4.12 in C, and instruction sets differ in what they do with it (the Shift semantics §4.20 entry). A conversion to a narrower type keeps only the low bits and can wrap.

Whether overflow wraps is a property of the language, not of the analysis. Machine instructions and unsigned arithmetic in C wrap. Signed overflow §4.15 in C is undefined behavior, so an analyzer for C reports it as an error instead of computing a wrapped value (the Signed integer overflow entry).

Questions answered

How does wrapping arithmetic change an interval transfer function?

With wrapping arithmetic, an interval transfer function must compute results modulo 2w2^w for a ww-bit value, so the result of an operation is no longer simply the interval of the mathematical results. For unsigned addition of [a,b][a, b] and [c,d][c, d], the rule first computes the exact sums s=a+cs = a + c and t=b+dt = b + d, then distinguishes three cases. If tt fits in ww bits, the result is [s,t][s, t], as in mathematics. If ss is already at least 2w2^w, every sum wraps and the result is [s−2w,t−2w][s - 2^w, t - 2^w]. If only the larger sums wrap, the true results split into a piece at the top of the range and a piece at the bottom, and a single plain interval must widen to the whole range [0,2w−1][0, 2^w - 1], losing all precision, unless the analyzer can represent a wrapped interval or several pieces.

For a 3-bit register, [5,7]+1[5, 7] + 1 stores 6, 7 and 0, so the sound plain result is [0,7][0, 7], not [6,8][6, 8]. With [6,8][6, 8], a branch taken when the value is below 6 would look unreachable, although 7+17 + 1 wraps to 0 and takes it. Signed values wrap at a different point from unsigned ones, so analyzers of machine code keep a signed and an unsigned interval for each value.

Related entries: Abstract domain (§3.6), ALU32 versus ALU64 (§2.11), Unsigned modular arithmetic (§4.16), Signed versus unsigned comparison (§2.12), Signed integer overflow (§4.15), Widening and narrowing (§3.23). Further reading: [2, 68].