Abstract
Static analysis tools are powerful for identifying software vulnerabilities, yet they are often limited by the "specification bottleneck"—the need for manually defined rules for every API and library. This paper presents a probabilistic approach to automate specification inference using factor graphs. By leveraging the assumption that software is mostly correct, the model identifies resource management rules directly from source code. The framework was successfully applied to major projects including the Mac OS X (XNU) kernel, identifying dozens of new bugs with high precision.
Introduction
The effectiveness of static bug-finding tools depends heavily on the quality of their underlying specifications. For instance, to detect a memory leak, a tool must know which functions allocate memory and which are responsible for deallocation. Manually authoring these rules for millions of lines of code is unscalable and error-prone as software evolves. This research addresses the problem by treating specification inference as a machine learning task, asking if the code itself can reveal its own latent rules.
Methodology: Annotation Factor Graphs
The authors introduce Annotation Factor Graphs (AFGs) to represent the unknown properties of functions—such as whether a function returns an owned resource—as random variables.
The "Bugs are Rare" Assumption
The core of the model is the intuition that while programs contain errors, they are predominantly correct. The model seeks a set of specifications that minimizes the number of bugs flagged by a static analysis checker. If a proposed rule makes a common coding pattern look like a bug, that rule is penalized; conversely, if it aligns with standard deallocation patterns, it is rewarded.
Factor Types
Several types of factors guide the inference process:
- Check Factors: These integrate results from a "mini" static analysis (visualized in the state machine above) to evaluate how well a specification fits the observed code paths.
- Prior Beliefs: These encode general programming knowledge, such as the rarity of functions claiming ownership of their arguments.
- Suggestive Naming: Probabilistic hints are derived from function names containing keywords like
alloc,free, ornew.
Inference is performed using Gibbs Sampling to find the most likely specifications, while Gradient Ascent is used to learn model parameters from a small set of labeled examples.
Experimental Evaluation
The model was evaluated on three significant C codebases: SDL (50k LOC), OpenSSH (80k LOC), and the XNU kernel (1.3M LOC).
Key Results
The probabilistic model demonstrated remarkable accuracy in inferring specifications across all datasets. As shown in the performance curves below, the system achieved near-perfect True Positive Rates with very low false-positive rates.
Beyond statistical accuracy, the model led to the discovery of significant real-world bugs:
- 3 bugs in SDL.
- 10 bugs in OpenSSH.
- 4 confirmed bugs in the XNU kernel, including a critical memory leak in the
coredumpfunction capable of crashing the system.
Discussion and Insights
A major finding was the cross-project generality of the model. A factor graph trained on OpenSSH performed effectively when applied to the XNU kernel, suggesting that C programmers share common "latent idioms" regardless of the specific project. Furthermore, the probabilistic nature of the model allowed it to handle noise and imprecision in static analysis far better than traditional heuristic-based approaches.
Conclusion and Future Work
By framing specification inference as a probabilistic graphical model, this work provides a scalable solution to the specification bottleneck. Future research directions include extending this framework to other domains, such as identifying tainted data for security analysis or inferring array bounds to prevent buffer overflows. The success in finding critical bugs in production systems like XNU underscores the practical utility of combining machine learning with static program analysis.
Summary updated