A Search-Based Testing Framework for Deep Neural Networks of Source Code Embedding

Maryam Vahdat Pour, Zhuo Li, Lei Ma, Hadi Hemmati

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:

  1. 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.
  2. 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.
The Guided Mutation (GM) framework uses an evolutionary search to identify elite adversarial code snippets.

Experimental Setup

The study evaluated three state-of-the-art code embedding models across various downstream tasks, including method name prediction and code search.

Overview of the code embedding models, evaluation metrics, and downstream tasks used in the study.

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

Abstract

Over the past few years, deep neural networks (DNNs) have been continuously expanding their real-world applications for source code processing tasks across the software engineering domain, e.g., clone detection, code search, comment generation. Although quite a few recent works have been performed on testing of DNNs in the context of image and speech processing, limited progress has been achieved so far on DNN testing in the context of source code processing, that exhibits rather unique characteristics and challenges.In this paper, we propose a search-based testing framework for DNNs of source code embedding and its downstream processing tasks like Code Search. To generate new test inputs, we adopt popular source code refactoring tools to generate the semantically equivalent variants. For more effective testing, we leverage the DNN mutation testing to guide the testing direction. To demonstrate the usefulness of our technique, we perform a large-scale evaluation on popular DNNs of source code processing based on multiple state-of-the-art code embedding methods (i.e., Code2vec, Code2seq and CodeBERT). The testing results show that our generated adversarial samples can on average reduce the performance of these DNNs from 5.41% to 9.58%. Through retraining the DNNs with our generated adversarial samples, the robustness of DNN can improve by 23.05% on average. The evaluation results also show that our adversarial test generation strategy has the least negative impact (median of 3.56%), on the performance of the DNNs for regular test data, compared to the other methods.
BibTeX
@inproceedings{pour2021search,
  author = {Maryam Vahdat Pour and Zhuo Li and Lei Ma and Hadi Hemmati},
  title = {{A Search-Based Testing Framework for Deep Neural Networks of Source Code Embedding}},
  booktitle = {ICST},
  year = {2021},
  url = {https://openreview.net/forum?id=gmgR_fEz-Yp},
}
Figure