OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/runtime/runtime-utils.h" |
| 6 |
| 7 #include "src/arguments.h" |
| 8 #include "src/assembler.h" |
| 9 #include "src/debug/debug.h" |
| 10 #include "src/factory.h" |
| 11 #include "src/objects-inl.h" |
| 12 #include "src/wasm/wasm-module.h" |
| 13 |
| 14 namespace v8 { |
| 15 namespace internal { |
| 16 |
| 17 RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { |
| 18 HandleScope scope(isolate); |
| 19 DCHECK_EQ(2, args.length()); |
| 20 CONVERT_INT32_ARG_CHECKED(delta_pages, 0); |
| 21 CONVERT_ARG_HANDLE_CHECKED(JSObject, module_object, 1); |
| 22 RUNTIME_ASSERT(!module_object->IsNull()); |
| 23 |
| 24 byte* old_mem_start; |
| 25 byte* new_mem_start; |
| 26 uint32_t old_size, new_size; |
| 27 const int kWasmMemArrayBuffer = 2; |
| 28 |
| 29 // Get mem buffer and its size associated with the module js_object |
| 30 Object* obj = module_object->GetInternalField(kWasmMemArrayBuffer); |
| 31 Handle<JSArrayBuffer> old_buffer = Handle<JSArrayBuffer>::null(); |
| 32 old_buffer = Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); |
| 33 |
| 34 if (old_buffer->byte_length()->Number() == 0) { |
| 35 // If module object does not have linear memory associated with it, |
| 36 // Allocate new array buffer of given size. |
| 37 old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); |
| 38 old_size = 0; |
| 39 // TODO(gdeepti): Figure out how to update new size correctly here. |
| 40 new_size = delta_pages * wasm::WasmModule::kPageSize; |
| 41 if (delta_pages > wasm::WasmModule::kMaxMemPages) { |
| 42 THROW_NEW_ERROR_RETURN_FAILURE( |
| 43 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
| 44 } |
| 45 new_mem_start = |
| 46 reinterpret_cast<byte*>(isolate->array_buffer_allocator()->Allocate( |
| 47 static_cast<int>(new_size))); |
| 48 RUNTIME_ASSERT(new_mem_start != NULL); |
| 49 #if DEBUG |
| 50 // Double check the API allocator actually zero-initialized the memory. |
| 51 for (size_t i = old_size; i < new_size; i++) { |
| 52 DCHECK_EQ(0, new_mem_start[i]); |
| 53 } |
| 54 #endif |
| 55 } else { |
| 56 old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); |
| 57 old_size = old_buffer->byte_length()->Number(); |
| 58 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; |
| 59 if (new_size > |
| 60 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { |
| 61 THROW_NEW_ERROR_RETURN_FAILURE( |
| 62 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
| 63 } |
| 64 new_mem_start = reinterpret_cast<byte*>(realloc(old_mem_start, new_size)); |
| 65 RUNTIME_ASSERT(new_mem_start != NULL); |
| 66 old_buffer->set_is_external(true); |
| 67 isolate->heap()->UnregisterArrayBuffer(*old_buffer); |
| 68 // Zero initializing uninitialized memory from realloc |
| 69 for (size_t i = old_size; i < new_size; i++) { |
| 70 new_mem_start[i] = 0; |
| 71 } |
| 72 } |
| 73 |
| 74 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
| 75 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); |
| 76 buffer->set_is_neuterable(false); |
| 77 |
| 78 // Set new buffer to be wasm memory |
| 79 module_object->SetInternalField(kWasmMemArrayBuffer, *buffer); |
| 80 |
| 81 RUNTIME_ASSERT(wasm::UpdateWasmModuleMemory( |
| 82 module_object, old_mem_start, new_mem_start, old_size, new_size)); |
| 83 |
| 84 return *isolate->factory()->NewNumberFromInt(old_size / |
| 85 wasm::WasmModule::kPageSize); |
| 86 } |
| 87 |
| 88 } // namespace internal |
| 89 } // namespace v8 |
OLD | NEW |