Skip to content

Commit b74c500

Browse files
committed
[SharedCache] Misc cleanup
- Make `SharedCache::m_entries` a simple std::vector we never lookup based off the entry ID - Remove some old comments - Rename some old variables so that they are accurate - Fix compiler warnings - Remove some unneeded copying
1 parent 043a863 commit b74c500

File tree

7 files changed

+21
-35
lines changed

7 files changed

+21
-35
lines changed

view/sharedcache/core/MachO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ std::vector<CacheSymbol> SharedCacheMachOHeader::ReadSymbolTable(VirtualMemory&
490490
{
491491
// TODO: where logger?
492492
LogError(
493-
"Symbol entry at index %llu has a string offset of %u which is outside the strings buffer of size %u "
493+
"Symbol entry at index %llu has a string offset of %u which is outside the strings buffer of size %llu "
494494
"for symbol table %x",
495495
entryIndex, nlist.n_strx, stringInfo.address, stringInfo.entries);
496496
continue;

view/sharedcache/core/ObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ std::optional<ObjCOptimizationHeader> GetObjCOptimizationHeader(SharedCache& cac
111111
// Find the first primary entry and use that header to read the obj opt header.
112112
// Don't ask me why this is done like this...
113113
std::optional<dyld_cache_header> primaryCacheHeader = std::nullopt;
114-
for (const auto& [_, entry] : cache.GetEntries())
114+
for (const auto& entry : cache.GetEntries())
115115
{
116116
if (entry.GetType() == CacheEntryType::Primary)
117117
{

view/sharedcache/core/SharedCache.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
using namespace BinaryNinja;
1010

11-
// The next id to use when calling Cache::AddEntry
12-
static CacheEntryId nextId = 1;
13-
1411
std::pair<std::string, Ref<Type>> CacheSymbol::DemangledName(BinaryView &view) const
1512
{
1613
QualifiedName qname;
@@ -230,13 +227,8 @@ void SharedCache::AddSymbols(std::vector<CacheSymbol>&& symbols)
230227
m_symbols.insert({symbol.address, std::move(symbol)});
231228
}
232229

233-
CacheEntryId SharedCache::AddEntry(CacheEntry entry)
230+
void SharedCache::AddEntry(CacheEntry entry)
234231
{
235-
// TODO: Maybe check to see if we already added the file?
236-
// TODO: I doubt we will ever accidentally call this for the same entry...
237-
// This is monotonically increasing so you can tell how many times we have called this function :)
238-
CacheEntryId id = nextId++;
239-
240232
// Get the file accessor to associate with the virtual memory region.
241233
auto fileAccessor = FileAccessorCache::Global().Open(entry.GetFilePath());
242234

@@ -253,8 +245,7 @@ CacheEntryId SharedCache::AddEntry(CacheEntry entry)
253245
}
254246

255247
// We are done and can make the entry visible to the entire cache.
256-
m_entries.insert({id, std::move(entry)});
257-
return id;
248+
m_entries.push_back(std::move(entry));
258249
}
259250

260251
bool SharedCache::ProcessEntryImage(const std::string& path, const dyld_cache_image_info& info)
@@ -330,7 +321,7 @@ void SharedCache::ProcessEntryImages(const CacheEntry& entry)
330321
// At this point all relevant mapping should be loaded in the virtual memory.
331322
void SharedCache::ProcessEntryRegions(const CacheEntry& entry)
332323
{
333-
auto entryHeader = entry.GetHeader();
324+
const auto& entryHeader = entry.GetHeader();
334325

335326
// Collect pool addresses as non image memory regions.
336327
for (size_t i = 0; i < entryHeader.branchPoolsCount; i++)
@@ -468,7 +459,7 @@ void SharedCache::ProcessSymbols()
468459

469460
std::optional<CacheEntry> SharedCache::GetEntryContaining(const uint64_t address) const
470461
{
471-
for (const auto& [_, entry] : m_entries)
462+
for (const auto& entry : m_entries)
472463
{
473464
for (const auto& mapping : entry.GetMappings())
474465
{
@@ -482,7 +473,7 @@ std::optional<CacheEntry> SharedCache::GetEntryContaining(const uint64_t address
482473

483474
std::optional<CacheEntry> SharedCache::GetEntryWithImage(const CacheImage& image) const
484475
{
485-
for (const auto& [_, entry] : m_entries)
476+
for (const auto& entry : m_entries)
486477
{
487478
for (const auto& [_, currentImage] : entry.GetImages())
488479
{

view/sharedcache/core/SharedCache.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ enum class CacheEntryType
106106
Primary,
107107
Secondary,
108108
// A special entry that holds symbols for other cache entries.
109-
// TODO: We dont need this i think.
110109
Symbols,
111110
// If the type is marked as this then all mappings will be marked as such.
112111
DyldData,
@@ -164,8 +163,6 @@ class CacheEntry
164163
// The ID for a given CacheEntry, use this instead of passing a pointer around to avoid complexity :V
165164
typedef uint32_t CacheEntryId;
166165

167-
// TODO: Add a "ViewCache" that keeps track of what has been added to the view.
168-
169166
// The C in DSC.
170167
// This represents the entire cache, all regions and images are visible from here.
171168
// This is the dump for all the information, and what the workflow activities and the UI want.
@@ -175,10 +172,9 @@ class SharedCache
175172
{
176173
// Calculated within `AddEntry`, this indicates where the shared cache image is based at.
177174
uint64_t m_baseAddress = 0;
178-
// TODO: Figure out when to lock the mutex on this shit lmfao
179175
// The shared cache can own the virtual memory, this is fine...
180176
std::shared_ptr<VirtualMemory> m_vm;
181-
std::unordered_map<CacheEntryId, CacheEntry> m_entries {};
177+
std::vector<CacheEntry> m_entries {};
182178
// This information is used in tandem with the cache images to load memory regions into the binary view.
183179
AddressRangeMap<CacheRegion> m_regions {};
184180
// Describes the images of the cache.
@@ -210,7 +206,7 @@ class SharedCache
210206

211207
uint64_t GetBaseAddress() const { return m_baseAddress; }
212208
std::shared_ptr<VirtualMemory> GetVirtualMemory() const { return m_vm; }
213-
const std::unordered_map<CacheEntryId, CacheEntry>& GetEntries() const { return m_entries; }
209+
const std::vector<CacheEntry>& GetEntries() const { return m_entries; }
214210
const AddressRangeMap<CacheRegion>& GetRegions() const { return m_regions; }
215211
const std::unordered_map<uint64_t, CacheImage>& GetImages() const { return m_images; }
216212
const std::unordered_map<uint64_t, CacheSymbol>& GetSymbols() const { return m_symbols; }
@@ -226,7 +222,7 @@ class SharedCache
226222

227223
// Adds the cache entry and populates the virtual memory using the mapping information.
228224
// After being added the entry is read only, there is nothing that can modify it.
229-
CacheEntryId AddEntry(CacheEntry entry);
225+
void AddEntry(CacheEntry entry);
230226

231227
void ProcessEntryImages(const CacheEntry& entry);
232228

view/sharedcache/core/SharedCacheController.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ DSCRef<SharedCacheController> SharedCacheController::Initialize(BinaryView& view
6666
auto id = GetViewIdFromView(view);
6767
std::unique_lock<std::shared_mutex> lock(GlobalControllersMutex);
6868
auto logger = new Logger("SharedCache.Controller", view.GetFile()->GetSessionId());
69-
DSCRef<SharedCacheController> dscView = new SharedCacheController(std::move(cache), logger);
69+
DSCRef<SharedCacheController> controller = new SharedCacheController(std::move(cache), logger);
7070

7171
// Pull the settings from the view.
7272
if (Ref<Settings> settings = view.GetLoadSettings(VIEW_NAME))
7373
{
7474
if (settings->Contains("loader.dsc.processObjC"))
75-
dscView->m_processObjC = settings->Get<bool>("loader.dsc.processObjC", &view);
75+
controller->m_processObjC = settings->Get<bool>("loader.dsc.processObjC", &view);
7676
if (settings->Contains("loader.dsc.processCFStrings"))
77-
dscView->m_processCFStrings = settings->Get<bool>("loader.dsc.processCFStrings", &view);
77+
controller->m_processCFStrings = settings->Get<bool>("loader.dsc.processCFStrings", &view);
7878
if (settings->Contains("loader.dsc.regionFilter"))
79-
dscView->m_regionFilter = std::regex(settings->Get<std::string>("loader.dsc.regionFilter", &view));
79+
controller->m_regionFilter = std::regex(settings->Get<std::string>("loader.dsc.regionFilter", &view));
8080
}
8181

8282
// TODO: Support old shared cache metadata
@@ -88,10 +88,10 @@ DSCRef<SharedCacheController> SharedCacheController::Initialize(BinaryView& view
8888
// This effectively restores the state of the opened database to when it was last saved.
8989
// NOTE: We store on the parent view because hilariously, the metadata is not present until after view init.
9090
if (const auto metadata = view.GetParentView()->QueryMetadata(METADATA_KEY))
91-
dscView->LoadMetadata(*metadata);
91+
controller->LoadMetadata(*metadata);
9292

93-
GlobalControllers().insert({id, dscView});
94-
return dscView;
93+
GlobalControllers().insert({id, controller});
94+
return controller;
9595
}
9696

9797
DSCRef<SharedCacheController> SharedCacheController::FromView(BinaryView& view)

view/sharedcache/core/SharedCacheView.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ bool SharedCacheView::InitController()
912912
// Write all the slide info pointers to the virtual memory.
913913
// This should be done before any other work begins, as the backing data will be altered by this process.
914914
auto startTime = std::chrono::high_resolution_clock::now();
915-
for (const auto& [_, entry] : sharedCache.GetEntries())
915+
for (const auto& entry : sharedCache.GetEntries())
916916
sharedCache.ProcessEntrySlideInfo(entry);
917917
auto endTime = std::chrono::high_resolution_clock::now();
918918
std::chrono::duration<double> elapsed = endTime - startTime;
@@ -923,7 +923,7 @@ bool SharedCacheView::InitController()
923923
// Load up all images into the cache before adding extra regions.
924924
// Currently, it is expected that a primary entry contain all relevant images, so we only check that one.
925925
auto startTime = std::chrono::high_resolution_clock::now();
926-
for (const auto& [_, entry] : sharedCache.GetEntries())
926+
for (const auto& entry : sharedCache.GetEntries())
927927
if (entry.GetType() == CacheEntryType::Primary)
928928
sharedCache.ProcessEntryImages(entry);
929929
auto endTime = std::chrono::high_resolution_clock::now();
@@ -936,10 +936,9 @@ bool SharedCacheView::InitController()
936936
}
937937

938938
{
939-
// TODO: Run this up on a separate thread maybe and have callback notifications?
940939
// Load up all the regions into the cache.
941940
auto startTime = std::chrono::high_resolution_clock::now();
942-
for (const auto& [_, entry] : sharedCache.GetEntries())
941+
for (const auto& entry : sharedCache.GetEntries())
943942
sharedCache.ProcessEntryRegions(entry);
944943
auto endTime = std::chrono::high_resolution_clock::now();
945944
std::chrono::duration<double> elapsed = endTime - startTime;

view/sharedcache/core/SlideInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void ApplySlideInfoV2(MappedFileAccessor& accessor, const SlideMappingInfo& mapp
158158

159159
std::vector<SlideMappingInfo> SlideInfoProcessor::ReadEntryInfo(const MappedFileAccessor& accessor, const CacheEntry& entry) const
160160
{
161-
auto& baseHeader = entry.GetHeader();
161+
const auto& baseHeader = entry.GetHeader();
162162

163163
// Handle legacy, single mapping slide info.
164164
if (baseHeader.slideInfoOffsetUnused)

0 commit comments

Comments
 (0)