-
Notifications
You must be signed in to change notification settings - Fork 15
/
near_core.cpp
527 lines (462 loc) · 24.1 KB
/
near_core.cpp
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
518
519
520
521
522
523
524
525
526
527
#include "near_core.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Metadata.h"
namespace Rustle {
void simpleFindUsers(llvm::Value *value, std::set<llvm::Value *> &set, bool restrictCrossFunction, bool disableCrossFunction) {
using namespace llvm;
auto *const VALUE_TYPE = value->getType();
if (!VALUE_TYPE || VALUE_TYPE->isLabelTy())
return;
if (set.find(value) != set.end())
return;
set.insert(value);
if (auto *callInst = dyn_cast<CallBase>(value)) {
for (unsigned i = 0; i < callInst->arg_size(); i++) {
if (!disableCrossFunction && callInst->getCalledFunction() && (!restrictCrossFunction || set.count(callInst->getArgOperand(i))))
simpleFindUsers(callInst->getCalledFunction()->getArg(i), set, restrictCrossFunction, disableCrossFunction); // cross-function
if (callInst->getCalledFunction() && callInst->getCalledFunction()->getName().contains("core..convert..Into") && callInst->arg_size() >= 2 && set.count(callInst->getArgOperand(1))) {
simpleFindUsers(callInst->getArgOperand(0), set, restrictCrossFunction, disableCrossFunction); // add `xxx.into()` as user of `xxx`
}
}
} else if (auto *stInst = dyn_cast<StoreInst>(value)) {
simpleFindUsers(stInst->getPointerOperand(), set, restrictCrossFunction, disableCrossFunction);
}
for (auto *U : value->users()) {
simpleFindUsers(U, set, restrictCrossFunction, disableCrossFunction);
}
}
void findUsers(llvm::Value *value, std::set<llvm::Value *> &set, const int GEP_OFFSET, int depth) {
using namespace llvm;
if (depth <= 0)
return;
if (set.count(value) == 1)
return;
auto *const VALUE_TYPE = value->getType();
if (!VALUE_TYPE || VALUE_TYPE->isLabelTy())
return;
set.insert(value);
if (auto *unaInst = dyn_cast<UnaryInstruction>(value)) { // alloca, cast, extract, freeze, load, unaryOp, VAA
if (GEP_OFFSET != -1) {
if (auto *bcInst = dyn_cast<BitCastInst>(value)) {
std::string const resType = printToString(bcInst->getDestTy()), srcType = printToString(bcInst->getSrcTy());
if (GEP_OFFSET != 0 && (StringRef(resType).contains("solana_program::pubkey::Pubkey") && StringRef(srcType).contains("solana_program::account_info::AccountInfo") ||
(StringRef(resType).contains("anchor_lang::prelude::Pubkey") && StringRef(srcType).contains("anchor_lang::prelude::AccountInfo"))))
return; // BitCast Not allowed for Offset!=0
}
}
for (auto *U : unaInst->users())
findUsers(U, set, GEP_OFFSET, depth - 1);
} else if (auto *binOpInst = dyn_cast<BinaryOperator>(value)) {
for (auto *U : binOpInst->users())
findUsers(U, set, GEP_OFFSET, depth - 1);
} else if (auto *gepInst = dyn_cast<GetElementPtrInst>(value)) {
bool goFurther = GEP_OFFSET == -1;
if (!goFurther) {
std::string const resType = printToString(gepInst->getResultElementType()), srcType = printToString(gepInst->getSourceElementType());
switch (dyn_cast<ConstantInt>(gepInst->getOperand(gepInst->getNumOperands() - 1))->getZExtValue()) {
// more cases can be set
case 3: // owner field
if (GEP_OFFSET == 3 && (StringRef(resType).contains("solana_program::pubkey::Pubkey") && StringRef(srcType).contains("solana_program::account_info::AccountInfo")) ||
(StringRef(resType).contains("anchor_lang::prelude::Pubkey") && StringRef(srcType).contains("anchor_lang::prelude::AccountInfo")))
goFurther = true;
break;
default: goFurther = true; break;
}
}
if (goFurther)
for (auto *U : gepInst->users())
findUsers(U, set, GEP_OFFSET, depth - 1);
} else if (auto *stInst = dyn_cast<StoreInst>(value)) {
findUsers(stInst->getPointerOperand(), set, GEP_OFFSET, depth - 1);
} else if (auto *callInst = dyn_cast<CallBase>(value)) {
if (callInst->getCalledFunction() && callInst->getCalledFunction()->getName().startswith("llvm.memcpy") && callInst->getArgOperand(0)) {
for (auto *U : callInst->getArgOperand(0)->users()) // memcpy target
findUsers(U, set, GEP_OFFSET, depth - 1);
if (auto *cstInst = dyn_cast<CastInst>(callInst->getArgOperand(0))) { // memcpy target pointer
if (cstInst->getOperand(0)) {
for (auto *U : cstInst->getOperand(0)->users())
findUsers(U, set, GEP_OFFSET, depth - 1);
}
}
} else if (callInst->getCalledFunction() && callInst->getCalledFunction()->getName().contains("core..cmp")) {
// ends here, skip compare function
} else if (GEP_OFFSET != -1 && callInst->getCalledFunction() && Regex("solana_program[0-9]+program_pack[0-9]+Pack[0-9]+unpack").match(callInst->getCalledFunction()->getName())) {
// ends here, skip unpack result for owner-check
} else if (callInst->getCalledFunction() && callInst->getCalledFunction()->getName().contains("core..convert..Into") && callInst->arg_size() >= 2 &&
set.count(callInst->getArgOperand(1))) {
findUsers(callInst->getArgOperand(0), set, GEP_OFFSET, depth - 1); // add `xxx.into()` as user of `xxx`
} else {
// for (unsigned i = 0; i < CallInst->getCalledFunction()->arg_size(); i++)
// findUsers(CallInst->getCalledFunction()->getArg(i), set, GEPOffset, depth - 1); // cross function
for (unsigned i = 0; i < callInst->arg_size(); i++)
findUsers(callInst->getArgOperand(i), set, GEP_OFFSET, depth - 1); // function args
for (auto *U : callInst->users())
findUsers(U, set, GEP_OFFSET, depth - 1);
}
} else if (auto *inst = dyn_cast<Instruction>(value)) {
// ignore other instructions
} else {
for (auto *U : value->users()) {
if (set.find(U) != set.end())
continue;
findUsers(U, set);
}
}
}
bool isFuncPrivileged(llvm::Function *F, int const depth) {
using namespace llvm;
Regex const static regex_predecessor = Regex("near_sdk[0-9]+environment[0-9]+env[0-9]+predecessor_account_id");
Regex const static regex_eq = Regex("near_sdk\\.\\.types\\.\\.account_id\\.\\.AccountId.+core\\.\\.cmp\\.\\.PartialEq");
if (F == nullptr)
return false;
if (depth < 0)
return false;
for (BasicBlock &BB : *F) {
for (Instruction &I : BB) {
if (!I.getDebugLoc().get() || Rustle::regexForLibLoc.match(I.getDebugLoc().get()->getFilename()))
continue;
if (Rustle::isInstCallFunc(&I, regex_predecessor)) {
// has called `predecessor_account_id`, check whether calls `PartialEq` in current function
for (BasicBlock &BB : *F) {
for (Instruction &i : BB) {
if (Rustle::isInstCallFunc(&i, regex_eq))
return true;
}
}
} else if (CallBase *callInst = dyn_cast<llvm::CallBase>(&I)) { // other call inst
if (callInst->getCalledFunction()) { // indirect function invocation returns null
if (isFuncPrivileged(callInst->getCalledFunction(), depth - 1)) { // check callee function
return true;
}
}
}
}
}
// not a call inst
return false;
}
bool isInstCallFunc(llvm::Instruction *I, llvm::Regex const ®ex) {
if (llvm::CallBase *callInst = llvm::dyn_cast<llvm::CallBase>(I)) {
if (callInst->getCalledFunction()) {
llvm::StringRef const calleeName = callInst->getCalledFunction()->getName();
if (regex.match(calleeName)) {
return true;
}
if (debug_print_derive_pack && calleeName == "llvm.dbg.declare" && callInst->getArgOperand(0)) {
std::string const argMetadata = printToString(llvm::dyn_cast<llvm::MetadataAsValue>(callInst->getArgOperand(0))->getMetadata());
if (regex.match(llvm::StringRef(argMetadata)))
return true;
}
}
}
return false;
}
namespace { // Hide this func
bool isFuncCallFuncRec(llvm::CallGraphNode *node, llvm::Regex const ®ex, std::vector<llvm::CallGraphNode *> &callStack) {
if (!node)
return false;
// Find in current level
for (auto callee : *node) {
if (callee.second->getFunction() && regex.match(callee.second->getFunction()->getName()))
return true;
}
// Find in next level
for (auto callee : *node) {
if (callee.second == node || find(callStack.begin(), callStack.end(), node) != callStack.end()) // important, avoid loop
continue;
callStack.push_back(callee.second);
if (isFuncCallFuncRec(callee.second, regex, callStack))
return true;
callStack.pop_back();
}
return false;
}
} // namespace
// Recursive version
bool isInstCallFuncRec(llvm::Instruction *I, llvm::CallGraph &CG, llvm::Regex const ®ex) {
if (isInstCallFunc(I, regex))
return true;
if (llvm::CallBase *callInst = llvm::dyn_cast<llvm::CallBase>(I)) {
if (callInst->getCalledFunction()) {
if (llvm::Regex("llvm").match(callInst->getCalledFunction()->getName()))
return false;
std::vector<llvm::CallGraphNode *> callStack;
callStack.push_back(CG[callInst->getCalledFunction()]);
return isFuncCallFuncRec(CG[callInst->getCalledFunction()], regex, callStack);
}
}
return false;
}
// Recursive version
bool isFuncCallFuncRec(llvm::Function *F, llvm::CallGraph &CG, llvm::Regex const ®ex) {
if (llvm::Regex("llvm").match(F->getName()))
return false;
std::vector<llvm::CallGraphNode *> callStack;
callStack.push_back(CG[F]);
return isFuncCallFuncRec(CG[F], regex, callStack);
}
void findFunctionCallerRec(llvm::Function *F, std::set<llvm::Function *> &set, int depth) {
using namespace llvm;
if (depth <= 0)
return;
for (auto &ui : F->uses()) {
if (auto *callInst = dyn_cast<CallBase>(ui.getUser())) {
// Function *caller = callInst->getParent()->getParent();
Function *caller = callInst->getFunction();
if (caller && !set.count(caller)) {
set.insert(caller);
findFunctionCallerRec(caller, set, depth - 1);
}
}
}
}
namespace { // Hide this func
/**
* @brief Get the Mode of user
*
* @param user
* @param prevUser
* @return int based on Mode enum
*/
int getMode(llvm::Value *user, llvm::Value *prevUser) {
using namespace llvm;
// outs() << "user: " << *user << "\n";
if (isa<LoadInst>(user)) {
// outs() << "load\n";
return Mode::Read;
} else if (isa<StoreInst>(user)) {
// outs() << "store\n";
return Mode::Write;
} else if (auto *memcpyInst = dyn_cast<MemCpyInst>(user)) {
if (memcpyInst->getRawDest() == prevUser)
return Mode::Write;
else if (memcpyInst->getRawSource() == prevUser)
return Mode::Read;
else
return Mode::Unknown;
} else if (auto *callInst = dyn_cast<CallBase>(user)) {
// _ZN8near_sdk11collections13unordered_set21UnorderedSet$LT$T$GT$6insert17hf8c99f299d0290bbE
if (callInst->getCalledFunction()) {
if (Regex("near_sdk[0-9]+collections([0-9a-zA-Z]|_+)+(\\$|[0-9a-zA-Z])+(insert_raw|remove_raw|remove|insert|clear|iter|extend|as_vector|keys|values|range|push|pop|replace|swap_"
"remove)")
.match(callInst->getCalledFunction()->getName())) {
return Mode::Write;
} else if (Regex("near_sdk[0-9]+collections([0-9a-zA-Z]|_+)+(\\$|[0-9a-zA-Z])+(len|is_empty|new|contains|to_vec|get|floor_key|ceil_key)")
.match(callInst->getCalledFunction()->getName())) {
return Mode::Write;
} else if (Regex("core[0-9]+ptr[0-9]+drop_in_place").match(callInst->getCalledFunction()->getName())) {
return Mode::Read;
} else if (callInst->getCalledFunction()->getName().contains("clone")) {
return Mode::Read;
} else {
// outs() << "call\n";
// outs() << "hasArgument(prevUser): " << callInst->hasArgument(prevUser) << "\n";
// outs() << "callInst->arg_size(): " << callInst->arg_size() << "\n";
// outs() << "args_op: ";
int userNo = -1;
for (auto i = 0; i < callInst->arg_size(); i++) {
if (callInst->getArgOperand(i) == prevUser)
userNo = i;
// outs() << *callInst->getArgOperand(i) << " | ";
}
// outs() << "\n";
// outs() << "user_no: " << user_no << "\n";
if (userNo == -1) { // user not found in args
// outs() << "[ERR] user not found in args\n";
return Mode::Unknown;
}
// outs() << "args: ";
// for (auto &u : callInst->args())
// outs() << u->getName() << " ";
// outs() << "\n";
// outs() << "callInst->getCalledFunction()->arg_size(): " << callInst->getCalledFunction()->arg_size() << "\n";
// outs() << "arg passed: ";
// for (auto &i : callInst->getCalledFunction()->args())
// outs() << i.getName() << " | ";
// outs() << "\n\n";
prevUser = user;
user = callInst->getCalledFunction()->getArg(userNo);
return getMode(user, prevUser);
// outs() << "getCalledOperand: " << callInst->getCalledOperand()[user_no] << "\n";
}
} else
return Mode::Unknown;
} else { // Other instructions
// outs() << "user->getNumUses(): " << user->getNumUses() << "\n";
if (user->getNumUses() > 0) {
prevUser = user;
int result = Mode::Unknown;
for (auto *i : user->users()) {
user = dyn_cast<Instruction>(i);
result |= getMode(user, prevUser);
}
return result;
} else {
// outs() << "[ERR] user->getNumUses() == 0\n";
return Mode::Unknown;
}
}
}
} // namespace
std::pair<std::pair<std::string, unsigned>, Mode> usingStructMember(llvm::Instruction *I, std::set<std::pair<std::string, unsigned>> &vars) {
using namespace llvm;
Value *castValue = nullptr; // pointer to llvm::Value of member that has been cast from struct pointer, also serves as a flag
std::pair<std::string, unsigned> target; // pair of struct name and member offset
if (auto *gepInst = dyn_cast<GetElementPtrInst>(I)) { // for offset > 0
for (auto i : vars) {
std::string const srcMeta = printToString(gepInst->getSourceElementType());
if (srcMeta.find('=') == std::string::npos)
continue;
std::string structName = srcMeta.substr(1, srcMeta.find('=') - 2);
structName.erase(std::remove(structName.begin(), structName.end(), '"'), structName.end());
/* Control exact match */
// if (structName.find(i.first) != std::string::npos && dyn_cast<ConstantInt>(GEPInst->getOperand(GEPInst->getNumOperands() - 1))->equalsInt(i.second)) {
if (structName == i.first && dyn_cast<ConstantInt>(gepInst->getOperand(gepInst->getNumOperands() - 1))->equalsInt(i.second)) {
castValue = dyn_cast<Value>(I);
target = i;
break;
}
}
} else if (auto *CI = dyn_cast<CastInst>(I)) { // for offset = 0
for (auto i : vars) {
if (i.second != 0) // skip offset > 0
continue;
std::string const srcMeta = printToString(CI->getSrcTy());
if (srcMeta.find(i.first) == std::string::npos)
continue;
std::string structName = srcMeta.substr(1, srcMeta.size() - 2); // remove '%' and '*'
if (structName.at(structName.size() - 1) == '*') // make sure only 1 level deref
continue;
/* Control exact match */
// if (structName.find(i.first) != std::string::npos && i.second == 0) {
if (structName == i.first && i.second == 0) {
castValue = dyn_cast<Value>(I);
target = i;
break;
}
}
}
if (!castValue) // Not found
return std::make_pair(std::make_pair("", 0), Mode::Unknown);
// outs() << "I: " << *I << "\n";
if (!castValue->hasOneUser()) {
return std::make_pair(target, Mode::Unknown);
}
if (I->getNumUses() > 0) {
Value *user = dyn_cast<Value>(*I->user_begin());
Value *prevUser = I;
int const mode = getMode(user, prevUser);
switch (mode) {
case Mode::RW:
return std::make_pair(target, Mode::Write);
// return std::make_pair(target, Mode::RW);
case Mode::Write: return std::make_pair(target, Mode::Write);
case Mode::Read: return std::make_pair(target, Mode::Read);
default: return std::make_pair(target, Mode::Unknown);
}
} else
return std::make_pair(target, Mode::Unknown);
}
void readStructMember(std::ifstream &is, std::set<std::pair<std::string, unsigned>> &vars) {
if (Rustle::debug_print_tmp)
llvm::outs() << "[*] Reading variable list\n";
is.open("struct_member.conf");
std::string structName;
unsigned offset;
while (is >> structName) {
if (structName[0] == '#') {
is.ignore(200, '\n');
continue;
} else
is >> offset;
vars.insert(std::make_pair(structName, offset));
}
if (Rustle::debug_print_tmp) {
llvm::outs() << "[*] Get " << vars.size() << " variables\n";
for (auto i : vars)
llvm::outs() << i.first << " " << i.second << "\n";
}
}
std::pair<std::string, Mode> usingStruct(llvm::Instruction *I, std::set<std::string> &structs) {
using namespace llvm;
Value *castValue = nullptr; // pointer to llvm::Value of member that has been cast from struct pointer
// also serves as a flag
std::string target; // pair of struct name and member offset
if (auto *gepInst = dyn_cast<GetElementPtrInst>(I)) { // for offset > 0
std::string const srcMeta = printToString(gepInst->getSourceElementType());
if (srcMeta.find('=') == std::string::npos)
;
else if (srcMeta.size() >= 2) {
std::string structName = srcMeta.substr(1, srcMeta.find('=') - 2);
structName.erase(std::remove(structName.begin(), structName.end(), '"'), structName.end());
for (auto i : structs) {
/* Control exact match */
// if (structName.find(i.first) != std::string::npos && dyn_cast<ConstantInt>(GEPInst->getOperand(GEPInst->getNumOperands() - 1))->equalsInt(i.second)) {
if (structName == i) {
castValue = dyn_cast<Value>(I);
target = i;
break;
}
}
}
} else if (auto *CI = dyn_cast<CastInst>(I)) { // for offset = 0
std::string srcMeta = printToString(CI->getSrcTy());
if (srcMeta[0] == '%' && srcMeta[srcMeta.size() - 1] == '*') {
std::string structName = srcMeta.substr(1, srcMeta.size() - 2); // remove '%' and '*'
if (structName.at(structName.size() - 1) == '*') // make sure only 1 level deref
;
else
for (auto i : structs) {
if (srcMeta.find(i) == std::string::npos)
continue;
/* Control exact match */
if (structName == i) {
castValue = dyn_cast<Value>(I);
target = i;
break;
}
}
}
}
if (!castValue) // Not found
return std::make_pair("", Mode::Unknown);
// outs() << "I: " << *I << "\n";
if (!castValue->hasOneUser()) {
return std::make_pair(target, Mode::Unknown);
}
if (I->getNumUses() > 0) {
Value *user = dyn_cast<Value>(*I->user_begin());
Value *prevUser = I;
int const mode = getMode(user, prevUser);
switch (mode) {
case Mode::RW:
return std::make_pair(target, Mode::Write);
// return std::make_pair(target, Mode::RW);
case Mode::Write: return std::make_pair(target, Mode::Write);
case Mode::Read: return std::make_pair(target, Mode::Read);
default: return std::make_pair(target, Mode::Unknown);
}
} else
return std::make_pair(target, Mode::Unknown);
}
void readStructLog(std::ifstream &is, std::set<std::string> &structs) {
if (Rustle::debug_print_tmp)
llvm::outs() << "[*] Reading variable list\n";
is.open(std::string(getenv("TMP_DIR")) + std::string("/.structs.tmp"));
std::string structName;
unsigned offset;
while (is >> structName) {
if (structName[0] == '#') {
is.ignore(200, '\n');
continue;
} else
structs.insert(structName);
}
if (Rustle::debug_print_tmp) {
llvm::outs() << "[*] Get " << structs.size() << " struct\n";
for (auto i : structs)
llvm::outs() << i << "\n";
}
}
} // namespace Rustle