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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#pragma once
#include <binaryninjaapi.h>
#include "kernelcachecore.h"
template<class T>
class KCRefCountObject {
void AddRefInternal() { m_refs.fetch_add(1); }
void ReleaseInternal() {
if (m_refs.fetch_sub(1) == 1)
delete this;
}
public:
std::atomic<int> m_refs;
T *m_object;
KCRefCountObject() : m_refs(0), m_object(nullptr) {}
virtual ~KCRefCountObject() = default;
T *GetObject() const { return m_object; }
static T *GetObject(KCRefCountObject *obj) {
if (!obj)
return nullptr;
return obj->GetObject();
}
void AddRef() { AddRefInternal(); }
void Release() { ReleaseInternal(); }
void AddRefForRegistration() { AddRefInternal(); }
};
template<class T, T *(*AddObjectReference)(T *), void (*FreeObjectReference)(T *)>
class KCCoreRefCountObject {
void AddRefInternal() { m_refs.fetch_add(1); }
void ReleaseInternal() {
if (m_refs.fetch_sub(1) == 1) {
if (!m_registeredRef)
delete this;
}
}
public:
std::atomic<int> m_refs;
bool m_registeredRef = false;
T *m_object;
KCCoreRefCountObject() : m_refs(0), m_object(nullptr) {}
virtual ~KCCoreRefCountObject() = default;
T *GetObject() const { return m_object; }
static T *GetObject(KCCoreRefCountObject *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 T>
class KCRef
{
T* m_obj;
#ifdef BN_REF_COUNT_DEBUG
void* m_assignmentTrace = nullptr;
#endif
public:
KCRef() : m_obj(NULL) {}
KCRef(T* obj) : m_obj(obj)
{
if (m_obj)
{
m_obj->AddRef();
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name());
#endif
}
}
KCRef(const KCRef<T>& obj) : m_obj(obj.m_obj)
{
if (m_obj)
{
m_obj->AddRef();
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name());
#endif
}
}
KCRef(KCRef<T>&& other) : m_obj(other.m_obj)
{
other.m_obj = 0;
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = other.m_assignmentTrace;
#endif
}
~KCRef()
{
if (m_obj)
{
m_obj->Release();
#ifdef BN_REF_COUNT_DEBUG
BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace);
#endif
}
}
KCRef<T>& operator=(const BinaryNinja::Ref<T>& 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;
}
KCRef<T>& operator=(KCRef<T>&& other)
{
if (m_obj)
{
#ifdef BN_REF_COUNT_DEBUG
BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace);
#endif
m_obj->Release();
}
m_obj = other.m_obj;
other.m_obj = 0;
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = other.m_assignmentTrace;
#endif
return *this;
}
KCRef<T>& 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 KCRef<T>& 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 KCRef<T>& 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 KCRef<T>& obj) const
{
return T::GetObject(m_obj) < T::GetObject(obj.m_obj);
}
T* GetPtr() const
{
return m_obj;
}
};
namespace KernelCacheAPI {
struct CacheMappingInfo
{
uint64_t vmAddress;
uint64_t size;
uint64_t fileOffset;
};
struct CacheImage
{
uint64_t headerFileAddress;
uint64_t headerVirtualAddress;
std::string name;
};
struct CacheEntry
{
std::string path;
std::string name;
BNKernelCacheEntryType entryType;
std::vector<CacheMappingInfo> mappings;
};
struct CacheSymbol
{
BNSymbolType type;
uint64_t address;
std::string name;
std::pair<std::string, BinaryNinja::Ref<BinaryNinja::Type>> DemangledName(BinaryNinja::BinaryView &view) const;
BinaryNinja::Ref<BinaryNinja::Symbol> GetBNSymbol(BinaryNinja::BinaryView& view) const;
};
std::string GetSymbolTypeAsString(const BNSymbolType& type);
class KernelCacheController : public KCCoreRefCountObject<BNKernelCacheController, BNNewKernelCacheControllerReference, BNFreeKernelCacheControllerReference> {
public:
explicit KernelCacheController(BNKernelCacheController* controller);
static KCRef<KernelCacheController> GetController(BinaryNinja::BinaryView& view);
// 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);
bool IsImageLoaded(const CacheImage& image) const;
std::optional<CacheImage> GetImageAt(uint64_t address) const;
std::optional<CacheImage> GetImageContaining(uint64_t address) const;
std::optional<CacheImage> GetImageWithName(const std::string& name) const;
std::vector<std::string> GetImageDependencies(const CacheImage& image) const;
std::optional<CacheSymbol> GetSymbolAt(uint64_t address) const;
std::optional<CacheSymbol> GetSymbolWithName(const std::string& name) const;
std::vector<CacheImage> GetImages() const;
std::vector<CacheImage> GetLoadedImages() const;
std::vector<CacheSymbol> GetSymbols() const;
};
}
|