wwwg / wasmdec

WebAssembly to C decompiler
https://wwwg.github.io/web-wasmdec/
MIT License
399 stars 26 forks source link

Doesn't support stack machine "flat" syntax #15

Open qpIlIpp opened 6 years ago

qpIlIpp commented 6 years ago

Hi! If u will get wast from EOS system (cleos -u https://mainnet.eoscalgary.io:443 get code eosio.token -c a.txt ) , it cannot be decompiled by your decompile, but wast is ok.

sorce code cpp is here : https://github.com/EOSIO/eosio.contracts/blob/master/eosio.token/include/eosio.token/eosio.token.hpp

wast is attached: a.txt

wwwg commented 6 years ago

Hey, it looks like that binary is in .wat format, which isn't supported by wasmdec. However, you can convert this binary into a .wasm with wat2wasm offered in the wabt repository, then decompile it with wasmdec. I'll leave this issue open and implement .wat parsing.

qpIlIpp commented 6 years ago

Cleos told me, that it was wast: "saving wast..", but anyway, thank you!

wwwg commented 6 years ago

Then there must be a mistake on Ceos' behalf, because a.txt appears to be a mix between s-expressions and the stack machine format, and .wast, generally speaking, is exclusively s-expressions.

You can see the two syntax's mix on line 178 of a.txt which defines func $75:

(func $75
    (param $0 i32)
    (param $1 i64)
    (param $2 i32)
    (local $3 i32)
    (local $4 i32)
    (local $5 i64)
    (local $6 i64)
    (local $7 i64)
    (local $8 i64)
    (local $9 i32)
    (local $10 i32)
    (local $11 i64)
    get_global $31
    i32.const 128
    i32.sub
    tee_local $3
    set_global $31
    get_local $0
    i64.load
    call $35
    i32.const 0
    set_local $4
    ;; truncated function body
)

In the example on the wasmdec README, you can clearly see the difference

(func $addTwo (param i32 i32) (result i32)
    (return
        (i32.add
                    ;; Notice how the get_local instructions are structured like a tree rather than stack values
                    (get_local 0)
                    (get_local 1)
               )
    )
)
qpIlIpp commented 6 years ago

oh, I see. Thank, I'll write to eosio.

binji commented 6 years ago

Sorry, the WebAssembly text format is a bit confusing. .wast is meant to be only for the WebAssembly spec test script format. This can include multiple modules, as well as mechanisms for linking modules together, invoking functions, and calling assertions.

The .wat format only has one module and has none of the scripting functionality.

Both formats allow S-expressions (what we call "folded") and the "flat" syntax. The folded format is syntactic sugar for the flat format. For example, (i32.add (i32.const 1) (i32.const 2)) and i32.const 1 i32.const 2 i32.add generate identical instruction sequences. Partially folded constructs are also supported, like i32.const 1 (i32.add (i32.const 2)).

Some tools like binaryen only parse the folded format, so many of the .wast files in the spec testsuite are written using the folded format (and because it is somewhat easier to read/write).

wwwg commented 6 years ago

Thank you for the clarification, I'll rename the issue accordingly.

jafri commented 6 years ago

I have attached a WASM in txt format from EOS that shows the same issues.

File: chintailease.txt