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 byte* old_mem_start; | |
ahaas
2016/06/24 11:10:15
I think you could use {Address} as the type for ol
gdeepti
2016/06/25 00:28:41
Done.
| |
37 byte* new_mem_start; | |
38 uint32_t old_size, new_size; | |
39 const int kWasmMemArrayBuffer = 2; | |
40 | |
41 // Get mem buffer associated with module object | |
42 Object* obj = module_object->GetInternalField(kWasmMemArrayBuffer); | |
43 Handle<JSArrayBuffer> old_buffer = Handle<JSArrayBuffer>::null(); | |
ahaas
2016/06/24 11:10:15
Could you not just write?
Handle<JSArrayBuffer> ol
gdeepti
2016/06/25 00:28:41
Done.
| |
44 old_buffer = Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); | |
45 | |
46 if (old_buffer->byte_length()->Number() == 0) { | |
47 // If module object does not have linear memory associated with it, | |
48 // Allocate new array buffer of given size. | |
49 old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); | |
50 old_size = 0; | |
51 // TODO(gdeepti): Fix bounds check to take into account size of memtype. | |
52 new_size = delta_pages * wasm::WasmModule::kPageSize; | |
53 if (delta_pages > wasm::WasmModule::kMaxMemPages) { | |
54 THROW_NEW_ERROR_RETURN_FAILURE( | |
55 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | |
56 } | |
57 new_mem_start = | |
58 reinterpret_cast<byte*>(isolate->array_buffer_allocator()->Allocate( | |
59 static_cast<uint32_t>(new_size))); | |
60 if (new_mem_start == NULL) { | |
61 THROW_NEW_ERROR_RETURN_FAILURE( | |
62 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); | |
63 } | |
64 #if DEBUG | |
65 // Double check the API allocator actually zero-initialized the memory. | |
66 for (size_t i = old_size; i < new_size; i++) { | |
67 DCHECK_EQ(0, new_mem_start[i]); | |
68 } | |
69 #endif | |
70 } else { | |
71 old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); | |
72 old_size = old_buffer->byte_length()->Number(); | |
73 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; | |
74 if (new_size > | |
75 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { | |
76 THROW_NEW_ERROR_RETURN_FAILURE( | |
77 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | |
78 } | |
79 new_mem_start = reinterpret_cast<byte*>(realloc(old_mem_start, new_size)); | |
80 if (new_mem_start == NULL) { | |
81 THROW_NEW_ERROR_RETURN_FAILURE( | |
82 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); | |
83 } | |
84 old_buffer->set_is_external(true); | |
85 isolate->heap()->UnregisterArrayBuffer(*old_buffer); | |
86 // Zero initializing uninitialized memory from realloc | |
87 for (size_t i = old_size; i < new_size; i++) { | |
ahaas
2016/06/24 11:10:15
use memset to zero initialize memory?
gdeepti
2016/06/25 00:28:41
Done.
| |
88 new_mem_start[i] = 0; | |
89 } | |
90 } | |
91 | |
92 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | |
93 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); | |
94 buffer->set_is_neuterable(false); | |
95 | |
96 // Set new buffer to be wasm memory | |
97 module_object->SetInternalField(kWasmMemArrayBuffer, *buffer); | |
98 | |
99 RUNTIME_ASSERT(wasm::UpdateWasmModuleMemory( | |
100 module_object, old_mem_start, new_mem_start, old_size, new_size)); | |
101 | |
102 return *isolate->factory()->NewNumberFromUint(old_size / | |
103 wasm::WasmModule::kPageSize); | |
104 } | |
105 | |
106 } // namespace internal | |
107 } // namespace v8 | |
OLD | NEW |