From 5d9d52b7f0d340c9cec6b63061087f1ba2b16d4c Mon Sep 17 00:00:00 2001 From: bjjwwang Date: Tue, 10 Dec 2024 14:09:32 +1100 Subject: [PATCH] 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..6a035dc7a 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->addConstantFPValNode(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->addConstantFPObjNode(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