Problemcore
Points-to analysis
Also called pointer analysis.
For each pointer variable, find a set of abstract memory locations, such as allocation sites, that it may point to.
Points-to analysis computes, for each pointer in a program, a set of memory locations that the pointer may hold the address of. A location is a variable or an abstract heap cell, usually one per allocation site. The answer must be sound: every address that a pointer holds in some run must be in its set. Smaller sets are more precise. The exact answer is undecidable in general, so every algorithm computes an over-approximation, and the design space is the trade between precision and cost: Andersen's analysis in cubic time, Steensgaard's analysis in almost linear time, and flow-sensitive and context-sensitive analyses that cost more. The result answers alias questions, builds call graphs through function pointers and virtual calls, and feeds almost every optimization that moves or removes loads and stores.
In plain words
Many program variables hold addresses. A compiler that wants to move a load past a store must know whether the two can touch the same memory. Points-to analysis gives, for each pointer, a list of the places it might point to. The list may contain places the pointer never reaches, but it must never miss one that it does reach. A short list helps the compiler more. Some analyses keep a separate list at each line of the program. Others keep one list per pointer for the whole program, which is cheaper but coarser. Memory made at run time is named by the line that makes it, so all objects made by one line share one name.
Definition
Input. A program in the toy IR with the pointer instructions p = &x, x = *p, *p = a and p = alloc, together with copies x = y and the other instructions. The abstract locations are the variables and one heap location for each alloc site ; write for their set and . In a run, the -th site can make many cells, and each of them maps to . A variable maps to itself.
Output. A points-to graph , where means that may hold the address of a location that maps to . Write . A flow-sensitive analysis gives one graph at each program point. A flow-insensitive analysis gives one graph for the whole program.
The lattice. The domain at a point is the powerset of , ordered by , with union as the join. This is the order of abstract interpretation in the direction of Cousot: bottom is , the most precise value (no address is held, or the point is not reached), and top is . The height is . Turning the order upside down maps this onto the meet convention of Kildall, where union becomes the meet and becomes ; this page uses only the join form.
Transfer functions. Each instruction maps a graph to a new graph. "Kill " removes all pairs .
| instruction | output |
|---|---|
p = &a |
kill , add |
p = alloc at site |
kill , add |
p = q |
kill , add for each |
p = *q |
kill , add for each and |
*p = q with , |
kill , add for each (a strong update) |
*p = q otherwise, |
add for each and (a weak update) |
*p = q with |
: no run gets past a store through a pointer with no target |
| any other instruction that defines | kill |
A strong update is allowed only when the target is one concrete location. A heap location stands for many cells, and a store changes only one of them, so a store into it is always weak. Each function is monotone. They are not distributive, because the load and the store combine two facts of .
Equations. For each block with the transfer function (the composition of its instructions):
| item | flow-sensitive | flow-insensitive |
|---|---|---|
| direction | forward | none: one answer for the whole program |
| may or must | may | may |
| domain | subsets of at each point | subsets of |
| order | (bottom is precise) | |
| join | union | union |
| bottom and top | and | and |
| boundary value | none | |
| initial value | everywhere | |
| transfer | the table above; monotone, not distributive | the inclusion constraints of Andersen's analysis; monotone, not distributive |
The ideal answer at a point is the join over all paths, , where ranges over the paths from the entry to , and composes the transfer functions along .
The computed answer is the least fixed point of the equations, found by a worklist that starts from everywhere and applies the transfer functions until nothing changes (worklist algorithm). The functions are monotone, so the least fixed point exists by the Knaster-Tarski theorem, and at every point.
Soundness. The concrete semantics is the set of states that reach each point along the executable paths. Map a state to the graph of pairs such that the variable or cell holds the address of a location that maps to . For every instruction, of the state after it is in the transfer function applied to of the state before it. A strong update is safe because it replaces the only location that the store can write. By induction along a path, for every state that reaches along . Every real path is a path of the graph, so the answer is sound.
Flow-insensitive soundness. The flow-insensitive answer is the least graph that is closed under the inclusion constraints of all instructions: for p = &a, for p = q, and so on. For every instruction, the transfer function maps any into , since it only adds pairs that the constraints already require. So and stay inside for every block, and covers every point. It ignores the order of the statements, not the program: it covers every run that executes the statements in any order, any number of times.
Precision. The join loses information when two paths each carry half of a fact. In this program, p gets z on every path, but the computed answer adds x and y:
entry:
a = &z
b = &z
br c left right
left:
q = &a
b = &y
jump join
right:
q = &b
a = &x
jump join
join:
p = *q
ret 0
On the path through left, q points to a and a to z; on the path through right, q points to b and b to z. So gives at ret. The fixed point joins first: at join, , and , so .
Cost. Let be the number of instructions and the number of edges of the control flow graph. Each only grows, at most times, so the worklist applies transfer functions and joins in the worst case. Each application handles sets of pairs, which a bit-vector does in word operations. The flow-insensitive answer takes time by Andersen's analysis.
Variants
- Flow sensitivity. One graph per point, as above, or one per program (flow sensitivity). Flow-insensitive analysis cannot kill facts, so it cannot do strong updates. Sparse flow-sensitive analyses (sparse flow-sensitive pointer analysis) propagate facts along def-use chains instead of the control flow graph.
- Context sensitivity. A procedure analyzed once for all its callers merges their facts. Context sensitivity keeps them apart, by call strings, by the receiver objects (object sensitivity), or by summaries. For higher-order programs the same question is control flow analysis (0-CFA, k-CFA).
- Heap abstraction. One location per allocation site is the common choice. Adding the calling context to the site (heap cloning) separates objects made by one factory method for different callers.
- Aggregates. A structure can be one location or one location per field (field sensitivity). Arrays are usually one location for all elements.
- Subset or unification. Inclusion constraints, as in Andersen's analysis, or equality constraints, as in Steensgaard's. Das's one-level flow uses inclusion at the top level and equality below it.
- May or must. A must points-to fact says that points to one concrete location on every path. Must facts are what allow strong updates.
- Demand-driven. Answer the set of one pointer only, by a backward search (demand-driven points-to analysis).
Examples
This program stores one heap pointer over another through r:
entry:
p = alloc
q = alloc
r = &p
*r = q
s = p
ret 0
Call the two sites h1 and h2. In every run, s ends up holding the cell from h2. Three analyses give these sets at ret:
| pointer | flow-sensitive | Andersen | Steensgaard |
|---|---|---|---|
p |
{h2} | {h1, h2} | {h1, h2} |
q |
{h2} | {h2} | {h1, h2} |
r |
{p} | {p} | {p} |
s |
{h2} | {h1, h2} | {h1, h2} |
The flow-sensitive analysis sees that r points only to the variable p when the store runs, so it replaces the set of p (a strong update). Andersen's analysis has one set per variable for the whole program, so p keeps h1 from its first statement. Steensgaard's analysis makes the targets of p and q one class at the store *r = q, so q also gets h1, which Andersen's analysis avoids.
Known algorithms
| algorithm | kind | worst-case time |
|---|---|---|
| Andersen's analysis | flow-insensitive, inclusion | |
| Steensgaard's analysis | flow-insensitive, unification | |
| Das's one-level flow | inclusion at the top level, unification below | |
| online cycle elimination, offline variable substitution, hybrid cycle detection | faster solvers for Andersen's constraints, same answer | cubic or worse, much faster in practice |
| BDD-based analysis | Andersen-style sets stored as binary decision diagrams | depends on the size of the diagrams |
| demand-driven points-to analysis | one query at a time, often context-sensitive | depends on the query budget |
| sparse flow-sensitive analysis | flow-sensitive, along def-use chains | polynomial |
| object sensitivity | context-sensitive for object-oriented programs | polynomial for a fixed depth , growing exponentially with |
Here is the size of the program. Type-based alias analysis answers alias questions from types alone, without points-to sets.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Andersen's analysis | O(n^3 + m) worst | O(n^2 + m) extra | not context sensitivedeterministicnot flow sensitiveofflineiterative | 1 language |
| One-level flow analysis | O(n (n + m)) worst | O(n^2 + m) extra | not context sensitivedeterministicnot flow sensitiveofflineiterative | 1 language |
| Demand-driven points-to analysis | O(m^3 + m s) worst | O(m^2 + s) extra | not context sensitivedeterministicnot flow sensitiveofflineiterative | 1 language |
| Lazy and hybrid cycle detection | O(n^4 + n^2 m) worst | O(n^2 + m) extra | not context sensitivedeterministicnot flow sensitiveonlineiterative | 1 language |
| Offline variable substitution | O(n^3 + m) worst | O(n^2 + m) extra | not context sensitivedeterministicnot flow sensitiveofflineiterative | 1 language |
| Online cycle elimination | O(n^4 + n^2 m) worst | O(n^2 + m) extra | not context sensitivedeterministicnot flow sensitiveonlineiterative | 1 language |
| Sparse flow-sensitive pointer analysis | O(n^3 m^4) worst | O(n^2 m + n m^2) extra | not context sensitivedeterministicflow sensitiveofflineiterative | 1 language |
| Steensgaard's analysis | O((n + m) α(n + m)) worst | O(n + m) extra | not context sensitivedeterministicnot flow sensitiveonlineiterative | 1 language |
Bounds
- Undecidable in general. With dynamic allocation, the precise flow-sensitive question is undecidable, even when every path is executable (undecidability of may-alias analysis). Chakaravarthy (2003) showed that this holds with scalar variables only.
- NP-hard without the order. Without dynamic memory, the precise flow-insensitive question is NP-hard when statements may dereference many times (NP-hardness of precise flow-insensitive alias analysis).
- Cubic for inclusion. Andersen's analysis takes time. Heintze and McAllester (1997) relate this cubic bound to the reachability problem of two-way nondeterministic pushdown automata, for which no subcubic algorithm is known. Sridharan and Fink (2009) show a quadratic bound for k-sparse programs, where, roughly, each variable and each location gets its values from at most a constant number of statements.
- Output size. The answer of Andersen's analysis can have pairs, so no algorithm that lists all sets is faster than quadratic in the worst case.
Reductions
Points-to gives alias, and alias gives points-to. *p and *q may alias when , and may point to when *p and a may alias. So the two questions have the same difficulty, and an alias analysis is usually built on a points-to analysis.
Transitive closure reduces to Andersen's analysis. Take a directed graph, a variable for each node, the statement u = &a_u with a new location for each node , and the copy v = u for each edge . Then exactly when is reachable from . So all points-to sets give the transitive closure of the graph.
Andersen's analysis is a Datalog program. With the input relations addr, copy, load and store, the four rules
pts(p, a) :- addr(p, a).
pts(p, a) :- copy(p, q), pts(q, a).
pts(p, a) :- load(p, q), pts(q, b), pts(b, a).
pts(b, a) :- store(p, q), pts(p, b), pts(q, a).
compute the least solution. Tools such as Doop write context-sensitive analyses for Java this way and evaluate them by semi-naive evaluation (Datalog).
CFL-reachability. Field-sensitive and context-sensitive points-to questions can be phrased as CFL-reachability on a graph with an edge for each statement, where a store and a load of the same field, or a call and its return, act as matched parentheses.
Applications
- Alias analysis for optimization. Loads and stores that cannot alias may be reordered by instruction scheduling, moved by loop-invariant code motion, or removed by common subexpression elimination and partial redundancy elimination. Fields whose address never escapes can be kept in registers (scalar replacement of aggregates).
- Call graphs. The targets of a call through a function pointer or a virtual method are the points-to set of the pointer or the receiver (call graph construction).
- Side effects. Mod-ref analysis finds the locations that a call may read or write, and escape analysis finds objects that do not outlive their method or thread.
- Bug finding and security. Null dereferences, use after free, and the flow of tainted data through the heap all need points-to sets.
References
- Lars Ole Andersen, Program Analysis and Specialization for the C Programming Language, PhD thesis, DIKU, University of Copenhagen, 1994 (DIKU report 94/19), the chapter on pointer analysis (inclusion constraints for the assignments of C)
- Bjarne Steensgaard, Points-to analysis in almost linear time, Proceedings of the 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '96), 32 to 41, 1996, the unification-based analysis and its almost linear bound
- Michael Hind, Pointer analysis, haven't we solved this problem yet?, Proceedings of the ACM SIGPLAN-SIGSOFT Workshop on Program Analysis for Software Tools and Engineering (PASTE '01), 54 to 61, 2001, the dimensions of precision (flow, context, heap model, aggregates) and the open problems
- Yannis Smaragdakis and George Balatsouras, Pointer Analysis, Foundations and Trends in Programming Languages 2(1), 1 to 69, 2015, the Datalog formulation of the analysis and the survey of context and heap abstractions
- Nevin Heintze and David McAllester, On the cubic bottleneck in subtyping and flow analysis, Proceedings of the 12th IEEE Symposium on Logic in Computer Science (LICS), 342 to 351, 1997, whole paper (the cubic bound of inclusion-based flow analysis and 2NPDA reachability)
- Manu Sridharan and Stephen J. Fink, The complexity of Andersen's analysis in practice, Static Analysis Symposium (SAS 2009), LNCS 5673, 205 to 221, the quadratic bound for k-sparse programs
- Venkatesan T. Chakaravarthy, New results on the computability and complexity of points-to analysis, Proceedings of the 30th ACM Symposium on Principles of Programming Languages (POPL 2003), 115 to 125, the undecidability with dynamic memory and the complexity of the flow-insensitive problem
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag BT2X7T Edit this page History Report an error Markdown