diff --git a/src/wasm/wasm-code-manager.cc b/src/wasm/wasm-code-manager.cc index b42cf9e59a..66a600db2b 100644 --- a/src/wasm/wasm-code-manager.cc +++ b/src/wasm/wasm-code-manager.cc @@ -661,8 +661,6 @@ Address NativeModule::AllocateForCode(size_t size) { wasm_code_manager_->TryAllocate(size, reinterpret_cast(hint))); VirtualMemory& new_mem = owned_code_space_.back(); if (!new_mem.IsReserved()) return kNullAddress; - base::LockGuard lock( - &wasm_code_manager_->native_modules_mutex_); wasm_code_manager_->AssignRanges(new_mem.address(), new_mem.end(), this); free_code_space_.Merge({new_mem.address(), new_mem.end()}); @@ -810,9 +808,17 @@ bool WasmCodeManager::Commit(Address start, size_t size) { void WasmCodeManager::AssignRanges(Address start, Address end, NativeModule* native_module) { + base::LockGuard lock(&native_modules_mutex_); lookup_map_.insert(std::make_pair(start, std::make_pair(end, native_module))); } +void WasmCodeManager::AssignRangesAndAddModule(Address start, Address end, + NativeModule* native_module) { + base::LockGuard lock(&native_modules_mutex_); + lookup_map_.insert(std::make_pair(start, std::make_pair(end, native_module))); + native_modules_.emplace(native_module); +} + VirtualMemory WasmCodeManager::TryAllocate(size_t size, void* hint) { v8::PageAllocator* page_allocator = GetPlatformPageAllocator(); DCHECK_GT(size, 0); @@ -929,9 +935,7 @@ std::unique_ptr WasmCodeManager::NewNativeModule( std::move(module), env)); TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start, size); - base::LockGuard lock(&native_modules_mutex_); - AssignRanges(start, end, ret.get()); - native_modules_.emplace(ret.get()); + AssignRangesAndAddModule(start, end, ret.get()); return ret; } diff --git a/src/wasm/wasm-code-manager.h b/src/wasm/wasm-code-manager.h index 96f062f715..f9b9eb295f 100644 --- a/src/wasm/wasm-code-manager.h +++ b/src/wasm/wasm-code-manager.h @@ -469,13 +469,21 @@ class V8_EXPORT_PRIVATE WasmCodeManager final { void FreeNativeModule(NativeModule*); void Free(VirtualMemory* mem); void AssignRanges(Address start, Address end, NativeModule*); + void AssignRangesAndAddModule(Address start, Address end, NativeModule*); bool ShouldForceCriticalMemoryPressureNotification(); WasmMemoryTracker* const memory_tracker_; + std::atomic remaining_uncommitted_code_space_; mutable base::Mutex native_modules_mutex_; + + ////////////////////////////////////////////////////////////////////////////// + // Protected by {native_modules_mutex_}: + std::map> lookup_map_; std::unordered_set native_modules_; - std::atomic remaining_uncommitted_code_space_; + + // End of fields protected by {native_modules_mutex_}. + ////////////////////////////////////////////////////////////////////////////// DISALLOW_COPY_AND_ASSIGN(WasmCodeManager); };