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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
#pragma once
#include <binaryninjaapi.h>
#include "warpcore.h"
template<class T, T *(*AddObjectReference)(T *), void (*FreeObjectReference)(T *)>
class WarpRefCountObject
{
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;
WarpRefCountObject() : m_refs(0), m_object(nullptr)
{
}
virtual ~WarpRefCountObject() = default;
T *GetObject() const { return m_object; }
static T *GetObject(WarpRefCountObject *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;
}
};
namespace Warp {
template<class T>
class Ref
{
T *m_obj;
#ifdef BN_REF_COUNT_DEBUG
void *m_assignmentTrace = nullptr;
#endif
public:
Ref() : m_obj(NULL)
{
}
Ref(T *obj) : m_obj(obj)
{
if (m_obj)
{
m_obj->AddRef();
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name());
#endif
}
}
Ref(const Ref &obj) : m_obj(obj.m_obj)
{
if (m_obj)
{
m_obj->AddRef();
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = BNRegisterObjectRefDebugTrace(typeid(T).name());
#endif
}
}
Ref(Ref &&other) : m_obj(other.m_obj)
{
other.m_obj = 0;
#ifdef BN_REF_COUNT_DEBUG
m_assignmentTrace = other.m_assignmentTrace;
#endif
}
~Ref()
{
if (m_obj)
{
m_obj->Release();
#ifdef BN_REF_COUNT_DEBUG
BNUnregisterObjectRefDebugTrace(typeid(T).name(), m_assignmentTrace);
#endif
}
}
Ref<T> &operator=(const 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;
}
Ref<T> &operator=(Ref<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;
}
Ref<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 Ref<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 Ref<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 Ref<T> &obj) const
{
return T::GetObject(m_obj) < T::GetObject(obj.m_obj);
}
T *GetPtr() const
{
return m_obj;
}
};
class WarpUUID
{
BNWARPUUID uuid;
public:
WarpUUID() = default;
WarpUUID(BNWARPUUID uuid) : uuid(uuid)
{
}
static std::optional<WarpUUID> FromString(const std::string &str);
std::string ToString() const;
bool operator==(const WarpUUID &other) const
{
return BNWARPUUIDEqual(&uuid, &other.uuid);
}
bool operator!=(const WarpUUID &other) const
{
return !(*this == other);
}
BNWARPUUID *RawMut()
{
return &uuid;
}
const BNWARPUUID *Raw() const
{
return &uuid;
}
};
typedef WarpUUID Source;
typedef WarpUUID BasicBlockGUID;
typedef WarpUUID FunctionGUID;
typedef WarpUUID ConstraintGUID;
typedef WarpUUID TypeGUID;
typedef std::string SourceTag;
class Target : public WarpRefCountObject<BNWARPTarget, BNWARPNewTargetReference,
BNWARPFreeTargetReference>
{
public:
explicit Target(BNWARPTarget *target);
static Ref<Target> FromPlatform(const BinaryNinja::Platform &platform);
};
class Type : public WarpRefCountObject<BNWARPType, BNWARPNewTypeReference,
BNWARPFreeTypeReference>
{
public:
explicit Type(BNWARPType *type);
[[nodiscard]] static Ref<Type> FromAnalysisType(const BinaryNinja::Type &type, uint8_t confidence);
std::optional<std::string> GetName() const;
uint8_t GetConfidence() const;
[[nodiscard]] BinaryNinja::Ref<BinaryNinja::Type> GetAnalysisType(BinaryNinja::Architecture* arch = nullptr) const;
};
struct Constraint
{
ConstraintGUID guid {};
std::optional<int64_t> offset;
Constraint(ConstraintGUID guid, std::optional<int64_t> offset);
static Constraint FromAPIObject(BNWARPConstraint *constraint);
};
struct FunctionComment
{
std::string text;
int64_t offset;
FunctionComment(std::string text, int64_t offset);
static FunctionComment FromAPIObject(BNWARPFunctionComment *comment);
};
class Function : public WarpRefCountObject<BNWARPFunction, BNWARPNewFunctionReference, BNWARPFreeFunctionReference>
{
public:
explicit Function(BNWARPFunction *function);
bool operator==(const Function &other) const
{
return BNWARPFunctionsEqual(m_object, other.m_object);
}
FunctionGUID GetGUID() const;
std::string GetSymbolName() const;
BinaryNinja::Ref<BinaryNinja::Symbol> GetSymbol(const BinaryNinja::Function &function) const;
Ref<Type> GetType() const;
std::vector<Constraint> GetConstraints() const;
std::vector<FunctionComment> GetComments() const;
static Ref<Function> Get(const BinaryNinja::Function &function);
static Ref<Function> GetMatched(const BinaryNinja::Function &function);
void Apply(const BinaryNinja::Function &function) const;
static void RemoveMatch(const BinaryNinja::Function &function);
};
class ContainerSearchQuery : public WarpRefCountObject<BNWARPContainerSearchQuery,
BNWARPNewContainerSearchQueryReference,
BNWARPFreeContainerSearchQueryReference>
{
public:
explicit ContainerSearchQuery(BNWARPContainerSearchQuery *query);
explicit ContainerSearchQuery(const std::string &query);
ContainerSearchQuery(const std::string &query, const Source &source);
ContainerSearchQuery(const std::string &query, size_t offset, size_t limit,
const std::optional<Source> &source = std::nullopt,
const std::vector<SourceTag> &tags = {});
};
class ContainerSearchItem : public WarpRefCountObject<BNWARPContainerSearchItem,
BNWARPNewContainerSearchItemReference,
BNWARPFreeContainerSearchItemReference>
{
public:
explicit ContainerSearchItem(BNWARPContainerSearchItem *item);
BNWARPContainerSearchItemKind GetKind() const;
Source GetSource() const;
Ref<Type> GetType() const;
std::string GetName() const;
Ref<Function> GetFunction() const;
};
struct ContainerSearchResponse
{
std::vector<Ref<ContainerSearchItem>> items;
size_t offset;
size_t total;
ContainerSearchResponse(std::vector<Ref<ContainerSearchItem> > &&items, size_t offset, size_t total);
static ContainerSearchResponse FromAPIObject(BNWARPContainerSearchResponse *response);
};
class Container : public WarpRefCountObject<BNWARPContainer, BNWARPNewContainerReference,
BNWARPFreeContainerReference>
{
public:
explicit Container(BNWARPContainer *container);
/// Retrieve all available containers.
static std::vector<Ref<Container>> All();
/// Add a new container with the given name.
static Ref<Container> Add(const std::string &name);
std::string GetName() const;
std::vector<Source> GetSources() const;
std::optional<Source> AddSource(const std::string &sourcePath) const;
bool CommitSource(const Source &source) const;
bool IsSourceUncommitted(const Source &source) const;
bool IsSourceWritable(const Source &source) const;
std::optional<std::string> SourcePath(const Source &source) const;
bool AddFunctions(const Target &target, const Source &source,
const std::vector<Ref<Function>> &functions) const;
bool AddTypes(const Source &source, const std::vector<Ref<Type>> &types) const;
bool RemoveFunctions(const Target &target, const Source &source,
const std::vector<Ref<Function>> &functions) const;
bool RemoveTypes(const Source &source, const std::vector<TypeGUID> &guids) const;
void FetchFunctions(const Target &target, const std::vector<FunctionGUID> &guids, const std::vector<SourceTag> &tags = {}, const std::vector<ConstraintGUID> &constraints = {}) const;
std::vector<Source> GetSourcesWithFunctionGUID(const Target &target, const FunctionGUID &guid) const;
std::vector<Source> GetSourcesWithTypeGUID(const TypeGUID &guid) const;
std::vector<Ref<Function> > GetFunctionsWithGUID(const Target &target, const Source &source,
const FunctionGUID &guid) const;
Ref<Type> GetTypeWithGUID(const Source &source, const TypeGUID &guid) const;
std::vector<TypeGUID> GetTypeGUIDsWithName(const Source &source, const std::string &name) const;
std::optional<ContainerSearchResponse> Search(const ContainerSearchQuery &query) const;
};
class Chunk : public WarpRefCountObject<BNWARPChunk, BNWARPNewChunkReference, BNWARPFreeChunkReference>
{
friend class File;
public:
explicit Chunk(BNWARPChunk *chunk);
Ref<Target> GetTarget() const;
[[nodiscard]] std::vector<Ref<Function>> GetFunctions() const;
[[nodiscard]] std::vector<Ref<Type>> GetTypes() const;
};
class File : public WarpRefCountObject<BNWARPFile, BNWARPNewFileReference, BNWARPFreeFileReference>
{
public:
explicit File(BNWARPFile *file);
static Ref<File> FromPath(const std::string &path);
[[nodiscard]] std::vector<Ref<Chunk>> GetChunks() const;
[[nodiscard]] BinaryNinja::DataBuffer ToDataBuffer() const;
};
class ProcessorState
{
public:
std::vector<std::string> analyzingFiles;
std::vector<std::string> processingFiles;
bool cancelled;
size_t unprocessedFilesCount;
size_t processedFilesCount;
ProcessorState() = default;
static ProcessorState FromAPIObject(BNWARPProcessorState *state);
};
class Processor
{
BNWARPProcessor* m_object;
public:
explicit Processor(BNWARPProcessorIncludedData includedData,
BNWARPProcessorIncludedFunctions includedFunctions, size_t workerCount);
~Processor();
void AddPath(const std::string &path) const;
void AddProject(const BinaryNinja::Project &project) const;
void AddProjectFile(const BinaryNinja::ProjectFile &projectFile) const;
void AddBinaryView(const BinaryNinja::BinaryView &view) const;
Ref<File> Start() const;
void Cancel() const;
ProcessorState GetState() const;
};
void RunMatcher(const BinaryNinja::BinaryView &view);
bool IsInstructionVariant(const BinaryNinja::LowLevelILFunction &function, BinaryNinja::ExprId idx);
bool IsInstructionBlacklisted(const BinaryNinja::LowLevelILFunction &function, BinaryNinja::ExprId idx);
std::optional<FunctionGUID> GetAnalysisFunctionGUID(const BinaryNinja::Function &function);
std::optional<BasicBlockGUID> GetBasicBlockGUID(const BinaryNinja::BasicBlock &basicBlock);
}
template<> struct std::hash<Warp::WarpUUID>
{
size_t operator()(Warp::WarpUUID const& item) const noexcept
{
// TODO: Use the raw bytes instead.
return std::hash<std::string>()(item.ToString());
}
};
|