Definition
Static analysis is the examination of a program’s code, without running it, to conclude how the program can behave when it runs. The code may be source code, bytecode §2.4 (instructions for a virtual machine) or machine code. A static analyzer either reports that a stated property holds, such as “no array §14.5 index is ever out of bounds”, or warns where it could not show the property. “Static” says only that the program is not run; it promises neither that every bug is found nor that every warning is real.
The idea
Testing runs a program on chosen inputs and watches what happens. A function that uses a 32-bit input as an index into a 4-element array has , about 4.3 billion, possible inputs, and a program with several inputs has far more combinations than anyone can run. Static analysis replaces running with reasoning. Instead of trying index values one at a time, the analyzer keeps a summary such as “the index is between 0 and 3 here” and checks the property once for every value the summary covers.
A structural engineer checking a building’s drawings works the same way: one calculation covers every load in the design range, without waiting for a storm. The comparison has a limit. The engineer’s verdict holds only for a building built as drawn, and an analyzer’s verdict holds only for a program that runs as the analyzer’s model of the language, the machine and the surroundings says.
Exact answers are impossible in general. A program that first simulates another computation and then reads outside an array performs the bad read exactly when the simulated computation finishes. A checker that always answered correctly would therefore solve the halting problem §19.2.2, which no algorithm §14.1 can. Rice’s theorem extends this argument to every property of program behavior that some programs have and others lack. A static analyzer that always finishes must err in at least one direction: it misses some real violations, or it warns about some that no execution can produce. Tools built to prove safety choose the second error.
How it works
Fix the property and the model. The property says what must never happen, for example a read outside an array. The model is the analyzer’s semantics: the rules for how each instruction changes the program’s concrete state §3.5 (variable and register values, memory, and the position of the next instruction), plus what is assumed about inputs, called library functions, hardware and other threads.
Build the control-flow graph §3.13: a graph whose nodes are instructions, or straight-line runs of instructions, and whose edges are the possible next steps.
Choose the facts to keep. This choice is the abstract domain §3.6. For array bounds it can be a range of integers for each variable; for null-pointer §4.26 errors, whether each pointer may be null. Everything else is forgotten.
Propagate the facts. Push the facts through each instruction. At a branch, refine them: on the true side of the test , the analyzer knows . Where two paths meet, combine their facts into one fact that covers both. Repeat around loops until no fact changes, which is called a fixed point §3.22.
Check. At each place where the property must hold, test whether the facts prove it. If they do, stay silent. If they do not, issue a warning, or reject the program.
Worked example
An analyzer keeps a range of integers for each variable and checks reads from an array buf of 4 elements, positions 0 to 3. Write for the integers from to inclusive.
A proof. The unsigned input passes the test , so on that branch . Every position the read
buf[i]can use lies in 0 to 3. No warning.A real warning. With the test instead, . The input reads a fifth element, so the warning describes a real execution.
A false warning. One branch sets and ; the other sets and . After the branches meet, the program reads
buf[x - y]. Every real execution reads position or . The analyzer combines the branches into and and forgets that . It computes the smallest possible difference as and the largest as , so . Negative positions are outside the array, so it warns. No execution produces a negative position: the warning is a false positive §1.22.
Two repairs remove this false positive, each at a cost. Path sensitivity §3.11, analyzing each branch separately up to the read, gives on both branches, but the number of paths can double at every branch. A relational abstract domain §15.12, one that records relations such as , keeps the fact at a higher cost per operation.
Stated precisely
Let be the set of all executions of program that the model allows, over every input, environment response and thread schedule the model admits. Let be the property. An analyzer is sound for when, whenever it reports no warning for , no execution in violates . It is complete for when, whenever no execution in violates , it reports no warning. Each warning is either a true positive, a violation that some execution in has, or a false positive, a violation that none has.
A sound analyzer works by over-approximation §19.2.5. It computes a set of represented executions with , and it warns whenever some execution in violates . False positives come from executions in that are not in . In the example, the real pairs are and . The two ranges represent all pairs with and in . The extra pairs include the 6 with , which give the negative positions.
The guarantee assumes that the model is faithful: every behavior the real system can have is in . By the halting-problem argument above, an analyzer that always finishes cannot be both sound and complete for a property that some programs have and others lack, if the language can express every computation.
Limits and common mistakes
A verdict covers only the model. Inline assembly, a library function with no stated contract, interrupts §6.11, another thread, or what the compiler did with undefined behavior §4.12 lie outside unless the model includes them. An analysis of source code says nothing directly about a miscompiled binary.
Many practical analyzers are deliberately unsound for some features, for example by ignoring integer overflow, to cut false positives. Read which assumptions a tool makes before trusting a clean report.
The word “sound” is used in two opposite senses. In verification it means that a clean report is trustworthy, as defined above. Some bug-finding literature uses it to mean that every warning is real. Ask which promise a tool makes.
An analysis that runs out of time or memory reports “unknown”. Unknown is not unsafe. A checker that rejects every unknown case is choosing to refuse safe programs rather than risk accepting an unsafe one.
A proof covers only the property checked. An analyzer that proves every array access in bounds says nothing about whether the program computes the right answer.
False positives have a cost. When most warnings are false, developers stop reading them or suppress them, and a suppressed warning can hide a later real bug.
Questions answered
What classes of behavior are approximated, and with which false positives?
A static analyzer, a tool that reasons about a program’s code without running it, approximates six classes of behavior. An analyzer that never misses a violation within its model replaces each class by a larger set, and each approximation causes its own typical false positive: a warning about a member of the larger set that no real execution produces.
Values of variables and registers, kept as ranges, signs or types. Relations between variables are lost, so can look negative although in every run: false out-of-bounds warnings.
Paths. Facts from different branches are combined where the branches meet, so combinations that no single path produces appear: a warning that a pointer may be null at a use guarded by the same flag that guaranteed the pointer was set.
Loops. To finish, the analyzer may use widening §3.23, which pushes a growing range straight to infinity: an overflow warning for a counter that never exceeds 100.
Memory and pointers. When the analyzer cannot tell which object a pointer refers to, a write through that pointer may seem to overwrite a value already checked; the check is forgotten and the later use is flagged.
Code and surroundings not analyzed, such as library functions, hardware registers and inputs, are assumed to return anything their stated contract permits, often any value at all: warnings about results the real function never returns.
Concurrency §9.1. Every interleaving of threads that the model allows is assumed possible, including interleavings that locks the tool does not understand would prevent: false data race §9.3 warnings.
How coarsely a tool approximates each class is a property of that tool. A pattern checker, which searches for code shapes known to be risky, proves nothing and can miss any bug it has no pattern for. A type checker summarizes each value by its type. A sound analyzer built on abstract interpretation §3.4, a theory of computing with summaries, proves that a stated class of errors never occurs and pays in false positives. Symbolic execution §3.27 and model checking §3.30 keep more detail at a higher cost. The label “static” says only that the program is not run.
Related entries: Dynamic analysis (§3.2), Abstract interpretation (§3.4), Abstract domain (§3.6), Soundness (§3.7), Completeness (§3.8), False positive and false negative (§1.22), Halting problem, Rice’s theorem, and restricted verifiers (§19.2.2). Further reading: [159].