-
Notifications
You must be signed in to change notification settings - Fork 3
wasm-opt fails to run on our binary #136
Comments
https://webassembly.github.io/spec/core/binary/modules.html#element-section $ bin/wasm-opt --debug sample/target/scala-2.12/sample-fastopt/main.wasm
...
== readElementSegments
<==
getInt8: 1 (at 7633) # number of elem
getU32LEB: 1 ==>
<==
getInt8: 7 (at 7634) # elem flag (declarative)
getU32LEB: 7 ==>
<==
getInt8: 112 (at 7635) # reftype
getU32LEB: 112 ==>
<==
getInt8: 17 (at 7636) # length of expr
getU32LEB: 17 ==>
<==
getInt8: 210 (at 7637) # ref.func
getInt8: 112 (at 7638) # funcidx
getU32LEB: 14418 ==>
<==
getInt8: 11 (at 7639) # 0x0B why ???
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7640)
getInt8: 113 (at 7641)
getU32LEB: 14546 ==>
<==
getInt8: 11 (at 7642)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7643)
getInt8: 115 (at 7644)
getU32LEB: 14802 ==>
<==
getInt8: 11 (at 7645)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7646)
getInt8: 116 (at 7647)
getU32LEB: 14930 ==>
<==
getInt8: 11 (at 7648)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7649)
getInt8: 117 (at 7650)
getU32LEB: 15058 ==>
<==
getInt8: 11 (at 7651)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7652)
getInt8: 122 (at 7653)
getU32LEB: 15698 ==>
<==
getInt8: 11 (at 7654)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7655)
getInt8: 123 (at 7656)
getU32LEB: 15826 ==>
<==
getInt8: 11 (at 7657)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7658)
getInt8: 124 (at 7659)
getU32LEB: 15954 ==>
<==
getInt8: 11 (at 7660)
getU32LEB: 11 ==>
<==
getInt8: 210 (at 7661)
getInt8: 125 (at 7662)
getU32LEB: 16082 ==>
[parse exception: bad section size, started at 7633 plus payload 61 not being equal to new position 7663 (at 0:7663)]
Assertion failed: (*this && "Cannot print an empty name"), function print, file name.cpp, line 44.
zsh: abort /Users/tanishiking/ghq/github.com/WebAssembly/binaryen/bin/wasm-opt --debug 11 (0x0B) is the opcode for end, which should appear only once in the end of the
It seems like we insert the |
So, we should use scala-wasm/wasm/src/main/scala/org/scalajs/linker/backend/webassembly/BinaryWriter.scala Lines 231 to 233 in fb3bcb2
|
Hmm, not actually, we follow the spec right, and binaryen parser seems to be a culprit 🤔 ![]() https://webassembly.github.io/spec/core/binary/modules.html#element-section |
I wouldn't be surprised if binaryen is confused about things only found in GC-heavy usage of Wasm. It was designed for users of the linear memory model first. |
Could fix the issue with the following diff, diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index ec06692a4..2aa5e7c11 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -3354,7 +3354,7 @@ void WasmBinaryReader::readElementSegments() {
[[maybe_unused]] auto type = getU32LEB();
auto num = getU32LEB();
for (Index i = 0; i < num; i++) {
- getU32LEB();
+ readExpression();
}
continue;
} but we hit the different problem anyway 😄 $ bin/wasm-opt sample/target/scala-2.12/sample-fastopt/main.wasm
[parse exception: control flow inputs are not supported yet (at 0:8437)]
Fatal: error parsing wasm (try --debug for more info) probably this WebAssembly/binaryen#6407 I'll send an above patch to binaryen, and call it a day for now |
The parser was incorrectly handling the parsing of declarative element segments (those with the `elemkind` of `declarative`). According to the spec, the `init` of the declarative element should be a `vec(expr)`. https://webassembly.github.io/spec/core/binary/modules.html#element-section However, binaryen was simple reading a sing `u32` value for each expression instead of parsing a expression. This commit updates the `WasmBinaryReader::readElementSegments` function to correctly parse the expressions for declarative element segments by calling `readExpression` instead of `getU32LEB`. Resolves the parsing exception: "[parse exception: bad section size, started at ... not being equal to new position ...]" Related discussion: tanishiking/scala-wasm#136
The parser was incorrectly handling the parsing of declarative element segments (those with the `elemkind` of `declarative`). According to the spec, the `init` of the declarative element should be a `vec(expr)`. https://webassembly.github.io/spec/core/binary/modules.html#element-section However, binaryen was simple reading a sing `u32` value for each expression instead of parsing a expression. This commit updates the `WasmBinaryReader::readElementSegments` function to correctly parse the expressions for declarative element segments by calling `readExpression` instead of `getU32LEB`. Resolves the parsing exception: "[parse exception: bad section size, started at ... not being equal to new position ...]" Related discussion: tanishiking/scala-wasm#136
The parser was incorrectly handling the parsing of declarative element segments (those with the `elemkind` of `declarative`). According to the spec, the `init` of the declarative element should be a `vec(expr)` if the prefix is 7. https://webassembly.github.io/spec/core/binary/modules.html#element-section However, binaryen was simply reading a single `u32LEB` value for each expression instead of parsing a expression regardless `usesExpressions = true`. This commit updates the `WasmBinaryReader::readElementSegments` function to correctly parse the expressions for declarative element segments by calling `readExpression` instead of `getU32LEB` if `usesExpressions = true`. Resolves the parsing exception: "[parse exception: bad section size, started at ... not being equal to new position ...]" Related discussion: tanishiking/scala-wasm#136
The parser was incorrectly handling the parsing of declarative element segments whose `init` is a `vec(expr)`. https://webassembly.github.io/spec/core/binary/modules.html#element-section Binry parser was simply reading a single `u32LEB` value for `init` instead of parsing a expression regardless `usesExpressions = true`. This commit updates the `WasmBinaryReader::readElementSegments` function to correctly parse the expressions for declarative element segments by calling `readExpression` instead of `getU32LEB` when `usesExpressions = true`. Resolves the parsing exception: "[parse exception: bad section size, started at ... not being equal to new position ...]" Related discussion: tanishiking/scala-wasm#136
The parser was incorrectly handling the parsing of declarative element segments whose `init` is a `vec(expr)`. https://webassembly.github.io/spec/core/binary/modules.html#element-section Binry parser was simply reading a single `u32LEB` value for `init` instead of parsing a expression regardless `usesExpressions = true`. This commit updates the `WasmBinaryReader::readElementSegments` function to correctly parse the expressions for declarative element segments by calling `readExpression` instead of `getU32LEB` when `usesExpressions = true`. Resolves the parsing exception: "[parse exception: bad section size, started at ... not being equal to new position ...]" Related discussion: tanishiking/scala-wasm#136
wasm-opt
build on 2024-05-24 WebAssembly/binaryen@5999c99$ bin/wasm-opt sample/target/scala-2.12/sample-fastopt/main.wasm [parse exception: bad section size, started at 7633 plus payload 61 not being equal to new position 7663 (at 0:7663)] Fatal: error parsing wasm (try --debug for more info)
Since
0x00001dd1 = 7633
, it looks like binaryen fails to parse Elem Section (more precisely, binaryen couldn't find the enough content of 61 bytes in the elem section) and failed at the validation https://github.com/WebAssembly/binaryen/blob/5999c996c36abeba912599b5fba83d0b2989ed4c/src/wasm/wasm-binary.cpp#L1829-L1834It seems binaryen stopped reading at
?
The text was updated successfully, but these errors were encountered: