Section 3.11

Path sensitivity

Definition

Path sensitivity is the property of a static analysis §3.1 that the facts it holds at a program point can depend on the path that reached the point, meaning the sequence of branch outcomes taken on the way. A path-sensitive analysis follows the two sides of each branch separately, uses the branch condition to narrow the facts on each side, and keeps facts from different paths apart instead of merging them into one summary. A path-insensitive analysis keeps a single summary per program point for all the paths that reach it.

The idea

A function sets a pointer pp to null, the special value that refers to no object, and points pp at a buffer only when a count nn is positive. Later it writes through pp, again only when n>0n > 0. Every run is safe: the write happens in exactly the runs where pp was set. A path-insensitive analysis merges the two outcomes of the first test, so after it pp is “null or the buffer”. At the write it no longer knows that the two tests agree, and it reports a possible write through a null pointer §4.26, a false alarm §1.22. A path-sensitive analysis keeps two states: one where n>0n > 0 and pp points to the buffer, and one where n≤0n \le 0 and pp is null. At the second test the second state contradicts n>0n > 0, so only the first state reaches the write, and the write is proved safe.

The same reasoning decides when a check on one variable says something about another. If qq is a copy of pp, the test q≠nullq \ne \text{null} holds on exactly the paths where p≠nullp \ne \text{null} holds, but only an analysis that has recorded p=qp = q can use that.

How it works

A path-sensitive analysis works in five steps.

  1. Split at a branch. For a branch on a condition CC, the analysis sends to the true edge only the states in which CC can hold, narrowed by CC, and sends to the false edge the states narrowed by the opposite of CC. A state that contradicts the condition does not follow that edge.

  2. Keep paths apart at merges. Where two edges meet, a path-insensitive analysis joins the two summaries into one; a path-sensitive analysis keeps both, each with its own facts.

  3. Drop infeasible paths. A later test that repeats or follows from an earlier one sends each kept state only along the edge that its facts allow, so paths whose conditions contradict each other are never analyzed.

  4. Narrow aliases through recorded equalities. Two names are aliases when they hold the same pointer (the Alias analysis §3.18 entry). A condition on one alias narrows another only if the state records that both hold the same value at the test. Linux checks every eBPF §2.1 (extended Berkeley Packet Filter) program, a small program loaded into the kernel, with the eBPF verifier §3.3, which records this with identifiers. Every copy of a pointer returned by a map lookup, a search in a key-value table that the program shares with the kernel, carries the same identifier; the pointer is either null or points to the stored value. When one copy is checked and found not to be null, all copies with that identifier become non-null. Assigning a new value to a name ends its link to the others.

  5. Control the cost. The number of paths doubles with each independent branch. Analyzers merge states when the merge loses no fact they need, stop following a path whose state is contained in a state already proved safe at the same instruction (the eBPF verifier calls this pruning), and give up when a work limit is reached.

Worked example

Let 0 stand for null and 1000 for the address of an object. A lookup sets pp to 0 or 1000, and the program copies it: q=pq = p. The possible states (p,q)(p, q) are (0,0)(0, 0) and (1000,1000)(1000, 1000). The test q≠0q \ne 0 keeps only the states in which it holds, so the true edge receives (1000,1000)(1000, 1000) alone. On that edge p=1000p = 1000, so a read through pp cannot follow a null pointer.

An analysis that keeps a separate fact per name, “null or not null”, loses this. Before the test it knows that pp may be null and that qq may be null. The test narrows qq to not null and leaves pp as possibly null, so the analysis must reject the read through pp, although the read is safe. Here the recorded equality, not the branch alone, carries the needed fact.

If pp and qq come from two independent lookups, with pp equal to 0 or 1000 and qq equal to 0 or 2000, there are 2×2=42 \times 2 = 4 states: (0,0)(0, 0), (0,2000)(0, 2000), (1000,0)(1000, 0) and (1000,2000)(1000, 2000). The test q≠0q \ne 0 leaves (0,2000)(0, 2000) and (1000,2000)(1000, 2000), and in the first of these p=0p = 0. A check on qq does not justify a read through pp, and an analysis that narrowed pp here would be unsound. Likewise, if the program assigns a new lookup result to pp after the test, the old test says nothing about the new pp.

The cost shows in the count of paths. A function with 10 independent tests in sequence has 210=10242^{10} = 1024 paths, and one with 30 such tests has 230=1,073,741,8242^{30} = 1{,}073{,}741{,}824, more than a billion.

Stated precisely

Let SS be the set of concrete states §3.5 that reach a branch on a condition CC. The true edge receives ST={s∈S:C(s)}S_T = \{ s \in S : C(s) \} and the false edge receives SF={s∈S:¬C(s)}S_F = \{ s \in S : \neg C(s) \}. Let γ(a)\gamma(a) be the set of concrete states that an abstract value aa stands for. An abstract filter filterC\mathrm{filter}_C is sound when γ(filterC(a))⊇{s∈γ(a):C(s)}\gamma(\mathrm{filter}_C(a)) \supseteq \{ s \in \gamma(a) : C(s) \} for every aa: it may keep extra states, but it must not drop a state that satisfies CC.

At a merge of abstract values a1a_1 and a2a_2, a path-insensitive analysis keeps the join a1⊔a2a_1 \sqcup a_2, the most precise single summary that covers both, and a path-sensitive analysis keeps the set {a1,a2}\{a_1, a_2\}, which stands for γ(a1)∪γ(a2)\gamma(a_1) \cup \gamma(a_2). Since γ(a1)∪γ(a2)⊆γ(a1⊔a2)\gamma(a_1) \cup \gamma(a_2) \subseteq \gamma(a_1 \sqcup a_2), both are sound, and the kept set is at least as precise. A condition q≠0q \ne 0 justifies p≠0p \ne 0 in exactly the states in which p=qp = q holds at the test.

Limits and common mistakes

Path sensitivity costs time and memory that can grow exponentially with the number of branches (the State explosion §3.26 entry). Practical analyzers merge or prune states, and every merge can lose a fact that only some paths had.

Keeping paths apart does not by itself decide which paths are feasible. Whether all the conditions along a path can hold together may need a solver, as in symbolic execution §3.27; an analysis with coarse summaries can keep paths that no run takes and raise false alarms on them.

A check narrows only what was checked. A test on qq says nothing about an unrelated pointer of the same type. A test on a value loaded from shared memory says nothing about that memory if another thread can change it before the use, a time-of-check to time-of-use §9.26 (TOCTOU) error.

Not null does not mean valid. A pointer that passed a null check can still point outside its object, or to an object that has since been freed, which is a use-after-free §4.6. Spatial safety §2.28 and temporal safety §2.29 need their own checks.

Three terms are often confused. A flow-sensitive analysis respects the order of statements but keeps one summary per point. A path-sensitive analysis distinguishes the paths that reach a point. A context-sensitive analysis §3.12 distinguishes the call sites from which a function is entered (the Context sensitivity entry).

Questions answered

Does a null check on one alias refine all equivalent aliases?

A null check on one alias refines another alias only when the analysis knows that both hold the same pointer value at the moment of the check, and only for as long as that stays true. Aliases are two names, such as two variables or registers, that hold the same pointer. If qq is a copy of pp, every possible state has p=qp = q, so on the edge where qq is not null, pp is not null either. An analysis that records the equality can mark every copy non-null after one check. The verifier that Linux runs on eBPF (extended Berkeley Packet Filter) programs does this by giving all copies of one looked-up pointer a shared identifier. An analysis that keeps an independent fact per name cannot, and must still treat pp as possibly null.

The conclusion ends where the equality ends. A new assignment to pp, a reload of pp from memory that something else can change, or a call that may modify pp breaks the link, and the old check no longer covers pp. An unrelated pointer is never refined by the check, however similar its type. Non-null is also only one fact: the pointer may still point outside its object or to a freed object, and those need separate checks.

Related entries: Context sensitivity (§3.12), Alias analysis (§3.18), Temporal memory safety (§2.29), Null pointer (§4.26), State explosion (§3.26). Further reading: [2].