A Black-Box Attack on Code Models via Representation Nearest Neighbor Search

Jie Zhang, Wei Ma, Qiang Hu, Shangqing Liu, Xiaofei Xie, Yves Le Traon, Yang Liu

Introduction

Deep learning models for code, such as CodeBERT and CodeT5, have become foundational for critical software engineering tasks like vulnerability detection and clone identification. However, these models exhibit significant fragility when faced with adversarial examples—small, semantics-preserving modifications designed to induce incorrect predictions. While white-box attacks can exploit internal model gradients, black-box attacks must rely solely on model outputs. Existing black-box methods often suffer from limited substitute diversity, high query costs, and a lack of stealth, frequently producing "unnatural" code that is easily flagged by human reviewers.

To address these challenges, researchers have introduced Representation Nearest Neighbor Search (RNNS), a novel black-box attack framework that shifts the search for adversarial substitutes from discrete word-level guessing to a continuous vector space.

The RNNS Approach: Moving to Continuous Space

The core innovation of RNNS lies in its use of a continuous embedding space to identify optimal variable name substitutes. Rather than relying on a fixed dictionary, RNNS leverages a massive pool of variable names extracted from real-world open-source projects.

The process involves several key steps:

  1. Dense Encoding: A pre-trained encoder maps potential substitutes into a high-dimensional vector space.
  2. Predictive Search Seed: Instead of random sampling, RNNS maintains a "search seed" vector. This seed is updated based on the success of previous queries, effectively "steering" the search toward regions of the vector space that are more likely to fool the victim model.
  3. Nearest Neighbor Search (NNS): Using the search seed, the algorithm efficiently identifies the $k$ nearest variable names that satisfy semantic and length constraints.
  4. Uncertainty-Based Prioritization: RNNS identifies which variables in the source code are most likely to impact the model's prediction if changed, attacking these high-uncertainty targets first to maximize efficiency.

Performance and Efficiency

The effectiveness of RNNS was evaluated across six diverse coding tasks and three programming languages (Java, Python, and C). When tested against 18 different victim models, RNNS demonstrated overwhelming superiority over existing baselines like MHM and ALERT.

RNNS achieves a 100% success rate across 18 diverse task-model combinations, often with superior query efficiency.

As shown in the table above, RNNS achieved a 100% success rate (18/18) in terms of Attack Success Rate (ASR). Beyond just being more effective, it was also highly efficient, winning the query time (QT) metric in nearly half of the scenarios. By using mathematical search in vector space rather than brute-force guessing, RNNS significantly reduces the number of interactions required with the victim model.

Stealth and Naturalness

A critical requirement for a successful adversarial attack is stealth. Previous methods often generated code with excessively long or semantically irrelevant variable names. RNNS addresses this by enforcing similarity and length constraints during its nearest neighbor search.

A comparison of adversarial code shows that RNNS produces changes that are much more natural and less disruptive than previous methods like MHM.

Visual inspection of the generated adversarial code reveals that RNNS maintains the "naturalness" of the original source. While methods like MHM might rename a large number of variables to obscure strings, RNNS typically requires fewer modifications (averaging ~3.6 variables vs. ~5.4 for MHM) and selects substitutes that blend in with standard coding practices.

Conclusion and Future Work

RNNS represents a significant advancement in adversarial testing for code models. By treating the attack as a trajectory in a continuous embedding space, it achieves higher success rates with fewer queries and greater stealth. While currently limited by a greedy search strategy and sensitivity to certain hyperparameters, the framework provides a robust tool for evaluating—and ultimately improving—the security of AI-driven software tools through adversarial training. Future research will focus on extending RNNS to targeted attack scenarios and automating hyperparameter selection.

Summary updated

BibTeX
@inproceedings{zhang2023black,
  author = {Jie Zhang and Wei Ma and Qiang Hu and Shangqing Liu and Xiaofei Xie and Yves Le Traon and Yang Liu},
  title = {{A Black-Box Attack on Code Models via Representation Nearest Neighbor Search}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.649},
}
Figure