/wams

Set initial memory in wasm binaries

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

wams

wams ("WebAssembly memory setter") is a command line utility written in Go that sets the initial number of memory pages of a WebAssembly (aka .wasm) binary file.

Background

Since version Go version 1.11 it is possible to compile go sources to WebAssembly. This file contains bytecode that can be executed by browsers and by Node.js and other tools.

The initial results are both impressive and exciting but there are some pitfalls to be aware of. One major issue is the memory requirement of the .wasm file that the Go toolchain generates. The generated memory section requires the WebAssembly runtime to allocate 16384 64KB pages, that is 1GB of memory. Most of this memory is not used during execution but it seems as if a considerable amount is required for the initialization of the Go runtime. As a result, .wasm files generated by the Go compiler did not run on any of the Android devices we tested. They did run on desktops and iOS devices though.

wams allows to set the number of 64kB pages in the memory section of a .wasm file.

Usage

  • wams file.wasm

    • displays the initial memory size in 64KB pages and the file offset of the memory section in file.wasm
  • wams -pages 2048 -write file.wasm

    • patches the memory section in file.wasm to 2048 64KB pages

Notes

As of today (September 2018), reducing the number of pages did not suffice to run Go generated .wasm files on Android. The tested devices could allocate about 7500 pages but about 11000 pages were required to instantiate a small Go generated .wasm binary. However, the tool might be of use for other toochains when experimenting with initial page sizes.

Resouces