Augmenting Decompiler Output with Learned Variable Names and Types

Qibin Chen, Jeremy Lacomis, Edward J. Schwartz, Claire Le Goues, Graham Neubig, Bogdan Vasilescu

TL;DR

Reverse engineering compiled binaries is notoriously difficult because compilation strips away human-readable abstractions like variable names and custom types. This paper introduces DIRTY (DecompIled variable ReTYper), a Transformer-based machine learning model that automatically recovers both variable names and complex types (like structs) from decompiled C code, recovering the original developer types 75.8% of the time and significantly improving code readability.

What Problem Does This Paper Address?

Security analysts and software engineers frequently use decompilers (like Hex-Rays or Ghidra) to reverse-engineer binaries when source code is unavailable—whether to analyze malware, find vulnerabilities, or patch legacy systems. Decompilers translate machine code back into a high-level language like C.

However, the compilation process is lossy. Variable names, comments, and custom data types (like structs) are completely erased. While decompilers try to guess types based on memory usage, they often fail. For example, a compiler might add a byte of padding to a 3-byte array for memory alignment. A decompiler might misinterpret this as a 4-byte array, or it might split a single custom struct into multiple disconnected scalar variables. This leaves the reverse engineer staring at a sea of meaningless variables (e.g., v1, v2) with incorrect or overly generic types, making program comprehension incredibly tedious.

Methods

DIRTY tackles this by treating type and name recovery as a sequence-to-sequence translation task using a Transformer-based encoder-decoder architecture.

While applying Transformers to code isn't new, DIRTY introduces two highly novel, task-specific innovations:

  1. Data Layout Encoder: Decompilers provide raw memory layout information (variable size, register/stack location, and internal offsets). Instead of using this as a hard constraint—which would fail when the decompiler inevitably makes a mistake due to padding—DIRTY encodes this layout into a "soft mask." This mask adjusts the probability distribution of the model's type predictions, allowing it to learn when to trust the decompiler's layout and when to ignore it.
  2. Multi-Task Decoder: Variable names and types are deeply correlated (e.g., an integer is often named i, a string str). DIRTY uses a joint multi-task architecture that interleaves the prediction of types and names for each variable. Predicting them together allows the tasks to inform one another, improving the accuracy of both.

Datasets

To train DIRTY, the authors introduce DIRT (Dataset for Idiomatic ReTyping).

  • They mined C repositories from GitHub, compiling over 4.3 million binaries with debug information (DWARF) enabled.
  • They then decompiled these binaries using Hex-Rays and aligned the decompiler-generated variables with the original developer-assigned names and types hidden in the debug symbols.
  • The final dataset contains nearly 1 million unique functions, 368 million code tokens, and a massive library of 48,888 unique types encountered "in the wild."

They also evaluated DIRTY against the existing DIRE dataset (used for variable renaming) and the GNU coreutils benchmark (used by prior type-recovery tools like OSPREY).

Key Insights and Ideas

  • Memory layout is a hint, not a rule: Because decompilers frequently misinterpret padding and alignment, treating memory layout as an absolute constraint breaks type inference. DIRTY's "soft masking" approach elegantly solves a fundamental limitation of static binary analysis.
  • Names and types are a package deal: Jointly predicting a variable's name and type yields better results than predicting either in isolation. A good type prediction narrows down the likely names, and vice versa.
  • Transformers scale beautifully for decompiled code: Prior state-of-the-art models (like DIRE) relied on complex combinations of Abstract Syntax Trees (ASTs) and LSTMs. DIRTY's pure Transformer approach, operating solely on tokenized code, captures long-range dependencies better and trains drastically faster (30 GPU hours vs. 200 GPU hours for DIRE).
  • Decompilers act as an optimization shield: Surprisingly, DIRTY's accuracy barely drops when analyzing heavily optimized code (-O3), even though it was trained on unoptimized code (-O0). This is because the decompiler itself normalizes away many low-level compiler optimizations, presenting a relatively consistent C-level view to the model.

Key Results

  • Type Recovery: DIRTY recovers the exact original developer type 75.8% of the time (compared to just 37.9% for Hex-Rays' built-in heuristics). For complex struct types, it achieves an impressive 68.6% accuracy.
  • Name Recovery: DIRTY achieves 66.4% accuracy on the DIRT dataset, outperforming the previous state-of-the-art DIRE model (57.5%).
  • Zero-Shot Generalization: On functions the model has never seen before, it still predicts the correct type 56.4% of the time.
  • Speed and Competitiveness: On the coreutils benchmark, a tailored version of DIRTY outperformed OSPREY (a heavy-duty probabilistic analysis tool) 80.1% to 71.6%. Furthermore, DIRTY analyzed the entire benchmark in 75 seconds, whereas OSPREY takes about 10 minutes per binary.

Limitations and Future Work

  • Closed Vocabulary: DIRTY can only predict types it saw during training (its library of 48,888 types). It cannot generate entirely novel, unseen struct definitions on the fly. The authors suggest exploring Byte Pair Encoding (BPE) to compose unseen structs in future work.
  • Decompiler Dependence: DIRTY currently relies on the commercial Hex-Rays decompiler. The authors noted that open-source alternatives like Ghidra currently drop the necessary DWARF debug info during their optimization passes, making training data generation on Ghidra infeasible for now.
  • Language Support: The model is designed for C. Extending it to C++ would require handling much richer type systems, virtual function tables, and class inheritance.
  • Input Length: Like many standard Transformers, DIRTY truncates functions longer than 512 tokens (though this only affects ~8.8% of functions in their dataset). Future iterations could use sparse attention models (like BigBird) to handle massive functions.

Summary updated

BibTeX
@inproceedings{chen2022augmenting,
  author = {Qibin Chen and Jeremy Lacomis and Edward J. Schwartz and Claire Le Goues and Graham Neubig and Bogdan Vasilescu},
  title = {{Augmenting Decompiler Output with Learned Variable Names and Types}},
  booktitle = {USENIX Security},
  year = {2022},
  url = {https://openreview.net/forum?id=2Utcq1FZsEm},
}
Figure