TL;DR
This paper introduces CoTR, a novel framework to assess and improve the robustness of pre-trained models (PTMs) for code translation against syntactic adversarial attacks. They demonstrate that current PTMs are surprisingly vulnerable to these attacks and propose CoTR-D, a defense mechanism combining data augmentation and adversarial training, to significantly enhance robustness without sacrificing performance.
What Problem Does This Paper Address?
Automated code translation using pre-trained models (PTMs) has shown great promise, but their reliability in real-world scenarios is questionable. Specifically, these models can be brittle when faced with subtle, syntactically different but semantically equivalent code variations. This vulnerability can lead to incorrect translations and introduce bugs, undermining the trust in these automated tools. Existing research has primarily focused on robustness against token-level attacks, neglecting the impact of syntactic transformations that are common in programming. This paper aims to bridge this gap by systematically evaluating and improving the syntactic adversarial robustness of PTMs in code translation.
Methods
The authors propose a two-component framework called CoTR:
- CoTR-A (Attack Component): This component generates adversarial examples by applying a set of program transformation rules. These rules are designed to create semantically equivalent code variations that can fool the PTM. CoTR-A focuses on "Statement Exchange" rules (Loop Exchange, Expression Exchange, Permutation Exchange, Condition Exchange) because these are more likely to preserve functional consistency while introducing syntactic variations. They use an exhaustive search to generate candidate snippets and identify the most effective adversarial examples.
- CoTR-D (Defense Component): This component aims to improve the robustness of PTMs. It employs a two-pronged strategy:
- Semantic Distance-based Data Augmentation: They augment the training data with semantically equivalent variants generated by CoTR-A. To prevent overfitting and select the most informative augmentations, they use CodeBERT to calculate semantic distances between original and variant code snippets and sample those with the largest distances.
- Adversarial Training (N-PGD): They incorporate a noisy-enhanced adversarial training method based on Projected Gradient Descent (PGD) to further train the model against adversarial examples.
Datasets
The study uses the AVATAR dataset, a compilation of Java/Python code from competitive programming sites, online platforms, and open-source repositories. The authors pre-process this dataset using four heuristic rules to ensure clean and high-quality data. They create a training set of 2,600 pairs, a validation set of 200 pairs, and a test set of 200 pairs. Test cases for the test set were constructed by postgraduate students.
Key Insights and Ideas
- Syntactic Vulnerability is Real: The paper convincingly shows that even sophisticated PTMs, including large language models (LLMs), are surprisingly susceptible to syntactic adversarial attacks. Minor code changes that preserve functionality can drastically degrade translation performance.
- Token-Based Attacks Aren't Enough: Existing robustness evaluation methods that focus on token-level attacks might miss critical vulnerabilities related to code structure and syntax. CoTR-A's success highlights the need for syntax-aware attack and defense strategies.
- Statement Exchange Rules are Key: The authors identify "Statement Exchange" rules as particularly effective for generating meaningful adversarial examples because they can alter syntax without necessarily breaking functionality, thus posing a genuine challenge to PTMs.
- Defense Requires a Dual Approach: Neither data augmentation nor adversarial training alone is sufficient. CoTR-D's combination of both strategies proves effective in enhancing robustness without significantly compromising accuracy.
- LLMs Still Have Robustness Issues: Even powerful LLMs like ChatGPT, while performing well in zero-shot scenarios, still exhibit significant robustness degradation under syntactic attacks.
Key Results
- Attack Effectiveness: CoTR-A significantly reduces the Pass@1 metric for existing PTMs, by at least 17.97% for Java-to-Python and 14.29% for Python-to-Java. It outperforms other token-based attack methods like RADAR and ALERT on most models.
- Model Performance: Encoder-Decoder models generally outperform Encoder-only and Decoder-only models in terms of translation accuracy.
- Defense Effectiveness (CoTR-D): CoTR-D successfully improves model robustness. For instance, for CodeT5 in Java-to-Python translation, CoTR-D keeps the Robust Drop@1 (RDs@1) metric to a low 0.67%, indicating minimal performance degradation under attack, while maintaining a high Pass@1. Most models show RDs@1 values within 10% after applying CoTR-D.
- Human Evaluation: A human study confirmed that CoTR-A generates adversarial code that is perceived as more informative and readable than that generated by RADAR and ALERT.
Limitations and Future Work
- Dataset Limitations: The AVATAR dataset, derived from code competitions, might not fully represent the complexity of real-world software development scenarios.
- Language Independence: While the approach is designed to be language-independent, the current study focuses on Java and Python. Future work could expand to more diverse programming languages.
- More Robust Models: The authors plan to develop more robust pre-trained models that can handle various programming styles and syntax conventions. They also suggest exploring program repair or leveraging LLMs further for improved robustness.
Summary updated