From 9347a223fcc1b6c297805b3c37fe1e7473158720 Mon Sep 17 00:00:00 2001 From: Rikito Taniguchi Date: Tue, 4 Jun 2024 04:42:02 +0900 Subject: Fix binary parser of declarative element segments (#6618) 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 --- test/lit/binary/declarative-element-use-expr.test | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/lit/binary/declarative-element-use-expr.test (limited to 'test/lit/binary/declarative-element-use-expr.test') 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 000000000..aecdf9ebd --- /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: ) -- cgit v1.2.3