OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/assembler-inl.h" | 7 #include "src/assembler-inl.h" |
8 #include "src/base/adapters.h" | 8 #include "src/base/adapters.h" |
9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
10 #include "src/code-stubs.h" | 10 #include "src/code-stubs.h" |
(...skipping 2307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2318 Handle<JSArrayBuffer> new_buffer(instance->memory_buffer()); | 2318 Handle<JSArrayBuffer> new_buffer(instance->memory_buffer()); |
2319 uint32_t new_size = new_buffer->byte_length()->Number(); | 2319 uint32_t new_size = new_buffer->byte_length()->Number(); |
2320 DCHECK(new_size <= std::numeric_limits<uint32_t>::max()); | 2320 DCHECK(new_size <= std::numeric_limits<uint32_t>::max()); |
2321 Address new_mem_start = static_cast<Address>(new_buffer->backing_store()); | 2321 Address new_mem_start = static_cast<Address>(new_buffer->backing_store()); |
2322 DCHECK_NOT_NULL(new_mem_start); | 2322 DCHECK_NOT_NULL(new_mem_start); |
2323 Handle<FixedArray> code_table = instance->compiled_module()->code_table(); | 2323 Handle<FixedArray> code_table = instance->compiled_module()->code_table(); |
2324 RelocateMemoryReferencesInCode(code_table, old_mem_start, new_mem_start, | 2324 RelocateMemoryReferencesInCode(code_table, old_mem_start, new_mem_start, |
2325 old_size, new_size); | 2325 old_size, new_size); |
2326 } | 2326 } |
2327 | 2327 |
2328 int32_t wasm::GrowWebAssemblyMemory(Isolate* isolate, Handle<Object> receiver, | 2328 int32_t wasm::GrowWebAssemblyMemory(Isolate* isolate, |
| 2329 Handle<WasmMemoryObject> receiver, |
2329 uint32_t pages) { | 2330 uint32_t pages) { |
2330 DCHECK(WasmJs::IsWasmMemoryObject(isolate, receiver)); | 2331 DCHECK(WasmJs::IsWasmMemoryObject(isolate, receiver)); |
2331 Handle<WasmMemoryObject> memory_object = | 2332 Handle<WasmMemoryObject> memory_object = |
2332 handle(WasmMemoryObject::cast(*receiver)); | 2333 handle(WasmMemoryObject::cast(*receiver)); |
2333 Handle<WasmInstanceWrapper> instance_wrapper(memory_object->instances_link()); | |
2334 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*instance_wrapper)); | |
2335 DCHECK(instance_wrapper->has_instance()); | |
2336 Handle<WasmInstanceObject> instance = instance_wrapper->instance_object(); | |
2337 DCHECK(IsWasmInstance(*instance)); | |
2338 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | |
2339 uint32_t max_pages = GetMaxInstanceMemoryPages(isolate, instance); | |
2340 | |
2341 // Grow memory object buffer and update instances associated with it. | |
2342 MaybeHandle<JSArrayBuffer> memory_buffer = handle(memory_object->buffer()); | 2334 MaybeHandle<JSArrayBuffer> memory_buffer = handle(memory_object->buffer()); |
2343 Handle<JSArrayBuffer> old_buffer; | 2335 Handle<JSArrayBuffer> old_buffer; |
2344 uint32_t old_size = 0; | 2336 uint32_t old_size = 0; |
2345 Address old_mem_start = nullptr; | 2337 Address old_mem_start = nullptr; |
2346 if (memory_buffer.ToHandle(&old_buffer) && | 2338 if (memory_buffer.ToHandle(&old_buffer) && |
2347 old_buffer->backing_store() != nullptr) { | 2339 old_buffer->backing_store() != nullptr) { |
2348 old_size = old_buffer->byte_length()->Number(); | 2340 old_size = old_buffer->byte_length()->Number(); |
2349 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | 2341 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
2350 } | 2342 } |
2351 Handle<JSArrayBuffer> new_buffer = | 2343 // Return current size if grow by 0 |
2352 GrowMemoryBuffer(isolate, memory_buffer, pages, max_pages); | 2344 if (pages == 0) { |
2353 if (new_buffer.is_null()) return -1; | 2345 DCHECK(old_size % WasmModule::kPageSize == 0); |
2354 DCHECK(!instance_wrapper->has_previous()); | 2346 return (old_size / WasmModule::kPageSize); |
2355 SetInstanceMemory(instance, *new_buffer); | 2347 } |
2356 UncheckedUpdateInstanceMemory(isolate, instance, old_mem_start, old_size); | 2348 Handle<JSArrayBuffer> new_buffer; |
2357 while (instance_wrapper->has_next()) { | 2349 if (!memory_object->has_instances_link()) { |
2358 instance_wrapper = instance_wrapper->next_wrapper(); | 2350 // Memory object does not have an instance associated with it, just grow |
| 2351 uint32_t max_pages; |
| 2352 if (memory_object->has_maximum_pages()) { |
| 2353 max_pages = static_cast<uint32_t>(memory_object->maximum_pages()); |
| 2354 if (kV8MaxWasmMemoryPages < max_pages) return -1; |
| 2355 } else { |
| 2356 max_pages = kV8MaxWasmMemoryPages; |
| 2357 } |
| 2358 new_buffer = GrowMemoryBuffer(isolate, memory_buffer, pages, max_pages); |
| 2359 if (new_buffer.is_null()) return -1; |
| 2360 } else { |
| 2361 Handle<WasmInstanceWrapper> instance_wrapper( |
| 2362 memory_object->instances_link()); |
2359 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*instance_wrapper)); | 2363 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*instance_wrapper)); |
| 2364 DCHECK(instance_wrapper->has_instance()); |
2360 Handle<WasmInstanceObject> instance = instance_wrapper->instance_object(); | 2365 Handle<WasmInstanceObject> instance = instance_wrapper->instance_object(); |
2361 DCHECK(IsWasmInstance(*instance)); | 2366 DCHECK(IsWasmInstance(*instance)); |
| 2367 uint32_t max_pages = GetMaxInstanceMemoryPages(isolate, instance); |
| 2368 |
| 2369 // Grow memory object buffer and update instances associated with it. |
| 2370 new_buffer = GrowMemoryBuffer(isolate, memory_buffer, pages, max_pages); |
| 2371 if (new_buffer.is_null()) return -1; |
| 2372 DCHECK(!instance_wrapper->has_previous()); |
2362 SetInstanceMemory(instance, *new_buffer); | 2373 SetInstanceMemory(instance, *new_buffer); |
2363 UncheckedUpdateInstanceMemory(isolate, instance, old_mem_start, old_size); | 2374 UncheckedUpdateInstanceMemory(isolate, instance, old_mem_start, old_size); |
| 2375 while (instance_wrapper->has_next()) { |
| 2376 instance_wrapper = instance_wrapper->next_wrapper(); |
| 2377 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*instance_wrapper)); |
| 2378 Handle<WasmInstanceObject> instance = instance_wrapper->instance_object(); |
| 2379 DCHECK(IsWasmInstance(*instance)); |
| 2380 SetInstanceMemory(instance, *new_buffer); |
| 2381 UncheckedUpdateInstanceMemory(isolate, instance, old_mem_start, old_size); |
| 2382 } |
2364 } | 2383 } |
2365 memory_object->set_buffer(*new_buffer); | 2384 memory_object->set_buffer(*new_buffer); |
2366 DCHECK(old_size % WasmModule::kPageSize == 0); | 2385 DCHECK(old_size % WasmModule::kPageSize == 0); |
2367 return (old_size / WasmModule::kPageSize); | 2386 return (old_size / WasmModule::kPageSize); |
2368 } | 2387 } |
2369 | 2388 |
2370 int32_t wasm::GrowMemory(Isolate* isolate, Handle<WasmInstanceObject> instance, | 2389 int32_t wasm::GrowMemory(Isolate* isolate, Handle<WasmInstanceObject> instance, |
2371 uint32_t pages) { | 2390 uint32_t pages) { |
2372 if (!IsWasmInstance(*instance)) return -1; | 2391 if (!IsWasmInstance(*instance)) return -1; |
2373 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | 2392 if (pages == 0) return GetInstanceMemorySize(isolate, instance); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2606 | 2625 |
2607 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), | 2626 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), |
2608 NONE); | 2627 NONE); |
2609 JSObject::AddProperty(entry, kind_string, export_kind, NONE); | 2628 JSObject::AddProperty(entry, kind_string, export_kind, NONE); |
2610 | 2629 |
2611 storage->set(index, *entry); | 2630 storage->set(index, *entry); |
2612 } | 2631 } |
2613 | 2632 |
2614 return array_object; | 2633 return array_object; |
2615 } | 2634 } |
OLD | NEW |