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 <stdlib.h> | |
6 #include "src/debug/debug.h" | |
7 #include "src/disasm.h" | |
8 #include "src/disassembler.h" | |
9 | |
10 #include "src/runtime/runtime-utils.h" | |
11 | |
12 #include "src/arguments.h" | |
13 #include "src/assembler.h" | |
14 #include "src/base/macros.h" | |
15 #include "src/conversions.h" | |
16 #include "src/factory.h" | |
17 #include "src/objects-inl.h" | |
18 #include "src/wasm/wasm-module.h" | |
19 | |
20 namespace v8 { | |
21 namespace internal { | |
22 | |
23 RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { | |
24 HandleScope scope(isolate); | |
25 DCHECK_EQ(2, args.length()); | |
26 CONVERT_INT32_ARG_CHECKED(delta_pages, 0); | |
27 CONVERT_ARG_CHECKED(JSObject, module_object, 1); | |
28 RUNTIME_ASSERT(!module_object->IsNull()); | |
29 | |
30 byte* old_mem_start; | |
31 byte* new_mem_start; | |
32 uint32_t old_size, new_size; | |
33 | |
34 // Get mem buffer and its size associated with the module js_object | |
35 Object* obj = module_object->GetInternalField(2); | |
36 Handle<JSArrayBuffer> old_buffer = Handle<JSArrayBuffer>::null(); | |
37 old_buffer = Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); | |
38 | |
39 if (old_buffer->byte_length()->Number() == 0) { | |
40 // If module object does not have linear memory associated with it, | |
41 // Allocate new array buffer of given size. | |
42 old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); | |
43 old_size = 0; | |
44 // TODO(gdeepti): Figure out how to update new size correctly here. | |
45 new_size = delta_pages * wasm::WasmModule::kPageSize; | |
46 if (delta_pages > wasm::WasmModule::kMaxMemPages) { | |
47 THROW_NEW_ERROR_RETURN_FAILURE( | |
48 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | |
49 } | |
50 new_mem_start = | |
51 reinterpret_cast<byte*>(isolate->array_buffer_allocator()->Allocate( | |
52 static_cast<int>(new_size))); | |
53 RUNTIME_ASSERT(new_mem_start != NULL); | |
54 #if DEBUG | |
55 // Double check the API allocator actually zero-initialized the memory. | |
56 for (size_t i = old_size; i < new_size; i++) { | |
57 DCHECK_EQ(0, new_mem_start[i]); | |
58 } | |
59 #endif | |
60 } else { | |
61 old_mem_start = reinterpret_cast<byte*>(old_buffer->backing_store()); | |
62 old_size = old_buffer->byte_length()->Number(); | |
63 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; | |
64 if (new_size > | |
65 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { | |
66 THROW_NEW_ERROR_RETURN_FAILURE( | |
67 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | |
68 } | |
69 new_mem_start = reinterpret_cast<byte*>(realloc(old_mem_start, new_size)); | |
70 RUNTIME_ASSERT(new_mem_start != NULL); | |
71 old_buffer->set_is_external(true); | |
72 isolate->heap()->UnregisterArrayBuffer(*old_buffer); | |
73 // Zero initializinf uninitialized memory from realloc | |
titzer
2016/06/10 09:38:13
s/initializinf/initializing
gdeepti
2016/06/10 22:39:57
Done.
| |
74 for (size_t i = old_size; i < new_size; i++) { | |
75 new_mem_start[i] = 0; | |
76 } | |
77 } | |
78 | |
79 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | |
80 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); | |
81 buffer->set_is_neuterable(false); | |
82 | |
83 // Set new buffer to be wasm memory | |
84 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.
| |
85 | |
86 // Get code table associated with the module js_object | |
87 obj = module_object->GetInternalField(1); | |
88 Handle<FixedArray> code_table; | |
89 code_table = Handle<FixedArray>(FixedArray::cast(obj)); | |
90 | |
91 // Iterate through the code objects in the code table and update relocation | |
92 // information | |
93 for (int i = 0; i < code_table->length(); i++) { | |
94 Handle<Code> code; | |
95 obj = code_table->get(i); | |
96 code = Handle<Code>(Code::cast(obj)); | |
97 | |
98 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | | |
99 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | |
100 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
| |
101 RelocInfo::Mode mode = it.rinfo()->rmode(); | |
102 if (RelocInfo::IsWasmMemoryReference(mode) || | |
103 RelocInfo::IsWasmMemorySizeReference(mode)) { | |
104 it.rinfo()->update_wasm_memory_reference( | |
105 reinterpret_cast<Address>(old_mem_start), | |
106 reinterpret_cast<Address>(new_mem_start), old_size, new_size); | |
107 } | |
108 } | |
109 } | |
110 | |
111 return *isolate->factory()->NewNumberFromInt(old_size / | |
112 wasm::WasmModule::kPageSize); | |
113 } | |
114 | |
115 } // namespace internal | |
116 } // namespace v8 | |
OLD | NEW |