[Paper Review] STRATA: Simple, Gradient-Free Attacks for Models of Code
STRATA proposes a simple, gradient-free attack for neural models of code by exploiting a strong correlation between token frequency in training data and the L2 norm of learned token embeddings. By replacing local variables with high-L2-norm tokens—identified without gradients—STRATA achieves state-of-the-art adversarial success on code2seq models with minimal computational cost, outperforming gradient-based methods and remaining effective even against robust models.
Neural networks are well-known to be vulnerable to imperceptible perturbations in the input, called adversarial examples, that result in misclassification. Generating adversarial examples for source code poses an additional challenge compared to the domains of images and natural language, because source code perturbations must retain the functional meaning of the code. We identify a striking relationship between token frequency statistics and learned token embeddings: the L2 norm of learned token embeddings increases with the frequency of the token except for the highest-frequnecy tokens. We leverage this relationship to construct a simple and efficient gradient-free method for generating state-of-the-art adversarial examples on models of code. Our method empirically outperforms competing gradient-based methods with less information and less computational effort.
Motivation & Objective
- To investigate whether statistical relationships between token frequency and learned token embeddings can be leveraged for adversarial example generation in code models.
- To develop a computationally efficient, gradient-free attack method that does not require model gradients or extensive search.
- To evaluate the effectiveness of the proposed method on state-of-the-art code summarization models like code2seq.
- To test the transferability and robustness of the attack against models hardened against gradient-based adversarial training.
- To demonstrate that high-L2-norm tokens, correlated with frequency, are more effective for variable replacement attacks.
Proposed method
- The method identifies a strong correlation between token frequency in the training data and the L2 norm of corresponding learned token embeddings across multiple code datasets.
- It selects replacement tokens for local variables based solely on their embedding L2 norm, prioritizing high-L2-norm tokens to maximize adversarial impact.
- The attack is implemented as a variable replacement strategy, replacing local variable identifiers with high-L2-norm tokens from the same vocabulary.
- The approach requires no gradient computation or iterative optimization, enabling fast execution even on CPU-only systems.
- The method is applied in both white-box and black-box settings, leveraging the frequency-L2 norm relationship to generate effective black-box attacks.
- The attack is evaluated on code2seq models trained on Java-small, Java-medium, Java-large, and Python150k datasets.
Experimental results
Research questions
- RQ1Is there a statistically significant relationship between token frequency in code training data and the L2 norm of learned token embeddings?
- RQ2Can this relationship be exploited to generate effective, gradient-free adversarial examples for code models?
- RQ3How does the performance of the proposed STRATA attack compare to gradient-based and search-based adversarial attack methods?
- RQ4Does the STRATA attack remain effective against models that have been adversarially trained to resist gradient-based attacks?
- RQ5Can the attack be effectively applied in a black-box setting using only frequency and embedding information?
Key findings
- A strong, non-linear correlation exists between token frequency and embedding L2 norm: higher-frequency tokens generally have higher L2 norms, except for the very highest-frequency tokens, which show a drop in norm.
- STRATA outperforms competing gradient-based methods on code2seq, achieving higher adversarial success rates with significantly less computational effort.
- On the code2seq/java-large model, STRATA achieved a 38.7% success rate on the test set, outperforming baseline methods and demonstrating high transferability across datasets.
- The method achieved adversarial examples in seconds on a CPU-only machine, while a comparable gradient-based method required multiple hours on the same hardware.
- Even models robust to gradient-based attacks remained vulnerable to STRATA, with F1 scores dropping from 0.367 to 0.240 on adversarial examples generated by STRATA.
- The attack was highly effective in targeted settings: for example, 86.3% success rate when targeting the word 'tournament' on code2seq/java-large.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.