From 3556d16451b9dbd80c45540f1a3accb9d8bb7fe4 Mon Sep 17 00:00:00 2001 From: Xiao Date: Tue, 10 Dec 2024 22:42:55 +1100 Subject: [PATCH] refactor icfgbuilder (#1605) * merge * fix * remove unused func --- svf-llvm/include/SVF-LLVM/ICFGBuilder.h | 31 +------------ svf-llvm/include/SVF-LLVM/LLVMModule.h | 31 ++++++++++--- svf-llvm/lib/ICFGBuilder.cpp | 14 +++--- svf-llvm/lib/LLVMModule.cpp | 20 ++------- svf/include/SVFIR/SVFFileSystem.h | 3 -- svf/include/SVFIR/SVFValue.h | 60 ------------------------- svf/lib/SVFIR/SVFFileSystem.cpp | 21 --------- 7 files changed, 36 insertions(+), 144 deletions(-) diff --git a/svf-llvm/include/SVF-LLVM/ICFGBuilder.h b/svf-llvm/include/SVF-LLVM/ICFGBuilder.h index 3f79ac767..1367568e2 100644 --- a/svf-llvm/include/SVF-LLVM/ICFGBuilder.h +++ b/svf-llvm/include/SVF-LLVM/ICFGBuilder.h @@ -58,44 +58,17 @@ class ICFGBuilder public: typedef FIFOWorkList 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 diff --git a/svf-llvm/include/SVF-LLVM/LLVMModule.h b/svf-llvm/include/SVF-LLVM/LLVMModule.h index fbe5d6a5b..be1c14797 100644 --- a/svf-llvm/include/SVF-LLVM/LLVMModule.h +++ b/svf-llvm/include/SVF-LLVM/LLVMModule.h @@ -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) { @@ -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; @@ -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; } diff --git a/svf-llvm/lib/ICFGBuilder.cpp b/svf-llvm/lib/ICFGBuilder.cpp index 985f7c0dc..4dde63ae7 100644 --- a/svf-llvm/lib/ICFGBuilder.cpp +++ b/svf-llvm/lib/ICFGBuilder.cpp @@ -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(); @@ -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(inst))); //creating interprocedural edges return callICFGNode; @@ -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(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)); } \ No newline at end of file diff --git a/svf-llvm/lib/LLVMModule.cpp b/svf-llvm/lib/LLVMModule.cpp index 88c45476b..d55254ff0 100644 --- a/svf-llvm/lib/LLVMModule.cpp +++ b/svf-llvm/lib/LLVMModule.cpp @@ -298,13 +298,7 @@ void LLVMModuleSet::createSVFFunction(const Function* func) SVFInstruction* svfInst = nullptr; if (const CallBase* call = SVFUtil::dyn_cast(&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()); @@ -387,12 +381,6 @@ void LLVMModuleSet::initSVFBasicBlock(const Function* func) { svfcall->setCalledOperand(getSVFValue(called_llvmval)); } - if(SVFVirtualCallInst* virtualCall = SVFUtil::dyn_cast(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)); @@ -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) diff --git a/svf/include/SVFIR/SVFFileSystem.h b/svf/include/SVFIR/SVFFileSystem.h index b3ab13b39..0fc17af3e 100644 --- a/svf/include/SVFIR/SVFFileSystem.h +++ b/svf/include/SVFIR/SVFFileSystem.h @@ -116,7 +116,6 @@ class SVFFunction; class SVFBasicBlock; class SVFInstruction; class SVFCallInst; -class SVFVirtualCallInst; class SVFConstant; class SVFGlobalValue; class SVFArgument; @@ -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); @@ -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); diff --git a/svf/include/SVFIR/SVFValue.h b/svf/include/SVFIR/SVFValue.h index bb416d640..bd0f0690a 100644 --- a/svf/include/SVFIR/SVFValue.h +++ b/svf/include/SVFIR/SVFValue.h @@ -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; diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index fc1d5fbda..994b0fae5 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -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: @@ -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); @@ -580,15 +577,6 @@ cJSON* SVFIRWriter::contentToJson(const SVFCallInst* value) return root; } -cJSON* SVFIRWriter::contentToJson(const SVFVirtualCallInst* value) -{ - cJSON* root = contentToJson(static_cast(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(value)); @@ -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); @@ -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(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(value));