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.
The core of SPACE involves three key principles:
- Identifier-Only Perturbation: Noise is only added to identifiers (variables, functions, classes), leaving syntax-critical keywords like
iforreturnuntouched to preserve code logic. - Consistency Constraint: To maintain the internal "grammar," every instance of the same identifier in a code snippet receives the identical virtual perturbation.
- 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.
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