Skip to main content
QUICK REVIEW

[Paper Review] Using Distributed Representation of Code for Bug Detection

Jón Arnar Briem, Jordi Smit|arXiv (Cornell University)|Nov 28, 2019
Software Engineering ResearchComputer Science15 references5 citations
TL;DR

This paper repurposes the Code2Vec model, originally designed for method name prediction, to detect off-by-one errors in Java code by retraining its final layer for binary classification. Using AST path embeddings with attention mechanisms on a corpus of mutated code, the model achieves promising generalization on off-by-one bugs, though it suffers from dataset bias and false positives on non-standard code patterns.

ABSTRACT

Recent advances in neural modeling for bug detection have been very promising. More specifically, using snippets of code to create continuous vectors or extit{embeddings} has been shown to be very good at method name prediction and claimed to be efficient at other tasks, such as bug detection. However, to this end, the method has not been empirically tested for the latter. In this work, we use the Code2Vec model of Alon et al. to evaluate it for detecting off-by-one errors in Java source code. We define bug detection as a binary classification problem and train our model on a large Java file corpus containing likely correct code. In order to properly classify incorrect code, the model needs to be trained on false examples as well. To achieve this, we create likely incorrect code by making simple mutations to the original corpus. Our quantitative and qualitative evaluations show that an attention-based model that uses a structural representation of code can be indeed successfully used for other tasks than method naming.

Motivation & Objective

  • To evaluate whether Code2Vec, a state-of-the-art code embedding model, can be effectively repurposed for off-by-one bug detection beyond its original method naming task.
  • To investigate the feasibility of using attention-based AST path representations for identifying semantic bugs in code.
  • To address the lack of empirical evaluation of code embedding models on real-world bug detection tasks.
  • To explore the impact of data augmentation via mutation on model generalization and robustness.
  • To identify limitations in model performance related to dataset bias, code style deviations, and scope constraints.

Proposed method

  • The Code2Vec model is fine-tuned by replacing its final layer with a binary classification head to detect off-by-one errors.
  • The model uses path-based attention mechanisms to learn vector representations from Abstract Syntax Tree (AST) paths in Java code.
  • A large corpus of likely correct Java code is used as a base, with off-by-one bugs introduced via syntactic mutations (e.g., replacing '<' with '<=') to generate negative examples.
  • The model is trained on a mixed dataset of original (correct) and mutated (incorrect) code samples to enable binary classification.
  • Model performance is evaluated quantitatively using standard metrics and qualitatively through case studies of detected bugs.
  • Transfer learning is explored by initializing the model with pre-trained weights from the original Code2Vec task, though results show minimal improvement over random initialization.

Experimental results

Research questions

  • RQ1Can the Code2Vec model, trained for method name prediction, be effectively repurposed for off-by-one bug detection?
  • RQ2How well does the attention-based AST path representation generalize to detecting logic errors beyond naming tasks?
  • RQ3To what extent does data augmentation via code mutation improve model detection performance and generalization?
  • RQ4What are the primary failure modes of the model, particularly regarding code style bias and context limitations?
  • RQ5Does transfer learning from the pre-trained Code2Vec model significantly improve detection performance on off-by-one errors?

Key findings

  • The model achieves promising detection performance on off-by-one errors, demonstrating that AST path embeddings with attention can generalize to bug detection beyond method naming.
  • Transfer learning from the pre-trained Code2Vec model provides only a slight improvement over random initialization and does not significantly accelerate training.
  • The model exhibits a strong bias toward standard coding patterns, generating false positives for non-conventional code styles, especially in for-loops using '<='.
  • The model's performance is negatively impacted by an imbalanced dataset, with significantly fewer examples of '>=', '<=' compared to '<', '>'.
  • The model's scope is limited to a single method's AST, potentially missing context from called methods, which may lead to unreliable predictions in complex code.
  • The model's false positives are largely attributable to deviations from common coding conventions, suggesting that style generalization remains a key challenge.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.