-
Notifications
You must be signed in to change notification settings - Fork 1
/
tainter.h
executable file
·1269 lines (1096 loc) · 40.8 KB
/
tainter.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
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef TAINTER_H
#define TAINTER_H
// #include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedUser.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm-c/IRReader.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/ADT/BreadthFirstIterator.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/IR/Dominators.h"
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <string>
#include <sstream>
#include <ctime>
// #include <stdlib.h>
#include <cxxabi.h>
#include <typeinfo> //for 'typeid' to work
#include <unistd.h>
//#include <bits/stdc++.h>
#include <set>
#include <regex>
#include <algorithm>
using namespace llvm;
using namespace std;
#define ENERGY_FOR_FIELD_SENSITIVE 4
#define _ERROR_LEVEL 0
#define _WARNING_LEVEL 1
#define _DEBUG_LEVEL 2
#define _REDUNDENCY_LEVEL 3
extern unsigned debug_level;
extern Module* M;
#define MY_DEBUG( level, x) if( debug_level >= level ) x
#define NO_OFFSET 1024*1024 // represent Single Variable without offset similar to struct-like
#define ERR_OORANGE 1024*1024 // represent out of range error in iterate vector
/*
Vector2 operator+(Vector2 a, Vector2 const& b) {
// note 'a' is passed by value and thus copied
a += b;
return a;
}
*/
bool prune = false;
set<string> visited_CF_BB_by_confName;
set<string> visited_CF_func_by_confName;
set<string> visited_entryPoint_by_confName;
std::string gen_random(const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::string tmp_s;
tmp_s.reserve(len);
for (int i = 0; i < len; ++i) {
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return tmp_s;
}
bool readConfigVariableNames( std::string, std::vector< struct ConfigVariableNameInfo* >&);
vector<string> splitWithTag( string, string);
bool findConfigVariable(std::vector<struct ConfigVariableNameInfo* >, GlobalVariable*, std::vector<struct GlobalVariableInfo*>&);
bool handleDIType(DIType*, std::vector<struct ConfigVariableNameInfo* >, GlobalVariable*, std::vector<struct GlobalVariableInfo*>&);
void handleUser(Value*, struct GlobalVariableInfo *, struct InstInfo* , unsigned, unsigned);
void handleInstruction(Value*, struct GlobalVariableInfo *, struct InstInfo* , unsigned, unsigned);
bool findVisitedInstruction(struct InstInfo* , struct InstInfo*);
struct SrcLoc getSrcLoc(Instruction*);
bool comesBefore(Instruction* , Instruction* );
void collectBFSReachableBB(vector<BasicBlock*>&, BasicBlock*);
void collectBFSReachableBB(vector<BasicBlock *>&, BasicBlock*, BasicBlock*);
void collectDFSReachableBB( vector<BasicBlock*>&, BasicBlock*);
vector<User*> getSequenceUsers(Value*);
void printSequenceUsers(Value*);
vector<Use*> getSequenceUses_OnlyGEPins(Value*);
unsigned findLastOf(Instruction*, vector<struct LSPair*>);
string getOriginalName(string);
string getStructTypeStrFromPrintAPI(Type*);
vector<unsigned> getIndexFromGEPO(GEPOperator*);
bool isMatchedGEPOperator(GEPOperator*, struct GlobalVariableInfo*, int);
void printTabs(unsigned);
unsigned getFuncArgIndex(CallBase*, Value*);
void traceFunction(struct FuncInfo*, uint, uint);
unsigned isFuncInfoRecorded(struct FuncInfo*, vector<struct FuncInfo*>);
void traceUser( Value*, struct FuncInfo*, struct InstInfo*, uint, uint);
string getAsString(Value*);
string getClassType(Value*);
//string leftTrim(string &s) {
// return regex_replace(s, regex("^\s+"), string(""));
//}
struct LSPair
{
StoreInst* _StoreInst;
LoadInst* _LoadInst;
Value* StoreAddr;
LSPair() {}
LSPair(StoreInst* _store_inst){
this->_StoreInst = _store_inst;
this->_LoadInst = nullptr;
this->StoreAddr = _store_inst->getPointerOperand();
}
};
struct SrcLoc
{
std::string Filename;
std::string Dirname;
unsigned Line;
unsigned Col;
unsigned Count; // used to record the visit times of this SrcLoc, mainly consider dirname/filename:line, no col.
SrcLoc() {}
SrcLoc(std::string _filename, std::string _dirname, unsigned _line, unsigned _col)
{
this->Filename = _filename;
this->Dirname = _dirname;
this->Line = _line;
this->Col = _col;
this->Count = 1;
}
void operator = (SrcLoc _loc)
{
this->Filename = _loc.Filename;
this->Dirname = _loc.Dirname;
this->Line = _loc.Line;
this->Col = _loc.Col;
};
bool operator == (SrcLoc _loc)
{
if(this->Filename == _loc.Filename &&
this->Dirname == _loc.Dirname &&
this->Line == _loc.Line &&
this->Col == _loc.Col)
{
return true;
}
return false;
};
// is a valid location in source file?
bool isValid()
{
if( this->Filename.length() == 0 ||
this->Dirname.length() == 0 ||
this->Line == 0 || this->Col == 0)
{
return false;
}
return true;
}
void print(unsigned tabs)
{
for(unsigned i=0; i<tabs; i++)
llvm::outs()<<" ";
// llvm::outs()<<"\t";
if(Dirname.length() == 0)
llvm::outs()<< this->Filename << ":" << this->Line << "\n";
else
llvm::outs()<< this->Dirname <<"/"<< this->Filename << ":" << this->Line << "\n";
// llvm::outs()<<this->Dirname<<"/"<<this->Filename<<":"<<this->Line<<":"<<this->Col<<"\n";
}
string toRealContent()
{
if(!this->isValid())
return " [错误] 文件路径有误";
string sLine = "";
ifstream read;
unsigned line_no = 0;
try {
read.open(this->Dirname+"/"+this->Filename);
} catch(std::ios_base::failure& e) {
llvm::outs() << e.what() << "\n";
return " [错误] 文件打开失败";
}
while (line_no != this->Line && getline(read, sLine)) {
++line_no;
}
if (line_no == this->Line) {
// sLine contains the [this->Line]-th line in the file.
return sLine;//leftTrim(sLine);
} else {
// The file contains fewer than two lines.
return " [错误] 文件行数小于指定行数";
}
}
string toString()
{
if(Dirname.length() == 0)
return this->Filename+":"+std::to_string(this->Line)+":"+std::to_string(this->Col);
else
return this->Dirname+"/"+this->Filename+":"+std::to_string(this->Line)+":"+std::to_string(this->Col);
}
string toStringNoDir()
{
return this->Filename+":"+std::to_string(this->Line)+":"+std::to_string(this->Col);
}
string toStringFilename()
{
return this->Filename;
}
string toStringLine(){
return std::to_string(this->Line);
}
unsigned toStringColNum(){
return this->Col;
}
bool dirHasString(string keyword)
{
std::size_t found = this->Dirname.find(keyword);
if(found != std::string::npos)
return true;
else
return false;
}
bool filenameHasString(string keyword)
{
std::size_t found = this->Filename.find(keyword);
if(found != std::string::npos)
return true;
else
return false;
}
};
enum VariableType
{
SINGLE,
STRUCT,
CLASS,
FIELD
};
struct ConfigVariableNameInfo
{
VariableType VarType;
//for SINGLE Type
string SingleName;
//for STRUCT Type
vector<string> StructName;
//for CLASS Type
vector<string> ClassName;
//for FIELD Type
vector<string> FieldName;
string OriginalConfigName;
bool isDynamicConfigurable;
string DynamicConfigurableStatus;
// bool found_flag;
ConfigVariableNameInfo()
{
this->VarType = SINGLE;
this->SingleName = "";
this->StructName.clear();
this->ClassName.clear();
this->FieldName.clear();
this->OriginalConfigName = "";
this->DynamicConfigurableStatus = "";
// this->found_flag = false;
}
string getNameAsStringPrefix()
{
string name;
if(this->VarType == SINGLE)
name = this->SingleName;
else if( this->VarType == STRUCT)
name = this->StructName[0];
else if( this->VarType == CLASS)
name = this->ClassName[0];
else if( this->VarType == FIELD)
name = this->FieldName[0];
else
name = "UNSUPPORTED type";
return name;
}
string getNameAsString()
{
if(this->VarType == SINGLE)
return this->SingleName;
else if( this->VarType == STRUCT)
{
string name = this->StructName[0];
for(unsigned i=1; i<this->StructName.size(); i++)
{
name = name+"."+this->StructName[i];
}
return name;
}
else if( this->VarType == CLASS)
{
string name = this->ClassName[0];
for(unsigned i=1; i<this->ClassName.size(); i++)
{
name = name+"."+this->ClassName[i];
}
return name;
}
else if( this->VarType == FIELD)
{
string name = this->FieldName[0];
for(unsigned i=1; i<this->FieldName.size(); i++)
{
name = name+"."+this->FieldName[i];
}
return name;
} else {
return "UNSUPPORTED type";
}
}
void operator = (ConfigVariableNameInfo _name_info)
{
this->VarType = _name_info.VarType;
this->SingleName = _name_info.SingleName;
this->StructName = _name_info.StructName;
this->ClassName = _name_info.ClassName;
this->FieldName = _name_info.FieldName;
// this->found_flag = _name_info.found_flag;
};
bool isValid()
{
if( this->SingleName.length() == 0 ||
this->StructName.size() == 0 ||
this->ClassName.size() == 0 ||
this->FieldName.size() == 0 )
{
return false;
} else
return true;
}
void print(unsigned tabs)
{
for(unsigned i=0; i<tabs; i++)
llvm::outs()<<" ";
// llvm::outs()<<"\t";
llvm::outs()<<this->getNameAsString()<<"\n";
}
};
enum InfluenceLevelType
{
STRONG,
WEAK
};
struct InstInfo
{
Instruction* InstPtr;
struct SrcLoc InstLoc;
InfluenceLevelType InfluenceLevel;
bool add_tab = true;
vector<struct InstInfo *> Successors;
struct InstInfo* Predecessor;
/// for Control Flow information.
bool isControllingInst;
vector<BasicBlock*> ControllingBBs;
/// do we need this???
vector<BasicBlock*> weakControllingBBs;
/// ByHHC
vector<Function*> ControllingFuncs;
InstInfo() {}
InstInfo(Instruction* _inst, struct SrcLoc _loc)
{
this->InstPtr = _inst;
this->InstLoc = _loc;
this->InfluenceLevel = STRONG;
this->isControllingInst = false;
}
InstInfo(Instruction* _inst, struct SrcLoc _loc, bool _add_tab)
{
this->InstPtr = _inst;
this->InstLoc = _loc;
this->InfluenceLevel = STRONG;
this->isControllingInst = false;
this->add_tab = _add_tab;
}
void setControllingBBs(vector<BasicBlock*> _bbs)
{
for(unsigned i=0; i<_bbs.size(); i++)
{
this->ControllingBBs.push_back(_bbs[i]);
}
}
/*
void setControllingFuncs(vector<Function*> _funcs)
{
for(unsigned i=0; i<_funcs.size(); i++)
{
if(!_funcs[i]->isIntrinsic())
this->ControllingFuncs.push_back(_funcs[i]);
}
}
*/
void addControllingFuncs(Function* _func)
{
this->ControllingFuncs.push_back(_func);
}
bool operator ==(InstInfo* _inst_info)
{
if( this->InstPtr == _inst_info->InstPtr)
return true;
return false;
}
bool operator <(InstInfo* _inst_info)
{
if( this->InstPtr == _inst_info->InstPtr)
return true;
return false;
}
};
enum InfluenceStatus
{
UNDEFINE,
YES,
NO
};
struct FuncInfo
{
Function* Ptr;
string FuncName;
unsigned ArgIndex; // which arg of this function is the User.
InfluenceStatus hasInsideDataFlowInfluence; // Whether the ArgIndex's argument has dataflow to return inside the Function.
std::vector<string> CallLocList;
vector<struct InstInfo*> ArgInstInfoList; // the instruction list about the dataflow of target Argument.
vector<struct FuncInfo*> InsideFuncInfoList; // if there are other calls inside this function, we record it
FuncInfo() {}
FuncInfo(Function* _ptr, string _name, unsigned _index, string _callloc)
{
this->Ptr = _ptr;
this->FuncName = _name;
this->ArgIndex = _index;
this->hasInsideDataFlowInfluence = UNDEFINE;
this->CallLocList.clear();
this->CallLocList.push_back(_callloc);
}
void print(unsigned tabs)
{
for(unsigned i=0; i<tabs; i++)
llvm::outs()<<" ";
// llvm::outs()<<"\t";
llvm::outs()<<this->FuncName<<"\n";
}
void printDetail(unsigned tabs)
{
for(unsigned i=0; i<tabs; i++)
llvm::outs()<<" ";
// llvm::outs()<<"\t";
llvm::outs()<<this->FuncName<<"\n";
for(unsigned j=0; j<this->CallLocList.size(); j++)
{
for(unsigned i=0; i<tabs+1; i++)
llvm::outs()<<" ";
// llvm::outs()<<"\t";
llvm::outs()<<this->CallLocList[j]<<"\n";
}
}
};
struct GlobalVariableInfo
{
struct ConfigVariableNameInfo* NameInfo;
GlobalVariable* Ptr;
string GlobalVariableType; // name info of
vector<unsigned> Offsets; // indicate the offset of struct field in GEPOperator
// to implement ssl's requirement, output the tainted code in an separat area.
std::vector<string> taintedCodeLines;
std::vector<struct InstInfo *> taintedCodeLinesIR;
// every first-user (which is usually an instruction) of this gv(configuration variable)
std::vector<struct InstInfo *> InstInfoList;
// all tainted called function starting from each occurance (according to each one in
// `InstInfoList`) of gv.
std::vector<struct FuncInfo *> FuncInfoList;
/// TODO: add a mapping between `InstInfoList` and `FuncInfoList` to be more human-friendly.
// ALL GEPinstruction to make the anaylze field-sensitive. Trade accuracy for completeness.
// That is to say, some taint by GEPinstruction anaylze may be false postive.
vector< pair< pair<Type *, vector<int>> , InstInfo* >> GEPInfoList;
std::vector<GlobalVariable*> InfluencedGVList;
// for every element in `InstInfoList`, it has a set of tainted (or influenced) function.
// These functions may need no further traing. They are regarded as the final point for
// the forward tainting.
std::map<string, vector<Function *>> InfluencedFuncList;
// this is for the recording process of `InfluencedFuncList`
Function* currentGVStartingFunc = nullptr;
string currentGVStartingFuncName;
// for every element in `InstInfoList`, it has a parent function (->getParent()->getParent()),
// stored here.
std::vector<Function*> CallerFunctionList;
GlobalVariableInfo() {}
GlobalVariableInfo(struct ConfigVariableNameInfo* _name_info, GlobalVariable* _ptr, vector<unsigned> _offsets)
{
this->NameInfo = _name_info;
this->Ptr = _ptr;
if(_ptr)
this->GlobalVariableType = getStructTypeStrFromPrintAPI(_ptr->getType());
for(auto it=_offsets.begin(); it!=_offsets.end(); it++)
{
this->Offsets.push_back(*it);
}
this->InstInfoList.clear();
this->FuncInfoList.clear();
}
/*
bool matchElementInGEPInfoList( pair<Type *, vector<int>> * cur_GEPInfo){
//bool match = true;
for(vector< pair< pair<Type *, vector<int>> , Value* >> i = this->GEPInfoList.begin(); i != this->GEPInfoList.end(); i++){
if(i->first != cur_GEPInfo->first)
return false;
if(i->second != cur_GEPInfo->second)
return false;
}
return true;
};
*/
void collectCallerFunction()
{
for(unsigned i=0; i<this->InstInfoList.size(); i++)
{
struct InstInfo* inst_info = InstInfoList[i];
Function* func = inst_info->InstPtr->getFunction();
if( ! func)
continue;
if( std::find(this->CallerFunctionList.begin(), this->CallerFunctionList.end(), func) == this->CallerFunctionList.end())
{
this->CallerFunctionList.push_back(func);
}
}
}
vector< std::pair< Function*, Function*> > collectOutsideCallerFunctions()
{
vector<std::pair<Function*, Function*>> outside_caller_func_list;
for(auto I=M->begin(); I!=M->end(); I++)
{
Function* func = &*I;
if(! func)
continue;
for(auto i=inst_begin(func); i!=inst_end(func); i++)
{
Instruction* inst = &*i;
if(CallBase* call = dyn_cast<CallBase>(inst))
{
Function* cur_func = call->getCalledFunction();
for(auto it=this->CallerFunctionList.begin(); it!=this->CallerFunctionList.end(); it++)
{
Function* inside_func = *it;
if( inside_func == cur_func)
{
outside_caller_func_list.push_back(std::pair< Function*, Function*>(inside_func, func));
}
}
}
}
}
return outside_caller_func_list;
}
void printNameInfo(unsigned tabs)
{
for(unsigned i=0; i<tabs; i++)
llvm::outs()<<" ";
// llvm::outs()<<"\t";
llvm::outs()<<"GlobalVariable Name: "<<this->NameInfo->getNameAsString();
if( ! this->Offsets.empty() )
llvm::outs()<<"\tOffset: ";
for(unsigned i=0; i<this->Offsets.size(); i++)
{
if( this->Offsets[i] != NO_OFFSET)
llvm::outs()<<this->Offsets[i]<<" ";
}
llvm::outs()<<"\n";
}
void dumpInstInfo(string output_file, struct InstInfo* inst_info, unsigned level)
{
//if(find(taintedCodeLinesIR.begin(), taintedCodeLinesIR.end(), inst_info) == taintedCodeLinesIR.end())
// taintedCodeLinesIR.push_back(inst_info);
ofstream fout(output_file.c_str(), ios::app);
if(! fout)
{
llvm::outs()<<"Open output file "<<output_file<<" FAILED!\n";
return;
}
// write pointer OR control-flow arrow.
if(!inst_info->add_tab){
for(unsigned i=0; i<level; i++)
fout<<"\t";
fout << "↓\t↓\t↓\t↓\t↓ control-flow OR field-sensitive-dependency (no `ComesBefore` relation cross functions.)\n";
}
// write instruction to file
for(unsigned i=0; i<level; i++)
fout<<"\t";
fout<<getAsString(inst_info->InstPtr);
if(CallBase* call = dyn_cast<CallBase>(inst_info->InstPtr))
fout<<" <===== Function Call.\n";
else
fout<<"\n";
// write source location to file
for(unsigned i=0; i<level; i++)
fout<<"\t";
fout<<inst_info->InstLoc.toString()<< " [" << getOriginalName(inst_info->InstPtr->getFunction()->getName().str()) <<"]\n";
fout.close();
// write other instructions
for(auto i=inst_info->Successors.begin(), e=inst_info->Successors.end(); i!=e; i++)
{
struct InstInfo* next = *i;
if(next->add_tab){
this->dumpInstInfo( output_file, next, level+1);
} else {
this->dumpInstInfo( output_file, next, level);
}
}
}
void dumpFuncInfo(string output_file, struct FuncInfo* func_info, unsigned level)
{
ofstream fout(output_file.c_str(), ios::app);
if(! fout)
{
llvm::outs()<<"Open output file "<<output_file<<" FAILED!\n";
return;
}
for(unsigned i=0; i<level; i++)
fout<<"\t";
fout<<func_info->FuncName<<" "<<func_info->ArgIndex<<"\n";
for(unsigned j=0; j<func_info->CallLocList.size(); j++)
{
for(unsigned i=0; i<level+1; i++)
fout<<"\t";
fout<<func_info->CallLocList[j]<<"\n";
}
fout.close();
for(auto i=func_info->ArgInstInfoList.begin(); i!=func_info->ArgInstInfoList.end(); i++)
{
struct InstInfo* inst_info = *i;
this->dumpInstInfo( output_file, inst_info, level+1);
}
for(auto i=func_info->InsideFuncInfoList.begin(); i!=func_info->InsideFuncInfoList.end(); i++)
{
struct FuncInfo* inner_func_info = *i;
this->dumpFuncInfo( output_file, inner_func_info, level+1);
}
}
bool writeToFile(string output_file)
{
ofstream fout(output_file.c_str(), ios::app);
if(! fout)
{
llvm::outs()<<"Open output file "<<output_file<<" FAILED!\n";
return false;
}
fout<<"\n\n\n";
fout<<"GlobalVariable Name: "<<this->NameInfo->getNameAsString();
// if( this->Offset != NO_OFFSET)
// fout<<"\tOffset: "<<this->Offset<<"\n";
// else
// fout<<"\n";
for(unsigned i=0; i<this->Offsets.size(); i++)
{
fout<<"\tOffset: ";
if( this->Offsets[i] != NO_OFFSET)
fout<<this->Offsets[i]<<" ";
}
fout<<"\n";
fout<<"\n\tCaller Functions: \n";
if( this->CallerFunctionList.empty() )
this->collectCallerFunction();
for(auto it=this->CallerFunctionList.begin(); it!=this->CallerFunctionList.end(); it++)
{
Function* func = *it;
fout<<"\t\t"<<getOriginalName(func->getName())<<"\n";
}
fout<<"\n\tTainted Functions (group by Caller-Functions): \n\n";
for (auto const& x : this->InfluencedFuncList) {
fout<<"\t\t"<< x.first <<"\n";
set<string> s;
for (auto const& y : x.second) {
s.insert(getOriginalName(y->getName()));
}
for (auto const& z : s) {
fout<<"\t\t\t\t"<<z<<"\n";
}
fout<<"\n";
}
fout<<"\n\tCalled Functions: \n";
fout.close();
for(auto i=this->FuncInfoList.begin(), e=this->FuncInfoList.end(); i!=e; i++)
{
struct FuncInfo* func_info = *i;
this->dumpFuncInfo( output_file, func_info, 2);
}
fout.open(output_file.c_str(), ios::app);
fout<<"\n\tCalled Chain:"<<"\n";
fout.close();
for(auto i=this->InstInfoList.begin(), e=this->InstInfoList.end(); i!=e; i++)
{
struct InstInfo* inst_info = *i;
this->dumpInstInfo( output_file, inst_info, 2);
}
fout.open(output_file.c_str(), ios::app);
fout<<"\n\tRelated GlobalVariables: \n";
for(auto i=this->InfluencedGVList.begin(); i!=this->InfluencedGVList.end(); i++)
{
GlobalVariable* gv = *i;
fout<<"\t\t"<<getAsString(gv)<<"\n";
}
fout.close();
return true;
}
bool writeStatistics(string stat_file, bool initialize)
{
ofstream stat_f(stat_file.c_str(), ios::app);
if(! stat_f)
{
llvm::outs()<<"Open statistics file "<<stat_file<<" FAILED!\n";
return false;
}
if( initialize )
{
stat_f<<"Config Name, ";
stat_f<<"Config Variable Name, ";
stat_f<<"Is Reconfigurable, ";
stat_f<<"Caller Function Number, ";
stat_f<<"Strong Called Function Number, ";
stat_f<<"Weak Called Function Number, ";
stat_f<<"Inside Called Function Dataflow Instruction Number, ";
stat_f<<"Inside Called Function Dataflow Line Number, ";
stat_f<<"Strong Related Dataflow Instruction Number, ";
stat_f<<"Strong Related Dataflow Line Number, ";
stat_f<<"Weak Related Dataflow Instruction Number, ";
stat_f<<"Weak Related Dataflow Line Number, ";
stat_f<<"Control Condition Number, ";
stat_f<<"Related Control BasicBlock Number, ";
stat_f<<"Related Control Instruction Number, ";
stat_f<<"Related Control Line Number\n";
stat_f.close();
return true;
}
/// TAG: Config Name
stat_f<<this->NameInfo->OriginalConfigName<<", ";
/// TAG: Config Variable Name
stat_f<<this->NameInfo->getNameAsString()<<", ";
/// TAG: Is Reconfigurable
stat_f<<this->NameInfo->isDynamicConfigurable<<" "<<this->NameInfo->DynamicConfigurableStatus<<", ";
/// TAG: Caller Function Number
stat_f<<this->CallerFunctionList.size()<<", ";
/// TAG: Strong Called Function Number
vector<string> strong_related_func_list;
for(auto it=this->FuncInfoList.begin(); it!=this->FuncInfoList.end(); it++)
{
struct FuncInfo* func_info = *it;
if( std::find(strong_related_func_list.begin(),strong_related_func_list.end(), func_info->FuncName) == strong_related_func_list.end())
strong_related_func_list.push_back(func_info->FuncName);
}
stat_f<<strong_related_func_list.size()<<", ";
/// TAG: Weak Called Function Number
vector<string> weak_related_func_list;
queue<struct FuncInfo*> Q;
for(auto it=this->FuncInfoList.begin(); it!=this->FuncInfoList.end(); it++)
{
struct FuncInfo* func_info = *it;
for(unsigned i=0; i<func_info->InsideFuncInfoList.size(); i++)
{
Q.push(func_info->InsideFuncInfoList[0]);
}
while( ! Q.empty() )
{
struct FuncInfo* func_info = Q.front();
weak_related_func_list.push_back(func_info->FuncName);
for(unsigned i=0; i<func_info->InsideFuncInfoList.size(); i++)
{
struct FuncInfo* inside_func_info = func_info->InsideFuncInfoList[i];
bool searchFlag = false;
for(unsigned i=0; i<Q.size(); ++i) {
if(Q.front() == inside_func_info)
searchFlag = true;
Q.push(Q.front());
Q.pop();
}
if(std::find(weak_related_func_list.begin(), weak_related_func_list.end(), inside_func_info->FuncName) != weak_related_func_list.end())
searchFlag = true;
if(searchFlag == false)
Q.push(inside_func_info);
}
Q.pop();
}
}
stat_f<<weak_related_func_list.size()<<", ";
/// TAG: Inside Called Function Dataflow Instruction Number
vector<struct InstInfo*> inside_inst_info_list;
queue<struct FuncInfo*> Q1;
for(auto it=this->FuncInfoList.begin(); it!=this->FuncInfoList.end(); it++)
{
struct FuncInfo* func_info = *it;
Q1.push(func_info);
while( ! Q1.empty() )
{
struct FuncInfo* func_info = Q1.front();
for(auto ite=func_info->ArgInstInfoList.begin(); ite!=func_info->ArgInstInfoList.end(); ite++)
{
inside_inst_info_list.push_back(*ite);
}
for(unsigned i=0; i<func_info->InsideFuncInfoList.size(); i++)
{
struct FuncInfo* inside_func_info = func_info->InsideFuncInfoList[i];
Q1.push(inside_func_info);
}
Q1.pop();
}
}
stat_f<<inside_inst_info_list.size()<<", ";
/// TAG: Inside Called Function Dataflow Line Number
vector<struct SrcLoc> inside_related_lines;
inside_related_lines.clear();
for(auto ii=inside_inst_info_list.begin(); ii!=inside_inst_info_list.end(); ii++)
{
struct InstInfo* inst_info = *ii;
bool flag_matched = false;
for(unsigned c=0; c<inside_related_lines.size(); c++)
{
if(inside_related_lines[c].Filename == inst_info->InstLoc.Filename &&
inside_related_lines[c].Dirname == inst_info->InstLoc.Dirname &&
inside_related_lines[c].Line == inst_info->InstLoc.Line)
{
flag_matched = true;
break;
}
}
if(flag_matched == false)
{
inside_related_lines.push_back(inst_info->InstLoc);
}
}
stat_f<<inside_related_lines.size()<<", ";
/// TAG: Strong Related Dataflow Instruction Number
vector<struct InstInfo*> strong_related_inst_list;
for(auto it=this->InstInfoList.begin(); it!=this->InstInfoList.end(); it++)
{
struct InstInfo* inst_info = *it;
if(inst_info->InfluenceLevel == STRONG)
strong_related_inst_list.push_back(inst_info);
}
stat_f<<strong_related_inst_list.size()<<", ";
/// TAG: Strong Related Dataflow Line Number
vector<struct SrcLoc> strong_related_lines;
strong_related_lines.clear();
for(auto ii=strong_related_inst_list.begin(); ii!=strong_related_inst_list.end(); ii++)
{
struct InstInfo* inst_info = *ii;
bool flag_matched = false;