From bd7db20a540e850eb9a87065daec0c91b0b3b071 Mon Sep 17 00:00:00 2001 From: Rikito Taniguchi Date: Tue, 21 May 2024 17:12:17 +0900 Subject: [PATCH 1/2] Fix parser of declarative element segments 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: https://github.com/tanishiking/scala-wasm/issues/136 --- src/wasm/wasm-binary.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index ec06692a4b0..c244aa531b4 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -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; } From 02efecd94395e94e7d28a097397ab590a3b8ba21 Mon Sep 17 00:00:00 2001 From: Rikito Taniguchi Date: Fri, 31 May 2024 17:04:18 +0900 Subject: [PATCH 2/2] Test binary parser parses declarative element segment with `init` as `vec(expr)` This commit adds a binary in `test/lit/binary` that is generated from the following WAT file with a reference-interpreter from the `wasm-3.0-exn` branch: https://github.com/WebAssembly/spec/commit/b16745e41a20024ee5446574da52a22849177596 ```wat (module (elem declare funcref (item ref.func $f)) (func $f (block (ref.func $f) (drop) )) ) ``` The disassembled result contains `(elem declare func $f)` instead of `(elem declare funcref (item ref.func $f))` because the binaryen parser doesn't preserve declarative segments. This is fine, as we test that the binary parser can parse the declarative element segment whose `init` is `vec(expr)` without trapping. --- .../binary/declarative-element-use-expr.test | 28 ++++++++++++++++++ .../declarative-element-use-expr.test.wasm | Bin 0 -> 59 bytes 2 files changed, 28 insertions(+) create mode 100644 test/lit/binary/declarative-element-use-expr.test create mode 100644 test/lit/binary/declarative-element-use-expr.test.wasm diff --git a/test/lit/binary/declarative-element-use-expr.test b/test/lit/binary/declarative-element-use-expr.test new file mode 100644 index 00000000000..aecdf9ebde9 --- /dev/null +++ b/test/lit/binary/declarative-element-use-expr.test @@ -0,0 +1,28 @@ +;; Verify a binary with declarative element segment whose init is vector of expr +;; can be parsed correctly. +;; The declarative-element-use-expr file contains this: +;; +;; (module +;; (type $0 (func)) +;; (func $0 (type 0) (block (ref.func 0) (drop))) +;; (elem $0 declare funcref (item ref.func 0)) +;; ) +;; +;; The wasm-opt output contains `(elem declare func 0)` instead of +;; `(elem declare funcref (item ref.func 0))` because the parser doesn't +;; preserve declarative segments. This is fine, as we test that the +;; binary parser can parse it correctly. + +;; RUN: wasm-opt -all %s.wasm -all --print | filecheck %s + +;; CHECK: (module +;; CHECK-NEXT: (type $0 (func)) +;; CHECK-NEXT: (elem declare func $0) +;; CHECK-NEXT: (func $0 (type $0) +;; CHECK-NEXT: (block $label$1 +;; CHECK-NEXT: (drop +;; CHECK-NEXT: (ref.func $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) diff --git a/test/lit/binary/declarative-element-use-expr.test.wasm b/test/lit/binary/declarative-element-use-expr.test.wasm new file mode 100644 index 0000000000000000000000000000000000000000..54ebc37d4524bc37d93fe0fd2b046e67cf1c677d GIT binary patch literal 59 zcmZQbEY4+QU|?WuX=rF*U`$|OU~U4l7&zO(GPL^3crTw;*o<^}+% CiVk`J literal 0 HcmV?d00001