svf-tools 1.0.682 → 1.0.684

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,8 +27,9 @@
27
27
  * Author: Xiaokang Fan
28
28
  */
29
29
 
30
- #include "Util/Options.h"
31
30
  #include <queue>
31
+ #include <algorithm>
32
+ #include "Util/Options.h"
32
33
  #include "SVFIR/SVFModule.h"
33
34
  #include "SVFIR/SVFModuleRW.h"
34
35
  #include "Util/SVFUtil.h"
@@ -95,7 +96,7 @@ SVFModule* LLVMModuleSet::buildSVFModule(Module &mod)
95
96
  double endSVFModuleTime = SVFStat::getClk(true);
96
97
  SVFStat::timeOfBuildingLLVMModule = (endSVFModuleTime - startSVFModuleTime)/TIMEINTERVAL;
97
98
 
98
- build_symbol_table();
99
+ buildSymbolTable();
99
100
 
100
101
  return svfModule.get();
101
102
  }
@@ -118,12 +119,12 @@ SVFModule* LLVMModuleSet::buildSVFModule(const std::vector<std::string> &moduleN
118
119
  double endSVFModuleTime = SVFStat::getClk(true);
119
120
  SVFStat::timeOfBuildingLLVMModule = (endSVFModuleTime - startSVFModuleTime)/TIMEINTERVAL;
120
121
 
121
- build_symbol_table();
122
+ buildSymbolTable();
122
123
 
123
124
  return svfModule.get();
124
125
  }
125
126
 
126
- void LLVMModuleSet::build_symbol_table() const
127
+ void LLVMModuleSet::buildSymbolTable() const
127
128
  {
128
129
  double startSymInfoTime = SVFStat::getClk(true);
129
130
  if (!SVFModule::pagReadFromTXT())
@@ -134,7 +135,8 @@ void LLVMModuleSet::build_symbol_table() const
134
135
  builder.buildMemModel(svfModule.get());
135
136
  }
136
137
  double endSymInfoTime = SVFStat::getClk(true);
137
- SVFStat::timeOfBuildingSymbolTable = (endSymInfoTime - startSymInfoTime)/TIMEINTERVAL;
138
+ SVFStat::timeOfBuildingSymbolTable =
139
+ (endSymInfoTime - startSymInfoTime) / TIMEINTERVAL;
138
140
  }
139
141
 
140
142
  void LLVMModuleSet::build()
@@ -158,67 +160,79 @@ void LLVMModuleSet::createSVFDataStructure()
158
160
  for (Module& mod : modules)
159
161
  {
160
162
  /// Function
161
- for (Module::const_iterator it = mod.begin(), eit = mod.end(); it != eit; ++it)
163
+ for (const Function& func : mod.functions())
162
164
  {
163
- const Function* func = &*it;
164
- SVFLoopAndDomInfo* ld = new SVFLoopAndDomInfo();
165
- SVFFunction* svfFunc = new SVFFunction(func->getName().str(), getSVFType(func->getType()), SVFUtil::cast<SVFFunctionType>(getSVFType(func->getFunctionType())), func->isDeclaration(), LLVMUtil::isIntrinsicFun(func), func->hasAddressTaken(), func->isVarArg(), ld);
165
+ SVFFunction* svfFunc = new SVFFunction(
166
+ func.getName().str(), getSVFType(func.getType()),
167
+ SVFUtil::cast<SVFFunctionType>(
168
+ getSVFType(func.getFunctionType())),
169
+ func.isDeclaration(), LLVMUtil::isIntrinsicFun(&func),
170
+ func.hasAddressTaken(), func.isVarArg(), new SVFLoopAndDomInfo);
166
171
  svfModule->addFunctionSet(svfFunc);
167
- addFunctionMap(func,svfFunc);
172
+ addFunctionMap(&func, svfFunc);
168
173
 
169
- for (Function::const_arg_iterator I = func->arg_begin(), E = func->arg_end(); I != E; ++I)
174
+ for (const Argument& arg : func.args())
170
175
  {
171
- const Argument* arg = &*I;
172
- SVFArgument* svfarg = new SVFArgument(arg->getName().str(), getSVFType(arg->getType()), svfFunc, arg->getArgNo(), LLVMUtil::isArgOfUncalledFunction(arg));
176
+ SVFArgument* svfarg = new SVFArgument(
177
+ arg.getName().str(), getSVFType(arg.getType()), svfFunc,
178
+ arg.getArgNo(), LLVMUtil::isArgOfUncalledFunction(&arg));
173
179
  svfFunc->addArgument(svfarg);
174
- addArgumentMap(arg,svfarg);
180
+ addArgumentMap(&arg, svfarg);
175
181
  }
176
182
 
177
- for (Function::const_iterator bit = func->begin(), ebit = func->end(); bit != ebit; ++bit)
183
+ for (const BasicBlock& bb : func)
178
184
  {
179
- const BasicBlock* bb = &*bit;
180
- SVFBasicBlock* svfBB = new SVFBasicBlock(bb->getName().str(), getSVFType(bb->getType()), svfFunc);
185
+ SVFBasicBlock* svfBB = new SVFBasicBlock(
186
+ bb.getName().str(), getSVFType(bb.getType()), svfFunc);
181
187
  svfFunc->addBasicBlock(svfBB);
182
- addBasicBlockMap(bb,svfBB);
183
- for (BasicBlock::const_iterator iit = bb->begin(), eiit = bb->end(); iit != eiit; ++iit)
188
+ addBasicBlockMap(&bb, svfBB);
189
+ for (const Instruction& inst : bb)
184
190
  {
185
- const Instruction* inst = &*iit;
186
191
  SVFInstruction* svfInst = nullptr;
187
- if(const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
192
+ if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(&inst))
188
193
  {
189
- if(LLVMUtil::isVirtualCallSite(call))
190
- svfInst = new SVFVirtualCallInst(call->getName().str(), getSVFType(call->getType()), svfBB,call->getFunctionType()->isVarArg(),inst->isTerminator());
194
+ if (LLVMUtil::isVirtualCallSite(call))
195
+ svfInst = new SVFVirtualCallInst(
196
+ call->getName().str(),
197
+ getSVFType(call->getType()), svfBB,
198
+ call->getFunctionType()->isVarArg(),
199
+ inst.isTerminator());
191
200
  else
192
- svfInst = new SVFCallInst(call->getName().str(), getSVFType(call->getType()), svfBB,call->getFunctionType()->isVarArg(),inst->isTerminator());
201
+ svfInst = new SVFCallInst(
202
+ call->getName().str(),
203
+ getSVFType(call->getType()), svfBB,
204
+ call->getFunctionType()->isVarArg(),
205
+ inst.isTerminator());
193
206
  }
194
207
  else
195
208
  {
196
- svfInst = new SVFInstruction(inst->getName().str(),getSVFType(inst->getType()), svfBB, inst->isTerminator(), SVFUtil::isa<ReturnInst>(inst));
209
+ svfInst = new SVFInstruction(
210
+ inst.getName().str(), getSVFType(inst.getType()),
211
+ svfBB, inst.isTerminator(),
212
+ SVFUtil::isa<ReturnInst>(inst));
197
213
  }
198
214
  svfBB->addInstruction(svfInst);
199
- addInstructionMap(inst,svfInst);
215
+ addInstructionMap(&inst, svfInst);
200
216
  }
201
217
  }
202
218
  }
203
219
 
204
220
  /// GlobalVariable
205
- for (Module::const_global_iterator it = mod.global_begin(),
206
- eit = mod.global_end(); it != eit; ++it)
221
+ for (const GlobalVariable& global : mod.globals())
207
222
  {
208
- const GlobalVariable* global = &*it;
209
- SVFGlobalValue* svfglobal = new SVFGlobalValue(global->getName().str(), getSVFType(global->getType()));
223
+ SVFGlobalValue* svfglobal = new SVFGlobalValue(
224
+ global.getName().str(), getSVFType(global.getType()));
210
225
  svfModule->addGlobalSet(svfglobal);
211
- addGlobalValueMap(global,svfglobal);
226
+ addGlobalValueMap(&global, svfglobal);
212
227
  }
213
228
 
214
229
  /// GlobalAlias
215
- for (Module::const_alias_iterator it = mod.alias_begin(),
216
- eit = mod.alias_end(); it != eit; ++it)
230
+ for (const GlobalAlias& alias : mod.aliases())
217
231
  {
218
- const GlobalAlias *alias = &*it;
219
- SVFGlobalValue* svfalias = new SVFGlobalValue(alias->getName().str(), getSVFType(alias->getType()));
232
+ SVFGlobalValue* svfalias = new SVFGlobalValue(
233
+ alias.getName().str(), getSVFType(alias.getType()));
220
234
  svfModule->addAliasSet(svfalias);
221
- addGlobalValueMap(alias,svfalias);
235
+ addGlobalValueMap(&alias, svfalias);
222
236
  }
223
237
  }
224
238
  }
@@ -228,15 +242,14 @@ void LLVMModuleSet::initSVFFunction()
228
242
  for (Module& mod : modules)
229
243
  {
230
244
  /// Function
231
- for (Module::iterator it = mod.begin(), eit = mod.end(); it != eit; ++it)
245
+ for (const Function& f : mod.functions())
232
246
  {
233
- const Function* f = &*it;
234
- SVFFunction* svffun = getSVFFunction(f);
235
- initSVFBasicBlock(f);
247
+ SVFFunction* svffun = getSVFFunction(&f);
248
+ initSVFBasicBlock(&f);
236
249
 
237
- if (SVFUtil::isExtCall(svffun) == false)
250
+ if (!SVFUtil::isExtCall(svffun))
238
251
  {
239
- initDomTree(svffun, f);
252
+ initDomTree(svffun, &f);
240
253
  }
241
254
  }
242
255
  }
@@ -554,36 +567,34 @@ std::vector<const Function* > LLVMModuleSet::getLLVMGlobalFunctions(const Global
554
567
 
555
568
  void LLVMModuleSet::addSVFMain()
556
569
  {
557
- std::vector<const Function* > ctor_funcs;
558
- std::vector<const Function* > dtor_funcs;
559
- Function* orgMain = 0;
570
+ std::vector<const Function*> ctor_funcs;
571
+ std::vector<const Function*> dtor_funcs;
572
+ Function* orgMain = 0;
560
573
  Module* mainMod = nullptr;
561
574
 
562
575
  for (Module &mod : modules)
563
576
  {
564
577
  // Collect ctor and dtor functions
565
- for (Module::global_iterator it = mod.global_begin(), eit = mod.global_end(); it != eit; ++it)
578
+ for (const GlobalVariable& global : mod.globals())
566
579
  {
567
- const GlobalVariable *global = &*it;
568
-
569
- if (global->getName().equals(SVF_GLOBAL_CTORS) &&
570
- global->hasInitializer())
580
+ if (global.getName().equals(SVF_GLOBAL_CTORS) && global.hasInitializer())
571
581
  {
572
- ctor_funcs = getLLVMGlobalFunctions(global);
582
+ ctor_funcs = getLLVMGlobalFunctions(&global);
573
583
  }
574
- else if (global->getName().equals(SVF_GLOBAL_DTORS) &&
575
- global->hasInitializer())
584
+ else if (global.getName().equals(SVF_GLOBAL_DTORS) && global.hasInitializer())
576
585
  {
577
- dtor_funcs = getLLVMGlobalFunctions(global);
586
+ dtor_funcs = getLLVMGlobalFunctions(&global);
578
587
  }
579
588
  }
580
589
 
581
590
  // Find main function
582
591
  for (auto &func : mod)
583
592
  {
584
- if (func.getName().equals(SVF_MAIN_FUNC_NAME))
585
- assert(false && SVF_MAIN_FUNC_NAME " already defined");
586
- if(func.getName().equals("main"))
593
+ auto funName = func.getName();
594
+
595
+ assert(!funName.equals(SVF_MAIN_FUNC_NAME) && SVF_MAIN_FUNC_NAME " already defined");
596
+
597
+ if (funName.equals("main"))
587
598
  {
588
599
  orgMain = &func;
589
600
  mainMod = &mod;
@@ -597,10 +608,10 @@ void LLVMModuleSet::addSVFMain()
597
608
  (ctor_funcs.size() > 0 || dtor_funcs.size() > 0))
598
609
  {
599
610
  assert(mainMod && "Module with main function not found.");
600
- Module & M = *mainMod;
611
+ Module& M = *mainMod;
601
612
  // char **
602
- Type* i8ptr2 = PointerType::getInt8PtrTy(M.getContext())->getPointerTo();
603
- Type* i32 = IntegerType::getInt32Ty(M.getContext());
613
+ Type* i8ptr2 = PointerType::getInt8PtrTy(M.getContext())->getPointerTo();
614
+ Type* i32 = IntegerType::getInt32Ty(M.getContext());
604
615
  // define void @svf.main(i32, i8**, i8**)
605
616
  #if (LLVM_VERSION_MAJOR >= 9)
606
617
  FunctionCallee svfmainFn = M.getOrInsertFunction(
@@ -621,7 +632,7 @@ void LLVMModuleSet::addSVFMain()
621
632
  IRBuilder Builder(block);
622
633
  // emit "call void @ctor()". ctor_funcs is sorted so the functions are
623
634
  // emitted in the order of priority
624
- for(auto & ctor: ctor_funcs)
635
+ for (auto& ctor : ctor_funcs)
625
636
  {
626
637
  auto target = M.getOrInsertFunction(
627
638
  ctor->getName(),
@@ -632,18 +643,15 @@ void LLVMModuleSet::addSVFMain()
632
643
  // main() should be called after all ctor functions and before dtor
633
644
  // functions.
634
645
  Function::arg_iterator arg_it = svfmain->arg_begin();
635
- Value* args[] = {arg_it, arg_it + 1, arg_it + 2 };
646
+ Value* args[] = {arg_it, arg_it + 1, arg_it + 2};
636
647
  size_t cnt = orgMain->arg_size();
637
648
  assert(cnt <= 3 && "Too many arguments for main()");
638
- Builder.CreateCall(orgMain, llvm::ArrayRef<Value*>(args,args + cnt));
649
+ Builder.CreateCall(orgMain, llvm::ArrayRef<Value*>(args, args + cnt));
639
650
  // emit "call void @dtor()". dtor_funcs is sorted so the functions are
640
651
  // emitted in the order of priority
641
- for (auto &dtor : dtor_funcs)
652
+ for (auto& dtor : dtor_funcs)
642
653
  {
643
- auto target = M.getOrInsertFunction(
644
- dtor->getName(),
645
- Type::getVoidTy(M.getContext())
646
- );
654
+ auto target = M.getOrInsertFunction(dtor->getName(), Type::getVoidTy(M.getContext()));
647
655
  Builder.CreateCall(target);
648
656
  }
649
657
  // return;
@@ -662,107 +670,79 @@ void LLVMModuleSet::buildFunToFunMap()
662
670
  for (Module& mod : modules)
663
671
  {
664
672
  /// Function
665
- for (Module::iterator it = mod.begin(), eit = mod.end(); it != eit; ++it)
673
+ for (const Function& fun : mod.functions())
666
674
  {
667
- const Function* fun = &*it;
668
- if (fun->isDeclaration())
675
+ if (fun.isDeclaration())
669
676
  {
670
- funDecls.insert(fun);
671
- declNames.insert(fun->getName().str());
677
+ funDecls.insert(&fun);
678
+ declNames.insert(fun.getName().str());
672
679
  }
673
680
  else
674
681
  {
675
- funDefs.insert(fun);
676
- defNames.insert(fun->getName().str());
682
+ funDefs.insert(&fun);
683
+ defNames.insert(fun.getName().str());
677
684
  }
678
685
  }
679
686
  }
680
687
  // Find the intersectNames
681
- OrderedSet<string>::iterator declIter, defIter;
682
- declIter = declNames.begin();
683
- defIter = defNames.begin();
684
- while (declIter != declNames.end() && defIter != defNames.end())
685
- {
686
- if (*declIter < *defIter)
687
- {
688
- declIter++;
689
- }
690
- else
691
- {
692
- if (!(*defIter < *declIter))
693
- {
694
- intersectNames.insert(*declIter);
695
- declIter++;
696
- }
697
- defIter++;
698
- }
699
- }
688
+ std::set_intersection(
689
+ declNames.begin(), declNames.end(), defNames.begin(), defNames.end(),
690
+ std::inserter(intersectNames, intersectNames.end()));
700
691
 
701
692
  ///// name to def map
702
693
  NameToFunDefMapTy nameToFunDefMap;
703
- for (Set<const Function*>::iterator it = funDefs.begin(),
704
- eit = funDefs.end(); it != eit; ++it)
694
+ for (const Function* fdef : funDefs)
705
695
  {
706
- const Function* fdef = *it;
707
696
  string funName = fdef->getName().str();
708
- if (intersectNames.find(funName) == intersectNames.end())
709
- continue;
710
- nameToFunDefMap[funName] = fdef;
697
+ if (intersectNames.find(funName) != intersectNames.end())
698
+ {
699
+ nameToFunDefMap.emplace(std::move(funName), fdef);
700
+ }
711
701
  }
712
702
 
713
703
  ///// name to decls map
714
704
  NameToFunDeclsMapTy nameToFunDeclsMap;
715
- for (Set<const Function*>::iterator it = funDecls.begin(),
716
- eit = funDecls.end(); it != eit; ++it)
705
+ for (const Function* fdecl : funDecls)
717
706
  {
718
- const Function* fdecl = *it;
719
707
  string funName = fdecl->getName().str();
720
- if (intersectNames.find(funName) == intersectNames.end())
721
- continue;
722
- NameToFunDeclsMapTy::iterator mit = nameToFunDeclsMap.find(funName);
723
- if (mit == nameToFunDeclsMap.end())
724
- {
725
- Set<const Function*> decls;
726
- decls.insert(fdecl);
727
- nameToFunDeclsMap[funName] = decls;
728
- }
729
- else
708
+ if (intersectNames.find(funName) != intersectNames.end())
730
709
  {
731
- Set<const Function*> &decls = mit->second;
732
- decls.insert(fdecl);
710
+ // pair with key funName will be created automatically if it does
711
+ // not exist
712
+ nameToFunDeclsMap[std::move(funName)].insert(fdecl);
733
713
  }
734
714
  }
735
715
 
736
716
  /// Fun decl --> def
737
- for (Set<const Function*>::iterator it = funDecls.begin(),
738
- eit = funDecls.end(); it != eit; ++it)
717
+ for (const Function* fdecl : funDecls)
739
718
  {
740
- const Function* fdecl = *it;
741
719
  string funName = fdecl->getName().str();
742
- if (intersectNames.find(funName) == intersectNames.end())
743
- continue;
744
- NameToFunDefMapTy::iterator mit = nameToFunDefMap.find(funName);
745
- if (mit == nameToFunDefMap.end())
746
- continue;
747
- FunDeclToDefMap[fdecl] = mit->second;
720
+ NameToFunDefMapTy::iterator mit;
721
+ if (intersectNames.find(funName) != intersectNames.end() &&
722
+ (mit = nameToFunDefMap.find(funName)) != nameToFunDefMap.end())
723
+ {
724
+ FunDeclToDefMap[fdecl] = mit->second;
725
+ }
748
726
  }
749
727
 
750
728
  /// Fun def --> decls
751
- for (Set<const Function*>::iterator it = funDefs.begin(),
752
- eit = funDefs.end(); it != eit; ++it)
729
+ for (const Function* fdef : funDefs)
753
730
  {
754
- const Function* fdef = *it;
755
731
  string funName = fdef->getName().str();
756
732
  if (intersectNames.find(funName) == intersectNames.end())
757
733
  continue;
758
734
  NameToFunDeclsMapTy::iterator mit = nameToFunDeclsMap.find(funName);
759
735
  if (mit == nameToFunDeclsMap.end())
760
736
  continue;
737
+
761
738
  std::vector<const Function*>& decls = FunDefToDeclsMap[fdef];
762
- for (Set<const Function*>::iterator sit = mit->second.begin(),
763
- seit = mit->second.end(); sit != seit; ++sit)
739
+ const auto& declsSet = mit->second;
740
+ // Reserve space for decls to avoid more than 1 reallocation
741
+ decls.reserve(decls.size() + declsSet.size());
742
+
743
+ for (const Function* decl : declsSet)
764
744
  {
765
- decls.push_back(*sit);
745
+ decls.push_back(decl);
766
746
  }
767
747
  }
768
748
  }
@@ -774,35 +754,22 @@ void LLVMModuleSet::buildGlobalDefToRepMap()
774
754
  for (Module &mod : modules)
775
755
  {
776
756
  // Collect ctor and dtor functions
777
- for (Module::global_iterator it = mod.global_begin(), eit = mod.global_end(); it != eit; ++it)
757
+ for (GlobalVariable& global : mod.globals())
778
758
  {
779
- GlobalVariable *global = &*it;
780
- if (global->hasPrivateLinkage())
759
+ if (global.hasPrivateLinkage())
781
760
  continue;
782
- string name = global->getName().str();
761
+ string name = global.getName().str();
783
762
  if (name.empty())
784
763
  continue;
785
- NameToGlobalsMapTy::iterator mit = nameToGlobalsMap.find(name);
786
- if (mit == nameToGlobalsMap.end())
787
- {
788
- Set<GlobalVariable*> globals;
789
- globals.insert(global);
790
- nameToGlobalsMap[name] = globals;
791
- }
792
- else
793
- {
794
- Set<GlobalVariable*> &globals = mit->second;
795
- globals.insert(global);
796
- }
764
+ nameToGlobalsMap[std::move(name)].insert(&global);
797
765
  }
798
766
  }
799
767
 
800
- for (NameToGlobalsMapTy::iterator it = nameToGlobalsMap.begin(),
801
- eit = nameToGlobalsMap.end(); it != eit; ++it)
768
+ for (const auto& pair : nameToGlobalsMap)
802
769
  {
803
- Set<GlobalVariable*> &globals = it->second;
770
+ const Set<GlobalVariable*> &globals = pair.second;
804
771
 
805
- auto repIt =
772
+ const auto repIt =
806
773
  std::find_if(globals.begin(), globals.end(),
807
774
  [](GlobalVariable* g)
808
775
  {
@@ -815,12 +782,17 @@ void LLVMModuleSet::buildGlobalDefToRepMap()
815
782
  : (assert(!globals.empty() && "Empty global set"),
816
783
  *globals.begin());
817
784
 
818
- for (Set<GlobalVariable*>::iterator sit = globals.begin(),
785
+ for (Set<GlobalVariable*>::const_iterator sit = globals.begin(),
819
786
  seit = globals.end(); sit != seit; ++sit)
820
787
  {
821
788
  GlobalVariable *cur = *sit;
822
789
  GlobalDefToRepMap[cur] = rep;
823
790
  }
791
+
792
+ for (const GlobalVariable* cur : globals)
793
+ {
794
+ GlobalDefToRepMap[cur] = rep;
795
+ }
824
796
  }
825
797
  }
826
798