Static Value-Flow Analysis
Loading...
Searching...
No Matches
Functions
SVF::LLVMUtil Namespace Reference

Functions

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

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:158
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

◆ 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) &&
133 LLVMUtil::isProgEntryFunction(
134 SVFUtil::cast<Argument>(val)->getParent());
135}

◆ 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 108 of file LLVMUtil.cpp.

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

◆ dumpType()

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

Definition at line 595 of file LLVMUtil.cpp.

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

◆ dumpValue()

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

Definition at line 584 of file LLVMUtil.cpp.

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

◆ dumpValueAndDbgInfo()

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

Definition at line 606 of file LLVMUtil.cpp.

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

◆ functionDoesNotRet()

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

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

Definition at line 122 of file LLVMUtil.cpp.

123{
126 {
127 return fun->getReturnType()->isVoidTy();
128 }
129 std::vector<const BasicBlock*> bbVec;
131 bbVec.push_back(&fun->getEntryBlock());
132 while(!bbVec.empty())
133 {
134 const BasicBlock* bb = bbVec.back();
135 bbVec.pop_back();
136 if (basicBlockHasRetInst(bb))
137 {
138 return false;
139 }
140
142 sit != esit; ++sit)
143 {
144 const BasicBlock* succbb = (*sit);
145 if(visited.find(succbb)==visited.end())
146 visited.insert(succbb);
147 else
148 continue;
149 bbVec.push_back(succbb);
150 }
151 }
152 return true;
153}
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:122
SVFFunction * getSVFFunction(const Function *fun) const
Definition LLVMModule.h:260
const SVFType * getReturnType() const
Returns the FunctionType.
Definition SVFValue.h:399
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

◆ getCalledFunctions()

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

Get all called funcions in a parent function.

Definition at line 364 of file LLVMUtil.cpp.

365{
366 std::vector<const Function *> calledFunctions;
367 for (const Instruction &I : instructions(F))
368 {
369 if (const CallBase *callInst = SVFUtil::dyn_cast<CallBase>(&I))
370 {
371 Function *calledFunction = callInst->getCalledFunction();
372 if (calledFunction)
373 {
375 std::vector<const Function *> nestedCalledFunctions = getCalledFunctions(calledFunction);
377 }
378 }
379 }
380 return calledFunctions;
381}
#define F(f)
std::vector< const Function * > getCalledFunctions(const Function *F)
Get all called funcions in a parent function.
Definition LLVMUtil.cpp:364
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 278 of file LLVMUtil.cpp.

279{
280 assert(SVFUtil::isa<PointerType>(val->getType()) && "this value should be a pointer type!");
282 const Value *latestUse = nullptr;
283 for (const auto &it : val->uses())
284 {
285 if (SVFUtil::isa<BitCastInst>(it.getUser()))
286 latestUse = it.getUser();
287 else
288 latestUse = nullptr;
289 }
290 return latestUse;
291}
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 411 of file LLVMUtil.cpp.

412{
414}
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
79
81 std::vector<const BasicBlock*> bbVec;
82 bbVec.push_back(&fun->getEntryBlock());
83 while(!bbVec.empty())
84 {
85 const BasicBlock* bb = bbVec.back();
86 bbVec.pop_back();
88 reachableBBs.push_back(svfbb);
89 if(DomTreeNode *dtNode = dt.getNode(const_cast<BasicBlock*>(bb)))
90 {
91 for (DomTreeNode::iterator DI = dtNode->begin(), DE = dtNode->end();
92 DI != DE; ++DI)
93 {
94 const BasicBlock* succbb = (*DI)->getBlock();
95 if(visited.find(succbb)==visited.end())
96 visited.insert(succbb);
97 else
98 continue;
99 bbVec.push_back(succbb);
100 }
101 }
102 }
103}
DominatorTree & getDomTree(const Function *fun)
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb) const
Definition LLVMModule.h:267
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 416 of file LLVMUtil.cpp.

417{
418 if (const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
419 {
420 if (LLVMModuleSet::getLLVMModuleSet()->hasGlobalRep(gvar))
422 }
423 return val;
424}
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
Definition LLVMModule.h:357
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 Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition LLVMUtil.h:45

◆ 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 550 of file LLVMUtil.cpp.

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

◆ 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 296 of file LLVMUtil.cpp.

297{
298 assert(ety && "type is null?");
299 u32_t numOfFields = 1;
300 if (SVFUtil::isa<StructType, ArrayType>(ety))
301 {
304 else
306 }
307 return numOfFields;
308}
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
static const Option< bool > ModelArrays
Definition Options.h:188
StInfo * getTypeInfo()
Definition SVFType.h:230
u32_t getNumOfFlattenElements() const
Return number of elements after flattening (including array elements)
Definition SVFType.h:139
u32_t getNumOfFlattenFields() const
Return the number of fields after flattening (ignoring array elements)
Definition SVFType.h:145
unsigned u32_t
Definition GeneralType.h:46

◆ getProgEntryFunction()

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

Get program entry function from module.

Definition at line 380 of file LLVMUtil.h.

381{
382 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
383 {
384 const Function *fun = &(*it);
385 if (isProgEntryFunction(fun))
386 return (fun);
387 }
388 return nullptr;
389}
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main)
Definition LLVMUtil.h:79

◆ 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 429 of file LLVMUtil.cpp.

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

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

◆ 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 578 of file LLVMUtil.cpp.

579{
581}
bool isConstantObjSym(const SVFValue *val)
Check whether this value points-to a constant object.
Definition LLVMUtil.cpp:578

◆ 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 {
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 }
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{
329 return SVFUtil::isa<ConstantData, ConstantAggregate,
331}
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:617
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:633

◆ isHeapAllocExtCallViaArg()

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

Definition at line 633 of file LLVMUtil.cpp.

634{
635 if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
636 {
637 const Function* fun = call->getCalledFunction();
638 return fun &&
640 LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(fun));
641 }
642 else
643 {
644 return false;
645 }
646}
bool is_arg_alloc(const SVFFunction *F)
Definition ExtAPI.cpp:223
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:42

◆ isHeapAllocExtCallViaRet()

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

Definition at line 617 of file LLVMUtil.cpp.

618{
621 bool isPtrTy = inst->getType()->isPointerTy();
622 if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
623 {
624 const Function* fun = call->getCalledFunction();
625 return fun && isPtrTy &&
626 (extApi->is_alloc(pSet->getSVFFunction(fun)) ||
627 extApi->is_realloc(pSet->getSVFFunction(fun)));
628 }
629 else
630 return false;
631}

◆ isHeapObj()

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

Check if a given value represents a heap object.

Parameters
valThe value to check.
Returns
True if the value represents a heap object, false otherwise.

Definition at line 669 of file LLVMUtil.cpp.

670{
671 // Check if the value is an argument in the program entry function
673 {
674 // Return true if the value does not have a first use via cast instruction
676 }
677 // Check if the value is an instruction and if it is a heap allocation external call
678 else if (SVFUtil::isa<Instruction>(val) &&
679 LLVMUtil::isHeapAllocExtCall(SVFUtil::cast<Instruction>(val)))
680 {
681 return true;
682 }
683 // Return false if none of the above conditions are met
684 return false;
685}
const Value * getFirstUseViaCastInst(const Value *val)
Definition LLVMUtil.cpp:278
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:358
bool ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main)
Definition LLVMUtil.h:130

◆ 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 190 of file LLVMUtil.cpp.

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

◆ isIntrinsicInst()

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

Return true if it is an intrinsic instruction.

Definition at line 203 of file LLVMUtil.cpp.

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

◆ isIRFile()

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

Check whether a file is an LLVM IR file.

Definition at line 315 of file LLVMUtil.cpp.

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

◆ 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{
147 return isUncalledFunction(fun) || LLVMUtil::isProgEntryFunction(fun);
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 707 of file LLVMUtil.cpp.

708{
709 bool res = false;
710
711 if(isIntrinsicInst(inst))
712 res = false;
713 else
714 res = isCallSite(inst);
715 return res;
716}

◆ 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 175 of file LLVMUtil.cpp.

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

◆ 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}

◆ isStackAllocExtCall()

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

Definition at line 365 of file LLVMUtil.h.

366{
367 return isStackAllocExtCallViaRet(inst);
368}
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:648

◆ isStackAllocExtCallViaRet()

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

Definition at line 648 of file LLVMUtil.cpp.

649{
652 bool isPtrTy = inst->getType()->isPointerTy();
653 if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
654 {
655 const Function* fun = call->getCalledFunction();
656 return fun && isPtrTy &&
657 extApi->is_alloc_stack_ret(pSet->getSVFFunction(fun));
658 }
659 else
660 return false;
661}

◆ isStackObj()

bool SVF::LLVMUtil::isStackObj ( const Value val)
Parameters
valThe value to check.
Returns
True if the value represents a stack object, false otherwise.

Definition at line 691 of file LLVMUtil.cpp.

692{
693 if (SVFUtil::isa<AllocaInst>(val))
694 {
695 return true;
696 }
697 // Check if the value is an instruction and if it is a stack allocation external call
698 else if (SVFUtil::isa<Instruction>(val) &&
699 LLVMUtil::isStackAllocExtCall(SVFUtil::cast<Instruction>(val)))
700 {
701 return true;
702 }
703 // Return false if none of the above conditions are met
704 return false;
705}
bool isStackAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:365

◆ 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 158 of file LLVMUtil.cpp.

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

◆ 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 336 of file LLVMUtil.cpp.

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

◆ restoreFuncName()

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

Definition at line 383 of file LLVMUtil.cpp.

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

◆ stripAllCasts()

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

Strip off the all casts.

Strip all casts

Definition at line 250 of file LLVMUtil.cpp.

251{
252 while (true)
253 {
254 if (const CastInst *ci = SVFUtil::dyn_cast<CastInst>(val))
255 {
256 val = ci->getOperand(0);
257 }
258 else if (const ConstantExpr *ce = SVFUtil::dyn_cast<ConstantExpr>(val))
259 {
260 if(ce->isCast())
261 val = ce->getOperand(0);
262 else
263 return val;
264 }
265 else
266 {
267 return val;
268 }
269 }
270 return nullptr;
271}
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 219 of file LLVMUtil.cpp.

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

◆ viewCFG()

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

Definition at line 231 of file LLVMUtil.cpp.

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

◆ viewCFGOnly()

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

Definition at line 239 of file LLVMUtil.cpp.

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