wasmerio / vscode-wasm

WebAssembly extension for VSCode
https://marketplace.visualstudio.com/items?itemName=dtsvet.vscode-wasm
MIT License
129 stars 19 forks source link

'br $labelName' cause CompileError 'invalid break depth' #14

Closed JustinSDK closed 6 years ago

JustinSDK commented 6 years ago

When writing the code:

(module
    (import "env" "log" (func $log (param i32)))
    (func $main
        block $B0
            block $B1
                br $B1
                i32.const 3
                call $log
            end
            i32.const 2
            call $log
        end
        i32.const 1
        call $log
    )
    (start $main)
)

After right-clicking the .wat file, 'Save as WebAssembly binary file' and saving a 'program.wasm' file, l use the html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <body>
    <script>
    const importObj = {
        env: {
            log(n) {
                console.log(n);
            }
        }
    };
    WebAssembly.instantiateStreaming(fetch('program.wasm'), importObj);
    </script>
  </body>
</html>

And loading it by Chrome, it says:

Uncaught (in promise) CompileError: AsyncCompile: Compiling wasm function #1 failed: invalid break depth: 3228196 @+48
Promise.then (async)
(anonymous) @ (index):15

The same code is ok after assembling and downloading .wasm from WebAssembly Explorer.

reklatsmasters commented 6 years ago

Seems like a bug in libwabt. I hope package https://www.npmjs.com/package/wabt will be updated. If not, i will use webassemblyjs again.

dcodeIO commented 6 years ago

FYI: There are new versions of wabt.js now, preferrably v1.0.5-nightly.20180920. Note that the API to instantiate it has changed a little (WABT now uses Emscripten's MODULARIZE):

// old
var wabt = require("wabt");
// new
var wabt = require("wabt")();
reklatsmasters commented 6 years ago

@dcodeIO Unfortunately, wast@latest and wabt@1.0.5-nightly.20180922 compiled that code incorrectly. This is a generated wasm:

(module
  (type $t0 (func (param i32)))
  (type $t1 (func))
  (import "env" "log" (func $log (type $t0)))
  (func $main (type $t1)
    block $B0
      block $B1
        br 3228196 (; INVALID ;)
        i32.const 3
        call $log
      end
      i32.const 2
      call $log
    end
    i32.const 1
    call $log)
  (start 1))

Probably, it's a bug in original wabt?

dcodeIO commented 6 years ago

Seems so, yeah, or somehow just in its JS build. Appears that br $LABEL always becomes br + some pointer value after parsing, as if a string wasn't correctly evaluated/printed.

dcodeIO commented 6 years ago

For a likely solution to the issue, see: https://github.com/WebAssembly/wabt/issues/915#issuecomment-423859867

reklatsmasters commented 6 years ago

Thanks @dcodeIO @binji for your help!