Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is tooling available to 'assemble' WebAssembly to x86-64 native code?

I am guessing that a Wasm binary is usually JIT-compiled to native code, but given a Wasm source, is there a tool to see the actual generated x86-64 machine code? Or asked in a different way, is there a tool that consumes Wasm and outputs native code?

like image 906
Erik Avatar asked Aug 31 '18 08:08

Erik


1 Answers

The online WasmExplorer compiles C code to both WebAssembly and FireFox x86, using the SpiderMonkey compiler. Given the following simple function:

int testFunction(int* input, int length) {
  int sum = 0;
  for (int i = 0; i < length; ++i) {
    sum += input[i];
  }
  return sum;
}

Here is the x86 output:

wasm-function[0]:
  sub rsp, 8                            ; 0x000000 48 83 ec 08
  cmp esi, 1                            ; 0x000004 83 fe 01
  jge 0x14                              ; 0x000007 0f 8d 07 00 00 00
 0x00000d:                              
  xor eax, eax                          ; 0x00000d 33 c0
  jmp 0x26                              ; 0x00000f e9 12 00 00 00
 0x000014:                              
  xor eax, eax                          ; 0x000014 33 c0
 0x000016:                              ; 0x000016 from: [0x000024]
  mov ecx, dword ptr [r15 + rdi]        ; 0x000016 41 8b 0c 3f
  add eax, ecx                          ; 0x00001a 03 c1
  add edi, 4                            ; 0x00001c 83 c7 04
  add esi, -1                           ; 0x00001f 83 c6 ff
  test esi, esi                         ; 0x000022 85 f6
  jne 0x16                              ; 0x000024 75 f0
 0x000026:                              
  nop                                   ; 0x000026 66 90
  add rsp, 8                            ; 0x000028 48 83 c4 08
  ret 

You can view this example online.

WasmExplorer compiles code into wasm / x86 via a service - you can see the scripts that are run on Github - you should be able to use these to construct a command-line tool yourself.

like image 140
ColinE Avatar answered Sep 20 '22 11:09

ColinE