-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
759 lines (705 loc) · 27.2 KB
/
main.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <cstring>
#include "llvm/Support/CommandLine.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Lex/Lexer.h"
#include "main.hpp"
/// no nested starttime and stoptime
/// actually, getint and getch also has side effect not
/// associated with variable(e.g., getint() / getint() ). To work
/// around this, I introduce a special global varibale __globalInput__
/// and let getarray, getch... write this variable and the same for __globalOutput__.
// clang-format off
const char* declstr =
"int __globalInput__; int __globalOutput__;"
"int getint(){__globalInput__ = __globalInput__ + 1;return 1;} "
"int getch(){__globalInput__ = __globalInput__ + 1;return 1;} "
"int getarray(int a[]){a[0] = 1;__globalInput__ = __globalInput__ + 1;return 1;}"
"void putint(int){__globalOutput__ = __globalOutput__ + 1;}"
"void putch(int){__globalOutput__ = __globalOutput__ + 1;}"
"void putarray(int, int a[]){__globalOutput__ = __globalOutput__ + 1;a[0] + 1;}"
"void starttime(){}"
"void stoptime(){}\n";
// clang-format on
using namespace llvm;
using namespace clang;
using namespace clang::tooling;
using namespace clang::ast_matchers;
using sideEffectMap = DenseMap<FunctionDecl*, DenseSet<DeclRefExpr*>>;
static cl::opt<std::string> inputFile(cl::Required, cl::Positional,
cl::desc("input file"));
static cl::list<std::string> extraArgs("extra-arg", cl::value_desc("extra arg"),
cl::desc("extra compiler args"));
static cl::opt<bool> verbose("verbose", cl::value_desc("verbose"),
cl::desc("output addition runtime info"));
static cl::opt<bool> initExprDump("init-list-expr-dump",
cl::value_desc("list-expr-dump"),
cl::desc("dump init list expr info"));
static cl::opt<bool> stmtRefDump(
"stmt-ref-dump", cl::value_desc("stmt-ref-dump"),
cl::desc("output declref graph for each stmt"));
static cl::opt<bool> sideEffectDump(
"side-effect-dump", cl::value_desc("side-effect-dump"),
cl::desc("output side effect of each function"));
VarDecl* getDeclFromRef(DeclRefExpr* ref) {
auto sidevar = dyn_cast<VarDecl>(ref->getDecl());
if (!sidevar) {
auto n = ref->getDecl()->getName();
errs() << n << "\n";
assert(sidevar &&
"decl of any valid sysy file should only contain varDecl and "
"FunctionDecl\n");
}
return sidevar;
}
bool varIsGloal(VarDecl* decl) {
auto linkType = decl->getFormalLinkage();
assert((linkType == Linkage::ExternalLinkage ||
linkType == Linkage::NoLinkage) &&
"there should only be global or local var in sysY");
return linkType == Linkage::ExternalLinkage;
}
// https://stackoverflow.com/a/11154162/15570633
std::string getStmtString(Stmt* s, ASTContext* ctx) {
auto start = s->getBeginLoc();
auto& sm = ctx->getSourceManager();
auto end = Lexer::getLocForEndOfToken(s->getEndLoc(), 0, sm, LangOptions());
return std::string(sm.getCharacterData(start),
sm.getCharacterData(end) - sm.getCharacterData(start));
}
uint32_t getParamPos(ParmVarDecl* parm) {
auto f = cast<FunctionDecl>(parm->getDeclContext());
auto iter = std::find(f->param_begin(), f->param_end(), parm);
assert(iter != f->param_end() && "can't find parameter pos");
return (uint32_t)std::distance(f->param_begin(), iter);
}
DeclRefExpr* getArrayDeclFromArrayExpr(ArraySubscriptExpr* arraySubExpr) {
auto base = arraySubExpr->getBase()->IgnoreImpCasts();
if (auto subExpr = dyn_cast<ArraySubscriptExpr>(base)) {
return getArrayDeclFromArrayExpr(subExpr);
}
return cast<DeclRefExpr>(base);
}
/// currentFunc is used to record the function that we are visiting
/// the function has side effect if it changes global var or memory
/// of pointer parameters or call other function with side effect
class FindSideEffectFuncVisitor
: public RecursiveASTVisitor<FindSideEffectFuncVisitor> {
public:
explicit FindSideEffectFuncVisitor(ASTContext* Context, sideEffectMap& wMap,
sideEffectMap& rMap)
: Context(Context), writeEffectFuncs(wMap), readEffectFuncs(rMap) {}
bool TraverseFunctionDecl(FunctionDecl* funcDecl) {
assert(!currentFunc && "currentFunc should be null!");
fakeArrayRef.clear();
currentFunc = funcDecl;
if (currentFunc->getName() == "main") main = currentFunc;
RecursiveASTVisitor<FindSideEffectFuncVisitor>::TraverseFunctionDecl(
funcDecl);
if (sideEffectDump) dumpSideEffectMap();
currentFunc = nullptr;
return true;
}
bool VisitVarDecl(VarDecl* v) { // debug only
if (verbose) {
auto t = v->getType().getTypePtr();
errs() << v->getName() << ": class," << t->getTypeClassName() << ", "
<< v->getType().getAsString()
<< ", isCanonical: " << v->getType().isCanonical()
<< ", isPointer: " << t->isPointerType()
<< ", isArray: " << t->isArrayType()
<< ", declContext: " << v->getDeclContext()->getDeclKindName()
<< "\n";
}
return true;
}
bool VisitInitListExpr(InitListExpr* initExpr) { // debug only
if (initExprDump) {
errs() << "is_semantic: " << initExpr->isSemanticForm() << "\n";
}
return true;
}
bool VisitDeclRefExpr(DeclRefExpr* declRef) {
if (isa<FunctionDecl>(declRef->getDecl()) || !currentFunc) return true;
if (fakeArrayRef.count(declRef)) return true;
if (writeEffectFuncs.count(currentFunc) &&
writeEffectFuncs[currentFunc].count(declRef))
return true;
auto decl = getDeclFromRef(declRef);
if (varIsGloal(decl)) {
readEffectFuncs[currentFunc].insert(declRef);
} else if (isa<ParmVarDecl>(decl) &&
decl->getType()->isPointerType()) { // array parameter
readEffectFuncs[currentFunc].insert(declRef);
}
return true;
}
bool VisitCallExpr(CallExpr* callExpr) {
auto funcDecl = callExpr->getDirectCallee();
assert(funcDecl && "only direct function call in sysY");
uint32_t tmpIndex = 0;
for (auto expr : callExpr->arguments()) { // get all fake array ref
if (funcDecl->getParamDecl(tmpIndex++)->getType()->isPointerType()) {
auto trueExpr = expr->IgnoreImpCasts();
if (isa<ArraySubscriptExpr>(trueExpr)) {
trueExpr =
getArrayDeclFromArrayExpr(cast<ArraySubscriptExpr>(trueExpr));
}
assert(isa<DeclRefExpr>(trueExpr) &&
"only ArraySubscriptExpr and DeclRefExpr can be as pointer "
"argument");
fakeArrayRef.insert(cast<DeclRefExpr>(trueExpr));
}
}
auto n = currentFunc->getName();
auto n2 = funcDecl->getName();
auto n3 = getStmtString(callExpr, Context);
if (funcDecl == currentFunc) return true;
auto sideCheck = [=](sideEffectMap& m) {
if (!m.count(funcDecl)) return;
for (auto sideExpr : m[funcDecl]) {
auto sideDecl = sideExpr->getDecl();
if (isa<ParmVarDecl>(sideDecl)) {
// callArg is a arraySubscriptExpr or DeclRefExpr because
// there is no pointer arithmetics in sysY
auto callArg =
callExpr->getArg(getParamPos(cast<ParmVarDecl>(sideDecl)))
->IgnoreImpCasts();
if (isa<ArraySubscriptExpr>(callArg)) {
callArg =
getArrayDeclFromArrayExpr(cast<ArraySubscriptExpr>(callArg));
}
auto argDecl = getDeclFromRef(cast<DeclRefExpr>(callArg));
if (argDecl->isLocalVarDecl()) {
return;
}
if (isa<ParmVarDecl>(argDecl)) { // also a parameter
assert(argDecl->getType()->isPointerType() &&
"only array type can pass side effect in sysY");
} else {
assert(varIsGloal(argDecl) &&
"only local, param and global var in sysY");
}
assert(currentFunc);
// implicitly remove repeat ref, should be safe?
// e.g, reading callArg multiple times in callee causes callArg
// inserted repeatedly
m[currentFunc].insert(cast<DeclRefExpr>(callArg));
} else {
assert(currentFunc);
// implicitly remove repeat ref, should be safe?
m[currentFunc].insert(sideExpr);
}
}
};
sideCheck(writeEffectFuncs);
sideCheck(readEffectFuncs);
return true;
}
bool VisitBinaryOperator(BinaryOperator* binOp) {
if (binOp->getOpcode() != BO_Assign || !currentFunc) return true;
auto lhs = binOp->getLHS();
if (auto declRef = dyn_cast<DeclRefExpr>(lhs)) {
auto sidevar = getDeclFromRef(declRef);
if (varIsGloal(sidevar)) { // sidevar is global
writeEffectFuncs[currentFunc].insert(declRef);
}
} else if (auto arraySubExpr = dyn_cast<ArraySubscriptExpr>(
lhs)) { // left operand is an array element
auto declRef = getArrayDeclFromArrayExpr(arraySubExpr);
auto arrayVar = getDeclFromRef(declRef);
if (varIsGloal(arrayVar)) {
writeEffectFuncs[currentFunc].insert(declRef);
} else if (auto paramDecl = dyn_cast<ParmVarDecl>(arrayVar)) {
auto iter = std::find(currentFunc->param_begin(),
currentFunc->param_end(), paramDecl);
assert(iter != currentFunc->param_end() && "unknown param decl");
writeEffectFuncs[currentFunc].insert(declRef);
}
} else {
llvm_unreachable("only array element or variable can be lvalue in sysY");
}
return true;
}
void dumpSideEffectMap();
private:
friend class UBCheckConsumer;
FunctionDecl* currentFunc = nullptr;
sideEffectMap& writeEffectFuncs;
sideEffectMap& readEffectFuncs;
FunctionDecl* main;
ASTContext* Context;
DenseSet<DeclRefExpr*> fakeArrayRef;
};
class FindUBVisitor : public RecursiveASTVisitor<FindUBVisitor> {
public:
enum Order : unsigned { ORDER, UNORDER };
using ReachableGraph = DenseMap<VarDecl*, DenseSet<RefPair>>;
using VarRefRecord = DenseMap<VarDecl*, std::vector<UniqueRef>>;
explicit FindUBVisitor(ASTContext* Context, sideEffectMap& wMap,
sideEffectMap& rMap)
: Context(Context), writeEffectFuncs(wMap), readEffectFuncs(rMap) {}
void UBcheck(FunctionDecl* entryFunc) { TraverseFunctionDecl(entryFunc); }
bool TraverseCallExpr(CallExpr* callExp) {
TraverseCallee(callExp);
auto funcDecl = callExp->getDirectCallee();
auto callRef = cast<DeclRefExpr>(
callExp->getCallee()->IgnoreImpCasts()); // safe in sysY
declRefCall[callRef] = callExp;
auto numArgs = callExp->getNumArgs();
auto tmpRefs = new VarRefRecord[numArgs];
VarRefRecord tmpOrgRef = std::move(refs);
uint32_t tmpIndex = 0;
for (auto arg : callExp->arguments()) {
if (funcDecl->getParamDecl(tmpIndex)->getType()->isPointerType()) {
auto trueExpr = arg->IgnoreImpCasts();
if (isa<ArraySubscriptExpr>(trueExpr)) {
trueExpr =
getArrayDeclFromArrayExpr(cast<ArraySubscriptExpr>(trueExpr));
}
assert(isa<DeclRefExpr>(trueExpr) &&
"only ArraySubscriptExpr and DeclRefExpr can be as pointer "
"argument");
fakeArrayRef.insert(cast<DeclRefExpr>(trueExpr));
}
TraverseStmt(arg);
tmpRefs[tmpIndex] = std::move(refs);
++tmpIndex;
}
VisitDeclRefExpr(callRef);
for (auto& p : refs) {
for (tmpIndex = 0; tmpIndex < numArgs; ++tmpIndex) {
auto& map = tmpRefs[tmpIndex];
if (map.count(p.getFirst())) {
for (auto refExpr1 : map[p.getFirst()]) {
for (auto refExpr2 : p.getSecond()) {
reachGraph[p.getFirst()].insert({refExpr2, refExpr1});
}
}
}
}
}
for (tmpIndex = 0; tmpIndex < numArgs; ++tmpIndex) { // merge refs
for (auto& p : tmpRefs[tmpIndex]) {
mergeVector(refs[p.getFirst()], p.getSecond());
}
}
for (auto& p : tmpOrgRef) {
mergeVector(refs[p.getFirst()], p.getSecond());
}
delete[] tmpRefs;
return true;
}
bool TraverseFunctionDecl(FunctionDecl* funcDecl) {
// simplified version of RecursiveVisitor::TraverseFunctionDecl
fakeArrayRef.clear();
for (auto parm : funcDecl->parameters()) {
TraverseDecl(parm);
}
assert(funcDecl->getBody() && "no declaration in sysY");
InternalVisitStmt(funcDecl->getBody());
return true;
}
bool TraverseBinaryOperator(BinaryOperator* binOp) {
if (binOp->getOpcode() == BO_Assign) {
if (isa<DeclRefExpr>(binOp->getLHS())) {
fakeArrayRef.insert(cast<DeclRefExpr>(binOp->getLHS()));
} else {
fakeArrayRef.insert(getArrayDeclFromArrayExpr(
cast<ArraySubscriptExpr>(binOp->getLHS())));
}
}
if (binOp->getOpcode() == BO_LAnd || binOp->getOpcode() == BO_LOr) {
VarRefRecord tmpOrgRef = std::move(refs);
TraverseStmt(binOp->getLHS());
VarRefRecord tmprefs = std::move(refs);
TraverseStmt(binOp->getRHS());
for (const auto& p1 : refs) {
if (tmprefs.count(p1.getFirst())) {
for (auto pair1 : p1.getSecond()) {
for (auto pair2 : tmprefs[p1.getFirst()]) {
reachGraph[p1.getFirst()].insert({pair1, pair2});
}
}
}
}
for (const auto& p : tmprefs) { // merge refs
mergeVector(refs[p.getFirst()], p.getSecond());
}
for (const auto& p : tmpOrgRef) {
mergeVector(refs[p.getFirst()], p.getSecond());
}
} else {
TraverseStmt(binOp->getLHS());
TraverseStmt(binOp->getRHS());
}
return true;
}
bool VisitDeclRefExpr(DeclRefExpr* refExp) {
if (fakeArrayRef.count(refExp)) return true;
auto decl = refExp->getDecl();
if (isa<FunctionDecl>(decl)) {
auto callExpr = declRefCall[refExp];
decltype(refs) tmpref;
assert(callExpr &&
"should map any DeclRef of func type into its callExpr");
auto helpFunc = [decl, callExpr, this, &tmpref](sideEffectMap& m,
RWType t) {
for (auto varRef : m[cast<FunctionDecl>(decl)]) {
auto varDecl = cast<VarDecl>(varRef->getDecl());
if (isa<ParmVarDecl>(varDecl)) {
auto arg = callExpr->getArg(getParamPos(cast<ParmVarDecl>(varDecl)))
->IgnoreImpCasts();
auto arrayRef =
isa<DeclRefExpr>(arg)
? cast<DeclRefExpr>(arg)
: getArrayDeclFromArrayExpr(cast<ArraySubscriptExpr>(arg));
// arrayRef should be a fake arrayRef
auto argDecl = cast<VarDecl>(arrayRef->getDecl());
if (isa<ParmVarDecl>(argDecl)) {
tmpref[getArgFromParm(cast<ParmVarDecl>(argDecl), callStack)]
.push_back({arrayRef, (refCnt += 2) | t});
} else {
assert(varIsGloal(argDecl) || argDecl->isLocalVarDecl());
tmpref[argDecl].push_back({arrayRef, (refCnt += 2) | t});
}
assert(fakeArrayRef.count(arrayRef) &&
"array parameter should be a fake ref");
} else {
assert(varIsGloal(varDecl) &&
"only global and parameter have side effect");
tmpref[varDecl].push_back({varRef, (refCnt += 2) | t});
}
}
};
helpFunc(writeEffectFuncs, WRITE);
helpFunc(readEffectFuncs, READ);
for (auto& p : tmpref) {
auto& vec = p.getSecond();
for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
for (auto iter2 = iter + 1; iter2 != vec.end(); ++iter2) {
reachGraph[p.getFirst()].insert({*iter, *iter2});
}
}
mergeVector(refs[p.getFirst()], p.getSecond());
}
} else {
// one statement has at most one write which is thought as fake ref
auto varDecl = cast<VarDecl>(refExp->getDecl());
if (isa<ParmVarDecl>(varDecl) && varDecl->getType()->isPointerType()) {
auto argDecl = getArgFromParm(cast<ParmVarDecl>(varDecl), callStack);
refs[argDecl].push_back({refExp, (refCnt += 2) | READ});
} else {
refs[varDecl].push_back({refExp, (refCnt += 2) | READ});
}
}
return true;
}
private:
enum RWType : unsigned { READ, WRITE, NONE };
enum Position : unsigned { START, END };
enum Direction : unsigned { LINE, COLUMN };
void InternalVisitStmt(Stmt* s) { // call this when s is a full expression
// for, while and if stmt will have optional cond variable(declare variable
// in condition expression), this will be nullptr in sysY but what is Init
// Statement in IfStmt ?
if (!s) return;
resetStmtTraversal();
switch (s->getStmtClass()) {
case Stmt::DeclStmtClass:
case Stmt::ForStmtClass:
case Stmt::WhileStmtClass:
case Stmt::CompoundStmtClass:
case Stmt::IfStmtClass:
case Stmt::BreakStmtClass: // break and continue stmt don't have children
case Stmt::ContinueStmtClass:
case Stmt::ReturnStmtClass: // optional ret expr
case Stmt::NullStmtClass: // ; is a null stmt
InternalVisitComplexStmt(s);
return;
default:
assert(isa<Expr>(s) && "unknown stmt class");
TraverseStmt(s);
}
if (stmtRefDump) {
errs() << "[Stmt Start, line " << getLineOrColumn(s, START, LINE, Context)
<< "] " << getStmtString(s, Context) << "\n";
graphDump();
errs() << "[Stmt End, line " << getLineOrColumn(s, END, LINE, Context)
<< "]\n\n";
}
DenseSet<VarDecl*> tmp;
findUB(getLineOrColumn(s, START, LINE, Context), s);
}
void TraverseCallee(CallExpr* call) {
auto subFunc = call->getDirectCallee();
// avoid recursive call
if (!callStack.empty() && callStack.back()->getDirectCallee() == subFunc)
return;
assert(subFunc && "only direct function call in sysY");
auto tmpFakeArrayRef = std::move(fakeArrayRef);
auto tmpReachGraph = std::move(reachGraph);
auto tmpRefs = std::move(refs);
auto tmpDeclRefCall = std::move(declRefCall);
callStack.push_back(call);
TraverseFunctionDecl(subFunc);
callStack.pop_back();
fakeArrayRef = std::move(tmpFakeArrayRef);
reachGraph = std::move(tmpReachGraph);
refs = std::move(tmpRefs);
declRefCall = std::move(tmpDeclRefCall);
}
/// @brief core function to find UB by flatten trees, and split forest
/// recursively
/// @param stmtLine, line number of current statement
void findUB(uint32_t stmtLine, Stmt* st) {
for (auto& p : refs) {
auto var = p.getFirst();
for (auto iter = p.getSecond().begin(); iter != p.getSecond().end();
++iter) {
if ((iter->second & WRITE) != WRITE) continue;
for (auto iter2 = p.getSecond().begin(); iter2 != p.getSecond().end();
++iter2) {
if (*iter != *iter2 &&
!reachGraph[p.getFirst()].count({*iter, *iter2}))
goto FAIL;
}
}
continue;
FAIL:
errs() << "[WARNING Possible Undefined Behavior in Statement line "
<< stmtLine << "] " << getStmtString(st, Context) << "\n";
dumpCallStack();
errs() << "VAR: " << var->getName() << " declaration at "
<< getLineOrColumn(var, START, LINE, Context) << ":"
<< getLineOrColumn(var, START, COLUMN, Context) << "\n";
for (auto ref : refs[p.getFirst()]) {
errs() << "at " << getLineOrColumn(ref.first, START, LINE, Context)
<< ":" << getLineOrColumn(ref.first, START, COLUMN, Context)
<< ", "
<< ((ref.second & WRITE) == WRITE ? "write\n" : "read\n");
}
errs() << "[END WARNING]\n";
}
}
static VarDecl* getArgFromParm(ParmVarDecl* parmDecl,
ArrayRef<CallExpr*> callStack) {
auto argExpr =
callStack.back()->getArg(getParamPos(parmDecl))->IgnoreImpCasts();
if (isa<ArraySubscriptExpr>(argExpr)) {
argExpr = getArrayDeclFromArrayExpr(cast<ArraySubscriptExpr>(argExpr));
}
auto argDecl = cast<VarDecl>(cast<DeclRefExpr>(argExpr)->getDecl());
assert((argDecl->getType()->isPointerType() ||
argDecl->getType()->isArrayType()) &&
"only array type need to find orignial arg");
if (isa<ParmVarDecl>(argDecl)) {
assert(callStack.size() > 1 && "top function shouldn't have parameter");
return getArgFromParm(cast<ParmVarDecl>(argDecl), callStack.drop_back());
} else {
assert(argDecl->isLocalVarDecl() || varIsGloal(argDecl));
return argDecl;
}
}
template <class T>
static uint32_t getLineOrColumn(T* s, Position p, Direction d,
ASTContext* context) {
auto pos = p == START ? s->getBeginLoc() : s->getEndLoc();
return d == LINE ? context->getFullLoc(pos).getLineNumber() - 1
: context->getFullLoc(pos).getColumnNumber();
}
void InternalVisitComplexStmt(Stmt* s) {
for (auto subSt : s->children()) {
// subSt will be init expr of decl varibale for DeclStmt
InternalVisitStmt(subSt);
}
}
void resetStmtTraversal() {
reachGraph.clear();
refs.clear();
declRefCall.clear();
}
void graphDump() {
for (auto& p : refs) {
auto map = dumpRefs(p.getFirst());
errs() << "[REACHABLITIY]\n";
bool hasOut = false;
for (uint32_t i = 0, leng = map.size(); i < leng; ++i) {
for (uint32_t j = i + 1; j < leng; ++j) {
if (reachGraph.count(p.getFirst()) &&
reachGraph[p.getFirst()].count({map[i], map[j]})) {
hasOut = true;
errs() << "[" << i << ", " << j << ", order]";
}
}
}
if (hasOut)
errs() << "\n[END]\n";
else
errs() << "[END]\n";
}
}
void dumpCallStack() {
errs() << "[Call Stack]: ";
if (callStack.empty()) {
errs() << "Empty\n";
return;
}
for (size_t len = callStack.size(), i = 0; i < len - 1; ++i) {
errs() << getStmtString(callStack[i], Context) << " --> ";
}
errs() << getStmtString(callStack[callStack.size() - 1], Context) << "\n";
}
std::vector<UniqueRef> dumpRefs(VarDecl* v) {
errs() << "[VAR]: " << v->getName() << "\n";
uint32_t tmpIndex = 0;
std::vector<UniqueRef> map;
if (!refs.count(v)) return map;
for (auto expr : refs[v]) {
errs() << "[" << map.size() << "]";
if ((expr.second & WRITE) == READ)
errs() << "read at ";
else if ((expr.second & WRITE) == WRITE)
errs() << "write at ";
else
llvm_unreachable("SHOULD ONLY READ AND WRITE");
errs() << getLineOrColumn(expr.first, START, LINE, Context) << ":"
<< getLineOrColumn(expr.first, START, COLUMN, Context) << "\n";
map.push_back(expr);
}
return map;
}
template <class T>
void mergeVector(std::vector<T>& v1, const std::vector<T>& v2) {
auto refsSize = v1.size();
v1.resize(refsSize + v2.size());
std::memcpy(v1.data() + refsSize, v2.data(), v2.size() * sizeof(T));
}
friend class UBCheckConsumer;
friend class FindSideEffectFuncVisitor;
sideEffectMap& writeEffectFuncs;
sideEffectMap& readEffectFuncs;
ASTContext* Context;
std::vector<CallExpr*> callStack;
uint64_t refCnt = 0;
// used for single stmt traversal
DenseMap<DeclRefExpr*, CallExpr*> declRefCall;
// if refpair in reachGraph, the ref pair is ordered
ReachableGraph reachGraph;
VarRefRecord refs;
DenseSet<DeclRefExpr*> fakeArrayRef;
};
void FindSideEffectFuncVisitor::dumpSideEffectMap() {
errs() << "[START] Function: " << currentFunc->getName() << "\n";
auto helpFunc = [this](const char* s, sideEffectMap& m) {
if (m.count(currentFunc)) {
for (auto p : m[currentFunc]) {
errs() << s << p->getDecl()->getName() << ", at "
<< FindUBVisitor::getLineOrColumn(p, FindUBVisitor::START,
FindUBVisitor::LINE, Context)
<< ":"
<< FindUBVisitor::getLineOrColumn(p, FindUBVisitor::START,
FindUBVisitor::COLUMN, Context)
<< "\n";
}
}
};
helpFunc("write ", writeEffectFuncs);
helpFunc("read ", readEffectFuncs);
errs() << "[END]\n";
}
class UBCheckConsumer : public clang::ASTConsumer {
public:
explicit UBCheckConsumer(ASTContext* Context)
: writeEffectMap(),
readEffectMap(),
allFuncs(),
Visitor(Context, writeEffectMap, readEffectMap),
UBVisitor(Context, writeEffectMap, readEffectMap) {}
virtual void HandleTranslationUnit(clang::ASTContext& Context) {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
if (verbose) {
errs() << "all functions\n";
for (auto func : allFuncs) {
errs() << func->getName() << "\n";
}
errs() << "\n";
for (const auto& p : writeEffectMap) {
auto f = p.getFirst();
errs() << "/* Side Effect of func " << f->getName()
<< " start */\n";
for (auto& effects : p.getSecond()) {
errs() << "w, ";
errs() << effects;
}
if (readEffectMap.count(p.getFirst())) {
for (auto& effects : readEffectMap[p.getFirst()]) {
errs() << "r, ";
errs() << effects;
}
}
errs() << "/* END */\n";
}
for (const auto& p : readEffectMap) {
auto f = p.getFirst();
if (!writeEffectMap.count(f)) {
errs() << "/* Side Effect of func " << f->getName()
<< " start */\n";
for (auto& effects : p.getSecond()) {
errs() << "r, ";
errs() << effects;
}
errs() << "/* END */\n";
}
}
}
UBVisitor.UBcheck(Visitor.main);
}
private:
FindSideEffectFuncVisitor Visitor;
FindUBVisitor UBVisitor;
std::vector<FunctionDecl*> allFuncs;
VarDecl* globalInput;
sideEffectMap writeEffectMap;
sideEffectMap readEffectMap;
};
class FindSideEffectFuncAction : public ASTFrontendAction {
public:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& instance,
StringRef inputF) override {
return std::make_unique<UBCheckConsumer>(&instance.getASTContext());
}
};
int main(int argc, const char** argv) {
cl::ParseCommandLineOptions(
argc, argv, "a tool used to check sysY UB about order of evaluation");
std::fstream file(inputFile, file.in);
std::ostringstream code;
if (!file) {
errs() << "open input file failed\n";
return 1;
}
code << declstr;
code << file.rdbuf();
if (verbose) errs() << code.str();
std::vector<std::string> args = extraArgs;
args.push_back("-Wno-unused-value");
runToolOnCodeWithArgs(std::make_unique<FindSideEffectFuncAction>(),
code.str(), args, inputFile);
return 0;
}