Skip to content

Commit

Permalink
Merge branch 'upstream/master' into nullderef
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanLin520 committed Oct 19, 2024
2 parents af7460b + 8370e68 commit 6db9e72
Show file tree
Hide file tree
Showing 29 changed files with 198 additions and 140 deletions.
1 change: 1 addition & 0 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class LLVMModuleSet
InstToBlockNodeMapTy InstToBlockNodeMap; ///< map a basic block to its ICFGNode
FunToFunEntryNodeMapTy FunToFunEntryNodeMap; ///< map a function to its FunExitICFGNode
FunToFunExitNodeMapTy FunToFunExitNodeMap; ///< map a function to its FunEntryICFGNode
CallGraph* callgraph;

/// Constructor
LLVMModuleSet();
Expand Down
6 changes: 3 additions & 3 deletions svf-llvm/lib/ICFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
}
InstVec nextInsts;
LLVMUtil::getNextInsts(inst, nextInsts);
u32_t branchID = 0;
s64_t branchID = 0;
for (InstVec::const_iterator nit = nextInsts.begin(), enit =
nextInsts.end(); nit != enit; ++nit)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
{
assert(branchID <= 1 && "if/else has more than two branches?");
if(br->isConditional())
icfg->addConditionalIntraEdge(srcNode, dstNode, llvmModuleSet()->getSVFValue(br->getCondition()), 1 - branchID);
icfg->addConditionalIntraEdge(srcNode, dstNode, 1 - branchID);
else
icfg->addIntraEdge(srcNode, dstNode);
}
Expand All @@ -197,7 +197,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
s64_t val = -1;
if (condVal && condVal->getBitWidth() <= 64)
val = condVal->getSExtValue();
icfg->addConditionalIntraEdge(srcNode, dstNode, llvmModuleSet()->getSVFValue(si->getCondition()),val);
icfg->addConditionalIntraEdge(srcNode, dstNode,val);
}
else
icfg->addIntraEdge(srcNode, dstNode);
Expand Down
5 changes: 5 additions & 0 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "SVF-LLVM/ObjTypeInference.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "SVF-LLVM/ICFGBuilder.h"
#include "Graphs/CallGraph.h"
#include "Util/CallGraphBuilder.h"

using namespace std;
using namespace SVF;
Expand Down Expand Up @@ -169,6 +171,9 @@ void LLVMModuleSet::build()
initSVFFunction();
ICFGBuilder icfgbuilder;
icfg = icfgbuilder.build();

CallGraphBuilder callGraphBuilder;
callgraph = callGraphBuilder.buildSVFIRCallGraph(svfModule);
}

void LLVMModuleSet::createSVFDataStructure()
Expand Down
23 changes: 23 additions & 0 deletions svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "SVFIR/SVFFileSystem.h"
#include "SVFIR/SVFModule.h"
#include "SVFIR/SVFValue.h"
#include "Util/CallGraphBuilder.h"
#include "Util/Options.h"
#include "Util/SVFUtil.h"

Expand All @@ -62,6 +63,9 @@ SVFIR* SVFIRBuilder::build()
// Build ICFG
pag->setICFG(llvmModuleSet()->getICFG());

// Set callgraph
pag->setCallGraph(llvmModuleSet()->callgraph);

// Set icfgnode in memobj
for (auto& it : SymbolTableInfo::SymbolInfo()->idToObjMap())
{
Expand Down Expand Up @@ -971,6 +975,17 @@ void SVFIRBuilder::visitBranchInst(BranchInst &inst)
branchID++;
}
addBranchStmt(brinst, cond, successors);
/// set conditional svf var
if (inst.isConditional())
{
for (auto& edge : llvmModuleSet()->getICFGNode(&inst)->getOutEdges())
{
if (IntraCFGEdge* intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
{
intraEdge->setConditionVar(pag->getGNode(cond));
}
}
}
}


Expand Down Expand Up @@ -1039,6 +1054,14 @@ void SVFIRBuilder::visitSwitchInst(SwitchInst &inst)
successors.push_back(std::make_pair(icfgNode, val));
}
addBranchStmt(brinst, cond, successors);
/// set conditional svf var
for (auto& edge : llvmModuleSet()->getICFGNode(&inst)->getOutEdges())
{
if (IntraCFGEdge* intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
{
intraEdge->setConditionVar(pag->getGNode(cond));
}
}
}


Expand Down
12 changes: 9 additions & 3 deletions svf/include/Graphs/CallGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ class CallGraphNode : public GenericCallGraphNodeTy

}

inline const std::string &getName() const
{
return fun->getName();
}

/// Get function of this call node
inline const SVFFunction* getFunction() const
{
Expand Down Expand Up @@ -254,8 +259,6 @@ class CallGraph : public GenericCallGraphTy
};

private:
CGEK kind;

/// Indirect call map
CallEdgeMap indirectCallMap;

Expand All @@ -270,6 +273,7 @@ class CallGraph : public GenericCallGraphTy

NodeID callGraphNodeNum;
u32_t numOfResolvedIndCallEdge;
CGEK kind;

/// Clean up memory
void destroy();
Expand All @@ -278,7 +282,9 @@ class CallGraph : public GenericCallGraphTy
/// Constructor
CallGraph(CGEK k = NormCallGraph);

/// Add callgraph Node
/// Copy constructor
CallGraph(const CallGraph& other);

void addCallGraphNode(const SVFFunction* fun);

/// Destructor
Expand Down
2 changes: 1 addition & 1 deletion svf/include/Graphs/ICFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ICFG : public GenericICFGTy
/// Add intraprocedural and interprocedural control-flow edges.
//@{
ICFGEdge* addIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode);
ICFGEdge* addConditionalIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode, const SVFValue* condition, s32_t branchCondVal);
ICFGEdge* addConditionalIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode, s64_t branchCondVal);
ICFGEdge* addCallEdge(ICFGNode* srcNode, ICFGNode* dstNode);
ICFGEdge* addRetEdge(ICFGNode* srcNode, ICFGNode* dstNode);
//@}
Expand Down
22 changes: 14 additions & 8 deletions svf/include/Graphs/ICFGEdge.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class IntraCFGEdge : public ICFGEdge
{
friend class SVFIRWriter;
friend class SVFIRReader;
friend class ICFG;
friend class SVFIRBuilder;

public:
/// Constructor
Expand All @@ -137,7 +139,7 @@ class IntraCFGEdge : public ICFGEdge
}
//@}

const SVFValue* getCondition() const
const SVFVar* getCondition() const
{
return conditionVar;
}
Expand All @@ -148,12 +150,6 @@ class IntraCFGEdge : public ICFGEdge
return branchCondVal;
}

void setBranchCondition(const SVFValue* c, s64_t bVal)
{
conditionVar = c;
branchCondVal = bVal;
}

virtual const std::string toString() const;

private:
Expand All @@ -166,8 +162,18 @@ class IntraCFGEdge : public ICFGEdge
/// Inst3: label 1;
/// for edge between Inst1 and Inst 2, the first element is %cmp and
/// the second element is 0
const SVFValue* conditionVar;
const SVFVar* conditionVar;
s64_t branchCondVal;

inline void setConditionVar(const SVFVar* c)
{
conditionVar = c;
}

inline void setBranchCondVal(s64_t bVal)
{
branchCondVal = bVal;
}
};

/*!
Expand Down
5 changes: 4 additions & 1 deletion svf/include/Graphs/ThreadCallGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ class ThreadCallGraph: public CallGraph
typedef Map<const CallICFGNode*, ParForEdgeSet> CallInstToParForEdgesMap;

/// Constructor
ThreadCallGraph();
ThreadCallGraph(const CallGraph& cg);

ThreadCallGraph(ThreadCallGraph& cg) = delete;

/// Destructor
virtual ~ThreadCallGraph()
{
Expand Down
13 changes: 13 additions & 0 deletions svf/include/SVFIR/SVFIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class SVFIR : public IRGraph
ICFG* icfg; // ICFG
CommonCHGraph* chgraph; // class hierarchy graph
CallSiteSet callSiteSet; /// all the callsites of a program
CallGraph* callGraph; /// call graph

static std::unique_ptr<SVFIR> pag; ///< Singleton pattern here to enable instance of SVFIR can only be created once.

Expand Down Expand Up @@ -182,6 +183,18 @@ class SVFIR : public IRGraph
assert(chgraph && "empty ICFG! Build SVF IR first!");
return chgraph;
}

/// Set/Get CG
inline void setCallGraph(CallGraph* c)
{
callGraph = c;
}
inline CallGraph* getCallGraph()
{
assert(callGraph && "empty CallGraph! Build SVF IR first!");
return callGraph;
}

/// Get/set methods to get SVFStmts based on their kinds and ICFGNodes
//@{
/// Get edges set according to its kind
Expand Down
17 changes: 0 additions & 17 deletions svf/include/SVFIR/SVFModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,6 @@ class SVFModule

/// Iterators
///@{
iterator begin()
{
return FunctionSet.begin();
}
const_iterator begin() const
{
return FunctionSet.begin();
}
iterator end()
{
return FunctionSet.end();
}
const_iterator end() const
{
return FunctionSet.end();
}

global_iterator global_begin()
{
return GlobalSet.begin();
Expand Down
26 changes: 5 additions & 21 deletions svf/include/Util/CallGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,18 @@ namespace SVF
{

class ICFG;
class SVFModule;

class CallGraphBuilder
{

protected:
CallGraph* callgraph;
ICFG* icfg;
public:
CallGraphBuilder(CallGraph* cg, ICFG* i): callgraph(cg),icfg(i)
{
}

/// Build normal callgraph
CallGraph* buildCallGraph(SVFModule* svfModule);

};
CallGraphBuilder()=default;

class ThreadCallGraphBuilder : public CallGraphBuilder
{

public:
ThreadCallGraphBuilder(ThreadCallGraph* cg, ICFG* i): CallGraphBuilder(cg,i)
{
}
/// Buidl SVFIR callgraoh
CallGraph* buildSVFIRCallGraph(SVFModule* svfModule);

/// Build thread-aware callgraph
CallGraph* buildThreadCallGraph(SVFModule* svfModule);

ThreadCallGraph* buildThreadCallGraph();
};

} // End namespace SVF
Expand Down
26 changes: 4 additions & 22 deletions svf/include/Util/SVFUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,29 +327,11 @@ inline bool isProgEntryFunction(const SVFFunction* fun)
return fun && fun->getName() == "main";
}

/// Get program entry function from module.
inline const SVFFunction* getProgFunction(SVFModule* svfModule, const std::string& funName)
{
for (SVFModule::const_iterator it = svfModule->begin(), eit = svfModule->end(); it != eit; ++it)
{
const SVFFunction *fun = *it;
if (fun->getName()==funName)
return fun;
}
return nullptr;
}
/// Get program entry function from function name.
const SVFFunction* getProgFunction(const std::string& funName);

/// Get program entry function from module.
inline const SVFFunction* getProgEntryFunction(SVFModule* svfModule)
{
for (SVFModule::const_iterator it = svfModule->begin(), eit = svfModule->end(); it != eit; ++it)
{
const SVFFunction *fun = *it;
if (isProgEntryFunction(fun))
return (fun);
}
return nullptr;
}
/// Get program entry function.
const SVFFunction* getProgEntryFunction();

/// Return true if this is a program exit function call
//@{
Expand Down
4 changes: 1 addition & 3 deletions svf/lib/AE/Svfexe/AbstractInterpretation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,7 @@ bool AbstractInterpretation::isSwitchBranchFeasible(const SVFVar* var, s64_t suc
bool AbstractInterpretation::isBranchFeasible(const IntraCFGEdge* intraEdge,
AbstractState& as)
{
const SVFValue *cond = intraEdge->getCondition();
NodeID cmpID = svfir->getValueNode(cond);
SVFVar *cmpVar = svfir->getGNode(cmpID);
const SVFVar *cmpVar = intraEdge->getCondition();
if (cmpVar->getInEdges().empty())
{
return isSwitchBranchFeasible(cmpVar,
Expand Down
Loading

0 comments on commit 6db9e72

Please sign in to comment.