Introduction
Recent advancements in software engineering have been driven by pre-trained programming language (PL) models like CodeT5 and CodeBERT. These models excel at tasks such as automated code summarization, translation, and bug fixing. However, research suggests these models primarily rely on the "natural channel" of code—elements like variable names, function names, and comments designed for human readability—rather than the "formal channel" of logic and syntax used by compilers.
CodeAttack is a novel black-box adversarial attack designed to expose the vulnerabilities inherent in this reliance. By making subtle, human-like modifications to the natural elements of code, CodeAttack can trick state-of-the-art models into producing incorrect outputs or gibberish, while the code remains syntactically correct and seemingly normal to human developers.
Methodology
CodeAttack operates as a black-box attack, requiring no knowledge of the target model's internal weights. The attack follows a two-step process:
- Vulnerability Identification: The algorithm identifies the most influential tokens in a code snippet by systematically "masking" them and measuring the resulting change in the model's output.
- Constraint-Based Substitution: Once vulnerable tokens are identified, CodeAttack uses a masked language model (CodeBERT) to suggest replacements. To maintain "code fluency" and avoid detection, it applies specific constraints:
- Operator Level: Maintains a consistent number of operators to prevent syntax errors.
- Token Level: Ensures replacements are valid identifiers or keywords that match the original coding style.
Experimental Evaluation
The effectiveness of CodeAttack was evaluated across three major tasks and multiple programming languages:
- Code Translation: C# to Java and vice-versa (using Lucene and JGit datasets).
- Code Repair: Automated bug fixing in Java (using the Tufano et al. dataset).
- Code Summarization: Generating natural language descriptions for Python, Java, and PHP (using the CodeSearchNet dataset).
Key Results and Insights
CodeAttack consistently outperformed existing NLP-based adversarial baselines, such as TextFooler and BERT-Attack, in both success rate and the quality of the generated adversarial samples.
Model Fragility and Success Rates
One of the most significant findings is the extreme fragility of current PL models. Modifying as few as 1 to 3 tokens was often sufficient to cause a massive performance drop. CodeAttack achieved success rates of 90-99% in many scenarios. For code summarization, it reduced BLEU scores by nearly 50% across all tested models.
Syntactic Correctness and Human Perception
Unlike standard NLP attacks that often produce syntactically broken code, CodeAttack generates samples that are highly fluent.
As shown in the figure above, CodeAttack maintains significantly higher syntactic correctness across C#, Java, and Python compared to TextFooler and BERT-Attack. In human evaluations, 72.1% of perturbed samples were indistinguishable from original code, with an average understandability score of 4.14 out of 5.
Vulnerability of Graph-Based Models
GraphCodeBERT, which utilizes data-flow graphs, proved particularly susceptible. By targeting identifiers, CodeAttack directly disrupts the data-flow relationships the model relies on. Conversely, CodeT5 showed more resilience, likely due to its "Masked Identifier Prediction" pre-training task, which acts as a form of inherent defense.
Conclusion
CodeAttack demonstrates that current pre-trained PL models are overly dependent on superficial "natural" features of code. To build more robust systems, the research community must move toward adversarial training—incorporating these types of code-specific perturbations during the training phase. Furthermore, future models should integrate deeper structural understanding, such as abstract syntax trees, to move beyond a reliance on variable names and comments.
Summary updated