From 2e855275732aed00486ca100f4151f4074d9949e Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Fri, 4 Apr 2025 23:45:06 -0400 Subject: [SharedCache] Add a named symbol map Fixes https://github.com/Vector35/binaryninja-api/issues/6561 - Also tightens the SharedCache class to move only, to prevent accidental copies. - Also removes some extra copies in FFI when should pass by ref - Also adds `get_symbol_with_name` to python API The current named symbol map is populated in a worker thread spawned in the view init. This is because populating the map can take about 1 second. If we are fine with another 1 second added to the view init time then we can add it serially but I don't think this way is _that_ bad, no analysis consults this, however a user might add a workflow that would be racing this. So we need to add a mutex. --- view/sharedcache/core/SharedCache.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'view/sharedcache/core/SharedCache.cpp') diff --git a/view/sharedcache/core/SharedCache.cpp b/view/sharedcache/core/SharedCache.cpp index 65d5aec1..9bbe51e4 100644 --- a/view/sharedcache/core/SharedCache.cpp +++ b/view/sharedcache/core/SharedCache.cpp @@ -201,10 +201,10 @@ void SharedCache::AddSymbol(CacheSymbol symbol) m_symbols.insert({symbol.address, std::move(symbol)}); } -void SharedCache::AddSymbols(std::vector symbols) +void SharedCache::AddSymbols(std::vector&& symbols) { - for (auto& symbol : symbols) - m_symbols.insert({symbol.address, std::move(symbol)}); + for (auto&& symbol : symbols) + m_symbols.emplace(symbol.address, std::move(symbol)); } CacheEntryId SharedCache::AddEntry(CacheEntry entry) @@ -403,6 +403,14 @@ void SharedCache::ProcessEntrySlideInfo(const CacheEntry& entry) slideInfoProcessor.ProcessEntry(*m_vm, entry); } +void SharedCache::ProcessSymbols() +{ + // Populate the named symbols from the regular symbols map. + m_namedSymbols.reserve(m_symbols.size()); + for (const auto& [address, symbol] : m_symbols) + m_namedSymbols.emplace(symbol.name, address); +} + std::optional SharedCache::GetEntryContaining(const uint64_t address) const { for (const auto& [_, entry] : m_entries) @@ -482,10 +490,10 @@ std::optional SharedCache::GetSymbolAt(uint64_t address) const std::optional SharedCache::GetSymbolWithName(const std::string& name) const { - for (const auto& [address, symbol] : m_symbols) - if (symbol.name == name) - return symbol; - return std::nullopt; + const auto it = m_namedSymbols.find(name); + if (it == m_namedSymbols.end()) + return std::nullopt; + return GetSymbolAt(it->second); } CacheProcessor::CacheProcessor(Ref view) -- cgit v1.3.1