Abstract
Pre-trained models for natural language (NL) code search, such as CodeBERT, have revolutionized how developers interact with codebases but remain vulnerable to adversarial perturbations. Existing attack methods often suffer from high query complexity and poor semantic preservation. We propose TABS (Efficient Textual Adversarial Attack using Semantic Beam Search), a black-box attack method that combines contextual semantic filtering with beam search. TABS generates high-quality adversarial examples that maintain the original query's meaning while significantly reducing the number of model queries. Evaluated on the CodeSearchNet dataset, TABS achieves a higher success rate and better semantic similarity with orders of magnitude fewer queries than state-of-the-art baselines.
Introduction
Pre-trained models like CodeBERT have become essential for tasks like code retrieval using natural language. However, these models are often "brittle": minor, meaning-preserving changes in an NL query—such as replacing "find" with "search"—can lead to drastically different and irrelevant code retrieval results. This vulnerability poses a significant challenge to the reliability and security of code intelligence systems.
Problem Statement
Existing adversarial attack strategies typically rely on "greedy search," replacing words sequentially to maximize model error. This approach has several drawbacks:
- Inefficiency: It requires a high volume of queries to the victim model, making it computationally expensive and easier to detect.
- Local Optima: Greedy search is "short-sighted," failing to account for how multiple word changes interact globally within a sentence.
- Semantic Degradation: The resulting adversarial queries often lose grammatical correctness or deviate significantly from the original intent.
Methodology: TABS
TABS addresses these limitations through two core innovations:
Contextual Semantic Filtering
Instead of relying on simple word-level synonyms, TABS utilizes a Masked Language Model (BERT) to generate candidate replacements. These candidates are then filtered using contextual embeddings to ensure they fit the specific surrounding sentence structure. This process narrows the search space to only the most plausible, meaning-preserving words, maintaining the semantic integrity of the query.
Semantic Beam Search
TABS employs a beam search strategy rather than a greedy one. By maintaining a set of $k$ (the "beam width") best candidate sentences at each step, the algorithm explores various combinations of word replacements. This global search perspective allows TABS to find more effective adversarial examples that a greedy approach would overlook. The aggressive filtering from the contextual semantic step prevents the query count explosion typically associated with beam search.
Experimental Setup
The effectiveness of TABS was evaluated using the CodeSearchNet dataset within the CodeXGLUE benchmark. The study focused on two primary tasks:
- NL-Code Search Classification: Determining if a text-code pair is semantically equivalent.
- NL-Code Search Retrieval: Retrieving the most relevant code snippet from a library based on an NL query.
CodeBERT served as the victim model, while RoBERTa was used as the underlying language model for generating candidate synonyms.
Results and Discussion
TABS consistently outperforms state-of-the-art baselines, including TextFooler, BERT-Attack, and CLARE, across multiple metrics.
Key Findings:
- Higher Success Rate: In classification tasks, TABS achieved an attack success rate of 87.4% at a similarity threshold ($\epsilon$) of 0.2, surpassing BERT-Attack's 79.2%.
- Massive Efficiency Gains: TABS demonstrated a significant speedup, requiring only ~50 queries per successful attack. In contrast, the high-performing CLARE method required over 900 queries to achieve comparable results.
- Superior Semantic Quality: Adversarial examples generated by TABS maintained higher semantic similarity (measured by Universal Sentence Encoder scores) compared to most baselines, ensuring the attacks remain stealthy and natural.
The success of TABS highlights that adversarial attacks are fundamentally a search problem. By allowing the attack to "change its mind" about earlier word choices through beam search, TABS finds more potent vulnerabilities. Furthermore, the efficiency gains prove that smarter synonym selection is more effective than exhaustive testing.
Conclusion
TABS provides a robust and efficient framework for exposing vulnerabilities in NL-code models. While currently optimized for code search, future work could explore its generalization to broader NLP tasks like sentiment analysis. Additionally, the efficiency of TABS makes it a prime candidate for adversarial training, helping to build more resilient code intelligence models in the future.
Summary updated