NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Learning How to Mutate Source Code from Bug-Fixes

Michele Tufano, Cody Watson, Gabriele Bavota, Massimiliano Di Penta, Martin White, Denys PoshyvanykarXiv 2018

TL;DR

This paper introduces a novel, data-driven approach that automatically learns how to perform mutation testing by training deep learning models on real-world bug fixes. By treating mutation generation as a reverse machine translation task (Fixed Code $\rightarrow$ Buggy Code), the system generates highly realistic, context-aware mutants, over 98% of which are syntactically correct.

What Problem Does This Paper Address?

Mutation testing is a powerful technique used to evaluate and improve test suites by intentionally injecting artificial faults (mutants) into source code. If a test suite fails to detect these mutants, it indicates a gap in test coverage.

Traditionally, mutation tools rely on handcrafted, rule-based operators (e.g., swapping a + for a -). However, these manual operators suffer from significant drawbacks:

  1. Lack of Realism: Handcrafted mutants often fail to resemble the complex, subtle bugs that developers actually introduce in practice.
  2. Context Blindness: Traditional tools apply mutations blindly without understanding where a mutation makes sense or how to realistically alter a specific code structure.
  3. High Manual Effort: Designing domain-specific mutation operators requires substantial engineering time and rarely generalizes across different projects.

The authors address this gap by asking: Can we leverage millions of existing open-source bug fixes to automatically learn how to inject realistic bugs?

Methods

The authors design a multi-step pipeline to train a Recurrent Neural Network (RNN) Encoder-Decoder model to act as an automated "bug injector":

  1. Bug-Fix Mining & AST Differencing: The pipeline extracts method-level pairs of buggy and fixed code from GitHub. It uses Abstract Syntax Tree (AST) differencing (via GumTree) to identify the exact structural changes made during the fix.
  2. Code Abstraction & Idioms: To prevent the model from choking on an infinite vocabulary of custom variable and method names, the code is abstracted (e.g., custom variables become VAR_1, VAR_2). Crucially, the authors retain "idioms"—frequently used terms like 0, 1, size(), or equals()—to preserve essential semantic context.
  3. Change Clustering: Instead of training one general model on all types of bugs, the authors use doc2vec and k-means clustering to group similar AST edit actions into 5 distinct clusters. This allows them to train specialized mutation models ($M_1$ through $M_5$).
  4. Deep Learning (NMT): The core engine is an RNN Encoder-Decoder architecture with an attention mechanism. It is trained in reverse: it takes fixed code as input and learns to output the buggy code.
Fig. 1: Transformation Pair Example.

Datasets

The authors compiled a massive dataset from the GitHub Archive, spanning public events between March 2011 and October 2017.

  • Initial Scraping: Mined over 10 million bug-fixing commits using keyword heuristics.
  • Filtering: Cleaned the dataset by removing non-Java repositories, massive/tangled commits, and methods longer than 50 tokens.
  • Final Corpus: Yielded approximately 380,000 method-level Transformation Pairs (TPs) used for training, validation, and testing.

Key Insights and Ideas

  • The "Perfect Mutant" Concept: A historical bug is theoretically the perfect mutant because it represents a mistake a human actually made, which a real-world test suite initially failed to catch.
  • The Power of Idioms: Pure code abstraction strips away too much meaning. Keeping a curated list of 272 "idioms" (like common numbers, standard types, and ubiquitous method names) strikes a golden balance: it keeps the vocabulary small enough for deep learning while retaining enough context to generate smart mutations.
  • Specialization via Clustering: Code changes are incredibly diverse. By clustering the AST diffs before training, the authors successfully trained specialized models that excel at specific mutation types (e.g., one model focuses on altering if-else logic, while another specializes in deleting method calls).

Key Results

The authors evaluated their general models ($GM_{ident}$, $GM_{ident-lit}$) and their five cluster-specialized models ($M_1$–$M_5$):

  • Recreating Real Bugs: Remarkably, when given completely unseen fixed code, the models perfectly predicted and recreated the exact historical bug in 9% to 45% of cases (depending on the model used). Model $M_1$ achieved the highest perfect prediction rate at 45%.
  • High Syntactic Validity: Over 98% of all generated mutants were lexically and syntactically correct, meaning they successfully parse without basic syntax errors. This vastly outperforms prior diff-based mutation techniques, which often suffer from high compilation failure rates.
TABLE II: Prediction Classification
  • Semantic Diversity: Qualitative analysis proved that the models learned complex, multi-token mutations, such as replacing .equals() method calls with == operators, or introducing infinite loops by subtly manipulating loop counters.

Limitations and Future Work

While a major step forward, the paper notes a few limitations:

  • Method Size Constraints: The current models are optimized for small-to-medium methods ($\le 50$ tokens). When tested on larger methods (50–100 tokens), the perfect prediction rate dropped sharply to around 6%.
  • Language Dependency: The evaluation was strictly performed on Java code, though the underlying conceptual framework is language-agnostic.
  • Tool Integration: The paper focuses on the machine learning generation aspect; it does not yet provide a fully integrated, production-ready mutation testing toolchain that automatically compiles and runs test suites against these mutants. The authors highlight this infrastructure development as a primary avenue for future work.

Summary updated

BibTeX
@misc{tufano2018learning,
  author = {Michele Tufano and Cody Watson and Gabriele Bavota and Massimiliano Di Penta and Martin White and Denys Poshyvanyk},
  title = {{Learning How to Mutate Source Code from Bug-Fixes}},
  year = {2018},
  eprint = {1812.10772},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1812.10772},
}
Figure