Self-Supervised Bug Detection and Repair

Miltiadis Allamanis, Henry Jackson-Flux, Marc Brockschmidt

TL;DR

BugLab is a self-supervised framework that co-trains two models: a "selector" that learns to hide tricky bugs in code and a "detector" that learns to find and fix them. By moving beyond random noise to this adversarial-style bug generation, the system significantly improves performance on real-world Python bugs and even discovered 19 previously unknown issues in popular open-source packages.

What Problem Does This Paper Address?

Deep learning has shown great promise in program analysis, but it faces a massive data bottleneck. Detecting and repairing bugs requires reasoning over both formal structures (like data flow) and ambiguous information (like variable names). While experts can write manual rules for critical bugs, this is slow and expensive.

To train neural models, researchers usually rely on synthetic data—often created by randomly inserting errors into code. However, random noise is a poor proxy for real-world bugs, which are often subtle and context-dependent. The authors aim to bridge this gap by creating a self-supervised method that generates "hard" training examples without requiring a massive, manually annotated corpus of real-world errors.

Methods

The core of the paper is BugLab, a co-training framework inspired by the "hide and seek" dynamics of GANs or ELECTRA.

Figure 1: BUGLAB overview: a se- lector model Sφ decides which (bug- introducing) rewrite to apply to an input code snippet. Then a bug detector Dθ tries to locate and repair the inserted bug (if one was inserted).
  1. The Bug Selector ($S_\phi$): This model takes a clean code snippet and decides where and how to introduce a bug. It uses a set of "rewrite rules" to alter the code. Its goal is to pick the "hardest" possible bug that will trick the detector.
  2. The Bug Detector ($D_\theta$): This model attempts to locate the bug and predict the inverse rewrite to fix it.
  3. Co-training: The two models are trained in a min-max game. As the detector gets better at finding simple bugs, the selector is forced to find more sophisticated ways to break the code, which in turn pushes the detector to learn deeper semantic patterns.

The authors represent code as a heterogeneous graph, capturing syntax (AST nodes), tokens, and symbols (variable/function references). They evaluate two neural architectures: standard Graph Neural Networks (GNNs) and the GREAT relational transformer.

Datasets

  • PyPIBugs (New): The authors introduced a manually curated dataset of 2,374 real-life "stupid simple bugs" (SSBs) found in the Python Package Index. These are single-statement errors that are easy to fix once found but hard to spot.
  • RandomBugs: A large synthetic dataset (761,445 snippets) where bugs were inserted uniformly at random, used as a baseline.
  • Training Corpus: 3,400 of the most downloaded PyPI packages (unlabeled) were used to provide the "clean" code for the self-supervised training process.

Key Insights and Ideas

  • Adversarial Selection is Key: Training against a "smart" selector is significantly more effective than training against random noise. The selector learns to avoid "easy" bugs (like swapping two obviously different variables) and focuses on subtle errors that require semantic understanding.
  • Localization is Harder than Repair: The models are surprisingly good at fixing a bug once the location is known, but finding the bug in the first place remains the primary challenge.
  • GNNs vs. Transformers: In a surprising twist, GNNs generally outperformed the GREAT transformer in these experiments. The authors suggest this is because GNNs have better access to fine-grained structural information (like syntax and symbol nodes) that might be lost in the token-projected sequences used by transformers.
  • Data Augmentation: Using semantics-preserving rewrites (e.g., renaming local variables or deleting comments) during training helped the models generalize better to unseen code.

Key Results

BugLab demonstrated a clear advantage over traditional supervised methods:

  • Performance Gain: On the PyPIBugs dataset, BugLab improved joint detection and repair accuracy by up to 30% compared to models trained only on randomly inserted bugs.
  • Real-World Impact: When run on popular open-source projects, the model identified 19 previously unknown bugs, leading to successful pull requests in projects like apache/beam, pytorch, and saltstack.
  • Comparison to CuBERT: BugLab significantly outperformed CuBERT (a BERT-based bug detector) in both recall and F1 score on variable misuse and operator bugs.
Table 2: Results for different training strategies and model architectures on PYPIBUGS.

Limitations and Future Work

Despite the progress, the authors highlight several hurdles:

  • False Positives: When scanning open-source software "in the wild," the false positive rate was approximately 98%. While the model finds real bugs, it also flags many "odd" but correct code patterns, making it noisy for developers.
  • Scope of Bugs: The current implementation is limited to "stupid simple bugs" (single-statement rewrites). It cannot yet detect complex logic errors that span multiple lines or require deep architectural knowledge.
  • Realistic vs. Hard: The selector sometimes generates bugs that are "hard" for the model but not "realistic" for humans (e.g., changing a configuration literal that doesn't actually break the program's logic).

Future work involves expanding the library of rewrite rules, improving model explainability to reduce false positives, and exploring ways to make the generated bugs more representative of human coding errors.

Summary updated

BibTeX
@inproceedings{allamanis2021self,
  author = {Miltiadis Allamanis and Henry 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