Commit 5afa8ed7 authored by gdeepti's avatar gdeepti Committed by Commit bot

[wasm] GrowMemory should update imported memory objects.

When the instance has imported memory, calling GrowMemory should update the memory object to have a consistent view of the memory. This fixes the failing emscripten test case, added a reduced test that simulates the same behavior.

R=titzer@chromium.org, dschuff@chromium.org

Review-Url: https://chromiumcodereview.appspot.com/2438673006
Cr-Commit-Position: refs/heads/master@{#40490}
parent 6e162add
......@@ -956,6 +956,12 @@ Handle<JSArrayBuffer> WasmJs::GetWasmMemoryArrayBuffer(Isolate* isolate,
return Handle<JSArrayBuffer>::cast(buf);
}
void WasmJs::SetWasmMemoryArrayBuffer(Isolate* isolate, Handle<Object> value,
Handle<JSArrayBuffer> buffer) {
DCHECK(IsWasmMemoryObject(isolate, value));
JSObject::cast(*value)->SetInternalField(kWasmMemoryBuffer, *buffer);
}
uint32_t WasmJs::GetWasmMemoryMaximumSize(Isolate* isolate,
Handle<Object> value) {
DCHECK(IsWasmMemoryObject(isolate, value));
......
......@@ -33,6 +33,9 @@ class WasmJs {
static Handle<JSArrayBuffer> GetWasmMemoryArrayBuffer(Isolate* isolate,
Handle<Object> value);
static void SetWasmMemoryArrayBuffer(Isolate* isolate, Handle<Object> value,
Handle<JSArrayBuffer> buffer);
static uint32_t GetWasmMemoryMaximumSize(Isolate* isolate,
Handle<Object> value);
......
......@@ -1906,6 +1906,12 @@ int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
Handle<FixedArray> code_table = GetCompiledModule(*instance)->code_table();
RelocateMemoryReferencesInCode(code_table, old_mem_start, new_mem_start,
old_size, new_size);
Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject),
isolate);
if (!memory_object->IsUndefined(isolate)) {
WasmJs::SetWasmMemoryArrayBuffer(isolate, memory_object, buffer);
}
DCHECK(old_size % WasmModule::kPageSize == 0);
return (old_size / WasmModule::kPageSize);
}
......
......@@ -181,3 +181,21 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
}
assertThrows(() => memory.grow(16381));
})();
(function ImportedMemoryBufferLength() {
print("ImportedMemoryBufferLength");
let memory = new WebAssembly.Memory({initial: 2, maximum: 10});
assertEquals(2*kPageSize, memory.buffer.byteLength);
let builder = new WasmModuleBuilder();
builder.addFunction("grow", kSig_i_i)
.addBody([kExprGetLocal, 0, kExprGrowMemory])
.exportFunc();
builder.addImportedMemory("mine");
let instance = builder.instantiate({mine: memory});
function grow(pages) { return instance.exports.grow(pages); }
assertEquals(2, grow(3));
assertEquals(5*kPageSize, memory.buffer.byteLength);
assertEquals(5, grow(5));
assertEquals(10*kPageSize, memory.buffer.byteLength);
assertThrows(() => memory.grow(1));
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment