zydis-wasm

example project with zydis targetting wasm

Stars
2

zydis-rs to WebAssembly

Demo of using zydis-rs in a Rust project that targets WebAssembly. Naively, this is a little tricky to do, because although Rust supports WebAssembly, the underlying Zydis library is built from C. So, we use a C/C++ cross compiler that can emit and link WebAssembly objects.

Specifically: This is a Rust library that exposes a single function disassemble(Vec<u8>) -> String. We use wasm-pack to invoke the compiler to build the library for wasm32. Notably, we override CC and CXX to use zig rather than clang, so that we build the underyling zydis C library for wasm (the original idea is described here and here). Next, wasm-bindgen creates Javascript wrappers that converts to and from Rust types. Finally, a simple HTML page loads the .wasm file and wrappers and invokes the disassemble routine.

prerequisites

build

First, update the path to zig in zcc:

#!/bin/sh

/home/user/.bin/zig-linux-x86_64-0.8.1/zig cc -target wasm32-wasi $@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ here

Then invoke wasm-pack:

CC=(readlink -f zcc) CXX=(readlink -f zcc) wasm-pack build --target web

And load the application in your browser: http://localhost:8000/

You should see the output:

0x007FFFFFFF400000 push rcx
0x007FFFFFFF400001 lea eax, [rbp-0x01]
0x007FFFFFFF400004 push rax
0x007FFFFFFF400005 push [rbp+0x0C]
0x007FFFFFFF400008 push [rbp+0x08]
0x007FFFFFFF40000B call [0x008000007588A5B1]
0x007FFFFFFF400011 test eax, eax
0x007FFFFFFF400013 js 0x007FFFFFFF42DB15

That replicates the example on the Zydis repository.