Chromium Code Reviews| 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/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 1738 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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, new_mem_start; | |
|
Mircea Trofin
2016/10/04 22:01:53
could you please initialize all these and the uint
gdeepti
2016/10/05 02:59:57
Done.
| |
| 1762 uint32_t old_size, new_size; | |
| 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 old_mem_start = nullptr; | |
| 1771 old_size = 0; | |
| 1772 // TODO(gdeepti): Fix bounds check to take into account size of memtype. | |
| 1773 new_size = pages * wasm::WasmModule::kPageSize; | |
| 1774 // The code generated in the wasm compiler guarantees this precondition. | |
| 1775 DCHECK(pages <= wasm::WasmModule::kMaxMemPages); | |
| 1776 new_mem_start = | |
| 1777 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( | |
| 1778 static_cast<uint32_t>(new_size))); | |
| 1779 if (new_mem_start == NULL) { | |
|
Mircea Trofin
2016/10/04 22:01:53
nullptr?
gdeepti
2016/10/05 02:59:56
Used NewArrayBuffer method instead of allocating,
| |
| 1780 return -1; | |
| 1781 } | |
| 1782 #if DEBUG | |
| 1783 // Double check the API allocator actually zero-initialized the memory. | |
|
Mircea Trofin
2016/10/04 22:01:53
could you encapsulate this and then reuse it below
gdeepti
2016/10/05 02:59:56
Used NewArrayBuffer method instead which sets up a
| |
| 1784 for (size_t i = old_size; i < new_size; i++) { | |
| 1785 DCHECK_EQ(0, new_mem_start[i]); | |
| 1786 } | |
| 1787 #endif | |
| 1788 } else { | |
| 1789 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | |
| 1790 old_size = old_buffer->byte_length()->Number(); | |
| 1791 // If the old memory was zero-sized, we should have been in the | |
| 1792 // "undefined" case above. | |
| 1793 DCHECK_NOT_NULL(old_mem_start); | |
| 1794 DCHECK_NE(0, old_size); | |
| 1795 | |
| 1796 new_size = old_size + pages * wasm::WasmModule::kPageSize; | |
|
Mircea Trofin
2016/10/04 22:01:53
In addition to the check below, we should check th
gdeepti
2016/10/05 02:59:57
Done.
| |
| 1797 if (new_size > | |
| 1798 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { | |
| 1799 return -1; | |
| 1800 } | |
| 1801 new_mem_start = | |
| 1802 static_cast<Address>(isolate->array_buffer_allocator()->Allocate( | |
| 1803 static_cast<uint32_t>(new_size))); | |
|
Mircea Trofin
2016/10/04 22:01:53
this static_cast is unnecessary, new_size is uint3
gdeepti
2016/10/05 02:59:57
Done.
| |
| 1804 if (new_mem_start == NULL) { | |
|
Mircea Trofin
2016/10/04 22:01:53
nullptr
gdeepti
2016/10/05 02:59:57
Used NewArrayBuffer method instead of allocating,
| |
| 1805 return -1; | |
| 1806 } | |
| 1807 #if DEBUG | |
| 1808 // Double check the API allocator actually zero-initialized the memory. | |
| 1809 for (size_t i = old_size; i < new_size; i++) { | |
| 1810 DCHECK_EQ(0, new_mem_start[i]); | |
| 1811 } | |
| 1812 #endif | |
| 1813 memcpy(new_mem_start, old_mem_start, old_size); | |
|
Mircea Trofin
2016/10/04 22:01:53
we should check earlier that old_size <= new_size,
gdeepti
2016/10/05 02:59:57
Done.
| |
| 1814 } | |
| 1815 | |
| 1816 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | |
| 1817 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size); | |
| 1818 wasm::SetInstanceMemory(instance, *buffer); | |
| 1819 if (!UpdateWasmModuleMemory(instance, old_mem_start, new_mem_start, old_size, | |
| 1820 new_size)) { | |
| 1821 return -1; | |
| 1822 } | |
| 1823 return (old_size / WasmModule::kPageSize); | |
|
Mircea Trofin
2016/10/04 22:01:53
A DCHECK here that old_size % WasmModule::kPageSiz
gdeepti
2016/10/05 02:59:57
Done.
| |
| 1824 } | |
| 1825 | |
| 1759 namespace testing { | 1826 namespace testing { |
| 1760 | 1827 |
| 1761 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, | 1828 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, |
| 1762 int instance_count) { | 1829 int instance_count) { |
| 1763 CHECK_GE(instance_count, 0); | 1830 CHECK_GE(instance_count, 0); |
| 1764 DisallowHeapAllocation no_gc; | 1831 DisallowHeapAllocation no_gc; |
| 1765 WasmCompiledModule* compiled_module = | 1832 WasmCompiledModule* compiled_module = |
| 1766 WasmCompiledModule::cast(module_obj->GetInternalField(0)); | 1833 WasmCompiledModule::cast(module_obj->GetInternalField(0)); |
| 1767 CHECK_EQ( | 1834 CHECK_EQ( |
| 1768 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()), | 1835 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1803 WasmCompiledModule* compiled_module = | 1870 WasmCompiledModule* compiled_module = |
| 1804 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule)); | 1871 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule)); |
| 1805 CHECK(compiled_module->has_weak_module_object()); | 1872 CHECK(compiled_module->has_weak_module_object()); |
| 1806 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); | 1873 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); |
| 1807 } | 1874 } |
| 1808 | 1875 |
| 1809 } // namespace testing | 1876 } // namespace testing |
| 1810 } // namespace wasm | 1877 } // namespace wasm |
| 1811 } // namespace internal | 1878 } // namespace internal |
| 1812 } // namespace v8 | 1879 } // namespace v8 |
| OLD | NEW |