V8 developers introduced a decompiler for WebAssembly

V8 JavaScript Engine Developers presented utility wasm-decompile, allowing to decompile the intermediate binary representation WebAssembly into a readable pseudo-language reminiscent of JavaScript and C. The proposed pseudo-language is much easier to understand and more suitable for manual parsing than the textual representation of WebAssembly in the ".wat" format, which is closer to assembler than to high-level languages. At the same time, the decompilation, if possible, fully reflects the Wasm representation.

Decompiler included in the toolkit WABT, which provides translation between binary and textual representations of WebAssembly, as well as parsing, processing, modifying and checking wasm files. A utility is also being developed as part of WABT wasm2c, which allows wasm files to be decompiled into equivalent C code that can be compiled by a C compiler, but is not much different from the textual representation of "wat" in terms of readability.

For example, the original C function compiled into wasm

typedef struct { float x, y, z; }vec3;

float dot(const vec3 *a, const vec3 *b) {
return a->x * b->x +
a->y * b->y +
a->z * b->z;
}

will be decompiled by the wasm-decompile utility into a pseudo-language

function dot(a:{ a:float, b:float, c:float },
b:{ a:float, b:float, c:float }):float {
return aa*ba+ab*bb+ac*bc
}

while converting to ".wat" text format would look like this

(func $dot (type 0) (param i32 i32) (result f32)
(f32.add
(f32.add
(f32.mul
(f32.load
(local.get 0))
(f32.load
(local.get 1)))
(f32.mul
(f32.load offset=4
(local.get 0))
(f32.load offset=4
(local.get 1))))
(f32.mul
(f32.load offset=8
(local.get 0))
(f32.load offset=8
(local.get 1))))))

Source: opennet.ru

Add a comment