Algorithm Search⌘K

Problemcore

Call graph construction

Also called call graph analysis.

Find a sound superset of the targets of each call site of a program with virtual or indirect calls, from type or pointer facts.

Call graph construction is the analysis that computes, for a program with virtual calls, function pointers or closures, a set of edges from call sites to procedures that contains every call that a run can make. The call graph page defines the graph itself and its structure; this page is about the problem of computing a sound and precise superset of it when the text of a call does not name its callee. The exact answer is undecidable, so every algorithm over-approximates, and the algorithms form a ladder of precision and cost: class hierarchy analysis looks only at declared types, rapid type analysis also looks at which classes the program creates, variable type analysis tracks the types that reach each variable, and analyses built on points-to analysis or control flow analysis (0-CFA, k-CFA) track values. Almost every interprocedural analysis needs a call graph first, and most of them get a better answer from a better graph.

In plain words

In a program written with objects, the line shape.area() does not say which code runs. It depends on the class of the object that shape holds at run time. The same is true for a call through a function pointer or for a call of a function that was passed as an argument. A compiler wants to know, for each such call, the list of functions it can reach. The list may contain extra functions, but it must not miss one that a run calls. A cheap method looks only at the class tree and lists every class that could be below the declared type. Better methods also look at which objects the program creates and at how they move between variables. Shorter lists help the compiler more, and cost more time to find.

Definition

Input. A program with a finite set Π\Pi of procedures (methods or functions), a root main∈Πmain \in \Pi, and a finite set SS of call sites. Each site ss lies in one procedure caller(s)\mathrm{caller}(s). A site is direct if its text names its one callee, and indirect if the callee depends on a value: a virtual call x = y.m(z), a call through a function pointer, or the application of a closure. For an object-oriented program the input also has a finite set of classes with a single-inheritance order ≤\le (where C≤DC \le D means that CC is DD or a subclass of it), a declared type decl(y)\mathrm{decl}(y) for each variable, and a dispatch function lookup(C,m)\mathrm{lookup}(C, m), the method that runs when mm is called on an object of class CC.

Output. A pair (R,E)(R, E) with R⊆ΠR \subseteq \Pi, the reachable procedures, and E⊆S×ΠE \subseteq S \times \Pi, the call edges. The edge (s,Q)(s, Q) says that site ss may call QQ.

Soundness. For each run of the program, every procedure that the run enters is in RR, and every pair (s,Q)(s, Q) such that the run executes site ss and enters QQ there is in EE. The exact call graph is the union of these facts over all runs. A sound output contains the exact graph.

Precision. Order outputs by inclusion of both parts. Of two sound outputs, the smaller one is more precise.

Assumptions. Soundness is with respect to a closed world: the analysis sees the whole program, and calls happen only through the call sites of the program text. Reflection, which looks up a method by a string at run time, native code that calls back, and classes loaded at run time all break this. No practical tool is sound for them, and Livshits and others (2015) name the usual compromise soundiness: a tool is sound except for a listed set of such features. Every soundness claim on the pages of this group holds under these assumptions and no other.

As a fixed point. Most algorithms compute the least (R,E)(R, E) that is closed under two rules: main∈Rmain \in R, and for each site ss with caller(s)∈R\mathrm{caller}(s) \in R and each Q∈T(s)Q \in T(s), (s,Q)∈E(s, Q) \in E and Q∈RQ \in R. Here T(s)T(s) is the algorithm's approximation of the targets of ss, and it may itself depend on RR and EE. The analysis in the terms of a data-flow framework:

item value
direction none: flow-insensitive, one answer for the whole program
may or must may
domain pairs (R,E)(R, E) with R⊆ΠR \subseteq \Pi and E⊆S×ΠE \subseteq S \times \Pi
order inclusion of both parts (bottom is precise)
join union of both parts
bottom and top (∅,∅)(\emptyset, \emptyset) and (Π,S×Π)(\Pi, S \times \Pi)
boundary value main∈Rmain \in R
initial value R={main}R = \{main\}, E=∅E = \emptyset
transfer the two closure rules; monotone when T(s)T(s) grows with (R,E)(R, E)

Variants

  • By the model of the receiver. Name-based resolution keeps every method called mm. Class hierarchy analysis keeps the methods that mm dispatches to on classes below decl(y)\mathrm{decl}(y). Rapid type analysis keeps only classes that a reachable method creates. Variable type analysis keeps the classes that reach yy along assignments. Points-to based construction keeps the classes of the abstract objects in the points-to set of yy, as in Andersen's analysis run together with the call graph.
  • Context-sensitive. The call graph of a context-sensitive analysis has nodes (Q,c)(Q, c) for a procedure and a context cc. Call strings give the k-CFA family; receiver objects give object sensitivity. See context sensitivity.
  • Higher-order functions. In a functional language the callee is a closure. The problem becomes control flow analysis: which lambda terms can reach each application. 0-CFA is the base analysis.
  • Ahead of time or on the fly. An algorithm can build a call graph first and then a points-to analysis over it, or it can build both together. On the fly is more precise, because an edge that the values never support is never added.
  • Whole program or library. For a library the roots are all public methods, and the classes that client code may create are unknown. This changes the answer of every algorithm above.
  • Dynamic call graphs. Recording the calls of test runs gives a subset of the exact graph. It is useful for profiling and unsound for optimization.

Examples

In this Java-like program, Oval inherits grow from Circle, and no code creates a Shape or an Oval:

class Shape          { Shape grow() { return this; } }
class Circle : Shape { Shape grow() { return new Square(); } }
class Square : Shape { Shape grow() { return this; } }
class Oval : Circle  { }

main() {
  Shape a = new Circle();
  Shape b = new Square();
  Shape c = a.grow();      // site s1
  Shape d = b.grow();      // site s2
  Shape e = c.grow();      // site s3
}

Every variable has the declared type Shape. The only run calls Circle.grow at s1 and Square.grow at s2 and s3.

algorithm s1 s2 s3 edges reachable methods
exact Circle Square Square 3 main, Circle.grow, Square.grow
class hierarchy analysis Shape, Circle, Square Shape, Circle, Square Shape, Circle, Square 9 main and all three grow
rapid type analysis Circle, Square Circle, Square Circle, Square 6 main, Circle.grow, Square.grow
variable type analysis on the RTA graph Circle Square Circle, Square 4 main, Circle.grow, Square.grow

The class hierarchy analysis keeps Shape.grow because a Shape could exist. Rapid type analysis drops it because no reachable code creates a Shape. Variable type analysis sees that only a Circle reaches a and only a Square reaches b. It keeps the extra target at s3 because the spurious RTA edge from s1 to Square.grow carries a Circle into that method's this and back out of its return into c.

Known algorithms

algorithm what it tracks worst-case time
class hierarchy analysis declared types and the class tree $O(
rapid type analysis also the set of created classes $O(
variable type analysis the classes that reach each variable and field $O((n +
Andersen's analysis, on the fly abstract objects per variable and field O(n3)O(n^3)
0-CFA the lambda terms that reach each expression O(n3)O(n^3)
k-CFA for k≥1k \ge 1 closures with the last kk call sites exponential in nn; EXPTIME-complete
object sensitivity abstract objects with their allocation context polynomial for a fixed depth

Here nn is the size of the program. The first three are the classic algorithms for object-oriented programs, and the bounds are those proved on their pages.

AlgorithmTimeSpacePropertiesCode
Class hierarchy analysisO(n + S K + K M) worstO(S K + K M) extranot context sensitivedeterministicnot flow sensitiveofflineiterative1 language
k-CFA2^O((k+1) n log n) worst2^O((k+1) n log n) extracontext sensitivedeterministicnot flow sensitiveofflineiterative1 language
Rapid type analysisO(n + S K + K M) worstO(S K + K M) extranot context sensitivedeterministicnot flow sensitiveofflineiterative1 language
Variable type analysisO((n + P) K) worstO((n + P) K) extranot context sensitivedeterministicnot flow sensitiveofflineiterative1 language
0-CFAO(n^3) worstO(n^2) extranot context sensitivedeterministicnot flow sensitiveofflineiterative1 language

Bounds

  • The exact call graph is undecidable. Take any program PP and build main() { P(); t(); } with a direct call to a new procedure t. The edge from main to t is in the exact graph if and only if PP halts, so the halting problem reduces to the question. This holds even with no indirect calls: exact means over all runs, and that is a semantic property (Rice's theorem). So all algorithms are sound over-approximations, and precision is measured, not proved optimal.
  • Linear time for declared types. Class hierarchy analysis needs only the text and the class tree. It is linear in the size of the program times the number of classes, and close to linear in practice.
  • Cubic time for value flow. With closures, the 0-CFA form of the problem takes O(n3)O(n^3) time, and Van Horn and Mairson (2007) proved that it is complete for polynomial time. So it is unlikely to be solvable in polylogarithmic parallel time.
  • Exponential time with call strings. k-CFA for any fixed k≥1k \ge 1 is complete for EXPTIME (the k-CFA EXPTIME-completeness theorem). The object-oriented form of kk-CFA is polynomial, because an object stores its fields in one context.

Reductions

  • Points-to analysis gives a call graph. The targets of y.m() are {lookup(C,m)}\{\mathrm{lookup}(C, m)\} for the classes CC of the abstract objects in the points-to set of yy. The targets of a call through a function pointer are the functions in its points-to set.
  • A call graph is needed for points-to analysis. Parameters receive values only along call edges. So the two analyses depend on each other, and the precise solution computes both as one least fixed point.
  • Graph reachability reduces to it. For a directed graph, make one procedure per vertex, with a direct call from uu to vv for each edge. The reachable procedures are the vertices reachable from the root. So any algorithm must take at least linear time.
  • Control flow analysis is call graph construction for closures. The application (e1 e2)(e_1\ e_2) is a call site, and the lambda terms that reach e1e_1 are its targets.

Applications

References

  1. David Grove and Craig Chambers, A framework for call graph construction algorithms, ACM Transactions on Programming Languages and Systems 23(6), 685 to 746, 2001, the lattice of call graphs, the parameterized family of algorithms (CHA, RTA, 0-CFA, k-CFA and others) and their measured precision and cost
  2. David Grove, Greg DeFouw, Jeffrey Dean and Craig Chambers, Call graph construction in object-oriented languages, Proceedings of OOPSLA '97, SIGPLAN Notices 32(10), 108 to 124, 1997, the first form of the framework and the measurements on Cecil and Java programs
  3. Jeffrey Dean, David Grove and Craig Chambers, Optimization of object-oriented programs using static class hierarchy analysis, ECOOP '95, LNCS 952, 77 to 101, 1995, class hierarchy analysis and its use to bind message sends statically
  4. David F. Bacon and Peter F. Sweeney, Fast static analysis of C++ virtual function calls, Proceedings of OOPSLA '96, 324 to 341, 1996, rapid type analysis and its comparison with class hierarchy analysis on C++ programs
  5. Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vallee-Rai, Patrick Lam, Etienne Gagnon and Charles Godin, Practical virtual method call resolution for Java, Proceedings of OOPSLA 2000, 264 to 280, variable type analysis and declared type analysis, built on a conservative call graph
  6. Benjamin Livshits, Manu Sridharan, Yannis Smaragdakis, Ondrej Lhotak, J. Nelson Amaral, Bor-Yuh Evan Chang, Samuel Z. Guyer, Uday P. Khedker, Anders Moller and Dimitrios Vardoulakis, In defense of soundiness, a manifesto, Communications of the ACM 58(2), 44 to 46, 2015, whole paper (no practical whole-program analysis is sound for reflection, native code and dynamic loading)
  7. Barbara G. Ryder, Constructing the call graph of a program, IEEE Transactions on Software Engineering SE-5(3), 216 to 226, 1979, call graphs of programs with procedure parameters
  8. Olin Shivers, Control-Flow Analysis of Higher-Order Languages, or Taming Lambda, PhD thesis, Carnegie Mellon University, 1991, technical report CMU-CS-91-145, the call graph problem for higher-order languages, and 0CFA and 1CFA as its solutions
  9. David Van Horn and Harry G. Mairson, Relating complexity and precision in control flow analysis, Proceedings of ICFP 2007, 85 to 96, Theorem 2, the PTIME-completeness of 0-CFA

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml