Algorithm Search⌘K

Conceptcore

Interprocedural dataflow analysis

Also called interprocedural analysis, whole-program analysis.

Dataflow analysis across procedure calls, which must match each return with its call to avoid facts from paths that cannot occur.

An interprocedural dataflow analysis follows facts across procedure calls: into a callee with the arguments, and back to the caller with the results and the side effects. An intraprocedural analysis stops at each call and must assume the worst about it. The central difficulty is that a procedure has many callers. A run that enters a procedure from one call site always returns to that call site, and the analysis should consider only paths with this property, the valid paths. If it lets facts enter from one caller and leave to another, it adds facts from paths that no run can follow. The goal of an exact interprocedural analysis is the meet over all valid paths (MVP). Context sensitivity is the name for how well an analysis keeps to that goal.

In plain words

Real programs are made of many functions that call each other. To learn what a variable can hold, an analysis must look inside the functions it calls. A simple way is to join all the flow charts into one big chart, with an arrow from each call into the function and an arrow from the end of the function back to each place that called it. But that big chart has paths that cannot happen: go into a function from one caller and come out at a different caller. A program never does that, because it always returns to where it was called from. If the analysis follows such false paths, it reports facts that are never true. A good interprocedural analysis follows only the paths where each return goes back to its own call.

The idea

Supergraph. Each procedure pp has a control flow graph with an entry point entryp\mathit{entry}_p and an exit point exitp\mathit{exit}_p. Each call site ss in pp is split into a call point csc_s and a return point rsr_s. The supergraph (Reps, Horwitz and Sagiv 1995) is the union of these graphs plus three kinds of edges for each call site ss that calls qq:

edge from to carries
call edge, label (s(_s csc_s entryq\mathit{entry}_q the arguments and global facts into the callee
return edge, label )s)_s exitq\mathit{exit}_q rsr_s the result and global facts back to the caller
call-to-return edge csc_s rsr_s the facts about the caller's locals, which the callee cannot touch

With indirect calls, the call graph itself is the result of an analysis (call graph construction).

Valid paths. Read the labels of the call and return edges along a path as a string of brackets. A path is same-level valid if its brackets are balanced and each )s)_s closes a (s(_s with the same ss. A path from entrymain\mathit{entry}_{main} is valid if it may leave some calls open but never closes a bracket that it did not open:

matched→ϵ∣matched matched∣(s matched )s∣e,valid→matched∣valid (s matched,\mathit{matched} \to \epsilon \mid \mathit{matched}\ \mathit{matched} \mid (_s\ \mathit{matched}\ )_s \mid e, \qquad \mathit{valid} \to \mathit{matched} \mid \mathit{valid}\ (_s\ \mathit{matched},

where ee is any edge that is not a call or return edge. These are the languages of a Dyck language with one bracket pair per call site, so a question about valid paths is a CFL-reachability question (Reps 1998). A run of the program follows a valid path, because its call stack sends each return to its own call site.

Three solutions. Use the meet convention of Kildall for a monotone framework: ⊤\top is the start value, lower is less precise, and fπf_\pi is the composition of the edge functions along a path π\pi. (The join convention of Cousot flips the order.) For a point nn:

  • MVP(n)=⊓{fπ(ι):π a valid path from entrymain to n}\mathrm{MVP}(n) = \mathop{\large\sqcap} \{ f_\pi(\iota) : \pi \text{ a valid path from } \mathit{entry}_{main} \text{ to } n \}, the goal.
  • MOPSG(n)\mathrm{MOP}_{SG}(n), the same meet over all paths of the supergraph.
  • MFPSG\mathrm{MFP}_{SG}, the greatest fixed point of the equations of the supergraph, as for one big procedure.

Proposition. MFPSG(n)⊑MOPSG(n)⊑MVP(n)\mathrm{MFP}_{SG}(n) \sqsubseteq \mathrm{MOP}_{SG}(n) \sqsubseteq \mathrm{MVP}(n) for every point nn, and all three are sound.

Proof. The first step is the coincidence theorem for monotone frameworks, applied to the supergraph as one graph. The second holds because every valid path is a path of the supergraph, and a meet over more values is lower. Every run follows a valid path, so MVP is below the value of every run's path, and the two lower solutions are below MVP. □\square

The first step can be strict for monotone functions that are not distributive, and the second can be strict even for distributive ones (the example below). Removing the second gap is what interprocedural analysis is about. The interprocedural coincidence theorem of Knoop and Steffen (1992) says that the functional approach, which works with procedure summaries, computes exactly MVP when every function is distributive.

Analysis items for the instance used in the example, "which variables may depend on the secret input", a forward may problem in the style of taint analysis:

item value
direction forward
may or must may
domain subsets of DD, the variables of each procedure plus a slot RET for the return value
order X⊑YX \sqsubseteq Y when X⊇YX \supseteq Y (Kildall convention; in the Cousot convention, ⊆\subseteq with ∪\cup as the join)
meet union
top and bottom ⊤=∅\top = \emptyset, ⊥=D\bot = D
boundary at entrymain\mathit{entry}_{main}: the secret parameter
initial value ⊤=∅\top = \emptyset everywhere else
transfer, inside a procedure x = e: xx is in the result if some operand of e is in the input; other variables keep their status; distributive
transfer, call edge the parameter is in the result if its argument is in the input
transfer, return edge the target of the call is in the result if RET is in the input
transfer, call-to-return edge the caller's facts, minus the target of the call
height ∣D∣\lvert D \rvert per point
fixed point the greatest, which is the least set of tainted variables
soundness below the value of every valid path, so below every run
cost MFPSG\mathrm{MFP}_{SG}: O(E∣D∣)O(E \lvert D \rvert) set operations; MVP by IFDS: O(E∣D∣3)O(E \lvert D \rvert^3)

Ways to get close to MVP.

approach idea equals MVP for
context-insensitive MFPSG\mathrm{MFP}_{SG} on the supergraph distributive problems on programs where each procedure has one call site
inlining copy each callee into its caller distributive problems without recursion
call strings tag facts with the last kk call sites (context sensitivity) large enough kk, no recursion, distributive problems
functional approach compute a summary function for each procedure and apply it at each call distributive problems, with recursion
IFDS summaries as edges of a graph of single facts finite distributive subset problems
IDE IFDS with value functions on the edges distributive environment problems
pushdown systems the program as a pushdown system, facts as weights distributive problems in a bounded idempotent semiring (Reps, Schwoon, Jha and Melski 2005)

Whole-program analysis. An interprocedural analysis needs the code of the procedures it enters. A whole-program analysis assumes that it sees all of them, as a linker or a link-time optimizer does. For code it cannot see, such as a library, it needs a summary, written by hand or computed earlier, or it must assume the worst at the call. Separate compilation makes the same trade inside a compiler: interprocedural constant propagation by Callahan, Cooper, Kennedy and Torczon (1986) uses jump functions, which are cheap summaries of how a procedure passes values on to its callees.

Examples

Take the taint instance and the program below. secret is the input whose influence we track.

id(a):         RET = a
main(secret):  s1: x = id(secret)
               s2: y = id(0)
               print(y)
point MFPSG\mathrm{MFP}_{SG} (tainted) MVP (tainted)
entryid\mathit{entry}_{id} {a} {a}: the union of {a} from s1 and {} from s2
exitid\mathit{exit}_{id} {a, RET} {a, RET}
rs1r_{s1} {secret, x} {secret, x}
rs2r_{s2} {secret, x, y} {secret, x}

Both solutions agree inside id, where the facts of the two calls are merged anyway. They differ after the second call. The supergraph has the path entrymain→cs1→(s1entryid→exitid→)s2rs2\mathit{entry}_{main} \to c_{s1} \xrightarrow{(_{s1}} \mathit{entry}_{id} \to \mathit{exit}_{id} \xrightarrow{)_{s2}} r_{s2}. It enters id from s1 and leaves to s2. Along it, RET is tainted, so y gets tainted, and print(y) looks like a leak. The brackets (s1 )s2(_{s1}\ )_{s2} do not match, so the path is not valid. On every valid path to rs2r_{s2}, id was entered from s2 with an untainted argument, so MVP finds that y is clean. The same leak of facts between callers makes context-insensitive constant propagation lose constants; the context sensitivity page shows that case and the limits of call strings.

Locals across a call. In the same program, x is tainted after s1. During the call at s2, x is a local of main, so id cannot change it. The call-to-return edge of s2 carries x around the call, and the call edge does not pass it into id. Without this edge, an analysis would have to send every local of the caller through every callee.

Where it is used

  • Security and bug finding. Taint analysis and many checkers for null pointers and resource leaks run interprocedurally, often with the IFDS framework, since a source and its sink are rarely in the same procedure.
  • Compilers. Interprocedural constant propagation, mod-ref analysis of side effects (mod-ref analysis) and inlining decisions use summaries of procedures. A link-time optimizer runs them on the whole program.
  • Pointer analysis. Points-to sets cross procedures through parameters, returns and the heap. The call graph and the points-to sets often depend on each other and are computed together (call graph construction).
  • Slicing. Interprocedural slicing on the system dependence graph uses the same idea of matching calls and returns, with summary edges for the procedures.
  • Model checking. Programs with recursion are pushdown systems, and interprocedural analyses can be phrased as reachability in them.

References

  1. Micha Sharir and Amir Pnueli, Two approaches to interprocedural data flow analysis, in Steven S. Muchnick and Neil D. Jones (eds.), Program Flow Analysis: Theory and Applications, Prentice-Hall, 1981, the interprocedurally valid paths, the meet over valid paths as the goal, and the call-string and functional approaches
  2. Thomas Reps, Susan Horwitz and Mooly Sagiv, Precise interprocedural dataflow analysis via graph reachability, Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '95), 49 to 61, 1995, the supergraph, realizable paths and the meet over all valid paths
  3. Jens Knoop and Bernhard Steffen, The interprocedural coincidence theorem, Compiler Construction (CC '92), Lecture Notes in Computer Science, 125 to 140, 1992, the equality of the interprocedural fixed point and the meet over valid paths for distributive frameworks
  4. Thomas Reps, Program analysis via graph reachability, Information and Software Technology 40(11-12), 701 to 726, 1998, valid paths as a context-free language and interprocedural analysis as CFL-reachability
  5. Thomas Reps, Stefan Schwoon, Somesh Jha and David Melski, Weighted pushdown systems and their application to interprocedural dataflow analysis, Science of Computer Programming 58(1-2), 206 to 263, 2005, interprocedural dataflow analysis as reachability in a weighted pushdown system
  6. David Callahan, Keith D. Cooper, Ken Kennedy and Linda Torczon, Interprocedural constant propagation, Proceedings of the 1986 SIGPLAN Symposium on Compiler Construction, 152 to 161, 1986, jump functions and the propagation of constants over the call graph

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)
WritingW4 complete
  • a review of the text: no review record reviews/D27P42/text.yaml