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/core/SharedCacheController.h | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 view/sharedcache/core/SharedCacheController.h (limited to 'view/sharedcache/core/SharedCacheController.h') diff --git a/view/sharedcache/core/SharedCacheController.h b/view/sharedcache/core/SharedCacheController.h new file mode 100644 index 00000000..354762eb --- /dev/null +++ b/view/sharedcache/core/SharedCacheController.h @@ -0,0 +1,65 @@ +#pragma once + +#include + +#include "SharedCache.h" +#include "refcountobject.h" +#include "ffi_global.h" + +DECLARE_DSC_API_OBJECT(BNSharedCacheController, SharedCacheController); + +void RegisterSharedCacheControllerDestructor(); + +namespace BinaryNinja::DSC { + static const char* METADATA_KEY = "shared_cache"; + + // Represents the view state for a given `DSCache` + class SharedCacheController : public DSCRefCountObject + { + IMPLEMENT_DSC_API_OBJECT(BNSharedCacheController); + Ref m_logger; + + SharedCache m_cache; + // Store the open images. + // Things other than the cache here will be serialized. + std::unordered_set m_loadedRegions; + std::unordered_set m_loadedImages; + + // Settings from the view. + std::regex m_regionFilter; + bool m_processObjC; + bool m_processCFStrings; + + explicit SharedCacheController(SharedCache cache, Ref logger); + + public: + // Initialize the DSCacheView, this should be called from the view initialize function only! + static DSCRef Initialize(BinaryView& view, SharedCache cache); + + // NOTE: This will not create one if it does not exist. To create one for the view call `Initialize`. + static DSCRef FromView(BinaryView& view); + + SharedCache& GetCache() { return m_cache; }; + const std::unordered_set& GetLoadedRegions() { return m_loadedRegions; }; + const std::unordered_set& GetLoadedImages() { return m_loadedImages; }; + + // TODO: LoadResult type? AlreadyLoaded, Loaded, NotLoaded. + // NOTE: `address` should be the start of a region, not containing the address. + bool ApplyRegionAtAddress(BinaryView& view, uint64_t address); + + bool ApplyRegion(BinaryView& view, const CacheRegion& region); + + bool IsRegionLoaded(const CacheRegion& region) const; + + // Loads the relevant image info into the view. This does not update analysis so if you + // call this make sure at some point you update analysis and likely with linear sweep. + bool ApplyImage(BinaryView& view, const CacheImage& image); + + bool IsImageLoaded(const CacheImage& image) const; + + // Get the metadata for saving the state of the shared cache. + Ref GetMetadata() const; + + void LoadMetadata(const Metadata& metadata); + }; +} // namespace BinaryNinja::DSC -- cgit v1.3.1