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 1408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1419 module_object_->SetInternalField(0, *compiled_module_); | 1419 module_object_->SetInternalField(0, *compiled_module_); |
1420 instance->SetInternalField(kWasmCompiledModule, *compiled_module_); | 1420 instance->SetInternalField(kWasmCompiledModule, *compiled_module_); |
1421 compiled_module_->set_weak_owning_instance(link_to_owning_instance); | 1421 compiled_module_->set_weak_owning_instance(link_to_owning_instance); |
1422 GlobalHandles::MakeWeak(global_handle.location(), | 1422 GlobalHandles::MakeWeak(global_handle.location(), |
1423 global_handle.location(), &InstanceFinalizer, | 1423 global_handle.location(), &InstanceFinalizer, |
1424 v8::WeakCallbackType::kFinalizer); | 1424 v8::WeakCallbackType::kFinalizer); |
1425 } | 1425 } |
1426 } | 1426 } |
1427 | 1427 |
1428 DCHECK(wasm::IsWasmObject(*instance)); | 1428 DCHECK(wasm::IsWasmObject(*instance)); |
| 1429 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), |
| 1430 isolate_); |
| 1431 WasmJs::SetWasmMemoryInstance(isolate_, memory_object, instance); |
1429 | 1432 |
1430 //-------------------------------------------------------------------------- | 1433 //-------------------------------------------------------------------------- |
1431 // Run the start function if one was specified. | 1434 // Run the start function if one was specified. |
1432 //-------------------------------------------------------------------------- | 1435 //-------------------------------------------------------------------------- |
1433 if (compiled_module_->has_startup_function()) { | 1436 if (compiled_module_->has_startup_function()) { |
1434 Handle<FixedArray> startup_data = compiled_module_->startup_function(); | 1437 Handle<FixedArray> startup_data = compiled_module_->startup_function(); |
1435 HandleScope scope(isolate_); | 1438 HandleScope scope(isolate_); |
1436 int32_t start_index = | 1439 int32_t start_index = |
1437 startup_data->GetValueChecked<Smi>(isolate_, kExportIndex)->value(); | 1440 startup_data->GetValueChecked<Smi>(isolate_, kExportIndex)->value(); |
1438 Handle<Code> startup_code = | 1441 Handle<Code> startup_code = |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2198 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2201 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
2199 GetInstanceMemory(isolate, instance); | 2202 GetInstanceMemory(isolate, instance); |
2200 Handle<JSArrayBuffer> buffer; | 2203 Handle<JSArrayBuffer> buffer; |
2201 if (!maybe_mem_buffer.ToHandle(&buffer)) { | 2204 if (!maybe_mem_buffer.ToHandle(&buffer)) { |
2202 return 0; | 2205 return 0; |
2203 } else { | 2206 } else { |
2204 return buffer->byte_length()->Number() / WasmModule::kPageSize; | 2207 return buffer->byte_length()->Number() / WasmModule::kPageSize; |
2205 } | 2208 } |
2206 } | 2209 } |
2207 | 2210 |
| 2211 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { |
| 2212 uint32_t max_pages = WasmModule::kMaxMemPages; |
| 2213 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), |
| 2214 isolate); |
| 2215 if (memory_object->IsUndefined(isolate)) return max_pages; |
| 2216 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); |
| 2217 } |
| 2218 |
2208 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, | 2219 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, |
2209 uint32_t pages) { | 2220 uint32_t pages) { |
2210 if (!IsWasmObject(*instance)) return false; | 2221 if (!IsWasmObject(*instance)) return -1; |
2211 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | 2222 if (pages == 0) return GetInstanceMemorySize(isolate, instance); |
| 2223 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); |
| 2224 if (WasmModule::kMaxMemPages < max_pages) return -1; |
2212 | 2225 |
2213 Address old_mem_start = nullptr; | 2226 Address old_mem_start = nullptr; |
2214 uint32_t old_size = 0, new_size = 0; | 2227 uint32_t old_size = 0, new_size = 0; |
2215 | 2228 |
2216 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2229 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
2217 GetInstanceMemory(isolate, instance); | 2230 GetInstanceMemory(isolate, instance); |
2218 Handle<JSArrayBuffer> old_buffer; | 2231 Handle<JSArrayBuffer> old_buffer; |
2219 if (!maybe_mem_buffer.ToHandle(&old_buffer)) { | 2232 if (!maybe_mem_buffer.ToHandle(&old_buffer) || |
| 2233 old_buffer->backing_store() == nullptr) { |
2220 // If module object does not have linear memory associated with it, | 2234 // If module object does not have linear memory associated with it, |
2221 // Allocate new array buffer of given size. | 2235 // Allocate new array buffer of given size. |
2222 // TODO(gdeepti): Fix bounds check to take into account size of memtype. | |
2223 new_size = pages * WasmModule::kPageSize; | 2236 new_size = pages * WasmModule::kPageSize; |
2224 // The code generated in the wasm compiler guarantees this precondition. | 2237 if (max_pages < pages) return -1; |
2225 DCHECK(pages <= WasmModule::kMaxMemPages); | |
2226 } else { | 2238 } else { |
2227 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | 2239 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
2228 old_size = old_buffer->byte_length()->Number(); | 2240 old_size = old_buffer->byte_length()->Number(); |
2229 // If the old memory was zero-sized, we should have been in the | 2241 // If the old memory was zero-sized, we should have been in the |
2230 // "undefined" case above. | 2242 // "undefined" case above. |
2231 DCHECK_NOT_NULL(old_mem_start); | 2243 DCHECK_NOT_NULL(old_mem_start); |
2232 DCHECK_NE(0, old_size); | |
2233 DCHECK(old_size + pages * WasmModule::kPageSize <= | 2244 DCHECK(old_size + pages * WasmModule::kPageSize <= |
2234 std::numeric_limits<uint32_t>::max()); | 2245 std::numeric_limits<uint32_t>::max()); |
2235 new_size = old_size + pages * WasmModule::kPageSize; | 2246 new_size = old_size + pages * WasmModule::kPageSize; |
2236 } | 2247 } |
2237 | 2248 |
2238 if (new_size <= old_size || | 2249 if (new_size <= old_size || max_pages * WasmModule::kPageSize < new_size) { |
2239 WasmModule::kMaxMemPages * WasmModule::kPageSize <= new_size) { | |
2240 return -1; | 2250 return -1; |
2241 } | 2251 } |
2242 Handle<JSArrayBuffer> buffer = NewArrayBuffer(isolate, new_size); | 2252 Handle<JSArrayBuffer> buffer = NewArrayBuffer(isolate, new_size); |
2243 if (buffer.is_null()) return -1; | 2253 if (buffer.is_null()) return -1; |
2244 Address new_mem_start = static_cast<Address>(buffer->backing_store()); | 2254 Address new_mem_start = static_cast<Address>(buffer->backing_store()); |
2245 if (old_size != 0) { | 2255 if (old_size != 0) { |
2246 memcpy(new_mem_start, old_mem_start, old_size); | 2256 memcpy(new_mem_start, old_mem_start, old_size); |
2247 } | 2257 } |
2248 SetInstanceMemory(instance, *buffer); | 2258 SetInstanceMemory(instance, *buffer); |
2249 RelocateInstanceCode(instance, old_mem_start, new_mem_start, old_size, | 2259 RelocateInstanceCode(instance, old_mem_start, new_mem_start, old_size, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2294 } | 2304 } |
2295 | 2305 |
2296 void testing::ValidateOrphanedInstance(Isolate* isolate, | 2306 void testing::ValidateOrphanedInstance(Isolate* isolate, |
2297 Handle<JSObject> instance) { | 2307 Handle<JSObject> instance) { |
2298 DisallowHeapAllocation no_gc; | 2308 DisallowHeapAllocation no_gc; |
2299 CHECK(IsWasmObject(*instance)); | 2309 CHECK(IsWasmObject(*instance)); |
2300 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); | 2310 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
2301 CHECK(compiled_module->has_weak_module_object()); | 2311 CHECK(compiled_module->has_weak_module_object()); |
2302 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); | 2312 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); |
2303 } | 2313 } |
OLD | NEW |