DIP: Dead code Insertion based Black-box Attack for Programming Language Model

CheolWon Na, YunSeok Choi, Jee-Hyong LeeACL 2023

Abstract

Programming Language (PL) models like CodeBERT and CodeT5 are increasingly used for critical tasks such as defect detection and clone identification. However, these models are vulnerable to adversarial attacks. This paper introduces DIP (Dead code Insertion based Black-box Attack), a novel black-box attack method that inserts semantically neutral "dead code" into source files. DIP identifies vulnerable positions and high-attention code snippets to mislead models while maintaining 100% compilability and functional equivalence. Experimental results across multiple benchmarks show that DIP significantly outperforms existing state-of-the-art attacks in success rate and query efficiency.

Introduction

The rise of large-scale pre-trained models for programming languages has transformed software engineering tasks. Despite their success, these models inherit vulnerabilities similar to natural language processing models. Adversarial attacks on code are particularly challenging because modifications must preserve both syntax (compilability) and semantics (logic).

Existing black-box attacks often rely on variable renaming. While effective in some contexts, variable renaming suffers from a large search space, high query overhead, and the risk of breaking code by renaming global variables or library functions. To address these limitations, we propose DIP, which focuses on the strategic insertion of dead code rather than the replacement of existing tokens.

Methodology

DIP operates by inserting a single, high-impact line of dead code into a target source file. The process consists of three primary stages:

  1. Vulnerable Position Selection: Using a surrogate pre-trained model, DIP identifies lines where the model's internal representation is most sensitive to perturbations. This is achieved by inserting [UNK] tokens and measuring changes in the [CLS] token embedding.
  2. Snippet Extraction: To maximize confusion, DIP searches for code snippets that are semantically dissimilar to the target. It leverages attention scores to extract the most "important" line from this dissimilar code.
  3. Dead Code Wrapping and Insertion: The extracted snippet is wrapped in a dummy variable assignment (e.g., String unused_var = "snippet";). This ensures the insertion is syntactically valid but logically inert. The resulting "dead code" is then placed in the previously identified vulnerable position.
Overview of the DIP attack generation and execution process.

Experimental Evaluation

The effectiveness of DIP was evaluated across three major tasks using nine different victim models:

  • Clone Detection: Java (BigCloneBench)
  • Defect Detection: C/C++ (Devign)
  • Authorship Attribution: Python (Google Code Jam)

The victim models included CodeBERT, GraphCodeBERT, and CodeT5. DIP was compared against two state-of-the-art black-box attacks: MHM (Metropolis-Hastings Modifier) and ALERT.

Results

DIP demonstrated superior performance across all metrics. As shown in the results table, DIP achieved an average Attack Success Rate (ASR) of 43.9%, surpassing ALERT (34.1%) and MHM (25.1%). Notably, DIP is significantly more efficient, requiring an average of only 106.2 queries compared to over 440 for the baselines.

Performance comparison of DIP against MHM and ALERT across various tasks and models.

Furthermore, the quality of the generated code remained high, with an average CodeBLEU score of 0.88, indicating that the adversarial examples maintain structural integrity and are indistinguishable from natural code to the human eye.

Discussion and Conclusion

The success of DIP highlights a fundamental vulnerability in how PL models utilize attention mechanisms. By inserting code that the model perceives as highly relevant but is actually dead code, DIP effectively "distracts" the model from the functional logic.

While DIP is highly effective, it does have limitations. The initial preprocessing to find vulnerable positions requires a surrogate model, and sophisticated static analysis tools might eventually be tuned to detect such insertions. Future work will explore combining DIP with other techniques like variable renaming to create even more robust adversarial examples and, conversely, developing more resilient PL models through adversarial training.

In conclusion, DIP provides a faster, more reliable, and more effective method for testing the robustness of programming language models, emphasizing the need for improved defensive strategies in AI-assisted software engineering.

Summary updated

BibTeX
@inproceedings{na2023dip,
  author = {CheolWon Na and YunSeok Choi and Jee-Hyong Lee},
  title = {{DIP: Dead code Insertion based Black-box Attack for Programming Language Model}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-long.430},
}
Figure