Exploring Software Naturalness through Neural Language Models

Luca Buratti, Saurabh Pujar, Mihaela A. Bornea, J. Scott McCarley, Yunhui Zheng, Gaetano Rossiello, Alessandro Morari, Jim Laredo, Veronika Thost, Yufan Zhuang, Giacomo DomeniconiarXiv 2020

TL;DR

This paper explores the "Software Naturalness" hypothesis, suggesting programming languages can be analyzed like natural languages. They adapt transformer-based language models (like BERT) to directly process raw source code for tasks like Abstract Syntax Tree (AST) analysis and vulnerability detection, achieving results comparable to traditional compiler-dependent methods.

What Problem Does This Paper Address?

Traditional code analysis techniques often rely on complex, compiler-generated structural information like Abstract Syntax Trees (ASTs), Control Flow Graphs (CFGs), and Data Flow Graphs (DFGs). Extracting this information can be computationally expensive and requires the entire project to be available and compilable, making it impractical for scenarios like analyzing code changes in a pull request. The paper investigates whether modern neural language models, trained directly on raw source code, can automatically learn these structural features and perform code analysis tasks effectively, bridging the gap between natural language processing (NLP) and programming language (PL) research.

Methods

The core of this work is the development and evaluation of C-BERT, a transformer-based language model pre-trained from scratch on a large corpus of C code. Key methodological contributions include:

  • Software Naturalness Hypothesis Exploration: Applying NLP techniques (transformer models) to PL tasks.
  • AST Feature Discovery: Introducing a novel sequence labeling task to directly probe the model's understanding of AST structure. This task aims to predict AST node types (token_kind) and their syntactic roles (cursor_kind) from raw code.
  • Vulnerability Identification (VI): Fine-tuning C-BERT for a binary classification task to identify software vulnerabilities.
  • Tokenizer Strategies: Investigating three tokenization approaches (Character, Character+Keyword, and SentencePiece) to balance vocabulary size and context window, addressing the Out-Of-Vocabulary (OOV) problem common in PL.
  • Whole Word Masking (WWM): Proposing a more challenging pre-training objective than standard Masked Language Model (MLM) for transformer models, particularly beneficial for code where variable and function names can be long and complex.
  • Aggregation for Long Sequences: For very long code functions that exceed the model's fixed context window, they employ a strategy of breaking the input into segments and using a BiLSTM layer to combine their outputs before classification.

Datasets

  • Pre-training Corpus: The top-100 starred GitHub C language repositories (approx. 5.8 GB of code), excluding forks and clones. Comments were removed.
  • AST Node Tagging Datasets: Two datasets were created from the FFmpeg and QEMU GitHub repositories. These datasets were annotated using Clang's tokenization and parsing capabilities to provide gold labels for token_kind and cursor_kind.
  • Vulnerability Identification Datasets: The paper uses datasets derived from the FFmpeg and QEMU projects, labeled for vulnerability (non-vulnerable/vulnerable) as released by the Devign paper. They also created a combined dataset. Reduced versions of these datasets were created to address compilation issues with the GGNN baseline.

Key Insights and Ideas

  • Programming Languages are "Natural": The results strongly support the "Software Naturalness" hypothesis, demonstrating that language models can learn complex syntactic and semantic properties of code without explicit structural features.
  • AST Features Learned Automatically: C-BERT can effectively learn to predict AST node types and roles directly from raw source code, bypassing the need for compiler-generated ASTs for certain tasks.
  • Raw Code is Sufficient: Transformer models can achieve competitive results in vulnerability identification using only raw source code, avoiding the costly compilation and feature extraction steps of graph-based methods.
  • Tokenizer Choice Matters (but less than expected): While different tokenizers showed some performance variation, the impact was less pronounced for the transformer models than for simpler baselines, suggesting the models are robust to tokenization choices. Character-based tokenization with WWM proved particularly effective.
  • WWM is Crucial for Code: The Whole Word Masking pre-training objective significantly improved performance, especially for character-based tokenization, by forcing the model to learn more complex dependencies within code structures.

Key Results

  • AST Node Tagging: C-BERT models achieved very high accuracy (>97%) and F1 scores (>98%) on the cursor_kind task for both FFmpeg and QEMU datasets, significantly outperforming a BiLSTM baseline.
  • Vulnerability Identification: C-BERT models, especially with BiLSTM aggregation and WWM pre-training, achieved accuracy scores comparable to, and in some cases exceeding, the strongest graph-based baseline (GGNN). For example, the CWB model (Char tokenizer, WWM, Truncate aggregation) achieved 65.8% accuracy on the QEMU full dataset, compared to GGNN's 65.8%. The CWB model also achieved the best results on 4 out of 6 datasets.
  • Tokenizer Performance: For MLM pre-training, SentencePiece (SPE) generally performed best, followed by KeyChar, then Char. However, with WWM pre-training, Char tokenization (CWB) became the best performing model on most datasets.

Limitations and Future Work

  • Limitations:
    • The paper notes that for very long functions, they had to resort to aggregation techniques (breaking into segments and using BiLSTM), which might not be as effective as processing the entire sequence.
  • While WWM improved performance for Char and KeyChar, it did not improve SPE tokenization.
  • The GGNN baseline had to be trained on reduced datasets due to compilation errors with Joern.
  • Future Work:
    • The authors suggest joint learning of AST and VI tasks on top of the language model to further improve code analysis.
  • They propose exploring ways to further improve code analysis without using the compiler to extract structured information.
  • Further investigation is needed to understand why WWM pre-training improved VI performance but hurt AST tagging performance.

Summary updated

BibTeX
@misc{buratti2020exploring,
  author = {Luca Buratti and Saurabh Pujar and Mihaela A. Bornea and J. Scott McCarley and Yunhui Zheng and Gaetano Rossiello and Alessandro Morari and Jim Laredo and Veronika Thost and Yufan Zhuang and Giacomo Domeniconi},
  title = {{Exploring Software Naturalness through Neural Language Models}},
  year = {2020},
  eprint = {2006.12641},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2006.12641},
}
Figure