Section 3.22

Fixed point

Definition

A fixed point of a function FF is a value xx that FF maps to itself: F(x)=xF(x) = x. In program analysis, FF is the rule for updating the facts about a program. At a fixed point, another application leaves those facts unchanged. For the sets of possible states used here, start with the empty set. If the update preserves set inclusion §15.40 and the sets can grow only finitely many times, this iteration reaches the least fixed point.

The idea

An analyzer wants the possible values of x at the test of the loop x = 0; while (x < 2) x = x + 1;. The test sees 0, then 1, then 2, and then the loop ends. In general an analyzer cannot follow the trips one by one: a loop may run millions of times, or a number of times that depends on input. Instead it writes an equation for the test: the values there are the starting value together with every value that one more trip produces. It solves the equation by repetition: start with no values, apply the rule, and see whether anything new appears. Once one application adds nothing, every further trip is covered, because the next trip starts from values already in the set and so can produce only values already in the set.

A rumor spreading through a village behaves the same way. Each day, everyone who knows the rumor tells their friends. On the first day on which nobody new hears it, the spreading is over for good: the next day the same people tell the same friends. The set of people who know the rumor is then a fixed point of “one day of telling”. The picture stops in one respect. A village is finite, so the spreading must stop. A domain of program facts can be infinite, and then repetition can add facts forever; the finite-height condition below rules that out.

How it works

A fixed-point analysis has three ingredients.

The analysis starts from x0=⊥x_0 = \bot, computes xk+1=F(xk)x_{k+1} = F(x_k) for k=0,1,2,…k = 0, 1, 2, \dots, and stops as soon as xk+1=xkx_{k+1} = x_k. Three facts explain why it stops.

  1. The sequence only goes up. x0⊑x1x_0 \sqsubseteq x_1 holds because ⊥\bot is below everything. If xk⊑xk+1x_k \sqsubseteq x_{k+1}, monotonicity gives F(xk)⊑F(xk+1)F(x_k) \sqsubseteq F(x_{k+1}), which is xk+1⊑xk+2x_{k+1} \sqsubseteq x_{k+2}. By induction every step is either equal or strictly up.

  2. Once equal, always equal. If xk+1=xkx_{k+1} = x_k, then xk+2=F(xk+1)=F(xk)=xk+1x_{k+2} = F(x_{k+1}) = F(x_k) = x_{k+1}.

  3. Strictly up only finitely often. The strict steps form a chain of strictly growing facts, so there are at most hh of them.

The sequence therefore reaches a fact xkx_k with F(xk)=xkF(x_k) = x_k after at most hh applications of FF, and one more application confirms that nothing changes.

That fact is the least fixed point. Let yy be any fixed point, F(y)=yF(y) = y. Then x0=⊥⊑yx_0 = \bot \sqsubseteq y, and if xk⊑yx_k \sqsubseteq y, monotonicity gives xk+1=F(xk)⊑F(y)=yx_{k+1} = F(x_k) \sqsubseteq F(y) = y. So every xkx_k, the final one included, lies below every fixed point: the iteration finds the most precise of all fixed points.

Real analyses do not recompute every program point in each round. A worklist algorithm §14.1 keeps a list of the points whose inputs have changed, recomputes only those, and puts the successors of a point on the list when the facts at that point change. With NN program points, the fact at each point can rise at most hh times, so at most N×hN \times h changes happen in total. Data-flow analysis §3.15 and abstract interpretation §3.4 both rest on this scheme.

Worked example

Take the loop x = 0; while (x < 2) x = x + 1; with x an 8-bit unsigned variable, so that its values are 0 to 255. The facts are sets of possible values at the loop test, ordered by ⊆\subseteq. The update rule collects the starting value 0 and, for every value ss in the current set that passes the test s<2s < 2, the value s+1s + 1 that one more trip produces: F(S)={0}∪{s+1∣s∈S,s<2}.F(S) = \{0\} \cup \{\, s + 1 \mid s \in S,\ s < 2 \,\}.

Iterate from the empty set:

Since x4=x3x_4 = x_3, the iteration stops after four applications of FF, three of them strict increases. The fixed point {0,1,2}\{0, 1, 2\} says that the test sees x equal to 0, 1 or 2 over all its visits. It describes all visits together, not the value at one moment. The loop is left when the test fails, which within {0,1,2}\{0, 1, 2\} happens only for 2, so x is 2 after the loop.

Monotonicity, checked on one pair: {0}⊆{0,1}\{0\} \subseteq \{0, 1\}, and F({0})={0,1}⊆F({0,1})={0,1,2}F(\{0\}) = \{0, 1\} \subseteq F(\{0, 1\}) = \{0, 1, 2\}. Height: a strictly growing chain of sets of the 256 possible values gains at least one value per step, so it has at most 256 steps, and h=256h = 256. The iteration needed only 3 strict steps.

A set can be closed under the rule without being a fixed point. Take C={0,1,2,7}C = \{0, 1, 2, 7\}. Then F(C)={0}∪{1,2}={0,1,2}F(C) = \{0\} \cup \{1, 2\} = \{0, 1, 2\}, because 7 fails the test and has no successor. So F(C)⊆CF(C) \subseteq C but F(C)≠CF(C) \neq C. The set CC is a correct description of the loop test, because it contains every value that really occurs there, but it is less precise: it contains 7, which never occurs. In program-verification terms, CC is an inductive invariant §19.2.4 of the loop, and the least fixed point {0,1,2}\{0, 1, 2\} lies inside it, as it lies inside every set closed under FF.

Stated precisely

Let (L,⊑)(L, \sqsubseteq) be a partially ordered set with a least element ⊥\bot. Partially ordered means that ⊑\sqsubseteq is reflexive (x⊑xx \sqsubseteq x), antisymmetric (x⊑yx \sqsubseteq y and y⊑xy \sqsubseteq x give x=yx = y) and transitive (x⊑yx \sqsubseteq y and y⊑zy \sqsubseteq z give x⊑zx \sqsubseteq z). Let F:L→LF : L \to L be monotone, and write FkF^k for kk applications of FF, with F0(x)=xF^0(x) = x.

The analysis is sound, meaning that its facts include every behavior the program can really have, when FF overapproximates the start and one real step of the program: F(x)F(x) describes every starting state, and every state that one real step can produce from a state described by xx. Abstract interpretation, the framework of Cousot and Cousot (1977), makes this condition precise. When it holds, lfp(F)\mathrm{lfp}(F) and every xx with F(x)⊑xF(x) \sqsubseteq x describe all reachable states §3.21. Stopping at such an xx instead of at lfp(F)\mathrm{lfp}(F) keeps soundness §3.7 and loses precision.

A must-analysis, which keeps only facts that hold on every path, such as the set of expressions computed on every path to a point and still current there, runs the other way. It starts from the top element, all facts, and removes facts until nothing changes, which gives the greatest fixed point. The same argument applies with the order reversed and finite depth in place of finite height.

Limits and common mistakes

Stopping proves only that the facts are closed under FF. If a transfer function is wrong, for example one that forgets that a store through a pointer can change another variable, the iteration stops at a fixed point of the wrong rule, and the facts say nothing reliable about the program.

Monotonicity is not optional. On the two sets ∅\emptyset and {0}\{0\}, the rule GG with G(∅)={0}G(\emptyset) = \{0\} and G({0})=∅G(\{0\}) = \emptyset is not monotone and has no fixed point, and the iteration alternates between the two sets forever, although this domain has height 1.

Finite height is not automatic. Ranges of integers, as used in interval analysis §3.9, form a domain of infinite height. For x = 0; while (true) x = x + 1; over unbounded integers, the iteration gives [0,0][0, 0], [0,1][0, 1], [0,2][0, 2] and so on without end. Widening §3.23 forces termination by jumping ahead, here to [0,+∞)[0, +\infty). The result is closed under FF but in general is not the least fixed point; narrowing can then recover part of the lost precision.

The start matters. The argument needs x0=⊥x_0 = \bot, or at least a start x0x_0 with x0⊑F(x0)x_0 \sqsubseteq F(x_0). From an arbitrary start the sequence need not go up, and it can stop at a fixed point other than the least one.

A finite bound can still be enormous. Sets of values of one 32-bit variable form a domain of height 2322^{32}, so finite height alone does not make an analysis fast. Practical domains use compact descriptions, such as ranges and bit masks §4.50, whose height is small.

The least fixed point is the best answer that the domain and the rule allow, not the exact behavior of the program. A domain that joins the facts of different paths, or that describes each variable separately, can include states that never occur.

A fixed point of an analysis is a property of the facts, not of the run of the program: a loop that runs forever has a perfectly good fixed point of facts.

Questions answered

Why does a monotone ascending analysis over a finite-height domain stabilize?

The analysis stabilizes because its sequence of facts can only go up, can go strictly up only a bounded number of times, and stays fixed once it stops going up. Let FF be the update rule, applied repeatedly from the least element ⊥\bot (no facts), giving x0=⊥x_0 = \bot, x1=F(x0)x_1 = F(x_0), x2=F(x1)x_2 = F(x_1), and so on.

  1. Up only. x0⊑x1x_0 \sqsubseteq x_1 because ⊥\bot is below everything, and monotonicity (a⊑ba \sqsubseteq b implies F(a)⊑F(b)F(a) \sqsubseteq F(b)) turns each xk⊑xk+1x_k \sqsubseteq x_{k+1} into xk+1⊑xk+2x_{k+1} \sqsubseteq x_{k+2}.

  2. Strictly up at most hh times. Finite height hh means that no chain of strictly growing facts has more than hh steps, and the strict steps of the sequence form such a chain.

  3. Stable once still. If xk+1=xkx_{k+1} = x_k, then xk+2=F(xk+1)=F(xk)=xk+1x_{k+2} = F(x_{k+1}) = F(x_k) = x_{k+1}, and so on forever.

So after at most hh applications the sequence reaches an xkx_k with F(xk)=xkF(x_k) = x_k, a fixed point. It is the least fixed point, because induction with monotonicity shows that every xkx_k lies below any fixed point.

Both conditions are needed. Without monotonicity, a rule can alternate between two facts forever, even in a domain of height 1. Without finite height, a monotone rule can climb forever, as the ranges [0,0][0, 0], [0,1][0, 1], [0,2][0, 2], and so on do for a counter in an endless loop; widening then forces termination at a coarser fact that is closed under the rule but in general is not the least fixed point. For the loop x = 0; while (x < 2) x = x + 1;, the sets of possible values at the loop test go ∅\emptyset, {0}\{0\}, {0,1}\{0, 1\}, {0,1,2}\{0, 1, 2\}, {0,1,2}\{0, 1, 2\} and stop.

Related entries: Abstract interpretation (§3.4), Widening and narrowing (§3.23), Loop invariant (§3.24), Data-flow analysis (§3.15), Reachability (§3.21), Inductive invariant and its three obligations (§19.2.4), Partial order, lattice, join, and meet (§19.2.6). Further reading: [68, 69, 265].