From 73130a1a88032d0575db003730deede8bd9d71f6 Mon Sep 17 00:00:00 2001 From: jumormt Date: Sat, 23 Nov 2024 01:01:38 +1100 Subject: [PATCH 01/28] add funcvar --- svf-llvm/include/SVF-LLVM/LLVMModule.h | 11 ++ svf-llvm/lib/LLVMModule.cpp | 15 +- svf-llvm/lib/SVFIRBuilder.cpp | 43 ++++- svf-llvm/lib/SVFIRExtAPI.cpp | 7 +- svf/include/Graphs/GenericGraph.h | 20 ++- svf/include/Graphs/ICFGNode.h | 6 +- svf/include/SVFIR/SVFIR.h | 31 ++-- svf/include/SVFIR/SVFVariables.h | 157 ++++++++++++++++--- svf/include/Util/SVFUtil.h | 6 +- svf/include/Util/ThreadAPI.h | 8 +- svf/lib/AE/Svfexe/AbsExtAPI.cpp | 3 +- svf/lib/AE/Svfexe/AbstractInterpretation.cpp | 7 +- svf/lib/Graphs/PTACallGraph.cpp | 3 +- svf/lib/Graphs/ThreadCallGraph.cpp | 15 +- svf/lib/Graphs/VFG.cpp | 2 +- svf/lib/MTA/MTAStat.cpp | 5 +- svf/lib/MemoryModel/PointerAnalysis.cpp | 3 +- svf/lib/MemoryModel/PointerAnalysisImpl.cpp | 10 +- svf/lib/SABER/SaberCondAllocator.cpp | 4 +- svf/lib/SABER/SaberSVFGBuilder.cpp | 9 +- svf/lib/SVFIR/SVFFileSystem.cpp | 2 + svf/lib/SVFIR/SVFIR.cpp | 18 ++- svf/lib/SVFIR/SVFVariables.cpp | 73 ++++++++- svf/lib/Util/CallGraphBuilder.cpp | 6 +- svf/lib/Util/SVFUtil.cpp | 7 + svf/lib/Util/ThreadAPI.cpp | 6 +- 26 files changed, 377 insertions(+), 100 deletions(-) diff --git a/svf-llvm/include/SVF-LLVM/LLVMModule.h b/svf-llvm/include/SVF-LLVM/LLVMModule.h index 7b5a81e38..1ef1184fb 100644 --- a/svf-llvm/include/SVF-LLVM/LLVMModule.h +++ b/svf-llvm/include/SVF-LLVM/LLVMModule.h @@ -54,6 +54,7 @@ class LLVMModuleSet typedef Map GlobalDefToRepMapTy; typedef Map LLVMFun2SVFFunMap; + typedef Map LLVMFun2CallGraphNodeMap; typedef Map LLVMBB2SVFBBMap; typedef Map LLVMInst2SVFInstMap; typedef Map LLVMArgument2SVFArgumentMap; @@ -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; @@ -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; @@ -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); diff --git a/svf-llvm/lib/LLVMModule.cpp b/svf-llvm/lib/LLVMModule.cpp index 5ecc89316..0bab01b66 100644 --- a/svf-llvm/lib/LLVMModule.cpp +++ b/svf-llvm/lib/LLVMModule.cpp @@ -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; @@ -174,6 +174,13 @@ void LLVMModuleSet::build() CallGraphBuilder callGraphBuilder; callgraph = callGraphBuilder.buildSVFIRCallGraph(svfModule); + + for (const auto& it : *callgraph) + { + addFunctionMap( + SVFUtil::cast(getLLVMValue(it.second->getFunction())), + it.second); + } } void LLVMModuleSet::createSVFDataStructure() @@ -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; diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index 2db52cf1b..f4eef2130 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -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" @@ -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(llvmModuleSet()->getLLVMValue( + it.second->getValue()))) + { + it.second->gNode = llvmModuleSet()->getCallGraphNode(func); } } @@ -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(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(llvmModuleSet()->getLLVMValue(iter->first))) + { + const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); + pag->addFuncValNode(cgn, iter->second, icfgNode); + } else { + pag->addValNode(iter->first, iter->second, icfgNode); + } } for (SymbolTableInfo::ValueToIDMapTy::iterator iter = @@ -235,7 +248,13 @@ 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); + CallGraphNode* pNode = nullptr; + if (const Function* func = SVFUtil::dyn_cast( + llvmModuleSet()->getLLVMValue(iter->first))) + { + pNode = llvmModuleSet()->getCallGraphNode(func); + } + pag->addObjNode(iter->first, pNode, iter->second); } for (SymbolTableInfo::FunToIDMapTy::iterator iter = @@ -243,7 +262,10 @@ void SVFIRBuilder::initialiseNodes() ++iter) { DBOUT(DPAGBuild, outs() << "add ret node " << iter->second << "\n"); - pag->addRetNode(iter->first, iter->second); + pag->addRetNode( + llvmModuleSet()->getCallGraphNode(SVFUtil::cast( + llvmModuleSet()->getLLVMValue(iter->first))), + iter->second); } for (SymbolTableInfo::FunToIDMapTy::iterator iter = @@ -251,7 +273,10 @@ void SVFIRBuilder::initialiseNodes() 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( + llvmModuleSet()->getLLVMValue(iter->first))), + iter->second); } /// add address edges for constant nodes. @@ -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(pag->getGNode(getValueNode(cs->getArgOperand(i))))); if(!cs->getType()->isVoidTy()) pag->addCallSiteRets(retBlockNode,pag->getGNode(getValueNode(cs))); @@ -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(edge) && !SVFUtil::isa(edge->getDstNode()->getValue())) + if(dstFun!=nullptr && !SVFUtil::isa(edge) && !SVFUtil::isa(edge->getDstNode())) { assert(dstFun==curInst->getFunction() && "DstNode of the PAGEdge not in the same function?"); } diff --git a/svf-llvm/lib/SVFIRExtAPI.cpp b/svf-llvm/lib/SVFIRExtAPI.cpp index 01930d41b..1ccdb82e8 100644 --- a/svf-llvm/lib/SVFIRExtAPI.cpp +++ b/svf-llvm/lib/SVFIRExtAPI.cpp @@ -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; @@ -256,9 +257,11 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle if (isThreadForkCall(callICFGNode)) { - if (const SVFFunction* forkedFun = SVFUtil::dyn_cast(getForkedFun(callICFGNode)->getValue())) + const ValVar* valVar = getForkedFun(callICFGNode); + if (const FuncValVar* funcValVar = SVFUtil::dyn_cast(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. diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index fafd1746e..b4e4b8e6d 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -161,6 +161,8 @@ class SVFBaseNode // │ ┌── ValVarKinds: Types of value variable nodes // │ │ ├── Represents a standard value variable ValNode, + // │ │ ├── Represents a Function value variable + FuncValNode, // │ │ ├── Represents a GEP value variable GepValNode, // │ │ ├── Represents a return value node @@ -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, + // │ ├──FuncObjNode: Types of function object + FuncObjNode, // │ └── DummyObjNode: Dummy node for uninitialized objects DummyObjNode, // └──────── @@ -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"); @@ -322,7 +326,7 @@ 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; @@ -330,12 +334,20 @@ class SVFBaseNode 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(FuncObjNode - FIObjNode == 1, + "The number of FIObjVarKinds has changed, make sure the " + "range is correct"); + return n <= FuncObjNode && n >= FIObjNode; + } + static inline bool isVFGNodeKinds(GNodeK n) { static_assert(MInterPhi - Cmp == 24, diff --git a/svf/include/Graphs/ICFGNode.h b/svf/include/Graphs/ICFGNode.h index 67fccfffe..033660187 100644 --- a/svf/include/Graphs/ICFGNode.h +++ b/svf/include/Graphs/ICFGNode.h @@ -425,7 +425,7 @@ class CallICFGNode : public InterICFGNode friend class SVFIRReader; public: - typedef std::vector ActualParmNodeVec; + typedef std::vector ActualParmNodeVec; protected: const RetICFGNode* ret; @@ -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]; } diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 96c174712..680393836 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -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); @@ -545,26 +545,36 @@ 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 addFuncValNode(const CallGraphNode* callGraphNode, NodeID i, const ICFGNode* icfgNode) { + FuncValVar* node = new FuncValVar(callGraphNode, i, icfgNode); + return addValNode(nullptr, node, i); + } + /// Add a memory obj node - inline NodeID addObjNode(const SVFValue* val, NodeID i) + inline NodeID addObjNode(const SVFValue* val, const CallGraphNode* callGraphNode, NodeID i) { const MemObj* mem = getMemObj(val); assert(mem->getId() == i && "not same object id?"); - return addFIObjNode(mem); + if(callGraphNode) + return addFuncObjNode(callGraphNode, mem); + else + return addFIObjNode(mem); } + /// 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); @@ -576,6 +586,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); + NodeID addFuncObjNode(const CallGraphNode* callGraphNode, const MemObj* obj); //@} /// Add a dummy value/object node according to node ID (llvm value is null) @@ -628,7 +639,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); } diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 01307c99b..46ceb7e03 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -87,8 +87,7 @@ class SVFVar : public GenericPAGNodeTy "dummy node do not have value!"); assert(!SymbolTableInfo::isBlkObjOrConstantObj(this->getId()) && "blackhole and constant obj do not have value"); - assert(value && - "value is null (GepObjNode whose basenode is a DummyObj?)"); + assert(value && "value is null (GepObjNode whose basenode is a DummyObj?)"); return value; } @@ -112,7 +111,7 @@ class SVFVar : public GenericPAGNodeTy bool isConstDataOrAggDataButNotNullPtr() const; /// Whether this is an isolated node on the SVFIR graph - bool isIsolatedNode() const; + virtual bool isIsolatedNode() const; /// Get name of the LLVM value // TODO: (Optimization) Should it return const reference instead of value? @@ -127,8 +126,6 @@ class SVFVar : public GenericPAGNodeTy return inst->getParent()->getParent(); else if (auto arg = SVFUtil::dyn_cast(value)) return arg->getParent(); - else if (auto fun = SVFUtil::dyn_cast(value)) - return fun; } return nullptr; } @@ -269,10 +266,10 @@ class ValVar: public SVFVar friend class SVFIRReader; private: - const SVFBaseNode* gNode; // constant, gepValvar, retPN, dummy could be null + const ICFGNode* icfgNode; // icfgnode related to valvar protected: /// Constructor to create an empty ValVar (for SVFIRReader/deserialization) - ValVar(NodeID i, PNODEK ty = ValNode) : SVFVar(i, ty), gNode(nullptr) {} + ValVar(NodeID i, PNODEK ty = ValNode) : SVFVar(i, ty), icfgNode(nullptr) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: @@ -296,8 +293,8 @@ class ValVar: public SVFVar //@} /// Constructor - ValVar(const SVFValue* val, NodeID i, PNODEK ty = ValNode, const SVFBaseNode* node = nullptr) - : SVFVar(val, i, ty), gNode(node) + ValVar(const SVFValue* val, NodeID i, PNODEK ty = ValNode, const ICFGNode* node = nullptr) + : SVFVar(val, i, ty), icfgNode(node) { } /// Return name of a LLVM value @@ -308,9 +305,9 @@ class ValVar: public SVFVar return ""; } - const SVFBaseNode* getGNode() const + const ICFGNode* getICFGNode() const { - return gNode; + return icfgNode; } virtual const std::string toString() const; @@ -540,7 +537,7 @@ class FIObjVar: public ObjVar friend class SVFIRWriter; friend class SVFIRReader; -private: +protected: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) FIObjVar(NodeID i, PNODEK ty = FIObjNode) : ObjVar(i, ty) {} @@ -553,19 +550,19 @@ class FIObjVar: public ObjVar } static inline bool classof(const ObjVar* node) { - return node->getNodeKind() == SVFVar::FIObjNode; + return isFIObjVarKinds(node->getNodeKind()); } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == SVFVar::FIObjNode; + return isFIObjVarKinds(node->getNodeKind()); } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == SVFVar::FIObjNode; + return isFIObjVarKinds(node->getNodeKind()); } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == SVFVar::FIObjNode; + return isFIObjVarKinds(node->getNodeKind()); } //@} @@ -587,6 +584,107 @@ class FIObjVar: public ObjVar virtual const std::string toString() const; }; +class CallGraphNode; + +class FuncValVar: public ValVar { + friend class SVFIRWriter; + friend class SVFIRReader; +private: + const CallGraphNode* callGraphNode; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FuncValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == FuncValNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == FuncValNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == FuncValNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == FuncValNode; + } + //@} + + inline const CallGraphNode* getCallGraphNode() const { + return callGraphNode; + } + + /// Constructor + FuncValVar(const CallGraphNode* cgn, NodeID i, const ICFGNode* icn, + PNODEK ty = FuncValNode) + : ValVar(nullptr, i, ty, icn), callGraphNode(cgn) + { + + } + + virtual const std::string toString() const; +}; + +class FuncObjVar: public FIObjVar { + friend class SVFIRWriter; + friend class SVFIRReader; + +private: + const CallGraphNode* callGraphNode; + +private: + /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) + FuncObjVar(NodeID i, PNODEK ty = FuncObjNode) : FIObjVar(i, ty) {} + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FuncObjVar*) + { + return true; + } + static inline bool classof(const FIObjVar* node) + { + return node->getNodeKind() == FuncObjNode; + } + static inline bool classof(const ObjVar* node) + { + return node->getNodeKind() == FuncObjNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == FuncObjNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == FuncObjNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == FuncObjNode; + } + //@} + + /// Constructor + FuncObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, + PNODEK ty = FuncObjNode); + + inline const CallGraphNode* getCallGraphNode() const { + return callGraphNode; + } + + virtual bool isIsolatedNode() const; + + virtual const std::string toString() const; +}; + /* * Unique Return node of a procedure */ @@ -595,6 +693,8 @@ class RetPN: public ValVar friend class SVFIRWriter; friend class SVFIRReader; +private: + const CallGraphNode* callGraphNode; private: /// Constructor to create empty RetPN (for SVFIRReader/deserialization) RetPN(NodeID i) : ValVar(i, RetNode) {} @@ -623,15 +723,19 @@ class RetPN: public ValVar } //@} + /// Constructor - RetPN(const SVFFunction* val, NodeID i) : ValVar(val, i, RetNode) {} + RetPN(const CallGraphNode* node, NodeID i); - /// Return name of a LLVM value - const std::string getValueName() const - { - return value->getName() + "_ret"; + inline const CallGraphNode* getCallGraphNode() const { + return callGraphNode; } + virtual const SVFFunction* getFunction() const; + + /// Return name of a LLVM value + const std::string getValueName() const; + virtual const std::string toString() const; }; @@ -642,6 +746,8 @@ class VarArgPN: public ValVar { friend class SVFIRWriter; friend class SVFIRReader; +private: + const CallGraphNode* callGraphNode; private: /// Constructor to create empty VarArgPN (for SVFIRReader/deserialization) @@ -672,13 +778,12 @@ class VarArgPN: public ValVar //@} /// Constructor - VarArgPN(const SVFFunction* val, NodeID i) : ValVar(val, i, VarargNode) {} + VarArgPN(const CallGraphNode* node, NodeID i) : ValVar(nullptr, i, VarargNode), callGraphNode(node) {} + + virtual const SVFFunction* getFunction() const; /// Return name of a LLVM value - inline const std::string getValueName() const - { - return value->getName() + "_vararg"; - } + const std::string getValueName() const; virtual const std::string toString() const; }; diff --git a/svf/include/Util/SVFUtil.h b/svf/include/Util/SVFUtil.h index a48387692..3ec67b1e2 100644 --- a/svf/include/Util/SVFUtil.h +++ b/svf/include/Util/SVFUtil.h @@ -351,9 +351,11 @@ inline bool isArgOfUncalledFunction(const SVFValue* svfval) return false; } +const ObjVar* getObjVarOfValVar(const ValVar* valVar); + /// Return thread fork function //@{ -inline const SVFVar* getForkedFun(const CallICFGNode *inst) +inline const ValVar* getForkedFun(const CallICFGNode *inst) { return ThreadAPI::getThreadAPI()->getForkedFun(inst); } @@ -432,7 +434,7 @@ inline bool isBarrierWaitCall(const CallICFGNode* cs) /// Return sole argument of the thread routine //@{ -inline const SVFVar* getActualParmAtForkSite(const CallICFGNode* cs) +inline const ValVar* getActualParmAtForkSite(const CallICFGNode* cs) { return ThreadAPI::getThreadAPI()->getActualParmAtForkSite(cs); } diff --git a/svf/include/Util/ThreadAPI.h b/svf/include/Util/ThreadAPI.h index 356bde4db..3acb042b5 100644 --- a/svf/include/Util/ThreadAPI.h +++ b/svf/include/Util/ThreadAPI.h @@ -39,6 +39,8 @@ class SVFModule; class ICFGNode; class CallICFGNode; class SVFVar; +class ValVar; +class ObjVar; /* * ThreadAPI class contains interfaces for pthread programs @@ -124,14 +126,14 @@ class ThreadAPI //@{ /// Return the first argument of the call, /// Note that, it is the pthread_t pointer - const SVFVar* getForkedThread(const CallICFGNode *inst) const; + const ValVar* getForkedThread(const CallICFGNode *inst) const; /// Return the third argument of the call, /// Note that, it could be function type or a void* pointer - const SVFVar* getForkedFun(const CallICFGNode *inst) const; + const ValVar* getForkedFun(const CallICFGNode *inst) const; /// Return the forth argument of the call, /// Note that, it is the sole argument of start routine ( a void* pointer ) - const SVFVar* getActualParmAtForkSite(const CallICFGNode *inst) const; + const ValVar* getActualParmAtForkSite(const CallICFGNode *inst) const; /// Return the formal parm of forked function (the first arg in pthread) const SVFVar* getFormalParmOfForkedFun(const SVFFunction* F) const; diff --git a/svf/lib/AE/Svfexe/AbsExtAPI.cpp b/svf/lib/AE/Svfexe/AbsExtAPI.cpp index bdd11e357..2260d3fba 100644 --- a/svf/lib/AE/Svfexe/AbsExtAPI.cpp +++ b/svf/lib/AE/Svfexe/AbsExtAPI.cpp @@ -130,8 +130,7 @@ void AbsExtAPI::initExtFunMap() assert(lb.getInterval().is_numeral() && ub.getInterval().is_numeral()); num.getInterval().set_to_top(); num.getInterval().meet_with(IntervalValue(lb.getInterval().lb(), ub.getInterval().ub())); - const ICFGNode* node = SVFUtil::cast( - SVFUtil::cast(callNode->getArgument(0))->getGNode()); + const ICFGNode* node = SVFUtil::cast(callNode->getArgument(0))->getICFGNode(); for (const SVFStmt* stmt: node->getSVFStmts()) { if (SVFUtil::isa(stmt)) diff --git a/svf/lib/AE/Svfexe/AbstractInterpretation.cpp b/svf/lib/AE/Svfexe/AbstractInterpretation.cpp index 8ea21a40a..318c41e9c 100644 --- a/svf/lib/AE/Svfexe/AbstractInterpretation.cpp +++ b/svf/lib/AE/Svfexe/AbstractInterpretation.cpp @@ -30,6 +30,7 @@ #include "SVFIR/SVFIR.h" #include "Util/Options.h" #include "Util/WorkList.h" +#include "Graphs/CallGraph.h" #include using namespace SVF; @@ -646,9 +647,9 @@ void AbstractInterpretation::indirectCallFunPass(const SVF::CallICFGNode *callNo AbstractValue Addrs = as[call_id]; NodeID addr = *Addrs.getAddrs().begin(); SVFVar *func_var = svfir->getGNode(AbstractState::getInternalID(addr)); - const SVFFunction *callfun = SVFUtil::dyn_cast(func_var->getValue()); - if (callfun) + if(const FuncObjVar *funObjVar = SVFUtil::dyn_cast(func_var)) { + const SVFFunction* callfun = funObjVar->getCallGraphNode()->getFunction(); callSiteStack.push_back(callNode); abstractTrace[callNode] = as; @@ -656,7 +657,7 @@ void AbstractInterpretation::indirectCallFunPass(const SVF::CallICFGNode *callNo handleWTOComponents(wto->getWTOComponents()); callSiteStack.pop_back(); // handle Ret node - const RetICFGNode *retNode = callNode->getRetICFGNode(); + const RetICFGNode* retNode = callNode->getRetICFGNode(); abstractTrace[retNode] = abstractTrace[callNode]; } } diff --git a/svf/lib/Graphs/PTACallGraph.cpp b/svf/lib/Graphs/PTACallGraph.cpp index 7c30f3828..60e4c384b 100644 --- a/svf/lib/Graphs/PTACallGraph.cpp +++ b/svf/lib/Graphs/PTACallGraph.cpp @@ -52,7 +52,8 @@ void PTACallGraphEdge::addDirectCallSite(const CallICFGNode* call) void PTACallGraphEdge::addInDirectCallSite(const CallICFGNode* call) { - assert((nullptr == call->getCalledFunction() || nullptr == SVFUtil::dyn_cast (SVFUtil::getForkedFun(call)->getValue())) && "not an indirect callsite??"); + assert((nullptr == call->getCalledFunction() || !SVFUtil::isa(SVFUtil::getForkedFun(call))) && + "not an indirect callsite??"); indirectCalls.insert(call); } //@} diff --git a/svf/lib/Graphs/ThreadCallGraph.cpp b/svf/lib/Graphs/ThreadCallGraph.cpp index 470293c52..2394d45bc 100644 --- a/svf/lib/Graphs/ThreadCallGraph.cpp +++ b/svf/lib/Graphs/ThreadCallGraph.cpp @@ -32,6 +32,7 @@ #include "Util/ThreadAPI.h" #include "SVFIR/SVFIR.h" #include "MemoryModel/PointerAnalysisImpl.h" +#include "Graphs/CallGraph.h" using namespace SVF; using namespace SVFUtil; @@ -73,8 +74,8 @@ void ThreadCallGraph::updateCallGraph(PointerAnalysis* pta) // Fork sites for (CallSiteSet::const_iterator it = forksitesBegin(), eit = forksitesEnd(); it != eit; ++it) { - const SVFVar* forkedval = tdAPI->getForkedFun(*it); - if(SVFUtil::dyn_cast(forkedval->getValue())==nullptr) + const ValVar* forkedval = tdAPI->getForkedFun(*it); + if(SVFUtil::dyn_cast(forkedval)==nullptr) { SVFIR* pag = pta->getPAG(); const NodeBS targets = pta->getPts(forkedval->getId()).toNodeBS(); @@ -85,7 +86,7 @@ void ThreadCallGraph::updateCallGraph(PointerAnalysis* pta) const MemObj* obj = pag->getObject(objPN); if(obj->isFunction()) { - const SVFFunction* svfCallee = SVFUtil::cast(obj->getValue()); + const SVFFunction* svfCallee = SVFUtil::cast(obj->getGNode())->getFunction(); this->addIndirectForkEdge(*it, svfCallee); } } @@ -126,7 +127,8 @@ bool ThreadCallGraph::addDirectForkEdge(const CallICFGNode* cs) { PTACallGraphNode* caller = getCallGraphNode(cs->getCaller()); - const SVFFunction* forkee = SVFUtil::dyn_cast(tdAPI->getForkedFun(cs)->getValue()); + const SVFFunction* forkee = SVFUtil::dyn_cast(tdAPI->getForkedFun(cs)) + ->getCallGraphNode()->getFunction(); assert(forkee && "callee does not exist"); PTACallGraphNode* callee = getCallGraphNode(forkee->getDefFunForMultipleModule()); CallSiteID csId = addCallSite(cs, callee->getFunction()); @@ -185,7 +187,10 @@ void ThreadCallGraph::addDirectJoinEdge(const CallICFGNode* cs,const CallSiteSet for (CallSiteSet::const_iterator it = forkset.begin(), eit = forkset.end(); it != eit; ++it) { - const SVFFunction* threadRoutineFun = SVFUtil::dyn_cast(tdAPI->getForkedFun(*it)->getValue()); + const SVFFunction* threadRoutineFun = + SVFUtil::dyn_cast(tdAPI->getForkedFun(*it)) + ->getCallGraphNode() + ->getFunction(); assert(threadRoutineFun && "thread routine function does not exist"); PTACallGraphNode* threadRoutineFunNode = getCallGraphNode(threadRoutineFun); CallSiteID csId = addCallSite(cs, threadRoutineFun); diff --git a/svf/lib/Graphs/VFG.cpp b/svf/lib/Graphs/VFG.cpp index c4609c97f..dd0924e82 100644 --- a/svf/lib/Graphs/VFG.cpp +++ b/svf/lib/Graphs/VFG.cpp @@ -1090,7 +1090,7 @@ const SVFValue* BinaryOPVFGNode::getValue() const const SVFValue* PHIVFGNode::getValue() const { - return getRes()->getValue(); + return getRes()->hasValue() ? getRes()->getValue(): nullptr; } const SVFValue* ArgumentVFGNode::getValue() const diff --git a/svf/lib/MTA/MTAStat.cpp b/svf/lib/MTA/MTAStat.cpp index 9abdedaf6..153508f7e 100644 --- a/svf/lib/MTA/MTAStat.cpp +++ b/svf/lib/MTA/MTAStat.cpp @@ -33,6 +33,7 @@ #include "MTA/MHP.h" #include "MTA/LockAnalysis.h" #include "Graphs/ThreadCallGraph.h" +#include "Graphs/CallGraph.h" using namespace SVF; @@ -49,8 +50,8 @@ void MTAStat::performThreadCallGraphStat(ThreadCallGraph* tcg) for (ThreadCallGraph::CallSiteSet::const_iterator it = tcg->forksitesBegin(), eit = tcg->forksitesEnd(); it != eit; ++it) { bool indirectfork = false; - const SVFFunction* spawnee = SVFUtil::dyn_cast(tcg->getThreadAPI()->getForkedFun(*it)->getValue()); - if(spawnee==nullptr) + const ValVar* pValVar = tcg->getThreadAPI()->getForkedFun(*it); + if(!SVFUtil::isa(pValVar)) { numOfIndForksite++; indirectfork = true; diff --git a/svf/lib/MemoryModel/PointerAnalysis.cpp b/svf/lib/MemoryModel/PointerAnalysis.cpp index 9f4a8122a..3d292fba5 100644 --- a/svf/lib/MemoryModel/PointerAnalysis.cpp +++ b/svf/lib/MemoryModel/PointerAnalysis.cpp @@ -36,6 +36,7 @@ #include "Util/PTAStat.h" #include "Graphs/ThreadCallGraph.h" #include "Graphs/ICFG.h" +#include "Graphs/CallGraph.h" #include "Util/CallGraphBuilder.h" #include @@ -399,7 +400,7 @@ void PointerAnalysis::resolveIndCalls(const CallICFGNode* cs, const PointsTo& ta if(obj->isFunction()) { - const SVFFunction* calleefun = SVFUtil::cast(obj->getValue()); + const SVFFunction* calleefun = SVFUtil::cast(obj->getGNode())->getFunction(); const SVFFunction* callee = calleefun->getDefFunForMultipleModule(); if(SVFUtil::matchArgs(cs, callee) == false) diff --git a/svf/lib/MemoryModel/PointerAnalysisImpl.cpp b/svf/lib/MemoryModel/PointerAnalysisImpl.cpp index 8e57096b8..236b1bb27 100644 --- a/svf/lib/MemoryModel/PointerAnalysisImpl.cpp +++ b/svf/lib/MemoryModel/PointerAnalysisImpl.cpp @@ -34,6 +34,8 @@ #include #include +#include "Graphs/CallGraph.h" + using namespace SVF; using namespace SVFUtil; using namespace std; @@ -521,11 +523,11 @@ void BVDataPTAImpl::onTheFlyThreadCallGraphSolve(const CallSiteToFunPtrMap& call for(CallSiteSet::const_iterator it = tdCallGraph->forksitesBegin(), eit = tdCallGraph->forksitesEnd(); it != eit; ++it) { - const SVFValue* forkedVal =tdCallGraph->getThreadAPI()->getForkedFun(*it)->getValue(); - if(SVFUtil::dyn_cast(forkedVal) == nullptr) + const ValVar* pVar = tdCallGraph->getThreadAPI()->getForkedFun(*it); + if(SVFUtil::dyn_cast(pVar) == nullptr) { SVFIR *pag = this->getPAG(); - const NodeBS targets = this->getPts(pag->getValueNode(forkedVal)).toNodeBS(); + const NodeBS targets = this->getPts(pVar->getId()).toNodeBS(); for(NodeBS::iterator ii = targets.begin(), ie = targets.end(); ii != ie; ++ii) { if(ObjVar *objPN = SVFUtil::dyn_cast(pag->getGNode(*ii))) @@ -533,7 +535,7 @@ void BVDataPTAImpl::onTheFlyThreadCallGraphSolve(const CallSiteToFunPtrMap& call const MemObj *obj = pag->getObject(objPN); if(obj->isFunction()) { - const SVFFunction *svfForkedFun = SVFUtil::cast(obj->getValue()); + const SVFFunction *svfForkedFun = SVFUtil::cast(obj->getGNode())->getFunction(); if(tdCallGraph->addIndirectForkEdge(*it, svfForkedFun)) newForkEdges[*it].insert(svfForkedFun); } diff --git a/svf/lib/SABER/SaberCondAllocator.cpp b/svf/lib/SABER/SaberCondAllocator.cpp index c6d989aad..082d7a52b 100644 --- a/svf/lib/SABER/SaberCondAllocator.cpp +++ b/svf/lib/SABER/SaberCondAllocator.cpp @@ -195,7 +195,7 @@ SaberCondAllocator::evaluateTestNullLikeExpr(const BranchStmt *branchStmt, const // br i1 false, label %44, label %75, !dbg !7669 { "ln": 2033, "cl": 7, "fl": "re_lexer.c" } return Condition::nullExpr(); } - if (isTestNullExpr(SVFUtil::cast(condVar->getGNode()))) + if (isTestNullExpr(SVFUtil::cast(condVar->getICFGNode()))) { // succ is then branch if (succ1 == succ) @@ -204,7 +204,7 @@ SaberCondAllocator::evaluateTestNullLikeExpr(const BranchStmt *branchStmt, const else return getTrueCond(); } - if (isTestNotNullExpr(SVFUtil::cast(condVar->getGNode()))) + if (isTestNotNullExpr(condVar->getICFGNode())) { // succ is then branch if (succ1 == succ) diff --git a/svf/lib/SABER/SaberSVFGBuilder.cpp b/svf/lib/SABER/SaberSVFGBuilder.cpp index b883adbe6..f90d86e36 100644 --- a/svf/lib/SABER/SaberSVFGBuilder.cpp +++ b/svf/lib/SABER/SaberSVFGBuilder.cpp @@ -82,11 +82,8 @@ void SaberSVFGBuilder::collectGlobals(BVDataPTAImpl* pta) if(SVFUtil::isa(pag->getGNode(gepobj->getBaseNode()))) continue; } - if(const SVFValue* val = pagNode->getValue()) - { - if(SVFUtil::isa(val)) - worklist.push_back(it->first); - } + if(pagNode->hasValue() && SVFUtil::isa(pagNode->getValue())) + worklist.push_back(it->first); } NodeToPTSSMap cachedPtsMap; @@ -138,7 +135,7 @@ PointsTo& SaberSVFGBuilder::CollectPtsChain(BVDataPTAImpl* pta, NodeID id, NodeT if(pta->isFIObjNode(baseId) && pag->getGNode(baseId)->hasValue()) { ValVar* valVar = SVFUtil::dyn_cast(pag->getGNode(baseId)); - if(valVar && valVar->getGNode() && SVFUtil::isExtCall(SVFUtil::cast(valVar->getGNode()))) + if(valVar && valVar->getICFGNode() && SVFUtil::isExtCall(valVar->getICFGNode())) { return pts; } diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index 0dfe58ac9..bbdc74b65 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -224,6 +224,8 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(FIObjNode, FIObjVar); CASE(DummyValNode, DummyValVar); CASE(DummyObjNode, DummyObjVar); + CASE(FuncObjNode, FuncObjVar); + CASE(FuncValNode, FuncValVar); #undef CASE } } diff --git a/svf/lib/SVFIR/SVFIR.cpp b/svf/lib/SVFIR/SVFIR.cpp index b79145c27..0e4e28d6b 100644 --- a/svf/lib/SVFIR/SVFIR.cpp +++ b/svf/lib/SVFIR/SVFIR.cpp @@ -472,6 +472,15 @@ NodeID SVFIR::addFIObjNode(const MemObj* obj) return addObjNode(obj->getValue(), node, obj->getId()); } +NodeID SVFIR::addFuncObjNode(const CallGraphNode* callGraphNode, const MemObj* obj) +{ + //assert(findPAGNode(i) == false && "this node should not be created before"); + NodeID base = obj->getId(); + memToFieldsMap[base].set(obj->getId()); + FuncObjVar *node = new FuncObjVar(callGraphNode, obj->getId(), obj); + return addObjNode(obj->getValue(), node, obj->getId()); +} + /*! * Get all fields object nodes of an object */ @@ -675,9 +684,14 @@ bool SVFIR::isValidTopLevelPtr(const SVFVar* node) { if (SVFUtil::isa(node)) { - if (isValidPointer(node->getId()) && node->hasValue()) + if (isValidPointer(node->getId())) { - return !SVFUtil::isArgOfUncalledFunction(node->getValue()); + // TODO: after svf value is removed, we use type to determine top level ptr + if (SVFUtil::isa(node) || SVFUtil::isa(node) || SVFUtil::isa(node)) + { + return true; + } else if(node->hasValue()) + return !SVFUtil::isArgOfUncalledFunction(node->getValue()); } } return false; diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 72b406e1f..126be1c22 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -30,6 +30,7 @@ #include "SVFIR/SVFVariables.h" #include "Util/Options.h" #include "Util/SVFUtil.h" +#include "Graphs/CallGraph.h" using namespace SVF; using namespace SVFUtil; @@ -52,11 +53,12 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : break; } case RetNode: + case FuncObjNode: { - assert(val != nullptr && "value is nullptr for RetNode"); - isPtr = SVFUtil::cast(val)->getReturnType()->isPointerTy(); + // to be completed in derived class break; } + case FuncValNode: case VarargNode: case DummyValNode: { @@ -85,8 +87,6 @@ bool SVFVar::isIsolatedNode() const return true; else if (isConstDataOrAggDataButNotNullPtr()) return true; - else if (value && SVFUtil::isa(value)) - return SVFUtil::cast(value)->isIntrinsic(); else return false; } @@ -144,6 +144,20 @@ const std::string GepValVar::toString() const return rawstr.str(); } +RetPN::RetPN(const CallGraphNode* node, NodeID i) : ValVar(i, RetNode), callGraphNode(node) { + isPtr = node->getFunction()->getReturnType()->isPointerTy(); +} + +const SVFFunction* RetPN::getFunction() const +{ + return callGraphNode->getFunction(); +} + +const std::string RetPN::getValueName() const +{ + return callGraphNode->getName() + "_ret"; +} + const std::string GepObjVar::toString() const { std::string str; @@ -170,19 +184,66 @@ const std::string FIObjVar::toString() const return rawstr.str(); } +const std::string FuncValVar::toString() const +{ + std::string str; + std::stringstream rawstr(str); + rawstr << "FuncValVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << callGraphNode->getName(); + } + return rawstr.str(); +} + +FuncObjVar::FuncObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, + PNODEK ty) + : FIObjVar(nullptr, i, mem, ty), callGraphNode(cgNode) +{ + isPtr = callGraphNode->getFunction()->getType()->isPointerTy(); +} + +bool FuncObjVar::isIsolatedNode() const +{ + return callGraphNode->getFunction()->isIntrinsic(); +} + +const std::string FuncObjVar::toString() const +{ + std::string str; + std::stringstream rawstr(str); + rawstr << "FuncObjVar ID: " << getId() << " (base object)"; + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << callGraphNode->getName(); + } + return rawstr.str(); +} + const std::string RetPN::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "RetPN ID: " << getId() << " unique return node for function " << SVFUtil::cast(value)->getName(); + rawstr << "RetPN ID: " << getId() << " unique return node for function " << callGraphNode->getName(); return rawstr.str(); } +const SVFFunction* VarArgPN::getFunction() const { + return callGraphNode->getFunction(); +} + +const std::string VarArgPN::getValueName() const +{ + return callGraphNode->getName() + "_vararg"; +} + const std::string VarArgPN::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "VarArgPN ID: " << getId() << " Var arg node for function " << SVFUtil::cast(value)->getName(); + rawstr << "VarArgPN ID: " << getId() << " Var arg node for function " << callGraphNode->getName(); return rawstr.str(); } diff --git a/svf/lib/Util/CallGraphBuilder.cpp b/svf/lib/Util/CallGraphBuilder.cpp index ba01d4d1f..5cc6446a0 100644 --- a/svf/lib/Util/CallGraphBuilder.cpp +++ b/svf/lib/Util/CallGraphBuilder.cpp @@ -88,15 +88,15 @@ ThreadCallGraph* CallGraphBuilder::buildThreadCallGraph() { const CallICFGNode* cs = cast(inst); cg->addForksite(cs); - const SVFFunction* forkee = SVFUtil::dyn_cast(tdAPI->getForkedFun(cs)->getValue()); - if (forkee) + const ValVar* svfVar = tdAPI->getForkedFun(cs); + if (SVFUtil::isa(svfVar)) { cg->addDirectForkEdge(cs); } // indirect call to the start routine function else { - cg->addThreadForkEdgeSetMap(cs,nullptr); + cg->addThreadForkEdgeSetMap(cs, nullptr); } } } diff --git a/svf/lib/Util/SVFUtil.cpp b/svf/lib/Util/SVFUtil.cpp index bf7282945..ecaed39fe 100644 --- a/svf/lib/Util/SVFUtil.cpp +++ b/svf/lib/Util/SVFUtil.cpp @@ -423,4 +423,11 @@ const SVFFunction* SVFUtil::getProgEntryFunction() return (fun->getFunction()); } return nullptr; +} + + +const ObjVar* SVFUtil::getObjVarOfValVar(const SVF::ValVar* valVar) +{ + assert(valVar->getInEdges().size() == 1); + return SVFUtil::dyn_cast((*valVar->getInEdges().begin())->getSrcNode()); } \ No newline at end of file diff --git a/svf/lib/Util/ThreadAPI.cpp b/svf/lib/Util/ThreadAPI.cpp index 6e5bbbf9c..2cf335753 100644 --- a/svf/lib/Util/ThreadAPI.cpp +++ b/svf/lib/Util/ThreadAPI.cpp @@ -161,13 +161,13 @@ bool ThreadAPI::isTDBarWait(const CallICFGNode *inst) const } -const SVFVar* ThreadAPI::getForkedThread(const CallICFGNode *inst) const +const ValVar* ThreadAPI::getForkedThread(const CallICFGNode *inst) const { assert(isTDFork(inst) && "not a thread fork function!"); return inst->getArgument(0); } -const SVFVar* ThreadAPI::getForkedFun(const CallICFGNode *inst) const +const ValVar* ThreadAPI::getForkedFun(const CallICFGNode *inst) const { assert(isTDFork(inst) && "not a thread fork function!"); return inst->getArgument(2); @@ -175,7 +175,7 @@ const SVFVar* ThreadAPI::getForkedFun(const CallICFGNode *inst) const /// Return the forth argument of the call, /// Note that, it is the sole argument of start routine ( a void* pointer ) -const SVFVar* ThreadAPI::getActualParmAtForkSite(const CallICFGNode *inst) const +const ValVar* ThreadAPI::getActualParmAtForkSite(const CallICFGNode *inst) const { assert(isTDFork(inst) && "not a thread fork function!"); return inst->getArgument(3); From 237c99184d98d332c4fa6cc9aedb958c7c16ab87 Mon Sep 17 00:00:00 2001 From: jumormt Date: Mon, 25 Nov 2024 22:31:44 +1100 Subject: [PATCH 02/28] rename --- svf-llvm/lib/SVFIRExtAPI.cpp | 2 +- svf/include/Graphs/GenericGraph.h | 10 +++--- svf/include/SVFIR/SVFIR.h | 2 +- svf/include/SVFIR/SVFVariables.h | 36 ++++++++++---------- svf/lib/AE/Svfexe/AbstractInterpretation.cpp | 2 +- svf/lib/Graphs/PTACallGraph.cpp | 2 +- svf/lib/Graphs/ThreadCallGraph.cpp | 6 ++-- svf/lib/MTA/MTAStat.cpp | 2 +- svf/lib/MemoryModel/PointerAnalysisImpl.cpp | 2 +- svf/lib/SVFIR/SVFFileSystem.cpp | 4 +-- svf/lib/SVFIR/SVFIR.cpp | 4 +-- svf/lib/SVFIR/SVFVariables.cpp | 16 ++++----- svf/lib/Util/CallGraphBuilder.cpp | 2 +- 13 files changed, 45 insertions(+), 45 deletions(-) diff --git a/svf-llvm/lib/SVFIRExtAPI.cpp b/svf-llvm/lib/SVFIRExtAPI.cpp index 1ccdb82e8..8c65a9083 100644 --- a/svf-llvm/lib/SVFIRExtAPI.cpp +++ b/svf-llvm/lib/SVFIRExtAPI.cpp @@ -258,7 +258,7 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle if (isThreadForkCall(callICFGNode)) { const ValVar* valVar = getForkedFun(callICFGNode); - if (const FuncValVar* funcValVar = SVFUtil::dyn_cast(valVar)) + if (const FunValVar* funcValVar = SVFUtil::dyn_cast(valVar)) { const SVFFunction* forkedFun = funcValVar->getCallGraphNode()->getFunction() ->getDefFunForMultipleModule(); diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index b4e4b8e6d..87fb354b3 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -162,7 +162,7 @@ class SVFBaseNode // │ │ ├── Represents a standard value variable ValNode, // │ │ ├── Represents a Function value variable - FuncValNode, + FunValNode, // │ │ ├── Represents a GEP value variable GepValNode, // │ │ ├── Represents a return value node @@ -178,8 +178,8 @@ class SVFBaseNode GepObjNode, // │ └── FIObjNode: Represents a flow-insensitive object node FIObjNode, - // │ ├──FuncObjNode: Types of function object - FuncObjNode, + // │ ├──FunObjNode: Types of function object + FunObjNode, // │ └── DummyObjNode: Dummy node for uninitialized objects DummyObjNode, // └──────── @@ -342,10 +342,10 @@ class SVFBaseNode static inline bool isFIObjVarKinds(GNodeK n) { - static_assert(FuncObjNode - FIObjNode == 1, + static_assert(FunObjNode - FIObjNode == 1, "The number of FIObjVarKinds has changed, make sure the " "range is correct"); - return n <= FuncObjNode && n >= FIObjNode; + return n <= FunObjNode && n >= FIObjNode; } static inline bool isVFGNodeKinds(GNodeK n) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 680393836..1bc218341 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -552,7 +552,7 @@ class SVFIR : public IRGraph } NodeID addFuncValNode(const CallGraphNode* callGraphNode, NodeID i, const ICFGNode* icfgNode) { - FuncValVar* node = new FuncValVar(callGraphNode, i, icfgNode); + FunValVar* node = new FunValVar(callGraphNode, i, icfgNode); return addValNode(nullptr, node, i); } diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 46ceb7e03..173fbe90e 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -586,7 +586,7 @@ class FIObjVar: public ObjVar class CallGraphNode; -class FuncValVar: public ValVar { +class FunValVar : public ValVar { friend class SVFIRWriter; friend class SVFIRReader; private: @@ -595,25 +595,25 @@ class FuncValVar: public ValVar { public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FuncValVar*) + static inline bool classof(const FunValVar*) { return true; } static inline bool classof(const ValVar* node) { - return node->getNodeKind() == FuncValNode; + return node->getNodeKind() == FunValNode; } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == FuncValNode; + return node->getNodeKind() == FunValNode; } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == FuncValNode; + return node->getNodeKind() == FunValNode; } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == FuncValNode; + return node->getNodeKind() == FunValNode; } //@} @@ -622,8 +622,8 @@ class FuncValVar: public ValVar { } /// Constructor - FuncValVar(const CallGraphNode* cgn, NodeID i, const ICFGNode* icn, - PNODEK ty = FuncValNode) + FunValVar(const CallGraphNode* cgn, NodeID i, const ICFGNode* icn, + PNODEK ty = FunValNode) : ValVar(nullptr, i, ty, icn), callGraphNode(cgn) { @@ -632,7 +632,7 @@ class FuncValVar: public ValVar { virtual const std::string toString() const; }; -class FuncObjVar: public FIObjVar { +class FunObjVar : public FIObjVar { friend class SVFIRWriter; friend class SVFIRReader; @@ -641,40 +641,40 @@ class FuncObjVar: public FIObjVar { private: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - FuncObjVar(NodeID i, PNODEK ty = FuncObjNode) : FIObjVar(i, ty) {} + FunObjVar(NodeID i, PNODEK ty = FunObjNode) : FIObjVar(i, ty) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FuncObjVar*) + static inline bool classof(const FunObjVar*) { return true; } static inline bool classof(const FIObjVar* node) { - return node->getNodeKind() == FuncObjNode; + return node->getNodeKind() == FunObjNode; } static inline bool classof(const ObjVar* node) { - return node->getNodeKind() == FuncObjNode; + return node->getNodeKind() == FunObjNode; } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == FuncObjNode; + return node->getNodeKind() == FunObjNode; } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == FuncObjNode; + return node->getNodeKind() == FunObjNode; } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == FuncObjNode; + return node->getNodeKind() == FunObjNode; } //@} /// Constructor - FuncObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, - PNODEK ty = FuncObjNode); + FunObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, + PNODEK ty = FunObjNode); inline const CallGraphNode* getCallGraphNode() const { return callGraphNode; diff --git a/svf/lib/AE/Svfexe/AbstractInterpretation.cpp b/svf/lib/AE/Svfexe/AbstractInterpretation.cpp index 318c41e9c..4e34e4d47 100644 --- a/svf/lib/AE/Svfexe/AbstractInterpretation.cpp +++ b/svf/lib/AE/Svfexe/AbstractInterpretation.cpp @@ -647,7 +647,7 @@ void AbstractInterpretation::indirectCallFunPass(const SVF::CallICFGNode *callNo AbstractValue Addrs = as[call_id]; NodeID addr = *Addrs.getAddrs().begin(); SVFVar *func_var = svfir->getGNode(AbstractState::getInternalID(addr)); - if(const FuncObjVar *funObjVar = SVFUtil::dyn_cast(func_var)) + if(const FunObjVar*funObjVar = SVFUtil::dyn_cast(func_var)) { const SVFFunction* callfun = funObjVar->getCallGraphNode()->getFunction(); callSiteStack.push_back(callNode); diff --git a/svf/lib/Graphs/PTACallGraph.cpp b/svf/lib/Graphs/PTACallGraph.cpp index 60e4c384b..7d24d7644 100644 --- a/svf/lib/Graphs/PTACallGraph.cpp +++ b/svf/lib/Graphs/PTACallGraph.cpp @@ -52,7 +52,7 @@ void PTACallGraphEdge::addDirectCallSite(const CallICFGNode* call) void PTACallGraphEdge::addInDirectCallSite(const CallICFGNode* call) { - assert((nullptr == call->getCalledFunction() || !SVFUtil::isa(SVFUtil::getForkedFun(call))) && + assert((nullptr == call->getCalledFunction() || !SVFUtil::isa(SVFUtil::getForkedFun(call))) && "not an indirect callsite??"); indirectCalls.insert(call); } diff --git a/svf/lib/Graphs/ThreadCallGraph.cpp b/svf/lib/Graphs/ThreadCallGraph.cpp index 2394d45bc..3f924b42e 100644 --- a/svf/lib/Graphs/ThreadCallGraph.cpp +++ b/svf/lib/Graphs/ThreadCallGraph.cpp @@ -75,7 +75,7 @@ void ThreadCallGraph::updateCallGraph(PointerAnalysis* pta) for (CallSiteSet::const_iterator it = forksitesBegin(), eit = forksitesEnd(); it != eit; ++it) { const ValVar* forkedval = tdAPI->getForkedFun(*it); - if(SVFUtil::dyn_cast(forkedval)==nullptr) + if(SVFUtil::dyn_cast(forkedval)==nullptr) { SVFIR* pag = pta->getPAG(); const NodeBS targets = pta->getPts(forkedval->getId()).toNodeBS(); @@ -127,7 +127,7 @@ bool ThreadCallGraph::addDirectForkEdge(const CallICFGNode* cs) { PTACallGraphNode* caller = getCallGraphNode(cs->getCaller()); - const SVFFunction* forkee = SVFUtil::dyn_cast(tdAPI->getForkedFun(cs)) + const SVFFunction* forkee = SVFUtil::dyn_cast(tdAPI->getForkedFun(cs)) ->getCallGraphNode()->getFunction(); assert(forkee && "callee does not exist"); PTACallGraphNode* callee = getCallGraphNode(forkee->getDefFunForMultipleModule()); @@ -188,7 +188,7 @@ void ThreadCallGraph::addDirectJoinEdge(const CallICFGNode* cs,const CallSiteSet { const SVFFunction* threadRoutineFun = - SVFUtil::dyn_cast(tdAPI->getForkedFun(*it)) + SVFUtil::dyn_cast(tdAPI->getForkedFun(*it)) ->getCallGraphNode() ->getFunction(); assert(threadRoutineFun && "thread routine function does not exist"); diff --git a/svf/lib/MTA/MTAStat.cpp b/svf/lib/MTA/MTAStat.cpp index 153508f7e..1f6c69456 100644 --- a/svf/lib/MTA/MTAStat.cpp +++ b/svf/lib/MTA/MTAStat.cpp @@ -51,7 +51,7 @@ void MTAStat::performThreadCallGraphStat(ThreadCallGraph* tcg) { bool indirectfork = false; const ValVar* pValVar = tcg->getThreadAPI()->getForkedFun(*it); - if(!SVFUtil::isa(pValVar)) + if(!SVFUtil::isa(pValVar)) { numOfIndForksite++; indirectfork = true; diff --git a/svf/lib/MemoryModel/PointerAnalysisImpl.cpp b/svf/lib/MemoryModel/PointerAnalysisImpl.cpp index 236b1bb27..0d47c1567 100644 --- a/svf/lib/MemoryModel/PointerAnalysisImpl.cpp +++ b/svf/lib/MemoryModel/PointerAnalysisImpl.cpp @@ -524,7 +524,7 @@ void BVDataPTAImpl::onTheFlyThreadCallGraphSolve(const CallSiteToFunPtrMap& call eit = tdCallGraph->forksitesEnd(); it != eit; ++it) { const ValVar* pVar = tdCallGraph->getThreadAPI()->getForkedFun(*it); - if(SVFUtil::dyn_cast(pVar) == nullptr) + if(SVFUtil::dyn_cast(pVar) == nullptr) { SVFIR *pag = this->getPAG(); const NodeBS targets = this->getPts(pVar->getId()).toNodeBS(); diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index bbdc74b65..fc1d5fbda 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -224,8 +224,8 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(FIObjNode, FIObjVar); CASE(DummyValNode, DummyValVar); CASE(DummyObjNode, DummyObjVar); - CASE(FuncObjNode, FuncObjVar); - CASE(FuncValNode, FuncValVar); + CASE(FunObjNode, FunObjVar); + CASE(FunValNode, FunValVar); #undef CASE } } diff --git a/svf/lib/SVFIR/SVFIR.cpp b/svf/lib/SVFIR/SVFIR.cpp index 0e4e28d6b..b9e5d72d2 100644 --- a/svf/lib/SVFIR/SVFIR.cpp +++ b/svf/lib/SVFIR/SVFIR.cpp @@ -477,7 +477,7 @@ NodeID SVFIR::addFuncObjNode(const CallGraphNode* callGraphNode, const MemObj* o //assert(findPAGNode(i) == false && "this node should not be created before"); NodeID base = obj->getId(); memToFieldsMap[base].set(obj->getId()); - FuncObjVar *node = new FuncObjVar(callGraphNode, obj->getId(), obj); + FunObjVar*node = new FunObjVar(callGraphNode, obj->getId(), obj); return addObjNode(obj->getValue(), node, obj->getId()); } @@ -687,7 +687,7 @@ bool SVFIR::isValidTopLevelPtr(const SVFVar* node) if (isValidPointer(node->getId())) { // TODO: after svf value is removed, we use type to determine top level ptr - if (SVFUtil::isa(node) || SVFUtil::isa(node) || SVFUtil::isa(node)) + if (SVFUtil::isa(node) || SVFUtil::isa(node) || SVFUtil::isa(node)) { return true; } else if(node->hasValue()) diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 126be1c22..1d041931b 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -53,12 +53,12 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : break; } case RetNode: - case FuncObjNode: + case FunObjNode: { // to be completed in derived class break; } - case FuncValNode: + case FunValNode: case VarargNode: case DummyValNode: { @@ -184,11 +184,11 @@ const std::string FIObjVar::toString() const return rawstr.str(); } -const std::string FuncValVar::toString() const +const std::string FunValVar::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "FuncValVar ID: " << getId(); + rawstr << "FunValVar ID: " << getId(); if (Options::ShowSVFIRValue()) { rawstr << "\n"; @@ -197,23 +197,23 @@ const std::string FuncValVar::toString() const return rawstr.str(); } -FuncObjVar::FuncObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, +FunObjVar::FunObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, PNODEK ty) : FIObjVar(nullptr, i, mem, ty), callGraphNode(cgNode) { isPtr = callGraphNode->getFunction()->getType()->isPointerTy(); } -bool FuncObjVar::isIsolatedNode() const +bool FunObjVar::isIsolatedNode() const { return callGraphNode->getFunction()->isIntrinsic(); } -const std::string FuncObjVar::toString() const +const std::string FunObjVar::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "FuncObjVar ID: " << getId() << " (base object)"; + rawstr << "FunObjVar ID: " << getId() << " (base object)"; if (Options::ShowSVFIRValue()) { rawstr << "\n"; diff --git a/svf/lib/Util/CallGraphBuilder.cpp b/svf/lib/Util/CallGraphBuilder.cpp index 5cc6446a0..6c7a012db 100644 --- a/svf/lib/Util/CallGraphBuilder.cpp +++ b/svf/lib/Util/CallGraphBuilder.cpp @@ -89,7 +89,7 @@ ThreadCallGraph* CallGraphBuilder::buildThreadCallGraph() const CallICFGNode* cs = cast(inst); cg->addForksite(cs); const ValVar* svfVar = tdAPI->getForkedFun(cs); - if (SVFUtil::isa(svfVar)) + if (SVFUtil::isa(svfVar)) { cg->addDirectForkEdge(cs); } From 5a6c48da7cdd1141b55c74a2ad318690cff9eb67 Mon Sep 17 00:00:00 2001 From: jumormt Date: Mon, 25 Nov 2024 22:47:35 +1100 Subject: [PATCH 03/28] refactor addobjvar --- svf-llvm/lib/SVFIRBuilder.cpp | 8 ++++---- svf/include/SVFIR/SVFIR.h | 12 +++++------- svf/lib/SVFIR/SVFIR.cpp | 12 +++++++----- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index f4eef2130..eabf83192 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -235,7 +235,7 @@ void SVFIRBuilder::initialiseNodes() SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); - pag->addFuncValNode(cgn, iter->second, icfgNode); + pag->addFunValNode(cgn, iter->second, icfgNode); } else { pag->addValNode(iter->first, iter->second, icfgNode); } @@ -248,13 +248,13 @@ void SVFIRBuilder::initialiseNodes() DBOUT(DPAGBuild, outs() << "add obj node " << iter->second << "\n"); if(iter->second == symTable->blackholeSymID() || iter->second == symTable->constantSymID()) continue; - CallGraphNode* pNode = nullptr; if (const Function* func = SVFUtil::dyn_cast( llvmModuleSet()->getLLVMValue(iter->first))) { - pNode = llvmModuleSet()->getCallGraphNode(func); + pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); + } else { + pag->addObjNode(iter->first, iter->second); } - pag->addObjNode(iter->first, pNode, iter->second); } for (SymbolTableInfo::FunToIDMapTy::iterator iter = diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 1bc218341..bf0bfdaa8 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -551,22 +551,20 @@ class SVFIR : public IRGraph return addValNode(val, node, i); } - NodeID addFuncValNode(const CallGraphNode* callGraphNode, NodeID i, const ICFGNode* icfgNode) { + 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, const CallGraphNode* callGraphNode, NodeID i) + inline NodeID addObjNode(const SVFValue* val, NodeID i) { const MemObj* mem = getMemObj(val); assert(mem->getId() == i && "not same object id?"); - if(callGraphNode) - return addFuncObjNode(callGraphNode, mem); - else - return addFIObjNode(mem); + return addFIObjNode(mem); } + NodeID addFunObjNode(const CallGraphNode* callGraphNode, NodeID id); /// Add a unique return node for a procedure inline NodeID addRetNode(const CallGraphNode* callGraphNode, NodeID i) { @@ -586,7 +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); - NodeID addFuncObjNode(const CallGraphNode* callGraphNode, const MemObj* obj); + //@} /// Add a dummy value/object node according to node ID (llvm value is null) diff --git a/svf/lib/SVFIR/SVFIR.cpp b/svf/lib/SVFIR/SVFIR.cpp index b9e5d72d2..8465ec763 100644 --- a/svf/lib/SVFIR/SVFIR.cpp +++ b/svf/lib/SVFIR/SVFIR.cpp @@ -472,13 +472,15 @@ NodeID SVFIR::addFIObjNode(const MemObj* obj) return addObjNode(obj->getValue(), node, obj->getId()); } -NodeID SVFIR::addFuncObjNode(const CallGraphNode* callGraphNode, const MemObj* obj) +NodeID SVFIR::addFunObjNode(const CallGraphNode* callGraphNode, NodeID id) { + const MemObj* mem = getMemObj(callGraphNode->getFunction()); + assert(mem->getId() == id && "not same object id?"); //assert(findPAGNode(i) == false && "this node should not be created before"); - NodeID base = obj->getId(); - memToFieldsMap[base].set(obj->getId()); - FunObjVar*node = new FunObjVar(callGraphNode, obj->getId(), obj); - return addObjNode(obj->getValue(), node, obj->getId()); + NodeID base = mem->getId(); + memToFieldsMap[base].set(mem->getId()); + FunObjVar*node = new FunObjVar(callGraphNode, mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); } /*! From f91bdba27160f1a70902c084f8b51bb52685fe5b Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Tue, 3 Dec 2024 19:51:44 +1100 Subject: [PATCH 04/28] WIP first commit --- svf-llvm/lib/SVFIRBuilder.cpp | 18 +- svf/include/Graphs/GenericGraph.h | 27 +- svf/include/SVFIR/SVFIR.h | 85 +++++ svf/include/SVFIR/SVFVariables.h | 566 ++++++++++++++++++++++++++++++ svf/lib/SVFIR/SVFVariables.cpp | 12 + 5 files changed, 700 insertions(+), 8 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index eabf83192..7501613c8 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -218,8 +218,12 @@ void SVFIRBuilder::initialiseNodes() ++iter) { DBOUT(DPAGBuild, outs() << "add val node " << iter->second << "\n"); - if(iter->second == symTable->blkPtrSymID() || iter->second == symTable->nullPtrSymID()) + if(iter->second == symTable->blkPtrSymID()) continue; + if (iter->second == symTable->nullPtrSymID()) { + //onst SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode + pag->addConstantValNode(iter->first, iter->second, nullptr); + } const ICFGNode* icfgNode = nullptr; if (const Instruction* inst = @@ -236,7 +240,11 @@ void SVFIRBuilder::initialiseNodes() { const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); pag->addFunValNode(cgn, iter->second, icfgNode); - } else { + } + else if (SVFUtil::isa(llvmModuleSet()->getLLVMValue(iter->first))) { + pag->addConstantValNode(iter->first, iter->second, icfgNode); + } + else { pag->addValNode(iter->first, iter->second, icfgNode); } } @@ -252,7 +260,11 @@ void SVFIRBuilder::initialiseNodes() llvmModuleSet()->getLLVMValue(iter->first))) { pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); - } else { + } + else if (SVFUtil::isa(llvmModuleSet()->getLLVMValue(iter->first))) { + pag->addConstantObjNode(iter->first, iter->second); + } + else { pag->addObjNode(iter->first, iter->second); } } diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index 87fb354b3..35bbfaa38 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -170,8 +170,17 @@ class SVFBaseNode // │ │ ├── Represents a variadic argument node VarargNode, // │ │ └── Dummy node for uninitialized values + ConstantValNode, + ConstantDataValNode, + GlobalValueValNode, + BlackHoleNode, + ConstantFPValNode, + ConstantIntValNode, DummyValNode, // │ └── ObjVarKinds: Types of object variable nodes + + + // │ ├── Represents an object variable ObjNode, // │ ├── GepObjNode: Represents a GEP object variable @@ -181,9 +190,17 @@ class SVFBaseNode // │ ├──FunObjNode: Types of function object FunObjNode, // │ └── DummyObjNode: Dummy node for uninitialized objects + ConstantObjNode, + ConstantDataObjNode, + GlobalValueObjNode, + ConstantFPObjNode, + ConstantIntObjNode, + ConstantNullptrNode, DummyObjNode, // └──────── + + // ┌── VFGNodeKinds: Various Value Flow Graph (VFG) node kinds with operations // │ ├── Represents a comparison operation Cmp, @@ -317,7 +334,7 @@ class SVFBaseNode static inline bool isSVFVarKind(GNodeK n) { - static_assert(DummyObjNode - ValNode == 10, + static_assert(DummyObjNode - ValNode == 22, "The number of SVFVarKinds has changed, make sure the " "range is correct"); @@ -326,7 +343,7 @@ class SVFBaseNode static inline bool isValVarKinds(GNodeK n) { - static_assert(DummyValNode - ValNode == 5, + static_assert(DummyValNode - ValNode == 11, "The number of ValVarKinds has changed, make sure the " "range is correct"); return n <= DummyValNode && n >= ValNode; @@ -334,7 +351,7 @@ class SVFBaseNode static inline bool isObjVarKinds(GNodeK n) { - static_assert(DummyObjNode - ObjNode == 4, + static_assert(DummyObjNode - ObjNode == 10, "The number of ObjVarKinds has changed, make sure the " "range is correct"); return n <= DummyObjNode && n >= ObjNode; @@ -342,10 +359,10 @@ class SVFBaseNode static inline bool isFIObjVarKinds(GNodeK n) { - static_assert(FunObjNode - FIObjNode == 1, + static_assert(ConstantNullptrNode - FIObjNode == 7, "The number of FIObjVarKinds has changed, make sure the " "range is correct"); - return n <= FunObjNode && n >= FIObjNode; + return n <= ConstantNullptrNode && n >= FIObjNode; } static inline bool isVFGNodeKinds(GNodeK n) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index bf0bfdaa8..4602a8e39 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -556,6 +556,7 @@ class SVFIR : public IRGraph return addValNode(nullptr, node, i); } + /// Add a memory obj node inline NodeID addObjNode(const SVFValue* val, NodeID i) { @@ -578,6 +579,90 @@ class SVFIR : public IRGraph return addNode(node,i); } + inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { + if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) + { + SVFVar* node = new ConstantFPValVar(constFp, i, icfgNode); + return addNode(node,i); + } + // ConstantInt + else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) + { + SVFVar* node = new ConstantIntValVar(constInt, i, icfgNode); + return addNode(node,i); + } + // constNullptr + else if (const SVFConstantNullPtr* constNullPtr = + SVFUtil::dyn_cast(curInst)) { + SVFVar* node = new ConstantNullPtrValVar(constNullPtr, i, icfgNode); + return addNode(node,i); + } + + else if (const SVFGlobalValue* globalVal = + SVFUtil::dyn_cast(curInst)) + { + SVFVar* node = new GlobalValueValvar(globalVal, i, icfgNode); + return addNode(node,i); + } + + else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { + SVFVar* node = new ConstantDataValVar(dataVal, i, icfgNode); + return addNode(node,i); + } + + else if (const SVFConstant* constVal = + SVFUtil::dyn_cast(curInst)) + { + SVFVar* node = new ConstantValVar(constVal, i, icfgNode); + return addNode(node, i); + } + assert(false && "not a constant value?"); + } + + inline NodeID addConstantObjNode(const SVFValue* curInst, const NodeID i) { + // const MemObj* mem = getMemObj(callGraphNode->getFunction()); + // assert(mem->getId() == id && "not same object id?"); + // //assert(findPAGNode(i) == false && "this node should not be created before"); NodeID base = mem->getId(); + // memToFieldsMap[base].set(mem->getId()); + // FunObjVar*node = new FunObjVar(callGraphNode, mem->getId(), mem); + // return addObjNode(mem->getValue(), node, mem->getId()); + const MemObj* mem = getMemObj(curInst); + NodeID base = mem->getId(); + memToFieldsMap[base].set(mem->getId()); + if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) + { + ConstantFPObjVar* node = new ConstantFPObjVar(mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + // ConstantInt + else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) + { + ConstantIntObjVar* node = new ConstantIntObjVar(mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + //TODO: constNullptrObj + + else if (const SVFGlobalValue* globalVal = + SVFUtil::dyn_cast(curInst)) + { + GlobalValueObjVar* node = new GlobalValueObjVar(mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + + else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { + ConstantDataObjVar* node = new ConstantDataObjVar(mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + + else if (const SVFConstant* constVal = + SVFUtil::dyn_cast(curInst)) + { + ConstantObjVar* node = new ConstantObjVar(nullptr, mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + assert(false && "not a constant value?"); + } + /// Add a temp field value node, this method can only invoked by getGepValVar NodeID addGepValNode(const SVFValue* curInst,const SVFValue* val, const AccessPath& ap, NodeID i, const SVFType* type); /// Add a field obj node, this method can only invoked by getGepObjVar diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 173fbe90e..b9dd35b75 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -685,6 +685,572 @@ class FunObjVar : public FIObjVar { virtual const std::string toString() const; }; + +/* + * Constant objects, including ConstantValVar inherited from ValVar, + * and ConstantObjVar inherited from FIObjVar + */ +class ConstantValVar: public ValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == ConstantValNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == ConstantValNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == ConstantValNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == ConstantValNode; + } + //@} + + /// Constructor + ConstantValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + PNODEK ty = ConstantValNode) + : ValVar(val, i, ty, icn) + { + + } + + virtual const std::string toString() const { + return "ConstantValVar"; + } +}; + +class ConstantDataValVar: public ConstantValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == ConstantDataValNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == ConstantDataValNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == ConstantDataValNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == ConstantDataValNode; + } + //@} + + /// Constructor + ConstantDataValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + PNODEK ty = ConstantDataValNode) + : ConstantValVar(val, i, icn, ty) + { + + } + + virtual const std::string toString() const { + return "ConstantDataValVar"; + } +}; + +class GlobalValueValvar: public ConstantValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == GlobalValueValNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == GlobalValueValNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == GlobalValueValNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == GlobalValueValNode; + } + //@} + + /// Constructor + GlobalValueValvar(const SVFValue* val, NodeID i, const ICFGNode* icn, + PNODEK ty = GlobalValueValNode) + : ConstantValVar(val, i, icn, ty) + { + + } + + virtual const std::string toString() const { + return "GlobalValueValvar"; + } +}; + +class BlackHoleVar: public ConstantDataValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == BlackHoleNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == BlackHoleNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == BlackHoleNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == BlackHoleNode; + } + //@} + + /// Constructor + BlackHoleVar(NodeID i, PNODEK ty = BlackHoleNode) + : ConstantDataValVar(nullptr, i, nullptr, ty) + { + + } + + virtual const std::string toString() const { + return "BlackHoleVar"; + } +}; + +class ConstantFPValVar: public ConstantDataValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == ConstantFPValNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == ConstantFPValNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == ConstantFPValNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == ConstantFPValNode; + } + //@} + + /// Constructor + ConstantFPValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + PNODEK ty = ConstantFPValNode) + : ConstantDataValVar(val, i, icn, ty) + { + + } + + virtual const std::string toString() const { + return "ConstantFPValVar"; + } +}; + +class ConstantIntValVar: public ConstantDataValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == ConstantIntValNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == ConstantIntValNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == ConstantIntValNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == ConstantIntValNode; + } + //@} + + /// Constructor + ConstantIntValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + PNODEK ty = ConstantIntValNode) + : ConstantDataValVar(val, i, icn, ty) + { + + } + virtual const std::string toString() const { + return "ConstantIntValVar"; + } +}; + +class ConstantNullPtrValVar: public ConstantDataValVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const FunValVar*) + { + return true; + } + static inline bool classof(const ValVar* node) + { + return node->getNodeKind() == ConstantNullptrNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == ConstantNullptrNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == ConstantNullptrNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == ConstantNullptrNode; + } + //@} + + /// Constructor + ConstantNullPtrValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + PNODEK ty = ConstantNullptrNode) + : ConstantDataValVar(val, i, icn, ty) + { + + } + + virtual const std::string toString() const { + return "ConstantNullPtrValVar"; + } +}; + +class ConstantObjVar: public FIObjVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +protected: + /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) + ConstantObjVar(NodeID i, PNODEK ty = FIObjNode) : FIObjVar(i, ty) {} + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const ConstantObjVar*) + { + return true; + } + static inline bool classof(const ObjVar* node) + { + return isFIObjVarKinds(node->getNodeKind()); + } + static inline bool classof(const SVFVar* node) + { + return isFIObjVarKinds(node->getNodeKind()); + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return isFIObjVarKinds(node->getNodeKind()); + } + static inline bool classof(const SVFBaseNode* node) + { + return isFIObjVarKinds(node->getNodeKind()); + } + //@} + + /// Constructor + ConstantObjVar(const SVFValue* val, NodeID i, const MemObj* mem, + PNODEK ty = FIObjNode) + : FIObjVar(val, i, mem, ty) + { + } + + /// Return name of a LLVM value + inline const std::string getValueName() const + { + if (value) + return value->getName() + " (base object)"; + return " (base object)"; + } + + virtual const std::string toString() const { + return "ConstantObjVar: " + getValueName(); + } +}; + +class GlobalValueObjVar: public ConstantObjVar { + friend class SVFIRWriter; + friend class SVFIRReader; + +private: + /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) + GlobalValueObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : ConstantObjVar(i, ty) {} + +public: + /// Methods for support type inquiry through isa, cast, and dyn_cast: + //@{ + static inline bool classof(const GlobalValueObjVar*) + { + return true; + } + static inline bool classof(const ConstantObjVar* node) + { + return node->getNodeKind() == GlobalValueObjNode; + } + static inline bool classof(const FIObjVar* node) + { + return node->getNodeKind() == GlobalValueObjNode; + } + static inline bool classof(const ObjVar* node) + { + return node->getNodeKind() == GlobalValueObjNode; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == GlobalValueObjNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == GlobalValueObjNode; + } + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == GlobalValueObjNode; + } + //@} + + /// Constructor + GlobalValueObjVar(NodeID i, const MemObj* mem, + PNODEK ty = GlobalValueObjNode): ConstantObjVar(nullptr,i,mem,ty){ + + } + + + virtual const std::string toString() const { + return "GlobalValueObjVar"; + } +}; + +class ConstantDataObjVar: public ConstantObjVar { + friend class SVFIRWriter; + friend class SVFIRReader; + +protected: + /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) + ConstantDataObjVar(NodeID i) : ConstantObjVar(i, ConstantDataObjNode) {} + +public: + //@{ Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ConstantDataObjVar*) + { + return true; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == SVFVar::ConstantDataObjNode; + } + static inline bool classof(const ObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantDataObjNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == SVFVar::ConstantDataObjNode; + } + + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == SVFVar::ConstantDataObjNode; + } + //@} + + /// Constructor + ConstantDataObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) + : ConstantObjVar(nullptr, i, m, ty) + { + } + + /// Return name of this node + inline const std::string getValueName() const + { + return "dummyObj"; + } + + virtual const std::string toString() const { + return "ConstantDataObjVar"; + } +}; + +class ConstantFPObjVar: public ConstantDataObjVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +private: + /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) + ConstantFPObjVar(NodeID i) : ConstantDataObjVar(i) {} + +public: + //@{ Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ConstantDataObjVar*) + { + return true; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == SVFVar::ConstantFPObjNode; + } + static inline bool classof(const ObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantFPObjNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == SVFVar::ConstantFPObjNode; + } + + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == SVFVar::ConstantFPObjNode; + } + //@} + + /// Constructor + ConstantFPObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) + : ConstantDataObjVar(i, m, ty) + { + } + + /// Return name of this node + inline const std::string getValueName() const + { + return "dummyObj"; + } + + virtual const std::string toString() const + { + return "ConstantFPObjVar"; + } +}; + +class ConstantIntObjVar: public ConstantDataObjVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; + +private: + /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) + ConstantIntObjVar(NodeID i) : ConstantDataObjVar(i) {} + +public: + //@{ Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ConstantDataObjVar*) + { + return true; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == SVFVar::ConstantIntObjNode; + } + static inline bool classof(const ObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantIntObjNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == SVFVar::ConstantIntObjNode; + } + + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == SVFVar::ConstantIntObjNode; + } + //@} + + /// Constructor + ConstantIntObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) + : ConstantDataObjVar(i, m, ty) + { + } + + /// Return name of this node + inline const std::string getValueName() const + { + return "dummyObj"; + } + + virtual const std::string toString() const { + return "ConstantIntObjVar"; + } +}; + + + /* * Unique Return node of a procedure */ diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 1d041931b..3a9b50d5e 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -60,6 +60,12 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : } case FunValNode: case VarargNode: + case ConstantValNode: + case ConstantDataValNode: + case GlobalValueValNode: + case BlackHoleNode: + case ConstantFPValNode: + case ConstantIntValNode: case DummyValNode: { isPtr = true; @@ -68,6 +74,12 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : case ObjNode: case GepObjNode: case FIObjNode: + case ConstantObjNode: + case ConstantDataObjNode: + case GlobalValueObjNode: + case ConstantFPObjNode: + case ConstantIntObjNode: + case ConstantNullptrNode: case DummyObjNode: { isPtr = true; From d6049678874235484e55bd8b86bf05e5c05e0383 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Thu, 5 Dec 2024 15:55:16 +1100 Subject: [PATCH 05/28] WIP first commit of ConstantVal/Obj --- svf/include/Graphs/GenericGraph.h | 34 +++-- svf/include/SVFIR/SVFIR.h | 10 +- svf/include/SVFIR/SVFVariables.h | 198 +++++++++++++++++++++++------- svf/lib/AE/Core/AbstractState.cpp | 18 +-- svf/lib/AE/Svfexe/AbsExtAPI.cpp | 6 - svf/lib/SVFIR/SVFFileSystem.cpp | 13 ++ svf/lib/SVFIR/SVFVariables.cpp | 3 +- 7 files changed, 204 insertions(+), 78 deletions(-) diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index 35bbfaa38..da1951b8a 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -169,15 +169,23 @@ class SVFBaseNode RetNode, // │ │ ├── Represents a variadic argument node VarargNode, - // │ │ └── Dummy node for uninitialized values + // │ │ ├── Represents a constant value node ConstantValNode, + // │ │ ├── Represents a constant data value node ConstantDataValNode, + // │ │ ├── Represents a global value node GlobalValueValNode, + // │ │ ├── Represents a black hole node BlackHoleNode, + // │ │ ├── Represents a constant float-point value node ConstantFPValNode, + // │ │ ├── Represents a constant integer value node ConstantIntValNode, + // │ | └── Represents a constant nullptr value node + ConstantNullptrValNode, + // │ └── Dummy node for uninitialized values DummyValNode, - // │ └── ObjVarKinds: Types of object variable nodes + // └──────── @@ -185,17 +193,23 @@ 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: Types of function object FunObjNode, - // │ └── DummyObjNode: Dummy node for uninitialized objects + // │ ├── ConstantObjNode: Types of constant object ConstantObjNode, + // │ ├── ConstantDataObjNode: Types of constant data object ConstantDataObjNode, + // │ ├── GlobalValueObjNode: Types of global value object GlobalValueObjNode, + // │ ├── ConstantFPObjNode: Types of constant float-point object ConstantFPObjNode, + // │ ├── ConstantIntObjNode: Types of constant integer object ConstantIntObjNode, - ConstantNullptrNode, + // │ ├── ConstantNullptrObjNode: Types of constant nullptr object + ConstantNullptrObjNode, + // │ └── DummyObjNode: Dummy node for uninitialized objects DummyObjNode, // └──────── @@ -334,7 +348,7 @@ class SVFBaseNode static inline bool isSVFVarKind(GNodeK n) { - static_assert(DummyObjNode - ValNode == 22, + static_assert(DummyObjNode - ValNode == 23, "The number of SVFVarKinds has changed, make sure the " "range is correct"); @@ -343,7 +357,7 @@ class SVFBaseNode static inline bool isValVarKinds(GNodeK n) { - static_assert(DummyValNode - ValNode == 11, + static_assert(DummyValNode - ValNode == 12, "The number of ValVarKinds has changed, make sure the " "range is correct"); return n <= DummyValNode && n >= ValNode; @@ -359,10 +373,10 @@ class SVFBaseNode static inline bool isFIObjVarKinds(GNodeK n) { - static_assert(ConstantNullptrNode - FIObjNode == 7, + static_assert(ConstantNullptrObjNode - FIObjNode == 7, "The number of FIObjVarKinds has changed, make sure the " "range is correct"); - return n <= ConstantNullptrNode && n >= FIObjNode; + return n <= ConstantNullptrObjNode && n >= FIObjNode; } static inline bool isVFGNodeKinds(GNodeK n) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 4602a8e39..b61664a20 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -631,13 +631,13 @@ class SVFIR : public IRGraph memToFieldsMap[base].set(mem->getId()); if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) { - ConstantFPObjVar* node = new ConstantFPObjVar(mem->getId(), mem); + ConstantFPObjVar* node = new ConstantFPObjVar(constFp, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } // ConstantInt else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) { - ConstantIntObjVar* node = new ConstantIntObjVar(mem->getId(), mem); + ConstantIntObjVar* node = new ConstantIntObjVar(constInt, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } //TODO: constNullptrObj @@ -645,19 +645,19 @@ class SVFIR : public IRGraph else if (const SVFGlobalValue* globalVal = SVFUtil::dyn_cast(curInst)) { - GlobalValueObjVar* node = new GlobalValueObjVar(mem->getId(), mem); + GlobalValueObjVar* node = new GlobalValueObjVar(globalVal, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { - ConstantDataObjVar* node = new ConstantDataObjVar(mem->getId(), mem); + ConstantDataObjVar* node = new ConstantDataObjVar(dataVal, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (const SVFConstant* constVal = SVFUtil::dyn_cast(curInst)) { - ConstantObjVar* node = new ConstantObjVar(nullptr, mem->getId(), mem); + ConstantObjVar* node = new ConstantObjVar(constVal, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } assert(false && "not a constant value?"); diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index b9dd35b75..0a9cdea60 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -729,7 +729,12 @@ class ConstantValVar: public ValVar } virtual const std::string toString() const { - return "ConstantValVar"; + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantValNode ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -771,8 +776,13 @@ class ConstantDataValVar: public ConstantValVar } - virtual const std::string toString() const { - return "ConstantDataValVar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantDataValNode ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -814,8 +824,13 @@ class GlobalValueValvar: public ConstantValVar } - virtual const std::string toString() const { - return "GlobalValueValvar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "GlobalValueValVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -899,8 +914,13 @@ class ConstantFPValVar: public ConstantDataValVar } - virtual const std::string toString() const { - return "ConstantFPValVar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantFPValVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -941,8 +961,13 @@ class ConstantIntValVar: public ConstantDataValVar { } - virtual const std::string toString() const { - return "ConstantIntValVar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantIntValNode ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -960,32 +985,37 @@ class ConstantNullPtrValVar: public ConstantDataValVar } static inline bool classof(const ValVar* node) { - return node->getNodeKind() == ConstantNullptrNode; + return node->getNodeKind() == ConstantNullptrValNode; } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == ConstantNullptrNode; + return node->getNodeKind() == ConstantNullptrValNode; } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == ConstantNullptrNode; + return node->getNodeKind() == ConstantNullptrValNode; } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == ConstantNullptrNode; + return node->getNodeKind() == ConstantNullptrValNode; } //@} /// Constructor ConstantNullPtrValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, - PNODEK ty = ConstantNullptrNode) + PNODEK ty = ConstantNullptrValNode) : ConstantDataValVar(val, i, icn, ty) { } - virtual const std::string toString() const { - return "ConstantNullPtrValVar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantNullPtrValNode ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -996,7 +1026,7 @@ class ConstantObjVar: public FIObjVar protected: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - ConstantObjVar(NodeID i, PNODEK ty = FIObjNode) : FIObjVar(i, ty) {} + ConstantObjVar(NodeID i, PNODEK ty = ConstantObjNode) : FIObjVar(i, ty) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: @@ -1025,7 +1055,7 @@ class ConstantObjVar: public FIObjVar /// Constructor ConstantObjVar(const SVFValue* val, NodeID i, const MemObj* mem, - PNODEK ty = FIObjNode) + PNODEK ty = ConstantObjNode) : FIObjVar(val, i, mem, ty) { } @@ -1039,7 +1069,12 @@ class ConstantObjVar: public FIObjVar } virtual const std::string toString() const { - return "ConstantObjVar: " + getValueName(); + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantObjVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -1085,14 +1120,19 @@ class GlobalValueObjVar: public ConstantObjVar { //@} /// Constructor - GlobalValueObjVar(NodeID i, const MemObj* mem, - PNODEK ty = GlobalValueObjNode): ConstantObjVar(nullptr,i,mem,ty){ + GlobalValueObjVar(const SVFValue* val, NodeID i, const MemObj* mem, + PNODEK ty = GlobalValueObjNode): ConstantObjVar(val,i,mem,ty){ } - virtual const std::string toString() const { - return "GlobalValueObjVar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "GlobalValueObjVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -1130,19 +1170,18 @@ class ConstantDataObjVar: public ConstantObjVar { //@} /// Constructor - ConstantDataObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) - : ConstantObjVar(nullptr, i, m, ty) + ConstantDataObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) + : ConstantObjVar(val, i, m, ty) { } - /// Return name of this node - inline const std::string getValueName() const - { - return "dummyObj"; - } - - virtual const std::string toString() const { - return "ConstantDataObjVar"; + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantDataObjVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -1181,20 +1220,25 @@ class ConstantFPObjVar: public ConstantDataObjVar //@} /// Constructor - ConstantFPObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) - : ConstantDataObjVar(i, m, ty) + ConstantFPObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) + : ConstantDataObjVar(val, i, m, ty) { } - /// Return name of this node - inline const std::string getValueName() const + inline double getFPValue () const { - return "dummyObj"; + return SVFUtil::dyn_cast(value)->getFPValue(); } + virtual const std::string toString() const { - return "ConstantFPObjVar"; + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantFPObjVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; @@ -1230,27 +1274,87 @@ class ConstantIntObjVar: public ConstantDataObjVar { return node->getNodeKind() == SVFVar::ConstantIntObjNode; } - //@} - /// Constructor - ConstantIntObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) - : ConstantDataObjVar(i, m, ty) + s64_t getSExtValue() const { + return SVFUtil::dyn_cast(value)->getSExtValue(); } - /// Return name of this node - inline const std::string getValueName() const + + u64_t getZExtValue() const + { + return SVFUtil::dyn_cast(value)->getZExtValue(); + } + //@} + + /// Constructor + ConstantIntObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) + : ConstantDataObjVar(val, i, m, ty) { - return "dummyObj"; } + virtual const std::string toString() const { - return "ConstantIntObjVar"; + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantIntObjVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); } }; +class ConstantNullPtrObjVar: public ConstantDataObjVar +{ + friend class SVFIRWriter; + friend class SVFIRReader; +private: + /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) + ConstantNullPtrObjVar(NodeID i) : ConstantDataObjVar(i) {} + +public: + //@{ Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ConstantDataObjVar*) + { + return true; + } + static inline bool classof(const SVFVar* node) + { + return node->getNodeKind() == SVFVar::ConstantNullptrObjNode; + } + static inline bool classof(const ObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantNullptrObjNode; + } + static inline bool classof(const GenericPAGNodeTy* node) + { + return node->getNodeKind() == SVFVar::ConstantNullptrObjNode; + } + + static inline bool classof(const SVFBaseNode* node) + { + return node->getNodeKind() == SVFVar::ConstantNullptrObjNode; + } + //@} + + /// Constructor + ConstantNullPtrObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantNullptrObjNode) + : ConstantDataObjVar(val, i, m, ty) + { + } + + + virtual const std::string toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantNullPtrObjVar ID: " << getId(); + rawstr << "\n"; + rawstr << value->toString(); + return rawstr.str(); + } +}; /* * Unique Return node of a procedure */ diff --git a/svf/lib/AE/Core/AbstractState.cpp b/svf/lib/AE/Core/AbstractState.cpp index 9a4ba8e5d..cdf0eb18d 100644 --- a/svf/lib/AE/Core/AbstractState.cpp +++ b/svf/lib/AE/Core/AbstractState.cpp @@ -188,20 +188,20 @@ void AbstractState::initObjVar(ObjVar* objVar) // Handle constant data, arrays, and structures if (obj->isConstDataOrConstGlobal() || obj->isConstantArray() || obj->isConstantStruct()) { - if (const SVFConstantInt* consInt = SVFUtil::dyn_cast(obj->getValue())) + if (const ConstantIntObjVar* consInt = SVFUtil::dyn_cast(objVar)) { s64_t numeral = consInt->getSExtValue(); (*this)[varId] = IntervalValue(numeral, numeral); } - else if (const SVFConstantFP* consFP = SVFUtil::dyn_cast(obj->getValue())) + else if (const ConstantFPObjVar* consFP = SVFUtil::dyn_cast(objVar)) { (*this)[varId] = IntervalValue(consFP->getFPValue(), consFP->getFPValue()); } - else if (SVFUtil::isa(obj->getValue())) + else if (SVFUtil::isa(objVar)) { (*this)[varId] = IntervalValue(0, 0); } - else if (SVFUtil::isa(obj->getValue())) + else if (SVFUtil::isa(objVar)) { (*this)[varId] = AddressValue(AbstractState::getVirtualMemAddress(varId)); } @@ -240,7 +240,7 @@ IntervalValue AbstractState::getElementIndex(const GepStmt* gep) for (int i = gep->getOffsetVarAndGepTypePairVec().size() - 1; i >= 0; i--) { AccessPath::IdxOperandPair IdxVarAndType = gep->getOffsetVarAndGepTypePairVec()[i]; - const SVFValue* value = gep->getOffsetVarAndGepTypePairVec()[i].first->getValue(); + const SVFVar* var = gep->getOffsetVarAndGepTypePairVec()[i].first; const SVFType* type = IdxVarAndType.second; // Variables to store the lower and upper bounds of the index value @@ -248,11 +248,11 @@ IntervalValue AbstractState::getElementIndex(const GepStmt* gep) s64_t idxUb; // Determine the lower and upper bounds based on whether the value is a constant - if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(value)) + if (const ConstantIntObjVar* constInt = SVFUtil::dyn_cast(var)) idxLb = idxUb = constInt->getSExtValue(); else { - IntervalValue idxItv = (*this)[PAG::getPAG()->getValueNode(value)].getInterval(); + IntervalValue idxItv = (*this)[var->getId()].getInterval(); if (idxItv.isBottom()) idxLb = idxUb = 0; else @@ -327,7 +327,7 @@ IntervalValue AbstractState::getByteOffset(const GepStmt* gep) else assert(false && "idxOperandType must be ArrType or PtrType"); - if (const SVFConstantInt* op = SVFUtil::dyn_cast(idxOperandVar->getValue())) + if (const ConstantIntObjVar* op = SVFUtil::dyn_cast(idxOperandVar)) { // Calculate the lower bound (lb) of the interval value s64_t lb = (double)Options::MaxFieldLimit() / elemByteSize >= op->getSExtValue() @@ -337,7 +337,7 @@ IntervalValue AbstractState::getByteOffset(const GepStmt* gep) } else { - u32_t idx = PAG::getPAG()->getValueNode(idxOperandVar->getValue()); + u32_t idx = idxOperandVar->getId(); IntervalValue idxVal = (*this)[idx].getInterval(); if (idxVal.isBottom()) diff --git a/svf/lib/AE/Svfexe/AbsExtAPI.cpp b/svf/lib/AE/Svfexe/AbsExtAPI.cpp index 2260d3fba..da29a7fb9 100644 --- a/svf/lib/AE/Svfexe/AbsExtAPI.cpp +++ b/svf/lib/AE/Svfexe/AbsExtAPI.cpp @@ -758,9 +758,3 @@ IntervalValue AbsExtAPI::getRangeLimitFromType(const SVFType* type) // other types, return top interval } } - -const SVFVar* AbsExtAPI::getSVFVar(const SVF::SVFValue* val) -{ - assert(svfir->hasGNode(svfir->getValueNode(val))); - return svfir->getGNode(svfir->getValueNode(val)); -} \ No newline at end of file diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index fc1d5fbda..935eb0bc2 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -226,6 +226,19 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(DummyObjNode, DummyObjVar); CASE(FunObjNode, FunObjVar); CASE(FunValNode, FunValVar); + CASE(ConstantValNode, ConstantValVar); + CASE(ConstantDataValNode, ConstantDataValVar); + CASE(GlobalValueValNode, GlobalValueValvar); + CASE(BlackHoleNode, BlackHoleVar); + CASE(ConstantFPValNode, ConstantFPValVar); + CASE(ConstantIntValNode, ConstantIntValVar); + CASE(ConstantNullptrValNode, ConstantNullPtrValVar); + CASE(ConstantObjNode, ConstantObjVar); + CASE(ConstantDataObjNode, ConstantDataObjVar); + CASE(GlobalValueObjNode, GlobalValueObjVar); + CASE(ConstantFPObjNode, ConstantFPObjVar); + CASE(ConstantIntObjNode, ConstantIntObjVar); + CASE(ConstantNullptrObjNode, ConstantNullPtrObjVar); #undef CASE } } diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 3a9b50d5e..01110057b 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -66,6 +66,7 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : case BlackHoleNode: case ConstantFPValNode: case ConstantIntValNode: + case ConstantNullptrValNode: case DummyValNode: { isPtr = true; @@ -79,7 +80,7 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : case GlobalValueObjNode: case ConstantFPObjNode: case ConstantIntObjNode: - case ConstantNullptrNode: + case ConstantNullptrObjNode: case DummyObjNode: { isPtr = true; From d3991c26d829a7ddcaad3db3798d73cad042af8a Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Fri, 6 Dec 2024 09:34:27 +1100 Subject: [PATCH 06/28] WIP fix new ValVar/ObjVar --- svf/include/SVFIR/SVFIR.h | 14 ++++++++++--- svf/lib/AE/Core/AbstractState.cpp | 34 +++++++++++++++---------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index d496e428c..83f6ef134 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -547,8 +547,13 @@ class SVFIR : public IRGraph /// Add a value (pointer) node inline NodeID addValNode(const SVFValue* val, NodeID i, const ICFGNode* icfgNode) { - SVFVar *node = new ValVar(val,i, ValVar::ValNode, icfgNode); - return addValNode(val, node, i); + if (SVFUtil::isa(val)) + return addConstantValNode(val, i, icfgNode); + else + { + SVFVar* node = new ValVar(val, i, ValVar::ValNode, icfgNode); + return addValNode(val, node, i); + } } NodeID addFunValNode(const CallGraphNode* callGraphNode, NodeID i, const ICFGNode* icfgNode) @@ -562,7 +567,10 @@ class SVFIR : public IRGraph { const MemObj* mem = getMemObj(val); assert(mem->getId() == i && "not same object id?"); - return addFIObjNode(mem); + if (SVFUtil::isa(val)) + return addConstantObjNode(val, i); + else + return addFIObjNode(mem); } NodeID addFunObjNode(const CallGraphNode* callGraphNode, NodeID id); diff --git a/svf/lib/AE/Core/AbstractState.cpp b/svf/lib/AE/Core/AbstractState.cpp index cdf0eb18d..18f12bad8 100644 --- a/svf/lib/AE/Core/AbstractState.cpp +++ b/svf/lib/AE/Core/AbstractState.cpp @@ -240,7 +240,7 @@ IntervalValue AbstractState::getElementIndex(const GepStmt* gep) for (int i = gep->getOffsetVarAndGepTypePairVec().size() - 1; i >= 0; i--) { AccessPath::IdxOperandPair IdxVarAndType = gep->getOffsetVarAndGepTypePairVec()[i]; - const SVFVar* var = gep->getOffsetVarAndGepTypePairVec()[i].first; + const SVFValue* value = gep->getOffsetVarAndGepTypePairVec()[i].first->getValue(); const SVFType* type = IdxVarAndType.second; // Variables to store the lower and upper bounds of the index value @@ -248,11 +248,11 @@ IntervalValue AbstractState::getElementIndex(const GepStmt* gep) s64_t idxUb; // Determine the lower and upper bounds based on whether the value is a constant - if (const ConstantIntObjVar* constInt = SVFUtil::dyn_cast(var)) + if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(value)) idxLb = idxUb = constInt->getSExtValue(); else { - IntervalValue idxItv = (*this)[var->getId()].getInterval(); + IntervalValue idxItv = (*this)[PAG::getPAG()->getValueNode(value)].getInterval(); if (idxItv.isBottom()) idxLb = idxUb = 0; else @@ -327,17 +327,17 @@ IntervalValue AbstractState::getByteOffset(const GepStmt* gep) else assert(false && "idxOperandType must be ArrType or PtrType"); - if (const ConstantIntObjVar* op = SVFUtil::dyn_cast(idxOperandVar)) + if (const SVFConstantInt* op = SVFUtil::dyn_cast(idxOperandVar->getValue())) { // Calculate the lower bound (lb) of the interval value s64_t lb = (double)Options::MaxFieldLimit() / elemByteSize >= op->getSExtValue() - ? op->getSExtValue() * elemByteSize - : Options::MaxFieldLimit(); + ? op->getSExtValue() * elemByteSize + : Options::MaxFieldLimit(); res = res + IntervalValue(lb, lb); } else { - u32_t idx = idxOperandVar->getId(); + u32_t idx = PAG::getPAG()->getValueNode(idxOperandVar->getValue()); IntervalValue idxVal = (*this)[idx].getInterval(); if (idxVal.isBottom()) @@ -347,12 +347,12 @@ IntervalValue AbstractState::getByteOffset(const GepStmt* gep) // Ensure the bounds are non-negative and within the field limit s64_t ub = (idxVal.ub().getIntNumeral() < 0) ? 0 : (double)Options::MaxFieldLimit() / elemByteSize >= idxVal.ub().getIntNumeral() - ? elemByteSize * idxVal.ub().getIntNumeral() - : Options::MaxFieldLimit(); + ? elemByteSize * idxVal.ub().getIntNumeral() + : Options::MaxFieldLimit(); s64_t lb = (idxVal.lb().getIntNumeral() < 0) ? 0 : (double)Options::MaxFieldLimit() / elemByteSize >= idxVal.lb().getIntNumeral() - ? elemByteSize * idxVal.lb().getIntNumeral() - : Options::MaxFieldLimit(); + ? elemByteSize * idxVal.lb().getIntNumeral() + : Options::MaxFieldLimit(); res = res + IntervalValue(lb, ub); } } @@ -395,9 +395,9 @@ void AbstractState::printAbstractState() const SVFUtil::outs().flags(std::ios::left); std::vector> varToAbsValVec(_varToAbsVal.begin(), _varToAbsVal.end()); std::sort(varToAbsValVec.begin(), varToAbsValVec.end(), [](const auto &a, const auto &b) - { - return a.first < b.first; - }); + { + return a.first < b.first; + }); for (const auto &item: varToAbsValVec) { SVFUtil::outs() << std::left << std::setw(fieldWidth) << ("Var" + std::to_string(item.first)); @@ -431,9 +431,9 @@ void AbstractState::printAbstractState() const std::vector> addrToAbsValVec(_addrToAbsVal.begin(), _addrToAbsVal.end()); std::sort(addrToAbsValVec.begin(), addrToAbsValVec.end(), [](const auto &a, const auto &b) - { - return a.first < b.first; - }); + { + return a.first < b.first; + }); for (const auto& item: addrToAbsValVec) { From 6653333b79f39b6cfae3cb5826e939cd90aaeaff Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Sat, 7 Dec 2024 16:08:51 +1100 Subject: [PATCH 07/28] 3) fix errors in dyn_cast --- svf/include/Graphs/GenericGraph.h | 40 +++++++++++++++++++++++++++---- svf/include/SVFIR/SVFVariables.h | 36 ++++++++++++++-------------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index 051fbb6eb..440d44728 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -171,10 +171,10 @@ class SVFBaseNode VarargNode, // │ │ ├── Represents a constant value node ConstantValNode, - // │ │ ├── Represents a constant data value node - ConstantDataValNode, // │ │ ├── Represents a global value node GlobalValueValNode, + // │ │ ├── Represents a constant data value node + ConstantDataValNode, // │ │ ├── Represents a black hole node BlackHoleNode, // │ │ ├── Represents a constant float-point value node @@ -199,10 +199,10 @@ class SVFBaseNode FunObjNode, // │ ├── ConstantObjNode: Types of constant object ConstantObjNode, - // │ ├── ConstantDataObjNode: Types of constant data object - ConstantDataObjNode, // │ ├── GlobalValueObjNode: Types of global value object GlobalValueObjNode, + // │ ├── ConstantDataObjNode: Types of constant data object + ConstantDataObjNode, // │ ├── ConstantFPObjNode: Types of constant float-point object ConstantFPObjNode, // │ ├── ConstantIntObjNode: Types of constant integer object @@ -363,6 +363,22 @@ class SVFBaseNode return n <= DummyValNode && n >= ValNode; } + static inline bool isConstantValVar(GNodeK n) + { + static_assert(ConstantNullptrValNode - ConstantValNode == 6, + "The number of ConstantValVarKinds has changed, make sure " + "the range is correct"); + return n <= ConstantNullptrValNode && n >= ConstantValNode; + } + + static inline bool isConstantDataValVar(GNodeK n) + { + static_assert(ConstantNullptrValNode - ConstantDataValNode == 4, + "The number of ConstantDataValVarKinds has changed, make " + "sure the range is correct"); + return n <= ConstantIntValNode && n >= ConstantValNode; + } + static inline bool isObjVarKinds(GNodeK n) { static_assert(DummyObjNode - ObjNode == 10, @@ -379,6 +395,22 @@ class SVFBaseNode return n <= ConstantNullptrObjNode && n >= FIObjNode; } + static inline bool isConstantObjVarKinds(GNodeK n) + { + static_assert(ConstantNullptrObjNode - ConstantObjNode == 5, + "The number of ConstantObjVarKinds has changed, make " + "sure the range is correct"); + return n <= ConstantNullptrObjNode && n >= ConstantObjNode; + } + + static inline bool isConstantDataObjVarKinds(GNodeK n) + { + static_assert(ConstantNullptrObjNode - ConstantDataObjNode == 3, + "The number of ConstantDataObjVarKinds has changed, make " + "sure the range is correct"); + return n <= ConstantNullptrObjNode && n >= ConstantDataObjNode; + } + static inline bool isVFGNodeKinds(GNodeK n) { static_assert(MInterPhi - Cmp == 24, diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 20a14520e..cb55c4f3d 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -698,25 +698,25 @@ class ConstantValVar: public ValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const ConstantValVar*) { return true; } static inline bool classof(const ValVar* node) { - return node->getNodeKind() == ConstantValNode; + return isConstantValVar(node->getNodeKind()); } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == ConstantValNode; + return isConstantValVar(node->getNodeKind()); } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == ConstantValNode; + return isConstantValVar(node->getNodeKind()); } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == ConstantValNode; + return isConstantValVar(node->getNodeKind()); } //@} @@ -746,25 +746,25 @@ class ConstantDataValVar: public ConstantValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const ConstantDataValVar*) { return true; } static inline bool classof(const ValVar* node) { - return node->getNodeKind() == ConstantDataValNode; + return isConstantDataValVar(node->getNodeKind()); } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == ConstantDataValNode; + return isConstantDataValVar(node->getNodeKind()); } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == ConstantDataValNode; + return isConstantDataValVar(node->getNodeKind()); } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == ConstantDataValNode; + return isConstantDataValVar(node->getNodeKind()); } //@} @@ -1037,19 +1037,19 @@ class ConstantObjVar: public FIObjVar } static inline bool classof(const ObjVar* node) { - return isFIObjVarKinds(node->getNodeKind()); + return isConstantObjVarKinds(node->getNodeKind()); } static inline bool classof(const SVFVar* node) { - return isFIObjVarKinds(node->getNodeKind()); + return isConstantObjVarKinds(node->getNodeKind()); } static inline bool classof(const GenericPAGNodeTy* node) { - return isFIObjVarKinds(node->getNodeKind()); + return isConstantObjVarKinds(node->getNodeKind()); } static inline bool classof(const SVFBaseNode* node) { - return isFIObjVarKinds(node->getNodeKind()); + return isConstantObjVarKinds(node->getNodeKind()); } //@} @@ -1152,20 +1152,20 @@ class ConstantDataObjVar: public ConstantObjVar { } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == SVFVar::ConstantDataObjNode; + return isConstantDataObjVarKinds(node->getNodeKind()); } static inline bool classof(const ObjVar* node) { - return node->getNodeKind() == SVFVar::ConstantDataObjNode; + return isConstantDataObjVarKinds(node->getNodeKind()); } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == SVFVar::ConstantDataObjNode; + return isConstantDataObjVarKinds(node->getNodeKind()); } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == SVFVar::ConstantDataObjNode; + return isConstantDataObjVarKinds(node->getNodeKind()); } //@} From 2a9c15381f59994f7bc658c5a198237a6a60df57 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Sun, 8 Dec 2024 22:13:34 +1100 Subject: [PATCH 08/28] refactor 1. create constant var/obj in SVFIRBuilder --- svf-llvm/lib/SVFIRBuilder.cpp | 6 +++ svf/include/SVFIR/SVFIR.h | 80 +++++++++++++++++++--------- svf/include/SVFIR/SVFVariables.h | 86 ++++++++++++++++++++----------- svf/lib/AE/Core/AbstractState.cpp | 59 ++++++++++----------- 4 files changed, 144 insertions(+), 87 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index 7cc4b5441..2d12b4903 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -246,6 +246,9 @@ void SVFIRBuilder::initialiseNodes() const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); pag->addFunValNode(cgn, iter->second, icfgNode); } + else if (const Constant* cons = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + pag->addConstantValNode(iter->first, iter->second, icfgNode); + } else { pag->addValNode(iter->first, iter->second, icfgNode); @@ -264,6 +267,9 @@ void SVFIRBuilder::initialiseNodes() { pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); } + else if (const Constant* cons = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + pag->addConstantObjNode(iter->first, iter->second); + } else { pag->addObjNode(iter->first, iter->second); diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 83f6ef134..b6cd32f00 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -547,13 +547,8 @@ class SVFIR : public IRGraph /// Add a value (pointer) node inline NodeID addValNode(const SVFValue* val, NodeID i, const ICFGNode* icfgNode) { - if (SVFUtil::isa(val)) - return addConstantValNode(val, i, icfgNode); - else - { - SVFVar* node = new ValVar(val, i, ValVar::ValNode, icfgNode); - return addValNode(val, node, i); - } + SVFVar* node = new ValVar(val, i, ValVar::ValNode, icfgNode); + return addValNode(val, node, i); } NodeID addFunValNode(const CallGraphNode* callGraphNode, NodeID i, const ICFGNode* icfgNode) @@ -567,10 +562,7 @@ class SVFIR : public IRGraph { const MemObj* mem = getMemObj(val); assert(mem->getId() == i && "not same object id?"); - if (SVFUtil::isa(val)) - return addConstantObjNode(val, i); - else - return addFIObjNode(mem); + return addFIObjNode(mem); } NodeID addFunObjNode(const CallGraphNode* callGraphNode, NodeID id); @@ -587,41 +579,74 @@ class SVFIR : public IRGraph return addNode(node,i); } + inline bool isConstant(const SVFValue* curInst) { + if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) + { + return true; + } + // ConstantInt + else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) + { + return true; + } + // constNullptr + else if (const SVFConstantNullPtr* constNullPtr = + SVFUtil::dyn_cast(curInst)) { + return true; + } + + else if (const SVFGlobalValue* globalVal = + SVFUtil::dyn_cast(curInst)) + { + return true; + } + + else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { + return true; + } + + else if (const SVFConstant* constVal = SVFUtil::dyn_cast(curInst)) + { + return true; + } + return false; + } + + inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantFPValVar(constFp, i, icfgNode); + SVFVar* node = new ConstantFPValVar(constFp->getFPValue(), i, icfgNode); return addNode(node,i); } // ConstantInt else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantIntValVar(constInt, i, icfgNode); + SVFVar* node = new ConstantIntValVar(constInt->getSExtValue(), constInt->getZExtValue(), i, icfgNode); return addNode(node,i); } // constNullptr else if (const SVFConstantNullPtr* constNullPtr = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantNullPtrValVar(constNullPtr, i, icfgNode); + SVFVar* node = new ConstantNullPtrValVar(i, icfgNode); return addNode(node,i); } else if (const SVFGlobalValue* globalVal = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new GlobalValueValvar(globalVal, i, icfgNode); + SVFVar* node = new GlobalValueValvar(i, icfgNode); return addNode(node,i); } else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantDataValVar(dataVal, i, icfgNode); + SVFVar* node = new ConstantDataValVar(i, icfgNode); return addNode(node,i); } - else if (const SVFConstant* constVal = - SVFUtil::dyn_cast(curInst)) + else if (const SVFConstant* constVal = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantValVar(constVal, i, icfgNode); + SVFVar* node = new ConstantValVar(i, icfgNode); return addNode(node, i); } assert(false && "not a constant value?"); @@ -639,33 +664,38 @@ class SVFIR : public IRGraph memToFieldsMap[base].set(mem->getId()); if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) { - ConstantFPObjVar* node = new ConstantFPObjVar(constFp, mem->getId(), mem); + ConstantFPObjVar* node = new ConstantFPObjVar(constFp->getFPValue(), mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } // ConstantInt else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) { - ConstantIntObjVar* node = new ConstantIntObjVar(constInt, mem->getId(), mem); + ConstantIntObjVar* node = new ConstantIntObjVar(constInt->getSExtValue(), constInt->getZExtValue(), mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + // constNullptr + else if (const SVFConstantNullPtr* constNullPtr = + SVFUtil::dyn_cast(curInst)) { + SVFVar* node = new ConstantNullPtrObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } - //TODO: constNullptrObj else if (const SVFGlobalValue* globalVal = SVFUtil::dyn_cast(curInst)) { - GlobalValueObjVar* node = new GlobalValueObjVar(globalVal, mem->getId(), mem); + GlobalValueObjVar* node = new GlobalValueObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { - ConstantDataObjVar* node = new ConstantDataObjVar(dataVal, mem->getId(), mem); + ConstantDataObjVar* node = new ConstantDataObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (const SVFConstant* constVal = SVFUtil::dyn_cast(curInst)) { - ConstantObjVar* node = new ConstantObjVar(constVal, mem->getId(), mem); + ConstantObjVar* node = new ConstantObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } assert(false && "not a constant value?"); diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index cb55c4f3d..f3fb568f2 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -721,9 +721,9 @@ class ConstantValVar: public ValVar //@} /// Constructor - ConstantValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + ConstantValVar(NodeID i, const ICFGNode* icn, PNODEK ty = ConstantValNode) - : ValVar(val, i, ty, icn) + : ValVar(nullptr, i, ty, icn) { } @@ -769,9 +769,9 @@ class ConstantDataValVar: public ConstantValVar //@} /// Constructor - ConstantDataValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + ConstantDataValVar(NodeID i, const ICFGNode* icn, PNODEK ty = ConstantDataValNode) - : ConstantValVar(val, i, icn, ty) + : ConstantValVar(i, icn, ty) { } @@ -817,9 +817,9 @@ class GlobalValueValvar: public ConstantValVar //@} /// Constructor - GlobalValueValvar(const SVFValue* val, NodeID i, const ICFGNode* icn, + GlobalValueValvar(NodeID i, const ICFGNode* icn, PNODEK ty = GlobalValueValNode) - : ConstantValVar(val, i, icn, ty) + : ConstantValVar(i, icn, ty) { } @@ -866,7 +866,7 @@ class BlackHoleVar: public ConstantDataValVar /// Constructor BlackHoleVar(NodeID i, PNODEK ty = BlackHoleNode) - : ConstantDataValVar(nullptr, i, nullptr, ty) + : ConstantDataValVar(i, nullptr, ty) { } @@ -880,6 +880,8 @@ class ConstantFPValVar: public ConstantDataValVar { friend class SVFIRWriter; friend class SVFIRReader; +private: + float dval; public: /// Methods for support type inquiry through isa, cast, and dyn_cast: @@ -906,10 +908,15 @@ class ConstantFPValVar: public ConstantDataValVar } //@} + inline double getFPValue() const + { + return dval; + } + /// Constructor - ConstantFPValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + ConstantFPValVar(double dv, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantFPValNode) - : ConstantDataValVar(val, i, icn, ty) + : ConstantDataValVar(i, icn, ty), dval(dv) { } @@ -928,6 +935,9 @@ class ConstantIntValVar: public ConstantDataValVar { friend class SVFIRWriter; friend class SVFIRReader; +private: + u64_t zval; + s64_t sval; public: /// Methods for support type inquiry through isa, cast, and dyn_cast: @@ -954,10 +964,21 @@ class ConstantIntValVar: public ConstantDataValVar } //@} + s64_t getSExtValue() const + { + return sval; + } + + + u64_t getZExtValue() const + { + return zval; + } + /// Constructor - ConstantIntValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + ConstantIntValVar(s64_t sv, u64_t zv, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantIntValNode) - : ConstantDataValVar(val, i, icn, ty) + : ConstantDataValVar(i, icn, ty), zval(zv), sval(sv) { } @@ -1002,9 +1023,9 @@ class ConstantNullPtrValVar: public ConstantDataValVar //@} /// Constructor - ConstantNullPtrValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + ConstantNullPtrValVar(NodeID i, const ICFGNode* icn, PNODEK ty = ConstantNullptrValNode) - : ConstantDataValVar(val, i, icn, ty) + : ConstantDataValVar(i, icn, ty) { } @@ -1054,9 +1075,9 @@ class ConstantObjVar: public FIObjVar //@} /// Constructor - ConstantObjVar(const SVFValue* val, NodeID i, const MemObj* mem, + ConstantObjVar(NodeID i, const MemObj* mem, PNODEK ty = ConstantObjNode) - : FIObjVar(val, i, mem, ty) + : FIObjVar(nullptr, i, mem, ty) { } @@ -1120,8 +1141,8 @@ class GlobalValueObjVar: public ConstantObjVar { //@} /// Constructor - GlobalValueObjVar(const SVFValue* val, NodeID i, const MemObj* mem, - PNODEK ty = GlobalValueObjNode): ConstantObjVar(val,i,mem,ty){ + GlobalValueObjVar(NodeID i, const MemObj* mem, + PNODEK ty = GlobalValueObjNode): ConstantObjVar(i,mem,ty){ } @@ -1170,8 +1191,8 @@ class ConstantDataObjVar: public ConstantObjVar { //@} /// Constructor - ConstantDataObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) - : ConstantObjVar(val, i, m, ty) + ConstantDataObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) + : ConstantObjVar(i, m, ty) { } @@ -1194,6 +1215,9 @@ class ConstantFPObjVar: public ConstantDataObjVar /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) ConstantFPObjVar(NodeID i) : ConstantDataObjVar(i) {} +private: + float dval; + public: //@{ Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const ConstantDataObjVar*) @@ -1220,14 +1244,14 @@ class ConstantFPObjVar: public ConstantDataObjVar //@} /// Constructor - ConstantFPObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) - : ConstantDataObjVar(val, i, m, ty) + ConstantFPObjVar(double dv, NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) + : ConstantDataObjVar(i, m, ty), dval(dv) { } - inline double getFPValue () const + inline double getFPValue() const { - return SVFUtil::dyn_cast(value)->getFPValue(); + return dval; } @@ -1251,6 +1275,10 @@ class ConstantIntObjVar: public ConstantDataObjVar /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) ConstantIntObjVar(NodeID i) : ConstantDataObjVar(i) {} +private: + u64_t zval; + s64_t sval; + public: //@{ Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const ConstantDataObjVar*) @@ -1277,19 +1305,19 @@ class ConstantIntObjVar: public ConstantDataObjVar s64_t getSExtValue() const { - return SVFUtil::dyn_cast(value)->getSExtValue(); + return sval; } u64_t getZExtValue() const { - return SVFUtil::dyn_cast(value)->getZExtValue(); + return zval; } //@} /// Constructor - ConstantIntObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) - : ConstantDataObjVar(val, i, m, ty) + ConstantIntObjVar(s64_t sv, u64_t zv, NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) + : ConstantDataObjVar(i, m, ty), zval(zv), sval(sv) { } @@ -1340,8 +1368,8 @@ class ConstantNullPtrObjVar: public ConstantDataObjVar //@} /// Constructor - ConstantNullPtrObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantNullptrObjNode) - : ConstantDataObjVar(val, i, m, ty) + ConstantNullPtrObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantNullptrObjNode) + : ConstantDataObjVar(i, m, ty) { } diff --git a/svf/lib/AE/Core/AbstractState.cpp b/svf/lib/AE/Core/AbstractState.cpp index 18f12bad8..3f947988e 100644 --- a/svf/lib/AE/Core/AbstractState.cpp +++ b/svf/lib/AE/Core/AbstractState.cpp @@ -181,46 +181,39 @@ void AbstractState::initObjVar(ObjVar* objVar) NodeID varId = objVar->getId(); // Check if the object variable has an associated value - if (objVar->hasValue()) - { - const MemObj* obj = objVar->getMemObj(); - // Handle constant data, arrays, and structures - if (obj->isConstDataOrConstGlobal() || obj->isConstantArray() || obj->isConstantStruct()) + const MemObj* obj = objVar->getMemObj(); + + // Handle constant data, arrays, and structures + if (obj->isConstDataOrConstGlobal() || obj->isConstantArray() || obj->isConstantStruct()) + { + if (const ConstantIntObjVar* consInt = SVFUtil::dyn_cast(objVar)) { - if (const ConstantIntObjVar* consInt = SVFUtil::dyn_cast(objVar)) - { - s64_t numeral = consInt->getSExtValue(); - (*this)[varId] = IntervalValue(numeral, numeral); - } - else if (const ConstantFPObjVar* consFP = SVFUtil::dyn_cast(objVar)) - { - (*this)[varId] = IntervalValue(consFP->getFPValue(), consFP->getFPValue()); - } - else if (SVFUtil::isa(objVar)) - { - (*this)[varId] = IntervalValue(0, 0); - } - else if (SVFUtil::isa(objVar)) - { - (*this)[varId] = AddressValue(AbstractState::getVirtualMemAddress(varId)); - } - else if (obj->isConstantArray() || obj->isConstantStruct()) - { - (*this)[varId] = IntervalValue::top(); - } - else - { - (*this)[varId] = IntervalValue::top(); - } + s64_t numeral = consInt->getSExtValue(); + (*this)[varId] = IntervalValue(numeral, numeral); } - // Handle non-constant memory objects - else + else if (const ConstantFPObjVar* consFP = SVFUtil::dyn_cast(objVar)) + { + (*this)[varId] = IntervalValue(consFP->getFPValue(), consFP->getFPValue()); + } + else if (SVFUtil::isa(objVar)) + { + (*this)[varId] = IntervalValue(0, 0); + } + else if (SVFUtil::isa(objVar)) { (*this)[varId] = AddressValue(AbstractState::getVirtualMemAddress(varId)); } + else if (obj->isConstantArray() || obj->isConstantStruct()) + { + (*this)[varId] = IntervalValue::top(); + } + else + { + (*this)[varId] = IntervalValue::top(); + } } - // If the object variable does not have an associated value, set it to a virtual memory address + // Handle non-constant memory objects else { (*this)[varId] = AddressValue(AbstractState::getVirtualMemAddress(varId)); From 1ac65e04e58af00a5f34e69a85c25a120e07d432 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Sun, 8 Dec 2024 22:29:35 +1100 Subject: [PATCH 09/28] refactor 1. create constant var/obj in SVFIRBuilder --- svf/include/SVFIR/SVFIR.h | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index b6cd32f00..6bbdc8189 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -579,39 +579,6 @@ class SVFIR : public IRGraph return addNode(node,i); } - inline bool isConstant(const SVFValue* curInst) { - if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) - { - return true; - } - // ConstantInt - else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) - { - return true; - } - // constNullptr - else if (const SVFConstantNullPtr* constNullPtr = - SVFUtil::dyn_cast(curInst)) { - return true; - } - - else if (const SVFGlobalValue* globalVal = - SVFUtil::dyn_cast(curInst)) - { - return true; - } - - else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { - return true; - } - - else if (const SVFConstant* constVal = SVFUtil::dyn_cast(curInst)) - { - return true; - } - return false; - } - inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) From 2ea14d01a045654d913ebb829c4197c522d150ff Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Sun, 8 Dec 2024 22:38:44 +1100 Subject: [PATCH 10/28] refactor 1. create constant var/obj in SVFIRBuilder --- svf-llvm/lib/SVFIRBuilder.cpp | 4 ++-- svf/include/SVFIR/SVFIR.h | 21 ++++++++------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index 2d12b4903..3c4e48b5f 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -246,7 +246,7 @@ void SVFIRBuilder::initialiseNodes() const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); pag->addFunValNode(cgn, iter->second, icfgNode); } - else if (const Constant* cons = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + else if (SVFUtil::isa(llvmModuleSet()->getLLVMValue(iter->first))) { pag->addConstantValNode(iter->first, iter->second, icfgNode); } else @@ -267,7 +267,7 @@ void SVFIRBuilder::initialiseNodes() { pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); } - else if (const Constant* cons = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + else if (SVFUtil::isa(llvmModuleSet()->getLLVMValue(iter->first))) { pag->addConstantObjNode(iter->first, iter->second); } else diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 6bbdc8189..463622199 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -593,25 +593,23 @@ class SVFIR : public IRGraph return addNode(node,i); } // constNullptr - else if (const SVFConstantNullPtr* constNullPtr = - SVFUtil::dyn_cast(curInst)) { + else if (SVFUtil::isa(curInst)) { SVFVar* node = new ConstantNullPtrValVar(i, icfgNode); return addNode(node,i); } - else if (const SVFGlobalValue* globalVal = - SVFUtil::dyn_cast(curInst)) + else if (SVFUtil::isa(curInst)) { SVFVar* node = new GlobalValueValvar(i, icfgNode); return addNode(node,i); } - else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { + else if (SVFUtil::isa(curInst)) { SVFVar* node = new ConstantDataValVar(i, icfgNode); return addNode(node,i); } - else if (const SVFConstant* constVal = SVFUtil::dyn_cast(curInst)) + else if (SVFUtil::isa(curInst)) { SVFVar* node = new ConstantValVar(i, icfgNode); return addNode(node, i); @@ -641,26 +639,23 @@ class SVFIR : public IRGraph return addObjNode(mem->getValue(), node, mem->getId()); } // constNullptr - else if (const SVFConstantNullPtr* constNullPtr = - SVFUtil::dyn_cast(curInst)) { + else if (SVFUtil::isa(curInst)) { SVFVar* node = new ConstantNullPtrObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } - else if (const SVFGlobalValue* globalVal = - SVFUtil::dyn_cast(curInst)) + else if (SVFUtil::isa(curInst)) { GlobalValueObjVar* node = new GlobalValueObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } - else if (const SVFConstantData* dataVal = SVFUtil::dyn_cast(curInst)) { + else if (SVFUtil::isa(curInst)) { ConstantDataObjVar* node = new ConstantDataObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } - else if (const SVFConstant* constVal = - SVFUtil::dyn_cast(curInst)) + else if (SVFUtil::isa(curInst)) { ConstantObjVar* node = new ConstantObjVar(mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); From abe509f0ab38cf206389e2e3dd90e95808cba6f2 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Mon, 9 Dec 2024 14:33:36 +1100 Subject: [PATCH 11/28] add SVFValue back --- svf-llvm/lib/SVFIRBuilder.cpp | 6 ---- svf/include/Graphs/GenericGraph.h | 2 +- svf/include/SVFIR/SVFIR.h | 24 +++++++-------- svf/include/SVFIR/SVFVariables.h | 50 +++++++++++++++---------------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index 3c4e48b5f..e707d8735 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -83,11 +83,6 @@ SVFIR* SVFIRBuilder::build() { it.second->gNode = llvmModuleSet()->getCallGraphNode(func); } - else if (const Function* func = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue( - it.second->getValue()))) - { - it.second->gNode = llvmModuleSet()->getCallGraphNode(func); - } } CHGraph* chg = new CHGraph(pag->getModule()); @@ -226,7 +221,6 @@ void SVFIRBuilder::initialiseNodes() if(iter->second == symTable->blkPtrSymID()) continue; if (iter->second == symTable->nullPtrSymID()) { - //onst SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode pag->addConstantValNode(iter->first, iter->second, nullptr); } diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index 440d44728..3675c4a8f 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -376,7 +376,7 @@ class SVFBaseNode static_assert(ConstantNullptrValNode - ConstantDataValNode == 4, "The number of ConstantDataValVarKinds has changed, make " "sure the range is correct"); - return n <= ConstantIntValNode && n >= ConstantValNode; + return n <= ConstantIntValNode && n >= ConstantDataValNode; } static inline bool isObjVarKinds(GNodeK n) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 463622199..ac6865b79 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -583,35 +583,35 @@ class SVFIR : public IRGraph inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantFPValVar(constFp->getFPValue(), i, icfgNode); + SVFVar* node = new ConstantFPValVar(curInst, constFp->getFPValue(), i, icfgNode); return addNode(node,i); } // ConstantInt else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) { - SVFVar* node = new ConstantIntValVar(constInt->getSExtValue(), constInt->getZExtValue(), i, icfgNode); + SVFVar* node = new ConstantIntValVar(curInst, constInt->getSExtValue(), constInt->getZExtValue(), i, icfgNode); return addNode(node,i); } // constNullptr else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantNullPtrValVar(i, icfgNode); + SVFVar* node = new ConstantNullPtrValVar(curInst, i, icfgNode); return addNode(node,i); } else if (SVFUtil::isa(curInst)) { - SVFVar* node = new GlobalValueValvar(i, icfgNode); + SVFVar* node = new GlobalValueValvar(curInst, i, icfgNode); return addNode(node,i); } else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantDataValVar(i, icfgNode); + SVFVar* node = new ConstantDataValVar(curInst, i, icfgNode); return addNode(node,i); } else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantValVar(i, icfgNode); + SVFVar* node = new ConstantValVar(curInst,i, icfgNode); return addNode(node, i); } assert(false && "not a constant value?"); @@ -629,35 +629,35 @@ class SVFIR : public IRGraph memToFieldsMap[base].set(mem->getId()); if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) { - ConstantFPObjVar* node = new ConstantFPObjVar(constFp->getFPValue(), mem->getId(), mem); + ConstantFPObjVar* node = new ConstantFPObjVar(curInst, constFp->getFPValue(), mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } // ConstantInt else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) { - ConstantIntObjVar* node = new ConstantIntObjVar(constInt->getSExtValue(), constInt->getZExtValue(), mem->getId(), mem); + ConstantIntObjVar* node = new ConstantIntObjVar(curInst, constInt->getSExtValue(), constInt->getZExtValue(), mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } // constNullptr else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantNullPtrObjVar(mem->getId(), mem); + SVFVar* node = new ConstantNullPtrObjVar(curInst, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (SVFUtil::isa(curInst)) { - GlobalValueObjVar* node = new GlobalValueObjVar(mem->getId(), mem); + GlobalValueObjVar* node = new GlobalValueObjVar(curInst, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (SVFUtil::isa(curInst)) { - ConstantDataObjVar* node = new ConstantDataObjVar(mem->getId(), mem); + ConstantDataObjVar* node = new ConstantDataObjVar(curInst, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } else if (SVFUtil::isa(curInst)) { - ConstantObjVar* node = new ConstantObjVar(mem->getId(), mem); + ConstantObjVar* node = new ConstantObjVar(curInst, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } assert(false && "not a constant value?"); diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index f3fb568f2..3736128e8 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -721,9 +721,9 @@ class ConstantValVar: public ValVar //@} /// Constructor - ConstantValVar(NodeID i, const ICFGNode* icn, + ConstantValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantValNode) - : ValVar(nullptr, i, ty, icn) + : ValVar(val, i, ty, icn) { } @@ -769,9 +769,9 @@ class ConstantDataValVar: public ConstantValVar //@} /// Constructor - ConstantDataValVar(NodeID i, const ICFGNode* icn, + ConstantDataValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantDataValNode) - : ConstantValVar(i, icn, ty) + : ConstantValVar(val, i, icn, ty) { } @@ -817,9 +817,9 @@ class GlobalValueValvar: public ConstantValVar //@} /// Constructor - GlobalValueValvar(NodeID i, const ICFGNode* icn, + GlobalValueValvar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = GlobalValueValNode) - : ConstantValVar(i, icn, ty) + : ConstantValVar(val, i, icn, ty) { } @@ -866,7 +866,7 @@ class BlackHoleVar: public ConstantDataValVar /// Constructor BlackHoleVar(NodeID i, PNODEK ty = BlackHoleNode) - : ConstantDataValVar(i, nullptr, ty) + : ConstantDataValVar(nullptr, i, nullptr, ty) { } @@ -914,9 +914,9 @@ class ConstantFPValVar: public ConstantDataValVar } /// Constructor - ConstantFPValVar(double dv, NodeID i, const ICFGNode* icn, + ConstantFPValVar(const SVFValue* val, double dv, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantFPValNode) - : ConstantDataValVar(i, icn, ty), dval(dv) + : ConstantDataValVar(val, i, icn, ty), dval(dv) { } @@ -976,9 +976,9 @@ class ConstantIntValVar: public ConstantDataValVar } /// Constructor - ConstantIntValVar(s64_t sv, u64_t zv, NodeID i, const ICFGNode* icn, + ConstantIntValVar(const SVFValue* val, s64_t sv, u64_t zv, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantIntValNode) - : ConstantDataValVar(i, icn, ty), zval(zv), sval(sv) + : ConstantDataValVar(val, i, icn, ty), zval(zv), sval(sv) { } @@ -1023,9 +1023,9 @@ class ConstantNullPtrValVar: public ConstantDataValVar //@} /// Constructor - ConstantNullPtrValVar(NodeID i, const ICFGNode* icn, + ConstantNullPtrValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantNullptrValNode) - : ConstantDataValVar(i, icn, ty) + : ConstantDataValVar(val, i, icn, ty) { } @@ -1075,9 +1075,9 @@ class ConstantObjVar: public FIObjVar //@} /// Constructor - ConstantObjVar(NodeID i, const MemObj* mem, + ConstantObjVar(const SVFValue* val, NodeID i, const MemObj* mem, PNODEK ty = ConstantObjNode) - : FIObjVar(nullptr, i, mem, ty) + : FIObjVar(val, i, mem, ty) { } @@ -1141,8 +1141,8 @@ class GlobalValueObjVar: public ConstantObjVar { //@} /// Constructor - GlobalValueObjVar(NodeID i, const MemObj* mem, - PNODEK ty = GlobalValueObjNode): ConstantObjVar(i,mem,ty){ + GlobalValueObjVar(const SVFValue* val, NodeID i, const MemObj* mem, + PNODEK ty = GlobalValueObjNode): ConstantObjVar(val, i,mem,ty){ } @@ -1191,8 +1191,8 @@ class ConstantDataObjVar: public ConstantObjVar { //@} /// Constructor - ConstantDataObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) - : ConstantObjVar(i, m, ty) + ConstantDataObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) + : ConstantObjVar(val, i, m, ty) { } @@ -1244,8 +1244,8 @@ class ConstantFPObjVar: public ConstantDataObjVar //@} /// Constructor - ConstantFPObjVar(double dv, NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) - : ConstantDataObjVar(i, m, ty), dval(dv) + ConstantFPObjVar(const SVFValue* val, double dv, NodeID i, const MemObj* m, PNODEK ty = ConstantFPObjNode) + : ConstantDataObjVar(val, i, m, ty), dval(dv) { } @@ -1316,8 +1316,8 @@ class ConstantIntObjVar: public ConstantDataObjVar //@} /// Constructor - ConstantIntObjVar(s64_t sv, u64_t zv, NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) - : ConstantDataObjVar(i, m, ty), zval(zv), sval(sv) + ConstantIntObjVar(const SVFValue* val, s64_t sv, u64_t zv, NodeID i, const MemObj* m, PNODEK ty = ConstantIntObjNode) + : ConstantDataObjVar(val, i, m, ty), zval(zv), sval(sv) { } @@ -1368,8 +1368,8 @@ class ConstantNullPtrObjVar: public ConstantDataObjVar //@} /// Constructor - ConstantNullPtrObjVar(NodeID i, const MemObj* m, PNODEK ty = ConstantNullptrObjNode) - : ConstantDataObjVar(i, m, ty) + ConstantNullPtrObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantNullptrObjNode) + : ConstantDataObjVar(val, i, m, ty) { } From ccc792395c5fef2658fd7a3129b12c3aee62825c Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Mon, 9 Dec 2024 14:37:19 +1100 Subject: [PATCH 12/28] add SVFValue back --- svf/include/SVFIR/SVFIR.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index ac6865b79..e610c30f8 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -618,12 +618,6 @@ class SVFIR : public IRGraph } inline NodeID addConstantObjNode(const SVFValue* curInst, const NodeID i) { - // const MemObj* mem = getMemObj(callGraphNode->getFunction()); - // assert(mem->getId() == id && "not same object id?"); - // //assert(findPAGNode(i) == false && "this node should not be created before"); NodeID base = mem->getId(); - // memToFieldsMap[base].set(mem->getId()); - // FunObjVar*node = new FunObjVar(callGraphNode, mem->getId(), mem); - // return addObjNode(mem->getValue(), node, mem->getId()); const MemObj* mem = getMemObj(curInst); NodeID base = mem->getId(); memToFieldsMap[base].set(mem->getId()); From e06cb6232b816414cbace1f644819d667eec1fc8 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Mon, 9 Dec 2024 15:03:26 +1100 Subject: [PATCH 13/28] refactor 1. move toString() to cpp 2. setAttribute --- svf-llvm/lib/SVFIRBuilder.cpp | 6 +- svf/include/SVFIR/SVFVariables.h | 109 +++-------------------- svf/lib/SVFIR/SVFVariables.cpp | 144 +++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 99 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index e707d8735..e35cca134 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -240,8 +240,9 @@ void SVFIRBuilder::initialiseNodes() const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); pag->addFunValNode(cgn, iter->second, icfgNode); } - else if (SVFUtil::isa(llvmModuleSet()->getLLVMValue(iter->first))) { + else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { pag->addConstantValNode(iter->first, iter->second, icfgNode); + llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); } else { @@ -261,8 +262,9 @@ void SVFIRBuilder::initialiseNodes() { pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); } - else if (SVFUtil::isa(llvmModuleSet()->getLLVMValue(iter->first))) { + else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { pag->addConstantObjNode(iter->first, iter->second); + llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); } else { diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 3736128e8..9aa0517d0 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -728,14 +728,7 @@ class ConstantValVar: public ValVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantValNode ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantDataValVar: public ConstantValVar @@ -776,14 +769,7 @@ class ConstantDataValVar: public ConstantValVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantDataValNode ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class GlobalValueValvar: public ConstantValVar @@ -824,14 +810,7 @@ class GlobalValueValvar: public ConstantValVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "GlobalValueValVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class BlackHoleVar: public ConstantDataValVar @@ -921,14 +900,7 @@ class ConstantFPValVar: public ConstantDataValVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantFPValVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantIntValVar: public ConstantDataValVar @@ -982,14 +954,7 @@ class ConstantIntValVar: public ConstantDataValVar { } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantIntValNode ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantNullPtrValVar: public ConstantDataValVar @@ -1030,14 +995,7 @@ class ConstantNullPtrValVar: public ConstantDataValVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantNullPtrValNode ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantObjVar: public FIObjVar @@ -1089,14 +1047,7 @@ class ConstantObjVar: public FIObjVar return " (base object)"; } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantObjVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class GlobalValueObjVar: public ConstantObjVar { @@ -1147,14 +1098,7 @@ class GlobalValueObjVar: public ConstantObjVar { } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "GlobalValueObjVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantDataObjVar: public ConstantObjVar { @@ -1196,14 +1140,7 @@ class ConstantDataObjVar: public ConstantObjVar { { } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantDataObjVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantFPObjVar: public ConstantDataObjVar @@ -1255,15 +1192,7 @@ class ConstantFPObjVar: public ConstantDataObjVar } - virtual const std::string toString() const - { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantFPObjVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; class ConstantIntObjVar: public ConstantDataObjVar @@ -1322,14 +1251,7 @@ class ConstantIntObjVar: public ConstantDataObjVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantIntObjVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; @@ -1374,14 +1296,7 @@ class ConstantNullPtrObjVar: public ConstantDataObjVar } - virtual const std::string toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantNullPtrObjVar ID: " << getId(); - rawstr << "\n"; - rawstr << value->toString(); - return rawstr.str(); - } + virtual const std::string toString() const; }; /* * Unique Return node of a procedure diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index b134c7d30..46703d060 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -211,6 +211,150 @@ const std::string FunValVar::toString() const return rawstr.str(); } +const std::string ConstantValVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantValNode ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantDataValVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantDataValNode ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string GlobalValueValvar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "GlobalValueValVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantFPValVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantFPValNode ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantIntValVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantIntValNode ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantNullPtrValVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantObjVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantObjVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantNullPtrValNode ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string GlobalValueObjVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "GlobalValueObjNode ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantDataObjVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantDataObjVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantFPObjVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantFPObjVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantIntObjVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantIntObjVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + +const std::string ConstantNullPtrObjVar::toString() const { + std::string str; + std::stringstream rawstr(str); + rawstr << "ConstantNullPtrObjVar ID: " << getId(); + if (Options::ShowSVFIRValue()) + { + rawstr << "\n"; + rawstr << valueOnlyToString(); + } + return rawstr.str(); +} + FunObjVar::FunObjVar(const CallGraphNode* cgNode, NodeID i, const MemObj* mem, PNODEK ty) : FIObjVar(nullptr, i, mem, ty), callGraphNode(cgNode) From 60f938653426545e33bddb528787dab21ec09ef4 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Mon, 9 Dec 2024 20:08:37 +1100 Subject: [PATCH 14/28] fix WPA test case --- svf/lib/SVFIR/SVFVariables.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 46703d060..36c0259cf 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -46,6 +46,13 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : switch (k) { case ValNode: + case ConstantValNode: + case ConstantDataValNode: + case GlobalValueValNode: + case BlackHoleNode: + case ConstantFPValNode: + case ConstantIntValNode: + case ConstantNullptrValNode: case GepValNode: { assert(val != nullptr && "value is nullptr for ValVar or GepValNode"); @@ -60,13 +67,6 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : } case FunValNode: case VarargNode: - case ConstantValNode: - case ConstantDataValNode: - case GlobalValueValNode: - case BlackHoleNode: - case ConstantFPValNode: - case ConstantIntValNode: - case ConstantNullptrValNode: case DummyValNode: { isPtr = true; From 00ccb521487d56509996b6a8dbe25ca303ea7bbd Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Tue, 10 Dec 2024 14:09:32 +1100 Subject: [PATCH 15/28] refactor addConstantVal/ObjNode --- svf-llvm/lib/SVFIRBuilder.cpp | 69 +++++++++++++++-- svf/include/SVFIR/SVFIR.h | 136 +++++++++++++++++----------------- 2 files changed, 131 insertions(+), 74 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index e35cca134..a6925d262 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -221,7 +221,7 @@ void SVFIRBuilder::initialiseNodes() if(iter->second == symTable->blkPtrSymID()) continue; if (iter->second == symTable->nullPtrSymID()) { - pag->addConstantValNode(iter->first, iter->second, nullptr); + pag->addConstantNullPtrValNode(iter->first, iter->second, nullptr); } const ICFGNode* icfgNode = nullptr; @@ -240,6 +240,31 @@ void SVFIRBuilder::initialiseNodes() const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); pag->addFunValNode(cgn, iter->second, icfgNode); } + else if (auto fpValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addConstantFPValNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second, icfgNode); + llvmModuleSet()->setValueAttr(fpValue, pag->getGNode(iter->second)); + } + else if (auto intValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addConstantIntValNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second, icfgNode); + llvmModuleSet()->setValueAttr(intValue, pag->getGNode(iter->second)); + } + else if (auto nullValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addConstantNullPtrValNode(iter->first, iter->second, icfgNode); + llvmModuleSet()->setValueAttr(nullValue, pag->getGNode(iter->second)); + } + else if (auto globalValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addGlobalValueValNode(iter->first, iter->second, icfgNode); + llvmModuleSet()->setValueAttr(globalValue, pag->getGNode(iter->second)); + } + else if (auto dataValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addGlobalValueValNode(iter->first, iter->second, icfgNode); + llvmModuleSet()->setValueAttr(dataValue, pag->getGNode(iter->second)); + } else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { pag->addConstantValNode(iter->first, iter->second, icfgNode); llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); @@ -257,18 +282,46 @@ void SVFIRBuilder::initialiseNodes() DBOUT(DPAGBuild, outs() << "add obj node " << iter->second << "\n"); if(iter->second == symTable->blackholeSymID() || iter->second == symTable->constantSymID()) continue; + if (const Function* func = SVFUtil::dyn_cast( llvmModuleSet()->getLLVMValue(iter->first))) { pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); } - else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { - pag->addConstantObjNode(iter->first, iter->second); - llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); - } - else - { - pag->addObjNode(iter->first, iter->second); + else { + if (auto fpValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addConstantFPObjNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second); + llvmModuleSet()->setValueAttr(fpValue, pag->getGNode(iter->second)); + } + else if (auto intValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addConstantIntObjNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second); + llvmModuleSet()->setValueAttr(intValue, pag->getGNode(iter->second)); + } + else if (auto nullValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addConstantNullPtrObjNode(iter->first, iter->second); + llvmModuleSet()->setValueAttr(nullValue, pag->getGNode(iter->second)); + } + else if (auto globalValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addGlobalValueObjNode(iter->first, iter->second); + llvmModuleSet()->setValueAttr(globalValue, pag->getGNode(iter->second)); + } + else if (auto dataValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + { + pag->addGlobalValueObjNode(iter->first, iter->second); + llvmModuleSet()->setValueAttr(dataValue, pag->getGNode(iter->second)); + } + else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + pag->addConstantObjNode(iter->first, iter->second); + llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); + } + else + { + pag->addObjNode(iter->first, iter->second); + } } } diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index e610c30f8..b978bf8e0 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -579,84 +579,88 @@ class SVFIR : public IRGraph return addNode(node,i); } + inline NodeID addConstantFPValNode(const SVFValue* curInst, double dval, const NodeID i, + const ICFGNode* icfgNode) + { + SVFVar* node = new ConstantFPValVar(curInst, dval, i, icfgNode); + return addNode(node, i); + } - inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { - if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) - { - SVFVar* node = new ConstantFPValVar(curInst, constFp->getFPValue(), i, icfgNode); - return addNode(node,i); - } - // ConstantInt - else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) - { - SVFVar* node = new ConstantIntValVar(curInst, constInt->getSExtValue(), constInt->getZExtValue(), i, icfgNode); - return addNode(node,i); - } - // constNullptr - else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantNullPtrValVar(curInst, i, icfgNode); - return addNode(node,i); - } + inline NodeID addConstantIntValNode(const SVFValue* curInst, s64_t sval, u64_t zval, const NodeID i, + const ICFGNode* icfgNode) + { + SVFVar* node = new ConstantIntValVar(curInst, sval, zval, i, icfgNode); + return addNode(node, i); + } - else if (SVFUtil::isa(curInst)) - { - SVFVar* node = new GlobalValueValvar(curInst, i, icfgNode); - return addNode(node,i); - } + inline NodeID addConstantNullPtrValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) + { + SVFVar* node = new ConstantNullPtrValVar(curInst, i, icfgNode); + return addNode(node, i); + } - else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantDataValVar(curInst, i, icfgNode); - return addNode(node,i); - } + inline NodeID addGlobalValueValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) + { + SVFVar* node = new GlobalValueValvar(curInst, i, icfgNode); + return addNode(node, i); + } - else if (SVFUtil::isa(curInst)) - { - SVFVar* node = new ConstantValVar(curInst,i, icfgNode); - return addNode(node, i); - } - assert(false && "not a constant value?"); + inline NodeID addConstantDataValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) + { + SVFVar* node = new ConstantDataValVar(curInst, i, icfgNode); + return addNode(node, i); } - inline NodeID addConstantObjNode(const SVFValue* curInst, const NodeID i) { + inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) + { + SVFVar* node = new ConstantValVar(curInst, i, icfgNode); + return addNode(node, i); + } + + inline NodeID addConstantFPObjNode(const SVFValue* curInst, double dval, const NodeID i) + { const MemObj* mem = getMemObj(curInst); - NodeID base = mem->getId(); - memToFieldsMap[base].set(mem->getId()); - if (const SVFConstantFP* constFp = SVFUtil::dyn_cast(curInst)) - { - ConstantFPObjVar* node = new ConstantFPObjVar(curInst, constFp->getFPValue(), mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } - // ConstantInt - else if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(curInst)) - { - ConstantIntObjVar* node = new ConstantIntObjVar(curInst, constInt->getSExtValue(), constInt->getZExtValue(), mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } - // constNullptr - else if (SVFUtil::isa(curInst)) { - SVFVar* node = new ConstantNullPtrObjVar(curInst, mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } + ConstantFPObjVar* node = new ConstantFPObjVar(curInst, dval, mem->getId(), mem); + return addObjNode(curInst, node, mem->getId()); + } - else if (SVFUtil::isa(curInst)) - { - GlobalValueObjVar* node = new GlobalValueObjVar(curInst, mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } - else if (SVFUtil::isa(curInst)) { - ConstantDataObjVar* node = new ConstantDataObjVar(curInst, mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } + inline NodeID addConstantIntObjNode(const SVFValue* curInst, s64_t sval, u64_t zval, const NodeID i) { + const MemObj* mem = getMemObj(curInst); + ConstantIntObjVar* node = + new ConstantIntObjVar(curInst, sval, zval, mem->getId(), mem); + return addObjNode(curInst, node, mem->getId()); + } - else if (SVFUtil::isa(curInst)) - { - ConstantObjVar* node = new ConstantObjVar(curInst, mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } - assert(false && "not a constant value?"); + + inline NodeID addConstantNullPtrObjNode(const SVFValue* curInst, const NodeID i) { + const MemObj* mem = getMemObj(curInst); + ConstantNullPtrObjVar* node = new ConstantNullPtrObjVar(curInst, mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + + inline NodeID addGlobalValueObjNode(const SVFValue* curInst, const NodeID i) + { + const MemObj* mem = getMemObj(curInst); + GlobalValueObjVar* node = new GlobalValueObjVar(curInst, mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + + inline NodeID addConstantDataObjNode(const SVFValue* curInst, const NodeID i) + { + const MemObj* mem = getMemObj(curInst); + ConstantDataObjVar* node = new ConstantDataObjVar(curInst, mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); } + inline NodeID addConstantObjNode(const SVFValue* curInst, const NodeID i) + { + const MemObj* mem = getMemObj(curInst); + ConstantObjVar* node = new ConstantObjVar(curInst, mem->getId(), mem); + return addObjNode(mem->getValue(), node, mem->getId()); + } + + /// Add a temp field value node, this method can only invoked by getGepValVar NodeID addGepValNode(const SVFValue* curInst,const SVFValue* val, const AccessPath& ap, NodeID i, const SVFType* type); /// Add a field obj node, this method can only invoked by getGepObjVar From 9dfc7fc2e4e54b30d5c4d44dcede3ce9b0e0c34d Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Tue, 10 Dec 2024 14:38:57 +1100 Subject: [PATCH 16/28] refactor addConstantVal/ObjNode --- svf-llvm/lib/SVFIRBuilder.cpp | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index a6925d262..0565b8304 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -225,8 +225,9 @@ void SVFIRBuilder::initialiseNodes() } const ICFGNode* icfgNode = nullptr; + auto llvmValue = llvmModuleSet()->getLLVMValue(iter->first); if (const Instruction* inst = - SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + SVFUtil::dyn_cast(llvmValue)) { if (llvmModuleSet()->hasICFGNode(inst)) { @@ -235,39 +236,39 @@ void SVFIRBuilder::initialiseNodes() } if (const Function* func = - SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + SVFUtil::dyn_cast(llvmValue)) { const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func); pag->addFunValNode(cgn, iter->second, icfgNode); } - else if (auto fpValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto fpValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantFPValNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second, icfgNode); llvmModuleSet()->setValueAttr(fpValue, pag->getGNode(iter->second)); } - else if (auto intValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto intValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantIntValNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second, icfgNode); llvmModuleSet()->setValueAttr(intValue, pag->getGNode(iter->second)); } - else if (auto nullValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto nullValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantNullPtrValNode(iter->first, iter->second, icfgNode); llvmModuleSet()->setValueAttr(nullValue, pag->getGNode(iter->second)); } - else if (auto globalValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto globalValue = SVFUtil::dyn_cast(llvmValue)) { pag->addGlobalValueValNode(iter->first, iter->second, icfgNode); llvmModuleSet()->setValueAttr(globalValue, pag->getGNode(iter->second)); } - else if (auto dataValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto dataValue = SVFUtil::dyn_cast(llvmValue)) { pag->addGlobalValueValNode(iter->first, iter->second, icfgNode); llvmModuleSet()->setValueAttr(dataValue, pag->getGNode(iter->second)); } - else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + else if (auto conValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantValNode(iter->first, iter->second, icfgNode); - llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); + llvmModuleSet()->setValueAttr(conValue, pag->getGNode(iter->second)); } else { @@ -282,41 +283,42 @@ void SVFIRBuilder::initialiseNodes() DBOUT(DPAGBuild, outs() << "add obj node " << iter->second << "\n"); if(iter->second == symTable->blackholeSymID() || iter->second == symTable->constantSymID()) continue; + auto llvmValue = llvmModuleSet()->getLLVMValue(iter->first); if (const Function* func = SVFUtil::dyn_cast( - llvmModuleSet()->getLLVMValue(iter->first))) + llvmValue)) { pag->addFunObjNode(llvmModuleSet()->getCallGraphNode(func), iter->second); } else { - if (auto fpValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + if (auto fpValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantFPObjNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second); llvmModuleSet()->setValueAttr(fpValue, pag->getGNode(iter->second)); } - else if (auto intValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto intValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantIntObjNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second); llvmModuleSet()->setValueAttr(intValue, pag->getGNode(iter->second)); } - else if (auto nullValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto nullValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantNullPtrObjNode(iter->first, iter->second); llvmModuleSet()->setValueAttr(nullValue, pag->getGNode(iter->second)); } - else if (auto globalValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto globalValue = SVFUtil::dyn_cast(llvmValue)) { pag->addGlobalValueObjNode(iter->first, iter->second); llvmModuleSet()->setValueAttr(globalValue, pag->getGNode(iter->second)); } - else if (auto dataValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) + else if (auto dataValue = SVFUtil::dyn_cast(llvmValue)) { pag->addGlobalValueObjNode(iter->first, iter->second); llvmModuleSet()->setValueAttr(dataValue, pag->getGNode(iter->second)); } - else if (auto llvmValue = SVFUtil::dyn_cast(llvmModuleSet()->getLLVMValue(iter->first))) { + else if (auto conValue = SVFUtil::dyn_cast(llvmValue)) { pag->addConstantObjNode(iter->first, iter->second); - llvmModuleSet()->setValueAttr(llvmValue, pag->getGNode(iter->second)); + llvmModuleSet()->setValueAttr(conValue, pag->getGNode(iter->second)); } else { From 251156df99468029377990f01cbaff47152b9f66 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Tue, 10 Dec 2024 22:24:38 +1100 Subject: [PATCH 17/28] fix AccessPath ConstantInt --- svf-llvm/lib/SVFIRExtAPI.cpp | 2 +- svf/include/Graphs/GenericGraph.h | 2 +- svf/include/SVFIR/SVFVariables.h | 125 ++++++++++++++--------------- svf/lib/MemoryModel/AccessPath.cpp | 13 +-- 4 files changed, 70 insertions(+), 72 deletions(-) diff --git a/svf-llvm/lib/SVFIRExtAPI.cpp b/svf-llvm/lib/SVFIRExtAPI.cpp index d9db98eec..1b67f9f9f 100644 --- a/svf-llvm/lib/SVFIRExtAPI.cpp +++ b/svf-llvm/lib/SVFIRExtAPI.cpp @@ -64,7 +64,7 @@ const Type* SVFIRBuilder::getBaseTypeAndFlattenedFields(const Value* V, std::vec { SymbolTableBuilder builder(pag->getSymbolInfo()); builder.collectSym(offset); - pag->addValNode(svfOffset, pag->getSymbolInfo()->getValSym(svfOffset), nullptr); + pag->addConstantIntValNode(svfOffset, offset->getSExtValue(), offset->getZExtValue(), pag->getSymbolInfo()->getValSym(svfOffset), nullptr); } ls.addOffsetVarAndGepTypePair(getPAG()->getGNode(getPAG()->getValueNode(svfOffset)), nullptr); fields.push_back(ls); diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index 3675c4a8f..ce50cb546 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -181,7 +181,7 @@ class SVFBaseNode ConstantFPValNode, // │ │ ├── Represents a constant integer value node ConstantIntValNode, - // │ | └── Represents a constant nullptr value node + // │ │ ├── Represents a constant nullptr value node ConstantNullptrValNode, // │ └── Dummy node for uninitialized values DummyValNode, diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 9aa0517d0..de3d807b9 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -313,136 +313,133 @@ class ValVar: public SVFVar virtual const std::string toString() const; }; -/* - * Memory Object variable - */ -class ObjVar: public SVFVar +class GepValVar: public ValVar { friend class SVFIRWriter; friend class SVFIRReader; -protected: - const MemObj* mem; ///< memory object - /// Constructor to create an empty ObjVar (for SVFIRReader/deserialization) - ObjVar(NodeID i, PNODEK ty = ObjNode) : SVFVar(i, ty), mem{} {} - /// Constructor - ObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ObjNode) : - SVFVar(val, i, ty), mem(m) - { - } +private: + AccessPath ap; // AccessPath + const SVFType* gepValType; + + /// Constructor to create empty GeValVar (for SVFIRReader/deserialization) + GepValVar(NodeID i) : ValVar(i, GepValNode), gepValType{} {} + public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const ObjVar*) + static inline bool classof(const GepValVar *) { return true; } - static inline bool classof(const SVFVar* node) + static inline bool classof(const ValVar * node) { - return isObjVarKinds(node->getNodeKind()); + return node->getNodeKind() == SVFVar::GepValNode; } - static inline bool classof(const GenericPAGNodeTy* node) + static inline bool classof(const SVFVar *node) { - return isObjVarKinds(node->getNodeKind()); + return node->getNodeKind() == SVFVar::GepValNode; } - static inline bool classof(const SVFBaseNode* node) + static inline bool classof(const GenericPAGNodeTy *node) { - return isObjVarKinds(node->getNodeKind()); + return node->getNodeKind() == SVFVar::GepValNode; } //@} - /// Return memory object - const MemObj* getMemObj() const + /// Constructor + GepValVar(const SVFValue* val, NodeID i, const AccessPath& ap, + const SVFType* ty) + : ValVar(val, i, GepValNode), ap(ap), gepValType(ty) { - return mem; + } + + /// offset of the base value variable + inline APOffset getConstantFieldIdx() const + { + return ap.getConstantStructFldIdx(); } /// Return name of a LLVM value - virtual const std::string getValueName() const + inline const std::string getValueName() const { if (value) - return value->getName(); - return ""; + return value->getName() + "_" + + std::to_string(getConstantFieldIdx()); + return "offset_" + std::to_string(getConstantFieldIdx()); } - /// Return type of the value - inline virtual const SVFType* getType() const + + inline const SVFType* getType() const { - return mem->getType(); + return gepValType; } virtual const std::string toString() const; }; + /* - * Gep Value (Pointer) variable, this variable can be dynamic generated for field sensitive analysis - * e.g. memcpy, temp gep value variable needs to be created - * Each Gep Value variable is connected to base value variable via gep edge + * Memory Object variable */ -class GepValVar: public ValVar +class ObjVar: public SVFVar { friend class SVFIRWriter; friend class SVFIRReader; -private: - AccessPath ap; // AccessPath - const SVFType* gepValType; - - /// Constructor to create empty GeValVar (for SVFIRReader/deserialization) - GepValVar(NodeID i) : ValVar(i, GepValNode), gepValType{} {} - +protected: + const MemObj* mem; ///< memory object + /// Constructor to create an empty ObjVar (for SVFIRReader/deserialization) + ObjVar(NodeID i, PNODEK ty = ObjNode) : SVFVar(i, ty), mem{} {} + /// Constructor + ObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ObjNode) : + SVFVar(val, i, ty), mem(m) + { + } public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const GepValVar *) + static inline bool classof(const ObjVar*) { return true; } - static inline bool classof(const ValVar * node) + static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == SVFVar::GepValNode; + return isObjVarKinds(node->getNodeKind()); } - static inline bool classof(const SVFVar *node) + static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == SVFVar::GepValNode; + return isObjVarKinds(node->getNodeKind()); } - static inline bool classof(const GenericPAGNodeTy *node) + static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == SVFVar::GepValNode; + return isObjVarKinds(node->getNodeKind()); } //@} - /// Constructor - GepValVar(const SVFValue* val, NodeID i, const AccessPath& ap, - const SVFType* ty) - : ValVar(val, i, GepValNode), ap(ap), gepValType(ty) - { - } - - /// offset of the base value variable - inline APOffset getConstantFieldIdx() const + /// Return memory object + const MemObj* getMemObj() const { - return ap.getConstantStructFldIdx(); + return mem; } /// Return name of a LLVM value - inline const std::string getValueName() const + virtual const std::string getValueName() const { if (value) - return value->getName() + "_" + - std::to_string(getConstantFieldIdx()); - return "offset_" + std::to_string(getConstantFieldIdx()); + return value->getName(); + return ""; } - - inline const SVFType* getType() const + /// Return type of the value + inline virtual const SVFType* getType() const { - return gepValType; + return mem->getType(); } virtual const std::string toString() const; }; + /* * Gep Obj variable, this is dynamic generated for field sensitive analysis * Each gep obj variable is one field of a MemObj (base) diff --git a/svf/lib/MemoryModel/AccessPath.cpp b/svf/lib/MemoryModel/AccessPath.cpp index be763bbfd..92b21868a 100644 --- a/svf/lib/MemoryModel/AccessPath.cpp +++ b/svf/lib/MemoryModel/AccessPath.cpp @@ -223,21 +223,22 @@ APOffset AccessPath::computeConstantOffset() const return getConstantStructFldIdx(); for(int i = idxOperandPairs.size() - 1; i >= 0; i--) { - const SVFValue* value = idxOperandPairs[i].first->getValue(); + const SVFVar* var = idxOperandPairs[i].first; const SVFType* type = idxOperandPairs[i].second; - const SVFConstantInt* op = SVFUtil::dyn_cast(value); - assert(op && "not a constant offset?"); + assert(SVFUtil::isa(var) && "not a constant offset?"); + s64_t constOffset = SVFUtil::dyn_cast(var)->getSExtValue(); + if(type==nullptr) { - totalConstOffset += op->getSExtValue(); + totalConstOffset += constOffset; continue; } if(SVFUtil::isa(type)) - totalConstOffset += op->getSExtValue() * getElementNum(gepPointeeType); + totalConstOffset += constOffset * getElementNum(gepPointeeType); else { - APOffset offset = op->getSExtValue(); + APOffset offset = constOffset; if (offset >= 0) { const std::vector& so = SymbolTableInfo::SymbolInfo()->getTypeInfo(type)->getFlattenedElemIdxVec(); From a3365f2bf27fb87b7af5edc84db34f0b4603aefc Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Wed, 11 Dec 2024 12:18:56 +1100 Subject: [PATCH 18/28] fix AE + Const AccessPath --- svf/include/SVFIR/SVFVariables.h | 11 +++++++++-- svf/lib/AE/Core/AbstractState.cpp | 11 +++++------ svf/lib/MemoryModel/AccessPath.cpp | 9 ++++----- svf/lib/SVFIR/SVFIR.cpp | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index de3d807b9..b9ea09622 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -320,6 +320,7 @@ class GepValVar: public ValVar private: AccessPath ap; // AccessPath + NodeID base; // base node id const SVFType* gepValType; /// Constructor to create empty GeValVar (for SVFIRReader/deserialization) @@ -347,9 +348,9 @@ class GepValVar: public ValVar //@} /// Constructor - GepValVar(const SVFValue* val, NodeID i, const AccessPath& ap, + GepValVar(NodeID baseID, const SVFValue* val, NodeID i, const AccessPath& ap, const SVFType* ty) - : ValVar(val, i, GepValNode), ap(ap), gepValType(ty) + : ValVar(val, i, GepValNode), ap(ap), base(baseID), gepValType(ty) { } @@ -359,6 +360,12 @@ class GepValVar: public ValVar return ap.getConstantStructFldIdx(); } + /// Return the base object from which this GEP node came from. + inline NodeID getBaseNode(void) const + { + return base; + } + /// Return name of a LLVM value inline const std::string getValueName() const { diff --git a/svf/lib/AE/Core/AbstractState.cpp b/svf/lib/AE/Core/AbstractState.cpp index 3f947988e..fe9059234 100644 --- a/svf/lib/AE/Core/AbstractState.cpp +++ b/svf/lib/AE/Core/AbstractState.cpp @@ -233,7 +233,7 @@ IntervalValue AbstractState::getElementIndex(const GepStmt* gep) for (int i = gep->getOffsetVarAndGepTypePairVec().size() - 1; i >= 0; i--) { AccessPath::IdxOperandPair IdxVarAndType = gep->getOffsetVarAndGepTypePairVec()[i]; - const SVFValue* value = gep->getOffsetVarAndGepTypePairVec()[i].first->getValue(); + const SVFVar* var = gep->getOffsetVarAndGepTypePairVec()[i].first; const SVFType* type = IdxVarAndType.second; // Variables to store the lower and upper bounds of the index value @@ -241,11 +241,11 @@ IntervalValue AbstractState::getElementIndex(const GepStmt* gep) s64_t idxUb; // Determine the lower and upper bounds based on whether the value is a constant - if (const SVFConstantInt* constInt = SVFUtil::dyn_cast(value)) + if (const ConstantIntValVar* constInt = SVFUtil::dyn_cast(var)) idxLb = idxUb = constInt->getSExtValue(); else { - IntervalValue idxItv = (*this)[PAG::getPAG()->getValueNode(value)].getInterval(); + IntervalValue idxItv = (*this)[var->getId()].getInterval(); if (idxItv.isBottom()) idxLb = idxUb = 0; else @@ -320,7 +320,7 @@ IntervalValue AbstractState::getByteOffset(const GepStmt* gep) else assert(false && "idxOperandType must be ArrType or PtrType"); - if (const SVFConstantInt* op = SVFUtil::dyn_cast(idxOperandVar->getValue())) + if (const ConstantIntValVar* op = SVFUtil::dyn_cast(idxOperandVar)) { // Calculate the lower bound (lb) of the interval value s64_t lb = (double)Options::MaxFieldLimit() / elemByteSize >= op->getSExtValue() @@ -330,8 +330,7 @@ IntervalValue AbstractState::getByteOffset(const GepStmt* gep) } else { - u32_t idx = PAG::getPAG()->getValueNode(idxOperandVar->getValue()); - IntervalValue idxVal = (*this)[idx].getInterval(); + IntervalValue idxVal = (*this)[idxOperandVar->getId()].getInterval(); if (idxVal.isBottom()) res = res + IntervalValue(0, 0); diff --git a/svf/lib/MemoryModel/AccessPath.cpp b/svf/lib/MemoryModel/AccessPath.cpp index 92b21868a..23c3dc934 100644 --- a/svf/lib/MemoryModel/AccessPath.cpp +++ b/svf/lib/MemoryModel/AccessPath.cpp @@ -50,7 +50,7 @@ bool AccessPath::isConstantOffset() const { for(auto it : idxOperandPairs) { - if(SVFUtil::isa(it.first->getValue()) == false) + if(SVFUtil::isa(it.first) == false) return false; } return true; @@ -97,9 +97,8 @@ u32_t AccessPath::getElementNum(const SVFType* type) const // then the return byte offset is 16 Bytes. u32_t AccessPath::getStructFieldOffset(const SVFVar* idxOperandVar, const SVFStructType* idxOperandType) const { - const SVFValue* idxValue = idxOperandVar->getValue(); u32_t structByteOffset = 0; - if (const SVFConstantInt *op = SVFUtil::dyn_cast(idxValue)) + if (const ConstantIntValVar *op = SVFUtil::dyn_cast(idxOperandVar)) { for (u32_t structField = 0; structField < (u32_t) op->getSExtValue(); ++structField) { @@ -132,7 +131,7 @@ APOffset AccessPath::computeConstantByteOffset() const /// For example, there is struct DEST{int a, char b[10], int c[5]} /// (1) %c = getelementptr inbounds %struct.DEST, %struct.DEST* %arr, i32 0, i32 2 // (2) %arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %b, i64 0, i64 8 - const SVFValue* value = idxOperandPairs[i].first->getValue(); + const SVFVar* var = idxOperandPairs[i].first; /// for (1) offsetVarAndGepTypePairs.size() = 2 /// i = 0, type: %struct.DEST*, PtrType, op = 0 /// i = 1, type: %struct.DEST, StructType, op = 2 @@ -157,7 +156,7 @@ APOffset AccessPath::computeConstantByteOffset() const type2 = gepSrcPointeeType(); } - const SVFConstantInt* op = SVFUtil::dyn_cast(value); + const ConstantIntValVar* op = SVFUtil::dyn_cast(var); if (const SVFStructType* structType = SVFUtil::dyn_cast(type)) { /// for (1) structType: %struct.DEST diff --git a/svf/lib/SVFIR/SVFIR.cpp b/svf/lib/SVFIR/SVFIR.cpp index ae52cb87a..dd4a366f8 100644 --- a/svf/lib/SVFIR/SVFIR.cpp +++ b/svf/lib/SVFIR/SVFIR.cpp @@ -391,7 +391,7 @@ NodeID SVFIR::addGepValNode(const SVFValue* curInst,const SVFValue* gepVal, cons assert(0==GepValObjMap[curInst].count(std::make_pair(base, ap)) && "this node should not be created before"); GepValObjMap[curInst][std::make_pair(base, ap)] = i; - GepValVar *node = new GepValVar(gepVal, i, ap, type); + GepValVar *node = new GepValVar(base, gepVal, i, ap, type); return addValNode(gepVal, node, i); } From 22045d8394fe8136cb866ec123dbd980c9f098a5 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Wed, 11 Dec 2024 12:37:54 +1100 Subject: [PATCH 19/28] fix saber --- svf-llvm/include/SVF-LLVM/SVFIRBuilder.h | 2 +- svf/lib/MemoryModel/PointerAnalysis.cpp | 15 +++++++++++---- svf/lib/SABER/SaberCondAllocator.cpp | 20 ++++++++++++++------ svf/lib/SABER/SaberSVFGBuilder.cpp | 2 +- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h b/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h index fe67316be..fc964a673 100644 --- a/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h +++ b/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h @@ -272,7 +272,7 @@ class SVFIRBuilder: public llvm::InstVisitor { LLVMContext& cxt = llvmModuleSet()->getContext(); ConstantPointerNull* constNull = ConstantPointerNull::get(PointerType::getUnqual(cxt)); - NodeID nullPtr = pag->addValNode(llvmModuleSet()->getSVFValue(constNull),pag->getNullPtr(), nullptr); + NodeID nullPtr = pag->addConstantNullPtrValNode(llvmModuleSet()->getSVFValue(constNull),pag->getNullPtr(), nullptr); setCurrentLocation(constNull, nullptr); addBlackHoleAddrEdge(pag->getBlkPtr()); return nullPtr; diff --git a/svf/lib/MemoryModel/PointerAnalysis.cpp b/svf/lib/MemoryModel/PointerAnalysis.cpp index 3d292fba5..e8b3b1467 100644 --- a/svf/lib/MemoryModel/PointerAnalysis.cpp +++ b/svf/lib/MemoryModel/PointerAnalysis.cpp @@ -446,10 +446,17 @@ void PointerAnalysis::getVFnsFromPts(const CallICFGNode* cs, const PointsTo &tar const PAGNode *ptdnode = pag->getGNode(*it); if (ptdnode->hasValue()) { - if (const SVFGlobalValue *vtbl = SVFUtil::dyn_cast(ptdnode->getValue())) - { - if (chaVtbls.find(vtbl) != chaVtbls.end()) - vtbls.insert(vtbl); + // ptd is global obj var or ptd's base is global obj var + if (const GlobalValueObjVar *global_vtbl = SVFUtil::dyn_cast(ptdnode)) { + const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(global_vtbl->getValue()); + if (chaVtbls.find(globalValue) != chaVtbls.end()) + vtbls.insert(globalValue); + } else if (const GepObjVar *gep_vtbl = SVFUtil::dyn_cast(ptdnode)) { + if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { + const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(gep_vtbl->getValue()); + if (chaVtbls.find(globalValue) != chaVtbls.end()) + vtbls.insert(globalValue); + } } } } diff --git a/svf/lib/SABER/SaberCondAllocator.cpp b/svf/lib/SABER/SaberCondAllocator.cpp index 082d7a52b..ef074ce90 100644 --- a/svf/lib/SABER/SaberCondAllocator.cpp +++ b/svf/lib/SABER/SaberCondAllocator.cpp @@ -399,9 +399,17 @@ bool SaberCondAllocator::isTestNotNullExpr(const ICFGNode* test) const bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const { - const SVFValue* op0 = cmp->getOpVar(0)->getValue(); - const SVFValue* op1 = cmp->getOpVar(1)->getValue(); - if (SVFUtil::isa(op1)) + const SVFVar* op0 = cmp->getOpVar(0); + const SVFVar* op1 = cmp->getOpVar(1); + bool a = SVFUtil::dyn_cast(op0) || + SVFUtil::dyn_cast(op0); + bool b = SVFUtil::dyn_cast(op1) || + SVFUtil::dyn_cast(op1); + bool c = SVFUtil::dyn_cast(op0->getValue()); + bool d = SVFUtil::dyn_cast(op1->getValue()); + assert(a == c); + assert(b == d); + if (SVFUtil::isa(op1)) { Set inDirVal; inDirVal.insert(getCurEvalSVFGNode()->getValue()); @@ -409,9 +417,9 @@ bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const { inDirVal.insert(it->getDstNode()->getValue()); } - return inDirVal.find(op0) != inDirVal.end(); + return inDirVal.find(op0->getValue()) != inDirVal.end(); } - else if (SVFUtil::isa(op0)) + else if (SVFUtil::isa(op0)) { Set inDirVal; inDirVal.insert(getCurEvalSVFGNode()->getValue()); @@ -419,7 +427,7 @@ bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const { inDirVal.insert(it->getDstNode()->getValue()); } - return inDirVal.find(op1) != inDirVal.end(); + return inDirVal.find(op1->getValue()) != inDirVal.end(); } return false; } diff --git a/svf/lib/SABER/SaberSVFGBuilder.cpp b/svf/lib/SABER/SaberSVFGBuilder.cpp index f90d86e36..766f4616f 100644 --- a/svf/lib/SABER/SaberSVFGBuilder.cpp +++ b/svf/lib/SABER/SaberSVFGBuilder.cpp @@ -82,7 +82,7 @@ void SaberSVFGBuilder::collectGlobals(BVDataPTAImpl* pta) if(SVFUtil::isa(pag->getGNode(gepobj->getBaseNode()))) continue; } - if(pagNode->hasValue() && SVFUtil::isa(pagNode->getValue())) + if(pagNode->hasValue() && SVFUtil::isa(pagNode)) worklist.push_back(it->first); } From efcf2deb55327abeb954ee83df6dbd8f7e281753 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Wed, 11 Dec 2024 12:38:26 +1100 Subject: [PATCH 20/28] rename GlobalValueValVar --- svf/include/SVFIR/SVFIR.h | 2 +- svf/include/SVFIR/SVFVariables.h | 4 ++-- svf/lib/SABER/SaberSVFGBuilder.cpp | 2 +- svf/lib/SVFIR/SVFFileSystem.cpp | 2 +- svf/lib/SVFIR/SVFVariables.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index b978bf8e0..38c6b7cda 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -601,7 +601,7 @@ class SVFIR : public IRGraph inline NodeID addGlobalValueValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { - SVFVar* node = new GlobalValueValvar(curInst, i, icfgNode); + SVFVar* node = new GlobalValueValVar(curInst, i, icfgNode); return addNode(node, i); } diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index b9ea09622..2d3aa67be 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -776,7 +776,7 @@ class ConstantDataValVar: public ConstantValVar virtual const std::string toString() const; }; -class GlobalValueValvar: public ConstantValVar +class GlobalValueValVar : public ConstantValVar { friend class SVFIRWriter; friend class SVFIRReader; @@ -807,7 +807,7 @@ class GlobalValueValvar: public ConstantValVar //@} /// Constructor - GlobalValueValvar(const SVFValue* val, NodeID i, const ICFGNode* icn, + GlobalValueValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = GlobalValueValNode) : ConstantValVar(val, i, icn, ty) { diff --git a/svf/lib/SABER/SaberSVFGBuilder.cpp b/svf/lib/SABER/SaberSVFGBuilder.cpp index 766f4616f..96fd1eacc 100644 --- a/svf/lib/SABER/SaberSVFGBuilder.cpp +++ b/svf/lib/SABER/SaberSVFGBuilder.cpp @@ -82,7 +82,7 @@ void SaberSVFGBuilder::collectGlobals(BVDataPTAImpl* pta) if(SVFUtil::isa(pag->getGNode(gepobj->getBaseNode()))) continue; } - if(pagNode->hasValue() && SVFUtil::isa(pagNode)) + if(pagNode->hasValue() && SVFUtil::isa(pagNode)) worklist.push_back(it->first); } diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index 935eb0bc2..12d86e30b 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -228,7 +228,7 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(FunValNode, FunValVar); CASE(ConstantValNode, ConstantValVar); CASE(ConstantDataValNode, ConstantDataValVar); - CASE(GlobalValueValNode, GlobalValueValvar); + CASE(GlobalValueValNode, GlobalValueValVar); CASE(BlackHoleNode, BlackHoleVar); CASE(ConstantFPValNode, ConstantFPValVar); CASE(ConstantIntValNode, ConstantIntValVar); diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 36c0259cf..fe6a3b34e 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -235,7 +235,7 @@ const std::string ConstantDataValVar::toString() const { return rawstr.str(); } -const std::string GlobalValueValvar::toString() const { +const std::string GlobalValueValVar::toString() const { std::string str; std::stringstream rawstr(str); rawstr << "GlobalValueValVar ID: " << getId(); From 2373b83c00bbd8f8d3e5e99ca50a98e8b454a76a Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Wed, 11 Dec 2024 12:59:46 +1100 Subject: [PATCH 21/28] fix CFL --- svf/lib/MemoryModel/PointerAnalysis.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/svf/lib/MemoryModel/PointerAnalysis.cpp b/svf/lib/MemoryModel/PointerAnalysis.cpp index e8b3b1467..54359e9e1 100644 --- a/svf/lib/MemoryModel/PointerAnalysis.cpp +++ b/svf/lib/MemoryModel/PointerAnalysis.cpp @@ -436,7 +436,6 @@ void PointerAnalysis::getVFnsFromCHA(const CallICFGNode* cs, VFunSet &vfns) */ void PointerAnalysis::getVFnsFromPts(const CallICFGNode* cs, const PointsTo &target, VFunSet &vfns) { - if (chgraph->csHasVtblsBasedonCHA(cs)) { Set vtbls; @@ -446,13 +445,13 @@ void PointerAnalysis::getVFnsFromPts(const CallICFGNode* cs, const PointsTo &tar const PAGNode *ptdnode = pag->getGNode(*it); if (ptdnode->hasValue()) { - // ptd is global obj var or ptd's base is global obj var - if (const GlobalValueObjVar *global_vtbl = SVFUtil::dyn_cast(ptdnode)) { - const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(global_vtbl->getValue()); + // ptd is global obj var or ptd's base is global val/obj var + if (SVFUtil::isa(ptdnode)) { + const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(ptdnode->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); } else if (const GepObjVar *gep_vtbl = SVFUtil::dyn_cast(ptdnode)) { - if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { + if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(gep_vtbl->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); From f0331b5988400183c2b714b07b9c809f735f4029 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Wed, 11 Dec 2024 22:09:29 +1100 Subject: [PATCH 22/28] fix nullptr --- svf-llvm/include/SVF-LLVM/SVFIRBuilder.h | 1 + 1 file changed, 1 insertion(+) diff --git a/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h b/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h index fc964a673..fbbb06ccd 100644 --- a/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h +++ b/svf-llvm/include/SVF-LLVM/SVFIRBuilder.h @@ -273,6 +273,7 @@ class SVFIRBuilder: public llvm::InstVisitor LLVMContext& cxt = llvmModuleSet()->getContext(); ConstantPointerNull* constNull = ConstantPointerNull::get(PointerType::getUnqual(cxt)); NodeID nullPtr = pag->addConstantNullPtrValNode(llvmModuleSet()->getSVFValue(constNull),pag->getNullPtr(), nullptr); + llvmModuleSet()->setValueAttr(constNull, pag->getGNode(nullPtr)); setCurrentLocation(constNull, nullptr); addBlackHoleAddrEdge(pag->getBlkPtr()); return nullPtr; From 713318a34ebd5dd9e6a212c931b0b03e365b90e9 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Thu, 12 Dec 2024 21:09:53 +1100 Subject: [PATCH 23/28] refactor class hierarchy --- svf-llvm/lib/SVFIRBuilder.cpp | 8 -- svf/include/Graphs/GenericGraph.h | 26 +------ svf/include/SVFIR/SVFIR.h | 12 --- svf/include/SVFIR/SVFVariables.h | 125 ++++-------------------------- svf/lib/SVFIR/SVFFileSystem.cpp | 2 - svf/lib/SVFIR/SVFVariables.cpp | 25 ------ 6 files changed, 18 insertions(+), 180 deletions(-) diff --git a/svf-llvm/lib/SVFIRBuilder.cpp b/svf-llvm/lib/SVFIRBuilder.cpp index 0565b8304..71669ca9d 100644 --- a/svf-llvm/lib/SVFIRBuilder.cpp +++ b/svf-llvm/lib/SVFIRBuilder.cpp @@ -266,10 +266,6 @@ void SVFIRBuilder::initialiseNodes() pag->addGlobalValueValNode(iter->first, iter->second, icfgNode); llvmModuleSet()->setValueAttr(dataValue, pag->getGNode(iter->second)); } - else if (auto conValue = SVFUtil::dyn_cast(llvmValue)) { - pag->addConstantValNode(iter->first, iter->second, icfgNode); - llvmModuleSet()->setValueAttr(conValue, pag->getGNode(iter->second)); - } else { pag->addValNode(iter->first, iter->second, icfgNode); @@ -316,10 +312,6 @@ void SVFIRBuilder::initialiseNodes() pag->addGlobalValueObjNode(iter->first, iter->second); llvmModuleSet()->setValueAttr(dataValue, pag->getGNode(iter->second)); } - else if (auto conValue = SVFUtil::dyn_cast(llvmValue)) { - pag->addConstantObjNode(iter->first, iter->second); - llvmModuleSet()->setValueAttr(conValue, pag->getGNode(iter->second)); - } else { pag->addObjNode(iter->first, iter->second); diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index ce50cb546..2305618ba 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -169,8 +169,6 @@ class SVFBaseNode RetNode, // │ │ ├── Represents a variadic argument node VarargNode, - // │ │ ├── Represents a constant value node - ConstantValNode, // │ │ ├── Represents a global value node GlobalValueValNode, // │ │ ├── Represents a constant data value node @@ -197,8 +195,6 @@ class SVFBaseNode FIObjNode, // │ ├── FunObjNode: Types of function object FunObjNode, - // │ ├── ConstantObjNode: Types of constant object - ConstantObjNode, // │ ├── GlobalValueObjNode: Types of global value object GlobalValueObjNode, // │ ├── ConstantDataObjNode: Types of constant data object @@ -348,7 +344,7 @@ class SVFBaseNode static inline bool isSVFVarKind(GNodeK n) { - static_assert(DummyObjNode - ValNode == 23, + static_assert(DummyObjNode - ValNode == 21, "The number of SVFVarKinds has changed, make sure the " "range is correct"); @@ -357,19 +353,12 @@ class SVFBaseNode static inline bool isValVarKinds(GNodeK n) { - static_assert(DummyValNode - ValNode == 12, + static_assert(DummyValNode - ValNode == 11, "The number of ValVarKinds has changed, make sure the " "range is correct"); return n <= DummyValNode && n >= ValNode; } - static inline bool isConstantValVar(GNodeK n) - { - static_assert(ConstantNullptrValNode - ConstantValNode == 6, - "The number of ConstantValVarKinds has changed, make sure " - "the range is correct"); - return n <= ConstantNullptrValNode && n >= ConstantValNode; - } static inline bool isConstantDataValVar(GNodeK n) { @@ -381,7 +370,7 @@ class SVFBaseNode static inline bool isObjVarKinds(GNodeK n) { - static_assert(DummyObjNode - ObjNode == 10, + static_assert(DummyObjNode - ObjNode == 9, "The number of ObjVarKinds has changed, make sure the " "range is correct"); return n <= DummyObjNode && n >= ObjNode; @@ -389,19 +378,12 @@ class SVFBaseNode static inline bool isFIObjVarKinds(GNodeK n) { - static_assert(ConstantNullptrObjNode - FIObjNode == 7, + static_assert(ConstantNullptrObjNode - FIObjNode == 6, "The number of FIObjVarKinds has changed, make sure the " "range is correct"); return n <= ConstantNullptrObjNode && n >= FIObjNode; } - static inline bool isConstantObjVarKinds(GNodeK n) - { - static_assert(ConstantNullptrObjNode - ConstantObjNode == 5, - "The number of ConstantObjVarKinds has changed, make " - "sure the range is correct"); - return n <= ConstantNullptrObjNode && n >= ConstantObjNode; - } static inline bool isConstantDataObjVarKinds(GNodeK n) { diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 38c6b7cda..051314ee5 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -611,12 +611,6 @@ class SVFIR : public IRGraph return addNode(node, i); } - inline NodeID addConstantValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) - { - SVFVar* node = new ConstantValVar(curInst, i, icfgNode); - return addNode(node, i); - } - inline NodeID addConstantFPObjNode(const SVFValue* curInst, double dval, const NodeID i) { const MemObj* mem = getMemObj(curInst); @@ -653,12 +647,6 @@ class SVFIR : public IRGraph return addObjNode(mem->getValue(), node, mem->getId()); } - inline NodeID addConstantObjNode(const SVFValue* curInst, const NodeID i) - { - const MemObj* mem = getMemObj(curInst); - ConstantObjVar* node = new ConstantObjVar(curInst, mem->getId(), mem); - return addObjNode(mem->getValue(), node, mem->getId()); - } /// Add a temp field value node, this method can only invoked by getGepValVar diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 2d3aa67be..2c4a9fb71 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -690,52 +690,7 @@ class FunObjVar : public FIObjVar { }; -/* - * Constant objects, including ConstantValVar inherited from ValVar, - * and ConstantObjVar inherited from FIObjVar - */ -class ConstantValVar: public ValVar -{ - friend class SVFIRWriter; - friend class SVFIRReader; - -public: - /// Methods for support type inquiry through isa, cast, and dyn_cast: - //@{ - static inline bool classof(const ConstantValVar*) - { - return true; - } - static inline bool classof(const ValVar* node) - { - return isConstantValVar(node->getNodeKind()); - } - static inline bool classof(const SVFVar* node) - { - return isConstantValVar(node->getNodeKind()); - } - static inline bool classof(const GenericPAGNodeTy* node) - { - return isConstantValVar(node->getNodeKind()); - } - static inline bool classof(const SVFBaseNode* node) - { - return isConstantValVar(node->getNodeKind()); - } - //@} - - /// Constructor - ConstantValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, - PNODEK ty = ConstantValNode) - : ValVar(val, i, ty, icn) - { - - } - - virtual const std::string toString() const; -}; - -class ConstantDataValVar: public ConstantValVar +class ConstantDataValVar: public ValVar { friend class SVFIRWriter; friend class SVFIRReader; @@ -768,7 +723,7 @@ class ConstantDataValVar: public ConstantValVar /// Constructor ConstantDataValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = ConstantDataValNode) - : ConstantValVar(val, i, icn, ty) + : ValVar(val, i, ty, icn) { } @@ -776,7 +731,7 @@ class ConstantDataValVar: public ConstantValVar virtual const std::string toString() const; }; -class GlobalValueValVar : public ConstantValVar +class GlobalValueValVar : public ValVar { friend class SVFIRWriter; friend class SVFIRReader; @@ -809,7 +764,7 @@ class GlobalValueValVar : public ConstantValVar /// Constructor GlobalValueValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = GlobalValueValNode) - : ConstantValVar(val, i, icn, ty) + : ValVar(val, i, ty, icn) { } @@ -1002,65 +957,13 @@ class ConstantNullPtrValVar: public ConstantDataValVar virtual const std::string toString() const; }; -class ConstantObjVar: public FIObjVar -{ - friend class SVFIRWriter; - friend class SVFIRReader; - -protected: - /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - ConstantObjVar(NodeID i, PNODEK ty = ConstantObjNode) : FIObjVar(i, ty) {} - -public: - /// Methods for support type inquiry through isa, cast, and dyn_cast: - //@{ - static inline bool classof(const ConstantObjVar*) - { - return true; - } - static inline bool classof(const ObjVar* node) - { - return isConstantObjVarKinds(node->getNodeKind()); - } - static inline bool classof(const SVFVar* node) - { - return isConstantObjVarKinds(node->getNodeKind()); - } - static inline bool classof(const GenericPAGNodeTy* node) - { - return isConstantObjVarKinds(node->getNodeKind()); - } - static inline bool classof(const SVFBaseNode* node) - { - return isConstantObjVarKinds(node->getNodeKind()); - } - //@} - - /// Constructor - ConstantObjVar(const SVFValue* val, NodeID i, const MemObj* mem, - PNODEK ty = ConstantObjNode) - : FIObjVar(val, i, mem, ty) - { - } - - /// Return name of a LLVM value - inline const std::string getValueName() const - { - if (value) - return value->getName() + " (base object)"; - return " (base object)"; - } - - virtual const std::string toString() const; -}; - -class GlobalValueObjVar: public ConstantObjVar { +class GlobalValueObjVar: public FIObjVar { friend class SVFIRWriter; friend class SVFIRReader; private: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - GlobalValueObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : ConstantObjVar(i, ty) {} + GlobalValueObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : FIObjVar(i, ty) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: @@ -1069,10 +972,6 @@ class GlobalValueObjVar: public ConstantObjVar { { return true; } - static inline bool classof(const ConstantObjVar* node) - { - return node->getNodeKind() == GlobalValueObjNode; - } static inline bool classof(const FIObjVar* node) { return node->getNodeKind() == GlobalValueObjNode; @@ -1097,7 +996,7 @@ class GlobalValueObjVar: public ConstantObjVar { /// Constructor GlobalValueObjVar(const SVFValue* val, NodeID i, const MemObj* mem, - PNODEK ty = GlobalValueObjNode): ConstantObjVar(val, i,mem,ty){ + PNODEK ty = GlobalValueObjNode): FIObjVar(val, i,mem,ty){ } @@ -1105,13 +1004,13 @@ class GlobalValueObjVar: public ConstantObjVar { virtual const std::string toString() const; }; -class ConstantDataObjVar: public ConstantObjVar { +class ConstantDataObjVar: public FIObjVar { friend class SVFIRWriter; friend class SVFIRReader; protected: /// Constructor to create empty DummyObjVar (for SVFIRReader/deserialization) - ConstantDataObjVar(NodeID i) : ConstantObjVar(i, ConstantDataObjNode) {} + ConstantDataObjVar(NodeID i) : FIObjVar(i, ConstantDataObjNode) {} public: //@{ Methods for support type inquiry through isa, cast, and dyn_cast: @@ -1127,6 +1026,10 @@ class ConstantDataObjVar: public ConstantObjVar { { return isConstantDataObjVarKinds(node->getNodeKind()); } + static inline bool classof(const FIObjVar* node) + { + return isConstantDataObjVarKinds(node->getNodeKind()); + } static inline bool classof(const GenericPAGNodeTy* node) { return isConstantDataObjVarKinds(node->getNodeKind()); @@ -1140,7 +1043,7 @@ class ConstantDataObjVar: public ConstantObjVar { /// Constructor ConstantDataObjVar(const SVFValue* val, NodeID i, const MemObj* m, PNODEK ty = ConstantDataObjNode) - : ConstantObjVar(val, i, m, ty) + : FIObjVar(val, i, m, ty) { } diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index 12d86e30b..df281f506 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -226,14 +226,12 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(DummyObjNode, DummyObjVar); CASE(FunObjNode, FunObjVar); CASE(FunValNode, FunValVar); - CASE(ConstantValNode, ConstantValVar); CASE(ConstantDataValNode, ConstantDataValVar); CASE(GlobalValueValNode, GlobalValueValVar); CASE(BlackHoleNode, BlackHoleVar); CASE(ConstantFPValNode, ConstantFPValVar); CASE(ConstantIntValNode, ConstantIntValVar); CASE(ConstantNullptrValNode, ConstantNullPtrValVar); - CASE(ConstantObjNode, ConstantObjVar); CASE(ConstantDataObjNode, ConstantDataObjVar); CASE(GlobalValueObjNode, GlobalValueObjVar); CASE(ConstantFPObjNode, ConstantFPObjVar); diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index fe6a3b34e..bc30cba7e 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -46,7 +46,6 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : switch (k) { case ValNode: - case ConstantValNode: case ConstantDataValNode: case GlobalValueValNode: case BlackHoleNode: @@ -75,7 +74,6 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : case ObjNode: case GepObjNode: case FIObjNode: - case ConstantObjNode: case ConstantDataObjNode: case GlobalValueObjNode: case ConstantFPObjNode: @@ -211,18 +209,6 @@ const std::string FunValVar::toString() const return rawstr.str(); } -const std::string ConstantValVar::toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantValNode ID: " << getId(); - if (Options::ShowSVFIRValue()) - { - rawstr << "\n"; - rawstr << valueOnlyToString(); - } - return rawstr.str(); -} - const std::string ConstantDataValVar::toString() const { std::string str; std::stringstream rawstr(str); @@ -283,17 +269,6 @@ const std::string ConstantNullPtrValVar::toString() const { return rawstr.str(); } -const std::string ConstantObjVar::toString() const { - std::string str; - std::stringstream rawstr(str); - rawstr << "ConstantNullPtrValNode ID: " << getId(); - if (Options::ShowSVFIRValue()) - { - rawstr << "\n"; - rawstr << valueOnlyToString(); - } - return rawstr.str(); -} const std::string GlobalValueObjVar::toString() const { std::string str; From c4a2cc02e986d47c07346512bf810b013aca9d73 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Fri, 13 Dec 2024 20:50:03 +1300 Subject: [PATCH 24/28] refactor GlobalClass, and remove unused codes --- svf/include/SVFIR/SVFIR.h | 10 +++++++--- svf/include/SVFIR/SVFVariables.h | 12 ++++++------ svf/lib/AE/Core/AbstractState.cpp | 2 +- svf/lib/MemoryModel/PointerAnalysis.cpp | 4 ++-- svf/lib/SABER/SaberCondAllocator.cpp | 8 -------- svf/lib/SABER/SaberSVFGBuilder.cpp | 2 +- svf/lib/SVFIR/SVFFileSystem.cpp | 4 ++-- svf/lib/SVFIR/SVFVariables.cpp | 7 +++---- 8 files changed, 22 insertions(+), 27 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 051314ee5..17541e7d8 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -174,6 +174,11 @@ class SVFIR : public IRGraph assert(icfg->totalICFGNode>0 && "empty ICFG! Build SVF IR first!"); return icfg; } + + inline bool isGlobalVar(const SVFVar* var) const { + return SVFUtil::isa(var) || SVFUtil::isa(var); + } + /// Set/Get CHG inline void setCHG(CommonCHGraph* c) { @@ -601,7 +606,7 @@ class SVFIR : public IRGraph inline NodeID addGlobalValueValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { - SVFVar* node = new GlobalValueValVar(curInst, i, icfgNode); + SVFVar* node = new GlobalValVar(curInst, i, icfgNode); return addNode(node, i); } @@ -636,7 +641,7 @@ class SVFIR : public IRGraph inline NodeID addGlobalValueObjNode(const SVFValue* curInst, const NodeID i) { const MemObj* mem = getMemObj(curInst); - GlobalValueObjVar* node = new GlobalValueObjVar(curInst, mem->getId(), mem); + GlobalObjVar* node = new GlobalObjVar(curInst, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } @@ -648,7 +653,6 @@ class SVFIR : public IRGraph } - /// Add a temp field value node, this method can only invoked by getGepValVar NodeID addGepValNode(const SVFValue* curInst,const SVFValue* val, const AccessPath& ap, NodeID i, const SVFType* type); /// Add a field obj node, this method can only invoked by getGepObjVar diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 2c4a9fb71..abe73fdbc 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -731,7 +731,7 @@ class ConstantDataValVar: public ValVar virtual const std::string toString() const; }; -class GlobalValueValVar : public ValVar +class GlobalValVar : public ValVar { friend class SVFIRWriter; friend class SVFIRReader; @@ -762,7 +762,7 @@ class GlobalValueValVar : public ValVar //@} /// Constructor - GlobalValueValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + GlobalValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = GlobalValueValNode) : ValVar(val, i, ty, icn) { @@ -957,18 +957,18 @@ class ConstantNullPtrValVar: public ConstantDataValVar virtual const std::string toString() const; }; -class GlobalValueObjVar: public FIObjVar { +class GlobalObjVar : public FIObjVar { friend class SVFIRWriter; friend class SVFIRReader; private: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - GlobalValueObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : FIObjVar(i, ty) {} + GlobalObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : FIObjVar(i, ty) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const GlobalValueObjVar*) + static inline bool classof(const GlobalObjVar*) { return true; } @@ -995,7 +995,7 @@ class GlobalValueObjVar: public FIObjVar { //@} /// Constructor - GlobalValueObjVar(const SVFValue* val, NodeID i, const MemObj* mem, + GlobalObjVar(const SVFValue* val, NodeID i, const MemObj* mem, PNODEK ty = GlobalValueObjNode): FIObjVar(val, i,mem,ty){ } diff --git a/svf/lib/AE/Core/AbstractState.cpp b/svf/lib/AE/Core/AbstractState.cpp index fe9059234..55edd6fe0 100644 --- a/svf/lib/AE/Core/AbstractState.cpp +++ b/svf/lib/AE/Core/AbstractState.cpp @@ -200,7 +200,7 @@ void AbstractState::initObjVar(ObjVar* objVar) { (*this)[varId] = IntervalValue(0, 0); } - else if (SVFUtil::isa(objVar)) + else if (SVFUtil::isa(objVar)) { (*this)[varId] = AddressValue(AbstractState::getVirtualMemAddress(varId)); } diff --git a/svf/lib/MemoryModel/PointerAnalysis.cpp b/svf/lib/MemoryModel/PointerAnalysis.cpp index 54359e9e1..f74d00ffa 100644 --- a/svf/lib/MemoryModel/PointerAnalysis.cpp +++ b/svf/lib/MemoryModel/PointerAnalysis.cpp @@ -446,12 +446,12 @@ void PointerAnalysis::getVFnsFromPts(const CallICFGNode* cs, const PointsTo &tar if (ptdnode->hasValue()) { // ptd is global obj var or ptd's base is global val/obj var - if (SVFUtil::isa(ptdnode)) { + if (pag->isGlobalVar(ptdnode)) { const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(ptdnode->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); } else if (const GepObjVar *gep_vtbl = SVFUtil::dyn_cast(ptdnode)) { - if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { + if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(gep_vtbl->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); diff --git a/svf/lib/SABER/SaberCondAllocator.cpp b/svf/lib/SABER/SaberCondAllocator.cpp index ef074ce90..15d876241 100644 --- a/svf/lib/SABER/SaberCondAllocator.cpp +++ b/svf/lib/SABER/SaberCondAllocator.cpp @@ -401,14 +401,6 @@ bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const const SVFVar* op0 = cmp->getOpVar(0); const SVFVar* op1 = cmp->getOpVar(1); - bool a = SVFUtil::dyn_cast(op0) || - SVFUtil::dyn_cast(op0); - bool b = SVFUtil::dyn_cast(op1) || - SVFUtil::dyn_cast(op1); - bool c = SVFUtil::dyn_cast(op0->getValue()); - bool d = SVFUtil::dyn_cast(op1->getValue()); - assert(a == c); - assert(b == d); if (SVFUtil::isa(op1)) { Set inDirVal; diff --git a/svf/lib/SABER/SaberSVFGBuilder.cpp b/svf/lib/SABER/SaberSVFGBuilder.cpp index 96fd1eacc..8adb4d948 100644 --- a/svf/lib/SABER/SaberSVFGBuilder.cpp +++ b/svf/lib/SABER/SaberSVFGBuilder.cpp @@ -82,7 +82,7 @@ void SaberSVFGBuilder::collectGlobals(BVDataPTAImpl* pta) if(SVFUtil::isa(pag->getGNode(gepobj->getBaseNode()))) continue; } - if(pagNode->hasValue() && SVFUtil::isa(pagNode)) + if(pagNode->hasValue() && pag->isGlobalVar(pagNode)) worklist.push_back(it->first); } diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index df281f506..e2759a884 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -227,13 +227,13 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(FunObjNode, FunObjVar); CASE(FunValNode, FunValVar); CASE(ConstantDataValNode, ConstantDataValVar); - CASE(GlobalValueValNode, GlobalValueValVar); + CASE(GlobalValueValNode, GlobalValVar); CASE(BlackHoleNode, BlackHoleVar); CASE(ConstantFPValNode, ConstantFPValVar); CASE(ConstantIntValNode, ConstantIntValVar); CASE(ConstantNullptrValNode, ConstantNullPtrValVar); CASE(ConstantDataObjNode, ConstantDataObjVar); - CASE(GlobalValueObjNode, GlobalValueObjVar); + CASE(GlobalValueObjNode, GlobalObjVar); CASE(ConstantFPObjNode, ConstantFPObjVar); CASE(ConstantIntObjNode, ConstantIntObjVar); CASE(ConstantNullptrObjNode, ConstantNullPtrObjVar); diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index bc30cba7e..fde2ca38b 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -221,7 +221,7 @@ const std::string ConstantDataValVar::toString() const { return rawstr.str(); } -const std::string GlobalValueValVar::toString() const { +const std::string GlobalValVar::toString() const { std::string str; std::stringstream rawstr(str); rawstr << "GlobalValueValVar ID: " << getId(); @@ -260,7 +260,7 @@ const std::string ConstantIntValVar::toString() const { const std::string ConstantNullPtrValVar::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "ConstantObjVar ID: " << getId(); + rawstr << "ConstantNullPtrValVar ID: " << getId(); if (Options::ShowSVFIRValue()) { rawstr << "\n"; @@ -269,8 +269,7 @@ const std::string ConstantNullPtrValVar::toString() const { return rawstr.str(); } - -const std::string GlobalValueObjVar::toString() const { +const std::string GlobalObjVar::toString() const { std::string str; std::stringstream rawstr(str); rawstr << "GlobalValueObjNode ID: " << getId(); From 879eca2440db00e84b5637b6995232a4639668e7 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Fri, 13 Dec 2024 20:50:03 +1300 Subject: [PATCH 25/28] refactor GlobalClass, and remove unused codes --- svf/include/SVFIR/SVFIR.h | 10 +++++++--- svf/include/SVFIR/SVFVariables.h | 12 ++++++------ svf/lib/AE/Core/AbstractState.cpp | 2 +- svf/lib/MemoryModel/PointerAnalysis.cpp | 4 ++-- svf/lib/SABER/SaberCondAllocator.cpp | 8 -------- svf/lib/SABER/SaberSVFGBuilder.cpp | 2 +- svf/lib/SVFIR/SVFFileSystem.cpp | 4 ++-- svf/lib/SVFIR/SVFVariables.cpp | 11 +++++------ 8 files changed, 24 insertions(+), 29 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 051314ee5..17541e7d8 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -174,6 +174,11 @@ class SVFIR : public IRGraph assert(icfg->totalICFGNode>0 && "empty ICFG! Build SVF IR first!"); return icfg; } + + inline bool isGlobalVar(const SVFVar* var) const { + return SVFUtil::isa(var) || SVFUtil::isa(var); + } + /// Set/Get CHG inline void setCHG(CommonCHGraph* c) { @@ -601,7 +606,7 @@ class SVFIR : public IRGraph inline NodeID addGlobalValueValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode) { - SVFVar* node = new GlobalValueValVar(curInst, i, icfgNode); + SVFVar* node = new GlobalValVar(curInst, i, icfgNode); return addNode(node, i); } @@ -636,7 +641,7 @@ class SVFIR : public IRGraph inline NodeID addGlobalValueObjNode(const SVFValue* curInst, const NodeID i) { const MemObj* mem = getMemObj(curInst); - GlobalValueObjVar* node = new GlobalValueObjVar(curInst, mem->getId(), mem); + GlobalObjVar* node = new GlobalObjVar(curInst, mem->getId(), mem); return addObjNode(mem->getValue(), node, mem->getId()); } @@ -648,7 +653,6 @@ class SVFIR : public IRGraph } - /// Add a temp field value node, this method can only invoked by getGepValVar NodeID addGepValNode(const SVFValue* curInst,const SVFValue* val, const AccessPath& ap, NodeID i, const SVFType* type); /// Add a field obj node, this method can only invoked by getGepObjVar diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 2c4a9fb71..abe73fdbc 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -731,7 +731,7 @@ class ConstantDataValVar: public ValVar virtual const std::string toString() const; }; -class GlobalValueValVar : public ValVar +class GlobalValVar : public ValVar { friend class SVFIRWriter; friend class SVFIRReader; @@ -762,7 +762,7 @@ class GlobalValueValVar : public ValVar //@} /// Constructor - GlobalValueValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, + GlobalValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, PNODEK ty = GlobalValueValNode) : ValVar(val, i, ty, icn) { @@ -957,18 +957,18 @@ class ConstantNullPtrValVar: public ConstantDataValVar virtual const std::string toString() const; }; -class GlobalValueObjVar: public FIObjVar { +class GlobalObjVar : public FIObjVar { friend class SVFIRWriter; friend class SVFIRReader; private: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - GlobalValueObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : FIObjVar(i, ty) {} + GlobalObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : FIObjVar(i, ty) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const GlobalValueObjVar*) + static inline bool classof(const GlobalObjVar*) { return true; } @@ -995,7 +995,7 @@ class GlobalValueObjVar: public FIObjVar { //@} /// Constructor - GlobalValueObjVar(const SVFValue* val, NodeID i, const MemObj* mem, + GlobalObjVar(const SVFValue* val, NodeID i, const MemObj* mem, PNODEK ty = GlobalValueObjNode): FIObjVar(val, i,mem,ty){ } diff --git a/svf/lib/AE/Core/AbstractState.cpp b/svf/lib/AE/Core/AbstractState.cpp index fe9059234..55edd6fe0 100644 --- a/svf/lib/AE/Core/AbstractState.cpp +++ b/svf/lib/AE/Core/AbstractState.cpp @@ -200,7 +200,7 @@ void AbstractState::initObjVar(ObjVar* objVar) { (*this)[varId] = IntervalValue(0, 0); } - else if (SVFUtil::isa(objVar)) + else if (SVFUtil::isa(objVar)) { (*this)[varId] = AddressValue(AbstractState::getVirtualMemAddress(varId)); } diff --git a/svf/lib/MemoryModel/PointerAnalysis.cpp b/svf/lib/MemoryModel/PointerAnalysis.cpp index 54359e9e1..f74d00ffa 100644 --- a/svf/lib/MemoryModel/PointerAnalysis.cpp +++ b/svf/lib/MemoryModel/PointerAnalysis.cpp @@ -446,12 +446,12 @@ void PointerAnalysis::getVFnsFromPts(const CallICFGNode* cs, const PointsTo &tar if (ptdnode->hasValue()) { // ptd is global obj var or ptd's base is global val/obj var - if (SVFUtil::isa(ptdnode)) { + if (pag->isGlobalVar(ptdnode)) { const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(ptdnode->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); } else if (const GepObjVar *gep_vtbl = SVFUtil::dyn_cast(ptdnode)) { - if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { + if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(gep_vtbl->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); diff --git a/svf/lib/SABER/SaberCondAllocator.cpp b/svf/lib/SABER/SaberCondAllocator.cpp index ef074ce90..15d876241 100644 --- a/svf/lib/SABER/SaberCondAllocator.cpp +++ b/svf/lib/SABER/SaberCondAllocator.cpp @@ -401,14 +401,6 @@ bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const const SVFVar* op0 = cmp->getOpVar(0); const SVFVar* op1 = cmp->getOpVar(1); - bool a = SVFUtil::dyn_cast(op0) || - SVFUtil::dyn_cast(op0); - bool b = SVFUtil::dyn_cast(op1) || - SVFUtil::dyn_cast(op1); - bool c = SVFUtil::dyn_cast(op0->getValue()); - bool d = SVFUtil::dyn_cast(op1->getValue()); - assert(a == c); - assert(b == d); if (SVFUtil::isa(op1)) { Set inDirVal; diff --git a/svf/lib/SABER/SaberSVFGBuilder.cpp b/svf/lib/SABER/SaberSVFGBuilder.cpp index 96fd1eacc..8adb4d948 100644 --- a/svf/lib/SABER/SaberSVFGBuilder.cpp +++ b/svf/lib/SABER/SaberSVFGBuilder.cpp @@ -82,7 +82,7 @@ void SaberSVFGBuilder::collectGlobals(BVDataPTAImpl* pta) if(SVFUtil::isa(pag->getGNode(gepobj->getBaseNode()))) continue; } - if(pagNode->hasValue() && SVFUtil::isa(pagNode)) + if(pagNode->hasValue() && pag->isGlobalVar(pagNode)) worklist.push_back(it->first); } diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index df281f506..e2759a884 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -227,13 +227,13 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(FunObjNode, FunObjVar); CASE(FunValNode, FunValVar); CASE(ConstantDataValNode, ConstantDataValVar); - CASE(GlobalValueValNode, GlobalValueValVar); + CASE(GlobalValueValNode, GlobalValVar); CASE(BlackHoleNode, BlackHoleVar); CASE(ConstantFPValNode, ConstantFPValVar); CASE(ConstantIntValNode, ConstantIntValVar); CASE(ConstantNullptrValNode, ConstantNullPtrValVar); CASE(ConstantDataObjNode, ConstantDataObjVar); - CASE(GlobalValueObjNode, GlobalValueObjVar); + CASE(GlobalValueObjNode, GlobalObjVar); CASE(ConstantFPObjNode, ConstantFPObjVar); CASE(ConstantIntObjNode, ConstantIntObjVar); CASE(ConstantNullptrObjNode, ConstantNullPtrObjVar); diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index bc30cba7e..1d70ed365 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -221,10 +221,10 @@ const std::string ConstantDataValVar::toString() const { return rawstr.str(); } -const std::string GlobalValueValVar::toString() const { +const std::string GlobalValVar::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "GlobalValueValVar ID: " << getId(); + rawstr << "GlobalValVar ID: " << getId(); if (Options::ShowSVFIRValue()) { rawstr << "\n"; @@ -260,7 +260,7 @@ const std::string ConstantIntValVar::toString() const { const std::string ConstantNullPtrValVar::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "ConstantObjVar ID: " << getId(); + rawstr << "ConstantNullPtrValVar ID: " << getId(); if (Options::ShowSVFIRValue()) { rawstr << "\n"; @@ -269,11 +269,10 @@ const std::string ConstantNullPtrValVar::toString() const { return rawstr.str(); } - -const std::string GlobalValueObjVar::toString() const { +const std::string GlobalObjVar::toString() const { std::string str; std::stringstream rawstr(str); - rawstr << "GlobalValueObjNode ID: " << getId(); + rawstr << "GlobalObjNode ID: " << getId(); if (Options::ShowSVFIRValue()) { rawstr << "\n"; From 3990a180bf31f18e726016d093607f6303b99da9 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Fri, 13 Dec 2024 21:22:23 +1300 Subject: [PATCH 26/28] refactor GlobalClass, and remove unused codes --- svf/include/Graphs/GenericGraph.h | 4 ++-- svf/include/SVFIR/SVFVariables.h | 24 ++++++++++++------------ svf/lib/SVFIR/SVFFileSystem.cpp | 4 ++-- svf/lib/SVFIR/SVFVariables.cpp | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/svf/include/Graphs/GenericGraph.h b/svf/include/Graphs/GenericGraph.h index 2305618ba..d183c3857 100644 --- a/svf/include/Graphs/GenericGraph.h +++ b/svf/include/Graphs/GenericGraph.h @@ -170,7 +170,7 @@ class SVFBaseNode // │ │ ├── Represents a variadic argument node VarargNode, // │ │ ├── Represents a global value node - GlobalValueValNode, + GlobalValNode, // │ │ ├── Represents a constant data value node ConstantDataValNode, // │ │ ├── Represents a black hole node @@ -196,7 +196,7 @@ class SVFBaseNode // │ ├── FunObjNode: Types of function object FunObjNode, // │ ├── GlobalValueObjNode: Types of global value object - GlobalValueObjNode, + GlobalObjNode, // │ ├── ConstantDataObjNode: Types of constant data object ConstantDataObjNode, // │ ├── ConstantFPObjNode: Types of constant float-point object diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index abe73fdbc..9aa49573c 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -745,25 +745,25 @@ class GlobalValVar : public ValVar } static inline bool classof(const ValVar* node) { - return node->getNodeKind() == GlobalValueValNode; + return node->getNodeKind() == GlobalValNode; } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == GlobalValueValNode; + return node->getNodeKind() == GlobalValNode; } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == GlobalValueValNode; + return node->getNodeKind() == GlobalValNode; } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == GlobalValueValNode; + return node->getNodeKind() == GlobalValNode; } //@} /// Constructor GlobalValVar(const SVFValue* val, NodeID i, const ICFGNode* icn, - PNODEK ty = GlobalValueValNode) + PNODEK ty = GlobalValNode) : ValVar(val, i, ty, icn) { @@ -963,7 +963,7 @@ class GlobalObjVar : public FIObjVar { private: /// Constructor to create empty ObjVar (for SVFIRReader/deserialization) - GlobalObjVar(NodeID i, PNODEK ty = GlobalValueObjNode) : FIObjVar(i, ty) {} + GlobalObjVar(NodeID i, PNODEK ty = GlobalObjNode) : FIObjVar(i, ty) {} public: /// Methods for support type inquiry through isa, cast, and dyn_cast: @@ -974,29 +974,29 @@ class GlobalObjVar : public FIObjVar { } static inline bool classof(const FIObjVar* node) { - return node->getNodeKind() == GlobalValueObjNode; + return node->getNodeKind() == GlobalObjNode; } static inline bool classof(const ObjVar* node) { - return node->getNodeKind() == GlobalValueObjNode; + return node->getNodeKind() == GlobalObjNode; } static inline bool classof(const SVFVar* node) { - return node->getNodeKind() == GlobalValueObjNode; + return node->getNodeKind() == GlobalObjNode; } static inline bool classof(const GenericPAGNodeTy* node) { - return node->getNodeKind() == GlobalValueObjNode; + return node->getNodeKind() == GlobalObjNode; } static inline bool classof(const SVFBaseNode* node) { - return node->getNodeKind() == GlobalValueObjNode; + return node->getNodeKind() == GlobalObjNode; } //@} /// Constructor GlobalObjVar(const SVFValue* val, NodeID i, const MemObj* mem, - PNODEK ty = GlobalValueObjNode): FIObjVar(val, i,mem,ty){ + PNODEK ty = GlobalObjNode): FIObjVar(val, i,mem,ty){ } diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index e2759a884..f328b513a 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -227,13 +227,13 @@ cJSON* SVFIRWriter::virtToJson(const SVFVar* var) CASE(FunObjNode, FunObjVar); CASE(FunValNode, FunValVar); CASE(ConstantDataValNode, ConstantDataValVar); - CASE(GlobalValueValNode, GlobalValVar); + CASE(GlobalValNode, GlobalValVar); CASE(BlackHoleNode, BlackHoleVar); CASE(ConstantFPValNode, ConstantFPValVar); CASE(ConstantIntValNode, ConstantIntValVar); CASE(ConstantNullptrValNode, ConstantNullPtrValVar); CASE(ConstantDataObjNode, ConstantDataObjVar); - CASE(GlobalValueObjNode, GlobalObjVar); + CASE(GlobalObjNode, GlobalObjVar); CASE(ConstantFPObjNode, ConstantFPObjVar); CASE(ConstantIntObjNode, ConstantIntObjVar); CASE(ConstantNullptrObjNode, ConstantNullPtrObjVar); diff --git a/svf/lib/SVFIR/SVFVariables.cpp b/svf/lib/SVFIR/SVFVariables.cpp index 1d70ed365..e9818790c 100644 --- a/svf/lib/SVFIR/SVFVariables.cpp +++ b/svf/lib/SVFIR/SVFVariables.cpp @@ -47,7 +47,7 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : { case ValNode: case ConstantDataValNode: - case GlobalValueValNode: + case GlobalValNode: case BlackHoleNode: case ConstantFPValNode: case ConstantIntValNode: @@ -75,7 +75,7 @@ SVFVar::SVFVar(const SVFValue* val, NodeID i, PNODEK k) : case GepObjNode: case FIObjNode: case ConstantDataObjNode: - case GlobalValueObjNode: + case GlobalObjNode: case ConstantFPObjNode: case ConstantIntObjNode: case ConstantNullptrObjNode: From 67995e33dcaa480bc5bc390f157839ba165741f0 Mon Sep 17 00:00:00 2001 From: jumormt Date: Fri, 13 Dec 2024 22:30:23 +1100 Subject: [PATCH 27/28] Refactor --- svf/include/SVFIR/SVFIR.h | 3 --- svf/include/Util/SVFUtil.h | 1 + svf/lib/MemoryModel/PointerAnalysis.cpp | 8 +------- svf/lib/SABER/SaberCondAllocator.cpp | 4 ++-- svf/lib/SABER/SaberSVFGBuilder.cpp | 2 +- svf/lib/Util/SVFUtil.cpp | 15 +++++++++++++++ 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/svf/include/SVFIR/SVFIR.h b/svf/include/SVFIR/SVFIR.h index 17541e7d8..da549c4d4 100644 --- a/svf/include/SVFIR/SVFIR.h +++ b/svf/include/SVFIR/SVFIR.h @@ -175,9 +175,6 @@ class SVFIR : public IRGraph return icfg; } - inline bool isGlobalVar(const SVFVar* var) const { - return SVFUtil::isa(var) || SVFUtil::isa(var); - } /// Set/Get CHG inline void setCHG(CommonCHGraph* c) diff --git a/svf/include/Util/SVFUtil.h b/svf/include/Util/SVFUtil.h index 3ec67b1e2..22e912045 100644 --- a/svf/include/Util/SVFUtil.h +++ b/svf/include/Util/SVFUtil.h @@ -443,6 +443,7 @@ inline const ValVar* getActualParmAtForkSite(const CallICFGNode* cs) bool isProgExitCall(const CallICFGNode* cs); +bool varHasGlobalValue(const SVFVar* var); template constexpr typename std::remove_reference::type && diff --git a/svf/lib/MemoryModel/PointerAnalysis.cpp b/svf/lib/MemoryModel/PointerAnalysis.cpp index f74d00ffa..0140085b4 100644 --- a/svf/lib/MemoryModel/PointerAnalysis.cpp +++ b/svf/lib/MemoryModel/PointerAnalysis.cpp @@ -446,16 +446,10 @@ void PointerAnalysis::getVFnsFromPts(const CallICFGNode* cs, const PointsTo &tar if (ptdnode->hasValue()) { // ptd is global obj var or ptd's base is global val/obj var - if (pag->isGlobalVar(ptdnode)) { + if (SVFUtil::varHasGlobalValue(ptdnode)) { const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(ptdnode->getValue()); if (chaVtbls.find(globalValue) != chaVtbls.end()) vtbls.insert(globalValue); - } else if (const GepObjVar *gep_vtbl = SVFUtil::dyn_cast(ptdnode)) { - if (SVFUtil::isa(pag->getGNode(gep_vtbl->getBaseNode()))) { - const SVFGlobalValue* globalValue = SVFUtil::dyn_cast(gep_vtbl->getValue()); - if (chaVtbls.find(globalValue) != chaVtbls.end()) - vtbls.insert(globalValue); - } } } } diff --git a/svf/lib/SABER/SaberCondAllocator.cpp b/svf/lib/SABER/SaberCondAllocator.cpp index 15d876241..92e80f367 100644 --- a/svf/lib/SABER/SaberCondAllocator.cpp +++ b/svf/lib/SABER/SaberCondAllocator.cpp @@ -401,7 +401,7 @@ bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const const SVFVar* op0 = cmp->getOpVar(0); const SVFVar* op1 = cmp->getOpVar(1); - if (SVFUtil::isa(op1)) + if (SVFUtil::isa(op1)) { Set inDirVal; inDirVal.insert(getCurEvalSVFGNode()->getValue()); @@ -411,7 +411,7 @@ bool SaberCondAllocator::isTestContainsNullAndTheValue(const CmpStmt *cmp) const } return inDirVal.find(op0->getValue()) != inDirVal.end(); } - else if (SVFUtil::isa(op0)) + else if (SVFUtil::isa(op0)) { Set inDirVal; inDirVal.insert(getCurEvalSVFGNode()->getValue()); diff --git a/svf/lib/SABER/SaberSVFGBuilder.cpp b/svf/lib/SABER/SaberSVFGBuilder.cpp index 8adb4d948..b82105ddc 100644 --- a/svf/lib/SABER/SaberSVFGBuilder.cpp +++ b/svf/lib/SABER/SaberSVFGBuilder.cpp @@ -82,7 +82,7 @@ void SaberSVFGBuilder::collectGlobals(BVDataPTAImpl* pta) if(SVFUtil::isa(pag->getGNode(gepobj->getBaseNode()))) continue; } - if(pagNode->hasValue() && pag->isGlobalVar(pagNode)) + if(SVFUtil::varHasGlobalValue(pagNode)) worklist.push_back(it->first); } diff --git a/svf/lib/Util/SVFUtil.cpp b/svf/lib/Util/SVFUtil.cpp index ecaed39fe..ba1c13730 100644 --- a/svf/lib/Util/SVFUtil.cpp +++ b/svf/lib/Util/SVFUtil.cpp @@ -430,4 +430,19 @@ const ObjVar* SVFUtil::getObjVarOfValVar(const SVF::ValVar* valVar) { assert(valVar->getInEdges().size() == 1); return SVFUtil::dyn_cast((*valVar->getInEdges().begin())->getSrcNode()); +} + +bool SVFUtil::varHasGlobalValue(const SVF::SVFVar* var) +{ + if(isa(var)) return true; + SVFIR* pag = SVFIR::getPAG(); + if(const auto globalValVar = dyn_cast(var)) + { + return isa(pag->getGNode(globalValVar->getBaseNode())); + } + else if (const auto globalObjVar = dyn_cast(var)) + { + return isa(pag->getGNode(globalObjVar->getBaseNode())); + } + return false; } \ No newline at end of file From 1e0c87015fa056639090be194577fd0ceb010448 Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Tue, 31 Dec 2024 20:26:19 +1100 Subject: [PATCH 28/28] fix some bugs in classof functions --- svf/include/SVFIR/SVFVariables.h | 66 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/svf/include/SVFIR/SVFVariables.h b/svf/include/SVFIR/SVFVariables.h index 9aa49573c..2648423c5 100644 --- a/svf/include/SVFIR/SVFVariables.h +++ b/svf/include/SVFIR/SVFVariables.h @@ -739,7 +739,7 @@ class GlobalValVar : public ValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const GlobalValVar*) { return true; } @@ -780,7 +780,7 @@ class BlackHoleVar: public ConstantDataValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const GlobalValVar*) { return true; } @@ -824,10 +824,14 @@ class ConstantFPValVar: public ConstantDataValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const ConstantFPValVar*) { return true; } + static inline bool classof(const ConstantDataValVar* node) + { + return node->getNodeKind() == ConstantFPValNode; + } static inline bool classof(const ValVar* node) { return node->getNodeKind() == ConstantFPValNode; @@ -873,10 +877,14 @@ class ConstantIntValVar: public ConstantDataValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const ConstantIntValVar*) { return true; } + static inline bool classof(const ConstantDataValVar* node) + { + return node->getNodeKind() == ConstantIntValNode; + } static inline bool classof(const ValVar* node) { return node->getNodeKind() == ConstantIntValNode; @@ -924,10 +932,14 @@ class ConstantNullPtrValVar: public ConstantDataValVar public: /// Methods for support type inquiry through isa, cast, and dyn_cast: //@{ - static inline bool classof(const FunValVar*) + static inline bool classof(const ConstantNullPtrValVar*) { return true; } + static inline bool classof(const ConstantDataValVar* node) + { + return node->getNodeKind() == ConstantNullptrValNode; + } static inline bool classof(const ValVar* node) { return node->getNodeKind() == ConstantNullptrValNode; @@ -1018,15 +1030,15 @@ class ConstantDataObjVar: public FIObjVar { { return true; } - static inline bool classof(const SVFVar* node) + static inline bool classof(const FIObjVar* node) { return isConstantDataObjVarKinds(node->getNodeKind()); } - static inline bool classof(const ObjVar* node) + static inline bool classof(const SVFVar* node) { return isConstantDataObjVarKinds(node->getNodeKind()); } - static inline bool classof(const FIObjVar* node) + static inline bool classof(const ObjVar* node) { return isConstantDataObjVarKinds(node->getNodeKind()); } @@ -1064,18 +1076,28 @@ class ConstantFPObjVar: public ConstantDataObjVar public: //@{ Methods for support type inquiry through isa, cast, and dyn_cast: - static inline bool classof(const ConstantDataObjVar*) + static inline bool classof(const ConstantFPObjVar*) { return true; } + static inline bool classof(const ConstantDataObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantFPObjNode; + } + static inline bool classof(const FIObjVar* node) { + return node->getNodeKind() == SVFVar::ConstantFPObjNode; + } + static inline bool classof(const SVFVar* node) { return node->getNodeKind() == SVFVar::ConstantFPObjNode; } + static inline bool classof(const ObjVar* node) { return node->getNodeKind() == SVFVar::ConstantFPObjNode; } + static inline bool classof(const GenericPAGNodeTy* node) { return node->getNodeKind() == SVFVar::ConstantFPObjNode; @@ -1117,10 +1139,21 @@ class ConstantIntObjVar: public ConstantDataObjVar public: //@{ Methods for support type inquiry through isa, cast, and dyn_cast: - static inline bool classof(const ConstantDataObjVar*) + static inline bool classof(const ConstantIntObjVar*) { return true; } + + static inline bool classof(const ConstantDataObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantIntObjNode; + } + + static inline bool classof(const FIObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantIntObjNode; + } + static inline bool classof(const SVFVar* node) { return node->getNodeKind() == SVFVar::ConstantIntObjNode; @@ -1173,10 +1206,21 @@ class ConstantNullPtrObjVar: public ConstantDataObjVar public: //@{ Methods for support type inquiry through isa, cast, and dyn_cast: - static inline bool classof(const ConstantDataObjVar*) + static inline bool classof(const ConstantNullPtrObjVar*) { return true; } + + static inline bool classof(const ConstantDataObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantNullptrObjNode; + } + + static inline bool classof(const FIObjVar* node) + { + return node->getNodeKind() == SVFVar::ConstantNullptrObjNode; + } + static inline bool classof(const SVFVar* node) { return node->getNodeKind() == SVFVar::ConstantNullptrObjNode;