33 #include <llvm/Support/raw_ostream.h>
45 if (fun.getName() == funName)
63 if (SVFUtil::isa<GlobalVariable>(ref))
65 if (SVFUtil::isa<Function, AllocaInst>(ref))
79 dt.recalculate(
const_cast<Function&
>(*fun));
82 std::vector<const BasicBlock*> bbVec;
83 bbVec.push_back(&fun->getEntryBlock());
89 reachableBBs.push_back(svfbb);
92 for (DomTreeNode::iterator DI = dtNode->begin(), DE = dtNode->end();
96 if(visited.find(succbb)==visited.end())
97 visited.insert(succbb);
100 bbVec.push_back(succbb);
111 for (BasicBlock::const_iterator it = bb->begin(), eit = bb->end();
114 if(SVFUtil::isa<ReturnInst>(*it))
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)
178 if(
const Instruction* inst = SVFUtil::dyn_cast<Instruction>(value))
183 else if(
const Argument* arg = SVFUtil::dyn_cast<Argument>(value))
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))
206 if (
const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
208 const Function* func = call->getCalledFunction();
224 else if (
const ConstantExpr *CE = SVFUtil::dyn_cast<ConstantExpr>(val))
226 if (Instruction::isCast(CE->getOpcode()))
255 if (
const CastInst *ci = SVFUtil::dyn_cast<CastInst>(val))
257 val = ci->getOperand(0);
259 else if (
const ConstantExpr *ce = SVFUtil::dyn_cast<ConstantExpr>(val))
262 val = ce->getOperand(0);
281 assert(SVFUtil::isa<PointerType>(val->getType()) &&
"this value should be a pointer type!");
283 const Value *latestUse =
nullptr;
284 for (
const auto &it : val->uses())
286 if (SVFUtil::isa<BitCastInst>(it.getUser()))
287 latestUse = it.getUser();
299 assert(ety &&
"type is null?");
300 u32_t numOfFields = 1;
301 if (SVFUtil::isa<StructType, ArrayType>(ety))
322 std::unique_ptr<llvm::Module> module = llvm::parseIRFile(filename, err, context);
338 std::vector<std::string> &moduleNameVec)
340 bool first_ir_file =
true;
341 for (
int i = 0; i < argc; ++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;
370 if (
const CallBase *callInst = SVFUtil::dyn_cast<CallBase>(&I))
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;
386 assert(!funcName.empty() &&
"Empty function name");
389 bool hasSpecialPrefix = funcName[0] ==
'\01';
390 bool hasDot = funcName.find(
'.') != std::string::npos;
392 if (!hasDot && !hasSpecialPrefix)
396 if (hasSpecialPrefix)
400 if (funcName.substr(0, prefix1.length()) == prefix1)
401 funcName = funcName.substr(prefix1.length());
402 else if (funcName.substr(0, prefix2.length()) == prefix2)
403 funcName = funcName.substr(prefix2.length());
407 std::replace(funcName.begin(), funcName.end(),
'.',
'_');
419 if (
const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
432 if(val==
nullptr)
return "{ empty val }";
435 std::stringstream rawstr(str);
438 if (
const Instruction* inst = SVFUtil::dyn_cast<Instruction>(val))
440 if (SVFUtil::isa<AllocaInst>(inst))
442 for (llvm::DbgInfoIntrinsic *DII : FindDbgDeclareUses(
const_cast<Instruction*
>(inst)))
444 if (llvm::DbgDeclareInst *DDI = SVFUtil::dyn_cast<llvm::DbgDeclareInst>(DII))
446 llvm::DIVariable *DIVar = SVFUtil::cast<llvm::DIVariable>(DDI->getVariable());
447 rawstr <<
"\"ln\": " << DIVar->getLine() <<
", \"fl\": \"" << DIVar->getFilename().str() <<
"\"";
452 else if (
MDNode *N = inst->getMetadata(
"dbg"))
454 llvm::DILocation* Loc = SVFUtil::cast<llvm::DILocation>(N);
455 unsigned Line = Loc->getLine();
456 unsigned Column = Loc->getColumn();
459 if(File.empty() || Line == 0)
461 auto inlineLoc = Loc->getInlinedAt();
464 Line = inlineLoc->getLine();
465 Column = inlineLoc->getColumn();
466 File = inlineLoc->getFilename().str();
469 rawstr <<
"\"ln\": " << Line <<
", \"cl\": " << Column <<
", \"fl\": \"" << File <<
"\"";
472 else if (
const Argument* argument = SVFUtil::dyn_cast<Argument>(val))
474 if (argument->getArgNo()%10 == 1)
475 rawstr << argument->getArgNo() <<
"st";
476 else if (argument->getArgNo()%10 == 2)
477 rawstr << argument->getArgNo() <<
"nd";
478 else if (argument->getArgNo()%10 == 3)
479 rawstr << argument->getArgNo() <<
"rd";
481 rawstr << argument->getArgNo() <<
"th";
482 rawstr <<
" arg " << argument->getParent()->getName().str() <<
" "
485 else if (
const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
488 NamedMDNode* CU_Nodes = gvar->getParent()->getNamedMetadata(
"llvm.dbg.cu");
491 for (
unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
493 llvm::DICompileUnit *CUNode = SVFUtil::cast<llvm::DICompileUnit>(CU_Nodes->getOperand(i));
494 for (llvm::DIGlobalVariableExpression *GV : CUNode->getGlobalVariables())
496 llvm::DIGlobalVariable * DGV = GV->getVariable();
498 if(DGV->getName() == gvar->getName())
500 rawstr <<
"\"ln\": " << DGV->getLine() <<
", \"fl\": \"" << DGV->getFilename().str() <<
"\"";
507 else if (
const Function* func = SVFUtil::dyn_cast<Function>(val))
511 else if (
const BasicBlock* bb = SVFUtil::dyn_cast<BasicBlock>(val))
513 rawstr <<
"\"basic block\": " << bb->getName().str() <<
", \"location\": " <<
getSourceLoc(bb->getFirstNonPHI());
517 rawstr <<
"constant data";
525 if(rawstr.str()==
"{ }")
537 std::stringstream rawstr(str);
544 if (SP->describes(
F))
545 rawstr <<
"\"ln\": " << SP->getLine() <<
", \"file\": \"" << SP->getFilename().str() <<
"\"";
553 if (!curInst->isTerminator())
555 const Instruction* nextInst = curInst->getNextNode();
559 instList.push_back(nextInst);
571 instList.push_back(nextInst);
588 llvm::raw_string_ostream rawstr(str);
590 rawstr <<
" " << *val <<
" ";
592 rawstr <<
" llvm Value is null";
599 llvm::raw_string_ostream rawstr(str);
601 rawstr <<
" " << *
type <<
" ";
603 rawstr <<
" llvm type is null";
610 llvm::raw_string_ostream rawstr(str);
614 rawstr <<
" llvm Value is null";
622 bool isPtrTy = inst->getType()->isPointerTy();
623 if (
const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
625 const Function* fun = call->getCalledFunction();
626 return fun && isPtrTy &&
636 if (
const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
638 const Function* fun = call->getCalledFunction();
666 llvm::raw_string_ostream rawstr(str);
669 rawstr <<
"Function: " << fun->getName() <<
" ";
671 else if (
const SVFBasicBlock* bb = SVFUtil::dyn_cast<SVFBasicBlock>(
this))
673 rawstr <<
"BasicBlock: " << bb->getName() <<
" ";
679 rawstr <<
" " << *llvmVal <<
" ";
681 rawstr <<
" No llvmVal found";
691 llvm::raw_string_ostream rawstr(str);
694 rawstr <<
"Function: " << fun->getFunction()->getName() <<
" ";
700 rawstr <<
" " << *llvmVal <<
" ";
702 rawstr <<
" No llvmVal found";
bool is_alloc(const SVFFunction *F)
bool is_arg_alloc(const SVFFunction *F)
static ExtAPI * getExtAPI()
bool is_realloc(const SVFFunction *F)
SVFFunction * getSVFFunction(const Function *fun) const
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
const Value * getLLVMValue(const SVFValue *value) const
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb) const
static LLVMModuleSet * getLLVMModuleSet()
static const Option< bool > ModelArrays
const std::string valueOnlyToString() const
virtual const std::string getSourceLoc() const
const SVFType * getReturnType() const
Returns the FunctionType.
std::string toString() const
Needs to be implemented by a SVF front end.
virtual const std::string getSourceLoc() 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)
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.
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 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)
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.
u32_t getNumOfElements(const Type *ety)
Return size of this object based on LLVM value.
const SVFFunction * getFunction(const std::string &name)
Get the corresponding Function based on its name.
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
void viewCFG(const Function *fun)
bool isHeapAllocExtCallViaArg(const Instruction *inst)
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 isConstantObjSym(const SVFValue *val)
Check whether this value points-to a constant object.
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
std::string restoreFuncName(std::string funcName)
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)
bool isExtCall(const SVFFunction *fun)
std::ostream & errs()
Overwrite llvm::errs()
llvm::GlobalVariable GlobalVariable
llvm::BasicBlock BasicBlock
llvm::NamedMDNode NamedMDNode
LLVM metadata and debug information.
llvm::succ_const_iterator succ_const_iterator
LLVM Iterators.
llvm::Instruction Instruction
llvm::DomTreeNode DomTreeNode
llvm::SMDiagnostic SMDiagnostic
llvm::DISubprogram DISubprogram
llvm::Value Value
LLVM Basic classes.
llvm::ConstantExpr ConstantExpr
llvm::DominatorTree DominatorTree
LLVM Dominators.
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
llvm::LLVMContext LLVMContext