jTrans: jump-aware transformer for binary code similarity detection

Hao Wang, Wenjie Qu, Gilad Katz, Wenyu Zhu, Zeyu Gao, Han Qiu, Jianwei Zhuge, Chao Zhang

Abstract

Binary Code Similarity Detection (BCSD) is a fundamental task in software security, yet existing approaches often struggle to balance the fine-grained semantics of assembly instructions with the structural logic of a program's control flow. This paper introduces jTrans, the first Transformer-based model to integrate control-flow information directly into its architecture. By employing a novel jump-aware representation and a specialized Jump Target Prediction (JTP) pre-training task, jTrans bridges the gap between natural language processing (NLP) techniques and graph-based analysis. Experimental results on the massive BinaryCorp dataset demonstrate that jTrans outperforms state-of-the-art methods by over 30% in large-scale searches and doubles the recall rate in real-world vulnerability detection.

1. Introduction

Binary Code Similarity Detection (BCSD) involves determining whether two compiled functions originate from the same source code, even when they are produced by different compilers or optimization settings. This capability is essential for identifying known vulnerabilities in proprietary software, analyzing malware variants, and detecting software plagiarism.

Historically, the field has been split between two paradigms. NLP-based models treat assembly code as a linear sequence of tokens, capturing instruction-level nuances but often ignoring the non-linear "jumps" that define program logic. Conversely, Graph Neural Networks (GNNs) focus on the Control Flow Graph (CFG) but suffer from high computational costs and a relative inability to capture the semantic richness of individual instructions. jTrans aims to unify these approaches by embedding structural control-flow information directly into the Transformer’s self-attention mechanism.

2. Methodology

jTrans is built upon the Transformer architecture (specifically BERT), modified to handle the unique properties of binary code. It introduces two primary innovations to move beyond linear sequence modeling.

2.1 Jump-Aware Representation

In a standard Transformer, tokens are associated with their linear position in a file. jTrans modifies this by sharing parameters between a jump instruction’s token embedding and the positional encoding of its destination. This architectural tweak informs the model that two instructions, though physically distant in the binary, are logically adjacent. By doing so, the self-attention mechanism can naturally learn to follow the "edges" of the program's control flow without the overhead of a separate GNN.

2.2 Jump Target Prediction (JTP)

To ensure the model understands program logic, the authors introduced a novel pre-training task called Jump Target Prediction (JTP). In addition to the standard Masked Language Model (MLM) task—where the model predicts hidden tokens—JTP requires the model to predict the correct destination for a jump instruction with a missing target. This forces the model to internalize the program's execution flow during the pre-training phase.

2.3 Contrastive Learning

For the downstream similarity task, jTrans utilizes a Siamese network approach with contrastive learning. This ensures that semantically identical functions are mapped to nearby points in a high-dimensional vector space, while dissimilar functions are pushed apart, facilitating efficient similarity searches.

3. The BinaryCorp Dataset

To evaluate jTrans, the researchers developed BinaryCorp, a benchmark that significantly exceeds previous datasets in scale and diversity. While older datasets like GNUtils contained roughly 160,000 functions, BinaryCorp-26M contains approximately 26 million functions. The dataset was compiled from the ArchLinux official and user repositories (AUR), capturing a vast array of real-world coding styles. It includes binaries compiled with multiple compilers (GCC, Clang) across five different optimization levels (-O0, -O1, -O2, -O3, -Os).

4. Experimental Results

4.1 State-of-the-Art Comparison

jTrans demonstrates significant performance gains over previous leaders like OrderMatters and SAFE. On the BinaryCorp-3M dataset with a challenging pool size of 10,000 candidate functions, jTrans achieved a Recall@1 of 62.5%. In comparison, the previous state-of-the-art, OrderMatters, reached only 32.0%.

4.2 Robustness to Scale

A critical insight from the study is the "pool size trap." Many previous models performed well when searching through small pools (e.g., 32 functions) but saw their performance collapse as the search space grew. jTrans remains remarkably robust as the pool size increases to 10,000, making it far more suitable for real-world applications where a single library might contain thousands of functions.

4.3 Vulnerability Hunting

In practical security applications, jTrans was tested against eight known CVEs (Common Vulnerabilities and Exposures). The model achieved a recall rate twice as high as leading baselines like SAFE and Asm2Vec, demonstrating its efficacy in identifying vulnerable code patterns across different compiled versions.

5. Discussion and Limitations

While jTrans represents a significant leap forward, several challenges remain:

  • Architecture Specificity: The current model is optimized for x86 architectures. While the methodology is cross-architecture in theory, the model requires re-training for ARM or MIPS.
  • Indirect Jumps: The jump-aware design currently focuses on direct jumps. Indirect jumps, where the destination is calculated at runtime (e.g., via a register), remain difficult for the model to resolve statically.
  • Inter-function Context: Currently, jTrans embeds individual functions in isolation. Future work could explore how functions interact within a larger program to provide a more holistic understanding of software behavior.

6. Conclusion

jTrans proves that Transformers can be successfully adapted for structural program analysis by integrating control-flow information into the attention mechanism. By combining the semantic power of Transformers with the logical structure of CFGs, jTrans sets a new standard for Binary Code Similarity Detection, offering a scalable and highly accurate tool for the security community.

Summary updated

BibTeX
@inproceedings{wang2022jtrans,
  author = {Hao Wang and Wenjie Qu and Gilad Katz and Wenyu Zhu and Zeyu Gao and Han Qiu and Jianwei Zhuge and Chao Zhang},
  title = {{jTrans: jump-aware transformer for binary code similarity detection}},
  booktitle = {ISSTA},
  year = {2022},
  doi = {10.1145/3533767.3534367},
}
Figure