Skip to content

Commit f37faca

Browse files
committed
wip
1 parent e691b44 commit f37faca

17 files changed

+167
-95
lines changed

view/sharedcache/HeadlessPlugin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <binaryninjaapi.h>
22
#include "SharedCacheView.h"
3-
#include "SharedCache.h"
43

54
#ifdef __cplusplus
65
extern "C" {

view/sharedcache/api/sharedcache.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ bool SharedCacheController::LoadImage(BinaryView &view, const CacheImage &image)
137137
return result;
138138
}
139139

140+
bool SharedCacheController::IsRegionLoaded(const CacheRegion &region) const
141+
{
142+
auto apiRegion = RegionToApi(region);
143+
bool result = BNSharedCacheControllerIsRegionLoaded(m_object, &apiRegion);
144+
BNSharedCacheFreeRegion(apiRegion);
145+
return result;
146+
}
147+
148+
bool SharedCacheController::IsImageLoaded(const CacheImage &image) const
149+
{
150+
auto apiImage = ImageToApi(image);
151+
bool result = BNSharedCacheControllerIsImageLoaded(m_object, &apiImage);
152+
BNSharedCacheFreeImage(apiImage);
153+
return result;
154+
}
155+
140156
std::optional<CacheRegion> SharedCacheController::GetRegionAt(uint64_t address) const
141157
{
142158
BNSharedCacheRegion apiRegion;
@@ -219,6 +235,30 @@ std::vector<CacheEntry> SharedCacheController::GetEntries() const
219235
return result;
220236
}
221237

238+
std::vector<CacheRegion> SharedCacheController::GetLoadedRegions() const
239+
{
240+
size_t count;
241+
BNSharedCacheRegion* regions = BNSharedCacheControllerGetLoadedRegions(m_object, &count);
242+
std::vector<CacheRegion> result;
243+
result.reserve(count);
244+
for (size_t i = 0; i < count; i++)
245+
result.emplace_back(RegionFromApi(regions[i]));
246+
BNSharedCacheFreeRegionList(regions, count);
247+
return result;
248+
}
249+
250+
std::vector<CacheRegion> SharedCacheController::GetRegions() const
251+
{
252+
size_t count;
253+
BNSharedCacheRegion* regions = BNSharedCacheControllerGetRegions(m_object, &count);
254+
std::vector<CacheRegion> result;
255+
result.reserve(count);
256+
for (size_t i = 0; i < count; i++)
257+
result.emplace_back(RegionFromApi(regions[i]));
258+
BNSharedCacheFreeRegionList(regions, count);
259+
return result;
260+
}
261+
222262
std::vector<CacheImage> SharedCacheController::GetImages() const
223263
{
224264
size_t count;
@@ -231,6 +271,18 @@ std::vector<CacheImage> SharedCacheController::GetImages() const
231271
return result;
232272
}
233273

274+
std::vector<CacheImage> SharedCacheController::GetLoadedImages() const
275+
{
276+
size_t count;
277+
BNSharedCacheImage* images = BNSharedCacheControllerGetLoadedImages(m_object, &count);
278+
std::vector<CacheImage> result;
279+
result.reserve(count);
280+
for (size_t i = 0; i < count; i++)
281+
result.emplace_back(ImageFromApi(images[i]));
282+
BNSharedCacheFreeImageList(images, count);
283+
return result;
284+
}
285+
234286
std::vector<CacheSymbol> SharedCacheController::GetSymbols() const
235287
{
236288
size_t count;
@@ -241,4 +293,4 @@ std::vector<CacheSymbol> SharedCacheController::GetSymbols() const
241293
result.emplace_back(SymbolFromApi(symbols[i]));
242294
BNSharedCacheFreeSymbolList(symbols, count);
243295
return result;
244-
}
296+
}

view/sharedcache/api/sharedcacheapi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ namespace SharedCacheAPI {
300300
// multiple images at a time.
301301
bool LoadImage(BinaryNinja::BinaryView& view, const CacheImage& image);
302302

303+
bool IsRegionLoaded(const CacheRegion& region) const;
304+
bool IsImageLoaded(const CacheImage& image) const;
305+
303306
std::optional<CacheRegion> GetRegionAt(uint64_t address) const;
304307
std::optional<CacheRegion> GetRegionContaining(uint64_t address) const;
305308

@@ -311,7 +314,10 @@ namespace SharedCacheAPI {
311314
std::optional<CacheSymbol> GetSymbolWithName(const std::string& name) const;
312315

313316
std::vector<CacheEntry> GetEntries() const;
317+
std::vector<CacheRegion> GetRegions() const;
318+
std::vector<CacheRegion> GetLoadedRegions() const;
314319
std::vector<CacheImage> GetImages() const;
320+
std::vector<CacheImage> GetLoadedImages() const;
315321
std::vector<CacheSymbol> GetSymbols() const;
316322
};
317323
}

view/sharedcache/api/sharedcachecore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ extern "C"
115115

116116
SHAREDCACHE_FFI_API bool BNSharedCacheControllerLoadImage(BNSharedCacheController* controller, BNBinaryView* view, BNSharedCacheImage* image);
117117
SHAREDCACHE_FFI_API bool BNSharedCacheControllerLoadRegion(BNSharedCacheController* controller, BNBinaryView* view, BNSharedCacheRegion* region);
118+
119+
SHAREDCACHE_FFI_API bool BNSharedCacheControllerIsImageLoaded(BNSharedCacheController* controller, BNSharedCacheImage* image);
120+
SHAREDCACHE_FFI_API bool BNSharedCacheControllerIsRegionLoaded(BNSharedCacheController* controller, BNSharedCacheRegion* region);
118121

119122
SHAREDCACHE_FFI_API bool BNSharedCacheControllerGetRegionAt(BNSharedCacheController* controller, uint64_t address, BNSharedCacheRegion* outRegion);
120123
SHAREDCACHE_FFI_API bool BNSharedCacheControllerGetRegionContaining(BNSharedCacheController* controller, uint64_t address, BNSharedCacheRegion* region);

view/sharedcache/core/MachO.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,7 @@ void SharedCacheMachOProcessor::ApplyHeader(Ref<BinaryView> view, SharedCacheMac
441441
applyFunctionStarts = settings->Get<bool>("loader.dsc.processFunctionStarts", view);
442442

443443
header.ApplyHeaderSections(view);
444-
445-
// TODO: Wtf is this?
446-
bool applyHeaderTypes = true;
447-
// for (const auto& region : regionsToLoad)
448-
// {
449-
// if (header.textBase >= region->start && header.textBase < region->start + region->size)
450-
// {
451-
// if (!MemoryRegionIsHeaderInitialized(lock, *region))
452-
// applyHeaderTypes = true;
453-
//
454-
// break;
455-
// }
456-
// }
457-
if (applyHeaderTypes)
458-
header.ApplyHeaderDataVariables(view);
444+
header.ApplyHeaderDataVariables(view);
459445

460446
if (header.linkeditPresent && m_vm->IsAddressMapped(header.linkeditSegment.vmaddr))
461447
{
@@ -492,20 +478,12 @@ void SharedCacheMachOProcessor::ApplyHeader(Ref<BinaryView> view, SharedCacheMac
492478
{
493479
// NOTE: This table is read relative to the link edit segment file base.
494480
// TODO: Remove this and use the m_symbols in the cache?
495-
const auto exportSymbols = header.ReadExportSymbolTable(*view, *m_vm);
481+
const auto exportSymbols = header.ReadExportSymbolTable(*m_vm);
496482
for (const auto& sym : exportSymbols)
497483
ApplySymbol(view, typeLib, sym.ToBNSymbol());
498484
}
499485
view->EndBulkModifySymbols();
500486
}
501-
502-
// Update the regions to initialized so we dont repeat work.
503-
// TODO: Should this really be here?
504-
// TODO: Cant we just check to see if the header info exists in the view?
505-
// for (auto region : regionsToLoad)
506-
// {
507-
// SetMemoryRegionHeaderInitialized(lock, *region);
508-
// }
509487
}
510488

511489
uint64_t SharedCacheMachOHeader::ApplyHeaderSections(Ref<BinaryView> view)
@@ -790,6 +768,7 @@ std::vector<uint64_t> SharedCacheMachOHeader::ReadFunctionTable(VirtualMemory& v
790768
return functionTable;
791769
}
792770

771+
// TODO: Replace view with address size?
793772
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadSymbolTable(BinaryView& view, VirtualMemory& vm) const
794773
{
795774
auto addressSize = view.GetAddressSize();
@@ -976,7 +955,7 @@ bool SharedCacheMachOHeader::ProcessLinkEditTrie(std::vector<CacheSymbol>& symbo
976955
return true;
977956
}
978957

979-
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadExportSymbolTable(BinaryView& view, VirtualMemory& vm) const
958+
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadExportSymbolTable(VirtualMemory& vm) const
980959
{
981960
if (exportTrie.datasize == 0)
982961
return {};

view/sharedcache/core/MachO.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct SharedCacheMachOHeader
5555
// Use this if you want to read offsets relative to the file containing the link edit segment.
5656
uint64_t GetLinkEditFileBase() const { return linkeditSegment.vmaddr - linkeditSegment.fileoff; };
5757

58+
// TODO: Replace view with address size?
5859
std::vector<CacheSymbol> ReadSymbolTable(BinaryNinja::BinaryView& view, VirtualMemory& vm) const;
5960

6061
std::optional<CacheSymbol> AddExportTerminalSymbol(const std::string &symbolName, const uint8_t *current,
@@ -63,10 +64,11 @@ struct SharedCacheMachOHeader
6364
bool ProcessLinkEditTrie(std::vector<CacheSymbol> &symbols, const std::string &currentText, const uint8_t *begin,
6465
const uint8_t *current, const uint8_t *end) const;
6566

66-
std::vector<CacheSymbol> ReadExportSymbolTable(BinaryNinja::BinaryView& view, VirtualMemory& vm) const;
67+
std::vector<CacheSymbol> ReadExportSymbolTable(VirtualMemory& vm) const;
6768

6869
std::vector<uint64_t> ReadFunctionTable(VirtualMemory& vm) const;
6970

71+
// TODO: Move the apply functions to the processor.
7072
// Applies header sections to the view if not already existing, returning the number
7173
// of newly added sections to the view. The return value will be zero if no new sections were added.
7274
uint64_t ApplyHeaderSections(BinaryNinja::Ref<BinaryNinja::BinaryView> view);

view/sharedcache/core/MappedFileAccessor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
std::shared_ptr<MappedFileAccessor> MappedFileAccessor::Open(const std::string& filePath)
55
{
6-
// TODO: Log here just to make sure we are not constantly opening the same files.
7-
BinaryNinja::LogInfo("Opening mapped file: %s", filePath.c_str());
86
auto file = MappedFile::OpenFile(filePath);
97
if (!file.has_value())
108
return nullptr;

view/sharedcache/core/ObjC.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using namespace BinaryNinja;
66
using namespace DSCObjC;
77

8-
SharedCacheObjCReader::SharedCacheObjCReader(VirtualMemoryReader reader, size_t addressSize) : m_reader(reader), m_addressSize(addressSize)
8+
SharedCacheObjCReader::SharedCacheObjCReader(VirtualMemoryReader reader) : m_reader(reader)
99
{
1010
}
1111

@@ -90,8 +90,8 @@ std::shared_ptr<ObjCReader> SharedCacheObjCProcessor::GetReader()
9090
// TODO: This should never happen.
9191
if (!controller)
9292
throw std::runtime_error("SharedCacheController not found for SharedCacheObjCProcessor::GetReader!");
93-
auto reader = VirtualMemoryReader(controller->GetCache().GetVirtualMemory());
94-
return std::make_shared<SharedCacheObjCReader>(reader, m_data->GetAddressSize());
93+
auto reader = VirtualMemoryReader(controller->GetCache().GetVirtualMemory(), m_data->GetAddressSize());
94+
return std::make_shared<SharedCacheObjCReader>(reader);
9595
}
9696

9797
void SharedCacheObjCProcessor::GetRelativeMethod(ObjCReader* reader, method_t& meth)

view/sharedcache/core/ObjC.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ struct ObjCOptimizationHeader
1919
namespace DSCObjC {
2020
class SharedCacheObjCReader : public BinaryNinja::ObjCReader {
2121
VirtualMemoryReader m_reader;
22-
size_t m_addressSize;
2322

2423
public:
2524
void Read(void* dest, size_t len) override;
@@ -39,7 +38,7 @@ namespace DSCObjC {
3938

4039
VirtualMemoryReader& GetVMReader();
4140

42-
SharedCacheObjCReader(VirtualMemoryReader reader, size_t addressSize);
41+
SharedCacheObjCReader(VirtualMemoryReader reader);
4342
};
4443

4544
class SharedCacheObjCProcessor : public BinaryNinja::ObjCProcessor {

view/sharedcache/core/SharedCache.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ typedef uint32_t CacheEntryId;
166166
// Creating this is expensive, both in actual processing and just copying, so we only generate this
167167
// once every time the database is open.
168168
class SharedCache {
169+
uint64_t m_addressSize = 8;
169170
uint64_t m_baseAddress = 0;
170171
// TODO: Figure out when to lock the mutex on this shit lmfao
171172
// The shared cache can own the virtual memory, this is fine...
@@ -179,9 +180,9 @@ class SharedCache {
179180
std::unordered_map<uint64_t, CacheSymbol> m_symbols {};
180181

181182
// Process the images and regions of the entry and add them to the cache.
182-
void ProcessEntry(BinaryNinja::BinaryView& view, const CacheEntry& entry);
183+
void ProcessEntry(const CacheEntry& entry);
183184
public:
184-
explicit SharedCache();
185+
explicit SharedCache(uint64_t addressSize);
185186

186187
uint64_t GetBaseAddress() const { return m_baseAddress; }
187188
std::shared_ptr<VirtualMemory> GetVirtualMemory() { return m_vm; }
@@ -207,7 +208,7 @@ class SharedCache {
207208
CacheEntryId AddEntry(CacheEntry entry);
208209

209210
// Call this after adding all the entries. This will process them to pull out the regions and images.
210-
void ProcessEntries(BinaryNinja::BinaryView& view);
211+
void ProcessEntries();
211212

212213
// TODO: This stuff might just be loading stuff, in that case move this to the controller class. :/
213214
// Objective-C processing is done inline so that later analysis is aware of it.

0 commit comments

Comments
 (0)