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 deployed for critical source code processing tasks, yet their robustness against semantic-preserving variations remains a significant concern. We present a search-based testing framework that utilizes code refactoring and DNN mutation testing to generate adversarial examples. Evaluated on state-of-the-art models including Code2vec and CodeBERT, our framework identifies vulnerabilities that reduce performance by up to 9.58%. Furthermore, we demonstrate that retraining with these adversarial samples improves model robustness by an average of 23.05% with minimal impact on standard accuracy.

Introduction

As Deep Learning becomes a cornerstone of modern software engineering tools—powering everything from automated code search to bug detection—the reliability of these models is paramount. These systems rely on "code embeddings" to translate complex source code into vector representations. However, recent studies highlight a significant fragility: minor, non-functional changes to code can drastically alter model predictions. Unlike the continuous perturbations used in image-based adversarial attacks, code-based attacks must adhere to strict syntactic and semantic constraints. This paper introduces a systematic, search-based approach to generate valid adversarial test cases and enhance model robustness.

Methodology

Our framework treats adversarial generation as an evolutionary search problem, employing a strategy termed Guided Mutation (GM).

Refactoring as Perturbation

To maintain semantic equivalence, we use a suite of ten Java refactoring operators. These transformations, such as renaming local variables or swapping for and while loops, ensure that the perturbed code remains functionally identical to the original while presenting a different structural input to the DNN.

Guided Mutation (GM) Strategy

The GM strategy evolves a population of code snippets over multiple iterations. Unlike standard genetic algorithms, we found that crossover operations often produce uncompilable code. Consequently, our framework focuses on mutation and elitism to navigate the search space effectively.

The Guided Mutation (GM) framework for generating adversarial code snippets.

Fitness Function via DNN Mutation Testing

The search is guided by a fitness function based on DNN mutation testing. We generate "mutant" versions of the target DNN by applying specific operators to its internal parameters (weights, states, or gates).

Mutation operators used to create DNN mutants for guiding the search process.

A code snippet's fitness is defined by its mutation score—the degree to which it causes the mutant models to deviate from the original model's output. This score identifies inputs that lie near the model's decision boundaries, making them potent adversarial candidates.

Experimental Evaluation

We conducted a large-scale evaluation using three state-of-the-art models: Code2vec, Code2seq, and CodeBERT. These models were tested on downstream tasks like method name prediction and code search using the Java-Large dataset and CodeSearchNet.

Results

Our findings confirm that state-of-the-art code embedding models are vulnerable to targeted semantic perturbations. The adversarial samples generated by our GM framework reduced model performance by an average of 5.41% to 9.58%.

Crucially, we found that retraining models with these adversarial samples significantly bolstered their defenses. Robustness improved by an average of 23.05%, with some tasks seeing gains up to 49%. Importantly, this targeted retraining proved more stable than random data augmentation, resulting in a median performance drop of only 3.56% on regular test data.

Conclusion

The proposed search-based framework provides a robust methodology for testing and improving DNNs in the source code domain. By leveraging mutation-guided search and semantic-preserving refactoring, we can systematically uncover and address model vulnerabilities, leading to more reliable AI-driven software engineering tools.

Summary updated

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