Static Value-Flow Analysis
Functions
SVF::LLVMUtil Namespace Reference

Functions

bool isCallSite (const Instruction *inst)
 Whether an instruction is a call or invoke instruction. More...
 
bool isCallSite (const Value *val)
 Whether an instruction is a call or invoke instruction. More...
 
const CallBasegetLLVMCallSite (const Value *value)
 Return LLVM callsite given a value. More...
 
const FunctiongetCallee (const CallBase *cs)
 
const FunctiongetLLVMFunction (const Value *val)
 Return LLVM function if this value is. More...
 
const FunctiongetProgFunction (const std::string &funName)
 Get program entry function from module. More...
 
bool isProgEntryFunction (const Function *fun)
 Check whether a function is an entry function (i.e., main) More...
 
bool isBlackholeSym (const Value *val)
 Check whether this value is a black hole. More...
 
bool isNullPtrSym (const Value *val)
 Check whether this value is a black hole. More...
 
static TypegetPtrElementType (const PointerType *pty)
 
u32_t getNumOfElements (const Type *ety)
 Return size of this object based on LLVM value. More...
 
bool isObject (const Value *ref)
 Return true if this value refers to a object. More...
 
bool isUncalledFunction (const Function *fun)
 whether this is a function without any possible caller? More...
 
bool ArgInDeadFunction (const Value *val)
 whether this is an argument in dead function More...
 
bool ArgInProgEntryFunction (const Value *val)
 Return true if this is an argument of a program entry function (e.g. main) More...
 
bool isPtrInUncalledFunction (const Value *value)
 Return true if this is value in a dead function (function without any caller) More...
 
bool isNoCallerFunction (const Function *fun)
 Function does not have any possible caller in the call graph. More...
 
bool isArgOfUncalledFunction (const Value *val)
 Return true if the argument in a function does not have a caller. More...
 
bool basicBlockHasRetInst (const BasicBlock *bb)
 Return true if the function has a return instruction. More...
 
bool functionDoesNotRet (const Function *fun)
 
void getFunReachableBBs (const Function *svfFun, std::vector< const SVFBasicBlock * > &bbs)
 Get reachable basic block from function entry. More...
 
const ValuestripConstantCasts (const Value *val)
 Strip off the constant casts. More...
 
const ValuestripAllCasts (const Value *val)
 Strip off the all casts. More...
 
const ValuegetFirstUseViaCastInst (const Value *val)
 
const ConstantExprisGepConstantExpr (const Value *val)
 Return corresponding constant expression, otherwise return nullptr. More...
 
const ConstantExprisInt2PtrConstantExpr (const Value *val)
 
const ConstantExprisPtr2IntConstantExpr (const Value *val)
 
const ConstantExprisCastConstantExpr (const Value *val)
 
const ConstantExprisSelectConstantExpr (const Value *val)
 
const ConstantExprisTruncConstantExpr (const Value *val)
 
const ConstantExprisCmpConstantExpr (const Value *val)
 
const ConstantExprisBinaryConstantExpr (const Value *val)
 
const ConstantExprisUnaryConstantExpr (const Value *val)
 
static DataLayoutgetDataLayout (Module *mod)
 
void getNextInsts (const Instruction *curInst, std::vector< const Instruction * > &instList)
 Get the next instructions following control flow. More...
 
bool isNoPrecessorBasicBlock (const BasicBlock *bb)
 
bool isIRFile (const std::string &filename)
 Check whether a file is an LLVM IR file. More...
 
void processArguments (int argc, char **argv, int &arg_num, char **arg_value, std::vector< std::string > &moduleNameVec)
 Parse argument for multi-module analysis. More...
 
const std::string getSourceLoc (const Value *val)
 
const std::string getSourceLocOfFunction (const Function *F)
 
bool isIntrinsicInst (const Instruction *inst)
 Return true if it is an intrinsic instruction. More...
 
bool isIntrinsicFun (const Function *func)
 
std::vector< const Function * > getCalledFunctions (const Function *F)
 Get all called funcions in a parent function. More...
 
std::string restoreFuncName (std::string funcName)
 
const SVFFunctiongetFunction (const std::string &name)
 Get the corresponding Function based on its name. More...
 
bool isConstDataOrAggData (const Value *val)
 Return true if the value refers to constant data, e.g., i32 0. More...
 
const ValuegetGlobalRep (const Value *val)
 find the unique defined global across multiple modules More...
 
bool isConstantObjSym (const SVFValue *val)
 Check whether this value points-to a constant object. More...
 
bool isConstantObjSym (const Value *val)
 Check whether this value points-to a constant object. More...
 
void viewCFG (const Function *fun)
 
void viewCFGOnly (const Function *fun)
 
std::string dumpValue (const Value *val)
 
std::string dumpType (const Type *type)
 
std::string dumpValueAndDbgInfo (const Value *val)
 
bool isHeapAllocExtCallViaRet (const Instruction *inst)
 
bool isHeapAllocExtCallViaArg (const Instruction *inst)
 
bool isHeapAllocExtCall (const Instruction *inst)
 
bool isNonInstricCallSite (const Instruction *inst)
 Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls. More...
 
const FunctiongetProgEntryFunction (Module &module)
 Get program entry function from module. More...
 

Function Documentation

◆ ArgInDeadFunction()

bool SVF::LLVMUtil::ArgInDeadFunction ( const Value val)
inline

whether this is an argument in dead function

Definition at line 122 of file LLVMUtil.h.

123 {
124  return SVFUtil::isa<Argument>(val)
125  && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
126 }
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
Definition: LLVMUtil.cpp:159

◆ ArgInProgEntryFunction()

bool SVF::LLVMUtil::ArgInProgEntryFunction ( const Value val)
inline

Return true if this is an argument of a program entry function (e.g. main)

Definition at line 130 of file LLVMUtil.h.

131 {
132  return SVFUtil::isa<Argument>(val) &&
134  SVFUtil::cast<Argument>(val)->getParent());
135 }
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main)
Definition: LLVMUtil.h:79

◆ basicBlockHasRetInst()

bool SVF::LLVMUtil::basicBlockHasRetInst ( const BasicBlock bb)

Return true if the function has a return instruction.

Return true if the basic block has a return instruction

Definition at line 109 of file LLVMUtil.cpp.

110 {
111  for (BasicBlock::const_iterator it = bb->begin(), eit = bb->end();
112  it != eit; ++it)
113  {
114  if(SVFUtil::isa<ReturnInst>(*it))
115  return true;
116  }
117  return false;
118 }

◆ dumpType()

std::string SVF::LLVMUtil::dumpType ( const Type type)

Definition at line 596 of file LLVMUtil.cpp.

597 {
598  std::string str;
599  llvm::raw_string_ostream rawstr(str);
600  if (type)
601  rawstr << " " << *type << " ";
602  else
603  rawstr << " llvm type is null";
604  return rawstr.str();
605 }
newitem type
Definition: cJSON.cpp:2739
const char *const string
Definition: cJSON.h:172

◆ dumpValue()

std::string SVF::LLVMUtil::dumpValue ( const Value val)

Definition at line 585 of file LLVMUtil.cpp.

586 {
587  std::string str;
588  llvm::raw_string_ostream rawstr(str);
589  if (val)
590  rawstr << " " << *val << " ";
591  else
592  rawstr << " llvm Value is null";
593  return rawstr.str();
594 }

◆ dumpValueAndDbgInfo()

std::string SVF::LLVMUtil::dumpValueAndDbgInfo ( const Value val)

Definition at line 607 of file LLVMUtil.cpp.

608 {
609  std::string str;
610  llvm::raw_string_ostream rawstr(str);
611  if (val)
612  rawstr << dumpValue(val) << getSourceLoc(val);
613  else
614  rawstr << " llvm Value is null";
615  return rawstr.str();
616 }
const std::string getSourceLoc(const Value *val)
Definition: LLVMUtil.cpp:430
std::string dumpValue(const Value *val)
Definition: LLVMUtil.cpp:585

◆ functionDoesNotRet()

bool SVF::LLVMUtil::functionDoesNotRet ( const Function fun)

Return true if the function has a return instruction reachable from function entry

Definition at line 123 of file LLVMUtil.cpp.

124 {
125  const SVFFunction* svffun = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(fun);
126  if (SVFUtil::isExtCall(svffun))
127  {
128  return fun->getReturnType()->isVoidTy();
129  }
130  std::vector<const BasicBlock*> bbVec;
131  Set<const BasicBlock*> visited;
132  bbVec.push_back(&fun->getEntryBlock());
133  while(!bbVec.empty())
134  {
135  const BasicBlock* bb = bbVec.back();
136  bbVec.pop_back();
137  if (basicBlockHasRetInst(bb))
138  {
139  return false;
140  }
141 
142  for (succ_const_iterator sit = succ_begin(bb), esit = succ_end(bb);
143  sit != esit; ++sit)
144  {
145  const BasicBlock* succbb = (*sit);
146  if(visited.find(succbb)==visited.end())
147  visited.insert(succbb);
148  else
149  continue;
150  bbVec.push_back(succbb);
151  }
152  }
153  return true;
154 }
const SVFType * getReturnType() const
Returns the FunctionType.
Definition: SVFValue.h:388
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
Definition: LLVMUtil.cpp:109
bool isExtCall(const SVFFunction *fun)
Definition: SVFUtil.h:278
llvm::BasicBlock BasicBlock
Definition: BasicTypes.h:86
llvm::succ_const_iterator succ_const_iterator
LLVM Iterators.
Definition: BasicTypes.h:276
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96

◆ getCalledFunctions()

std::vector< const Function * > SVF::LLVMUtil::getCalledFunctions ( const Function F)

Get all called funcions in a parent function.

Definition at line 365 of file LLVMUtil.cpp.

366 {
367  std::vector<const Function *> calledFunctions;
368  for (const Instruction &I : instructions(F))
369  {
370  if (const CallBase *callInst = SVFUtil::dyn_cast<CallBase>(&I))
371  {
372  Function *calledFunction = callInst->getCalledFunction();
373  if (calledFunction)
374  {
375  calledFunctions.push_back(calledFunction);
376  std::vector<const Function *> nestedCalledFunctions = getCalledFunctions(calledFunction);
377  calledFunctions.insert(calledFunctions.end(), nestedCalledFunctions.begin(), nestedCalledFunctions.end());
378  }
379  }
380  }
381  return calledFunctions;
382 }
#define F(f)
std::vector< const Function * > getCalledFunctions(const Function *F)
Get all called funcions in a parent function.
Definition: LLVMUtil.cpp:365
llvm::CallBase CallBase
Definition: BasicTypes.h:146
llvm::Function Function
Definition: BasicTypes.h:85
llvm::Instruction Instruction
Definition: BasicTypes.h:87

◆ getCallee()

const Function* SVF::LLVMUtil::getCallee ( const CallBase cs)
inline

Definition at line 63 of file LLVMUtil.h.

64 {
65  // FIXME: do we need to strip-off the casts here to discover more library functions
66  return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
67 }

◆ getDataLayout()

static DataLayout* SVF::LLVMUtil::getDataLayout ( Module mod)
inlinestatic

Definition at line 279 of file LLVMUtil.h.

280 {
281  static DataLayout *dl = nullptr;
282  if (dl == nullptr)
283  dl = new DataLayout(mod);
284  return dl;
285 }
llvm::DataLayout DataLayout
Definition: BasicTypes.h:108

◆ getFirstUseViaCastInst()

const Value * SVF::LLVMUtil::getFirstUseViaCastInst ( const Value val)

Return the bitcast instruction right next to val, otherwise return nullptr

If type is void* (i8*) and val is immediately used at a bitcast instruction

Definition at line 279 of file LLVMUtil.cpp.

280 {
281  assert(SVFUtil::isa<PointerType>(val->getType()) && "this value should be a pointer type!");
283  const Value *latestUse = nullptr;
284  for (const auto &it : val->uses())
285  {
286  if (SVFUtil::isa<BitCastInst>(it.getUser()))
287  latestUse = it.getUser();
288  else
289  latestUse = nullptr;
290  }
291  return latestUse;
292 }
llvm::Value Value
LLVM Basic classes.
Definition: BasicTypes.h:82

◆ getFunction()

const SVFFunction * SVF::LLVMUtil::getFunction ( const std::string name)

Get the corresponding Function based on its name.

Definition at line 412 of file LLVMUtil.cpp.

413 {
414  return LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(name);
415 }
const char *const name
Definition: cJSON.h:264

◆ getFunReachableBBs()

void SVF::LLVMUtil::getFunReachableBBs ( const Function fun,
std::vector< const SVFBasicBlock * > &  reachableBBs 
)

Get reachable basic block from function entry.

Return reachable bbs from function entry

Definition at line 74 of file LLVMUtil.cpp.

75 {
76  assert(!SVFUtil::isExtCall(LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(fun)) && "The calling function cannot be an external function.");
77  //initial DominatorTree
78  DominatorTree dt;
79  dt.recalculate(const_cast<Function&>(*fun));
80 
81  Set<const BasicBlock*> visited;
82  std::vector<const BasicBlock*> bbVec;
83  bbVec.push_back(&fun->getEntryBlock());
84  while(!bbVec.empty())
85  {
86  const BasicBlock* bb = bbVec.back();
87  bbVec.pop_back();
88  const SVFBasicBlock* svfbb = LLVMModuleSet::getLLVMModuleSet()->getSVFBasicBlock(bb);
89  reachableBBs.push_back(svfbb);
90  if(DomTreeNode *dtNode = dt.getNode(const_cast<BasicBlock*>(bb)))
91  {
92  for (DomTreeNode::iterator DI = dtNode->begin(), DE = dtNode->end();
93  DI != DE; ++DI)
94  {
95  const BasicBlock* succbb = (*DI)->getBlock();
96  if(visited.find(succbb)==visited.end())
97  visited.insert(succbb);
98  else
99  continue;
100  bbVec.push_back(succbb);
101  }
102  }
103  }
104 }
llvm::DomTreeNode DomTreeNode
Definition: BasicTypes.h:134
llvm::DominatorTree DominatorTree
LLVM Dominators.
Definition: BasicTypes.h:133

◆ getGlobalRep()

const Value * SVF::LLVMUtil::getGlobalRep ( const Value val)

find the unique defined global across multiple modules

Definition at line 417 of file LLVMUtil.cpp.

418 {
419  if (const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
420  {
421  if (LLVMModuleSet::getLLVMModuleSet()->hasGlobalRep(gvar))
422  val = LLVMModuleSet::getLLVMModuleSet()->getGlobalRep(gvar);
423  }
424  return val;
425 }
llvm::GlobalVariable GlobalVariable
Definition: BasicTypes.h:130

◆ getLLVMCallSite()

const CallBase* SVF::LLVMUtil::getLLVMCallSite ( const Value value)
inline

Return LLVM callsite given a value.

Definition at line 57 of file LLVMUtil.h.

58 {
59  assert(isCallSite(value) && "not a callsite?");
60  return SVFUtil::cast<CallBase>(value);
61 }
bool isCallSite(const Value *val)
Whether an instruction is a call or invoke instruction.
Definition: LLVMUtil.h:50

◆ getLLVMFunction()

const Function* SVF::LLVMUtil::getLLVMFunction ( const Value val)
inline

Return LLVM function if this value is.

Definition at line 70 of file LLVMUtil.h.

71 {
72  return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
73 }

◆ getNextInsts()

void SVF::LLVMUtil::getNextInsts ( const Instruction curInst,
std::vector< const Instruction * > &  instList 
)

Get the next instructions following control flow.

Definition at line 551 of file LLVMUtil.cpp.

552 {
553  if (!curInst->isTerminator())
554  {
555  const Instruction* nextInst = curInst->getNextNode();
556  if (LLVMUtil::isIntrinsicInst(nextInst))
557  getNextInsts(nextInst, instList);
558  else
559  instList.push_back(nextInst);
560  }
561  else
562  {
563  const BasicBlock *BB = curInst->getParent();
564  // Visit all successors of BB in the CFG
565  for (succ_const_iterator it = succ_begin(BB), ie = succ_end(BB); it != ie; ++it)
566  {
567  const Instruction* nextInst = &((*it)->front());
568  if (LLVMUtil::isIntrinsicInst(nextInst))
569  getNextInsts(nextInst, instList);
570  else
571  instList.push_back(nextInst);
572  }
573  }
574 }
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition: LLVMUtil.cpp:204
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition: LLVMUtil.cpp:551

◆ getNumOfElements()

u32_t SVF::LLVMUtil::getNumOfElements ( const Type ety)

Return size of this object based on LLVM value.

Return size of this Object

Definition at line 297 of file LLVMUtil.cpp.

298 {
299  assert(ety && "type is null?");
300  u32_t numOfFields = 1;
301  if (SVFUtil::isa<StructType, ArrayType>(ety))
302  {
303  if(Options::ModelArrays())
304  return LLVMModuleSet::getLLVMModuleSet()->getSVFType(ety)->getTypeInfo()->getNumOfFlattenElements();
305  else
306  return LLVMModuleSet::getLLVMModuleSet()->getSVFType(ety)->getTypeInfo()->getNumOfFlattenFields();
307  }
308  return numOfFields;
309 }
unsigned u32_t
Definition: CommandLine.h:18

◆ getProgEntryFunction()

const Function* SVF::LLVMUtil::getProgEntryFunction ( Module module)
inline

Get program entry function from module.

Definition at line 367 of file LLVMUtil.h.

368 {
369  for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
370  {
371  const Function *fun = &(*it);
372  if (isProgEntryFunction(fun))
373  return (fun);
374  }
375  return nullptr;
376 }

◆ getProgFunction()

const Function * SVF::LLVMUtil::getProgFunction ( const std::string funName)

Get program entry function from module.

Definition at line 39 of file LLVMUtil.cpp.

40 {
41  for (const Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
42  {
43  for (const Function& fun : M)
44  {
45  if (fun.getName() == funName)
46  return &fun;
47  }
48  }
49  return nullptr;
50 }
llvm::Module Module
Definition: BasicTypes.h:84

◆ getPtrElementType()

static Type* SVF::LLVMUtil::getPtrElementType ( const PointerType pty)
inlinestatic

Definition at line 96 of file LLVMUtil.h.

97 {
98 #if (LLVM_VERSION_MAJOR < 14)
99  return pty->getPointerElementType();
100 #elif (LLVM_VERSION_MAJOR < 17)
101  assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
102  return pty->getNonOpaquePointerElementType();
103 #else
104  assert(false && "llvm version 17+ only support opaque pointers!");
105 #endif
106 }

◆ getSourceLoc()

const std::string SVF::LLVMUtil::getSourceLoc ( const Value val)

Get the meta data (line number and file name) info of a LLVM value

Definition at line 430 of file LLVMUtil.cpp.

431 {
432  if(val==nullptr) return "{ empty val }";
433 
434  std::string str;
435  std::stringstream rawstr(str);
436  rawstr << "{ ";
437 
438  if (const Instruction* inst = SVFUtil::dyn_cast<Instruction>(val))
439  {
440  if (SVFUtil::isa<AllocaInst>(inst))
441  {
442  for (llvm::DbgInfoIntrinsic *DII : FindDbgDeclareUses(const_cast<Instruction*>(inst)))
443  {
444  if (llvm::DbgDeclareInst *DDI = SVFUtil::dyn_cast<llvm::DbgDeclareInst>(DII))
445  {
446  llvm::DIVariable *DIVar = SVFUtil::cast<llvm::DIVariable>(DDI->getVariable());
447  rawstr << "\"ln\": " << DIVar->getLine() << ", \"fl\": \"" << DIVar->getFilename().str() << "\"";
448  break;
449  }
450  }
451  }
452  else if (MDNode *N = inst->getMetadata("dbg")) // Here I is an LLVM instruction
453  {
454  llvm::DILocation* Loc = SVFUtil::cast<llvm::DILocation>(N); // DILocation is in DebugInfo.h
455  unsigned Line = Loc->getLine();
456  unsigned Column = Loc->getColumn();
457  std::string File = Loc->getFilename().str();
458  //StringRef Dir = Loc.getDirectory();
459  if(File.empty() || Line == 0)
460  {
461  auto inlineLoc = Loc->getInlinedAt();
462  if(inlineLoc)
463  {
464  Line = inlineLoc->getLine();
465  Column = inlineLoc->getColumn();
466  File = inlineLoc->getFilename().str();
467  }
468  }
469  rawstr << "\"ln\": " << Line << ", \"cl\": " << Column << ", \"fl\": \"" << File << "\"";
470  }
471  }
472  else if (const Argument* argument = SVFUtil::dyn_cast<Argument>(val))
473  {
474  if (argument->getArgNo()%10 == 1)
475  rawstr << argument->getArgNo() << "st";
476  else if (argument->getArgNo()%10 == 2)
477  rawstr << argument->getArgNo() << "nd";
478  else if (argument->getArgNo()%10 == 3)
479  rawstr << argument->getArgNo() << "rd";
480  else
481  rawstr << argument->getArgNo() << "th";
482  rawstr << " arg " << argument->getParent()->getName().str() << " "
483  << getSourceLocOfFunction(argument->getParent());
484  }
485  else if (const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
486  {
487  rawstr << "Glob ";
488  NamedMDNode* CU_Nodes = gvar->getParent()->getNamedMetadata("llvm.dbg.cu");
489  if(CU_Nodes)
490  {
491  for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
492  {
493  llvm::DICompileUnit *CUNode = SVFUtil::cast<llvm::DICompileUnit>(CU_Nodes->getOperand(i));
494  for (llvm::DIGlobalVariableExpression *GV : CUNode->getGlobalVariables())
495  {
496  llvm::DIGlobalVariable * DGV = GV->getVariable();
497 
498  if(DGV->getName() == gvar->getName())
499  {
500  rawstr << "\"ln\": " << DGV->getLine() << ", \"fl\": \"" << DGV->getFilename().str() << "\"";
501  }
502 
503  }
504  }
505  }
506  }
507  else if (const Function* func = SVFUtil::dyn_cast<Function>(val))
508  {
509  rawstr << getSourceLocOfFunction(func);
510  }
511  else if (const BasicBlock* bb = SVFUtil::dyn_cast<BasicBlock>(val))
512  {
513  rawstr << "\"basic block\": " << bb->getName().str() << ", \"location\": " << getSourceLoc(bb->getFirstNonPHI());
514  }
515  else if(LLVMUtil::isConstDataOrAggData(val))
516  {
517  rawstr << "constant data";
518  }
519  else
520  {
521  rawstr << "N/A";
522  }
523  rawstr << " }";
524 
525  if(rawstr.str()=="{ }")
526  return "";
527  return rawstr.str();
528 }
const std::string getSourceLocOfFunction(const Function *F)
Definition: LLVMUtil.cpp:534
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition: LLVMUtil.h:327
llvm::NamedMDNode NamedMDNode
LLVM metadata and debug information.
Definition: BasicTypes.h:111
llvm::Argument Argument
Definition: BasicTypes.h:145
llvm::MDNode MDNode
Definition: BasicTypes.h:112

◆ getSourceLocOfFunction()

const std::string SVF::LLVMUtil::getSourceLocOfFunction ( const Function F)

Get source code line number of a function according to debug info

Definition at line 534 of file LLVMUtil.cpp.

535 {
536  std::string str;
537  std::stringstream rawstr(str);
538  /*
539  * https://reviews.llvm.org/D18074?id=50385
540  * looks like the relevant
541  */
542  if (llvm::DISubprogram *SP = F->getSubprogram())
543  {
544  if (SP->describes(F))
545  rawstr << "\"ln\": " << SP->getLine() << ", \"file\": \"" << SP->getFilename().str() << "\"";
546  }
547  return rawstr.str();
548 }
llvm::DISubprogram DISubprogram
Definition: BasicTypes.h:113

◆ isArgOfUncalledFunction()

bool SVF::LLVMUtil::isArgOfUncalledFunction ( const Value val)
inline

Return true if the argument in a function does not have a caller.

Definition at line 151 of file LLVMUtil.h.

152 {
153  return SVFUtil::isa<Argument>(val)
154  && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
155 }
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition: LLVMUtil.h:145

◆ isBinaryConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isBinaryConstantExpr ( const Value val)
inline

Definition at line 256 of file LLVMUtil.h.

257 {
258  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
259  {
260  if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
261  (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
262  return constExpr;
263  }
264  return nullptr;
265 }
llvm::ConstantExpr ConstantExpr
Definition: BasicTypes.h:120

◆ isBlackholeSym()

bool SVF::LLVMUtil::isBlackholeSym ( const Value val)
inline

Check whether this value is a black hole.

Definition at line 85 of file LLVMUtil.h.

86 {
87  return SVFUtil::isa<UndefValue>(val);
88 }

◆ isCallSite() [1/2]

bool SVF::LLVMUtil::isCallSite ( const Instruction inst)
inline

Whether an instruction is a call or invoke instruction.

Definition at line 45 of file LLVMUtil.h.

46 {
47  return SVFUtil::isa<CallBase>(inst);
48 }

◆ isCallSite() [2/2]

bool SVF::LLVMUtil::isCallSite ( const Value val)
inline

Whether an instruction is a call or invoke instruction.

Definition at line 50 of file LLVMUtil.h.

51 {
52  return SVFUtil::isa<CallBase>(val);
53 }

◆ isCastConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isCastConstantExpr ( const Value val)
inline

Definition at line 211 of file LLVMUtil.h.

212 {
213  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
214  {
215  if (constExpr->getOpcode() == Instruction::BitCast)
216  return constExpr;
217  }
218  return nullptr;
219 }

◆ isCmpConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isCmpConstantExpr ( const Value val)
inline

Definition at line 245 of file LLVMUtil.h.

246 {
247  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
248  {
249  if (constExpr->getOpcode() == Instruction::ICmp ||
250  constExpr->getOpcode() == Instruction::FCmp)
251  return constExpr;
252  }
253  return nullptr;
254 }

◆ isConstantObjSym() [1/2]

bool SVF::LLVMUtil::isConstantObjSym ( const SVFValue val)

Check whether this value points-to a constant object.

Definition at line 579 of file LLVMUtil.cpp.

580 {
581  return isConstantObjSym(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(val));
582 }
bool isConstantObjSym(const SVFValue *val)
Check whether this value points-to a constant object.
Definition: LLVMUtil.cpp:579

◆ isConstantObjSym() [2/2]

bool SVF::LLVMUtil::isConstantObjSym ( const Value val)

Check whether this value points-to a constant object.

Check whether this value points-to a constant object

Definition at line 672 of file CppUtil.cpp.

673 {
674  if (const GlobalVariable* v = SVFUtil::dyn_cast<GlobalVariable>(val))
675  {
676  if (cppUtil::isValVtbl(v))
677  return false;
678  else if (!v->hasInitializer())
679  {
680  return !v->isExternalLinkage(v->getLinkage());
681  }
682  else
683  {
684  StInfo *stInfo = LLVMModuleSet::getLLVMModuleSet()->getSVFType(v->getInitializer()->getType())->getTypeInfo();
685  const std::vector<const SVFType*> &fields = stInfo->getFlattenFieldTypes();
686  for (std::vector<const SVFType*>::const_iterator it = fields.begin(), eit = fields.end(); it != eit; ++it)
687  {
688  const SVFType* elemTy = *it;
689  assert(!SVFUtil::isa<SVFFunctionType>(elemTy) && "Initializer of a global is a function?");
690  if (SVFUtil::isa<SVFPointerType>(elemTy))
691  return false;
692  }
693 
694  return v->isConstant();
695  }
696  }
697  return LLVMUtil::isConstDataOrAggData(val);
698 }
std::vector< const SVFType * > & getFlattenFieldTypes()
Definition: SVFType.h:106
bool isValVtbl(const Value *val)
Definition: CppUtil.cpp:336

◆ isConstDataOrAggData()

bool SVF::LLVMUtil::isConstDataOrAggData ( const Value val)
inline

Return true if the value refers to constant data, e.g., i32 0.

Definition at line 327 of file LLVMUtil.h.

328 {
331 }
LLVM_NODISCARD bool isa(const Y &Val)
Definition: Casting.h:241
llvm::ConstantData ConstantData
Definition: BasicTypes.h:116
llvm::MetadataAsValue MetadataAsValue
Definition: BasicTypes.h:102
llvm::ConstantAggregate ConstantAggregate
Definition: BasicTypes.h:117
llvm::BlockAddress BlockAddress
Definition: BasicTypes.h:91

◆ isGepConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isGepConstantExpr ( const Value val)
inline

Return corresponding constant expression, otherwise return nullptr.

Definition at line 181 of file LLVMUtil.h.

182 {
183  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
184  {
185  if (constExpr->getOpcode() == Instruction::GetElementPtr)
186  return constExpr;
187  }
188  return nullptr;
189 }

◆ isHeapAllocExtCall()

bool SVF::LLVMUtil::isHeapAllocExtCall ( const Instruction inst)
inline

Definition at line 358 of file LLVMUtil.h.

359 {
361 }
bool isHeapAllocExtCallViaRet(const Instruction *inst)
Definition: LLVMUtil.cpp:618
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition: LLVMUtil.cpp:634

◆ isHeapAllocExtCallViaArg()

bool SVF::LLVMUtil::isHeapAllocExtCallViaArg ( const Instruction inst)

Definition at line 634 of file LLVMUtil.cpp.

635 {
636  if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
637  {
638  const Function* fun = call->getCalledFunction();
639  return fun &&
640  ExtAPI::getExtAPI()->is_arg_alloc(
641  LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(fun));
642  }
643  else
644  {
645  return false;
646  }
647 }

◆ isHeapAllocExtCallViaRet()

bool SVF::LLVMUtil::isHeapAllocExtCallViaRet ( const Instruction inst)

Definition at line 618 of file LLVMUtil.cpp.

619 {
620  LLVMModuleSet* pSet = LLVMModuleSet::getLLVMModuleSet();
621  ExtAPI* extApi = ExtAPI::getExtAPI();
622  bool isPtrTy = inst->getType()->isPointerTy();
623  if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
624  {
625  const Function* fun = call->getCalledFunction();
626  return fun && isPtrTy &&
627  (extApi->is_alloc(pSet->getSVFFunction(fun)) ||
628  extApi->is_realloc(pSet->getSVFFunction(fun)));
629  }
630  else
631  return false;
632 }
bool is_alloc(const SVFFunction *F)
Definition: ExtAPI.cpp:217
bool is_realloc(const SVFFunction *F)
Definition: ExtAPI.cpp:245
SVFFunction * getSVFFunction(const Function *fun) const
Definition: LLVMModule.h:230

◆ isInt2PtrConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isInt2PtrConstantExpr ( const Value val)
inline

Definition at line 191 of file LLVMUtil.h.

192 {
193  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
194  {
195  if (constExpr->getOpcode() == Instruction::IntToPtr)
196  return constExpr;
197  }
198  return nullptr;
199 }

◆ isIntrinsicFun()

bool SVF::LLVMUtil::isIntrinsicFun ( const Function func)

Definition at line 191 of file LLVMUtil.cpp.

192 {
193  if (func && (func->getIntrinsicID() == llvm::Intrinsic::donothing ||
194  func->getIntrinsicID() == llvm::Intrinsic::dbg_declare ||
195  func->getIntrinsicID() == llvm::Intrinsic::dbg_label ||
196  func->getIntrinsicID() == llvm::Intrinsic::dbg_value))
197  {
198  return true;
199  }
200  return false;
201 }

◆ isIntrinsicInst()

bool SVF::LLVMUtil::isIntrinsicInst ( const Instruction inst)

Return true if it is an intrinsic instruction.

Definition at line 204 of file LLVMUtil.cpp.

205 {
206  if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
207  {
208  const Function* func = call->getCalledFunction();
209  if (isIntrinsicFun(func))
210  {
211  return true;
212  }
213  }
214  return false;
215 }
bool isIntrinsicFun(const Function *func)
Definition: LLVMUtil.cpp:191

◆ isIRFile()

bool SVF::LLVMUtil::isIRFile ( const std::string filename)

Check whether a file is an LLVM IR file.

Definition at line 316 of file LLVMUtil.cpp.

317 {
318  llvm::LLVMContext context;
319  llvm::SMDiagnostic err;
320 
321  // Parse the input LLVM IR file into a module
322  std::unique_ptr<llvm::Module> module = llvm::parseIRFile(filename, err, context);
323 
324  // Check if the parsing succeeded
325  if (!module)
326  {
327  err.print("isIRFile", llvm::errs());
328  return false; // Not an LLVM IR file
329  }
330 
331  return true; // It is an LLVM IR file
332 }
std::ostream & errs()
Overwrite llvm::errs()
Definition: SVFUtil.h:56
llvm::SMDiagnostic SMDiagnostic
Definition: BasicTypes.h:90
llvm::LLVMContext LLVMContext
Definition: BasicTypes.h:70

◆ isNoCallerFunction()

bool SVF::LLVMUtil::isNoCallerFunction ( const Function fun)
inline

Function does not have any possible caller in the call graph.

Return true if the function does not have a caller (either it is a main function or a dead function)

Definition at line 145 of file LLVMUtil.h.

146 {
148 }

◆ isNonInstricCallSite()

bool SVF::LLVMUtil::isNonInstricCallSite ( const Instruction inst)

Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.

Definition at line 649 of file LLVMUtil.cpp.

650 {
651  bool res = false;
652 
653  if(isIntrinsicInst(inst))
654  res = false;
655  else
656  res = isCallSite(inst);
657  return res;
658 }
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition: LLVMUtil.h:45

◆ isNoPrecessorBasicBlock()

bool SVF::LLVMUtil::isNoPrecessorBasicBlock ( const BasicBlock bb)
inline

Basic block does not have predecessors map-1.cpp.bc try.cont: ; No predecessors! call void @llvm.trap() unreachable

Definition at line 297 of file LLVMUtil.h.

298 {
299  return bb != &bb->getParent()->getEntryBlock() &&
300  pred_empty(bb);
301 }

◆ isNullPtrSym()

bool SVF::LLVMUtil::isNullPtrSym ( const Value val)
inline

Check whether this value is a black hole.

Definition at line 91 of file LLVMUtil.h.

92 {
93  return SVFUtil::dyn_cast<ConstantPointerNull>(val);
94 }

◆ isObject()

bool SVF::LLVMUtil::isObject ( const Value ref)

Return true if this value refers to a object.

A value represents an object if it is 1) function, 2) global 3) stack 4) heap

Definition at line 59 of file LLVMUtil.cpp.

60 {
61  if (SVFUtil::isa<Instruction>(ref) && isHeapAllocExtCallViaRet(SVFUtil::cast<Instruction>(ref)))
62  return true;
63  if (SVFUtil::isa<GlobalVariable>(ref))
64  return true;
65  if (SVFUtil::isa<Function, AllocaInst>(ref))
66  return true;
67 
68  return false;
69 }

◆ isProgEntryFunction()

bool SVF::LLVMUtil::isProgEntryFunction ( const Function fun)
inline

Check whether a function is an entry function (i.e., main)

Definition at line 79 of file LLVMUtil.h.

80 {
81  return fun && fun->getName() == "main";
82 }

◆ isPtr2IntConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isPtr2IntConstantExpr ( const Value val)
inline

Definition at line 201 of file LLVMUtil.h.

202 {
203  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
204  {
205  if (constExpr->getOpcode() == Instruction::PtrToInt)
206  return constExpr;
207  }
208  return nullptr;
209 }

◆ isPtrInUncalledFunction()

bool SVF::LLVMUtil::isPtrInUncalledFunction ( const Value value)

Return true if this is value in a dead function (function without any caller)

Return true if this is a value in a dead function (function without any caller)

Definition at line 176 of file LLVMUtil.cpp.

177 {
178  if(const Instruction* inst = SVFUtil::dyn_cast<Instruction>(value))
179  {
180  if(isUncalledFunction(inst->getParent()->getParent()))
181  return true;
182  }
183  else if(const Argument* arg = SVFUtil::dyn_cast<Argument>(value))
184  {
185  if(isUncalledFunction(arg->getParent()))
186  return true;
187  }
188  return false;
189 }

◆ isSelectConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isSelectConstantExpr ( const Value val)
inline

Definition at line 221 of file LLVMUtil.h.

222 {
223  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
224  {
225  if (constExpr->getOpcode() == Instruction::Select)
226  return constExpr;
227  }
228  return nullptr;
229 }

◆ isTruncConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isTruncConstantExpr ( const Value val)
inline

Definition at line 231 of file LLVMUtil.h.

232 {
233  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
234  {
235  if (constExpr->getOpcode() == Instruction::Trunc ||
236  constExpr->getOpcode() == Instruction::FPTrunc ||
237  constExpr->getOpcode() == Instruction::ZExt ||
238  constExpr->getOpcode() == Instruction::SExt ||
239  constExpr->getOpcode() == Instruction::FPExt)
240  return constExpr;
241  }
242  return nullptr;
243 }

◆ isUnaryConstantExpr()

const ConstantExpr* SVF::LLVMUtil::isUnaryConstantExpr ( const Value val)
inline

Definition at line 267 of file LLVMUtil.h.

268 {
269  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
270  {
271  if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
272  (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
273  return constExpr;
274  }
275  return nullptr;
276 }

◆ isUncalledFunction()

bool SVF::LLVMUtil::isUncalledFunction ( const Function fun)

whether this is a function without any possible caller?

Method for dead function, which does not have any possible caller function address is not taken and never be used in call or invoke instruction

Return true if this is a function without any possible caller

Definition at line 159 of file LLVMUtil.cpp.

160 {
161  if(fun->hasAddressTaken())
162  return false;
164  return false;
165  for (Value::const_user_iterator i = fun->user_begin(), e = fun->user_end(); i != e; ++i)
166  {
167  if (LLVMUtil::isCallSite(*i))
168  return false;
169  }
170  return true;
171 }

◆ processArguments()

void SVF::LLVMUtil::processArguments ( int  argc,
char **  argv,
int &  arg_num,
char **  arg_value,
std::vector< std::string > &  moduleNameVec 
)

Parse argument for multi-module analysis.

Get the names of all modules into a vector And process arguments

Definition at line 337 of file LLVMUtil.cpp.

339 {
340  bool first_ir_file = true;
341  for (int i = 0; i < argc; ++i)
342  {
343  std::string argument(argv[i]);
344  if (LLVMUtil::isIRFile(argument))
345  {
346  if (find(moduleNameVec.begin(), moduleNameVec.end(), argument)
347  == moduleNameVec.end())
348  moduleNameVec.push_back(argument);
349  if (first_ir_file)
350  {
351  arg_value[arg_num] = argv[i];
352  arg_num++;
353  first_ir_file = false;
354  }
355  }
356  else
357  {
358  arg_value[arg_num] = argv[i];
359  arg_num++;
360  }
361  }
362 }
bool isIRFile(const std::string &filename)
Check whether a file is an LLVM IR file.
Definition: LLVMUtil.cpp:316

◆ restoreFuncName()

std::string SVF::LLVMUtil::restoreFuncName ( std::string  funcName)

Definition at line 384 of file LLVMUtil.cpp.

385 {
386  assert(!funcName.empty() && "Empty function name");
387  // Some function names change due to mangling, such as "fopen" to "\01_fopen" on macOS.
388  // Since C function names cannot include '.', change the function name from llvm.memcpy.p0i8.p0i8.i64 to llvm_memcpy_p0i8_p0i8_i64."
389  bool hasSpecialPrefix = funcName[0] == '\01';
390  bool hasDot = funcName.find('.') != std::string::npos;
391 
392  if (!hasDot && !hasSpecialPrefix)
393  return funcName;
394 
395  // Remove prefix "\01_" or "\01"
396  if (hasSpecialPrefix)
397  {
398  const std::string prefix1 = "\01_";
399  const std::string prefix2 = "\01";
400  if (funcName.substr(0, prefix1.length()) == prefix1)
401  funcName = funcName.substr(prefix1.length());
402  else if (funcName.substr(0, prefix2.length()) == prefix2)
403  funcName = funcName.substr(prefix2.length());
404  }
405  // Replace '.' with '_'
406  if (hasDot)
407  std::replace(funcName.begin(), funcName.end(), '.', '_');
408 
409  return funcName;
410 }

◆ stripAllCasts()

const Value * SVF::LLVMUtil::stripAllCasts ( const Value val)

Strip off the all casts.

Strip all casts

Definition at line 251 of file LLVMUtil.cpp.

252 {
253  while (true)
254  {
255  if (const CastInst *ci = SVFUtil::dyn_cast<CastInst>(val))
256  {
257  val = ci->getOperand(0);
258  }
259  else if (const ConstantExpr *ce = SVFUtil::dyn_cast<ConstantExpr>(val))
260  {
261  if(ce->isCast())
262  val = ce->getOperand(0);
263  else
264  return val;
265  }
266  else
267  {
268  return val;
269  }
270  }
271  return nullptr;
272 }
llvm::CastInst CastInst
Definition: BasicTypes.h:158

◆ stripConstantCasts()

const Value * SVF::LLVMUtil::stripConstantCasts ( const Value val)

Strip off the constant casts.

Strip constant casts

Definition at line 220 of file LLVMUtil.cpp.

221 {
222  if (SVFUtil::isa<GlobalValue>(val) || isInt2PtrConstantExpr(val))
223  return val;
224  else if (const ConstantExpr *CE = SVFUtil::dyn_cast<ConstantExpr>(val))
225  {
226  if (Instruction::isCast(CE->getOpcode()))
227  return stripConstantCasts(CE->getOperand(0));
228  }
229  return val;
230 }
const Value * stripConstantCasts(const Value *val)
Strip off the constant casts.
Definition: LLVMUtil.cpp:220
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
Definition: LLVMUtil.h:191

◆ viewCFG()

void SVF::LLVMUtil::viewCFG ( const Function fun)

Definition at line 232 of file LLVMUtil.cpp.

233 {
234  if (fun != nullptr)
235  {
236  fun->viewCFG();
237  }
238 }

◆ viewCFGOnly()

void SVF::LLVMUtil::viewCFGOnly ( const Function fun)

Definition at line 240 of file LLVMUtil.cpp.

241 {
242  if (fun != nullptr)
243  {
244  fun->viewCFGOnly();
245  }
246 }