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/compiler/wasm-compiler.h" | 9 #include "src/compiler/wasm-compiler.h" |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 isolate); | 51 isolate); |
52 | 52 |
53 if (obj->IsUndefined(isolate)) { | 53 if (obj->IsUndefined(isolate)) { |
54 // If module object does not have linear memory associated with it, | 54 // If module object does not have linear memory associated with it, |
55 // Allocate new array buffer of given size. | 55 // Allocate new array buffer of given size. |
56 old_mem_start = nullptr; | 56 old_mem_start = nullptr; |
57 old_size = 0; | 57 old_size = 0; |
58 // TODO(gdeepti): Fix bounds check to take into account size of memtype. | 58 // TODO(gdeepti): Fix bounds check to take into account size of memtype. |
59 new_size = delta_pages * wasm::WasmModule::kPageSize; | 59 new_size = delta_pages * wasm::WasmModule::kPageSize; |
60 if (delta_pages > wasm::WasmModule::kMaxMemPages) { | 60 if (delta_pages > wasm::WasmModule::kMaxMemPages) { |
61 THROW_NEW_ERROR_RETURN_FAILURE( | 61 return *isolate->factory()->NewNumberFromInt(-1); |
62 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | |
63 } | 62 } |
64 new_mem_start = | 63 new_mem_start = |
65 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( | 64 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( |
66 static_cast<uint32_t>(new_size))); | 65 static_cast<uint32_t>(new_size))); |
67 if (new_mem_start == NULL) { | 66 if (new_mem_start == NULL) { |
68 THROW_NEW_ERROR_RETURN_FAILURE( | 67 return *isolate->factory()->NewNumberFromInt(-1); |
69 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); | |
70 } | 68 } |
71 #if DEBUG | 69 #if DEBUG |
72 // Double check the API allocator actually zero-initialized the memory. | 70 // Double check the API allocator actually zero-initialized the memory. |
73 for (size_t i = old_size; i < new_size; i++) { | 71 for (size_t i = old_size; i < new_size; i++) { |
74 DCHECK_EQ(0, new_mem_start[i]); | 72 DCHECK_EQ(0, new_mem_start[i]); |
75 } | 73 } |
76 #endif | 74 #endif |
77 } else { | 75 } else { |
78 Handle<JSArrayBuffer> old_buffer = Handle<JSArrayBuffer>::cast(obj); | 76 Handle<JSArrayBuffer> old_buffer = Handle<JSArrayBuffer>::cast(obj); |
79 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | 77 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
80 old_size = old_buffer->byte_length()->Number(); | 78 old_size = old_buffer->byte_length()->Number(); |
81 // If the old memory was zero-sized, we should have been in the | 79 // If the old memory was zero-sized, we should have been in the |
82 // "undefined" case above. | 80 // "undefined" case above. |
83 DCHECK_NOT_NULL(old_mem_start); | 81 DCHECK_NOT_NULL(old_mem_start); |
84 DCHECK_NE(0, old_size); | 82 DCHECK_NE(0, old_size); |
85 | 83 |
86 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; | 84 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; |
87 if (new_size > | 85 if (new_size > |
88 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { | 86 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { |
89 THROW_NEW_ERROR_RETURN_FAILURE( | 87 return *isolate->factory()->NewNumberFromInt(-1); |
90 isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds)); | |
91 } | 88 } |
92 new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size)); | 89 new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size)); |
93 if (new_mem_start == NULL) { | 90 if (new_mem_start == NULL) { |
94 THROW_NEW_ERROR_RETURN_FAILURE( | 91 return *isolate->factory()->NewNumberFromInt(-1); |
95 isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail)); | |
96 } | 92 } |
97 old_buffer->set_is_external(true); | 93 old_buffer->set_is_external(true); |
98 isolate->heap()->UnregisterArrayBuffer(*old_buffer); | 94 isolate->heap()->UnregisterArrayBuffer(*old_buffer); |
99 // Zero initializing uninitialized memory from realloc | 95 // Zero initializing uninitialized memory from realloc |
100 memset(new_mem_start + old_size, 0, new_size - old_size); | 96 memset(new_mem_start + old_size, 0, new_size - old_size); |
101 } | 97 } |
102 | 98 |
103 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 99 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
104 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); | 100 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); |
105 buffer->set_is_neuterable(false); | 101 buffer->set_is_neuterable(false); |
106 | 102 |
107 // Set new buffer to be wasm memory | 103 // Set new buffer to be wasm memory |
108 module_object->SetInternalField(kWasmMemArrayBuffer, *buffer); | 104 module_object->SetInternalField(kWasmMemArrayBuffer, *buffer); |
109 | 105 |
110 CHECK(wasm::UpdateWasmModuleMemory(module_object, old_mem_start, | 106 CHECK(wasm::UpdateWasmModuleMemory(module_object, old_mem_start, |
111 new_mem_start, old_size, new_size)); | 107 new_mem_start, old_size, new_size)); |
112 | 108 |
113 return *isolate->factory()->NewNumberFromUint(old_size / | 109 return *isolate->factory()->NewNumberFromInt(old_size / |
114 wasm::WasmModule::kPageSize); | 110 wasm::WasmModule::kPageSize); |
115 } | 111 } |
116 | 112 |
117 RUNTIME_FUNCTION(Runtime_JITSingleFunction) { | 113 RUNTIME_FUNCTION(Runtime_JITSingleFunction) { |
118 const int fixed_args = 6; | 114 const int fixed_args = 6; |
119 | 115 |
120 HandleScope scope(isolate); | 116 HandleScope scope(isolate); |
121 DCHECK_LE(fixed_args, args.length()); | 117 DCHECK_LE(fixed_args, args.length()); |
122 CONVERT_SMI_ARG_CHECKED(base, 0); | 118 CONVERT_SMI_ARG_CHECKED(base, 0); |
123 CONVERT_SMI_ARG_CHECKED(length, 1); | 119 CONVERT_SMI_ARG_CHECKED(length, 1); |
124 CONVERT_SMI_ARG_CHECKED(index, 2); | 120 CONVERT_SMI_ARG_CHECKED(index, 2); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 return isolate->heap()->undefined_value(); | 181 return isolate->heap()->undefined_value(); |
186 } | 182 } |
187 | 183 |
188 function_table->set(index, Smi::FromInt(sig_index)); | 184 function_table->set(index, Smi::FromInt(sig_index)); |
189 function_table->set(index + function_table->length() / 2, *code); | 185 function_table->set(index + function_table->length() / 2, *code); |
190 | 186 |
191 return isolate->heap()->undefined_value(); | 187 return isolate->heap()->undefined_value(); |
192 } | 188 } |
193 } // namespace internal | 189 } // namespace internal |
194 } // namespace v8 | 190 } // namespace v8 |
OLD | NEW |