Definition
A fixed point of a function is a value that maps to itself: . In program analysis, 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.
A domain of facts with an order. Each element of the domain describes what the analysis knows, such as a set of possible values of
x. The order , read “is below”, says when one fact claims no more possible behavior than another; for sets of possible values, means . A lower fact is more precise. The domain has a least element , read “bottom”, which here is the empty set: no behavior found yet. The join of two sets is their union, the smallest set that contains both. Their meet is their intersection, the largest set contained in both. An ordered domain with a join and a meet for every pair is a lattice §19.2.6.An update rule . One application of recomputes the facts at every program point from the facts at the points before it. It uses the transfer function of each statement, which gives the effect of the statement on facts, and the join where paths meet.
Two properties. is monotone: implies , so more facts in never give fewer facts out. The domain has finite height : every chain of strictly growing facts has at most steps, where means and .
The analysis starts from , computes for , and stops as soon as . Three facts explain why it stops.
The sequence only goes up. holds because is below everything. If , monotonicity gives , which is . By induction every step is either equal or strictly up.
Once equal, always equal. If , then .
Strictly up only finitely often. The strict steps form a chain of strictly growing facts, so there are at most of them.
The sequence therefore reaches a fact with after at most applications of , and one more application confirms that nothing changes.
That fact is the least fixed point. Let be any fixed point, . Then , and if , monotonicity gives . So every , 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 program points, the fact at each point can rise at most times, so at most 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 . The update rule collects the starting value 0 and, for every value in the current set that passes the test , the value that one more trip produces:
Iterate from the empty set:
;
;
;
;
, because 2 fails the test and adds nothing.
Since , the iteration stops after four applications of , three of them strict increases. The fixed point 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 happens only for 2, so x is 2 after the loop.
Monotonicity, checked on one pair: , and . 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 . The iteration needed only 3 strict steps.
A set can be closed under the rule without being a fixed point. Take . Then , because 7 fails the test and has no successor. So but . The set 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, is an inductive invariant §19.2.4 of the loop, and the least fixed point lies inside it, as it lies inside every set closed under .
Stated precisely
Let be a partially ordered set with a least element . Partially ordered means that is reflexive (), antisymmetric ( and give ) and transitive ( and give ). Let be monotone, and write for applications of , with .
Iteration. If has no infinite strictly ascending chain, which is called the ascending chain condition, the sequence is ascending and reaches some with . That element is the least fixed point of , written . If every strictly ascending chain in has at most steps, then .
Existence without iteration. If is a complete lattice, meaning that every set of elements has a least upper bound and a greatest lower bound, then every monotone has a least fixed point, and This is the Knaster–Tarski theorem, which Alfred Tarski published in general form in 1955 [265].
The analysis is sound, meaning that its facts include every behavior the program can really have, when overapproximates the start and one real step of the program: describes every starting state, and every state that one real step can produce from a state described by . Abstract interpretation, the framework of Cousot and Cousot (1977), makes this condition precise. When it holds, and every with describe all reachable states §3.21. Stopping at such an instead of at 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 . 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 and , the rule with and 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 , , and so on without end. Widening §3.23 forces termination by jumping ahead, here to . The result is closed under but in general is not the least fixed point; narrowing can then recover part of the lost precision.
The start matters. The argument needs , or at least a start with . 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 , 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 be the update rule, applied repeatedly from the least element (no facts), giving , , , and so on.
Up only. because is below everything, and monotonicity ( implies ) turns each into .
Strictly up at most times. Finite height means that no chain of strictly growing facts has more than steps, and the strict steps of the sequence form such a chain.
Stable once still. If , then , and so on forever.
So after at most applications the sequence reaches an with , a fixed point. It is the least fixed point, because induction with monotonicity shows that every 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 , , , 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 , , , , 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].