From 9320e181a5d12ee2cf722a374820f4b6b7d95e51 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 21 Feb 2024 15:33:13 -0800 Subject: [PATCH] Validator: ArrayNew|InitData require Bulk Memory (#6331) Those instructions refer to a data segment, which mean the DataCount section must be emitted before them (so that, per the spec, they can be validated by looking only at previous sections), which implies bulk-memory is needed. --- src/wasm/wasm-validator.cpp | 8 +++++++ test/lit/validation/array-init-data.wast | 27 ++++++++++++++++++++++++ test/lit/validation/array-new-data.wast | 25 ++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 test/lit/validation/array-init-data.wast create mode 100644 test/lit/validation/array-new-data.wast diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 3688587be05..3fbcb0bfb74 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -2909,6 +2909,10 @@ void FunctionValidator::visitArrayNew(ArrayNew* curr) { void FunctionValidator::visitArrayNewData(ArrayNewData* curr) { visitArrayNew(curr); + shouldBeTrue( + getModule()->features.hasBulkMemory(), + curr, + "Data segment operations require bulk memory [--enable-bulk-memory]"); if (!shouldBeTrue(getModule()->getDataSegment(curr->segment), curr, "array.new_data segment should exist")) { @@ -3175,6 +3179,10 @@ void FunctionValidator::visitArrayInit(ArrayInit* curr) { void FunctionValidator::visitArrayInitData(ArrayInitData* curr) { visitArrayInit(curr); + shouldBeTrue( + getModule()->features.hasBulkMemory(), + curr, + "Data segment operations require bulk memory [--enable-bulk-memory]"); shouldBeTrue(getModule()->getDataSegmentOrNull(curr->segment), curr, "array.init_data segment must exist"); diff --git a/test/lit/validation/array-init-data.wast b/test/lit/validation/array-init-data.wast new file mode 100644 index 00000000000..088d0e9dee3 --- /dev/null +++ b/test/lit/validation/array-init-data.wast @@ -0,0 +1,27 @@ +;; array.init_data refers to a data segment and therefore requires the datacount +;; section be emitted (so it can be validated, per the spec, using only previous +;; sections), which means bulk memory must be enabled. + +;; RUN: not wasm-opt --enable-reference-types --enable-gc %s 2>&1 | filecheck %s + +;; CHECK: Data segment operations require bulk memory + +(module + (type $0 (array i8)) + + (memory $0 16 17) + + (data $0 (i32.const 0) "") + + (func $0 + (array.init_data $0 $0 + (ref.null $0) + (i32.const 0) + (i32.const 0) + (i32.const 0) + ) + ) +) + +;; But it passes with the feature enabled. +;; RUN: wasm-opt --enable-reference-types --enable-gc --enable-bulk-memory %s diff --git a/test/lit/validation/array-new-data.wast b/test/lit/validation/array-new-data.wast new file mode 100644 index 00000000000..e7d67ee347d --- /dev/null +++ b/test/lit/validation/array-new-data.wast @@ -0,0 +1,25 @@ +;; array.new_data refers to a data segment and therefore requires the datacount +;; section be emitted (so it can be validated, per the spec, using only previous +;; sections), which means bulk memory must be enabled. + +;; RUN: not wasm-opt --enable-reference-types --enable-gc %s 2>&1 | filecheck %s + +;; CHECK: Data segment operations require bulk memory + +(module + (type $0 (array i8)) + + (memory $0 16 17) + + (data $0 (i32.const 0) "") + + (func $0 (result (ref $0)) + (array.new_data $0 $0 + (i32.const 0) + (i32.const 0) + ) + ) +) + +;; But it passes with the feature enabled. +;; RUN: wasm-opt --enable-reference-types --enable-gc --enable-bulk-memory %s