Semantic-Preserving Adversarial Code Comprehension

Yiyang Li, Hongqiu Wu, Hai Zhao

TL;DR

Researchers have developed SPACE (Semantic-Preserving Adversarial Code Embeddings), a training method that enhances both the accuracy and adversarial robustness of code-based AI models. By injecting "worst-case" perturbations into internal representations while respecting programming language syntax, SPACE overcomes the traditional trade-off between performance and security.

Introduction

Pre-trained Language Models (PrLMs) like CodeBERT and GraphCodeBERT are essential for modern software engineering tasks, such as vulnerability detection and code search. However, these models are highly susceptible to adversarial attacks. A simple semantic-preserving change, such as renaming a variable, can lead to incorrect model predictions. Existing defenses often suffer from a performance-robustness trade-off or high computational complexity due to searching for attacks in discrete code space.

Methodology: The SPACE Framework

The authors propose SPACE to address these issues by operating in the continuous embedding space rather than on raw code tokens. This approach allows for efficient, gradient-based optimization to find adversarial examples during training.

Overview of the SPACE framework architecture, highlighting the Adversarial Block where differentiable perturbations are applied to identifiers.

The core of SPACE involves three key principles:

  1. Identifier-Only Perturbation: Noise is only added to identifiers (variables, functions, classes), leaving syntax-critical keywords like if or return untouched to preserve code logic.
  2. Consistency Constraint: To maintain the internal "grammar," every instance of the same identifier in a code snippet receives the identical virtual perturbation.
  3. Differentiable Optimization: By working in the embedding space, the model can use min-max optimization to identify and learn from "worst-case" scenarios efficiently.

Experimental Results

The researchers evaluated SPACE across multiple benchmarks, including Defects4J (defect detection), CodeSearchNet (code search), and CodeQA (question answering).

Performance and Robustness

SPACE consistently outperformed standard baselines and vanilla adversarial training. For instance, on the Defects4J task, SPACE improved CodeBERT’s accuracy by 3.93%. More importantly, it significantly bolstered the models against sophisticated attacks.

Attack Success Rate (ASR) comparison across different models and attack methods, showing SPACE's superior robustness.

As shown in the results, SPACE reduced the Attack Success Rate (ASR) of the ALERT attack by over 10% compared to the baseline. This demonstrates that the model learns to focus on structural logic rather than specific naming conventions.

Conclusion and Future Work

SPACE demonstrates that adversarial training in the embedding space can yield a "synergy" where models become both more robust and more accurate. While currently focused on identifier renaming, future research could expand SPACE to include other semantic-preserving transformations like statement reordering. Although it increases initial training time, the method has no impact on inference speed, making it a practical solution for deploying secure code-intelligence tools.

Summary updated

BibTeX
@inproceedings{li2022semantic,
  author = {Yiyang Li and Hongqiu Wu and Hai Zhao},
  title = {{Semantic-Preserving Adversarial Code Comprehension}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2022},
  url = {https://openreview.net/forum?id=XxImLjRBYbA},
}
Figure