Skip to content

Commit 1f0be23

Browse files
bdash0cyn
authored andcommitted
[SharedCache] Cache type libraries in the view-specific state
They're surprisingly expensive to look up.
1 parent 1dc9179 commit 1f0be23

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

view/sharedcache/core/SharedCache.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct SharedCache::State
8282

8383
struct SharedCache::ViewSpecificState {
8484
std::mutex typeLibraryMutex;
85+
std::unordered_map<std::string, Ref<TypeLibrary>> typeLibraries;
86+
8587
std::mutex viewOperationsThatInfluenceMetadataMutex;
8688

8789
std::atomic<BNDSCViewLoadProgress> progress;
@@ -1906,21 +1908,7 @@ bool SharedCache::LoadImageWithInstallName(std::string installName, bool skipObj
19061908
return false;
19071909
}
19081910

1909-
std::unique_lock typelibLock(m_viewSpecificState->typeLibraryMutex);
1910-
auto typeLib = m_dscView->GetTypeLibrary(header.installName);
1911-
1912-
if (!typeLib)
1913-
{
1914-
auto typeLibs = m_dscView->GetDefaultPlatform()->GetTypeLibrariesByName(header.installName);
1915-
if (!typeLibs.empty())
1916-
{
1917-
typeLib = typeLibs[0];
1918-
m_dscView->AddTypeLibrary(typeLib);
1919-
m_logger->LogInfo("shared-cache: adding type library for '%s': %s (%s)",
1920-
targetImage->installName.c_str(), typeLib->GetName().c_str(), typeLib->GetGuid().c_str());
1921-
}
1922-
}
1923-
typelibLock.unlock();
1911+
auto typeLib = TypeLibraryForImage(header.installName);
19241912

19251913
SaveToDSCView();
19261914

@@ -2955,6 +2943,24 @@ std::string SharedCache::SerializedImageHeaderForName(std::string name)
29552943
return "";
29562944
}
29572945

2946+
Ref<TypeLibrary> SharedCache::TypeLibraryForImage(const std::string& installName) {
2947+
std::lock_guard lock(m_viewSpecificState->typeLibraryMutex);
2948+
if (auto it = m_viewSpecificState->typeLibraries.find(installName); it != m_viewSpecificState->typeLibraries.end()) {
2949+
return it->second;
2950+
}
2951+
2952+
auto typeLib = m_dscView->GetTypeLibrary(installName);
2953+
if (!typeLib) {
2954+
auto typeLibs = m_dscView->GetDefaultPlatform()->GetTypeLibrariesByName(installName);
2955+
if (!typeLibs.empty()) {
2956+
typeLib = typeLibs[0];
2957+
m_dscView->AddTypeLibrary(typeLib);
2958+
}
2959+
}
2960+
2961+
m_viewSpecificState->typeLibraries[installName] = typeLib;
2962+
return typeLib;
2963+
}
29582964

29592965
void SharedCache::FindSymbolAtAddrAndApplyToAddr(
29602966
uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis)
@@ -2999,18 +3005,7 @@ void SharedCache::FindSymbolAtAddrAndApplyToAddr(
29993005
}
30003006
auto exportList = SharedCache::ParseExportTrie(mapping, *header);
30013007
std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>> exportMapping;
3002-
std::unique_lock lock(m_viewSpecificState->typeLibraryMutex);
3003-
auto typeLib = m_dscView->GetTypeLibrary(header->installName);
3004-
if (!typeLib)
3005-
{
3006-
auto typeLibs = m_dscView->GetDefaultPlatform()->GetTypeLibrariesByName(header->installName);
3007-
if (!typeLibs.empty())
3008-
{
3009-
typeLib = typeLibs[0];
3010-
m_dscView->AddTypeLibrary(typeLib);
3011-
}
3012-
}
3013-
lock.unlock();
3008+
auto typeLib = TypeLibraryForImage(header->installName);
30143009
id = m_dscView->BeginUndoActions();
30153010
m_dscView->BeginBulkModifySymbols();
30163011
for (const auto& sym : exportList)

view/sharedcache/core/SharedCache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ namespace SharedCacheCore {
603603
explicit SharedCache(BinaryNinja::Ref<BinaryNinja::BinaryView> rawView);
604604
virtual ~SharedCache();
605605

606+
private:
606607
std::optional<SharedCacheMachOHeader> LoadHeaderForAddress(
607608
std::shared_ptr<VM> vm, uint64_t address, std::string installName);
608609
void InitializeHeader(
@@ -612,6 +613,8 @@ namespace SharedCacheCore {
612613
std::vector<Ref<Symbol>> ParseExportTrie(
613614
std::shared_ptr<MMappedFileAccessor> linkeditFile, SharedCacheMachOHeader header);
614615

616+
Ref<TypeLibrary> TypeLibraryForImage(const std::string& installName);
617+
615618
const State& State() const { return *m_state; }
616619
struct State& MutableState() { AssertMutable(); return *m_state; }
617620

0 commit comments

Comments
 (0)