Abstract
Deep Neural Networks (DNNs) are increasingly employed for critical software engineering tasks, yet their robustness against semantic-preserving code transformations remains a concern. This paper introduces Guided Mutation (GM), a search-based testing framework designed to identify vulnerabilities in source code embedding models. By leveraging evolutionary algorithms to find adversarial code changes that maintain original logic while inducing model mispredictions, GM exposes structural weaknesses. Experimental results across models like Code2vec and CodeBERT demonstrate that retraining with GM-generated samples improves model robustness by an average of 23% with minimal impact on standard performance.
Introduction
As Deep Learning becomes a cornerstone for tasks such as code search, comment generation, and bug fixing, the reliability of the underlying "code embeddings" is paramount. Unlike image or natural language processing, where adversarial perturbations like pixel noise or synonym swapping are common, source code requires strict adherence to grammar and semantics. Previous research has shown that trivial, non-functional changes—such as adding a print statement—can significantly degrade model performance. The challenge lies in systematically identifying these vulnerabilities while respecting the rigid constraints of programming languages.
Methodology: The Guided Mutation Framework
The proposed Guided Mutation (GM) framework treats the generation of adversarial test cases as a search problem. It consists of two primary stages:
- Semantic-Preserving Refactoring: The framework utilizes ten common Java refactoring operators (e.g., local variable renaming, loop transformations, or adding redundant arguments). These operations modify the code's syntax without altering its execution logic.
- Evolutionary Search: Rather than applying transformations randomly, GM employs an evolutionary strategy to find the most effective perturbations. It calculates a "Mutation Score" based on the DeepMutation++ framework to quantify model sensitivity. Code snippets are evolved over multiple iterations, with "elite" candidates selected to form potent adversarial examples.
Experimental Setup
The study evaluated three state-of-the-art code embedding models across various downstream tasks, including method name prediction and code search.
The models were tested using two large-scale datasets: the Java-Large Dataset, comprising 16 million examples from 9,000 projects, and CodeSearchNet, a bimodal dataset featuring 2.1 million datapoints.
Key Results and Insights
The GM framework successfully identified significant vulnerabilities in existing models. Adversarial samples reduced the performance of state-of-the-art DNNs by an average of 5.41% to 9.58%.
Crucially, retraining these models with GM-generated adversarial samples led to substantial robustness gains, averaging 23.05%. In specific instances, such as Code2vec for method name prediction, robustness improved by nearly 50%. Notably, this approach maintained a high level of accuracy on "clean" data, with only a 3.56% median performance drop—outperforming simpler random mutation baselines.
The research also highlighted that robustness is task-dependent. For instance, CodeBERT exhibited higher sensitivity to adversarial changes during "Code Document Generation" compared to "Code Search" tasks.
Conclusion and Future Work
The Guided Mutation framework provides a robust method for testing and improving DNNs used in software engineering. While currently focused on Java, the underlying principles are language-agnostic. Future research will focus on expanding the set of refactoring operators, reducing the computational overhead of retraining, and exploring how these adversarial examples correlate with diverse human coding styles.
Summary updated