Abstract
Deep learning models for source code analysis are increasingly used for tasks like classification and clone detection, yet they remain surprisingly fragile. These models often rely on superficial features such as variable names rather than the underlying program logic. This paper introduces the Metropolis-Hastings Modifier (MHM), a novel sampling-based attack that generates valid, semantics-preserving adversarial code through identifier renaming. By training models on these adversarial examples, the authors demonstrate significant improvements in both robustness and raw performance across state-of-the-art code processing architectures.
Introduction
Deep Learning (DL) has become a staple for automated software engineering tasks. However, these models suffer from a lack of adversarial robustness. Similar to how small perturbations in images can fool computer vision models, simple variable renames in source code can lead a model to misclassify a program's function.
Generating adversarial examples for code is uniquely challenging because code is discrete and must adhere to rigid lexical and syntactical rules. Unlike natural language attacks that often produce broken code, an effective code attack must ensure the resulting program is "valid"—meaning it compiles and remains functionally identical to the original.
Methodology: The Metropolis-Hastings Modifier (MHM)
The authors propose the Metropolis-Hastings Modifier (MHM), a sampling-based algorithm rooted in Markov Chain Monte Carlo (MCMC) techniques. MHM treats adversarial generation as a search problem rather than a gradient-based optimization, which is difficult in discrete code spaces.
The process follows three stages in each iteration:
- Select a Source Identifier: A variable or function name defined within the code is chosen for modification.
- Choose a Target Identifier: A new name is selected from a large vocabulary.
- Accept or Reject: The algorithm uses the Metropolis-Hastings acceptance criteria to decide whether to keep the change, aiming to maximize the model's prediction error while ensuring functional equivalence.
Experimental Setup
The study utilizes the Open Judge (OJ) dataset, a benchmark containing 52,000 C/C++ files categorized into 104 functionality types (e.g., "bubble sort"). To focus the models on structure and identifiers, the authors preprocessed the code by removing comments and macros and replacing literals (strings, integers, floats) with generic placeholders.
Key Insights: Identifiers as Non-Robust Features
A primary finding is that models often "cheat" by associating specific variable names with labels. For instance, a programmer might name a variable sushu (Chinese pinyin for "prime number"). The model learns this association rather than the mathematical logic of the prime-finding algorithm.
When MHM renames sushu to maxindex, the model's prediction fails despite the logic remaining untouched. Because MHM only renames identifiers within their proper scope, it achieves a 100% validity rate, a significant improvement over traditional NLP-based attacks which often fail to produce compilable code.
Results and Robustness
MHM was tested against two major architectures: a standard LSTM and the ASTNN (Abstract Syntax Tree-based Neural Network).
- Attack Success: MHM achieved success rates of 71.3% on LSTM and 92.1% on ASTNN, proving that even structure-aware models are highly susceptible to renaming.
- Adversarial Training: By incorporating MHM-generated examples into the training set, the models learned to ignore superficial naming conventions. The attack success rate on LSTM dropped to 46.4%, and on ASTNN to 54.7%.
- Performance Boost: Interestingly, the raw accuracy of the LSTM model increased by 1.1% after adversarial training, suggesting that the process helps the model generalize better by forcing it to focus on structural logic.
Conclusion
While MHM is currently limited to identifier renaming, it demonstrates the profound impact of semantics-preserving transformations on model robustness. Future work will explore more complex perturbations, such as swapping equivalent control structures (e.g., for to while loops) and tree-based modifications to further challenge and improve source code processing models.
Summary updated