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 to null, the special value that refers to no object, and points at a buffer only when a count is positive. Later it writes through , again only when . Every run is safe: the write happens in exactly the runs where was set. A path-insensitive analysis merges the two outcomes of the first test, so after it 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 and points to the buffer, and one where and is null. At the second test the second state contradicts , 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 is a copy of , the test holds on exactly the paths where holds, but only an analysis that has recorded can use that.
How it works
A path-sensitive analysis works in five steps.
Split at a branch. For a branch on a condition , the analysis sends to the true edge only the states in which can hold, narrowed by , and sends to the false edge the states narrowed by the opposite of . A state that contradicts the condition does not follow that edge.
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.
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.
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.
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 to 0 or 1000, and the program copies it: . The possible states are and . The test keeps only the states in which it holds, so the true edge receives alone. On that edge , so a read through 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 may be null and that may be null. The test narrows to not null and leaves as possibly null, so the analysis must reject the read through , although the read is safe. Here the recorded equality, not the branch alone, carries the needed fact.
If and come from two independent lookups, with equal to 0 or 1000 and equal to 0 or 2000, there are states: , , and . The test leaves and , and in the first of these . A check on does not justify a read through , and an analysis that narrowed here would be unsound. Likewise, if the program assigns a new lookup result to after the test, the old test says nothing about the new .
The cost shows in the count of paths. A function with 10 independent tests in sequence has paths, and one with 30 such tests has , more than a billion.
Stated precisely
Let be the set of concrete states §3.5 that reach a branch on a condition . The true edge receives and the false edge receives . Let be the set of concrete states that an abstract value stands for. An abstract filter is sound when for every : it may keep extra states, but it must not drop a state that satisfies .
At a merge of abstract values and , a path-insensitive analysis keeps the join , the most precise single summary that covers both, and a path-sensitive analysis keeps the set , which stands for . Since , both are sound, and the kept set is at least as precise. A condition justifies in exactly the states in which 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 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 is a copy of , every possible state has , so on the edge where is not null, 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 as possibly null.
The conclusion ends where the equality ends. A new assignment to , a reload of from memory that something else can change, or a call that may modify breaks the link, and the old check no longer covers . 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].