Skip to content

Commit

Permalink
Fix parser of declarative element segments
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tanishiking committed May 23, 2024
1 parent 5999c99 commit 9b56e6d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3354,7 +3354,11 @@ void WasmBinaryReader::readElementSegments() {
[[maybe_unused]] auto type = getU32LEB();
auto num = getU32LEB();
for (Index i = 0; i < num; i++) {
getU32LEB();
if (usesExpressions) {
readExpression();
} else {
getU32LEB();
}
}
continue;
}
Expand Down

0 comments on commit 9b56e6d

Please sign in to comment.