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.
 
double getDoubleValue (const ConstantFP *fpValue)
 
std::pair< s64_t, u64_tgetIntegerValue (const ConstantInt *intValue)
 
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)
 
bool isExtCall (const Function *fun)
 
bool isMemcpyExtFun (const Function *fun)
 
bool isMemsetExtFun (const Function *fun)
 
u32_t getHeapAllocHoldingArgPosition (const Function *fun)
 
const FunObjVargetFunObjVar (const std::string &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 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 158 of file LLVMUtil.h.

159{
160 return SVFUtil::isa<Argument>(val)
161 && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
162}
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
Definition LLVMUtil.cpp:158
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76

◆ 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 166 of file LLVMUtil.h.

167{
168 return SVFUtil::isa<Argument>(val) &&
169 LLVMUtil::isProgEntryFunction(
170 SVFUtil::cast<Argument>(val)->getParent());
171}

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

617{
618 std::string str;
619 llvm::raw_string_ostream rawstr(str);
620 if (type)
621 rawstr << " " << *type << " ";
622 else
623 rawstr << " llvm type is null";
624 return rawstr.str();
625}
newitem type
Definition cJSON.cpp:2739

◆ dumpValue()

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

Definition at line 605 of file LLVMUtil.cpp.

606{
607 std::string str;
608 llvm::raw_string_ostream rawstr(str);
609 if (val)
610 rawstr << " " << *val << " ";
611 else
612 rawstr << " llvm Value is null";
613 return rawstr.str();
614}

◆ dumpValueAndDbgInfo()

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

Definition at line 627 of file LLVMUtil.cpp.

628{
629 std::string str;
630 llvm::raw_string_ostream rawstr(str);
631 if (val)
633 else
634 rawstr << " llvm Value is null";
635 return rawstr.str();
636}
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:453
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:605

◆ 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 if (LLVMUtil::isExtCall(fun))
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}
bool isExtCall(const Function *fun)
Definition LLVMUtil.cpp:384
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:90
llvm::succ_const_iterator succ_const_iterator
LLVM Iterators.
Definition BasicTypes.h:287

◆ 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}
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:153
llvm::Function Function
Definition BasicTypes.h:89
llvm::Instruction Instruction
Definition BasicTypes.h:91

◆ getCallee()

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

Definition at line 98 of file LLVMUtil.h.

99{
100 // FIXME: do we need to strip-off the casts here to discover more library functions
101 return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
102}

◆ getDataLayout()

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

Definition at line 315 of file LLVMUtil.h.

316{
317 static DataLayout *dl = nullptr;
318 if (dl == nullptr)
319#if LLVM_VERSION_MAJOR >= 19
320 dl = new DataLayout(mod->getDataLayout());
321#else
322 dl = new DataLayout(mod);
323#endif
324 return dl;
325}
llvm::DataLayout DataLayout
Definition BasicTypes.h:112

◆ getDoubleValue()

double SVF::LLVMUtil::getDoubleValue ( const ConstantFP fpValue)
inline

Definition at line 55 of file LLVMUtil.h.

56{
57 double dval = 0;
58 if (fpValue->isNormalFP())
59 {
60 const llvm::fltSemantics& semantics = fpValue->getValueAPF().getSemantics();
61 if (&semantics == &llvm::APFloat::IEEEhalf() ||
62 &semantics == &llvm::APFloat::IEEEsingle() ||
63 &semantics == &llvm::APFloat::IEEEdouble() ||
64 &semantics == &llvm::APFloat::IEEEquad() ||
65 &semantics == &llvm::APFloat::x87DoubleExtended())
66 {
67 dval = fpValue->getValueAPF().convertToDouble();
68 }
69 else
70 {
71 assert (false && "Unsupported floating point type");
72 abort();
73 }
74 }
75 else
76 {
77 // other cfp type, like isZero(), isInfinity(), isNegative(), etc.
78 // do nothing
79 }
80 return dval;
81}

◆ 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:86

◆ getFunObjVar()

const FunObjVar * SVF::LLVMUtil::getFunObjVar ( const std::string &  name)

Definition at line 436 of file LLVMUtil.cpp.

437{
439}
const char *const name
Definition cJSON.h:264
const FunObjVar * getFunObjVar(const Function *fun) const
Definition LLVMModule.h:267
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:131

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

76{
77 assert(!LLVMUtil::isExtCall(fun) && "The calling function cannot be an external function.");
78 //initial DominatorTree
80
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();
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}
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb)
Definition LLVMModule.h:298
DominatorTree & getDomTree(const Function *fun)
llvm::DomTreeNode DomTreeNode
Definition BasicTypes.h:141
llvm::DominatorTree DominatorTree
LLVM Dominators.
Definition BasicTypes.h:140

◆ getGlobalRep()

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

find the unique defined global across multiple modules

Definition at line 440 of file LLVMUtil.cpp.

441{
442 if (const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
443 {
444 if (LLVMModuleSet::getLLVMModuleSet()->hasGlobalRep(gvar))
446 }
447 return val;
448}
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
Definition LLVMModule.h:359
llvm::GlobalVariable GlobalVariable
Definition BasicTypes.h:137

◆ getHeapAllocHoldingArgPosition()

u32_t SVF::LLVMUtil::getHeapAllocHoldingArgPosition ( const Function fun)

Definition at line 401 of file LLVMUtil.cpp.

402{
404}
s32_t get_alloc_arg_pos(const Function *F)

◆ getIntegerValue()

std::pair< s64_t, u64_t > SVF::LLVMUtil::getIntegerValue ( const ConstantInt intValue)
inline

Definition at line 83 of file LLVMUtil.h.

84{
85 if (intValue->getBitWidth() <= 64 && intValue->getBitWidth() >= 1)
86 return std::make_pair(intValue->getSExtValue(), intValue->getZExtValue());
87 else
88 return std::make_pair(0,0);
89}

◆ getLLVMCallSite()

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

Return LLVM callsite given a value.

Definition at line 92 of file LLVMUtil.h.

93{
94 assert(isCallSite(value) && "not a callsite?");
95 return SVFUtil::cast<CallBase>(value);
96}
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 105 of file LLVMUtil.h.

106{
107 return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
108}

◆ getNextInsts()

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

Get the next instructions following control flow.

Definition at line 578 of file LLVMUtil.cpp.

579{
580 if (!curInst->isTerminator())
581 {
582 const Instruction* nextInst = curInst->getNextNode();
585 else
586 instList.push_back(nextInst);
587 }
588 else
589 {
590 const BasicBlock *BB = curInst->getParent();
591 // Visit all successors of BB in the CFG
592 for (succ_const_iterator it = succ_begin(BB), ie = succ_end(BB); it != ie; ++it)
593 {
594 const Instruction* nextInst = &((*it)->front());
596 getNextInsts(nextInst, instList);
597 else
598 instList.push_back(nextInst);
599 }
600 }
601}
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:578

◆ 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 Option< bool > ModelArrays
Definition Options.h:185
StInfo * getTypeInfo()
Definition SVFType.h:275
u32_t getNumOfFlattenElements() const
Return number of elements after flattening (including array elements)
Definition SVFType.h:166
u32_t getNumOfFlattenFields() const
Return the number of fields after flattening (ignoring array elements)
Definition SVFType.h:172
unsigned u32_t
Definition GeneralType.h:47

◆ getProgEntryFunction()

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

Get program entry function from module.

Definition at line 424 of file LLVMUtil.h.

425{
426 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
427 {
428 const Function *fun = &(*it);
429 if (isProgEntryFunction(fun))
430 return (fun);
431 }
432 return nullptr;
433}
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main)
Definition LLVMUtil.h:114

◆ getProgFunction()

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

Get program entry function from module.

Definition at line 40 of file LLVMUtil.cpp.

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

◆ getPtrElementType()

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

Definition at line 132 of file LLVMUtil.h.

133{
134#if (LLVM_VERSION_MAJOR < 14)
135 return pty->getPointerElementType();
136#elif (LLVM_VERSION_MAJOR < 17)
137 assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
138 return pty->getNonOpaquePointerElementType();
139#else
140 assert(false && "llvm version 17+ only support opaque pointers!");
141#endif
142}

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

454{
455 if(val==nullptr) return "{ empty val }";
456
457 std::string str;
458 std::stringstream rawstr(str);
459 rawstr << "{ ";
460
461 if (const Instruction* inst = SVFUtil::dyn_cast<Instruction>(val))
462 {
463 if (SVFUtil::isa<AllocaInst>(inst))
464 {
465#if LLVM_VERSION_MAJOR > 16
466 for (llvm::DbgInfoIntrinsic *DII : llvm::findDbgDeclares(const_cast<Instruction*>(inst)))
467#else
469#endif
470 {
471 if (llvm::DbgDeclareInst *DDI = SVFUtil::dyn_cast<llvm::DbgDeclareInst>(DII))
472 {
473 llvm::DIVariable *DIVar = SVFUtil::cast<llvm::DIVariable>(DDI->getVariable());
474 rawstr << "\"ln\": " << DIVar->getLine() << ", \"fl\": \"" << DIVar->getFilename().str() << "\"";
475 break;
476 }
477 }
478 }
479 else if (MDNode *N = inst->getMetadata("dbg")) // Here I is an LLVM instruction
480 {
481 llvm::DILocation* Loc = SVFUtil::cast<llvm::DILocation>(N); // DILocation is in DebugInfo.h
482 unsigned Line = Loc->getLine();
483 unsigned Column = Loc->getColumn();
484 std::string File = Loc->getFilename().str();
485 //StringRef Dir = Loc.getDirectory();
486 if(File.empty() || Line == 0)
487 {
488 auto inlineLoc = Loc->getInlinedAt();
489 if(inlineLoc)
490 {
491 Line = inlineLoc->getLine();
492 Column = inlineLoc->getColumn();
493 File = inlineLoc->getFilename().str();
494 }
495 }
496 rawstr << "\"ln\": " << Line << ", \"cl\": " << Column << ", \"fl\": \"" << File << "\"";
497 }
498 }
499 else if (const Argument* argument = SVFUtil::dyn_cast<Argument>(val))
500 {
501 if (argument->getArgNo()%10 == 1)
502 rawstr << argument->getArgNo() << "st";
503 else if (argument->getArgNo()%10 == 2)
504 rawstr << argument->getArgNo() << "nd";
505 else if (argument->getArgNo()%10 == 3)
506 rawstr << argument->getArgNo() << "rd";
507 else
508 rawstr << argument->getArgNo() << "th";
509 rawstr << " arg " << argument->getParent()->getName().str() << " "
510 << getSourceLocOfFunction(argument->getParent());
511 }
512 else if (const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
513 {
514 rawstr << "Glob ";
515 NamedMDNode* CU_Nodes = gvar->getParent()->getNamedMetadata("llvm.dbg.cu");
516 if(CU_Nodes)
517 {
518 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
519 {
520 llvm::DICompileUnit *CUNode = SVFUtil::cast<llvm::DICompileUnit>(CU_Nodes->getOperand(i));
521 for (llvm::DIGlobalVariableExpression *GV : CUNode->getGlobalVariables())
522 {
523 llvm::DIGlobalVariable * DGV = GV->getVariable();
524
525 if(DGV->getName() == gvar->getName())
526 {
527 rawstr << "\"ln\": " << DGV->getLine() << ", \"fl\": \"" << DGV->getFilename().str() << "\"";
528 }
529
530 }
531 }
532 }
533 }
534 else if (const Function* func = SVFUtil::dyn_cast<Function>(val))
535 {
537 }
538 else if (const BasicBlock* bb = SVFUtil::dyn_cast<BasicBlock>(val))
539 {
540 rawstr << "\"basic block\": " << bb->getName().str() << ", \"location\": " << getSourceLoc(bb->getFirstNonPHI());
541 }
543 {
544 rawstr << "constant data";
545 }
546 else
547 {
548 rawstr << "N/A";
549 }
550 rawstr << " }";
551
552 if(rawstr.str()=="{ }")
553 return "";
554 return rawstr.str();
555}
else
Definition cJSON.cpp:2292
for(i=0;a &&(i<(size_t) count);i++)
Definition cJSON.cpp:2569
const std::string getSourceLocOfFunction(const Function *F)
Definition LLVMUtil.cpp:561
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition LLVMUtil.h:374
llvm::NamedMDNode NamedMDNode
LLVM metadata and debug information.
Definition BasicTypes.h:115
llvm::Argument Argument
Definition BasicTypes.h:152
llvm::MDNode MDNode
Definition BasicTypes.h:116

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

562{
563 std::string str;
564 std::stringstream rawstr(str);
565 /*
566 * https://reviews.llvm.org/D18074?id=50385
567 * looks like the relevant
568 */
569 if (llvm::DISubprogram *SP = F->getSubprogram())
570 {
571 if (SP->describes(F))
572 rawstr << "\"ln\": " << SP->getLine() << ", \"file\": \"" << SP->getFilename().str() << "\"";
573 }
574 return rawstr.str();
575}

◆ 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 187 of file LLVMUtil.h.

188{
189 return SVFUtil::isa<Argument>(val)
190 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
191}
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition LLVMUtil.h:181

◆ isBinaryConstantExpr()

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

Definition at line 292 of file LLVMUtil.h.

293{
294 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
295 {
296 if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
297 (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
298 return constExpr;
299 }
300 return nullptr;
301}
llvm::ConstantExpr ConstantExpr
Definition BasicTypes.h:124

◆ isBlackholeSym()

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

Check whether this value is a black hole.

Definition at line 121 of file LLVMUtil.h.

122{
123 return SVFUtil::isa<UndefValue>(val);
124}

◆ 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 247 of file LLVMUtil.h.

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

◆ isCmpConstantExpr()

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

Definition at line 281 of file LLVMUtil.h.

282{
283 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
284 {
285 if (constExpr->getOpcode() == Instruction::ICmp ||
286 constExpr->getOpcode() == Instruction::FCmp)
287 return constExpr;
288 }
289 return nullptr;
290}

◆ isConstantObjSym()

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 747 of file CppUtil.cpp.

748{
749 if (const GlobalVariable* v = SVFUtil::dyn_cast<GlobalVariable>(val))
750 {
752 return false;
753 else if (!v->hasInitializer())
754 {
755 return !v->isExternalLinkage(v->getLinkage());
756 }
757 else
758 {
759 StInfo *stInfo = LLVMModuleSet::getLLVMModuleSet()->getSVFType(v->getInitializer()->getType())->getTypeInfo();
760 const std::vector<const SVFType*> &fields = stInfo->getFlattenFieldTypes();
761 for (std::vector<const SVFType*>::const_iterator it = fields.begin(), eit = fields.end(); it != eit; ++it)
762 {
763 const SVFType* elemTy = *it;
764 assert(!SVFUtil::isa<SVFFunctionType>(elemTy) && "Initializer of a global is a function?");
765 if (SVFUtil::isa<SVFPointerType>(elemTy))
766 return false;
767 }
768
769 return v->isConstant();
770 }
771 }
773}
std::vector< const SVFType * > & getFlattenFieldTypes()
Definition SVFType.h:133
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 374 of file LLVMUtil.h.

375{
376 return SVFUtil::isa<ConstantData, ConstantAggregate,
378}
llvm::ConstantData ConstantData
Definition BasicTypes.h:120
llvm::MetadataAsValue MetadataAsValue
Definition BasicTypes.h:106
llvm::ConstantAggregate ConstantAggregate
Definition BasicTypes.h:121
llvm::BlockAddress BlockAddress
Definition BasicTypes.h:95

◆ isExtCall()

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

Definition at line 384 of file LLVMUtil.cpp.

385{
386 return fun && LLVMModuleSet::getLLVMModuleSet()->is_ext(fun);
387}
bool is_ext(const Function *F)

◆ isGepConstantExpr()

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

Return corresponding constant expression, otherwise return nullptr.

Definition at line 217 of file LLVMUtil.h.

218{
219 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
220 {
221 if (constExpr->getOpcode() == Instruction::GetElementPtr)
222 return constExpr;
223 }
224 return nullptr;
225}

◆ isHeapAllocExtCall()

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

Definition at line 402 of file LLVMUtil.h.

403{
405}
bool isHeapAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:638
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:653

◆ isHeapAllocExtCallViaArg()

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

Definition at line 653 of file LLVMUtil.cpp.

654{
655 if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
656 {
657 const Function* fun = call->getCalledFunction();
658 return fun &&
660 }
661 else
662 {
663 return false;
664 }
665}
bool is_arg_alloc(const Function *F)

◆ isHeapAllocExtCallViaRet()

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

Definition at line 638 of file LLVMUtil.cpp.

639{
641 bool isPtrTy = inst->getType()->isPointerTy();
642 if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
643 {
644 const Function* fun = call->getCalledFunction();
645 return fun && isPtrTy &&
646 (pSet->is_alloc(fun) ||
647 pSet->is_realloc(fun));
648 }
649 else
650 return false;
651}

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

688{
689 // Check if the value is an argument in the program entry function
691 {
692 // Return true if the value does not have a first use via cast instruction
694 }
695 // Check if the value is an instruction and if it is a heap allocation external call
696 else if (SVFUtil::isa<Instruction>(val) &&
697 LLVMUtil::isHeapAllocExtCall(SVFUtil::cast<Instruction>(val)))
698 {
699 return true;
700 }
701 // Return false if none of the above conditions are met
702 return false;
703}
const Value * getFirstUseViaCastInst(const Value *val)
Definition LLVMUtil.cpp:278
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:402
bool ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main)
Definition LLVMUtil.h:166

◆ isInt2PtrConstantExpr()

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

Definition at line 227 of file LLVMUtil.h.

228{
229 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
230 {
231 if (constExpr->getOpcode() == Instruction::IntToPtr)
232 return constExpr;
233 }
234 return nullptr;
235}

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

◆ isMemcpyExtFun()

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

Definition at line 389 of file LLVMUtil.cpp.

390{
391 return fun && LLVMModuleSet::getLLVMModuleSet()->is_memcpy(fun);
392}
bool is_memcpy(const Function *F)

◆ isMemsetExtFun()

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

Definition at line 395 of file LLVMUtil.cpp.

396{
397 return fun && LLVMModuleSet::getLLVMModuleSet()->is_memset(fun);
398}
bool is_memset(const Function *F)

◆ 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 181 of file LLVMUtil.h.

182{
183 return isUncalledFunction(fun) || LLVMUtil::isProgEntryFunction(fun);
184}

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

726{
727 bool res = false;
728
729 if(isIntrinsicInst(inst))
730 res = false;
731 else
732 res = isCallSite(inst);
733 return res;
734}

◆ 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 337 of file LLVMUtil.h.

338{
339 return bb != &bb->getParent()->getEntryBlock() &&
340 pred_empty(bb);
341}

◆ isNullPtrSym()

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

Check whether this value is a black hole.

Definition at line 127 of file LLVMUtil.h.

128{
129 return SVFUtil::dyn_cast<ConstantPointerNull>(val);
130}

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

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

◆ isProgEntryFunction()

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

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

Definition at line 114 of file LLVMUtil.h.

115{
116 const char* main_name=Options::SVFMain() ? "svf.main" : "main";
117 return fun && fun->getName() == main_name;
118}

◆ isPtr2IntConstantExpr()

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

Definition at line 237 of file LLVMUtil.h.

238{
239 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
240 {
241 if (constExpr->getOpcode() == Instruction::PtrToInt)
242 return constExpr;
243 }
244 return nullptr;
245}

◆ 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 257 of file LLVMUtil.h.

258{
259 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
260 {
261 if (constExpr->getOpcode() == Instruction::Select)
262 return constExpr;
263 }
264 return nullptr;
265}

◆ isStackAllocExtCall()

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

Definition at line 409 of file LLVMUtil.h.

410{
411 return isStackAllocExtCallViaRet(inst);
412}
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:667

◆ isStackAllocExtCallViaRet()

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

Definition at line 667 of file LLVMUtil.cpp.

668{
670 bool isPtrTy = inst->getType()->isPointerTy();
671 if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
672 {
673 const Function* fun = call->getCalledFunction();
674 return fun && isPtrTy &&
675 pSet->is_alloc_stack_ret(fun);
676 }
677 else
678 return false;
679}

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

710{
711 if (SVFUtil::isa<AllocaInst>(val))
712 {
713 return true;
714 }
715 // Check if the value is an instruction and if it is a stack allocation external call
716 else if (SVFUtil::isa<Instruction>(val) &&
717 LLVMUtil::isStackAllocExtCall(SVFUtil::cast<Instruction>(val)))
718 {
719 return true;
720 }
721 // Return false if none of the above conditions are met
722 return false;
723}
bool isStackAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:409

◆ isTruncConstantExpr()

const ConstantExpr * SVF::LLVMUtil::isTruncConstantExpr ( 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::Trunc ||
272 constExpr->getOpcode() == Instruction::FPTrunc ||
273 constExpr->getOpcode() == Instruction::ZExt ||
274 constExpr->getOpcode() == Instruction::SExt ||
275 constExpr->getOpcode() == Instruction::FPExt)
276 return constExpr;
277 }
278 return nullptr;
279}

◆ isUnaryConstantExpr()

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

Definition at line 303 of file LLVMUtil.h.

304{
305 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
306 {
307 if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
308 (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
309 return constExpr;
310 }
311 return nullptr;
312}

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

408{
409 assert(!funcName.empty() && "Empty function name");
410 // Some function names change due to mangling, such as "fopen" to "\01_fopen" on macOS.
411 // Since C function names cannot include '.', change the function name from llvm.memcpy.p0i8.p0i8.i64 to llvm_memcpy_p0i8_p0i8_i64."
412 bool hasSpecialPrefix = funcName[0] == '\01';
413 bool hasDot = funcName.find('.') != std::string::npos;
414
415 if (!hasDot && !hasSpecialPrefix)
416 return funcName;
417
418 // Remove prefix "\01_" or "\01"
420 {
421 const std::string prefix1 = "\01_";
422 const std::string prefix2 = "\01";
423 if (funcName.substr(0, prefix1.length()) == prefix1)
424 funcName = funcName.substr(prefix1.length());
425 else if (funcName.substr(0, prefix2.length()) == prefix2)
426 funcName = funcName.substr(prefix2.length());
427 }
428 // Replace '.' with '_'
429 if (hasDot)
430 std::replace(funcName.begin(), funcName.end(), '.', '_');
431
432 return funcName;
433}

◆ 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:165

◆ 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:227

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