TL;DR
This paper introduces STRATA, a novel gradient-free attack method for generating adversarial examples in source code. By exploiting a discovered relationship between token frequency and embedding norms in code models, STRATA efficiently creates perturbations that fool models, outperforming existing gradient-based methods with less computational cost and information.
What Problem Does This Paper Address?
Neural networks are susceptible to adversarial examples – subtle input changes that cause misclassification. While this is a known issue in image and natural language processing, creating adversarial examples for source code is particularly challenging. The key difficulty lies in ensuring that any code modification, while fooling the model, must not alter the code's original functionality. Existing methods for code often rely on gradient-based approaches, which are computationally expensive and require deep knowledge of the model's internal workings. This paper tackles the problem of developing an efficient, gradient-free attack that can generate effective adversarial examples for code models, applicable in both white-box and black-box scenarios.
Methods
The core of STRATA lies in a discovered statistical relationship: the L2 norm of a token's embedding in code models tends to increase with its frequency in the training data, up to a certain point, after which very high-frequency tokens have lower L2 norms. This "sweet spot" of frequency and embedding norm is hypothesized to correlate with a token's impact on the model.
STRATA leverages this by:
- Token Selection: Identifying "high-impact" tokens based on their L2 norm (white-box) or their frequency (black-box approximation).
- Variable Replacement Attack: Replacing a randomly chosen local variable in a code snippet with an adversarial identifier constructed from these high-impact tokens.
- Concatenation Strategies: Using different methods to construct adversarial identifiers, such as repeating a single token multiple times (e.g., "5-same") to maximize the L2 norm of the resulting identifier embedding.
This approach bypasses the need for computationally intensive gradient calculations or extensive search spaces, making it significantly more efficient.
Datasets
The authors evaluated their method on four datasets:
- JAVA-SMALL: 700k Java examples.
- JAVA-MED: 4M Java examples.
- JAVA-LARGE: 16M Java examples.
- PYTHON150K: 150k Python examples.
These datasets were used to train CODE2SEQ models, which were then subjected to STRATA attacks. The JAVA-SMALL dataset was also used for evaluating the effectiveness of different "top-n" parameters for selecting adversarial tokens.
Key Insights and Ideas
- Frequency-Norm Correlation: A striking and consistent relationship exists between token frequency in training data and the L2 norm of their learned embeddings in code models. This correlation is not monotonic; very high-frequency tokens have lower norms.
- "Goldilocks Zone" for Impact: Tokens with moderate frequency and high L2 norms appear to be the most impactful for adversarial attacks, suggesting an optimal "sweet spot" for information content.
- L2 Norm as a Proxy for Impact: The L2 norm of a token's embedding serves as a reliable indicator of how much changing that token will affect a code model's prediction.
- Frequency as a Black-Box Proxy: Token frequency can effectively approximate the L2 norm, enabling efficient black-box adversarial attacks without needing direct access to model parameters or embedding norms.
- Gradient-Free Efficiency: STRATA demonstrates that powerful adversarial attacks can be achieved without the computational burden of gradient calculations, making it highly practical.
Key Results
- Outperforms Gradient-Based Methods: STRATA empirically outperformed competing gradient-based adversarial attack methods in terms of effectiveness (lower F1 scores indicating better attacks) and significantly reduced computational effort.
- High Success Rates: STRATA achieved high success rates in both untargeted and targeted attacks. For instance, using the "5-same" strategy with top-n tokens by L2 norm or frequency, F1 scores were significantly reduced compared to the baseline. For example, on CODE2SEQ/JAVA-SMALL, STRATA with "5-same" and top 1k by L2 norm achieved an F1 score of .316, compared to the baseline of .425.
- Effective Black-Box Attacks: The frequency-based black-box STRATA attacks were nearly as effective as the white-box L2-norm attacks.
- Targeted Attacks Achieved: STRATA successfully generated targeted adversarial examples, demonstrating its versatility.
- Cross-Dataset Transferability: STRATA attacks showed effectiveness even when the attack vocabulary was constructed from a different, but related, dataset, suggesting robustness and transferability.
Limitations and Future Work
- BILSTM Model Limitation: While STRATA was highly effective on CODE2SEQ, its impact on BILSTM models was less pronounced. The authors hypothesize this might be due to CODE2SEQ's method of splitting identifiers into multiple tokens and summing their embeddings, which could amplify the effect of high-L2-norm tokens.
- Uncovered Story: The authors acknowledge that the full story behind the frequency-L2 norm correlation and its impact on model vulnerability is not yet completely understood.
- Future Directions: The paper suggests that the discovered relationship could be useful for defenders in identifying adversarial tokens and for optimizing token thresholding in models to reduce vocabulary size without sacrificing accuracy. They also speculate that this relationship might extend to natural language models.
Summary updated