OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/assembler.h" | 8 #include "src/assembler.h" |
9 #include "src/conversions.h" | 9 #include "src/conversions.h" |
10 #include "src/debug/debug.h" | 10 #include "src/debug/debug.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 DCHECK(deopt_data->length() == 2); | 32 DCHECK(deopt_data->length() == 2); |
33 JSObject* module_object = JSObject::cast(deopt_data->get(0)); | 33 JSObject* module_object = JSObject::cast(deopt_data->get(0)); |
34 RUNTIME_ASSERT(!module_object->IsNull(isolate)); | 34 RUNTIME_ASSERT(!module_object->IsNull(isolate)); |
35 | 35 |
36 Address old_mem_start, new_mem_start; | 36 Address old_mem_start, new_mem_start; |
37 uint32_t old_size, new_size; | 37 uint32_t old_size, new_size; |
38 const int kWasmMemArrayBuffer = 2; | 38 const int kWasmMemArrayBuffer = 2; |
39 | 39 |
40 // Get mem buffer associated with module object | 40 // Get mem buffer associated with module object |
41 Object* obj = module_object->GetInternalField(kWasmMemArrayBuffer); | 41 Object* obj = module_object->GetInternalField(kWasmMemArrayBuffer); |
42 Handle<JSArrayBuffer> old_buffer = | |
43 Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); | |
44 | 42 |
45 if (old_buffer->byte_length()->Number() == 0) { | 43 if (obj->IsUndefined(isolate)) { |
46 // If module object does not have linear memory associated with it, | 44 // If module object does not have linear memory associated with it, |
47 // Allocate new array buffer of given size. | 45 // Allocate new array buffer of given size. |
48 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | 46 old_mem_start = nullptr; |
49 old_size = 0; | 47 old_size = 0; |
50 // TODO(gdeepti): Fix bounds check to take into account size of memtype. | 48 // TODO(gdeepti): Fix bounds check to take into account size of memtype. |
51 new_size = delta_pages * wasm::WasmModule::kPageSize; | 49 new_size = delta_pages * wasm::WasmModule::kPageSize; |
52 if (delta_pages > wasm::WasmModule::kMaxMemPages) { | 50 if (delta_pages > wasm::WasmModule::kMaxMemPages) { |
53 THROW_NEW_ERROR_RETURN_FAILURE( | 51 THROW_NEW_ERROR_RETURN_FAILURE( |
54 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | 52 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
55 } | 53 } |
56 new_mem_start = | 54 new_mem_start = |
57 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( | 55 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( |
58 static_cast<uint32_t>(new_size))); | 56 static_cast<uint32_t>(new_size))); |
59 if (new_mem_start == NULL) { | 57 if (new_mem_start == NULL) { |
60 THROW_NEW_ERROR_RETURN_FAILURE( | 58 THROW_NEW_ERROR_RETURN_FAILURE( |
61 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); | 59 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); |
62 } | 60 } |
63 #if DEBUG | 61 #if DEBUG |
64 // Double check the API allocator actually zero-initialized the memory. | 62 // Double check the API allocator actually zero-initialized the memory. |
65 for (size_t i = old_size; i < new_size; i++) { | 63 for (size_t i = old_size; i < new_size; i++) { |
66 DCHECK_EQ(0, new_mem_start[i]); | 64 DCHECK_EQ(0, new_mem_start[i]); |
67 } | 65 } |
68 #endif | 66 #endif |
69 } else { | 67 } else { |
| 68 Handle<JSArrayBuffer> old_buffer = |
| 69 Handle<JSArrayBuffer>(JSArrayBuffer::cast(obj)); |
70 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | 70 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
71 old_size = old_buffer->byte_length()->Number(); | 71 old_size = old_buffer->byte_length()->Number(); |
| 72 // If the old memory was zero-sized, we should have been in the |
| 73 // "undefined" case above. |
| 74 DCHECK_NOT_NULL(old_mem_start); |
| 75 DCHECK_NE(0, old_size); |
| 76 |
72 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; | 77 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; |
73 if (new_size > | 78 if (new_size > |
74 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { | 79 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { |
75 THROW_NEW_ERROR_RETURN_FAILURE( | 80 THROW_NEW_ERROR_RETURN_FAILURE( |
76 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | 81 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); |
77 } | 82 } |
78 new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size)); | 83 new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size)); |
79 if (new_mem_start == NULL) { | 84 if (new_mem_start == NULL) { |
80 THROW_NEW_ERROR_RETURN_FAILURE( | 85 THROW_NEW_ERROR_RETURN_FAILURE( |
81 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); | 86 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); |
(...skipping 13 matching lines...) Expand all Loading... |
95 | 100 |
96 RUNTIME_ASSERT(wasm::UpdateWasmModuleMemory( | 101 RUNTIME_ASSERT(wasm::UpdateWasmModuleMemory( |
97 module_object, old_mem_start, new_mem_start, old_size, new_size)); | 102 module_object, old_mem_start, new_mem_start, old_size, new_size)); |
98 | 103 |
99 return *isolate->factory()->NewNumberFromUint(old_size / | 104 return *isolate->factory()->NewNumberFromUint(old_size / |
100 wasm::WasmModule::kPageSize); | 105 wasm::WasmModule::kPageSize); |
101 } | 106 } |
102 | 107 |
103 } // namespace internal | 108 } // namespace internal |
104 } // namespace v8 | 109 } // namespace v8 |
OLD | NEW |