Chromium Code Reviews| Index: src/runtime/runtime-wasm.cc |
| diff --git a/src/runtime/runtime-wasm.cc b/src/runtime/runtime-wasm.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a3c58aea24c6d12fbb5e134d0bd185b88f7baa7b |
| --- /dev/null |
| +++ b/src/runtime/runtime-wasm.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2016 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <stdlib.h> |
| +#include "src/debug/debug.h" |
| +#include "src/disasm.h" |
| +#include "src/disassembler.h" |
| + |
| +#include "src/runtime/runtime-utils.h" |
| + |
| +#include "src/arguments.h" |
| +#include "src/assembler.h" |
| +#include "src/base/macros.h" |
| +#include "src/conversions.h" |
| +#include "src/factory.h" |
| +#include "src/objects-inl.h" |
| +#include "src/wasm/wasm-module.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| +RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { |
| + HandleScope scope(isolate); |
| + DCHECK_EQ(2, args.length()); |
| + CONVERT_INT32_ARG_CHECKED(delta_pages, 0); |
| + CONVERT_ARG_CHECKED(JSObject, module_object, 1); |
| + RUNTIME_ASSERT(!module_object->IsNull()); |
| + |
| + byte* old_mem_start; |
| + byte* new_mem_start; |
| + uint32_t old_size, new_size; |
| + |
| + // Get mem buffer and its size associated with the module js_object |
| + Object* obj = module_object->GetInternalField(2); |
| + Handle<JSArrayBuffer> old_buffer = Handle<JSArrayBuffer>::null(); |
| + old_buffer = Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); |
| + |
| + if (old_buffer->byte_length()->Number() == 0) { |
| + // If module object does not have linear memory associated with it, |
| + // Allocate new array buffer of given size. |
| + old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); |
| + old_size = 0; |
| + // TODO(gdeepti): Figure out how to update new size correctly here. |
| + new_size = delta_pages * wasm::WasmModule::kPageSize; |
| + if (delta_pages > wasm::WasmModule::kMaxMemPages) { |
| + THROW_NEW_ERROR_RETURN_FAILURE( |
| + isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
| + } |
| + new_mem_start = |
| + reinterpret_cast<byte*>(isolate->array_buffer_allocator()->Allocate( |
| + static_cast<int>(new_size))); |
| + RUNTIME_ASSERT(new_mem_start != NULL); |
| +#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 = reinterpret_cast<byte*>(old_buffer->backing_store()); |
| + old_size = old_buffer->byte_length()->Number(); |
| + new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; |
| + if (new_size > |
| + wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { |
| + THROW_NEW_ERROR_RETURN_FAILURE( |
| + isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
| + } |
| + new_mem_start = reinterpret_cast<byte*>(realloc(old_mem_start, new_size)); |
| + RUNTIME_ASSERT(new_mem_start != NULL); |
| + old_buffer->set_is_external(true); |
| + isolate->heap()->UnregisterArrayBuffer(*old_buffer); |
| + // Zero initializinf uninitialized memory from realloc |
|
titzer
2016/06/10 09:38:13
s/initializinf/initializing
gdeepti
2016/06/10 22:39:57
Done.
|
| + for (size_t i = old_size; i < new_size; i++) { |
| + new_mem_start[i] = 0; |
| + } |
| + } |
| + |
| + 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 |
| + module_object->SetInternalField(2, *buffer); |
|
titzer
2016/06/10 09:38:13
Can we introduce some constants for the module fie
gdeepti
2016/06/10 22:39:57
Done.
|
| + |
| + // Get code table associated with the module js_object |
| + obj = module_object->GetInternalField(1); |
| + Handle<FixedArray> code_table; |
| + code_table = Handle<FixedArray>(FixedArray::cast(obj)); |
| + |
| + // Iterate through the code objects in the code table and update relocation |
| + // information |
| + for (int i = 0; i < code_table->length(); i++) { |
| + Handle<Code> code; |
| + obj = code_table->get(i); |
| + code = Handle<Code>(Code::cast(obj)); |
| + |
| + int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | |
| + RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
| + for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { |
|
titzer
2016/06/10 09:38:13
Can you factor this routine out into a unittestabl
gdeepti
2016/06/10 22:39:57
Moved to wasm-module.cc
|
| + RelocInfo::Mode mode = it.rinfo()->rmode(); |
| + if (RelocInfo::IsWasmMemoryReference(mode) || |
| + RelocInfo::IsWasmMemorySizeReference(mode)) { |
| + it.rinfo()->update_wasm_memory_reference( |
| + reinterpret_cast<Address>(old_mem_start), |
| + reinterpret_cast<Address>(new_mem_start), old_size, new_size); |
| + } |
| + } |
| + } |
| + |
| + return *isolate->factory()->NewNumberFromInt(old_size / |
| + wasm::WasmModule::kPageSize); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace v8 |