Self-Supervised Bug Detection and Repair

Miltiadis Allamanis, Henry Richard Jackson-Flux, Marc Brockschmidt

TL;DR

BUGLAB is a self-supervised framework that co-trains two models in a min-max game: a "selector" that learns to create challenging synthetic bugs and a "detector" that learns to fix them. By moving beyond simple random bug insertion, BUGLAB improves the detection of real-world errors by up to 30% and successfully identified 19 previously unknown bugs in popular open-source Python packages.

The Data Hunger Problem in Program Analysis

Deep learning has shown great promise in program analysis, but it faces a massive "data hunger" problem. To train a model to find bugs, you need a massive corpus of buggy code paired with its corrected version. Manually annotating such a dataset is prohibitively expensive and slow.

To get around this, researchers often use "synthetic bugs"—taking healthy code and randomly swapping a variable or an operator. However, these random bugs are often "too easy" to catch and don't represent the subtle, semantic mistakes humans actually make. The gap between these simple synthetic benchmarks and messy, real-world code remains a major hurdle for deploying AI-based bug finders.

Methods: The BUGLAB Framework

To bridge this gap, the authors introduce BUGLAB, a self-supervised approach inspired by adversarial training and models like ELECTRA.

BUGLAB Co-training Architecture
  1. The Co-training Game: BUGLAB consists of two "symmetric" models:
  2. The Selector ($S$): Given a clean code snippet, it decides where and how to insert a bug to make it "hard" for the detector.
  3. The Detector ($D$): It examines the code, attempts to locate the bug (if any), and predicts the correct "repair" rewrite.
  4. Min-Max Optimization: The models are trained together. The selector is rewarded when it creates a bug that the detector fails to find. The detector is trained to minimize its error on these increasingly difficult examples.
  5. Code Representation: Code is represented as a heterogeneous graph. Nodes represent entities like tokens and syntax tree elements, while edges represent relationships like data flow, control flow, and syntax nesting. The authors test two architectures to process this: Gated Graph Neural Networks (GNNs) and the GREAT relational transformer.
  6. Rewrite Rules: The system focuses on four classes of common, simple bugs: Variable Misuse, Argument Swapping, Wrong Operator, and Wrong Literal.

Datasets and Evaluation

A major contribution of this paper is the introduction of PYPIBUGS, a manually curated dataset of 2,374 real-life bugs found in open-source history that match the four bug classes BUGLAB targets. The models were trained on 3,400 of the most downloaded Python packages from PyPI and evaluated against both traditional random synthetic bugs (RANDOMBUGS) and the real-world PYPIBUGS dataset.

Key Insights

  • Harder is Better: The core insight is that training on "hard" bugs (selected by a model) generalizes much better to real-world human errors than training on "random" bugs. The selector learns to avoid trivial mistakes and focuses on deeper semantic patterns.
  • Data Augmentation for Code: To help the model ignore irrelevant changes, they use "semantics-preserving" rewrites during training, such as renaming local variables or deleting comments.
  • GNNs vs. Transformers: Interestingly, the authors found that GNNs often outperformed the GREAT transformer. This suggests that explicit structural information in the code graph is more valuable for bug hunting than the implicit relationships learned by standard attention mechanisms.

Key Results

On the real-world PYPIBUGS dataset, BUGLAB outperformed models trained on random bugs by significant margins.

Performance on PYPIBUGS Dataset
  • Performance Boost: BUGLAB achieved up to a 30% improvement in joint localization and repair accuracy compared to the random selector baseline.
  • Real-World Impact: When run on popular open-source projects, the model flagged numerous issues. The authors reported 11 of these to developers on GitHub; 6 were immediately merged, and 5 are pending. In total, they found 19 previously unknown bugs.
  • Localization is the Bottleneck: The results show that once the model finds the location of a bug, it is quite good at fixing it. The hardest part remains "bug localization"—distinguishing a subtle error from "weird but correct" code.

Limitations and Future Work

While promising, the approach has limitations. False Positives remain the biggest hurdle for practical use; in "the wild," the model had a high false positive rate, which could lead to "alert fatigue" for developers. Additionally, BUGLAB currently only handles "single-token" rewrites and cannot yet detect complex logic bugs that span multiple lines. Future work could involve training the selector to better mimic actual human coding mistakes to improve realism alongside difficulty.

Summary updated

BibTeX
@inproceedings{allamanis2021self,
  author = {Miltiadis Allamanis and Henry Richard Jackson-Flux and Marc Brockschmidt},
  title = {{Self-Supervised Bug Detection and Repair}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2021},
  url = {https://openreview.net/forum?id=zOngaSKrElL},
}
Figure