Scalable and Precise Application-Centered Call Graph Construction for Python

Yixuan Yan, Kaifeng Huang, Bihuan Chen, Zixin Tao, Xin Peng · DOI

TL;DR

JARVIS is a new static analysis tool that constructs highly accurate call graphs for Python programs by focusing only on the code that actually matters to the application. By combining "application-centered" analysis with flow-sensitive type inference, it outperforms the current state-of-the-art (PyCG) with 67% faster execution, 84% higher precision, and 20% better recall.

What Problem Does This Paper Address?

Call Graph Construction (CGC) is the "map-making" phase of software analysis—it identifies which functions call which other functions. This is essential for security auditing, finding dead code, and managing dependencies. However, Python’s dynamic nature makes this incredibly difficult. Variables don't have fixed types, and a single line of code might call different functions depending on the execution path.

Existing tools like PyCG attempt "whole-program" analysis, trying to map every possible connection across the application and all its libraries. This leads to two major failures:

  1. Scalability: Modern Python apps often pull in hundreds of libraries. Analyzing everything causes tools to crash (Out of Memory) or time out.
  2. Precision: Most tools are "flow-insensitive," meaning they ignore the order of operations. If a variable is an integer on line 5 and a string on line 10, a flow-insensitive tool might mistakenly think it could be either type at any time, creating "spurious" (fake) edges in the call graph.

Methods

The authors introduce JARVIS, which shifts the strategy in three key ways:

  • Application-Centered Analysis: Instead of mapping the entire universe of installed libraries, JARVIS starts at the application's entry points and only explores library functions that are actually reachable. This effectively ignores "library bloat."
  • Flow-Sensitive Type Inference: JARVIS tracks the "state" of variables as it moves through a function. It uses Function Type Graphs (FTGs) to maintain local type relations. Crucially, it performs "strong updates"—if a variable is reassigned, JARVIS updates its type rather than just adding to a list of possibilities.
  • On-the-Fly Construction: JARVIS alternates between intra-procedural (inside a function) and inter-procedural (between functions) analysis. As it discovers a new call, it resolves the target and immediately analyzes that target to refine the graph.
  • Enhanced Feature Support: JARVIS adds specific logic to handle Python-specific features that previous tools ignored, such as context managers (with statements) and built-in types.

Datasets

The researchers evaluated JARVIS using two primary benchmarks:

  1. Micro-benchmark: A set of 135 small Python programs. This includes 112 programs from the original PyCG paper plus 23 new ones designed to test edge cases like context managers and complex control flows.
  2. Macro-benchmark: 6 popular, real-world GitHub projects (including bpytop, sqlparse, and rich-cli). These projects were chosen for their popularity (>1000 stars) and complexity, involving over 100k lines of code and nearly 200 dependent libraries each.

Key Insights and Ideas

  • The "Outlier" Problem: The authors found that PyCG spends massive amounts of time analyzing "outliers"—library functions that are never actually called by the application. By being application-centered, JARVIS avoids this wasted effort.
  • Precision Magnification: In a call graph, one wrong guess (a false positive) early in a chain creates a "butterfly effect," leading to a cascade of incorrect edges. JARVIS’s flow-sensitivity stops these errors at the source.
  • Summarization for Speed: JARVIS caches function summaries. If a function is called multiple times with the same input types, JARVIS reuses the previous result instead of re-analyzing the code, which is vital for scaling to large apps.

Key Results

JARVIS demonstrated significant improvements over PyCG across the board:

  • Speed: In whole-program scenarios, JARVIS was 67% faster than PyCG. On average, it generated application-centered graphs for large real-world projects in just 8.16 seconds.
  • Scalability: While PyCG hit "Out of Memory" or "Recursion Error" on several large projects (P3 and P5), JARVIS handled them comfortably within an 8GB memory limit.
  • Accuracy: In macro-benchmarks, JARVIS achieved 84% higher precision and 20% higher recall.
  • Real-world Utility: In a case study on vulnerability tracking, JARVIS successfully identified 6 reachable vulnerabilities across 9 projects, whereas PyCG crashed on 3 of those projects and missed several vulnerabilities in the others.

Limitations and Future Work

Despite its strengths, JARVIS has a few remaining hurdles:

  • Complex Data Structures: It currently struggles to track types when functions are stored inside nested lists or dictionaries, as its current Type Graph doesn't maintain those specific points-to relations.
  • Dynamic/Reflective Code: Like most static analysis tools, JARVIS has difficulty with highly dynamic Python features like getattr() or functions invoked via string names.
  • C-Extensions: JARVIS cannot analyze compiled C-extensions (common in libraries like NumPy), which remains a "black box" for the tool. The authors suggest future work could involve better modeling of these external interfaces.

Summary updated

Abstract

Call graph construction is the foundation of inter-procedural static analysis. PYCG is the state-of-the-art approach for constructing call graphs for Python programs. Unfortunately, PyCG does not scale to large programs when adapted to whole-program analysis where application and dependent libraries are both analyzed. Moreover, PyCG is flow-insensitive and does not fully support Python's features, hindering its accuracy. To overcome these drawbacks, we propose a scalable and precise approach for constructing application-centered call graphs for Python programs, and implement it as a prototype tool JARVIS. JARVIS maintains a type graph (i.e., type relations of program identifiers) for each function in a program to allow type inference. Taking one function as an input, JARVIS generates the call graph on-the-fly, where flow-sensitive intra-procedural analysis and inter-procedural analysis are conducted in turn and strong updates are conducted. Our evaluation on a micro-benchmark of 135 small Python programs and a macro-benchmark of 6 real-world Python applications has demonstrated that JARVIS can significantly improve PYCG by at least 67% faster in time, 84% higher in precision, and at least 20% higher in recall.
BibTeX
@misc{yan2023scalable,
  author = {Yixuan Yan and Kaifeng Huang and Bihuan Chen and Zixin Tao and Xin Peng},
  title = {{Scalable and Precise Application-Centered Call Graph Construction for Python}},
  year = {2023},
  eprint = {2305.05949},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.05949},
}

Loading related content...

Figure