WebAssembly Linux Binary Format

This past week, I setup a custom binary format on my workstation for WebAssembly. Such formats can be setup without modifying the kernel thru the binfmt_misc capability built into the Linux kernel. This allows specifying an interpreter for files that are neither a native binary format nor a script starting with #!.

In my case, I setup an interpreter for WebAssembly using the wasmtime runtime. To do so, I created a file at /etc/binfmt.d/webasm.conf with the contents

:wasm:M::\x00asm::/usr/bin/wasmtime:

then ran the command cat /etc/binfmt.d/webasm.conf > /proc/sys/fs/binfmt_misc/register, which is where the binfmt_misc filesystem is mounted on my system.

To test this, I opened a terminal in a directory where I am experimenting with compiling crystal to WebAssembly, set the execute bit on the file (main.wasm), then ran ./main.wasm as was greeted with the same output as when I run wasmtime main.wasm, so I consider it a success.

Comments