summaryrefslogtreecommitdiff
path: root/view/sharedcache/core/SharedCacheController.h
blob: 3b92f198379b153105de9a2837cf553ff62c45ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include <regex>

#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<Logger> m_logger;
		SharedCache m_cache;

		// Locks on load attempts (region or image).
		std::shared_mutex m_loadMutex;

		// Store the open images.
		// Things other than the cache here will be serialized.
		std::unordered_set<uint64_t> m_loadedRegions;
		std::unordered_set<uint64_t> m_loadedImages;

		// Settings from the view.
		std::regex m_regionFilter;
		bool m_processObjC;
		bool m_processCFStrings;

		explicit SharedCacheController(SharedCache cache, Ref<Logger> logger);

	public:
		// Initialize the DSCacheView, this should be called from the view initialize function only!
		static DSCRef<SharedCacheController> 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<SharedCacheController> FromView(BinaryView& view);

		SharedCache& GetCache() { return m_cache; };
		const std::unordered_set<uint64_t>& GetLoadedRegions() { return m_loadedRegions; };
		const std::unordered_set<uint64_t>& 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);

		// 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);

		// Get the metadata for saving the state of the shared cache.
		Ref<Metadata> GetMetadata() const;

		void LoadMetadata(const Metadata& metadata);
	};
}  // namespace BinaryNinja::DSC