Section 3.26

State explosion

Definition

State explosion is the exponential growth of the number of states that an analysis must examine when it keeps every state apart. A state is one snapshot of a running system: the position in the code and the value of every variable. Each independent yes-or-no choice, such as a branch on an input bit, can double the number of cases; each concurrent thread multiplies it. Counted in paths through the code, the same growth is called path explosion. It is the main practical limit of model checking §3.30 and symbolic execution §3.27.

The idea

A function of 30 lines tests 30 different bits of its input, one if statement per bit. Each if either runs its branch or skips it, so the code has 230=1,073,741,8242^{30} = 1{,}073{,}741{,}824 paths. A tool that checks the paths one at a time, at 1 millisecond per path, needs 1,073,741,8241{,}073{,}741{,}824 ms, which is about 1,073,7421{,}073{,}742 s or 12.4 days. A 31st line doubles that to about 24.9 days.

The cause is multiplication. The state of a whole system combines the states of its parts, and the number of combinations is the product of the numbers of choices. The description of the system grows by addition: one more line, one more variable, one more thread. So the search grows exponentially while the program grows linearly. A combination lock behaves the same way: four digits give 104=10,00010^4 = 10{,}000 combinations, and a fifth digit multiplies the search by 10. The lock picture is accurate except in one respect: in a program many combinations are impossible or lead to the same state, so the true count is often far below the product, and every remedy exploits exactly that.

How it works

The usual sources of multiplication:

Analyses fight the growth in five main ways, each with a cost:

  1. Merging the states of paths that meet into one summary, as abstract interpretation §3.4 does. It is cheap but forgets correlations between variables.

  2. Skipping covered states: remember the states already explored at each program point and drop a new state that one of them covers. In the Linux kernel, eBPF §2.1 (extended Berkeley Packet Filter) programs are checked by the eBPF verifier §3.3, which calls this state pruning.

  3. Symbolic representation: describe large sets of states by formulas or by binary decision diagrams, compressed graphs of yes-or-no functions, instead of listing them.

  4. Reduction and abstraction: explore only one order of thread steps that do not affect each other (partial-order reduction §19.2.11), or drop details that do not matter for the property and restore them only when a spurious error appears (counterexample-guided abstraction refinement §15.13).

  5. Bounding: explore only up to a fixed depth, as bounded model checking §3.29 does, or stop at a resource limit, and report the result as limited.

Worked example

Take three independent input bits b1,b2,b3b_1, b_2, b_3, each 0 or 1, and the code x = 0; if (b1) x = x + 1; if (b2) x = x + 2; if (b3) x = x + 4;, followed by a check that x≤7x \le 7. There are 23=82^3 = 8 paths, and each ends with a different value, x=b1+2b2+4b3x = b_1 + 2b_2 + 4b_3, from 0 (no branch taken) to 1+2+4=71 + 2 + 4 = 7 (all taken). A path-by-path analysis keeps 8 states and checks each one.

An interval analysis §3.9 merges at each point where paths meet, using the join ⊔\sqcup, which gives the smallest interval covering both of its arguments: [0,0]⊔[1,1]=[0,1][0, 0] \sqcup [1, 1] = [0, 1] after the first if, [0,1]⊔[2,3]=[0,3][0, 1] \sqcup [2, 3] = [0, 3] after the second and [0,3]⊔[4,7]=[0,7][0, 3] \sqcup [4, 7] = [0, 7] after the third. One state per point proves x≤7x \le 7. With 30 such if statements adding 1,2,4,…,2291, 2, 4, \dots, 2^{29}, there are 2302^{30} paths, while merging needs 30 joins and ends with [0,230−1][0, 2^{30} - 1]. Merging fails when the property depends on a correlation: “xx is odd exactly when b1=1b_1 = 1” holds on all 8 paths, but the interval [0,7][0, 7] has forgotten it.

Aliasing splits the same way. Let pointers p and q each point to object A or object B, chosen by the input, with A starting at 0. After *p = 1; *q = 2; the value of A depends on the targets of p and q: (A,A)(A, A) gives 2, (A,B)(A, B) gives 1, (B,A)(B, A) gives 2 and (B,B)(B, B) gives 0. Two pointers with two targets each give 22=42^2 = 4 cases and three different results.

Threads multiply faster. Two threads with 3 steps each have 6!/(3!×3!)=720/36=206! / (3! \times 3!) = 720 / 36 = 20 interleavings; three threads with 3 steps each have 9!/(3!)3=362,880/216=1,6809! / (3!)^3 = 362{,}880 / 216 = 1{,}680.

Stated precisely

If a system consists of parts with state sets S1,…,SkS_1, \dots, S_k, its states are the tuples (s1,…,sk)(s_1, \dots, s_k) with each sjs_j in SjS_j, and their number is |S1|×…×|Sk||S_1| \times \dots \times |S_k|, where |S||S| is the number of elements of SS. If every part has at least 2 states, that is at least 2k2^k: exponential in kk, while the description of the system grows in proportion to kk. For nn two-way branches in sequence with independent conditions the number of paths is 2n2^n; for tt threads of kk indivisible steps the number of interleavings is (tk)!/(k!)t(tk)! / (k!)^t.

These counts measure the cases that an analysis keeping every combination apart must examine. For real programs they are upper bounds, because correlated conditions and impossible combinations reduce them, and they do not predict the running time of any particular tool. A finite state space, however large, keeps a question decidable §19.2.1, since an algorithm §14.1 could in principle list every state; state explosion limits what can be answered in practice. Unbounded data, such as unbounded loop counters or unbounded integers, raises the separate problem of undecidability.

Limits and common mistakes

Merging trades the explosion for imprecision. A merged state can contain combinations that no real path produces, so the analysis raises false alarms §1.22; keeping paths apart to avoid this is path sensitivity §3.11, and it brings the explosion back.

Skipping is only as safe as its coverage test. Dropping a new state is correct only if an explored state allows everything the new one allows, for the property being checked; a coverage test that is too generous misses errors.

A limit reached is not a verdict. A tool that stops at a time, memory or state limit and reports “safe” is unsound; it must report “unknown” or reject. The Linux eBPF verifier rejects any program whose check needs more than one million instruction steps, summed over all the paths it explores, even when the program is safe. Rejecting what cannot be checked within the limit is a conservative refusal §3.36.

Program size predicts little. The 30-line function above has over a billion paths, while a long program whose branches test the same condition can have few distinct states.

Questions answered

How do branching, loops, and aliasing multiply states?

They multiply the cases an exact analysis must keep apart, because each adds an independent choice and the choices combine. A two-way branch whose condition the analysis cannot decide splits every case that reaches it into two, so nn independent branches in sequence give 2n2^n paths; 30 branches give 1,073,741,8241{,}073{,}741{,}824. A loop multiplies paths by the number of iterations it may run: with a two-way branch in its body and up to NN iterations there are 2N+1−12^{N+1} - 1 paths, and with no known bound there are infinitely many. Aliasing, where two pointers may refer to the same object, multiplies memory states: if a pointer may refer to any of mm objects, a write through it has mm possible targets, and kk such pointers give up to mkm^k combinations, each leaving memory differently.

The counts are exact products only when the choices are independent; correlated conditions and impossible combinations make them smaller. Analyses fight the growth by merging states where paths meet (which loses correlations), by skipping states already covered, by representing sets of states symbolically, and by stopping at a limit with the answer “unknown” rather than “safe”.

Related entries: Fixed point (§3.22), Symbolic execution (§3.27), Conservative refusal (§3.36), Path sensitivity (§3.11), Alias analysis (§3.18), Bounded model checking (§3.29), Model checking (§3.30), Partial-order reduction and commuting actions (§19.2.11).