Open tanishiking opened 6 months ago
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 expr
, but it seems like we insert 0x0B for each instr?
Expressions are encoded by their instruction sequence terminated with an explicit https://webassembly.github.io/spec/core/binary/modules.html#element-section
It seems like we insert the
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 https://github.com/WebAssembly/binaryen/issues/6407
I'll send an above patch to binaryen, and call it a day for now
wasm-opt
build on 2024-05-24 https://github.com/WebAssembly/binaryen/commit/5999c996c36abeba912599b5fba83d0b2989ed4cSince
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
?