Introduction
While WebAssembly (Wasm) promises a "compile once, run anywhere" future for legacy C/C++ code, the transition is far from seamless. Developers often assume that if a codebase compiles to Wasm, it will behave exactly as it did on a native x86-64 machine. However, this research reveals a critical gap in semantics preservation. Because Wasm operates in a sandboxed environment with its own memory model, virtual file system, and specific compiler toolchains like Emscripten, the resulting binary may diverge from the original source's intended behavior.
The WebAssembly Compilation Challenge
The complexity of the Wasm toolchain is a primary source of these discrepancies. Unlike traditional native compilation, the path to Wasm involves multiple stages of intermediate representation and backend processing, often requiring additional JavaScript glue code to interface with the host environment.
This research addresses two fundamental questions:
- What technical hurdles prevent legacy code from compiling to Wasm?
- Does the Wasm version produce the same outputs as the native version once compiled?
Methods: The WasmChecker Framework
To answer these questions, the researchers developed WasmChecker, an automated framework designed to bridge the gap between native execution and Wasm. The tool operates in two main phases:
- Automated Build Transformation: Using CodeQL for static analysis, WasmChecker scans source code for patterns that typically break Wasm builds—such as exception handling, complex function pointer casting, or specific data types like
long double. It then automatically injects the necessary Emscripten compiler flags to resolve these issues. - Differential Testing: WasmChecker builds the same project into two versions: a native x86-64 binary (using GCC) and a Wasm binary (using Emscripten). It executes the project's existing test suite on both and compares the outputs bit-for-bit. If the native test passes but the Wasm test fails or produces a different result, a "semantics divergence" is flagged.
Experimental Dataset
The study utilized a robust collection of open-source software to ensure broad applicability. The initial dataset comprised 115 CMake-based C/C++ projects from GitHub, which was later supplemented by a validation dataset of 20 additional projects.
In total, the authors analyzed 135 projects containing over 34,000 test cases, spanning utilities, parsers, and complex algorithms.
Key Insights and Results
The researchers identified several recurring themes in semantics failures:
- The "Standard Library" Trap: Wasm binaries often link against different standard libraries (e.g.,
muslfor C) compared to theglibctypically found on Linux. This leads to subtle differences in hash map iteration orders or error codes (errno), breaking logic that expects specific native behaviors. - Performance vs. Reliability: Emscripten’s default settings often prioritize performance by disabling features like exception catching or function pointer type-checking. This can cause silent crashes in codebases that rely on those C++ features.
- The VFS Bottleneck: Wasm's Virtual File System (VFS) is a major source of bugs. For instance, a confirmed bug was found where Emscripten failed to load empty directories, causing directory-traversal logic to fail.
Overall, WasmChecker successfully resolved 57% of identified build issues automatically. Out of 5,862 executable tests, the team found 226 cases of semantics divergence and uncovered 11 new bugs in the Emscripten compiler toolchain, all of which were confirmed by the developers.
Conclusion
WasmChecker demonstrates that cross-compiling legacy code requires more than just a different compiler target; it requires rigorous verification of runtime semantics. While the tool is powerful, it currently cannot automatically rewrite source code for architecture-specific issues like inline assembly. Future work will focus on advanced string analysis to improve file preloading and automated source-to-source translation for non-portable system calls.
Summary updated