Skip to content

Commit

Permalink
Validator: ArrayNew|InitData require Bulk Memory (WebAssembly#6331)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kripken authored and radekdoulik committed Jul 12, 2024
1 parent 1886322 commit 9320e18
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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");
Expand Down
27 changes: 27 additions & 0 deletions test/lit/validation/array-init-data.wast
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/lit/validation/array-new-data.wast
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9320e18

Please sign in to comment.