Section 3.1

Static analysis

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 2322^{32}, 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

  1. 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.

  2. 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.

  3. 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.

  4. Propagate the facts. Push the facts through each instruction. At a branch, refine them: on the true side of the test i<4i < 4, the analyzer knows i≤3i \le 3. 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.

  5. 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 [a,b][a, b] for the integers from aa to bb inclusive.

  1. A proof. The unsigned input ii passes the test i<4i < 4, so on that branch i∈[0,3]i \in [0, 3]. Every position the read buf[i] can use lies in 0 to 3. No warning.

  2. A real warning. With the test i≤4i \le 4 instead, i∈[0,4]i \in [0, 4]. The input i=4i = 4 reads a fifth element, so the warning describes a real execution.

  3. A false warning. One branch sets x=0x = 0 and y=0y = 0; the other sets x=3x = 3 and y=3y = 3. After the branches meet, the program reads buf[x - y]. Every real execution reads position 0−0=00 - 0 = 0 or 3−3=03 - 3 = 0. The analyzer combines the branches into x∈[0,3]x \in [0, 3] and y∈[0,3]y \in [0, 3] and forgets that x=yx = y. It computes the smallest possible difference as 0−3=−30 - 3 = -3 and the largest as 3−0=33 - 0 = 3, so x−y∈[−3,3]x - y \in [-3, 3]. 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 x−y∈[0,0]x - y \in [0, 0] 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 x−y=0x - y = 0, keeps the fact at a higher cost per operation.

Stated precisely

Let Exec(p)\mathrm{Exec}(p) be the set of all executions of program pp that the model allows, over every input, environment response and thread schedule the model admits. Let PP be the property. An analyzer is sound for PP when, whenever it reports no warning for pp, no execution in Exec(p)\mathrm{Exec}(p) violates PP. It is complete for PP when, whenever no execution in Exec(p)\mathrm{Exec}(p) violates PP, it reports no warning. Each warning is either a true positive, a violation that some execution in Exec(p)\mathrm{Exec}(p) has, or a false positive, a violation that none has.

A sound analyzer works by over-approximation §19.2.5. It computes a set A(p)A(p) of represented executions with Exec(p)⊆A(p)\mathrm{Exec}(p) \subseteq A(p), and it warns whenever some execution in A(p)A(p) violates PP. False positives come from executions in A(p)A(p) that are not in Exec(p)\mathrm{Exec}(p). In the example, the real pairs (x,y)(x, y) are (0,0)(0, 0) and (3,3)(3, 3). The two ranges represent all 4×4=164 \times 4 = 16 pairs with xx and yy in {0,1,2,3}\{0, 1, 2, 3\}. The 16−2=1416 - 2 = 14 extra pairs include the 6 with x<yx < y, which give the negative positions.

The guarantee assumes that the model is faithful: every behavior the real system can have is in Exec(p)\mathrm{Exec}(p). 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 Exec(p)\mathrm{Exec}(p) 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.

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].