forked from jonpalmisc/ObjectiveNinja
-
Notifications
You must be signed in to change notification settings - Fork 8
/
GlobalState.h
137 lines (112 loc) · 3.06 KB
/
GlobalState.h
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2022-2023 Jon Palmisciano. All rights reserved.
*
* Use of this source code is governed by the BSD 3-Clause license; the full
* terms of the license can be found in the LICENSE.txt file.
*/
#pragma once
#include <condition_variable>
#include "BinaryNinja.h"
#include "MessageHandler.h"
/**
* Namespace to hold metadata flag key constants.
*/
namespace Flag {
constexpr auto DidRunWorkflow = "objectiveNinja.didRunWorkflow";
constexpr auto DidRunStructureAnalysis = "objectiveNinja.didRunStructureAnalysis";
}
struct AnalysisInfo {
std::uint64_t imageBase;
bool hasObjcStubs = false;
std::pair<uint64_t, uint64_t> objcStubsStartEnd;
std::unordered_map<uint64_t, std::vector<uint64_t>> selRefToImp;
std::unordered_map<uint64_t, std::vector<uint64_t>> selToImp;
};
typedef std::shared_ptr<AnalysisInfo> SharedAnalysisInfo;
class ReadWriteLock
{
public:
void lockRead()
{
std::unique_lock<std::mutex> lock(mutex);
readersCond.wait(lock, [this]() { return !writerActive; });
++readersActive;
}
void unlockRead()
{
std::unique_lock<std::mutex> lock(mutex);
if (--readersActive == 0)
writersCond.notify_one();
}
void lockWrite()
{
std::unique_lock<std::mutex> lock(mutex);
writersCond.wait(lock, [this]() { return !writerActive && readersActive == 0; });
writerActive = true;
}
void unlockWrite()
{
std::unique_lock<std::mutex> lock(mutex);
writerActive = false;
writersCond.notify_one();
readersCond.notify_all();
}
private:
std::mutex mutex;
std::condition_variable readersCond;
std::condition_variable writersCond;
int readersActive = 0;
bool writerActive = false;
};
class ReaderLock {
public:
ReaderLock(ReadWriteLock& lock) : lock(lock) { lock.lockRead(); }
~ReaderLock() { lock.unlockRead(); }
private:
ReadWriteLock& lock;
};
class WriterLock {
public:
WriterLock(ReadWriteLock& lock) : lock(lock) { lock.lockWrite(); }
~WriterLock() { lock.unlockWrite(); }
private:
ReadWriteLock& lock;
};
/**
* Global state/storage interface.
*/
class GlobalState {
/**
* Get the ID for a view.
*/
static BinaryViewID id(BinaryViewRef);
public:
/**
* Get the analysis info for a view.
*/
static SharedAnalysisInfo analysisInfo(BinaryViewRef);
/**
* Get ObjC Message Handler for a view
*/
static MessageHandler* messageHandler(BinaryViewRef);
/**
* Check if analysis info exists for a view.
*/
static bool hasAnalysisInfo(BinaryViewRef);
/**
* Add a view to the list of ignored views.
*/
static void addIgnoredView(BinaryViewRef);
/**
* Check if a view is ignored.
*/
static bool viewIsIgnored(BinaryViewRef);
/**
* Check if the a metadata flag is present for a view.
*/
static bool hasFlag(BinaryViewRef, const std::string&);
/**
* Set a metadata flag for a view.
*/
static void setFlag(BinaryViewRef, const std::string&);
};