Example-based Synthesis of Static Analysis Rules

Pranav Garg, Srinivasan SengameduarXiv 2022 · DOI

Introduction

Static analysis tools like SonarQube and Semgrep are vital for modern software development, yet their effectiveness is bottlenecked by the difficulty of rule creation. Writing these rules is currently a "black art" that requires deep expertise in both programming language semantics and specialized domain-specific languages (DSLs). This manual process is slow and fails to scale with the rapid release of new libraries and APIs.

RhoSynth addresses this challenge by introducing an example-based synthesis approach. Instead of requiring experts to manually encode logic, RhoSynth allows developers to provide examples of "buggy" and "fixed" code, from which it automatically derives robust static analysis rules.

Examples of buggy (red) and fixed (green) code used as input for rule synthesis.

Methodology: From Graphs to Logic

RhoSynth moves beyond simple text or Abstract Syntax Tree (AST) matching by utilizing Program Dependence Graphs (PDGs). PDGs capture the underlying data and control flow of a program, making synthesized rules resilient to superficial changes like variable renaming or statement reordering.

The synthesis process involves several key technical innovations:

  1. ILP-based Graph Alignment: Identifying common patterns across multiple code examples is computationally expensive (NP-hard). RhoSynth uses Integer Linear Programming (ILP) to find the maximal alignment between graphs, isolating the core semantic elements relevant to a bug.
  2. Logic Formulation: Rules are synthesized as First-Order Logic (FOL) formulas. The system identifies a precondition (the buggy pattern) and a set of postconditions (the necessary safety checks).
  3. Iterative Refinement: To reduce false positives, RhoSynth can learn from "unpaired" examples of correct code. Using an entropy-based partitioning algorithm, it "tightens" the rule by learning multiple valid ways a developer might handle a specific operation (disjunctive postconditions).
Graph-based representations of code patterns and the resulting synthesized First-Order Logic (FOL) rule showing preconditions and negated postconditions.

Key Insights

  • The Power of Negation: A fundamental insight of RhoSynth is the rule structure: $\text{Precondition} \land \neg \text{Postcondition}$. This mirrors developer intuition: "If I am using this API (precondition) but I have not performed this check (negated postcondition), then flag a bug."
  • Semantic Intent over Syntax: By focusing on PDGs rather than ASTs, RhoSynth captures the intent of a fix. This allows it to synthesize rules that state-of-the-art baselines (like ProSynth) often miss due to their reliance on rigid code context.
  • Learning from Diversity: Developers often have multiple ways to write "safe" code. RhoSynth’s ability to learn disjunctive postconditions allows it to support diverse coding styles without triggering unnecessary warnings.

Industrial Validation and Results

RhoSynth was evaluated using a massive GitHub corpus of 1.84 million code changes and validated through a live industrial deployment at AWS.

  • High Precision: In real-world production environments, the tool achieved over 75% precision based on direct developer feedback. In controlled expert evaluations, this precision reached 91%.
  • Refinement Efficacy: The iterative refinement process proved critical, boosting average rule precision from 58% to 97%.
  • Broad Coverage: The researchers successfully synthesized rules for 31 distinct categories, including complex API misuse, concurrency issues, and performance bottlenecks.

Conclusion and Future Work

RhoSynth demonstrates that high-quality static analysis rules can be synthesized automatically from developer commits. While currently optimized for Java, the underlying framework of graph-based ILP alignment and FOL synthesis provides a scalable path forward for automated bug detection. Future work aims to automate the discovery of rule clusters from raw commit data and expand the system's capability to handle more complex architectural predicates.

Summary updated

BibTeX
@misc{garg2022example,
  author = {Pranav Garg and Srinivasan Sengamedu},
  title = {{Example-based Synthesis of Static Analysis Rules}},
  year = {2022},
  eprint = {2204.08643},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2204.08643},
}
Figure