WABT: A wonderful CLI for manipulating Wasm

Reading Time: 4 minutes

As we know, WebAssembly is a binary instruction format that is not understandable by us. So to manipulate the WebAssembly files we have a set of tools called wabt. Let us explore it more and learn how to use it.

Introduction

WABT is an abbreviation for WebAssembly Binary Toolkit. It has a set of command-line tools that helps us to manipulate WebAssembly files like generating wat file from wasm file or creating wasm file from wat file and much more. The tools which we will see in this blog are –

  • wat2wam
  • wasm-objdump
  • wasm2wat
  • wasm2c
  • wasm decompile
  • wasm-interp

So let us begin by installing wabt.

Installing WABT

Installing wabt is quite simple. First of all, you need CMake to build wabt. Follow the steps to install CMake

  1. Visit the official CMake website and move to the download section
  2. For Linux download .sh file and run it using /bin/sh
  3. For windows download the .msi file and run it

Now install the wabt

  1. Clone the GitHub repo https://github.com/WebAssembly/wabt
  2. Follow the instruction in the readme file.
  3. Now you will see some files in the bin folder

We need these files for our work.

wat2wasm

wat2wasm is a command-line tool that converts a wat file to a wasm file for you. It is a very useful tool and helps you to create wasm easily from wat files. Let us see an example.

Create a file named mul.wat and copy the following content.

;; mul.wat

(module
	(func $mul (param $num1 i32) (param $num2 i32) (result i32)
		(i32.mul
			(get_local $num1)
			(get_local $num2)
		)
	)
	(export "mul" (func $mul))
)

Now open the terminal and run the command

$ wat2wasm mul.wat -o mul.wasm

You will see no output in the terminal but it will create a file name mul.wasm. This is the wasm file created from the wat file for multiplication which we wrote earlier.

wasm-objdump

This is another useful command-line tool that allows us to dump some information about the wasm file in the terminal. Try this command for the newly created mul.wasm. Run the following command.

$ wasm-objdump mul.wasm -x

The wasm-objdump binary can be found in the bin folder of the wabt directory and can be executed using bin/wasm-objdump.

You will see something like this after you execute the command.

It tells that your file contains a function type that takes two integer arguments and returns an integer. So this is how it works.

wasm2wat

wasm2wat is the tool using which we can generate a wat representation of a wasm file. So let us try this on the newly created mul.wasm file. Run the following command

$ wasm2wat mul.wasm -o mul2.wat

It will generate a wat file named mul2.wat which will contain

(module
  (type (;0;) (func (param i32 i32) (result i32)))
  (func (;0;) (type 0) (param i32 i32) (result i32)
    local.get 0
    local.get 1
    i32.mul)
  (export "mul" (func 0))
)

It looks similar to our previous wat file but this function does not have a name. That do not make any significant difference.

wasm2c

This command-line tool converts a WebAssembly binary file to a C source and header. Let us try it on our mul.wasm file. Run the command.

$ wasm2c mul.wasm -o mulC.c

It will create a file like this.

/* Automically generated by wasm2c */
#include <math.h>
#include <string.h>

#include "mulC.h"
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#define LIKELY(x) __builtin_expect(!!(x), 1)

#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0)

#define FUNC_PROLOGUE                                            \
  if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \
    TRAP(EXHAUSTION)

#define FUNC_EPILOGUE --wasm_rt_call_stack_depth

#define UNREACHABLE TRAP(UNREACHABLE)
...

This will create a long mulC.c file along with its header file which does the same task as the wasm file.

wasm-decompile

wasm-decompile converts a wasm binary into readable C-like syntax. It may look similar to wasm2c but it is not. It only makes a file having a C-like syntax that is more readable but it is not a C file. Let us understand it with an example. Run the command –

$ wasm-decompile mul.wasm -o readable

This command generates a file named readable which contains the following code –

export function mul(a:int, b:int):int {
  return a * b
}

It looks similar to C but it is not a C code. This is a C like representation of a wasm file.

wasm-interp

wasm-interp command lets you to decode and run a WebAssembly binary file using a stack-based interpreter. The commands which you can run for your test files are –

1.Parse binary file test.wasm, and type-check it

$ wasm-interp test.wasm

2. Parse test.wasm and run all its exported functions

$ wasm-interp test.wasm --run-all-exports

3. Parse test.wasm, run the exported functions and trace the output

$ wasm-interp test.wasm --run-all-exports --trace


So this was all for this blog. There are many more wabt tools that you can explore.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.


Knoldus-blog-footer-image

1 thought on “WABT: A wonderful CLI for manipulating Wasm5 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading