fix: memory.grow by a negative number is an error (#498)

This is to comply with e.g. wasmtime. The spec does not make it
explicit what growing by a negative number should mean. It's likely a
mistake, so let's fail.

Closes #442.
This commit is contained in:
tommie
2024-08-07 12:28:25 +03:00
committed by GitHub
parent 256cd37bbd
commit 35b5e2fb53
3 changed files with 23 additions and 10 deletions
+16 -10
View File
@@ -708,18 +708,24 @@ d_m3Op (MemGrow)
IM3Runtime runtime = m3MemRuntime(_mem);
IM3Memory memory = & runtime->memory;
u32 numPagesToGrow = (u32) _r0;
_r0 = memory->numPages;
i32 numPagesToGrow = _r0;
if (numPagesToGrow >= 0) {
_r0 = memory->numPages;
if (M3_LIKELY(numPagesToGrow))
if (M3_LIKELY(numPagesToGrow))
{
u32 requiredPages = memory->numPages + numPagesToGrow;
M3Result r = ResizeMemory (runtime, requiredPages);
if (r)
_r0 = -1;
_mem = memory->mallocated;
}
}
else
{
u32 requiredPages = memory->numPages + numPagesToGrow;
M3Result r = ResizeMemory (runtime, requiredPages);
if (r)
_r0 = -1;
_mem = memory->mallocated;
_r0 = -1;
}
nextOp ();
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
(module
(func $to_test (result i32)
i32.const -1
memory.grow)
(memory 1 5)
(export "to_test" (func $to_test))
)