From 25cc02431b61097b2adfc2fbc493b648b0300c3b Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Mon, 10 Mar 2025 11:05:40 -0400 Subject: [SharedCache] Refactor Shared Cache In absence of a better name, this commit refactors the shared cache code. --- view/sharedcache/api/sharedcacheapi.h | 516 ++++++++++++++++++---------------- 1 file changed, 279 insertions(+), 237 deletions(-) (limited to 'view/sharedcache/api/sharedcacheapi.h') diff --git a/view/sharedcache/api/sharedcacheapi.h b/view/sharedcache/api/sharedcacheapi.h index 5c50e283..e12582f2 100644 --- a/view/sharedcache/api/sharedcacheapi.h +++ b/view/sharedcache/api/sharedcacheapi.h @@ -1,285 +1,327 @@ #pragma once #include -#include "../core/MetadataSerializable.hpp" -#include "view/macho/machoview.h" #include "sharedcachecore.h" -namespace SharedCacheAPI { - template - class SCRefCountObject { - void AddRefInternal() { m_refs.fetch_add(1); } +template +class DSCRefCountObject { + void AddRefInternal() { m_refs.fetch_add(1); } - void ReleaseInternal() { - if (m_refs.fetch_sub(1) == 1) - delete this; - } + void ReleaseInternal() { + if (m_refs.fetch_sub(1) == 1) + delete this; + } - public: - std::atomic m_refs; - T *m_object; +public: + std::atomic m_refs; + T *m_object; - SCRefCountObject() : m_refs(0), m_object(nullptr) {} + DSCRefCountObject() : m_refs(0), m_object(nullptr) {} - virtual ~SCRefCountObject() {} + virtual ~DSCRefCountObject() = default; - T *GetObject() const { return m_object; } + T *GetObject() const { return m_object; } - static T *GetObject(SCRefCountObject *obj) { - if (!obj) - return nullptr; - return obj->GetObject(); - } + static T *GetObject(DSCRefCountObject *obj) { + if (!obj) + return nullptr; + return obj->GetObject(); + } - void AddRef() { AddRefInternal(); } + void AddRef() { AddRefInternal(); } - void Release() { ReleaseInternal(); } + void Release() { ReleaseInternal(); } - void AddRefForRegistration() { AddRefInternal(); } - }; + void AddRefForRegistration() { AddRefInternal(); } +}; - template - class SCCoreRefCountObject { - void AddRefInternal() { m_refs.fetch_add(1); } +template +class DSCCoreRefCountObject { + void AddRefInternal() { m_refs.fetch_add(1); } - void ReleaseInternal() { - if (m_refs.fetch_sub(1) == 1) { - if (!m_registeredRef) - delete this; - } + void ReleaseInternal() { + if (m_refs.fetch_sub(1) == 1) { + if (!m_registeredRef) + delete this; } - - public: - std::atomic m_refs; - bool m_registeredRef = false; - T *m_object; - - SCCoreRefCountObject() : m_refs(0), m_object(nullptr) {} - - virtual ~SCCoreRefCountObject() {} - - T *GetObject() const { return m_object; } - - static T *GetObject(SCCoreRefCountObject *obj) { - if (!obj) - return nullptr; - return obj->GetObject(); + } + +public: + std::atomic m_refs; + bool m_registeredRef = false; + T *m_object; + + DSCCoreRefCountObject() : m_refs(0), m_object(nullptr) {} + + virtual ~DSCCoreRefCountObject() = default; + + T *GetObject() const { return m_object; } + + static T *GetObject(DSCCoreRefCountObject *obj) { + if (!obj) + return nullptr; + return obj->GetObject(); + } + + void AddRef() { + if (m_object && (m_refs != 0)) + AddObjectReference(m_object); + AddRefInternal(); + } + + void Release() { + if (m_object) + FreeObjectReference(m_object); + ReleaseInternal(); + } + + void AddRefForRegistration() { m_registeredRef = true; } + + void ReleaseForRegistration() { + m_object = nullptr; + m_registeredRef = false; + if (m_refs == 0) + delete this; + } +}; + +template +class DSCRef +{ + T* m_obj; +#ifdef BN_REF_COUNT_DEBUG + void* m_assignmentTrace = nullptr; +#endif + +public: + DSCRef() : m_obj(NULL) {} + + DSCRef(T* obj) : m_obj(obj) + { + if (m_obj) + { + m_obj->AddRef(); +#ifdef BN_REF_COUNT_DEBUG + m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name()); +#endif } - - void AddRef() { - if (m_object && (m_refs != 0)) - AddObjectReference(m_object); - AddRefInternal(); + } + + DSCRef(const DSCRef& obj) : m_obj(obj.m_obj) + { + if (m_obj) + { + m_obj->AddRef(); +#ifdef BN_REF_COUNT_DEBUG + m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name()); +#endif } - - void Release() { - if (m_object) - FreeObjectReference(m_object); - ReleaseInternal(); + } + + DSCRef(DSCRef&& other) : m_obj(other.m_obj) + { + other.m_obj = 0; +#ifdef BN_REF_COUNT_DEBUG + m_assignmentTrace = other.m_assignmentTrace; +#endif + } + + ~DSCRef() + { + if (m_obj) + { + m_obj->Release(); +#ifdef BN_REF_COUNT_DEBUG + BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace); +#endif } - - void AddRefForRegistration() { m_registeredRef = true; } - - void ReleaseForRegistration() { - m_object = nullptr; - m_registeredRef = false; - if (m_refs == 0) - delete this; + } + + DSCRef& operator=(const BinaryNinja::Ref& obj) + { +#ifdef BN_REF_COUNT_DEBUG + if (m_obj) + BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace); + if (obj.m_obj) + m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name()); +#endif + T* oldObj = m_obj; + m_obj = obj.m_obj; + if (m_obj) + m_obj->AddRef(); + if (oldObj) + oldObj->Release(); + return *this; + } + + DSCRef& operator=(DSCRef&& other) + { + if (m_obj) + { +#ifdef BN_REF_COUNT_DEBUG + BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace); +#endif + m_obj->Release(); } - }; - - struct DSCMemoryRegion { - uint64_t vmAddress; + m_obj = other.m_obj; + other.m_obj = 0; +#ifdef BN_REF_COUNT_DEBUG + m_assignmentTrace = other.m_assignmentTrace; +#endif + return *this; + } + + DSCRef& operator=(T* obj) + { +#ifdef BN_REF_COUNT_DEBUG + if (m_obj) + BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace); + if (obj) + m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name()); +#endif + T* oldObj = m_obj; + m_obj = obj; + if (m_obj) + m_obj->AddRef(); + if (oldObj) + oldObj->Release(); + return *this; + } + + operator T*() const + { + return m_obj; + } + + T* operator->() const + { + return m_obj; + } + + T& operator*() const + { + return *m_obj; + } + + bool operator!() const + { + return m_obj == NULL; + } + + bool operator==(const T* obj) const + { + return T::GetObject(m_obj) == T::GetObject(obj); + } + + bool operator==(const DSCRef& obj) const + { + return T::GetObject(m_obj) == T::GetObject(obj.m_obj); + } + + bool operator!=(const T* obj) const + { + return T::GetObject(m_obj) != T::GetObject(obj); + } + + bool operator!=(const DSCRef& obj) const + { + return T::GetObject(m_obj) != T::GetObject(obj.m_obj); + } + + bool operator<(const T* obj) const + { + return T::GetObject(m_obj) < T::GetObject(obj); + } + + bool operator<(const DSCRef& obj) const + { + return T::GetObject(m_obj) < T::GetObject(obj.m_obj); + } + + T* GetPtr() const + { + return m_obj; + } +}; + + + +// TODO: replace namespace? +namespace SharedCacheAPI { + struct CacheRegion + { + BNSharedCacheRegionType type; + std::string name; + uint64_t start; uint64_t size; - std::string prettyName; + std::optional imageStart; + BNSegmentFlag flags; }; - struct BackingCacheMapping { + std::string GetRegionTypeAsString(const BNSharedCacheRegionType& type); + + struct CacheMappingInfo + { uint64_t vmAddress; uint64_t size; uint64_t fileOffset; }; - struct BackingCache { - std::string path; - BNBackingCacheType cacheType; - std::vector mappings; - }; - - struct DSCImageMemoryMapping { - std::string filePath; + struct CacheImage + { + uint64_t headerAddress; std::string name; - uint64_t vmAddress; - uint64_t size; - bool loaded; - uint64_t rawViewOffset; + std::vector regionStarts; }; - struct DSCImage { - std::string name; - uint64_t headerAddress; - std::vector mappings; + struct CacheEntry + { + std::string path; + BNSharedCacheEntryType entryType; + std::vector mappings; }; - struct DSCSymbol { + struct CacheSymbol + { + BNSymbolType type; uint64_t address; - BinaryNinja::StringRef name; - std::string image; - }; - - struct SharedCacheMachOHeader : public SharedCacheCore::MetadataSerializable { - uint64_t textBase = 0; - uint64_t loadCommandOffset = 0; - mach_header_64 ident; - std::string identifierPrefix; - std::string installName; - - std::vector> entryPoints; - std::vector m_entryPoints; // list of entrypoints - - symtab_command symtab; - dysymtab_command dysymtab; - dyld_info_command dyldInfo; - routines_command_64 routines64; - function_starts_command functionStarts; - std::vector moduleInitSections; - linkedit_data_command exportTrie; - linkedit_data_command chainedFixups {}; - - uint64_t relocationBase; - // Section and program headers, internally use 64-bit form as it is a superset of 32-bit - std::vector segments; // only three types of sections __TEXT, __DATA, __IMPORT - segment_command_64 linkeditSegment; - std::vector sections; - std::vector sectionNames; - - std::vector symbolStubSections; - std::vector symbolPointerSections; - - std::vector dylibs; - - build_version_command buildVersion; - std::vector buildToolVersions; - - std::string exportTriePath; - - bool linkeditPresent = false; - bool dysymPresent = false; - bool dyldInfoPresent = false; - bool exportTriePresent = false; - bool chainedFixupsPresent = false; - bool routinesPresent = false; - bool functionStartsPresent = false; - bool relocatable = false; - - void Store(SharedCacheCore::SerializationContext& context) const { - MSS(textBase); - MSS(loadCommandOffset); - MSS_SUBCLASS(ident); - MSS(identifierPrefix); - MSS(installName); - MSS(entryPoints); - MSS(m_entryPoints); - MSS_SUBCLASS(symtab); - MSS_SUBCLASS(dysymtab); - MSS_SUBCLASS(dyldInfo); - MSS_SUBCLASS(routines64); - MSS_SUBCLASS(functionStarts); - MSS_SUBCLASS(moduleInitSections); - MSS_SUBCLASS(exportTrie); - MSS_SUBCLASS(chainedFixups); - MSS(relocationBase); - MSS_SUBCLASS(segments); - MSS_SUBCLASS(linkeditSegment); - MSS_SUBCLASS(sections); - MSS(sectionNames); - MSS_SUBCLASS(symbolStubSections); - MSS_SUBCLASS(symbolPointerSections); - MSS(dylibs); - MSS_SUBCLASS(buildVersion); - MSS_SUBCLASS(buildToolVersions); - MSS(exportTriePath); - MSS(linkeditPresent); - MSS(dysymPresent); - MSS(dyldInfoPresent); - MSS(exportTriePresent); - MSS(chainedFixupsPresent); - MSS(routinesPresent); - MSS(functionStartsPresent); - MSS(relocatable); - } - - static SharedCacheMachOHeader Load(SharedCacheCore::DeserializationContext& context) { - SharedCacheMachOHeader header; - header.MSL(textBase); - header.MSL(loadCommandOffset); - header.MSL(ident); - header.MSL(identifierPrefix); - header.MSL(installName); - header.MSL(entryPoints); - header.MSL(m_entryPoints); - header.MSL(symtab); - header.MSL(dysymtab); - header.MSL(dyldInfo); - header.MSL(routines64); - header.MSL(functionStarts); - header.MSL(moduleInitSections); - header.MSL(exportTrie); - header.MSL(chainedFixups); - header.MSL(relocationBase); - header.MSL(segments); - header.MSL(linkeditSegment); - header.MSL(sections); - header.MSL(sectionNames); - header.MSL(symbolStubSections); - header.MSL(symbolPointerSections); - header.MSL(dylibs); - header.MSL(buildVersion); - header.MSL(buildToolVersions); - header.MSL(exportTriePath); - header.MSL(linkeditPresent); - header.MSL(dysymPresent); - header.MSL(dyldInfoPresent); - header.MSL(exportTriePresent); - header.MSL(chainedFixupsPresent); - header.MSL(routinesPresent); - header.MSL(functionStartsPresent); - header.MSL(relocatable); - return header; - } + std::string name; }; - - class SharedCache : public SCCoreRefCountObject { + class SharedCacheController : public DSCCoreRefCountObject { public: - SharedCache(Ref view); + explicit SharedCacheController(BNSharedCacheController* controller); + static DSCRef GetController(BinaryNinja::BinaryView& view); - BNDSCViewState GetState(); - static BNDSCViewLoadProgress GetLoadProgress(Ref view); - static uint64_t FastGetBackingCacheCount(Ref view); + bool ApplyRegion(BinaryNinja::BinaryView& view, const CacheRegion& region); - bool LoadImageWithInstallName(std::string installName, bool skipObjC = false); - bool LoadSectionAtAddress(uint64_t addr); - bool LoadImageContainingAddress(uint64_t addr, bool skipObjC = false); - std::vector GetAvailableImages(); - - void ProcessObjCSectionsForImageWithInstallName(std::string installName); - void ProcessAllObjCSections(); + // Attempt to load the given image into the view. + // + // It is the callers responsibility to run linear sweep and update analysis, as you might want to add + // multiple images at a time. + bool ApplyImage(BinaryNinja::BinaryView& view, const CacheImage& image); - std::vector LoadAllSymbolsAndWait(); + bool IsRegionLoaded(const CacheRegion& region) const; + bool IsImageLoaded(const CacheImage& image) const; - std::string GetNameForAddress(uint64_t address); - std::string GetImageNameForAddress(uint64_t address); + std::optional GetRegionAt(uint64_t address) const; + std::optional GetRegionContaining(uint64_t address) const; - std::vector GetBackingCaches(); - std::vector GetImages(); + std::optional GetImageAt(uint64_t address) const; + std::optional GetImageContaining(uint64_t address) const; + std::optional GetImageWithName(const std::string& name) const; - std::optional GetMachOHeaderForImage(std::string name); - std::optional GetMachOHeaderForAddress(uint64_t address); + std::vector GetImageDependencies(const CacheImage& image) const; - std::vector GetLoadedMemoryRegions(); + std::optional GetSymbolAt(uint64_t address) const; + std::optional GetSymbolWithName(const std::string& name) const; - void FindSymbolAtAddrAndApplyToAddr(uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis = true) const; + std::vector GetEntries() const; + std::vector GetRegions() const; + std::vector GetLoadedRegions() const; + std::vector GetImages() const; + std::vector GetLoadedImages() const; + std::vector GetSymbols() const; }; -} \ No newline at end of file +} -- cgit v1.3.1