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/conversions.h" |
| 10 #include "src/debug/debug.h" |
| 11 #include "src/factory.h" |
| 12 #include "src/frames-inl.h" |
| 13 #include "src/objects-inl.h" |
| 14 #include "src/v8memory.h" |
| 15 #include "src/wasm/wasm-module.h" |
| 16 |
| 17 namespace v8 { |
| 18 namespace internal { |
| 19 |
| 20 RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { |
| 21 HandleScope scope(isolate); |
| 22 DCHECK_EQ(1, args.length()); |
| 23 uint32_t delta_pages = 0; |
| 24 RUNTIME_ASSERT(args[0]->ToUint32(&delta_pages)); |
| 25 |
| 26 // Get the module JSObject |
| 27 const Address entry = Isolate::c_entry_fp(isolate->thread_local_top()); |
| 28 Address pc = |
| 29 Memory::Address_at(entry + StandardFrameConstants::kCallerPCOffset); |
| 30 Code* code = isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code; |
| 31 FixedArray* deopt_data = code->deoptimization_data(); |
| 32 DCHECK(deopt_data->length() == 2); |
| 33 JSObject* module_object = JSObject::cast(deopt_data->get(0)); |
| 34 RUNTIME_ASSERT(!module_object->IsNull(isolate)); |
| 35 |
| 36 Address old_mem_start, new_mem_start; |
| 37 uint32_t old_size, new_size; |
| 38 const int kWasmMemArrayBuffer = 2; |
| 39 |
| 40 // Get mem buffer associated with module object |
| 41 Object* obj = module_object->GetInternalField(kWasmMemArrayBuffer); |
| 42 Handle<JSArrayBuffer> old_buffer = |
| 43 Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); |
| 44 |
| 45 if (old_buffer->byte_length()->Number() == 0) { |
| 46 // If module object does not have linear memory associated with it, |
| 47 // Allocate new array buffer of given size. |
| 48 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
| 49 old_size = 0; |
| 50 // TODO(gdeepti): Fix bounds check to take into account size of memtype. |
| 51 new_size = delta_pages * wasm::WasmModule::kPageSize; |
| 52 if (delta_pages > wasm::WasmModule::kMaxMemPages) { |
| 53 THROW_NEW_ERROR_RETURN_FAILURE( |
| 54 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
| 55 } |
| 56 new_mem_start = |
| 57 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( |
| 58 static_cast<uint32_t>(new_size))); |
| 59 if (new_mem_start == NULL) { |
| 60 THROW_NEW_ERROR_RETURN_FAILURE( |
| 61 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); |
| 62 } |
| 63 #if DEBUG |
| 64 // Double check the API allocator actually zero-initialized the memory. |
| 65 for (size_t i = old_size; i < new_size; i++) { |
| 66 DCHECK_EQ(0, new_mem_start[i]); |
| 67 } |
| 68 #endif |
| 69 } else { |
| 70 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
| 71 old_size = old_buffer->byte_length()->Number(); |
| 72 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; |
| 73 if (new_size > |
| 74 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { |
| 75 THROW_NEW_ERROR_RETURN_FAILURE( |
| 76 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
| 77 } |
| 78 new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size)); |
| 79 if (new_mem_start == NULL) { |
| 80 THROW_NEW_ERROR_RETURN_FAILURE( |
| 81 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); |
| 82 } |
| 83 old_buffer->set_is_external(true); |
| 84 isolate->heap()->UnregisterArrayBuffer(*old_buffer); |
| 85 // Zero initializing uninitialized memory from realloc |
| 86 memset(new_mem_start + old_size, 0, new_size - old_size); |
| 87 } |
| 88 |
| 89 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
| 90 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); |
| 91 buffer->set_is_neuterable(false); |
| 92 |
| 93 // Set new buffer to be wasm memory |
| 94 module_object->SetInternalField(kWasmMemArrayBuffer, *buffer); |
| 95 |
| 96 RUNTIME_ASSERT(wasm::UpdateWasmModuleMemory( |
| 97 module_object, old_mem_start, new_mem_start, old_size, new_size)); |
| 98 |
| 99 return *isolate->factory()->NewNumberFromUint(old_size / |
| 100 wasm::WasmModule::kPageSize); |
| 101 } |
| 102 |
| 103 } // namespace internal |
| 104 } // namespace v8 |
OLD | NEW |