diff options
| author | Mark Rowe <mrowe@bdash.net.nz> | 2024-11-24 16:55:39 -0800 |
|---|---|---|
| committer | kat <kat@vector35.com> | 2025-01-10 12:23:07 -0500 |
| commit | 1f0be2385be2c796152e12ffebc229f10e3ffc28 (patch) | |
| tree | da2e3d231fc9343afc6c876fb2cd1fb5bd17bfc2 /view/sharedcache | |
| parent | 1dc9179158d8af2555e9182c6e0425b86d378909 (diff) | |
[SharedCache] Cache type libraries in the view-specific state
They're surprisingly expensive to look up.
Diffstat (limited to 'view/sharedcache')
| -rw-r--r-- | view/sharedcache/core/SharedCache.cpp | 49 | ||||
| -rw-r--r-- | view/sharedcache/core/SharedCache.h | 3 |
2 files changed, 25 insertions, 27 deletions
diff --git a/view/sharedcache/core/SharedCache.cpp b/view/sharedcache/core/SharedCache.cpp index cafa6bd8..b734a6b1 100644 --- a/view/sharedcache/core/SharedCache.cpp +++ b/view/sharedcache/core/SharedCache.cpp @@ -82,6 +82,8 @@ struct SharedCache::State struct SharedCache::ViewSpecificState { std::mutex typeLibraryMutex; + std::unordered_map<std::string, Ref<TypeLibrary>> typeLibraries; + std::mutex viewOperationsThatInfluenceMetadataMutex; std::atomic<BNDSCViewLoadProgress> progress; @@ -1906,21 +1908,7 @@ bool SharedCache::LoadImageWithInstallName(std::string installName, bool skipObj return false; } - std::unique_lock typelibLock(m_viewSpecificState->typeLibraryMutex); - auto typeLib = m_dscView->GetTypeLibrary(header.installName); - - if (!typeLib) - { - auto typeLibs = m_dscView->GetDefaultPlatform()->GetTypeLibrariesByName(header.installName); - if (!typeLibs.empty()) - { - typeLib = typeLibs[0]; - m_dscView->AddTypeLibrary(typeLib); - m_logger->LogInfo("shared-cache: adding type library for '%s': %s (%s)", - targetImage->installName.c_str(), typeLib->GetName().c_str(), typeLib->GetGuid().c_str()); - } - } - typelibLock.unlock(); + auto typeLib = TypeLibraryForImage(header.installName); SaveToDSCView(); @@ -2955,6 +2943,24 @@ std::string SharedCache::SerializedImageHeaderForName(std::string name) return ""; } +Ref<TypeLibrary> SharedCache::TypeLibraryForImage(const std::string& installName) { + std::lock_guard lock(m_viewSpecificState->typeLibraryMutex); + if (auto it = m_viewSpecificState->typeLibraries.find(installName); it != m_viewSpecificState->typeLibraries.end()) { + return it->second; + } + + auto typeLib = m_dscView->GetTypeLibrary(installName); + if (!typeLib) { + auto typeLibs = m_dscView->GetDefaultPlatform()->GetTypeLibrariesByName(installName); + if (!typeLibs.empty()) { + typeLib = typeLibs[0]; + m_dscView->AddTypeLibrary(typeLib); + } + } + + m_viewSpecificState->typeLibraries[installName] = typeLib; + return typeLib; +} void SharedCache::FindSymbolAtAddrAndApplyToAddr( uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis) @@ -2999,18 +3005,7 @@ void SharedCache::FindSymbolAtAddrAndApplyToAddr( } auto exportList = SharedCache::ParseExportTrie(mapping, *header); std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>> exportMapping; - std::unique_lock lock(m_viewSpecificState->typeLibraryMutex); - auto typeLib = m_dscView->GetTypeLibrary(header->installName); - if (!typeLib) - { - auto typeLibs = m_dscView->GetDefaultPlatform()->GetTypeLibrariesByName(header->installName); - if (!typeLibs.empty()) - { - typeLib = typeLibs[0]; - m_dscView->AddTypeLibrary(typeLib); - } - } - lock.unlock(); + auto typeLib = TypeLibraryForImage(header->installName); id = m_dscView->BeginUndoActions(); m_dscView->BeginBulkModifySymbols(); for (const auto& sym : exportList) diff --git a/view/sharedcache/core/SharedCache.h b/view/sharedcache/core/SharedCache.h index 7b198305..1dcc3033 100644 --- a/view/sharedcache/core/SharedCache.h +++ b/view/sharedcache/core/SharedCache.h @@ -603,6 +603,7 @@ namespace SharedCacheCore { explicit SharedCache(BinaryNinja::Ref<BinaryNinja::BinaryView> rawView); virtual ~SharedCache(); +private: std::optional<SharedCacheMachOHeader> LoadHeaderForAddress( std::shared_ptr<VM> vm, uint64_t address, std::string installName); void InitializeHeader( @@ -612,6 +613,8 @@ namespace SharedCacheCore { std::vector<Ref<Symbol>> ParseExportTrie( std::shared_ptr<MMappedFileAccessor> linkeditFile, SharedCacheMachOHeader header); + Ref<TypeLibrary> TypeLibraryForImage(const std::string& installName); + const State& State() const { return *m_state; } struct State& MutableState() { AssertMutable(); return *m_state; } |
