Skip to content

Commit

Permalink
add funcvar (#1598)
Browse files Browse the repository at this point in the history
* add funcvar

* rename

* refactor addobjvar
  • Loading branch information
jumormt authored Nov 30, 2024
1 parent dc8d089 commit 4708c4e
Show file tree
Hide file tree
Showing 26 changed files with 375 additions and 98 deletions.
11 changes: 11 additions & 0 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LLVMModuleSet
typedef Map<const GlobalVariable*, GlobalVariable*> GlobalDefToRepMapTy;

typedef Map<const Function*, SVFFunction*> LLVMFun2SVFFunMap;
typedef Map<const Function*, CallGraphNode*> LLVMFun2CallGraphNodeMap;
typedef Map<const BasicBlock*, SVFBasicBlock*> LLVMBB2SVFBBMap;
typedef Map<const Instruction*, SVFInstruction*> LLVMInst2SVFInstMap;
typedef Map<const Argument*, SVFArgument*> LLVMArgument2SVFArgumentMap;
Expand Down Expand Up @@ -89,6 +90,7 @@ class LLVMModuleSet
GlobalDefToRepMapTy GlobalDefToRepMap;

LLVMFun2SVFFunMap LLVMFunc2SVFFunc; ///< Map an LLVM Function to an SVF Function
LLVMFun2CallGraphNodeMap LLVMFunc2CallGraphNode; ///< Map an LLVM Function to an CallGraph Node
LLVMBB2SVFBBMap LLVMBB2SVFBB;
LLVMInst2SVFInstMap LLVMInst2SVFInst;
LLVMArgument2SVFArgumentMap LLVMArgument2SVFArgument;
Expand Down Expand Up @@ -170,6 +172,8 @@ class LLVMModuleSet
LLVMFunc2SVFFunc[func] = svfFunc;
setValueAttr(func,svfFunc);
}
void addFunctionMap(const Function* func, CallGraphNode* svfFunc);

inline void addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB)
{
LLVMBB2SVFBB[bb] = svfBB;
Expand Down Expand Up @@ -234,6 +238,13 @@ class LLVMModuleSet
return it->second;
}

inline CallGraphNode* getCallGraphNode(const Function* fun) const
{
LLVMFun2CallGraphNodeMap::const_iterator it = LLVMFunc2CallGraphNode.find(fun);
assert(it!=LLVMFunc2CallGraphNode.end() && "CallGraph Node not found!");
return it->second;
}

inline SVFBasicBlock* getSVFBasicBlock(const BasicBlock* bb) const
{
LLVMBB2SVFBBMap::const_iterator it = LLVMBB2SVFBB.find(bb);
Expand Down
15 changes: 14 additions & 1 deletion svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "SVF-LLVM/ObjTypeInference.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "SVF-LLVM/ICFGBuilder.h"
#include "Graphs/PTACallGraph.h"
#include "Graphs/CallGraph.h"
#include "Util/CallGraphBuilder.h"

using namespace std;
Expand Down Expand Up @@ -174,6 +174,13 @@ void LLVMModuleSet::build()

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

for (const auto& it : *callgraph)
{
addFunctionMap(
SVFUtil::cast<Function>(getLLVMValue(it.second->getFunction())),
it.second);
}
}

void LLVMModuleSet::createSVFDataStructure()
Expand Down Expand Up @@ -1210,6 +1217,12 @@ void LLVMModuleSet::dumpModulesToFile(const std::string& suffix)
}
}

void LLVMModuleSet::addFunctionMap(const SVF::Function* func, SVF::CallGraphNode* svfFunc)
{
LLVMFunc2CallGraphNode[func] = svfFunc;
setValueAttr(func,svfFunc);
}

void LLVMModuleSet::setValueAttr(const Value* val, SVFValue* svfvalue)
{
SVFValue2LLVMValue[svfvalue] = val;
Expand Down
43 changes: 35 additions & 8 deletions svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "SVFIR/SVFModule.h"
#include "SVFIR/SVFValue.h"
#include "Util/CallGraphBuilder.h"
#include "Graphs/CallGraph.h"
#include "Util/Options.h"
#include "Util/SVFUtil.h"

Expand Down Expand Up @@ -77,6 +78,10 @@ SVFIR* SVFIRBuilder::build()
{
if(llvmModuleSet()->hasICFGNode(inst))
it.second->gNode = llvmModuleSet()->getICFGNode(inst);
} else if (const Function* func = SVFUtil::dyn_cast<Function>(llvmModuleSet()->getLLVMValue(
it.second->getValue())))
{
it.second->gNode = llvmModuleSet()->getCallGraphNode(func);
}
}

Expand Down Expand Up @@ -216,16 +221,24 @@ void SVFIRBuilder::initialiseNodes()
if(iter->second == symTable->blkPtrSymID() || iter->second == symTable->nullPtrSymID())
continue;

const SVFBaseNode* gNode = nullptr;
const ICFGNode* icfgNode = nullptr;
if (const Instruction* inst =
SVFUtil::dyn_cast<Instruction>(llvmModuleSet()->getLLVMValue(iter->first)))
{
if (llvmModuleSet()->hasICFGNode(inst))
{
gNode = llvmModuleSet()->getICFGNode(inst);
icfgNode = llvmModuleSet()->getICFGNode(inst);
}
}
pag->addValNode(iter->first, iter->second, gNode);

if (const Function* func =
SVFUtil::dyn_cast<Function>(llvmModuleSet()->getLLVMValue(iter->first)))
{
const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func);
pag->addFunValNode(cgn, iter->second, icfgNode);
} else {
pag->addValNode(iter->first, iter->second, icfgNode);
}
}

for (SymbolTableInfo::ValueToIDMapTy::iterator iter =
Expand All @@ -235,23 +248,35 @@ void SVFIRBuilder::initialiseNodes()
DBOUT(DPAGBuild, outs() << "add obj node " << iter->second << "\n");
if(iter->second == symTable->blackholeSymID() || iter->second == symTable->constantSymID())
continue;
pag->addObjNode(iter->first, iter->second);
if (const Function* func = SVFUtil::dyn_cast<Function>(
llvmModuleSet()->getLLVMValue(iter->first)))
{
pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second);
} else {
pag->addObjNode(iter->first, iter->second);
}
}

for (SymbolTableInfo::FunToIDMapTy::iterator iter =
symTable->retSyms().begin(); iter != symTable->retSyms().end();
++iter)
{
DBOUT(DPAGBuild, outs() << "add ret node " << iter->second << "\n");
pag->addRetNode(iter->first, iter->second);
pag->addRetNode(
llvmModuleSet()->getCallGraphNode(SVFUtil::cast<Function>(
llvmModuleSet()->getLLVMValue(iter->first))),
iter->second);
}

for (SymbolTableInfo::FunToIDMapTy::iterator iter =
symTable->varargSyms().begin();
iter != symTable->varargSyms().end(); ++iter)
{
DBOUT(DPAGBuild, outs() << "add vararg node " << iter->second << "\n");
pag->addVarargNode(iter->first, iter->second);
pag->addVarargNode(
llvmModuleSet()->getCallGraphNode(SVFUtil::cast<Function>(
llvmModuleSet()->getLLVMValue(iter->first))),
iter->second);
}

/// add address edges for constant nodes.
Expand Down Expand Up @@ -867,7 +892,9 @@ void SVFIRBuilder::visitCallSite(CallBase* cs)

/// Collect callsite arguments and returns
for (u32_t i = 0; i < cs->arg_size(); i++)
pag->addCallSiteArgs(callBlockNode,pag->getGNode(getValueNode(cs->getArgOperand(i))));
pag->addCallSiteArgs(
callBlockNode,
SVFUtil::cast<ValVar>(pag->getGNode(getValueNode(cs->getArgOperand(i)))));

if(!cs->getType()->isVoidTy())
pag->addCallSiteRets(retBlockNode,pag->getGNode(getValueNode(cs)));
Expand Down Expand Up @@ -1319,7 +1346,7 @@ void SVFIRBuilder::setCurrentBBAndValueForPAGEdge(PAGEdge* edge)
{
assert(srcFun==curInst->getFunction() && "SrcNode of the PAGEdge not in the same function?");
}
if(dstFun!=nullptr && !SVFUtil::isa<CallPE>(edge) && !SVFUtil::isa<SVFFunction>(edge->getDstNode()->getValue()))
if(dstFun!=nullptr && !SVFUtil::isa<CallPE>(edge) && !SVFUtil::isa<RetPN>(edge->getDstNode()))
{
assert(dstFun==curInst->getFunction() && "DstNode of the PAGEdge not in the same function?");
}
Expand Down
7 changes: 5 additions & 2 deletions svf-llvm/lib/SVFIRExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Util/SVFUtil.h"
#include "SVF-LLVM/SymbolTableBuilder.h"
#include "SVF-LLVM/ObjTypeInference.h"
#include "Graphs/CallGraph.h"

using namespace std;
using namespace SVF;
Expand Down Expand Up @@ -256,9 +257,11 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle

if (isThreadForkCall(callICFGNode))
{
if (const SVFFunction* forkedFun = SVFUtil::dyn_cast<SVFFunction>(getForkedFun(callICFGNode)->getValue()))
const ValVar* valVar = getForkedFun(callICFGNode);
if (const FunValVar* funcValVar = SVFUtil::dyn_cast<FunValVar>(valVar))
{
forkedFun = forkedFun->getDefFunForMultipleModule();
const SVFFunction* forkedFun = funcValVar->getCallGraphNode()->getFunction()
->getDefFunForMultipleModule();
const SVFVar* actualParm = getActualParmAtForkSite(callICFGNode);
/// pthread_create has 1 arg.
/// apr_thread_create has 2 arg.
Expand Down
20 changes: 16 additions & 4 deletions svf/include/Graphs/GenericGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class SVFBaseNode
// │ ┌── ValVarKinds: Types of value variable nodes
// │ │ ├── Represents a standard value variable
ValNode,
// │ │ ├── Represents a Function value variable
FunValNode,
// │ │ ├── Represents a GEP value variable
GepValNode,
// │ │ ├── Represents a return value node
Expand All @@ -174,8 +176,10 @@ class SVFBaseNode
ObjNode,
// │ ├── GepObjNode: Represents a GEP object variable
GepObjNode,
//── FIObjNode: Represents a flow-insensitive object node
//── FIObjNode: Represents a flow-insensitive object node
FIObjNode,
// │ ├──FunObjNode: Types of function object
FunObjNode,
// │ └── DummyObjNode: Dummy node for uninitialized objects
DummyObjNode,
// └────────
Expand Down Expand Up @@ -313,7 +317,7 @@ class SVFBaseNode

static inline bool isSVFVarKind(GNodeK n)
{
static_assert(DummyObjNode - ValNode == 8,
static_assert(DummyObjNode - ValNode == 10,
"The number of SVFVarKinds has changed, make sure the "
"range is correct");

Expand All @@ -322,20 +326,28 @@ class SVFBaseNode

static inline bool isValVarKinds(GNodeK n)
{
static_assert(DummyValNode - ValNode == 4,
static_assert(DummyValNode - ValNode == 5,
"The number of ValVarKinds has changed, make sure the "
"range is correct");
return n <= DummyValNode && n >= ValNode;
}

static inline bool isObjVarKinds(GNodeK n)
{
static_assert(DummyObjNode - ObjNode == 3,
static_assert(DummyObjNode - ObjNode == 4,
"The number of ObjVarKinds has changed, make sure the "
"range is correct");
return n <= DummyObjNode && n >= ObjNode;
}

static inline bool isFIObjVarKinds(GNodeK n)
{
static_assert(FunObjNode - FIObjNode == 1,
"The number of FIObjVarKinds has changed, make sure the "
"range is correct");
return n <= FunObjNode && n >= FIObjNode;
}

static inline bool isVFGNodeKinds(GNodeK n)
{
static_assert(MInterPhi - Cmp == 24,
Expand Down
6 changes: 3 additions & 3 deletions svf/include/Graphs/ICFGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ class CallICFGNode : public InterICFGNode
friend class SVFIRReader;

public:
typedef std::vector<const SVFVar *> ActualParmNodeVec;
typedef std::vector<const ValVar *> ActualParmNodeVec;

protected:
const RetICFGNode* ret;
Expand Down Expand Up @@ -491,13 +491,13 @@ class CallICFGNode : public InterICFGNode
}

/// Add actual parameters
inline void addActualParms(const SVFVar *ap)
inline void addActualParms(const ValVar *ap)
{
APNodes.push_back(ap);
}
/// Parameter operations
//@{
inline const SVFVar* getArgument(u32_t ArgNo) const
inline const ValVar* getArgument(u32_t ArgNo) const
{
return getActualParms()[ArgNo];
}
Expand Down
25 changes: 17 additions & 8 deletions svf/include/SVFIR/SVFIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ class SVFIR : public IRGraph
funRetMap[fun] = ret;
}
/// Add callsite arguments
inline void addCallSiteArgs(CallICFGNode* callBlockNode,const SVFVar* arg)
inline void addCallSiteArgs(CallICFGNode* callBlockNode,const ValVar* arg)
{
callBlockNode->addActualParms(arg);
callSiteArgsListMap[callBlockNode].push_back(arg);
Expand All @@ -545,26 +545,34 @@ class SVFIR : public IRGraph
/// add node into SVFIR
//@{
/// Add a value (pointer) node
inline NodeID addValNode(const SVFValue* val, NodeID i, const SVFBaseNode* gNode)
inline NodeID addValNode(const SVFValue* val, NodeID i, const ICFGNode* icfgNode)
{
SVFVar *node = new ValVar(val,i, ValVar::ValNode, gNode);
SVFVar *node = new ValVar(val,i, ValVar::ValNode, icfgNode);
return addValNode(val, node, i);
}

NodeID addFunValNode(const CallGraphNode* callGraphNode, NodeID i, const ICFGNode* icfgNode) {
FunValVar* node = new FunValVar(callGraphNode, i, icfgNode);
return addValNode(nullptr, node, i);
}

/// Add a memory obj node
inline NodeID addObjNode(const SVFValue* val, NodeID i)
{
const MemObj* mem = getMemObj(val);
assert(mem->getId() == i && "not same object id?");
return addFIObjNode(mem);
}

NodeID addFunObjNode(const CallGraphNode* callGraphNode, NodeID id);
/// Add a unique return node for a procedure
inline NodeID addRetNode(const SVFFunction* val, NodeID i)
inline NodeID addRetNode(const CallGraphNode* callGraphNode, NodeID i)
{
SVFVar *node = new RetPN(val,i);
return addRetNode(val, node, i);
SVFVar *node = new RetPN(callGraphNode,i);
return addRetNode(callGraphNode, node, i);
}
/// Add a unique vararg node for a procedure
inline NodeID addVarargNode(const SVFFunction* val, NodeID i)
inline NodeID addVarargNode(const CallGraphNode* val, NodeID i)
{
SVFVar *node = new VarArgPN(val,i);
return addNode(node,i);
Expand All @@ -576,6 +584,7 @@ class SVFIR : public IRGraph
NodeID addGepObjNode(const MemObj* obj, const APOffset& apOffset, const NodeID gepId);
/// Add a field-insensitive node, this method can only invoked by getFIGepObjNode
NodeID addFIObjNode(const MemObj* obj);

//@}

/// Add a dummy value/object node according to node ID (llvm value is null)
Expand Down Expand Up @@ -628,7 +637,7 @@ class SVFIR : public IRGraph
return addNode(node, i);
}
/// Add a unique return node for a procedure
inline NodeID addRetNode(const SVFFunction*, SVFVar *node, NodeID i)
inline NodeID addRetNode(const CallGraphNode*, SVFVar *node, NodeID i)
{
return addNode(node,i);
}
Expand Down
Loading

0 comments on commit 4708c4e

Please sign in to comment.