SPY / haskell-wasm

Haskell WebAssembly Toolkit
Other
151 stars 24 forks source link

Valid module cannot be serialized #15

Closed agustinmista closed 3 years ago

agustinmista commented 3 years ago

Hi @SPY!

Here is a randomly generated module that crashes when serialized:

ghci> m = Module {types = [FuncType {params = [I32,F32], results = [I32]}], functions = [Function {funcType = 0, localTypes = [F64], body = [Block {resultType = [I32,I32], body = [GetLocal 0,Br 0]},I32Const 1,Return]}], tables = [], mems = [Memory (Limit 1 Nothing)], globals = [], elems = [], datas = [], start = Nothing, imports = [], exports = [Export {name = "foo", desc = ExportFunc 0}]}

It is possible to validate, instantiate and run the exported function:

ghci> Right vm = validate m
ghci> Right (mi, s) <- instantiate emptyStore mempty vm
ghci> invokeExport s mi "foo" [VI32 2, VF32 2.0]
Just [VI32 1]

However, if I try to serialize it so I can run it against the spec interpreter, it crashes:

ghci> BS.writeFile "foo.wasm" (dumpModule m)
*** Exception: Current WebAssembly spec does not support returning more then one value
CallStack (from HasCallStack):
  error, called at src/Language/Wasm/Binary.hs:157:19 in wasm-0.1.0-inplace:Language.Wasm.Binary

Is this supposed to happen? I understand the limitation on the return type of a top-level function, but I'm not sure that it also applies to inner blocks. Again, I would appreciate it if you can confirm that the issue is reproducible on your side.

Thanks a lot.

/Agustín

SPY commented 3 years ago

Hi @agustinmista Multi-value returns were not supported at the time of initial implementation. My validator is more generic so it validates it anyway, but binary format supported only 0 or 1 returning values for blocks back then. Currently I work on bringing a parity with the spec compiler back in update-tests branch and multi-value blocks and functions are part of my effort.

Best, Ilya

SPY commented 3 years ago

Fixed