Scalable Demand-Driven Call Graph Generation for Python

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

TL;DR

This paper introduces JARVIS, a novel approach for constructing application-centered call graphs for Python programs. JARVIS significantly improves upon existing methods like PYCG by being both more scalable and precise, achieving substantial speedups and accuracy gains, especially for large, dependency-heavy applications.

What Problem Does This Paper Address?

Static analysis of software, crucial for tasks like security auditing and dependency management, relies heavily on accurate call graphs. These graphs map function calls within a program. For Python, a dynamically typed language, generating precise call graphs is particularly challenging. The current state-of-the-art, PYCG, struggles with large applications that include numerous external libraries. PYCG is flow-insensitive (meaning it doesn't fully consider control flow, leading to over-approximation and false positives) and doesn't fully support all Python features (leading to false negatives). This limits its accuracy and scalability, making it impractical for real-world, complex Python projects.

Methods

JARVIS tackles these limitations with a three-pronged approach:

  1. Application-Centered Call Graph Construction: Instead of analyzing the entire program (application + all libraries), JARVIS focuses on generating call graphs that trace calls originating from the application's functions to invoked library functions. This is more efficient when the primary interest is in the application's behavior and its interaction with dependencies, rather than the internal workings of every library.
  2. Flow-Sensitive Type Inference: JARVIS maintains a "type graph" for each function individually. This allows it to perform flow-sensitive analysis within functions, precisely tracking how data types change based on control flow. It also supports "strong updates," meaning type information is updated precisely when possible, further enhancing accuracy.
  3. Comprehensive Python Feature Support: JARVIS is designed to better handle Python's dynamic features, aiming to reduce false negatives that plague other methods.

The core of JARVIS involves an iterative process of intra-procedural (within a function) and inter-procedural (between functions) analysis. It builds the call graph "on-the-fly" as it analyzes.

Datasets

The authors evaluated JARVIS using two benchmarks:

  • Micro-benchmark: An extended dataset of 135 small Python programs, including newly added categories for features like context managers and control flow to better test flow-sensitivity.
  • Macro-benchmark: A dataset of 6 real-world Python applications, selected for their popularity, quality, and scale (over 100k LOC and 10k functions). This benchmark is noted as being one of the largest of its kind.

Key Insights and Ideas

  • Application-centricity is key for scalability: Analyzing only the application's relevant call paths, rather than the entire program, dramatically reduces the computational burden for large projects with many dependencies.
  • Flow-sensitivity is crucial for precision: By considering control flow within functions, JARVIS can avoid the over-approximation common in flow-insensitive analyses, leading to fewer false positives.
  • On-the-fly generation is efficient: Building the call graph incrementally during analysis, rather than as a post-processing step, contributes to JARVIS's speed.
  • Type inference is a powerful tool for dynamic languages: Precisely tracking types is fundamental to resolving calls in Python, and JARVIS's function-local type graphs enable this effectively.

Key Results

JARVIS demonstrated significant improvements over PYCG:

  • Scalability: JARVIS was substantially faster, achieving a 67% speedup in whole-program analysis (E.W. (1)) and over 157% faster in E.W. (2) compared to PYCG. For application-centered whole-program analysis (A.W.), it averaged only 8.16 seconds. JARVIS was also more memory-efficient.
  • Precision: JARVIS achieved an 84% improvement in precision for application-centered whole-program analysis (A.W.).
  • Recall: JARVIS improved recall by at least 20% in application-centered whole-program analysis (A.W.).
  • Completeness & Soundness: On the micro-benchmark, JARVIS generated complete call graphs for more programs and had fewer unsound cases compared to PYCG.
  • Case Study: JARVIS successfully identified reachable vulnerable dependencies in a case study, demonstrating its utility for security and dependency management tools.

Limitations and Future Work

The authors acknowledge that the construction of ground truth for their macro-benchmark was a challenging and time-consuming process. They also note that JARVIS's imprecision can sometimes stem from unresolved function definitions or issues with handling dynamic linked libraries and certain data structures like lists, tuples, and dicts when they store functions.

Future work includes applying JARVIS to downstream applications in security analysis and debloating Python dependencies, and potentially extending its capabilities to handle more complex dynamic features.

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 Demand-Driven Call Graph Generation for Python}},
  year = {2023},
  eprint = {2305.05949},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.05949},
}
Figure