Commit 096b5f64 authored by gdeepti's avatar gdeepti Committed by Commit bot

[wasm] Refactor GrowMemory runtime call.

Refactor to move module specific functionality to wasm-module.cc, this provides a better interface for the grow() method on WebAssembly.memory objects.

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

Review-Url: https://codereview.chromium.org/2396473003
Cr-Commit-Position: refs/heads/master@{#39967}
parent a5a9198a
......@@ -23,7 +23,6 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
DCHECK_EQ(1, args.length());
CONVERT_UINT32_ARG_CHECKED(delta_pages, 0);
Handle<JSObject> module_instance;
{
// Get the module JSObject
DisallowHeapAllocation no_allocation;
......@@ -36,77 +35,8 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
CHECK_NOT_NULL(owning_instance);
module_instance = handle(JSObject::cast(owning_instance), isolate);
}
Address old_mem_start, new_mem_start;
uint32_t old_size, new_size;
// Get mem buffer associated with module object
MaybeHandle<JSArrayBuffer> maybe_mem_buffer =
wasm::GetInstanceMemory(isolate, module_instance);
Handle<JSArrayBuffer> old_buffer;
if (!maybe_mem_buffer.ToHandle(&old_buffer)) {
// If module object does not have linear memory associated with it,
// Allocate new array buffer of given size.
old_mem_start = nullptr;
old_size = 0;
// TODO(gdeepti): Fix bounds check to take into account size of memtype.
new_size = delta_pages * wasm::WasmModule::kPageSize;
// The code generated in the wasm compiler guarantees this precondition.
DCHECK(delta_pages <= wasm::WasmModule::kMaxMemPages);
new_mem_start =
static_cast<Address>(isolate->array_buffer_allocator()->Allocate(
static_cast<uint32_t>(new_size)));
if (new_mem_start == NULL) {
return *isolate->factory()->NewNumberFromInt(-1);
}
#if DEBUG
// Double check the API allocator actually zero-initialized the memory.
for (size_t i = old_size; i < new_size; i++) {
DCHECK_EQ(0, new_mem_start[i]);
}
#endif
} else {
old_mem_start = static_cast<Address>(old_buffer->backing_store());
old_size = old_buffer->byte_length()->Number();
// If the old memory was zero-sized, we should have been in the
// "undefined" case above.
DCHECK_NOT_NULL(old_mem_start);
DCHECK_NE(0, old_size);
new_size = old_size + delta_pages * wasm::WasmModule::kPageSize;
if (new_size >
wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) {
return *isolate->factory()->NewNumberFromInt(-1);
}
new_mem_start =
static_cast<Address>(isolate->array_buffer_allocator()->Allocate(
static_cast<uint32_t>(new_size)));
if (new_mem_start == NULL) {
return *isolate->factory()->NewNumberFromInt(-1);
}
#if DEBUG
// Double check the API allocator actually zero-initialized the memory.
for (size_t i = old_size; i < new_size; i++) {
DCHECK_EQ(0, new_mem_start[i]);
}
#endif
// Copy contents of the old buffer to the new buffer
memcpy(new_mem_start, old_mem_start, old_size);
}
Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size);
buffer->set_is_neuterable(false);
// Set new buffer to be wasm memory
wasm::SetInstanceMemory(module_instance, *buffer);
CHECK(wasm::UpdateWasmModuleMemory(module_instance, old_mem_start,
new_mem_start, old_size, new_size));
return *isolate->factory()->NewNumberFromInt(old_size /
wasm::WasmModule::kPageSize);
return *isolate->factory()->NewNumberFromInt(
wasm::GrowInstanceMemory(isolate, module_instance, delta_pages));
}
RUNTIME_FUNCTION(Runtime_WasmThrowTypeError) {
......
......@@ -1405,7 +1405,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
uint32_t size = Smi::cast(metadata->get(kSize))->value();
Handle<FixedArray> table =
metadata->GetValueChecked<FixedArray>(isolate, kTable);
wasm::PopulateFunctionTable(table, size, &functions);
PopulateFunctionTable(table, size, &functions);
}
instance->SetInternalField(kWasmModuleFunctionTable, *indirect_tables);
}
......@@ -1809,6 +1809,52 @@ void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) {
module->set_ptr_to_heap(buffer);
}
int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
uint32_t pages) {
Address old_mem_start = nullptr;
uint32_t old_size = 0, new_size = 0;
MaybeHandle<JSArrayBuffer> maybe_mem_buffer =
GetInstanceMemory(isolate, instance);
Handle<JSArrayBuffer> old_buffer;
if (!maybe_mem_buffer.ToHandle(&old_buffer)) {
// If module object does not have linear memory associated with it,
// Allocate new array buffer of given size.
// TODO(gdeepti): Fix bounds check to take into account size of memtype.
new_size = pages * WasmModule::kPageSize;
// The code generated in the wasm compiler guarantees this precondition.
DCHECK(pages <= WasmModule::kMaxMemPages);
} else {
old_mem_start = static_cast<Address>(old_buffer->backing_store());
old_size = old_buffer->byte_length()->Number();
// If the old memory was zero-sized, we should have been in the
// "undefined" case above.
DCHECK_NOT_NULL(old_mem_start);
DCHECK_NE(0, old_size);
DCHECK(old_size + pages * WasmModule::kPageSize <=
std::numeric_limits<uint32_t>::max());
new_size = old_size + pages * WasmModule::kPageSize;
}
if (new_size <= old_size ||
WasmModule::kMaxMemPages * WasmModule::kPageSize <= new_size) {
return -1;
}
Handle<JSArrayBuffer> buffer = NewArrayBuffer(isolate, new_size);
if (buffer.is_null()) return -1;
Address new_mem_start = static_cast<Address>(buffer->backing_store());
if (old_size != 0) {
memcpy(new_mem_start, old_mem_start, old_size);
}
SetInstanceMemory(instance, *buffer);
if (!UpdateWasmModuleMemory(instance, old_mem_start, new_mem_start, old_size,
new_size)) {
return -1;
}
DCHECK(old_size % WasmModule::kPageSize == 0);
return (old_size / WasmModule::kPageSize);
}
namespace testing {
void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj,
......
......@@ -534,9 +534,8 @@ uint32_t GetNumImportedFunctions(Handle<JSObject> wasm_object);
// Returns nullptr on failing to get owning instance.
Object* GetOwningWasmInstance(Code* code);
MaybeHandle<JSArrayBuffer> GetInstanceMemory(Isolate* isolate,
Handle<JSObject> instance);
void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer);
int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
uint32_t pages);
namespace testing {
......
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