[Paper Review] Embedding Java Classes with code2vec: Improvements from Variable Obfuscation
This paper proposes a method to improve code2vec embeddings for Java classes by obfuscating variable names during training, forcing the model to rely on code structure rather than lexical cues. It further introduces a simple aggregation technique to generate class-level embeddings, resulting in models that are robust to variable name variations and more semantically accurate, as validated on a new benchmark dataset for source code classification.
Automatic source code analysis in key areas of software engineering, such as code security, can benefit from Machine Learning (ML). However, many standard ML approaches require a numeric representation of data and cannot be applied directly to source code. Thus, to enable ML, we need to embed source code into numeric feature vectors while maintaining the semantics of the code as much as possible. code2vec is a recently released embedding approach that uses the proxy task of method name prediction to map Java methods to feature vectors. However, experimentation with code2vec shows that it learns to rely on variable names for prediction, causing it to be easily fooled by typos or adversarial attacks. Moreover, it is only able to embed individual Java methods and cannot embed an entire collection of methods such as those present in a typical Java class, making it difficult to perform predictions at the class level (e.g., for the identification of malicious Java classes). Both shortcomings are addressed in the research presented in this paper. We investigate the effect of obfuscating variable names during the training of a code2vec model to force it to rely on the structure of the code rather than specific names and consider a simple approach to creating class-level embeddings by aggregating sets of method embeddings. Our results, obtained on a challenging new collection of source-code classification problems, indicate that obfuscating variable names produces an embedding model that is both impervious to variable naming and more accurately reflects code semantics. The datasets, models, and code are shared for further ML research on source code.
Motivation & Objective
- To address code2vec's reliance on variable names, which makes it vulnerable to typos and adversarial attacks.
- To extend code2vec beyond individual methods to enable class-level code embeddings for tasks like malware detection.
- To improve semantic fidelity of code embeddings by reducing dependence on lexical features such as variable names.
- To develop and evaluate a practical method for generating class-level embeddings using aggregated method representations.
- To release datasets, models, and code to support further research in machine learning for source code.
Proposed method
- Obfuscate variable names in Java source code during code2vec training to prevent the model from learning patterns based on specific identifiers.
- Train a code2vec model on obfuscated code to learn structural and syntactic features instead of lexical cues.
- Aggregate individual method embeddings using simple pooling operations (e.g., mean or max) to form a single vector representation for an entire Java class.
- Use the resulting class-level embeddings for downstream classification tasks, such as identifying malicious code.
- Evaluate performance on a new benchmark dataset of source code classification problems with adversarial and obfuscated variants.
- Share trained models, datasets, and code to enable reproducibility and further research in code embedding.
Experimental results
Research questions
- RQ1Does obfuscating variable names during code2vec training reduce model reliance on lexical features like variable names?
- RQ2Can class-level embeddings be effectively constructed by aggregating method-level embeddings from code2vec?
- RQ3How does the performance of the obfuscated model compare to the original code2vec model on adversarial and semantically similar code variants?
- RQ4To what extent do the resulting embeddings reflect true code semantics rather than superficial naming patterns?
- RQ5Can the proposed method improve classification accuracy on challenging source code datasets?
Key findings
- Obfuscating variable names during training significantly reduces the model's dependence on lexical features, making it robust to variable name variations and adversarial perturbations.
- The resulting embeddings are more semantically accurate, as evidenced by improved performance on a new benchmark dataset of source code classification tasks.
- Aggregating method embeddings using simple pooling strategies produces effective class-level representations suitable for high-level code analysis.
- The model trained on obfuscated code outperforms the original code2vec model on both standard and adversarial test cases.
- The released datasets, models, and code provide a valuable resource for future research in machine learning for software engineering.
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.