Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: src/wasm/wasm-module.cc

Issue 2396473003: [wasm] Refactor GrowMemory runtime call. (Closed)
Patch Set: Fix header Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/wasm/wasm-module.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/base/atomic-utils.h" 7 #include "src/base/atomic-utils.h"
8 #include "src/code-stubs.h" 8 #include "src/code-stubs.h"
9 9
10 #include "src/macro-assembler.h" 10 #include "src/macro-assembler.h"
(...skipping 1363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 : handle(FixedArray::cast(owner->GetInternalField( 1374 : handle(FixedArray::cast(owner->GetInternalField(
1375 kWasmModuleFunctionTable))); 1375 kWasmModuleFunctionTable)));
1376 Handle<FixedArray> indirect_tables = SetupIndirectFunctionTable( 1376 Handle<FixedArray> indirect_tables = SetupIndirectFunctionTable(
1377 isolate, code_table, indirect_tables_template, to_replace); 1377 isolate, code_table, indirect_tables_template, to_replace);
1378 for (int i = 0; i < indirect_tables->length(); ++i) { 1378 for (int i = 0; i < indirect_tables->length(); ++i) {
1379 Handle<FixedArray> metadata = 1379 Handle<FixedArray> metadata =
1380 indirect_tables->GetValueChecked<FixedArray>(isolate, i); 1380 indirect_tables->GetValueChecked<FixedArray>(isolate, i);
1381 uint32_t size = Smi::cast(metadata->get(kSize))->value(); 1381 uint32_t size = Smi::cast(metadata->get(kSize))->value();
1382 Handle<FixedArray> table = 1382 Handle<FixedArray> table =
1383 metadata->GetValueChecked<FixedArray>(isolate, kTable); 1383 metadata->GetValueChecked<FixedArray>(isolate, kTable);
1384 wasm::PopulateFunctionTable(table, size, &functions); 1384 PopulateFunctionTable(table, size, &functions);
1385 } 1385 }
1386 instance->SetInternalField(kWasmModuleFunctionTable, *indirect_tables); 1386 instance->SetInternalField(kWasmModuleFunctionTable, *indirect_tables);
1387 } 1387 }
1388 } 1388 }
1389 1389
1390 //-------------------------------------------------------------------------- 1390 //--------------------------------------------------------------------------
1391 // Set up the exports object for the new instance. 1391 // Set up the exports object for the new instance.
1392 //-------------------------------------------------------------------------- 1392 //--------------------------------------------------------------------------
1393 bool mem_export = compiled_module->export_memory(); 1393 bool mem_export = compiled_module->export_memory();
1394 ModuleOrigin origin = compiled_module->origin(); 1394 ModuleOrigin origin = compiled_module->origin();
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
1749 1749
1750 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) { 1750 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) {
1751 DisallowHeapAllocation no_gc; 1751 DisallowHeapAllocation no_gc;
1752 DCHECK(IsWasmObject(*instance)); 1752 DCHECK(IsWasmObject(*instance));
1753 instance->SetInternalField(kWasmMemArrayBuffer, buffer); 1753 instance->SetInternalField(kWasmMemArrayBuffer, buffer);
1754 WasmCompiledModule* module = 1754 WasmCompiledModule* module =
1755 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule)); 1755 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule));
1756 module->set_ptr_to_heap(buffer); 1756 module->set_ptr_to_heap(buffer);
1757 } 1757 }
1758 1758
1759 int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
1760 uint32_t pages) {
1761 Address old_mem_start = nullptr;
1762 uint32_t old_size = 0, new_size = 0;
1763
1764 MaybeHandle<JSArrayBuffer> maybe_mem_buffer =
1765 GetInstanceMemory(isolate, instance);
1766 Handle<JSArrayBuffer> old_buffer;
1767 if (!maybe_mem_buffer.ToHandle(&old_buffer)) {
1768 // If module object does not have linear memory associated with it,
1769 // Allocate new array buffer of given size.
1770 // TODO(gdeepti): Fix bounds check to take into account size of memtype.
1771 new_size = pages * WasmModule::kPageSize;
1772 // The code generated in the wasm compiler guarantees this precondition.
1773 DCHECK(pages <= WasmModule::kMaxMemPages);
1774 } else {
1775 old_mem_start = static_cast<Address>(old_buffer->backing_store());
1776 old_size = old_buffer->byte_length()->Number();
1777 // If the old memory was zero-sized, we should have been in the
1778 // "undefined" case above.
1779 DCHECK_NOT_NULL(old_mem_start);
1780 DCHECK_NE(0, old_size);
1781 DCHECK(old_size + pages * WasmModule::kPageSize <=
1782 std::numeric_limits<uint32_t>::max());
1783 new_size = old_size + pages * WasmModule::kPageSize;
1784 }
1785
1786 if (new_size <= old_size ||
1787 WasmModule::kMaxMemPages * WasmModule::kPageSize <= new_size) {
1788 return -1;
1789 }
1790 Handle<JSArrayBuffer> buffer = NewArrayBuffer(isolate, new_size);
1791 if (buffer.is_null()) return -1;
1792 Address new_mem_start = static_cast<Address>(buffer->backing_store());
1793 if (old_size != 0) {
1794 memcpy(new_mem_start, old_mem_start, old_size);
1795 }
1796 SetInstanceMemory(instance, *buffer);
1797 if (!UpdateWasmModuleMemory(instance, old_mem_start, new_mem_start, old_size,
1798 new_size)) {
1799 return -1;
1800 }
1801 DCHECK(old_size % WasmModule::kPageSize == 0);
1802 return (old_size / WasmModule::kPageSize);
1803 }
1804
1759 namespace testing { 1805 namespace testing {
1760 1806
1761 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, 1807 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj,
1762 int instance_count) { 1808 int instance_count) {
1763 CHECK_GE(instance_count, 0); 1809 CHECK_GE(instance_count, 0);
1764 DisallowHeapAllocation no_gc; 1810 DisallowHeapAllocation no_gc;
1765 WasmCompiledModule* compiled_module = 1811 WasmCompiledModule* compiled_module =
1766 WasmCompiledModule::cast(module_obj->GetInternalField(0)); 1812 WasmCompiledModule::cast(module_obj->GetInternalField(0));
1767 CHECK_EQ( 1813 CHECK_EQ(
1768 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()), 1814 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1803 WasmCompiledModule* compiled_module = 1849 WasmCompiledModule* compiled_module =
1804 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule)); 1850 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule));
1805 CHECK(compiled_module->has_weak_module_object()); 1851 CHECK(compiled_module->has_weak_module_object());
1806 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); 1852 CHECK(compiled_module->ptr_to_weak_module_object()->cleared());
1807 } 1853 }
1808 1854
1809 } // namespace testing 1855 } // namespace testing
1810 } // namespace wasm 1856 } // namespace wasm
1811 } // namespace internal 1857 } // namespace internal
1812 } // namespace v8 1858 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698