34#include <llvm/IRReader/IRReader.h>
35#include <llvm/Support/raw_ostream.h>
47 if (fun.getName() == funName)
78 assert(!
LLVMUtil::isExtCall(fun) &&
"The calling function cannot be an external function.");
83 std::vector<const BasicBlock*> bbVec;
84 bbVec.push_back(&fun->getEntryBlock());
90 reachableBBs.push_back(svfbb);
93 for (DomTreeNode::iterator DI = dtNode->begin(), DE = dtNode->end();
97 if(visited.find(succbb)==visited.end())
98 visited.insert(succbb);
101 bbVec.push_back(succbb);
112 for (BasicBlock::const_iterator it = bb->begin(), eit = bb->end();
128 return fun->getReturnType()->isVoidTy();
130 std::vector<const BasicBlock*> bbVec;
132 bbVec.push_back(&fun->getEntryBlock());
133 while(!bbVec.empty())
146 if(visited.find(succbb)==visited.end())
147 visited.insert(succbb);
150 bbVec.push_back(succbb);
161 if(fun->hasAddressTaken())
165 for (Value::const_user_iterator i = fun->user_begin(), e = fun->user_end(); i != e; ++i)
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))
208 const Function* func = call->getCalledFunction();
226 if (Instruction::isCast(CE->getOpcode()))
257 val = ci->getOperand(0);
262 val = ce->getOperand(0);
283 const Value *latestUse =
nullptr;
284 for (
const auto &it : val->uses())
287 latestUse = it.getUser();
299 assert(ety &&
"type is null?");
300 u32_t numOfFields = 1;
318 llvm::LLVMContext context;
319 llvm::SMDiagnostic err;
322 std::unique_ptr<llvm::Module> module = llvm::parseIRFile(filename, err, context);
327 err.print(
"isIRFile", llvm::errs());
338 std::vector<std::string> &moduleNameVec)
340 bool first_ir_file =
true;
341 for (
int i = 0; i < argc; ++i)
343 std::string argument(argv[i]);
346 if (find(moduleNameVec.begin(), moduleNameVec.end(), argument)
347 == moduleNameVec.end())
348 moduleNameVec.push_back(argument);
351 arg_value[arg_num] = argv[i];
353 first_ir_file =
false;
358 arg_value[arg_num] = argv[i];
367 std::vector<const Function *> calledFunctions;
372 Function *calledFunction = callInst->getCalledFunction();
375 calledFunctions.push_back(calledFunction);
376 std::vector<const Function *> nestedCalledFunctions =
getCalledFunctions(calledFunction);
377 calledFunctions.insert(calledFunctions.end(), nestedCalledFunctions.begin(), nestedCalledFunctions.end());
381 return calledFunctions;
410 assert(!funcName.empty() &&
"Empty function name");
413 bool hasSpecialPrefix = funcName[0] ==
'\01';
414 bool hasDot = funcName.find(
'.') != std::string::npos;
416 if (!hasDot && !hasSpecialPrefix)
420 if (hasSpecialPrefix)
422 const std::string prefix1 =
"\01_";
423 const std::string prefix2 =
"\01";
424 if (funcName.substr(0, prefix1.length()) == prefix1)
425 funcName = funcName.substr(prefix1.length());
426 else if (funcName.substr(0, prefix2.length()) == prefix2)
427 funcName = funcName.substr(prefix2.length());
431 std::replace(funcName.begin(), funcName.end(),
'.',
'_');
456 if(val==
nullptr)
return "{ empty val }";
459 std::stringstream rawstr(str);
466#if LLVM_VERSION_MAJOR >= 20
467 for (llvm::DbgVariableRecord *DVR : llvm::findDVRDeclares(
const_cast<Instruction*
>(inst)))
469 llvm::DIVariable *DIVar = DVR->getVariable();
470 rawstr <<
"\"ln\": " << DIVar->getLine() <<
", \"fl\": \"" << DIVar->getFilename().str() <<
"\"";
474 for (llvm::DbgInfoIntrinsic *DII : llvm::findDbgDeclares(
const_cast<Instruction*
>(inst)))
479 rawstr <<
"\"ln\": " << DIVar->getLine() <<
", \"fl\": \"" << DIVar->getFilename().str() <<
"\"";
485 else if (
MDNode *N = inst->getMetadata(
"dbg"))
488 unsigned Line = Loc->getLine();
489 unsigned Column = Loc->getColumn();
490 std::string File = Loc->getFilename().str();
492 if(File.empty() || Line == 0)
494 auto inlineLoc = Loc->getInlinedAt();
497 Line = inlineLoc->getLine();
498 Column = inlineLoc->getColumn();
499 File = inlineLoc->getFilename().str();
502 rawstr <<
"\"ln\": " << Line <<
", \"cl\": " << Column <<
", \"fl\": \"" << File <<
"\"";
507 if (argument->getArgNo()%10 == 1)
508 rawstr << argument->getArgNo() <<
"st";
509 else if (argument->getArgNo()%10 == 2)
510 rawstr << argument->getArgNo() <<
"nd";
511 else if (argument->getArgNo()%10 == 3)
512 rawstr << argument->getArgNo() <<
"rd";
514 rawstr << argument->getArgNo() <<
"th";
515 rawstr <<
" arg " << argument->getParent()->getName().str() <<
" "
521 NamedMDNode* CU_Nodes = gvar->getParent()->getNamedMetadata(
"llvm.dbg.cu");
524 for (
unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
527 for (llvm::DIGlobalVariableExpression *GV : CUNode->getGlobalVariables())
529 llvm::DIGlobalVariable * DGV = GV->getVariable();
531 if(DGV->getName() == gvar->getName())
533 rawstr <<
"\"ln\": " << DGV->getLine() <<
", \"fl\": \"" << DGV->getFilename().str() <<
"\"";
546 auto nonPhiIt = bb->getFirstNonPHIIt();
547 rawstr <<
"\"basic block\": " << bb->getName().str() <<
", \"location\": " <<
getSourceLoc(nonPhiIt != bb->end() ? &*nonPhiIt :
nullptr);
551 rawstr <<
"constant data";
559 if(rawstr.str()==
"{ }")
571 std::stringstream rawstr(str);
576 if (llvm::DISubprogram *SP = F->getSubprogram())
578 if (SP->describes(F))
579 rawstr <<
"\"ln\": " << SP->getLine() <<
", \"file\": \"" << SP->getFilename().str() <<
"\"";
587 if (!curInst->isTerminator())
589 const Instruction* nextInst = curInst->getNextNode();
593 instList.push_back(nextInst);
605 instList.push_back(nextInst);
615 llvm::raw_string_ostream rawstr(str);
617 rawstr <<
" " << *val <<
" ";
619 rawstr <<
" llvm Value is null";
626 llvm::raw_string_ostream rawstr(str);
628 rawstr <<
" " << *
type <<
" ";
630 rawstr <<
" llvm type is null";
637 llvm::raw_string_ostream rawstr(str);
641 rawstr <<
" llvm Value is null";
648 bool isPtrTy = inst->getType()->isPointerTy();
651 const Function* fun = call->getCalledFunction();
652 return fun && isPtrTy &&
664 const Function* fun = call->getCalledFunction();
677 bool isPtrTy = inst->getType()->isPointerTy();
680 const Function* fun = call->getCalledFunction();
681 return fun && isPtrTy &&
750 llvm::raw_string_ostream rawstr(str);
755 "invalid value, refer to their toString method");
759 rawstr <<
" " << *llvmVal <<
" ";
bool is_alloc_stack_ret(const Function *F)
const FunObjVar * getFunObjVar(const Function *fun) const
static LLVMModuleSet * getLLVMModuleSet()
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb)
bool hasLLVMValue(const SVFValue *value) const
DominatorTree & getDomTree(const Function *fun)
bool is_alloc(const Function *F)
const Value * getLLVMValue(const SVFValue *value) const
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
bool is_arg_alloc(const Function *F)
s32_t get_alloc_arg_pos(const Function *F)
bool is_ext(const Function *F)
bool is_memset(const Function *F)
bool is_memcpy(const Function *F)
bool is_realloc(const Function *F)
static Option< bool > ModelArrays
virtual const std::string getSourceLoc() const
const bool hasLLVMValue() const
const std::string valueOnlyToString() const
u32_t getNumOfFlattenElements() const
Return number of elements after flattening (including array elements).
u32_t getNumOfFlattenFields() const
Return the number of fields after flattening (ignoring array elements).
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
const Function * getProgFunction(const std::string &funName)
Get program entry function from module.
const Value * stripConstantCasts(const Value *val)
Strip off the constant casts.
bool isPtrInUncalledFunction(const Value *value)
Return true if this is value in a dead function (function without any caller).
bool isHeapAllocExtCallViaRet(const Instruction *inst)
const Value * getFirstUseViaCastInst(const Value *val)
bool isHeapAllocExtCall(const Instruction *inst)
void viewCFGOnly(const Function *fun)
const std::string getSourceLocOfFunction(const Function *F)
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
const Value * stripAllCasts(const Value *val)
Strip off the all casts.
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
bool isMemcpyExtFun(const Function *fun)
bool isIntrinsicFun(const Function *func)
std::vector< const Function * > getCalledFunctions(const Function *F)
Get all called funcions in a parent function.
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
bool isStackAllocExtCall(const Instruction *inst)
bool functionDoesNotRet(const Function *fun)
std::string dumpType(const Type *type)
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
std::string dumpValueAndDbgInfo(const Value *val)
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
const std::string getSourceLoc(const Value *val)
bool isHeapObj(const Value *val)
const Value * getGlobalRep(const Value *val)
find the unique defined global across multiple modules
void getFunReachableBBs(const Function *svfFun, std::vector< const SVFBasicBlock * > &bbs)
Get reachable basic block from function entry.
bool isExtCall(const Function *fun)
u32_t getNumOfElements(const Type *ety)
Return size of this object based on LLVM value.
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
void viewCFG(const Function *fun)
bool isStackObj(const Value *val)
bool isHeapAllocExtCallViaArg(const Instruction *inst)
bool isMemsetExtFun(const Function *fun)
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main).
bool isObject(const Value *ref)
Return true if this value refers to a object.
void processArguments(int argc, char **argv, int &arg_num, char **arg_value, std::vector< std::string > &moduleNameVec)
Parse argument for multi-module analysis.
bool isIRFile(const std::string &filename)
Check whether a file is an LLVM IR file.
bool ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main).
bool isStackAllocExtCallViaRet(const Instruction *inst)
u32_t getHeapAllocHoldingArgPosition(const Function *fun)
std::string restoreFuncName(std::string funcName)
const FunObjVar * getFunObjVar(const std::string &name)
bool isNonInstricCallSite(const Instruction *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
std::string dumpValue(const Value *val)
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
LLVM_NODISCARD bool isa(const Y &Val)
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
llvm::GlobalVariable GlobalVariable
llvm::const_succ_iterator succ_const_iterator
LLVM Iterators.
llvm::BasicBlock BasicBlock
llvm::NamedMDNode NamedMDNode
LLVM metadata and debug information.
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
llvm::Instruction Instruction
llvm::DomTreeNode DomTreeNode
llvm::Value Value
LLVM Basic classes.
llvm::ConstantExpr ConstantExpr
llvm::DominatorTree DominatorTree
LLVM Dominators.