Introduction
Code Pre-trained Models (CodePTMs) such as CodeBERT and CodeT5 have revolutionized neural code intelligence, achieving state-of-the-art performance in tasks like code summarization, translation, and defect detection. However, these models often exhibit significant fragility; minor, semantically insignificant changes to the input code can lead to incorrect or nonsensical outputs. This research addresses this "brittleness" by focusing on the hierarchical structure of programming languages, specifically through the lens of Abstract Syntax Trees (ASTs).
Methods: Structure-Aware Adversarial Attacks
The authors propose a framework that moves beyond simple textual perturbations into structural manipulation. By treating code as a tree of logical relationships rather than a flat sequence of tokens, they introduce three main types of "structure-aware" attacks:
- Subtree Attack: This identifies a non-leaf node in the AST (such as a loop or conditional block) and deletes it along with all its descendants, testing if the model can still interpret the remaining code fragment.
- Signature Attack: This replaces a function's name (its signature) with a random word. While the internal logic remains identical, the change in context tests if the model relies too heavily on identifier names rather than the actual logic.
- Reconstruction Attack (DFS/BFS): The AST is "flattened" back into a sequence using Depth-First Search (DFS) or Breadth-First Search (BFS) traversals. This preserves all original tokens but reorders them according to the tree structure, challenging the model's sensitivity to token order.
To counter these vulnerabilities, the authors employ Adversarial Training. Instead of training only on "clean" code, they incorporate these structurally perturbed samples into the fine-tuning process, forcing the model to minimize a joint loss function that accounts for both standard and adversarial inputs.
Key Insights: The Vulnerability of CodePTMs
The evaluation reveals that current CodePTMs are heavily reliant on textual patterns. A primary finding is the "Reconstruction Wall": performance plummeted when token order was changed via DFS/BFS, even though all logical information remained present. This suggests that models lack a deep understanding of underlying code logic.
Furthermore, models show a dangerous over-reliance on naming conventions. In a Signature Attack, changing a function name from max to sum often tricked models into summarizing the code as a "sum of two terms," even when the logic clearly performed a maximum operation.
Notably, UniXcoder—which incorporates AST information during its initial pre-training—showed more resilience to reconstruction attacks than models like CodeT5. This suggests that structural awareness is most effective when integrated early in the model's development.
Results and Discussion
The quantitative impact of these attacks was staggering. In Code Translation (Java → C#), CodeT5’s BLEU score fell from 81.95 to 8.70 under a DFS reconstruction attack. However, adversarial training provided a significant recovery, bringing the score back up to 46.94.
Across the CodeXGLUE benchmark, which includes clone detection, defect detection, and code summarization across six languages, adversarial training consistently improved robustness. It also stabilized model outputs, preventing common failure modes such as the generation of empty strings when confronted with structural perturbations.
Conclusion
This work highlights the necessity of considering code structure in the evaluation and training of CodePTMs. By moving beyond textual perturbations to structure-aware adversarial attacks, the authors expose fundamental weaknesses in current models and demonstrate that structural adversarial training is a viable path toward more robust neural code intelligence. Future research may explore integrating these structural insights directly into the large-scale pre-training phase of LLMs.
Summary updated