Is This the Same Code? A Comprehensive Study of Decompilation Techniques for WebAssembly Binaries

Wei-Cheng Wu, Yutian Yan, Hallgrimur David Egilsson, David Park, Steven Chan, Christophe Hauser, Weihang WangarXiv 2024 · DOI

Introduction

WebAssembly (WASM) has rapidly evolved into a critical compilation target for high-performance web applications, IoT ecosystems, and smart contracts. Despite its ubiquity, the low-level, stack-based nature of WASM bytecode presents significant challenges for security auditing and reverse engineering. While the community has long-established decompilers for native architectures like x86, WASM decompilation remains a developing field. This study provides the first comprehensive empirical evaluation of WASM decompilers, focusing on the critical trade-offs between functional correctness, human readability, and structural similarity to the original source.

Methodology

The researchers developed a robust evaluation pipeline designed to treat decompilation as a verifiable round-trip process. As illustrated in Figure 1, the framework consists of four primary stages:

  1. Compilation & Decompilation: Original C source code is compiled into WASM binaries, which are then processed by various decompilers, including WASM-specific tools (wasm2c, w2c2, wasm-decompile) and established native tools (Ghidra, RetDec).
  2. Correctness Testing: To ensure functional equivalence, the decompiled C-like code is re-compiled and executed within a sandboxed environment. The output is then compared against the execution results of the original binary.
  3. Readability Assessment: The study employs standard software engineering metrics such as Lines of Code (LoC), Cyclomatic Complexity, and Halstead Complexity to quantify the effort required for a human to understand the output.
  4. Structural Similarity: The researchers utilized Abstract Syntax Tree (AST) comparisons, specifically Node Quantity Compare (NQC) and Tree Edit Distance (TED), to measure how closely the decompiled structure mirrors the original source code.
Overview of the evaluation framework for WebAssembly decompilers.

Benchmarks and Datasets

The evaluation leveraged three distinct benchmark suites to ensure a broad coverage of code patterns:

  • CSmith (via DecFuzzer): 1,000 synthesized C programs used for rigorous correctness testing.
  • PolyBenchC: A set of numerical kernels used to evaluate readability and structural similarity in real-world computational contexts.
  • CHStone: A high-level synthesis suite used to test the decompilers' ability to handle complex program logic.

Key Insights: The Correctness-Readability Trade-off

The study reveals a stark "wall" between correctness and readability. Tools like wasm2c prioritize functional accuracy by performing a near-direct translation of WASM instructions into C. While this ensures the code runs correctly, it produces a "flattened" output filled with goto statements and pointer arithmetic that is virtually unreadable for humans.

Conversely, tools like wasm-decompile attempt to lift the code into a more readable Static Single Assignment (SSA) format. However, this lifting process is error-prone; the tool frequently fails to faithfully represent the WASM stack workflow, leading to logic errors in approximately 35% of cases.

Furthermore, the study highlights the difficulty of "re-materializing" high-level data structures. Because WASM views memory as a single linear blob, decompilers often struggle to distinguish between independent variables and arrays, frequently grouping them into arbitrary, incorrect structures.

Experimental Results

The quantitative results underscore the current limitations of WASM-specific tooling. As shown in Figure 6, wasm2c was the only tool to achieve a 100% success rate in functional correctness. In contrast, wasm-decompile and native tools like Ghidra and RetDec exhibited significant execution discrepancies.

Correctness results across different decompilation tools.

Beyond correctness, the study found massive code bloat. A source file of ~2,600 lines can balloon to over 72,000 lines when processed by wasm2c. Interestingly, native decompilers like Ghidra often produced more compact and structurally similar code (NQC scores of 0.4-0.5) than dedicated WASM tools (NQC scores of 0.04-0.2), suggesting that the mature data-flow heuristics of native reverse engineering tools are highly effective even for WASM.

Conclusion and Future Directions

Current WASM decompilers face a significant gap between producing executable code and producing understandable code. While wasm2c provides a reliable baseline for correctness, its output is unsuitable for manual auditing. Future development should focus on moving beyond simple instruction translation toward the heuristic-driven pattern matching and advanced data-flow analysis seen in tools like Ghidra. Additionally, expanding the evaluation to include languages like Rust and Go is essential as WASM's ecosystem continues to diversify.

Summary updated

BibTeX
@misc{wu2024is,
  author = {Wei-Cheng Wu and Yutian Yan and Hallgrimur David Egilsson and David Park and Steven Chan and Christophe Hauser and Weihang Wang},
  title = {{Is This the Same Code? A Comprehensive Study of Decompilation Techniques for WebAssembly Binaries}},
  year = {2024},
  eprint = {2411.02278},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2411.02278},
}
Figure