Skip to content

Commit 197c03f

Browse files
committed
wip
1 parent c91df9b commit 197c03f

15 files changed

+1067
-461
lines changed

view/sharedcache/HeadlessPlugin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <binaryninjaapi.h>
2-
#include "DSCView.h"
2+
#include "SharedCacheView.h"
33
#include "SharedCache.h"
44

55
#ifdef __cplusplus
@@ -16,7 +16,7 @@ extern "C"
1616

1717
BINARYNINJAPLUGIN bool CorePluginInit()
1818
{
19-
DSCViewType::Register();
19+
SharedCacheViewType::Register();
2020
RegisterSharedCacheWorkflow();
2121
return true;
2222
}

view/sharedcache/api/sharedcacheapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ namespace SharedCacheAPI {
133133
std::string name;
134134
};
135135

136-
class SharedCache : public SCCoreRefCountObject<BNSharedCache, BNNewSharedCacheReference, BNFreeSharedCacheReference> {
136+
class SharedCache : public SCCoreRefCountObject<BNDSCController, BNNewSharedCacheReference, BNFreeSharedCacheReference> {
137137
public:
138138
explicit SharedCache(BinaryNinja::Ref<BinaryNinja::BinaryView> view);
139139

Lines changed: 53 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
#pragma once
22

3-
4-
#ifdef __cplusplus
5-
extern "C"
6-
{
7-
#endif
8-
93
#ifdef __GNUC__
104
#ifdef SHAREDCACHE_LIBRARY
115
#define SHAREDCACHE_FFI_API __attribute__((visibility("default")))
@@ -28,138 +22,102 @@ extern "C"
2822
#endif // _MSC_VER
2923
#endif // __GNUC__C
3024

31-
#define CORE_ALLOCATED_STRUCT(T)
32-
33-
#define CORE_ALLOCATED_CLASS(T) \
34-
public: \
35-
CORE_ALLOCATED_STRUCT(T) \
36-
private:
37-
38-
#define DECLARE_SHAREDCACHE_API_OBJECT_INTERNAL(handle, cls, ns) \
39-
namespace ns { class cls; } struct handle { ns::cls* object; }
40-
41-
#define DECLARE_SHAREDCACHE_API_OBJECT(handle, cls) DECLARE_SHAREDCACHE_API_OBJECT_INTERNAL(handle, cls, SharedCacheCore)
42-
43-
#define IMPLEMENT_SHAREDCACHE_API_OBJECT(handle) \
44-
CORE_ALLOCATED_CLASS(handle) \
45-
private: \
46-
handle m_apiObject; \
47-
public: \
48-
typedef handle* APIHandle; \
49-
handle* GetAPIObject() { return &m_apiObject; } \
50-
private:
51-
#define INIT_SHAREDCACHE_API_OBJECT() \
52-
m_apiObject.object = this;
53-
54-
typedef enum BNDSCViewState {
55-
Unloaded,
56-
Loaded,
57-
LoadedWithImages,
58-
} BNDSCViewState;
59-
60-
typedef enum BNDSCViewLoadProgress {
61-
LoadProgressNotStarted,
62-
LoadProgressLoadingCaches,
63-
LoadProgressLoadingImages,
64-
LoadProgressFinished,
65-
} BNDSCViewLoadProgress;
66-
67-
typedef enum BNBackingCacheType {
68-
BackingCacheTypePrimary,
69-
BackingCacheTypeSecondary,
70-
BackingCacheTypeSymbols,
71-
} BNBackingCacheType;
72-
25+
#ifdef __cplusplus
26+
extern "C"
27+
{
28+
#endif
7329
typedef struct BNBinaryView BNBinaryView;
74-
typedef struct BNSharedCache BNSharedCache;
30+
typedef struct BNSharedCacheController BNSharedCacheController;
7531
typedef struct BNStringRef BNStringRef;
7632

77-
typedef struct BNDSCImageMemoryMapping {
33+
typedef enum BNSharedCacheEntryType {
34+
SharedCacheEntryTypePrimary,
35+
SharedCacheEntryTypeSecondary,
36+
SharedCacheEntryTypeSymbols,
37+
SharedCacheEntryTypeDyldData,
38+
SharedCacheEntryTypeStub,
39+
} BNSharedCacheEntryType;
40+
41+
typedef struct BNSharedCacheImageMemoryMapping {
7842
char* filePath;
7943
char* name;
8044
uint64_t vmAddress;
8145
uint64_t size;
8246
bool loaded;
8347
uint64_t rawViewOffset;
84-
} BNDSCImageMemoryMapping;
48+
} BNSharedCacheImageMemoryMapping;
8549

86-
typedef struct BNDSCImage {
50+
typedef struct BNSharedCacheImage {
8751
char* name;
8852
uint64_t headerAddress;
89-
BNDSCImageMemoryMapping* mappings;
90-
size_t mappingCount;
91-
} BNDSCImage;
53+
size_t regionStartCount;
54+
uint64_t* regionStarts;
55+
} BNSharedCacheImage;
9256

93-
typedef struct BNDSCMappedMemoryRegion {
57+
typedef struct BNSharedCacheRegion {
9458
uint64_t vmAddress;
9559
uint64_t size;
9660
char* name;
97-
} BNDSCMappedMemoryRegion;
61+
} BNSharedCacheRegion;
9862

99-
typedef struct BNDSCBackingCacheMapping {
63+
typedef struct BNSharedCacheMappingInfo {
10064
uint64_t vmAddress;
10165
uint64_t size;
10266
uint64_t fileOffset;
103-
} BNDSCBackingCacheMapping;
67+
} BNSharedCacheMappingInfo;
10468

105-
typedef struct BNDSCBackingCache {
69+
typedef struct BNSharedCacheEntry {
10670
char* path;
107-
BNBackingCacheType cacheType;
108-
BNDSCBackingCacheMapping* mappings;
71+
BNSharedCacheEntryType cacheType;
10972
size_t mappingCount;
110-
} BNDSCBackingCache;
111-
112-
typedef struct BNDSCMemoryUsageInfo {
113-
uint64_t sharedCacheRefs;
114-
uint64_t mmapRefs;
115-
} BNDSCMemoryUsageInfo;
73+
BNSharedCacheMappingInfo* mappings;
74+
} BNSharedCacheEntry;
11675

11776
typedef struct BNDSCSymbolRep {
11877
uint64_t address;
11978
BNStringRef* name;
12079
char* image;
12180
} BNDSCSymbolRep;
12281

123-
SHAREDCACHE_FFI_API BNSharedCache* BNGetSharedCache(BNBinaryView* data);
82+
SHAREDCACHE_FFI_API BNSharedCacheController* BNGetSharedCacheController(BNBinaryView* data);
12483

125-
SHAREDCACHE_FFI_API BNSharedCache* BNNewSharedCacheReference(BNSharedCache* cache);
126-
SHAREDCACHE_FFI_API void BNFreeSharedCacheReference(BNSharedCache* cache);
84+
SHAREDCACHE_FFI_API BNSharedCacheController* BNNewSharedCacheControllerReference(BNSharedCacheController* controller);
85+
SHAREDCACHE_FFI_API void BNFreeSharedCacheControllerReference(BNSharedCacheController* controller);
12786

128-
SHAREDCACHE_FFI_API char** BNDSCViewGetInstallNames(BNSharedCache* cache, size_t* count);
129-
130-
SHAREDCACHE_FFI_API bool BNDSCViewLoadImageWithInstallName(BNSharedCache* cache, const char* name, bool skipObjC);
131-
SHAREDCACHE_FFI_API bool BNDSCViewLoadSectionAtAddress(BNSharedCache* cache, uint64_t name);
132-
SHAREDCACHE_FFI_API bool BNDSCViewLoadImageContainingAddress(BNSharedCache* cache, uint64_t address, bool skipObjC);
87+
SHAREDCACHE_FFI_API bool BNSharedCacheControllerLoadImage(BNSharedCacheController* controller, BNBinaryView* view, BNSharedCacheImage* image);
88+
SHAREDCACHE_FFI_API bool BNSharedCacheControllerLoadRegion(BNSharedCacheController* controller, BNBinaryView* view, BNSharedCacheRegion* region);
13389

134-
SHAREDCACHE_FFI_API void BNDSCViewProcessObjCSectionsForImageWithInstallName(BNSharedCache* cache, const char* name);
135-
SHAREDCACHE_FFI_API void BNDSCViewProcessAllObjCSections(BNSharedCache* cache);
90+
// SHAREDCACHE_FFI_API void BNDSCViewProcessObjCSectionsForImageWithInstallName(BNSharedCacheController* cache, const char* name);
91+
// SHAREDCACHE_FFI_API void BNDSCViewProcessAllObjCSections(BNSharedCacheController* cache);
13692

137-
SHAREDCACHE_FFI_API char* BNDSCViewGetNameForAddress(BNSharedCache* cache, uint64_t address);
138-
SHAREDCACHE_FFI_API char* BNDSCViewGetImageNameForAddress(BNSharedCache* cache, uint64_t address);
93+
SHAREDCACHE_FFI_API char* BNSharedCacheControllerGetRegionNameForAddress(BNSharedCacheController* controller, uint64_t address);
94+
SHAREDCACHE_FFI_API char* BNSharedCacheControllerGetImageNameForAddress(BNSharedCacheController* controller, uint64_t address);
13995

140-
SHAREDCACHE_FFI_API BNDSCViewState BNDSCViewGetState(BNSharedCache* cache);
141-
SHAREDCACHE_FFI_API BNDSCViewLoadProgress BNDSCViewGetLoadProgress(uint64_t sessionID);
142-
SHAREDCACHE_FFI_API uint64_t BNDSCViewFastGetBackingCacheCount(BNBinaryView* view);
96+
// SHAREDCACHE_FFI_API BNDSCSymbolRep* BNSharedCacheControllerLoadAllSymbolsAndWait(BNSharedCacheController* cache, size_t* count);
97+
// SHAREDCACHE_FFI_API void BNSharedCacheControllerFreeSymbols(BNDSCSymbolRep* symbols, size_t count);
14398

144-
SHAREDCACHE_FFI_API BNDSCSymbolRep* BNDSCViewLoadAllSymbolsAndWait(BNSharedCache* cache, size_t* count);
145-
SHAREDCACHE_FFI_API void BNDSCViewFreeSymbols(BNDSCSymbolRep* symbols, size_t count);
99+
SHAREDCACHE_FFI_API BNSharedCacheRegion* BNSharedCacheControllerGetRegions(BNSharedCacheController* controller, size_t* count);
100+
SHAREDCACHE_FFI_API BNSharedCacheRegion* BNSharedCacheControllerGetLoadedRegions(BNSharedCacheController* controller, size_t* count);
146101

147-
SHAREDCACHE_FFI_API BNDSCMappedMemoryRegion* BNDSCViewGetLoadedRegions(BNSharedCache* cache, size_t* count);
148-
SHAREDCACHE_FFI_API void BNDSCViewFreeLoadedRegions(BNDSCMappedMemoryRegion* images, size_t count);
102+
SHAREDCACHE_FFI_API void BNSharedCacheFreeRegion(BNSharedCacheRegion region);
103+
SHAREDCACHE_FFI_API void BNSharedCacheFreeRegions(BNSharedCacheRegion* regions, size_t count);
149104

150-
SHAREDCACHE_FFI_API BNDSCImage* BNDSCViewGetAllImages(BNSharedCache* cache, size_t* count);
151-
SHAREDCACHE_FFI_API void BNDSCViewFreeAllImages(BNDSCImage* images, size_t count);
105+
SHAREDCACHE_FFI_API BNSharedCacheImage* BNSharedCacheControllerGetImages(BNSharedCacheController* controller, size_t* count);
106+
SHAREDCACHE_FFI_API BNSharedCacheImage* BNSharedCacheControllerGetLoadedImages(BNSharedCacheController* controller, size_t* count);
152107

153-
SHAREDCACHE_FFI_API BNDSCBackingCache* BNDSCViewGetBackingCaches(BNSharedCache* cache, size_t* count);
154-
SHAREDCACHE_FFI_API void BNDSCViewFreeBackingCaches(BNDSCBackingCache* caches, size_t count);
108+
SHAREDCACHE_FFI_API void BNSharedCacheFreeImage(BNSharedCacheImage image);
109+
SHAREDCACHE_FFI_API void BNSharedCacheFreeImages(BNSharedCacheImage* images, size_t count);
110+
155111

156-
SHAREDCACHE_FFI_API void BNDSCFindSymbolAtAddressAndApplyToAddress(BNSharedCache* cache, uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis);
112+
SHAREDCACHE_FFI_API BNSharedCacheEntry* BNSharedCacheControllerGetEntries(BNSharedCacheController* controller, size_t* count);
157113

158-
SHAREDCACHE_FFI_API char* BNDSCViewGetImageHeaderForAddress(BNSharedCache* cache, uint64_t address);
159-
SHAREDCACHE_FFI_API char* BNDSCViewGetImageHeaderForName(BNSharedCache* cache, const char* name);
114+
SHAREDCACHE_FFI_API void BNSharedCacheFreeEntry(BNSharedCacheEntry entry);
115+
SHAREDCACHE_FFI_API void BNSharedCacheFreeEntries(BNSharedCacheEntry* entries, size_t count);
160116

161-
SHAREDCACHE_FFI_API BNDSCMemoryUsageInfo BNDSCViewGetMemoryUsageInfo();
117+
// SHAREDCACHE_FFI_API void BNDSCFindSymbolAtAddressAndApplyToAddress(BNSharedCacheController* cache, uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis);
162118

119+
SHAREDCACHE_FFI_API char* BNSharedCacheControllerGetImageHeaderForAddress(BNSharedCacheController* controller, uint64_t address);
120+
SHAREDCACHE_FFI_API char* BNSharedCacheControllerGetImageHeaderForName(BNSharedCacheController* controller, const char* name);
163121
#ifdef __cplusplus
164122
}
165123
#endif

view/sharedcache/core/FileAccessorCache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class WeakFileAccessor
4545
std::weak_ptr<MappedFileAccessor> m_weakPtr;
4646
// File path for re-opening if needed
4747
std::string m_filePath;
48-
// TODO: Store a shared_ptr to FileAccessorCache?
48+
// TODO: Store a weak_ptr/shared_ptr to FileAccessorCache? That way we dont access Global()
49+
// TODO: Only need to do the above if we want multiple caches.
4950
public:
5051
explicit WeakFileAccessor(std::weak_ptr<MappedFileAccessor> weakPtr, std::string filePath)
5152
: m_weakPtr(std::move(weakPtr))

view/sharedcache/core/MachO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void SharedCacheMachOProcessor::InitializeHeaderForView(Ref<BinaryView> view, Sh
436436
header.ApplyHeaderSections(view);
437437

438438
// TODO: Wtf is this?
439-
/bool applyHeaderTypes = false;
439+
bool applyHeaderTypes = false;
440440
// for (const auto& region : regionsToLoad)
441441
// {
442442
// if (header.textBase >= region->start && header.textBase < region->start + region->size)

0 commit comments

Comments
 (0)