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 // TODO(gdeepti): This should be a weak list of instance objects | |
1430 // for instances that share memory. | |
1431 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), | |
1432 isolate_); | |
1433 static const int kWasmMemoryInstanceIndex = 2; | |
bradnelson
2016/10/17 22:13:25
Shouldn't this live in wasm-module.h ?
gdeepti
2016/10/18 02:34:17
Refactored this and GetMaxInstanceMemorySize so th
| |
1434 if (!memory_object->IsUndefined(isolate_)) { | |
1435 JSObject::cast(*memory_object) | |
1436 ->SetInternalField(kWasmMemoryInstanceIndex, *instance); | |
1437 } | |
1429 | 1438 |
1430 //-------------------------------------------------------------------------- | 1439 //-------------------------------------------------------------------------- |
1431 // Run the start function if one was specified. | 1440 // Run the start function if one was specified. |
1432 //-------------------------------------------------------------------------- | 1441 //-------------------------------------------------------------------------- |
1433 if (compiled_module_->has_startup_function()) { | 1442 if (compiled_module_->has_startup_function()) { |
1434 Handle<FixedArray> startup_data = compiled_module_->startup_function(); | 1443 Handle<FixedArray> startup_data = compiled_module_->startup_function(); |
1435 HandleScope scope(isolate_); | 1444 HandleScope scope(isolate_); |
1436 int32_t start_index = | 1445 int32_t start_index = |
1437 startup_data->GetValueChecked<Smi>(isolate_, kExportIndex)->value(); | 1446 startup_data->GetValueChecked<Smi>(isolate_, kExportIndex)->value(); |
1438 Handle<Code> startup_code = | 1447 Handle<Code> startup_code = |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2198 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2207 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
2199 GetInstanceMemory(isolate, instance); | 2208 GetInstanceMemory(isolate, instance); |
2200 Handle<JSArrayBuffer> buffer; | 2209 Handle<JSArrayBuffer> buffer; |
2201 if (!maybe_mem_buffer.ToHandle(&buffer)) { | 2210 if (!maybe_mem_buffer.ToHandle(&buffer)) { |
2202 return 0; | 2211 return 0; |
2203 } else { | 2212 } else { |
2204 return buffer->byte_length()->Number() / WasmModule::kPageSize; | 2213 return buffer->byte_length()->Number() / WasmModule::kPageSize; |
2205 } | 2214 } |
2206 } | 2215 } |
2207 | 2216 |
2217 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { | |
2218 static const int kWasmMemoryMaximumIndex = 1; | |
2219 uint32_t max_pages = WasmModule::kMaxMemPages; | |
2220 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), | |
2221 isolate); | |
2222 if (memory_object->IsUndefined(isolate)) return max_pages; | |
2223 Object* max_mem = | |
2224 JSObject::cast(*memory_object)->GetInternalField(kWasmMemoryMaximumIndex); | |
2225 if (max_mem->IsUndefined(isolate)) return max_pages; | |
2226 max_pages = Smi::cast(max_mem)->value(); | |
2227 DCHECK(max_pages <= WasmModule::kMaxMemPages); | |
2228 return max_pages; | |
2229 } | |
2230 | |
2208 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, | 2231 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, |
2209 uint32_t pages) { | 2232 uint32_t pages) { |
2210 if (!IsWasmObject(*instance)) return false; | 2233 if (!IsWasmObject(*instance)) return -1; |
2211 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | 2234 if (pages == 0) return GetInstanceMemorySize(isolate, instance); |
bradnelson
2016/10/17 22:23:26
Move max_pages = ... to above and reuse max_pages
gdeepti
2016/10/18 02:34:17
GetInstanceMemorySize, GetMaxInstanceMemorySize ar
| |
2235 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); | |
2236 if (WasmModule::kMaxMemPages < max_pages) return -1; | |
2212 | 2237 |
2213 Address old_mem_start = nullptr; | 2238 Address old_mem_start = nullptr; |
2214 uint32_t old_size = 0, new_size = 0; | 2239 uint32_t old_size = 0, new_size = 0; |
2215 | 2240 |
2216 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2241 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
2217 GetInstanceMemory(isolate, instance); | 2242 GetInstanceMemory(isolate, instance); |
2218 Handle<JSArrayBuffer> old_buffer; | 2243 Handle<JSArrayBuffer> old_buffer; |
2219 if (!maybe_mem_buffer.ToHandle(&old_buffer)) { | 2244 if (!maybe_mem_buffer.ToHandle(&old_buffer) || |
2245 old_buffer->backing_store() == nullptr) { | |
2220 // If module object does not have linear memory associated with it, | 2246 // If module object does not have linear memory associated with it, |
2221 // Allocate new array buffer of given size. | 2247 // 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; | 2248 new_size = pages * WasmModule::kPageSize; |
2224 // The code generated in the wasm compiler guarantees this precondition. | 2249 if (max_pages < pages) return -1; |
2225 DCHECK(pages <= WasmModule::kMaxMemPages); | |
2226 } else { | 2250 } else { |
2227 old_mem_start = static_cast<Address>(old_buffer->backing_store()); | 2251 old_mem_start = static_cast<Address>(old_buffer->backing_store()); |
2228 old_size = old_buffer->byte_length()->Number(); | 2252 old_size = old_buffer->byte_length()->Number(); |
2229 // If the old memory was zero-sized, we should have been in the | 2253 // If the old memory was zero-sized, we should have been in the |
2230 // "undefined" case above. | 2254 // "undefined" case above. |
2231 DCHECK_NOT_NULL(old_mem_start); | 2255 DCHECK_NOT_NULL(old_mem_start); |
2232 DCHECK_NE(0, old_size); | 2256 DCHECK_NE(0, old_size); |
2233 DCHECK(old_size + pages * WasmModule::kPageSize <= | 2257 DCHECK(old_size + pages * WasmModule::kPageSize <= |
2234 std::numeric_limits<uint32_t>::max()); | 2258 std::numeric_limits<uint32_t>::max()); |
2235 new_size = old_size + pages * WasmModule::kPageSize; | 2259 new_size = old_size + pages * WasmModule::kPageSize; |
2236 } | 2260 } |
2237 | 2261 |
2238 if (new_size <= old_size || | 2262 if (new_size <= old_size || max_pages * WasmModule::kPageSize < new_size) { |
2239 WasmModule::kMaxMemPages * WasmModule::kPageSize <= new_size) { | |
2240 return -1; | 2263 return -1; |
2241 } | 2264 } |
2242 Handle<JSArrayBuffer> buffer = NewArrayBuffer(isolate, new_size); | 2265 Handle<JSArrayBuffer> buffer = NewArrayBuffer(isolate, new_size); |
2243 if (buffer.is_null()) return -1; | 2266 if (buffer.is_null()) return -1; |
2244 Address new_mem_start = static_cast<Address>(buffer->backing_store()); | 2267 Address new_mem_start = static_cast<Address>(buffer->backing_store()); |
2245 if (old_size != 0) { | 2268 if (old_size != 0) { |
2246 memcpy(new_mem_start, old_mem_start, old_size); | 2269 memcpy(new_mem_start, old_mem_start, old_size); |
2247 } | 2270 } |
2248 SetInstanceMemory(instance, *buffer); | 2271 SetInstanceMemory(instance, *buffer); |
2249 RelocateInstanceCode(instance, old_mem_start, new_mem_start, old_size, | 2272 RelocateInstanceCode(instance, old_mem_start, new_mem_start, old_size, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2294 } | 2317 } |
2295 | 2318 |
2296 void testing::ValidateOrphanedInstance(Isolate* isolate, | 2319 void testing::ValidateOrphanedInstance(Isolate* isolate, |
2297 Handle<JSObject> instance) { | 2320 Handle<JSObject> instance) { |
2298 DisallowHeapAllocation no_gc; | 2321 DisallowHeapAllocation no_gc; |
2299 CHECK(IsWasmObject(*instance)); | 2322 CHECK(IsWasmObject(*instance)); |
2300 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); | 2323 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
2301 CHECK(compiled_module->has_weak_module_object()); | 2324 CHECK(compiled_module->has_weak_module_object()); |
2302 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); | 2325 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); |
2303 } | 2326 } |
OLD | NEW |