Skip to content

Commit

Permalink
refactor Global/Constant Val/Obj
Browse files Browse the repository at this point in the history
1) remove unused format
2) remove confusing function like varHasGlobalVar
3) align the comment
  • Loading branch information
bjjwwang committed Jan 6, 2025
1 parent 43e7eac commit a851e8c
Show file tree
Hide file tree
Showing 17 changed files with 989 additions and 97 deletions.
8 changes: 4 additions & 4 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ class LLVMModuleSet
inline void addInstructionMap(const Instruction* inst, CallICFGNode* svfInst)
{
CSToCallNodeMap[inst] = svfInst;
setValueAttr(inst,svfInst);
addToLLVMVal2SVFVarMap(inst, svfInst);
}
inline void addInstructionMap(const Instruction* inst, RetICFGNode* svfInst)
{
CSToRetNodeMap[inst] = svfInst;
setValueAttr(inst,svfInst);
addToLLVMVal2SVFVarMap(inst, svfInst);
}
inline void addInstructionMap(const Instruction* inst, IntraICFGNode* svfInst)
{
InstToBlockNodeMap[inst] = svfInst;
setValueAttr(inst,svfInst);
addToLLVMVal2SVFVarMap(inst, svfInst);
}

inline void addArgumentMap(const Argument* arg, SVFArgument* svfArg)
Expand Down Expand Up @@ -426,7 +426,7 @@ class LLVMModuleSet
void initSVFBasicBlock(const Function* func);
void initDomTree(SVFFunction* func, const Function* f);
void setValueAttr(const Value* val, SVFValue* value);
void setValueAttr(const Value* val, SVFBaseNode* svfBaseNode);
void addToLLVMVal2SVFVarMap(const Value* val, SVFBaseNode* svfBaseNode);
void buildFunToFunMap();
void buildGlobalDefToRepMap();
/// Invoke llvm passes to modify module
Expand Down
4 changes: 3 additions & 1 deletion svf-llvm/include/SVF-LLVM/SVFIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
{
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);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
constNull, pag->getGNode(pag->getNullPtr()));
setCurrentLocation(constNull, nullptr);
addBlackHoleAddrEdge(pag->getBlkPtr());
return nullPtr;
Expand Down
5 changes: 3 additions & 2 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ void LLVMModuleSet::dumpModulesToFile(const std::string& suffix)
void LLVMModuleSet::addFunctionMap(const Function* func, CallGraphNode* svfFunc)
{
LLVMFunc2CallGraphNode[func] = svfFunc;
setValueAttr(func, svfFunc);
addToLLVMVal2SVFVarMap(func, svfFunc);
}

void LLVMModuleSet::setValueAttr(const Value* val, SVFValue* svfvalue)
Expand Down Expand Up @@ -1254,7 +1254,8 @@ void LLVMModuleSet::setValueAttr(const Value* val, SVFValue* svfvalue)
svfvalue->setSourceLoc(LLVMUtil::getSourceLoc(val));
}

void LLVMModuleSet::setValueAttr(const SVF::Value* val, SVF::SVFBaseNode* svfBaseNode)
void LLVMModuleSet::addToLLVMVal2SVFVarMap(const Value* val,
SVFBaseNode* svfBaseNode)
{
SVFBaseNode2LLVMValue[svfBaseNode] = val;
svfBaseNode->setSourceLoc(LLVMUtil::getSourceLoc(val));
Expand Down
74 changes: 67 additions & 7 deletions svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ void SVFIRBuilder::initialiseNodes()
continue;

const ICFGNode* icfgNode = nullptr;

// Check if the value is an instruction and get its ICFG node
auto llvmValue = llvmModuleSet()->getLLVMValue(iter->first);
if (const Instruction* inst =
SVFUtil::dyn_cast<Instruction>(llvmModuleSet()->getLLVMValue(iter->first)))
SVFUtil::dyn_cast<Instruction>(llvmValue))
{
if (llvmModuleSet()->hasICFGNode(inst))
{
Expand All @@ -240,12 +239,42 @@ void SVFIRBuilder::initialiseNodes()

// Check if the value is a function and get its call graph node
if (const Function* func =
SVFUtil::dyn_cast<Function>(llvmModuleSet()->getLLVMValue(iter->first)))
SVFUtil::dyn_cast<Function>(llvmValue))
{
const CallGraphNode* cgn = llvmModuleSet()->getCallGraphNode(func);
// add value node representing the function
pag->addFunValNode(cgn, iter->second, icfgNode);
}
else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
{
pag->addConstantFPValNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
fpValue, pag->getGNode(iter->second));
}
else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
{
pag->addConstantIntValNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
intValue, pag->getGNode(iter->second));
}
else if (auto nullValue = SVFUtil::dyn_cast<ConstantPointerNull>(llvmValue))
{
pag->addConstantNullPtrValNode(iter->first, iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
nullValue, pag->getGNode(iter->second));

Check warning on line 264 in svf-llvm/lib/SVFIRBuilder.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/SVFIRBuilder.cpp#L262-L264

Added lines #L262 - L264 were not covered by tests
}
else if (auto globalValue = SVFUtil::dyn_cast<GlobalValue>(llvmValue))
{
pag->addGlobalValueValNode(iter->first, iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
globalValue, pag->getGNode(iter->second));
}
else if (auto dataValue = SVFUtil::dyn_cast<ConstantData>(llvmValue))
{
pag->addConstantDataValNode(iter->first, iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
dataValue, pag->getGNode(iter->second));

Check warning on line 276 in svf-llvm/lib/SVFIRBuilder.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/SVFIRBuilder.cpp#L274-L276

Added lines #L274 - L276 were not covered by tests
}
else
{
// Add value node to PAG
Expand Down Expand Up @@ -279,16 +308,47 @@ void SVFIRBuilder::initialiseNodes()
const SVFFunction* f =
SVFUtil::cast<SVFInstruction>(iter->first)->getFunction();
pag->addHeapObjNode(iter->first, f, iter->second);
llvmModuleSet()->setValueAttr(llvmValue,pag->getGNode(iter->second));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
llvmValue, pag->getGNode(iter->second));
}
// Check if the value is an alloca instruction and add a stack object node
else if (LLVMUtil::isStackObj(llvmValue))
{
const SVFFunction* f =
SVFUtil::cast<SVFInstruction>(iter->first)->getFunction();
pag->addStackObjNode(iter->first, f, iter->second);
llvmModuleSet()->setValueAttr(llvmValue,
pag->getGNode(iter->second));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
llvmValue, pag->getGNode(iter->second));
}
else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
{
pag->addConstantFPObjNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
fpValue, pag->getGNode(iter->second));
}
else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
{
pag->addConstantIntObjNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
intValue, pag->getGNode(iter->second));
}
else if (auto nullValue = SVFUtil::dyn_cast<ConstantPointerNull>(llvmValue))
{
pag->addConstantNullPtrObjNode(iter->first, iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
nullValue, pag->getGNode(iter->second));

Check warning on line 339 in svf-llvm/lib/SVFIRBuilder.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/SVFIRBuilder.cpp#L337-L339

Added lines #L337 - L339 were not covered by tests
}
else if (auto globalValue = SVFUtil::dyn_cast<GlobalValue>(llvmValue))
{
pag->addGlobalValueObjNode(iter->first, iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
globalValue, pag->getGNode(iter->second));
}
else if (auto dataValue = SVFUtil::dyn_cast<ConstantData>(llvmValue))
{
pag->addConstantDataObjNode(iter->first, iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
dataValue, pag->getGNode(iter->second));

Check warning on line 351 in svf-llvm/lib/SVFIRBuilder.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/SVFIRBuilder.cpp#L349-L351

Added lines #L349 - L351 were not covered by tests
}
// Add a generic object node for other types of values
else
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/SVFIRExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
62 changes: 45 additions & 17 deletions svf/include/Graphs/GenericGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,32 @@ class SVFBaseNode

// ┌── SVFVar: Classes of top-level variables (ValVar) and address-taken variables (ObjVar)
// │ └── ValVar: Classes of top-level variable nodes
ValNode, // ├──Represents a standard value variable
FunValNode, // ├──Represents a Function value variable
GepValNode, // ├──Represents a GEP value variable
RetNode, // ├──Represents a return value node
VarargNode, // ├──Represents a variadic argument node
DummyValNode, // ├──Dummy node for uninitialized values
ValNode, // ├──Represents a standard value variable
FunValNode, // ├──Represents a Function value variable
GepValNode, // ├──Represents a GEP value variable
RetNode, // ├──Represents a return value node
VarargNode, // ├──Represents a variadic argument node
GlobalValNode, // ├──Represents a global variable node
ConstantDataValNode, // ├──Represents a constant data variable
BlackHoleNode, // ├──Represents a black hole node
ConstantFPValNode, // ├──Represents a constant float-point value node
ConstantIntValNode, // ├── Represents a constant integer value node
ConstantNullptrValNode, // ├── Represents a constant nullptr value node
DummyValNode, // ├──Dummy node for uninitialized values
// │ └── ObjVar: Classes of object variable nodes
ObjNode, // ├──Represents an object variable
GepObjNode, // ├──Represents a GEP object variable
ObjNode, // ├──Represents an object variable
GepObjNode, // ├──Represents a GEP object variable
// │ └── BaseObjVar: Classes of base object nodes
BaseObjNode, // ├──Represents a base object node
FunObjNode, // ├──Types of function object
HeapObjNode, // ├──Types of heap object
StackObjNode, // ├──Types of stack object
DummyObjNode, // ├──Dummy node for uninitialized objects
BaseObjNode, // ├──Represents a base object node
FunObjNode, // ├──Types of function object
HeapObjNode, // ├──Types of heap object
StackObjNode, // ├──Types of stack object
GlobalObjNode, // ├──Types of global object
ConstantDataObjNode, // ├──Types of constant data object
ConstantFPObjNode, // ├──Types of constant float-point object
ConstantIntObjNode, // ├──Types of constant integer object
ConstantNullptrObjNode, // ├──Types of constant nullptr object
DummyObjNode, // ├──Dummy node for uninitialized objects
// └────────

// ┌── VFGNode: Classes of Value Flow Graph (VFG) node kinds with operations
Expand Down Expand Up @@ -278,7 +289,7 @@ class SVFBaseNode

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

Expand All @@ -287,28 +298,45 @@ 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;
}


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 <= ConstantNullptrValNode && n >= ConstantDataValNode;
}

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

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

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,
Expand Down
91 changes: 91 additions & 0 deletions svf/include/SVFIR/SVFIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ class SVFIR : public IRGraph
return SVFUtil::dyn_cast<BaseObjVar>(node);
}

inline const ValVar* getBaseValVar(NodeID id) const
{
const SVFVar* node = getGNode(id);
if(const GepValVar* gepVar = SVFUtil::dyn_cast<GepValVar>(node))
return SVFUtil::dyn_cast<ValVar>(
getGNode(gepVar->getBaseNode()));
else
return SVFUtil::dyn_cast<ValVar>(node);
}

inline const MemObj*getObject(const ObjVar* node) const
{
return node->getMemObj();
Expand Down Expand Up @@ -568,6 +578,39 @@ class SVFIR : public IRGraph
return addValNode(nullptr, 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 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);
}

inline NodeID addConstantNullPtrValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode)
{
SVFVar* node = new ConstantNullPtrValVar(curInst, i, icfgNode);
return addNode(node, i);
}

inline NodeID addGlobalValueValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode)
{
SVFVar* node = new GlobalValVar(curInst, i, icfgNode);
return addNode(node, i);
}

inline NodeID addConstantDataValNode(const SVFValue* curInst, const NodeID i, const ICFGNode* icfgNode)

Check warning on line 607 in svf/include/SVFIR/SVFIR.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFIR.h#L607

Added line #L607 was not covered by tests
{
SVFVar* node = new ConstantDataValVar(curInst, i, icfgNode);
return addNode(node, i);

Check warning on line 610 in svf/include/SVFIR/SVFIR.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFIR.h#L609-L610

Added lines #L609 - L610 were not covered by tests
}


/// Add a memory obj node
inline NodeID addObjNode(const SVFValue* val, NodeID i)
{
Expand Down Expand Up @@ -601,6 +644,54 @@ class SVFIR : public IRGraph
}

NodeID addFunObjNode(const CallGraphNode* callGraphNode, NodeID id);


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());
ConstantFPObjVar* node = new ConstantFPObjVar(curInst, dval, mem->getId(), mem);
return addObjNode(curInst, node, mem->getId());
}


inline NodeID addConstantIntObjNode(const SVFValue* curInst, s64_t sval, u64_t zval, const NodeID i) {
const MemObj* mem = getMemObj(curInst);
NodeID base = mem->getId();
memToFieldsMap[base].set(mem->getId());
ConstantIntObjVar* node =
new ConstantIntObjVar(curInst, sval, zval, mem->getId(), mem);
return addObjNode(curInst, node, mem->getId());
}


inline NodeID addConstantNullPtrObjNode(const SVFValue* curInst, const NodeID i) {
const MemObj* mem = getMemObj(curInst);
NodeID base = mem->getId();
memToFieldsMap[base].set(mem->getId());
ConstantNullPtrObjVar* node = new ConstantNullPtrObjVar(curInst, mem->getId(), mem);
return addObjNode(mem->getValue(), node, mem->getId());

Check warning on line 674 in svf/include/SVFIR/SVFIR.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFIR.h#L669-L674

Added lines #L669 - L674 were not covered by tests
}

inline NodeID addGlobalValueObjNode(const SVFValue* curInst, const NodeID i)
{
const MemObj* mem = getMemObj(curInst);
NodeID base = mem->getId();
memToFieldsMap[base].set(mem->getId());
GlobalObjVar* node = new GlobalObjVar(curInst, mem->getId(), mem);
return addObjNode(mem->getValue(), node, mem->getId());
}

inline NodeID addConstantDataObjNode(const SVFValue* curInst, const NodeID i)

Check warning on line 686 in svf/include/SVFIR/SVFIR.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFIR.h#L686

Added line #L686 was not covered by tests
{
const MemObj* mem = getMemObj(curInst);
NodeID base = mem->getId();
memToFieldsMap[base].set(mem->getId());
ConstantDataObjVar* node = new ConstantDataObjVar(curInst, mem->getId(), mem);
return addObjNode(mem->getValue(), node, mem->getId());

Check warning on line 692 in svf/include/SVFIR/SVFIR.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFIR.h#L688-L692

Added lines #L688 - L692 were not covered by tests
}

/// Add a unique return node for a procedure
inline NodeID addRetNode(const CallGraphNode* callGraphNode, NodeID i)
{
Expand Down
Loading

0 comments on commit a851e8c

Please sign in to comment.