From 05e3335c27a477492fee6d031654dd3152f722b6 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 6 Apr 2025 21:10:38 -0400 Subject: [SharedCache] Add mutex to guard `m_namedSymbols` It is modified on a worker thread so if a user tries to call `GetSymbolWithName` after view init but before the processing on the worker thread finishes, there would have been issues! --- view/sharedcache/core/SharedCache.cpp | 5 ++++- view/sharedcache/core/SharedCache.h | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'view/sharedcache') diff --git a/view/sharedcache/core/SharedCache.cpp b/view/sharedcache/core/SharedCache.cpp index 936dca4d..e468b559 100644 --- a/view/sharedcache/core/SharedCache.cpp +++ b/view/sharedcache/core/SharedCache.cpp @@ -149,6 +149,7 @@ SharedCache::SharedCache(uint64_t addressSize) { m_addressSize = addressSize; m_vm = std::make_shared(); + m_namedSymMutex = std::make_unique(); } @@ -437,6 +438,7 @@ void SharedCache::ProcessEntrySlideInfo(const CacheEntry& entry) const void SharedCache::ProcessSymbols() { + std::unique_lock lock(*m_namedSymMutex); // Populate the named symbols from the regular symbols map. m_namedSymbols.reserve(m_symbols.size()); for (const auto& [address, symbol] : m_symbols) @@ -520,8 +522,9 @@ std::optional SharedCache::GetSymbolAt(uint64_t address) const return it->second; } -std::optional SharedCache::GetSymbolWithName(const std::string& name) const +std::optional SharedCache::GetSymbolWithName(const std::string& name) { + std::shared_lock lock(*m_namedSymMutex); const auto it = m_namedSymbols.find(name); if (it == m_namedSymbols.end()) return std::nullopt; diff --git a/view/sharedcache/core/SharedCache.h b/view/sharedcache/core/SharedCache.h index ff329ecf..3211b90c 100644 --- a/view/sharedcache/core/SharedCache.h +++ b/view/sharedcache/core/SharedCache.h @@ -203,6 +203,9 @@ class SharedCache // Quickly lookup a symbol by name, populated by `FinalizeSymbols`. // `m_namedSymbols` is modified in a worker thread spawned by view init so we must not get a symbol until its populated. std::unordered_map m_namedSymbols {}; + // Used to guard `m_namedSymbols` as it's accessed on multiple threads. + // NOTE: Wrapped in unique_ptr to keep SharedCache movable. + std::unique_ptr m_namedSymMutex; bool ProcessEntryImage(const std::string& path, const dyld_cache_image_info& info); @@ -225,7 +228,6 @@ public: const AddressRangeMap& GetRegions() const { return m_regions; } const std::unordered_map& GetImages() const { return m_images; } const std::unordered_map& GetSymbols() const { return m_symbols; } - const std::unordered_map& GetNamedSymbols() const { return m_namedSymbols; } void AddImage(CacheImage&& image); @@ -266,7 +268,7 @@ public: std::optional GetSymbolAt(uint64_t address) const; - std::optional GetSymbolWithName(const std::string& name) const; + std::optional GetSymbolWithName(const std::string& name); }; // This constructs a Cache, give it a file path, and it will add all relevant cache entries. -- cgit v1.3.1