Static Program Analysis
Book: https://users-cs.au.dk/amoeller/spa/
Applications
- Optimization
- Correctness
- Development
The input/behavior of nontrivial programs are undecidable.
The Tiny Imperative Language
- Syntax
- Normalization
- AST
- CFG
Type Analysis
rs
Type -> int
| ↑Type
| (Type, ..., Type) -> Type
// Infinite types (caused by recursion)
| μ TypeVar. Type
| TypeVar
// Record
| { Id: Type, ..., Id: Type }
| ◊ // Absent field
TypeVar -> t | u | ...
Polymorphic + recursive = undecidable type analysis.
Limits:
- flow-sensitive
- polymorphic
- ignores other kinds of runtime errors
Lattice Theory
Partially ordered set
- Reflexive
- Transitive
- Antisymmetric
Lattice
- Least upper bound (join)
- Greatest lower bound (meet)
Complete lattice: every subset has a join and a meet.
- Top
- Bottom
Examples:
- Powerset lattice
- Flat lattice
- Product lattice
- Function lattice
Homomorphism:
- Homomorphism:
such that - Isomorphism = bijective homomorphism
Equation System:
fixed point, least fixed point (LFP)
For monotonic function
Dataflow Analysis with Monotone Frameworks
Sign analysis, Constant propagation analysis:
X=E
:var X
:
Live variable analysis:
X=E
:if (E)
,output E
:var X
:exit
:
Available expressions analysis:
- State = (P({exprs}),
) X=E
:removed all expressions that contain if (E)
,output E
:
Very busy expressions analysis:
X=E
:removed all expressions that contain - Usage: "Code hoisting"
Reaching definitions analysis:
- State = (P({definitions}),
) X=E
:- Usage: "DCE", "Code motion"
Summary
Forward | Backward | |
---|---|---|
May | Reaching definitions | Live variables |
Must | Available expressions | Very busy expressions |
Transfer function:
Widening and Narrowing
- Allowing lattices with infinite height to converge.
- Accelerate finite height lattices.
- Widening:
- Narrowing: Not guaranteed to converge. Heuristics must determine how many iterations to apply.
Widening operator:
- Only apply widening at recursive dataflow constraints.
Path Sensitivity
Assertions:
- Trivial:
- Predicates:
- Sound & Monotone
Relations
flag=0
:assert(flag)
:
Interprocedural Analysis
Context Sensitivity
Using the lattice
: Context-insensitive : Call string approach : Functional approach: full context sensitivity
Distributive Analysis Frameworks
Possibly-Uninitialized Variables Analysis
- Monotone framework: not scalable
- Pre-analysis + Main analysis:
- Pre-analysis:
- Lattice:
X=E
:
- Lattice:
- Main analysis
- Lattice:
- Non-entry node:
- Lattice:
- Pre-analysis:
Compat Representation of Distributive Functions
which is distributive, and is a finite set. - Used in: "Pre-analysis" above, the IFDS framework
- A special case of 2.
txt
· d1 d2 d3 d4
|\ \ |
| \ \ |
| \ \|
· d1 d2 d3 d4
which is distributive and is a finite set and is a complete lattice. - Used in: the IDE framework
Define
The IFDS Framework
Interprocedural, Finite, Distributive, Subset
- Lattice:
of a finite set - Transfer functions:
are distributive
Phase 1
Phase 2
, where is the entry node of the function containing .
The IDE Framework
Interprocedural, Distributive, Environment
- Lattice:
, where is a finite set and is a complete lattice (the "environment") - Transfer functions:
are distributive
Phase 1
Phase 2
Properties of the IFDS and IDE Frameworks
- Produces meet-over-all-paths solutions
- The worst-case time complexity is:
Control Flow Analysis
Closure analysis
E1 E2
:for every
First-class Functions
Pointer Analysis
Allocation-site abstraction
Interprocedural pointer analysis
Null pointer analysis
Flow-sensitive pointer analysis
Escape analysis
Abstract Interpretation
TODO