DIRECT : A Transformer-based Model for Decompiled Identifier Renaming

Vikram Nitin, Anthony Saieva, Baishakhi Ray, Gail E. Kaiser2021

Abstract

Decompiling binary executables to high-level code is an important step in reverse engineering scenarios, such as malware analysis and legacy code maintenance. However, the generated high-level code is difficult to understand since the original variable names are lost. In this paper, we leverage transformer models to reconstruct the original variable names from decompiled code. Inherent differences between code and natural language present certain challenges in applying conventional transformer-based architectures to variable name recovery. We propose DIRECT, a novel transformer-based architecture customized specifically for the task at hand. We evaluate our model on a dataset of decompiled functions and find that DIRECT outperforms the previous state-of-the-art model by up to 20%. We also present ablation studies evaluating the impact of each of our modifications. We make the source code of DIRECT available to encourage reproducible research.

Introduction

Reverse engineering is a critical task for software analysts, particularly when dealing with malware or legacy systems where source code is unavailable. Decompilers like Hex-Rays or Ghidra can translate binary executables back into high-level C code, but they cannot recover original variable names. Instead, they produce generic placeholders (e.g., v1, a2), resulting in an "alphabet soup" that obscures the code's logic and intent.

Previous attempts to automate variable renaming used LSTMs or Gated Graph Neural Networks (GGNNs), but these models often failed to capture long-range dependencies within complex functions. To address this, researchers at Columbia University developed DIRECT (Decompiled Identifier Renaming Engine using Contextual Transformers), a model specifically architected to handle the unique syntactic and structural constraints of programming languages.

Methods

DIRECT adapts the Transformer architecture for code through several key innovations designed to ensure syntactic consistency and handle long-range context.

Overview of the DIRECT architecture including the encoder-decoder setup and the handling of long sequences.

1. Encoder-Decoder Architecture

Unlike standard BERT models that predict a fixed number of tokens, DIRECT utilizes an encoder-decoder setup. This allows the model to predict variable names composed of multiple subtokens (e.g., my_var as my, _, and var), providing more flexibility than simple classification.

2. Joint Probability and Consistency

In programming, a variable must retain the same name throughout its scope. Standard transformers predict tokens independently, which could lead to inconsistent naming. DIRECT employs a specialized beam search algorithm to calculate joint probabilities, ensuring that a variable like v1 is renamed consistently across the entire function.

3. Identifier Token Coefficient (ITC)

Code contains significant amounts of "boilerplate" syntax (e.g., if, while, {). To focus the model on the most semantically meaningful parts, the authors introduced a custom loss function. The Identifier Token Coefficient (ITC) weights the importance of variable names 50 times higher than standard syntax tokens during training, forcing the model to prioritize identifier recovery.

4. Splitting and Merging for Long Sequences

To overcome the 512-token limit of standard Transformers, DIRECT breaks long functions into chunks. After processing, it merges predictions by prioritizing the "first occurrence" of a variable—typically found in function signatures where context is richest.

Evaluation

The model was evaluated using the DIRE dataset, comprising over 1 million C functions scraped from GitHub. This dataset provides a parallel corpus of generic decompiled code and the original "ground truth" source code.

Key Results

DIRECT demonstrates a significant performance leap over the previous state-of-the-art model, DIRE.

Performance comparison between DIRECT and the previous state-of-the-art model, DIRE.

As shown in the table above, DIRECT achieved an Exact Match Accuracy of 42.8%, representing a 20% relative improvement over DIRE. When considering the top five predictions, the accuracy increases to 49.3%. Ablation studies confirmed that the encoder-decoder architecture was essential; a decoder-only model achieved only 19.6% accuracy, highlighting the importance of bidirectional context.

Discussion and Conclusion

The success of DIRECT underscores that code is not merely another form of natural language. Its "strict syntax" requires specialized architectural considerations, such as the consistency algorithm and ITC weighting. Qualitative analysis revealed that the model heavily weights the function header (return types and function names) when predicting variable names, suggesting it learns to infer purpose from the function's high-level "contract."

While DIRECT sets a new benchmark, challenges remain. Accuracy declines for functions exceeding 500 tokens, even with splitting techniques. Future work may involve integrating Abstract Syntax Trees (ASTs) directly into the Transformer architecture to better capture structural relationships. Beyond renaming, this approach holds promise for other reverse engineering tasks, such as automated documentation generation and function name recovery.

Summary updated

BibTeX
@misc{nitin2021direct,
  author = {Vikram Nitin and Anthony Saieva and Baishakhi Ray and Gail E. Kaiser},
  title = {{DIRECT : A Transformer-based Model for Decompiled Identifier Renaming}},
  year = {2021},
  doi = {10.18653/v1/2021.nlp4prog-1.6},
}
Figure