40#include "llvm/Support/FileSystem.h"
42#include "llvm/Transforms/Utils/Cloning.h"
73#define SVF_MAIN_FUNC_NAME "svf.main"
74#define SVF_GLOBAL_CTORS "llvm.global_ctors"
75#define SVF_GLOBAL_DTORS "llvm.global_dtors"
91 item.second =
nullptr;
117 mset->modules.emplace_back(mod);
118 mset->loadExtAPIModules();
123 mset->buildSymbolTable();
135 mset->loadExtAPIModules();
148 mset->buildSymbolTable();
214 for (
Function& func : mod.functions())
216 if (func.isDeclaration())
259 alias.getName().str(),
getSVFType(alias.getType()));
279 SVFUtil::cast<SVFFunctionType>(
293 svfarg->setName(std::to_string(
arg.getArgNo()));
308 if (
const CallBase* call = SVFUtil::dyn_cast<CallBase>(&inst))
312 call->getFunctionType()->isVarArg(),
313 inst.isTerminator());
319 svfBB, inst.isTerminator(),
320 SVFUtil::isa<ReturnInst>(inst));
333 for (
const Function&
f : mod.functions())
349 for (Function::const_iterator bit = func->begin(),
ebit = func->end(); bit !=
ebit; ++bit)
365 if (
svfbb->getSuccessors().empty())
370 SVFUtil::isa<ReturnInst>(bb->back())) &&
371 "last inst must be return inst");
376 for (BasicBlock::const_iterator
iit = bb->begin(),
eiit = bb->end();
iit !=
eiit; ++
iit)
379 if(
const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
383 auto called_llvmval = call->getCalledOperand()->stripPointerCasts();
393 for(
u32_t i = 0;
i < call->arg_size();
i++)
403 if (
svfFun->exitBlock ==
nullptr &&
svfFun->hasBasicBlock())
407 SVFUtil::isa<ReturnInst>(&func->back().back())) &&
408 "last inst must be return inst");
416 if (fun->isDeclaration())
437 std::vector<const SVFBasicBlock*> reachableBBs;
439 ld->setReachableBBs(reachableBBs);
441 for (Function::const_iterator bit = fun->begin(),
beit = fun->end(); bit!=
beit; ++bit)
489 std::unique_ptr<BreakConstantGEPs>
p1 = std::make_unique<BreakConstantGEPs>();
496 std::unique_ptr<UnifyFunctionExitNodes>
p2 =
497 std::make_unique<UnifyFunctionExitNodes>();
500 for (
auto F =
M.begin(),
E =
M.end();
F !=
E; ++
F)
503 if (fun.isDeclaration())
505 p2->runOnFunction(fun);
514 mset->loadExtAPIModules();
515 mset->prePassSchedule();
574 owned_ctx = std::make_unique<LLVMContext>();
579 SVFUtil::errs() <<
"not an IR file: " << moduleName << std::endl;
587 SVFUtil::errs() <<
"load module: " << moduleName <<
"failed!!\n\n";
588 Err.print(
"SVFModuleLoader", llvm::errs());
609 assert(!
empty() &&
"LLVMModuleSet contains no modules; cannot load ExtAPI module without LLVMContext!");
625 Err.print(
"SVFModuleLoader", llvm::errs());
674 return func >
other.func;
679 std::priority_queue<LLVMGlobalFunction, std::vector<LLVMGlobalFunction>,
682 std::vector<const Function* >
result;
688 SVFUtil::dyn_cast<ConstantArray>(
global->getInitializer()))
695 SVFUtil::dyn_cast<ConstantStruct>(
702 const Function* func = SVFUtil::dyn_cast<Function>(
716 while (!
queue.empty())
747 for (
auto &func : mod)
769 Type*
ptr = PointerType::getUnqual(
M.getContext());
770 Type*
i32 = IntegerType::getInt32Ty(
M.getContext());
772#if (LLVM_VERSION_MAJOR >= 9)
775 Type::getVoidTy(
M.getContext()),
782 Type::getVoidTy(
M.getContext()),
786 svfmain->setCallingConv(llvm::CallingConv::C);
793 auto target =
M.getOrInsertFunction(
795 Type::getVoidTy(
M.getContext())
804 assert(
cnt <= 3 &&
"Too many arguments for main()");
810 auto target =
M.getOrInsertFunction(
dtor->getName(), Type::getVoidTy(
M.getContext()));
821 if (
glob ==
nullptr || !
glob->hasInitializer())
828 for (
unsigned i = 0;
i <
ca->getNumOperands(); ++
i)
840 if (
expr->getOpcode() == Instruction::BitCast && SVFUtil::isa<Function>(
expr->getOperand(0)))
841 fun = SVFUtil::cast<Function>(
expr->getOperand(0));
844 if (
note->getOpcode() != Instruction::GetElementPtr)
852 fun = SVFUtil::dyn_cast<Function>(
structAn->getOperand(0));
860 if (data && data->isString())
862 std::string
annotation = data->getAsString().str();
901 for (
const Function& fun : mod.functions())
905 if (fun.getName().str() ==
"main")
911 else if (fun.getName().str() ==
"svf__main")
926 for (
const Function& fun : mod.functions())
928 if (fun.isDeclaration())
943 std::set_intersection(
1038 if(
annotation.find(
"OVERWRITE") != std::string::npos)
1040 assert(
false &&
"overwrite and other annotations cannot co-exist");
1054 if (
CallInst *callInst = SVFUtil::dyn_cast<CallInst>(inst))
1056 Function *calledFunc = callInst->getCalledFunction();
1058 if (calledFunc && calledFunc->getName() ==
callee->getName())
1060 callInst->setCalledFunction(
callee);
1144 return &moduleRef.get() == extModule;
1164 if (
global.hasPrivateLinkage())
1181 return g->hasInitializer();
1202 std::string moduleName = mod.getName().str();
1204 std::size_t
pos = moduleName.rfind(
'.');
1205 if (
pos != std::string::npos)
1213#if (LLVM_VERSION_MAJOR >= 7)
1236 svfvalue->setPtrInUncalledFunction();
1243 assert(SVFUtil::isa<GlobalValue>(
llvmVal) &&
"not a GlobalValue?");
1248 const Function* func = SVFUtil::cast<Function>(
val);
1269 assert(SVFUtil::isa<SVFConstantData>(
it->second) &&
"not a SVFConstantData type!");
1270 return SVFUtil::cast<SVFConstantData>(
it->second);
1280 if(
cint->getBitWidth() == 1)
1282 else if(
cint->getBitWidth() <= 64 &&
cint->getBitWidth() > 1)
1291 if (
cfp->isNormalFP())
1293 const llvm::fltSemantics&
semantics =
cfp->getValueAPF().getSemantics();
1294 if (&
semantics == &llvm::APFloat::IEEEhalf() ||
1295 &
semantics == &llvm::APFloat::IEEEsingle() ||
1296 &
semantics == &llvm::APFloat::IEEEdouble() ||
1297 &
semantics == &llvm::APFloat::IEEEquad() ||
1298 &
semantics == &llvm::APFloat::x87DoubleExtended())
1300 dval =
cfp->getValueAPF().convertToDouble();
1304 assert (
false &&
"Unsupported floating point type");
1315 else if(SVFUtil::isa<ConstantPointerNull>(
cd))
1317 else if (SVFUtil::isa<UndefValue>(
cd))
1355 SVFUtil::isa<MetadataAsValue>(
ov)
1366 if (
const Function* fun = SVFUtil::dyn_cast<Function>(value))
1368 else if (
const BasicBlock* bb = SVFUtil::dyn_cast<BasicBlock>(value))
1370 else if(
const Instruction* inst = SVFUtil::dyn_cast<Instruction>(value))
1372 else if (
const Argument*
arg = SVFUtil::dyn_cast<Argument>(value))
1374 else if (
const Constant*
cons = SVFUtil::dyn_cast<Constant>(value))
1391 if (
it->second ==
T)
1394 assert(
false &&
"can't find the corresponding LLVM Type");
1403 assert(
T &&
"SVFType should not be null");
1426 assert (node!=
nullptr &&
"no ICFGNode for this instruction?");
1440 return node !=
nullptr;
1448 assert (node!=
nullptr &&
"no CallICFGNode for this instruction?");
1457 assert (node!=
nullptr &&
"no RetICFGNode for this instruction?");
1464 assert (node!=
nullptr &&
"no IntraICFGNode for this instruction?");
1481 else if (
const StructType*
sty = SVFUtil::dyn_cast<StructType>(
T))
1503 "SVFType has been added before");
1512 byteSize =
DL.getTypeAllocSize(
mut_T);
1516 if (SVFUtil::isa<PointerType>(
T))
1530 else if (
const StructType*
st = SVFUtil::dyn_cast<StructType>(
T))
1534 svfst->setName(
st->getName().str());
1537 else if (
const auto at = SVFUtil::dyn_cast<ArrayType>(
T))
1540 svfat->setNumOfElement(
at->getNumElements());
1548 llvm::raw_string_ostream(
buffer) << *
T;
1579 stInfo->setNumOfFieldsAndElems(1, 1);
1610 stInfo->getFlattenElementTypes().push_back(
et);
1615 "typeForArray size incorrect!!!");
1641 if (SVFUtil::isa<StructType, ArrayType>(
elemTy))
1657 stInfo->getFlattenElementTypes().push_back(
ty);
1672 "typeForStruct size incorrect!");
1691 stInfo->setNumOfFieldsAndElems(1,1);
#define SVF_MAIN_FUNC_NAME
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
const CallGraphNode * getCallGraphNode(const std::string &name)
std::string getExtBcPath()
static ExtAPI * getExtAPI()
void setExtFuncAnnotations(const SVFFunction *fun, const std::vector< std::string > &funcAnnotations)
void addOtherValueMap(const Value *ov, SVFOtherValue *svfov)
void createSVFFunction(const Function *func)
IntraICFGNode * getIntraBlock(const Instruction *inst)
LLVMType2SVFTypeMap LLVMType2SVFType
std::vector< const Function * > getLLVMGlobalFunctions(const GlobalVariable *global)
SVFOtherValue * getSVFOtherValue(const Value *ov)
void buildSymbolTable() const
void addInstructionMap(const Instruction *inst, SVFInstruction *svfInst)
Module * getMainLLVMModule() const
LLVMFun2CallGraphNodeMap LLVMFunc2CallGraphNode
Map an LLVM Function to an CallGraph Node.
StInfo * collectTypeInfo(const Type *ty)
Collect a type info.
void initSVFBasicBlock(const Function *func)
static LLVMModuleSet * getLLVMModuleSet()
SVFConstantData * getSVFConstantData(const ConstantData *cd)
void loadModules(const std::vector< std::string > &moduleNameVec)
ObjTypeInference * typeInference
static void releaseLLVMModuleSet()
static void preProcessBCs(std::vector< std::string > &moduleNameVec)
void addToLLVMVal2SVFVarMap(const Value *val, SVFBaseNode *svfBaseNode)
void addBasicBlockMap(const BasicBlock *bb, SVFBasicBlock *svfBB)
DominatorTree & getDomTree(const Function *fun)
LLVMInst2SVFInstMap LLVMInst2SVFInst
SVFModule * svfModule
Borrowed from singleton SVFModule::svfModule.
RetICFGNode * getRetBlock(const Instruction *cs)
Get/Add a return node.
std::unique_ptr< LLVMContext > owned_ctx
LLVMConst2SVFConstMap LLVMConst2SVFConst
void createSVFDataStructure()
StInfo * collectSimpleTypeInfo(const Type *T)
Collect simple type (non-aggregate) info.
SymbolTableInfo * symInfo
void addArgumentMap(const Argument *arg, SVFArgument *svfArg)
const Value * getLLVMValue(const SVFValue *value) const
void addOtherConstantMap(const Constant *cons, SVFConstant *svfcons)
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
const Type * getLLVMType(const SVFType *T) const
Get LLVM Type.
bool hasICFGNode(const Instruction *inst)
CallICFGNode * getCallBlock(const Instruction *cs)
Get/Add a call node.
ICFGNode * getICFGNode(const Instruction *inst)
Get a basic block ICFGNode.
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb) const
SVFConstant * getOtherSVFConstant(const Constant *oc)
SVFArgument * getSVFArgument(const Argument *arg) const
CallICFGNode * getCallICFGNode(const Instruction *cs)
get a call node
Fun2AnnoMap ExtFun2Annotations
Record annotations of function in extapi.bc.
void setValueAttr(const Value *val, SVFValue *value)
void addGlobalValueMap(const GlobalValue *glob, SVFGlobalValue *svfglob)
SVFValue2LLVMValueMap SVFValue2LLVMValue
RetICFGNode * getRetICFGNode(const Instruction *cs)
get a return node
void prePassSchedule()
Invoke llvm passes to modify module.
void initDomTree(SVFFunction *func, const Function *f)
StInfo * collectStructInfo(const StructType *structTy, u32_t &numFields)
Collect the struct info and set the number of fields after flattening.
SVFFunction * getSVFFunction(const Function *fun) const
const std::vector< std::reference_wrapper< Module > > & getLLVMModules() const
LLVMContext & getContext() const
Map< const Function *, DominatorTree > FunToDominatorTree
SVFBaseNode2LLVMValueMap SVFBaseNode2LLVMValue
StInfo * collectArrayInfo(const ArrayType *T)
Collect the array info.
LLVMModuleSet()
Constructor.
void buildGlobalDefToRepMap()
SVFType * addSVFTypeInfo(const Type *t)
Create SVFTypes.
SVFGlobalValue * getSVFGlobalValue(const GlobalValue *g) const
void addFunctionMap(const Function *func, SVFFunction *svfFunc)
FunctionSetType ExtFuncsVec
Record some "sse_" function declarations used in other ext function definition, e....
LLVMValue2SVFOtherValueMap LLVMValue2SVFOtherValue
static LLVMModuleSet * llvmModuleSet
IntraICFGNode * getIntraICFGNode(const Instruction *inst)
get a intra node
std::vector< std::reference_wrapper< Module > > modules
SVFInstruction * getSVFInstruction(const Instruction *inst) const
Map< std::string, std::vector< std::string > > Fun2AnnoMap
u32_t getModuleNum() const
GlobalDefToRepMapTy GlobalDefToRepMap
Global definition to a rep definition map.
std::vector< std::unique_ptr< Module > > owned_modules
static SVFModule * buildSVFModule(Module &mod)
Type2TypeInfoMap Type2TypeInfo
void dumpModulesToFile(const std::string &suffix)
void addConstantDataMap(const ConstantData *cd, SVFConstantData *svfcd)
void collectExtFunAnnotations(const Module *mod)
ObjTypeInference * getTypeInference()
SVFValue * getSVFValue(const Value *value)
static const Option< bool > SVFMain
static const Option< std::string > Graphtxt
void setCallGraphNode(CallGraphNode *cgn)
call graph node for this function
const Map< const SVFBasicBlock *, BBSet > & getDomFrontierMap() const
Set< const SVFBasicBlock * > BBSet
void addGlobalSet(SVFGlobalValue *glob)
static bool pagReadFromTXT()
void addOtherValue(SVFOtherValue *ov)
static void setPagFromTXT(const std::string &txt)
void addFunctionSet(SVFFunction *svfFunc)
void addAliasSet(SVFGlobalValue *alias)
const FunctionSetType & getFunctionSet() const
static SVFModule * getSVFModule()
void setModuleIdentifier(const std::string &moduleIdentifier)
void addConstant(SVFConstant *cd)
static double timeOfBuildingLLVMModule
static double getClk(bool mark=false)
static double timeOfBuildingSymbolTable
static SVFType * svfPtrTy
ptr type
static SVFType * svfI8Ty
8-bit int type
const SVFType * maxStruct
The struct type with the most fields.
void addStInfo(StInfo *stInfo)
void addTypeInfo(const SVFType *ty)
u32_t maxStSize
The number of fields in max_struct.
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
bool isPtrInUncalledFunction(const Value *value)
Return true if this is value in a dead function (function without any caller)
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
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)
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.
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
bool isIRFile(const std::string &filename)
Check whether a file is an LLVM IR file.
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.
bool isExtCall(const SVFFunction *fun)
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
std::ostream & errs()
Overwrite llvm::errs()
std::ostream & outs()
Overwrite llvm::outs()
llvm::GlobalVariable GlobalVariable
llvm::GlobalAlias GlobalAlias
llvm::ArrayType ArrayType
llvm::raw_fd_ostream raw_fd_ostream
LLVM outputs.
llvm::BasicBlock BasicBlock
llvm::inst_iterator inst_iterator
llvm::ConstantStruct ConstantStruct
llvm::StructType StructType
LLVM types.
llvm::succ_const_iterator succ_const_iterator
LLVM Iterators.
llvm::IntegerType IntegerType
llvm::ConstantArray ConstantArray
llvm::const_pred_iterator const_pred_iterator
llvm::GlobalValue GlobalValue
llvm::FunctionType FunctionType
llvm::ConstantData ConstantData
llvm::Instruction Instruction
llvm::DomTreeNode DomTreeNode
llvm::SMDiagnostic SMDiagnostic
llvm::ConstantDataSequential ConstantDataSequential
llvm::Value Value
LLVM Basic classes.
llvm::ConstantExpr ConstantExpr
llvm::IRBuilder IRBuilder
llvm::GlobalIFunc GlobalIFunc
llvm::PostDominatorTree PostDominatorTree
llvm::DominanceFrontier DominanceFrontier
llvm::Loop Loop
LLVM Loop.
llvm::ConstantFP ConstantFP
llvm::ConstantInt ConstantInt
llvm::DominatorTree DominatorTree
LLVM Dominators.
IntervalValue operator>(const IntervalValue &lhs, const IntervalValue &rhs)