Chapter 3

Program analysis, verification, and compiler foundations

This chapter explains how to reason about every possible run of a program without executing them all, and what such reasoning can and cannot prove. It first separates static analysis §3.1, which examines the code, from dynamic analysis §3.2, which observes runs, and uses as a working example the eBPF verifier §3.3, which checks a small program written for eBPF §2.1, an instruction set for programs that a kernel loads while it is running, before the program is accepted. The central idea is abstract interpretation §3.4: the analyzer replaces the exact set of states that a program can reach by a simpler description drawn from an abstract domain §3.6, such as a range of numbers or a word whose bits are only partly known, and computes with those descriptions. Soundness §3.7 and completeness name the two ways in which such an approximation can be right or wrong, and path and context sensitivity §3.12 describe how much detail it keeps.

The middle of the chapter covers the standard machinery: control-flow graphs §3.13 and basic blocks §3.14; data-flow analyses such as reaching definitions §3.16 and def-use chains §3.17; alias, taint, reachability §3.21 and slicing analyses; and the fixed points §3.22, widening and narrowing §3.23, and loop invariants §3.24 that let an analysis finish on programs with loops. The last part turns to stronger methods and their limits: ranking functions §3.25 for proving termination, symbolic execution §3.27, SMT (satisfiability modulo theories) solvers, bounded and unbounded model checking §3.30 with their state explosion §3.26, Hoare triples §3.31 and refinement, and the comparison of two implementations of one semantics. It also explains what a verifier must do with behavior it cannot model, keeping undefined behavior §4.12 apart from unsupported behavior and refusing when in doubt. It ends with compilers: intermediate representations §3.37, static single assignment §3.38 form, optimization, and what it means for a compiler, or for one run of it, to be correct.

In this chapter

  1. 3.1 Static analysis
  2. 3.2 Dynamic analysis
  3. 3.3 eBPF verifier
  4. 3.4 Abstract interpretation
  5. 3.5 Concrete state
  6. 3.6 Abstract domain
  7. 3.7 Soundness
  8. 3.8 Completeness
  9. 3.9 Interval analysis
  10. 3.10 Bitmask abstract value (tnum)
  11. 3.11 Path sensitivity
  12. 3.12 Context sensitivity
  13. 3.13 Control-flow graph (CFG)
  14. 3.14 Basic block
  15. 3.15 Data-flow analysis
  16. 3.16 Reaching definitions
  17. 3.17 Def-use chain
  18. 3.18 Alias analysis
  19. 3.19 Taint analysis
  20. 3.20 Program slicing
  21. 3.21 Reachability
  22. 3.22 Fixed point
  23. 3.23 Widening and narrowing
  24. 3.24 Loop invariant
  25. 3.25 Ranking function
  26. 3.26 State explosion
  27. 3.27 Symbolic execution
  28. 3.28 SMT solver
  29. 3.29 Bounded model checking
  30. 3.30 Model checking
  31. 3.31 Hoare triple
  32. 3.32 Refinement
  33. 3.33 Translation validation
  34. 3.34 Differential semantics
  35. 3.35 Undefined versus unsupported semantics
  36. 3.36 Conservative refusal
  37. 3.37 Intermediate representation (IR)
  38. 3.38 Static single assignment (SSA)
  39. 3.39 Compiler optimization
  40. 3.40 Compiler correctness