Skip to content

Commit

Permalink
refactor icfgbuilder (#1605)
Browse files Browse the repository at this point in the history
* merge

* fix

* remove unused func
  • Loading branch information
jumormt authored Dec 10, 2024
1 parent 91b0eeb commit 3556d16
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 144 deletions.
31 changes: 2 additions & 29 deletions svf-llvm/include/SVF-LLVM/ICFGBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,17 @@ class ICFGBuilder
public:
typedef FIFOWorkList<const Instruction*> WorkList;

ICFGBuilder(): icfg(new ICFG())
{
ICFGBuilder() = default;

}
ICFG* build();

private:

LLVMModuleSet* llvmModuleSet()
inline LLVMModuleSet* llvmModuleSet()
{
return LLVMModuleSet::getLLVMModuleSet();
}

CSToRetNodeMapTy& csToRetNodeMap()
{
return llvmModuleSet()->CSToRetNodeMap;
}

CSToCallNodeMapTy& csToCallNodeMap()
{
return llvmModuleSet()->CSToCallNodeMap;
}

InstToBlockNodeMapTy& instToBlockNodeMap()
{
return llvmModuleSet()->InstToBlockNodeMap;
}

FunToFunEntryNodeMapTy& funToFunEntryNodeMap()
{
return llvmModuleSet()->FunToFunEntryNodeMap;
}

FunToFunExitNodeMapTy& funToFunExitNodeMap()
{
return llvmModuleSet()->FunToFunExitNodeMap;
}

private:

/// Create edges between ICFG nodes within a function
Expand Down
31 changes: 24 additions & 7 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ class LLVMModuleSet
LLVMFunc2SVFFunc[func] = svfFunc;
setValueAttr(func,svfFunc);
}
void addFunctionMap(const Function* func, CallGraphNode* cgNode);

void addFunctionMap(const Function* func, CallGraphNode* svfFunc);

inline void addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB)
{
Expand All @@ -184,6 +185,22 @@ class LLVMModuleSet
LLVMInst2SVFInst[inst] = svfInst;
setValueAttr(inst,svfInst);
}
inline void addInstructionMap(const Instruction* inst, CallICFGNode* svfInst)
{
CSToCallNodeMap[inst] = svfInst;
setValueAttr(inst,svfInst);
}
inline void addInstructionMap(const Instruction* inst, RetICFGNode* svfInst)
{
CSToRetNodeMap[inst] = svfInst;
setValueAttr(inst,svfInst);
}
inline void addInstructionMap(const Instruction* inst, IntraICFGNode* svfInst)
{
InstToBlockNodeMap[inst] = svfInst;
setValueAttr(inst,svfInst);
}

inline void addArgumentMap(const Argument* arg, SVFArgument* svfArg)
{
LLVMArgument2SVFArgument[arg] = svfArg;
Expand Down Expand Up @@ -231,17 +248,17 @@ class LLVMModuleSet
return it->second;
}

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

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

Expand Down
14 changes: 6 additions & 8 deletions svf-llvm/lib/ICFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using namespace SVFUtil;
*/
ICFG* ICFGBuilder::build()
{
icfg = new ICFG();
DBOUT(DGENERAL, outs() << pasMsg("\t Building ICFG ...\n"));
// Add the unique global ICFGNode at the entry of a program (before the main method).
addGlobalICFGNode();
Expand Down Expand Up @@ -260,13 +261,11 @@ InterICFGNode* ICFGBuilder::addInterBlockICFGNode(const Instruction* inst)
calledFunc, cb->getFunctionType()->isVarArg(), isvcall,
isvcall ? cppUtil::getVCallIdx(cb) : 0,
isvcall ? cppUtil::getFunNameOfVCallSite(cb) : "");
csToCallNodeMap()[inst] = callICFGNode;
llvmModuleSet()->setValueAttr(inst, callICFGNode);
llvmModuleSet()->addInstructionMap(inst, callICFGNode);

assert(llvmModuleSet()->getRetBlock(inst)==nullptr && "duplicate RetICFGNode");
RetICFGNode* retICFGNode = icfg->addRetICFGNode(callICFGNode);
csToRetNodeMap()[inst] = retICFGNode;
llvmModuleSet()->setValueAttr(inst, retICFGNode);
llvmModuleSet()->addInstructionMap(inst, retICFGNode);

addICFGInterEdges(inst, LLVMUtil::getCallee(SVFUtil::cast<CallBase>(inst))); //creating interprocedural edges
return callICFGNode;
Expand Down Expand Up @@ -347,19 +346,18 @@ IntraICFGNode* ICFGBuilder::addIntraBlockICFGNode(const Instruction* inst)
assert (node==nullptr && "no IntraICFGNode for this instruction?");
IntraICFGNode* sNode = icfg->addIntraICFGNode(
llvmModuleSet()->getSVFBasicBlock(inst->getParent()), SVFUtil::isa<ReturnInst>(inst));
instToBlockNodeMap()[inst] = sNode;
llvmModuleSet()->setValueAttr(inst, sNode);
llvmModuleSet()->addInstructionMap(inst, sNode);
return sNode;
}

FunEntryICFGNode* ICFGBuilder::addFunEntryBlock(const Function* fun)
{
return funToFunEntryNodeMap()[fun] =
return llvmModuleSet()->FunToFunEntryNodeMap[fun] =
icfg->addFunEntryICFGNode(llvmModuleSet()->getSVFFunction(fun));
}

inline FunExitICFGNode* ICFGBuilder::addFunExitBlock(const Function* fun)
{
return funToFunExitNodeMap()[fun] =
return llvmModuleSet()->FunToFunExitNodeMap[fun] =
icfg->addFunExitICFGNode(llvmModuleSet()->getSVFFunction(fun));
}
20 changes: 4 additions & 16 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,7 @@ void LLVMModuleSet::createSVFFunction(const Function* func)
SVFInstruction* svfInst = nullptr;
if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(&inst))
{
if (cppUtil::isVirtualCallSite(call))
svfInst = new SVFVirtualCallInst(
getSVFType(call->getType()), svfBB,
call->getFunctionType()->isVarArg(),
inst.isTerminator());
else
svfInst = new SVFCallInst(
svfInst = new SVFCallInst(
getSVFType(call->getType()), svfBB,
call->getFunctionType()->isVarArg(),
inst.isTerminator());
Expand Down Expand Up @@ -387,12 +381,6 @@ void LLVMModuleSet::initSVFBasicBlock(const Function* func)
{
svfcall->setCalledOperand(getSVFValue(called_llvmval));
}
if(SVFVirtualCallInst* virtualCall = SVFUtil::dyn_cast<SVFVirtualCallInst>(svfcall))
{
virtualCall->setVtablePtr(getSVFValue(cppUtil::getVCallVtblPtr(call)));
virtualCall->setFunIdxInVtable(cppUtil::getVCallIdx(call));
virtualCall->setFunNameOfVirtualCall(cppUtil::getFunNameOfVCallSite(call));
}
for(u32_t i = 0; i < call->arg_size(); i++)
{
SVFValue* svfval = getSVFValue(call->getArgOperand(i));
Expand Down Expand Up @@ -1224,10 +1212,10 @@ void LLVMModuleSet::dumpModulesToFile(const std::string& suffix)
}
}

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

void LLVMModuleSet::setValueAttr(const Value* val, SVFValue* svfvalue)
Expand Down
3 changes: 0 additions & 3 deletions svf/include/SVFIR/SVFFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class SVFFunction;
class SVFBasicBlock;
class SVFInstruction;
class SVFCallInst;
class SVFVirtualCallInst;
class SVFConstant;
class SVFGlobalValue;
class SVFArgument;
Expand Down Expand Up @@ -516,7 +515,6 @@ class SVFIRWriter
cJSON* contentToJson(const SVFBasicBlock* value);
cJSON* contentToJson(const SVFInstruction* value);
cJSON* contentToJson(const SVFCallInst* value);
cJSON* contentToJson(const SVFVirtualCallInst* value);
cJSON* contentToJson(const SVFConstant* value);
cJSON* contentToJson(const SVFGlobalValue* value);
cJSON* contentToJson(const SVFArgument* value);
Expand Down Expand Up @@ -1290,7 +1288,6 @@ class SVFIRReader
void fill(const cJSON*& fieldJson, SVFBasicBlock* value);
void fill(const cJSON*& fieldJson, SVFInstruction* value);
void fill(const cJSON*& fieldJson, SVFCallInst* value);
void fill(const cJSON*& fieldJson, SVFVirtualCallInst* value);
void fill(const cJSON*& fieldJson, SVFConstant* value);
void fill(const cJSON*& fieldJson, SVFGlobalValue* value);
void fill(const cJSON*& fieldJson, SVFArgument* value);
Expand Down
60 changes: 0 additions & 60 deletions svf/include/SVFIR/SVFValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,66 +746,6 @@ class SVFCallInst : public SVFInstruction
}
};

class SVFVirtualCallInst : public SVFCallInst
{
friend class SVFIRWriter;
friend class SVFIRReader;
friend class LLVMModuleSet;

private:
const SVFValue* vCallVtblPtr; /// virtual table pointer
s32_t virtualFunIdx; /// virtual function index of the virtual table(s) at a virtual call
std::string funNameOfVcall; /// the function name of this virtual call

protected:
inline void setFunIdxInVtable(s32_t idx)
{
virtualFunIdx = idx;
}
inline void setFunNameOfVirtualCall(const std::string& name)
{
funNameOfVcall = name;
}
inline void setVtablePtr(const SVFValue* vptr)
{
vCallVtblPtr = vptr;
}

public:
SVFVirtualCallInst(const SVFType* ty, const SVFBasicBlock* b, bool vararg,
bool tm)
: SVFCallInst(ty, b, vararg, tm, SVFVCall), vCallVtblPtr(nullptr),
virtualFunIdx(-1), funNameOfVcall()
{
}
inline const SVFValue* getVtablePtr() const
{
assert(vCallVtblPtr && "virtual call does not have a vtblptr? set it first");
return vCallVtblPtr;
}
inline s32_t getFunIdxInVtable() const
{
assert(virtualFunIdx >=0 && "virtual function idx is less than 0? not set yet?");
return virtualFunIdx;
}
inline const std::string& getFunNameOfVirtualCall() const
{
return funNameOfVcall;
}
static inline bool classof(const SVFValue *node)
{
return node->getKind() == SVFVCall;
}
static inline bool classof(const SVFInstruction *node)
{
return node->getKind() == SVFVCall;
}
static inline bool classof(const SVFCallInst *node)
{
return node->getKind() == SVFVCall;
}
};

class SVFConstant : public SVFValue
{
friend class SVFIRWriter;
Expand Down
21 changes: 0 additions & 21 deletions svf/lib/SVFIR/SVFFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ static SVFValue* createSVFValue(SVFValue::GNodeK kind, const SVFType* type,
return new SVFInstruction(type, {}, {}, {});
case SVFValue::SVFCall:
return new SVFCallInst(type, {}, {}, {});
case SVFValue::SVFVCall:
return new SVFVirtualCallInst(type, {}, {}, {});
case SVFValue::SVFGlob:
return new SVFGlobalValue(type);
case SVFValue::SVFArg:
Expand Down Expand Up @@ -189,7 +187,6 @@ cJSON* SVFIRWriter::virtToJson(const SVFValue* value)
CASE(SVFBB, SVFBasicBlock);
CASE(SVFInst, SVFInstruction);
CASE(SVFCall, SVFCallInst);
CASE(SVFVCall, SVFVirtualCallInst);
CASE(SVFGlob, SVFGlobalValue);
CASE(SVFArg, SVFArgument);
CASE(SVFConst, SVFConstant);
Expand Down Expand Up @@ -580,15 +577,6 @@ cJSON* SVFIRWriter::contentToJson(const SVFCallInst* value)
return root;
}

cJSON* SVFIRWriter::contentToJson(const SVFVirtualCallInst* value)
{
cJSON* root = contentToJson(static_cast<const SVFCallInst*>(value));
JSON_WRITE_FIELD(root, value, vCallVtblPtr);
JSON_WRITE_FIELD(root, value, virtualFunIdx);
JSON_WRITE_FIELD(root, value, funNameOfVcall);
return root;
}

cJSON* SVFIRWriter::contentToJson(const SVFConstant* value)
{
return contentToJson(static_cast<const SVFValue*>(value));
Expand Down Expand Up @@ -2310,7 +2298,6 @@ void SVFIRReader::virtFill(const cJSON*& fieldJson, SVFValue* value)
CASE(SVFBB, SVFBasicBlock);
CASE(SVFInst, SVFInstruction);
CASE(SVFCall, SVFCallInst);
CASE(SVFVCall, SVFVirtualCallInst);
CASE(SVFGlob, SVFGlobalValue);
CASE(SVFArg, SVFArgument);
CASE(SVFConst, SVFConstant);
Expand Down Expand Up @@ -2375,14 +2362,6 @@ void SVFIRReader::fill(const cJSON*& fieldJson, SVFCallInst* value)
JSON_READ_FIELD_FWD(fieldJson, value, calledVal);
}

void SVFIRReader::fill(const cJSON*& fieldJson, SVFVirtualCallInst* value)
{
fill(fieldJson, static_cast<SVFCallInst*>(value));
JSON_READ_FIELD_FWD(fieldJson, value, vCallVtblPtr);
JSON_READ_FIELD_FWD(fieldJson, value, virtualFunIdx);
JSON_READ_FIELD_FWD(fieldJson, value, funNameOfVcall);
}

void SVFIRReader::fill(const cJSON*& fieldJson, SVFConstant* value)
{
fill(fieldJson, static_cast<SVFValue*>(value));
Expand Down

0 comments on commit 3556d16

Please sign in to comment.