Abstract
Deep learning models for code are increasingly used for critical tasks like autocompletion and summarization, yet they remain highly vulnerable to adversarial obfuscation. Traditionally, enhancing model robustness has come at the cost of accuracy on clean data. We introduce CLAWSAT, a framework that breaks this trade-off. By utilizing adversarial examples as "hard positives" during contrastive pre-training (CLAW) and implementing a staggered adversarial training schedule (SAT) during fine-tuning, CLAWSAT achieves state-of-the-art performance in both robustness and accuracy across multiple programming languages and tasks.
Introduction
Deep learning models for code often fail when faced with simple semantic-preserving transformations, such as variable renaming or the insertion of dead code. While these modifications do not change the program's logic, they can drastically alter a model's output. In other domains, like computer vision, defending against such attacks typically results in a "robustness-accuracy trade-off." This research investigates whether this trade-off is inevitable for code models and proposes a self-supervised learning (SSL) approach to overcome it.
The CLAWSAT Framework
The CLAWSAT framework consists of two distinct modules designed to build a robust foundation and then specialize it for downstream tasks without losing that robustness.
CLAW: Contrastive Learning with Adversarial Views
During the self-supervised pre-training phase, CLAW employs contrastive learning to teach the model that different versions of the same code should have similar internal representations. Unlike standard methods that use random transformations, CLAW uses bi-level optimization to generate the most challenging adversarial views possible.
By forcing the model to remain invariant to these "hard positives," CLAW creates a more stable and flatter loss landscape, which is a key indicator of generalization and robustness.
SAT: Staggered Adversarial Training
In the supervised fine-tuning phase, the authors introduce Staggered Adversarial Training (SAT). Standard adversarial training, which attacks the model in every batch, can often "wash out" the useful features learned during pre-training. SAT utilizes a staggered schedule (defined by a hyperparameter $\tau$), where adversarial examples are only introduced every few epochs. This allows the model to optimize for task-specific accuracy while retaining the robust features established by CLAW.
Experimental Evaluation
The framework was evaluated using Python and Java datasets, including PY-CSN and JAVA-CSN for pre-training, and PY150 and JAVA-C2S for fine-tuning. The researchers tested the model on three primary tasks:
- Code Summarization: Generating natural language docstrings.
- Code Completion: Predicting subsequent code tokens.
- Code Clone Detection: Identifying functionally equivalent code snippets.
Results and Key Insights
CLAWSAT consistently outperformed existing state-of-the-art models like ContraCode in both accuracy (GEN-F1) and robustness (ROB-F1).
- Performance Gains: In Python code summarization, CLAWSAT achieved an 11% absolute increase in robustness and a 6% increase in accuracy over the baseline. Similar improvements were noted in code completion (3% robustness, 2% accuracy).
- The "Sweet Spot" of Frequency: A critical finding was that attacking the model less frequently during fine-tuning (via SAT) yielded better results than constant adversarial training. This staggering prevents the optimization process from over-correcting and destroying the model's ability to generalize.
- Interpretability: Through "Explanation-by-Example" (EBE) analysis, the authors demonstrated that CLAWSAT is significantly better at mapping obfuscated code back to its original functional counterparts in the training set compared to standard models.
Conclusion and Future Work
CLAWSAT demonstrates that the robustness-accuracy trade-off is not an inherent limitation for code models. By combining adversarial contrastive pre-training with a staggered fine-tuning schedule, models can become both more secure and more capable. Future work will explore the framework's effectiveness against "black-box" attacks and seek a deeper theoretical understanding of why discrete code structures respond uniquely to these optimization strategies.
Summary updated