Skip to main content
QUICK REVIEW

[论文解读] Embedding Java Classes with code2vec: Improvements from Variable Obfuscation

Rhys Compton, Eibe Frank|Research Commons (University of Waikato)|Apr 6, 2020
Software Engineering Research参考文献 16被引用 19
一句话总结

本文提出一种方法,通过在训练过程中混淆Java类的变量名称,改进code2vec嵌入表示,迫使模型依赖代码结构而非词汇线索。此外,引入一种简单的聚合技术,生成类级别的嵌入表示,从而构建对变量名称变化具有鲁棒性且语义更准确的模型,该方法在一项新的源代码分类基准数据集上得到验证。

ABSTRACT

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.

研究动机与目标

  • 解决code2vec对变量名称的依赖问题,使其对拼写错误和对抗性攻击更具鲁棒性。
  • 将code2vec从单个方法扩展到类级别代码嵌入,以支持如恶意软件检测等任务。
  • 通过减少对变量名称等词汇特征的依赖,提升代码嵌入的语义保真度。
  • 开发并评估一种实用方法,利用聚合的方法表示生成类级别的嵌入表示。
  • 发布数据集、模型和代码,以支持源代码机器学习领域的进一步研究。

提出的方法

  • 在code2vec训练过程中对Java源代码中的变量名称进行混淆,防止模型学习到特定标识符的模式。
  • 在混淆后的代码上训练code2vec模型,使其学习结构和语法特征,而非词汇线索。
  • 使用简单的池化操作(如平均或最大池化)对单个方法嵌入进行聚合,形成整个Java类的单一向量表示。
  • 将生成的类级别嵌入用于下游分类任务,如识别恶意代码。
  • 在包含对抗性变体和混淆变体的新源代码分类基准数据集上评估性能。
  • 发布训练好的模型、数据集和代码,以支持可复现性及源代码嵌入领域的进一步研究。

实验结果

研究问题

  • RQ1在code2vec训练过程中混淆变量名称是否能显著降低模型对词汇特征(如变量名称)的依赖?
  • RQ2能否通过聚合code2vec的细粒度方法嵌入有效构建类级别的嵌入表示?
  • RQ3在对抗性样本和语义相似的代码变体上,混淆模型的性能与原始code2vec模型相比如何?
  • RQ4所生成的嵌入在多大程度上反映了真实的代码语义,而非表面的命名模式?
  • RQ5所提出的方法是否能提升在具有挑战性的源代码数据集上的分类准确率?

主要发现

  • 在训练过程中对变量名称进行混淆,显著降低了模型对词汇特征的依赖,使其对变量名称变化和对抗性扰动具有鲁棒性。
  • 实验结果表明,生成的嵌入更具语义准确性,这在新的源代码分类基准数据集上得到了验证。
  • 使用简单的池化策略聚合方法嵌入,可生成有效的类级别表示,适用于高层次的代码分析任务。
  • 在混淆代码上训练的模型在标准和对抗性测试用例上均优于原始code2vec模型。
  • 发布的数据集、模型和代码为软件工程领域机器学习研究提供了宝贵的资源。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。