summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshley Nelson <nashley@google.com>2022-11-11 09:38:21 -0800
committerGitHub <noreply@github.com>2022-11-11 09:38:21 -0800
commitb7697808988037469d352b044bd6b2469f99da55 (patch)
tree099cc09af322e2149b6437df90e0751e5cd39ef6
parent38c152e0d8bc593c2861c251cc2a875cfb1a21f0 (diff)
downloadbinaryen-b7697808988037469d352b044bd6b2469f99da55.tar.gz
binaryen-b7697808988037469d352b044bd6b2469f99da55.tar.bz2
binaryen-b7697808988037469d352b044bd6b2469f99da55.zip
Handles memory.grow failure in MultiMemoryLowering Pass (#5241)
Per the wasm spec, memory.grow instructions should return -1 when there is a failure to allocate enough memory. This PR adds support for returning this error code.
-rw-r--r--src/passes/MultiMemoryLowering.cpp13
-rw-r--r--test/lit/passes/multi-memory-lowering.wast36
2 files changed, 36 insertions, 13 deletions
diff --git a/src/passes/MultiMemoryLowering.cpp b/src/passes/MultiMemoryLowering.cpp
index 9e9637b36..527a15804 100644
--- a/src/passes/MultiMemoryLowering.cpp
+++ b/src/passes/MultiMemoryLowering.cpp
@@ -304,12 +304,17 @@ struct MultiMemoryLowering : public Pass {
sizeLocal, builder.makeMemorySize(combinedMemory, memoryInfo)));
}
- // TODO: Check the result of makeMemoryGrow for errors and return the error
- // instead
+ // Attempt to grow the combinedMemory. If -1 returns, enough memory could
+ // not be allocated, so return -1.
functionBody = builder.blockify(
functionBody,
- builder.makeDrop(builder.makeMemoryGrow(
- builder.makeLocalGet(0, pointerType), combinedMemory, memoryInfo)));
+ builder.makeIf(
+ builder.makeBinary(
+ EqInt32,
+ builder.makeMemoryGrow(
+ builder.makeLocalGet(0, pointerType), combinedMemory, memoryInfo),
+ builder.makeConst(-1)),
+ builder.makeReturn(builder.makeConst(-1))));
// If we are not growing the last memory, then we need to copy data,
// shifting it over to accomodate the increase from page_delta
diff --git a/test/lit/passes/multi-memory-lowering.wast b/test/lit/passes/multi-memory-lowering.wast
index 6bf1ecc7c..fc402d877 100644
--- a/test/lit/passes/multi-memory-lowering.wast
+++ b/test/lit/passes/multi-memory-lowering.wast
@@ -147,9 +147,15 @@
;; CHECK-NEXT: (local.set $memory_size
;; CHECK-NEXT: (memory.size)
;; CHECK-NEXT: )
-;; CHECK-NEXT: (drop
-;; CHECK-NEXT: (memory.grow
-;; CHECK-NEXT: (local.get $page_delta)
+;; CHECK-NEXT: (if
+;; CHECK-NEXT: (i32.eq
+;; CHECK-NEXT: (memory.grow
+;; CHECK-NEXT: (local.get $page_delta)
+;; CHECK-NEXT: )
+;; CHECK-NEXT: (i32.const -1)
+;; CHECK-NEXT: )
+;; CHECK-NEXT: (return
+;; CHECK-NEXT: (i32.const -1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (memory.copy
@@ -199,9 +205,15 @@
;; CHECK-NEXT: (local.set $memory_size
;; CHECK-NEXT: (memory.size)
;; CHECK-NEXT: )
-;; CHECK-NEXT: (drop
-;; CHECK-NEXT: (memory.grow
-;; CHECK-NEXT: (local.get $page_delta)
+;; CHECK-NEXT: (if
+;; CHECK-NEXT: (i32.eq
+;; CHECK-NEXT: (memory.grow
+;; CHECK-NEXT: (local.get $page_delta)
+;; CHECK-NEXT: )
+;; CHECK-NEXT: (i32.const -1)
+;; CHECK-NEXT: )
+;; CHECK-NEXT: (return
+;; CHECK-NEXT: (i32.const -1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (memory.copy
@@ -238,9 +250,15 @@
;; CHECK-NEXT: (local.set $return_size
;; CHECK-NEXT: (call $memory3_size)
;; CHECK-NEXT: )
-;; CHECK-NEXT: (drop
-;; CHECK-NEXT: (memory.grow
-;; CHECK-NEXT: (local.get $page_delta)
+;; CHECK-NEXT: (if
+;; CHECK-NEXT: (i32.eq
+;; CHECK-NEXT: (memory.grow
+;; CHECK-NEXT: (local.get $page_delta)
+;; CHECK-NEXT: )
+;; CHECK-NEXT: (i32.const -1)
+;; CHECK-NEXT: )
+;; CHECK-NEXT: (return
+;; CHECK-NEXT: (i32.const -1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $return_size)