Introduction
Deep learning models for source code, such as code2seq, are increasingly deployed for critical tasks like code summarization and vulnerability detection. However, these models are susceptible to adversarial attacks—small, semantics-preserving modifications that drastically alter model predictions. Traditional attacks often rely on computationally expensive gradient-based optimization or exhaustive searches, which are often impractical in real-world black-box scenarios.
The "Goldilocks Zone" of Token Frequency
The core innovation of the Simple Trained Token Attack (STRATA) is the discovery of a statistical "shortcut." The authors identified a consistent relationship between a token's training frequency and the L2 norm (magnitude) of its learned embedding.
As shown in the figure above, there is a distinct "hump" in embedding magnitude. Low-frequency tokens lack sufficient data to develop large norms, while extremely high-frequency tokens (like language keywords) carry little specific semantic weight. The "Goldilocks" tokens—those that are frequent but not ubiquitous—possess the highest L2 norms and, consequently, the greatest influence on a model's internal state.
The STRATA Attack Strategy
STRATA leverages these high-impact tokens through a simple variable renaming strategy:
- Token Selection: In a white-box setting, tokens with the highest L2 norms are selected from the model's embedding matrix. In a black-box setting, high-frequency tokens from the training data serve as a proxy.
- Concatenation: To amplify the effect, a single high-impact token is concatenated five times (the "5-same" strategy, e.g.,
token_token_token_token_token). - Replacement: A random local variable in the target code is replaced with this compound identifier.
Because models like code2seq represent identifiers by summing constituent token embeddings, this repetition effectively "overwhelms" the model's logic via the triangle inequality. Unlike previous state-of-the-art attacks that required hours of GPU time, STRATA can generate effective attacks in seconds on a standard CPU.
Key Results and Performance
STRATA achieves state-of-the-art performance with minimal computational cost. On the java-small dataset, it reduced the model's F1 score from 0.425 to 0.316, outperforming complex gradient-based methods.
The experimental results demonstrate that ranking tokens by L2 norm or frequency is significantly more effective than random selection for degrading model performance. Furthermore, STRATA excels at targeted attacks, forcing specific incorrect predictions with high success rates (e.g., forcing a prediction of tournament with 86.3% success).
Conclusion
STRATA demonstrates that effective adversarial attacks do not always require complex optimization. By exploiting the inherent statistical properties of learned embeddings, attackers can generate powerful perturbations in seconds. These findings highlight a critical vulnerability in current models of code and suggest that defenders should monitor for suspicious patterns of high-frequency tokens.
Summary updated