Search⌘ K
AI Features

Converting WASM into C

Explore how to convert WebAssembly (WASM) binaries into C source code using the wasm2c tool from the WebAssembly Binary Toolkit (WABT). Gain insights into the autogenerated header and source files, runtime configurations, memory operations, and how functions are implemented in C after conversion. This lesson helps you understand the process of reverse-engineering WASM binaries into manageable C code for debugging and integration.

We'll cover the following...

WABT has a wasm2c converter that converts WASM into C source code and a header.

Let’s create a simple.wat file:

Shell
touch simple.wat

Add the following contents to simple.wat:

C++
(module
(func $uanswer (result i32)
i32.const 22
i32.const 20
i32.add
)
)

wat here defines a uanswer function that adds 22 and 20 to give 42 as the answer. Let's create a WebAssembly binary using wat2wasm:

Shell
/path/to/build/directory/of/wabt/wat2wasm simple.wat -o simple.wasm

This generates the simple.wasm binary. Now, convert the binary into C code using wasm2c:

Shell
(module
(func $uanswer (result i32)
i32.const 22
i32.const 20
i32.add
)
)

This generates simple.c and simple.h.

Note: Both simple.c and simple.h might ...