Skip to content

Commit

Permalink
remove MemObj in SVFIR (SVF-tools#1631)
Browse files Browse the repository at this point in the history
* AB Test, pass all ci

* AB Test removed, pass all ci

* remove MemObj

* fix asan err

* remove useless NodeID

---------

Co-authored-by: bjjwwang <bjjwwang@github.com>
bjjwwang and bjjwwang authored Jan 17, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c280409 commit 89b8815
Showing 32 changed files with 532 additions and 917 deletions.
4 changes: 2 additions & 2 deletions svf-llvm/include/SVF-LLVM/SymbolTableBuilder.h
Original file line number Diff line number Diff line change
@@ -118,9 +118,9 @@ class SymbolTableBuilder
///Get a reference to StructInfo.
StInfo* getOrAddSVFTypeInfo(const Type* T);

MemObj* createBlkObj(SymID symId);
ObjTypeInfo* createBlkObjTypeInfo(SymID symId);

MemObj* createConstantObj(SymID symId);
ObjTypeInfo* createConstantObjTypeInfo(SymID symId);
};

} // End namespace SVF
64 changes: 36 additions & 28 deletions svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
@@ -67,25 +67,6 @@ SVFIR* SVFIRBuilder::build()
// Set callgraph
pag->setCallGraph(llvmModuleSet()->callgraph);

// Set icfgnode in memobj
for (auto& it : SymbolTableInfo::SymbolInfo()->idToObjMap())
{
if(!it.second->getValue())
continue;
if (const Instruction* inst =
SVFUtil::dyn_cast<Instruction>(llvmModuleSet()->getLLVMValue(
it.second->getValue())))
{
if(llvmModuleSet()->hasICFGNode(inst))
it.second->gNode = llvmModuleSet()->getICFGNode(inst);
}
else if (const Function* func = SVFUtil::dyn_cast<Function>(llvmModuleSet()->getLLVMValue(
it.second->getValue())))
{
it.second->gNode = llvmModuleSet()->getCallGraphNode(func);
}
}

CHGraph* chg = new CHGraph(pag->getModule());
CHGBuilder chgbuilder(chg);
chgbuilder.buildCHG();
@@ -309,60 +290,87 @@ void SVFIRBuilder::initialiseNodes()
// Check if the value is a function and add a function object node
if (const Function* func = SVFUtil::dyn_cast<Function>(llvmValue))
{
pag->addFunObjNode(iter->second, llvmModuleSet()->getCallGraphNode(func));
SymID id = symTable->getObjSym(llvmModuleSet()->getCallGraphNode(func)->getFunction());
pag->addFunObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id), llvmModuleSet()->getCallGraphNode(func));
}
// Check if the value is a heap object and add a heap object node
else if (LLVMUtil::isHeapObj(llvmValue))
{
SymID id = symTable->getObjSym(iter->first);
const SVFFunction* f =
SVFUtil::cast<SVFInstruction>(iter->first)->getFunction();
pag->addHeapObjNode(iter->first, iter->second, f);
pag->addHeapObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id), f);
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))
{
NodeID id = symTable->getObjSym(iter->first);
const SVFFunction* f =
SVFUtil::cast<SVFInstruction>(iter->first)->getFunction();
pag->addStackObjNode(iter->first, iter->second, f);
pag->addStackObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id), f);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
llvmValue, pag->getGNode(iter->second));
}
else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
{
pag->addConstantFPObjNode(iter->first, iter->second, LLVMUtil::getDoubleValue(fpValue));
NodeID id = symTable->getObjSym(iter->first);
pag->addConstantFPObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id), LLVMUtil::getDoubleValue(fpValue));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
fpValue, pag->getGNode(iter->second));
}
else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
{
pag->addConstantIntObjNode(iter->first, iter->second, LLVMUtil::getIntegerValue(intValue));
NodeID id = symTable->getObjSym(iter->first);
pag->addConstantIntObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id), LLVMUtil::getIntegerValue(intValue));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
intValue, pag->getGNode(iter->second));
}
else if (auto nullValue = SVFUtil::dyn_cast<ConstantPointerNull>(llvmValue))
{
pag->addConstantNullPtrObjNode(iter->first, iter->second);
NodeID id = symTable->getObjSym(iter->first);
pag->addConstantNullPtrObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
nullValue, pag->getGNode(iter->second));
}
else if (auto globalValue = SVFUtil::dyn_cast<GlobalValue>(llvmValue))
{
pag->addGlobalValueObjNode(iter->first, iter->second);
NodeID id = symTable->getObjSym(iter->first);
pag->addGlobalValueObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
globalValue, pag->getGNode(iter->second));
}
else if (auto dataValue = SVFUtil::dyn_cast<ConstantData>(llvmValue))
{
pag->addConstantDataObjNode(iter->first, iter->second);
NodeID id = symTable->getObjSym(iter->first);
pag->addConstantDataObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id));
llvmModuleSet()->addToLLVMVal2SVFVarMap(
dataValue, pag->getGNode(iter->second));
}
// Add a generic object node for other types of values
else
{
pag->addObjNode(iter->first, iter->second);
NodeID id = symTable->getObjSym(iter->first);
pag->addObjNode(iter->first, iter->second, symTable->getObjTypeInfo(id));
}
}

for (auto& it : SymbolTableInfo::SymbolInfo()->idToObjTypeInfoMap()) {
NodeID id = it.first;
if (BaseObjVar* obj = SVFUtil::dyn_cast<BaseObjVar>(pag->getGNode(id))) {
if (!obj->hasValue())
continue;

if (const Instruction* inst = SVFUtil::dyn_cast<Instruction>(llvmModuleSet()->getLLVMValue(obj->getValue())))
{
if(llvmModuleSet()->hasICFGNode(inst))
obj->gNode = llvmModuleSet()->getICFGNode(inst);
}
else if (const Function* func = SVFUtil::dyn_cast<Function>(llvmModuleSet()->getLLVMValue(obj->getValue())))
{
obj->gNode = llvmModuleSet()->getCallGraphNode(func);
}
}
}

43 changes: 22 additions & 21 deletions svf-llvm/lib/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
@@ -44,28 +44,31 @@ using namespace SVF;
using namespace SVFUtil;
using namespace LLVMUtil;

MemObj* SymbolTableBuilder::createBlkObj(SymID symId)
ObjTypeInfo* SymbolTableBuilder::createBlkObjTypeInfo(SymID symId)
{
assert(symInfo->isBlkObj(symId));
assert(symInfo->objMap.find(symId)==symInfo->objMap.end());
LLVMModuleSet* llvmset = LLVMModuleSet::getLLVMModuleSet();
MemObj* obj =
new MemObj(symId, symInfo->createObjTypeInfo(llvmset->getSVFType(
IntegerType::get(llvmset->getContext(), 32))));
symInfo->objMap[symId] = obj;
return obj;
if (symInfo->objTypeInfoMap.find(symId)==symInfo->objTypeInfoMap.end()) {
ObjTypeInfo* ti =symInfo->createObjTypeInfo(llvmset->getSVFType(
IntegerType::get(llvmset->getContext(), 32)));
symInfo->objTypeInfoMap[symId] = ti;
}
ObjTypeInfo* ti = symInfo->objTypeInfoMap[symId];
return ti;
}

MemObj* SymbolTableBuilder::createConstantObj(SymID symId)
ObjTypeInfo* SymbolTableBuilder::createConstantObjTypeInfo(SymID symId)
{
assert(symInfo->isConstantObj(symId));
assert(symInfo->objMap.find(symId)==symInfo->objMap.end());
LLVMModuleSet* llvmset = LLVMModuleSet::getLLVMModuleSet();
MemObj* obj =
new MemObj(symId, symInfo->createObjTypeInfo(llvmset->getSVFType(
IntegerType::get(llvmset->getContext(), 32))));
symInfo->objMap[symId] = obj;
return obj;
if (symInfo->objTypeInfoMap.find(symId)==symInfo->objTypeInfoMap.end())
{
ObjTypeInfo* ti = symInfo->createObjTypeInfo(
llvmset->getSVFType(IntegerType::get(llvmset->getContext(), 32)));
symInfo->objTypeInfoMap[symId] = ti;
}
ObjTypeInfo* ti = symInfo->objTypeInfoMap[symId];
return ti;
}


@@ -86,11 +89,11 @@ void SymbolTableBuilder::buildMemModel(SVFModule* svfModule)

// Object #2 is black hole the object that may point to any object
assert(symInfo->totalSymNum++ == SymbolTableInfo::BlackHole && "Something changed!");
createBlkObj(SymbolTableInfo::BlackHole);
createBlkObjTypeInfo(SymbolTableInfo::BlackHole);

// Object #3 always represents the unique constant of a program (merging all constants if Options::ModelConsts is disabled)
assert(symInfo->totalSymNum++ == SymbolTableInfo::ConstantObj && "Something changed!");
createConstantObj(SymbolTableInfo::ConstantObj);
createConstantObjTypeInfo(SymbolTableInfo::ConstantObj);

for (Module &M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
{
@@ -339,11 +342,9 @@ void SymbolTableBuilder::collectObj(const Value* val)
outs() << "create a new obj sym " << id << "\n");

// create a memory object
MemObj* mem =
new MemObj(id, createObjTypeInfo(val),
llvmModuleSet->getSVFValue(val));
assert(symInfo->objMap.find(id) == symInfo->objMap.end());
symInfo->objMap[id] = mem;
ObjTypeInfo* ti = createObjTypeInfo(val);
assert(symInfo->objTypeInfoMap.find(id) == symInfo->objTypeInfoMap.end());
symInfo->objTypeInfoMap[id] = ti;
}
}
}
12 changes: 5 additions & 7 deletions svf/include/DDA/DDAVFSolver.h
Original file line number Diff line number Diff line change
@@ -473,8 +473,6 @@ class DDAVFSolver
NodeID id = getPtrNodeID(var);
const BaseObjVar* baseObj = _pag->getBaseObject(id);
assert(baseObj && "base object is null??");
const MemObj* obj = _pag->getObject(id);
assert(obj && "object not found!!");
if(SVFUtil::isa<StackObjVar>(baseObj))
{
if(const SVFFunction* svffun = _pag->getGNode(id)->getFunction())
@@ -645,14 +643,14 @@ class DDAVFSolver

inline bool isArrayCondMemObj(const CVar& var) const
{
const MemObj* mem = _pag->getObject(getPtrNodeID(var));
assert(mem && "memory object is null??");
return mem->isArray();
const BaseObjVar* obj = _pag->getBaseObject(getPtrNodeID(var));
assert(obj && "base object is null??");
return obj->isArray();
}
inline bool isFieldInsenCondMemObj(const CVar& var) const
{
const MemObj* mem = _pag->getBaseObj(getPtrNodeID(var));
return mem->isFieldInsensitive();
const BaseObjVar* baseObj = _pag->getBaseObject(getPtrNodeID(var));
return baseObj->isFieldInsensitive();
}
//@}
private:
4 changes: 2 additions & 2 deletions svf/include/Graphs/ConsG.h
Original file line number Diff line number Diff line change
@@ -323,8 +323,8 @@ class ConstraintGraph : public GenericGraph<ConstraintNode,ConstraintEdge>
}
inline bool isSingleFieldObj(NodeID id) const
{
const MemObj* mem = pag->getBaseObj(id);
return (mem->getMaxFieldOffsetLimit() == 1);
const BaseObjVar* baseObj = pag->getBaseObject(id);
return (baseObj->getMaxFieldOffsetLimit() == 1);
}
/// Get a field of a memory object
inline NodeID getGepObjVar(NodeID id, const APOffset& apOffset)
23 changes: 5 additions & 18 deletions svf/include/Graphs/IRGraph.h
Original file line number Diff line number Diff line change
@@ -65,10 +65,11 @@ class IRGraph : public GenericGraph<SVFVar, SVFStmt>
SymbolTableInfo* symInfo;

/// Add a node into the graph
inline NodeID addNode(SVFVar* node, NodeID i)
inline NodeID addNode(SVFVar* node)
{
addGNode(i,node);
return i;
assert(node && "cannot add a null node");
addGNode(node->getId(),node);
return node->getId();
}
/// Add an edge into the graph
bool addEdge(SVFVar* src, SVFVar* dst, SVFStmt* edge);
@@ -94,12 +95,6 @@ class IRGraph : public GenericGraph<SVFVar, SVFStmt>
inserted.first->second.emplace(edge);
}
}
/// get MemObj according to LLVM value
inline const MemObj* getMemObj(const SVFValue* val) const
{
return symInfo->getObj(symInfo->getObjSym(val));
}

public:
IRGraph(bool buildFromFile)
: fromFile(buildFromFile), nodeNumAfterPAGBuild(0), totalPTAPAGEdge(0)
@@ -174,22 +169,14 @@ class IRGraph : public GenericGraph<SVFVar, SVFStmt>
{
return symInfo->nullPtrSymID();
}
inline const MemObj* getBlackHoleObj() const
{
return symInfo->getBlkObj();
}
inline const MemObj* getConstantObj() const
{
return symInfo->getConstantObj();
}

inline u32_t getValueNodeNum() const
{
return symInfo->valSyms().size();
}
inline u32_t getObjectNodeNum() const
{
return symInfo->idToObjMap().size();
return symInfo->idToObjTypeInfoMap().size();
}
inline u32_t getNodeNumAfterPAGBuild() const
{
14 changes: 7 additions & 7 deletions svf/include/MemoryModel/PointerAnalysis.h
Original file line number Diff line number Diff line change
@@ -309,9 +309,9 @@ class PointerAnalysis

inline bool isArrayMemObj(NodeID id) const
{
const MemObj* mem = pag->getObject(id);
assert(mem && "memory object is null??");
return mem->isArray();
const BaseObjVar* obj = pag->getBaseObject(id);
assert(obj && "base object is null??");
return obj->isArray();
}
//@}

@@ -339,13 +339,13 @@ class PointerAnalysis
}
inline void setObjFieldInsensitive(NodeID id)
{
MemObj* mem = const_cast<MemObj*>(pag->getBaseObj(id));
mem->setFieldInsensitive();
BaseObjVar* baseObj = const_cast<BaseObjVar*>(pag->getBaseObject(id));
baseObj->setFieldInsensitive();
}
inline bool isFieldInsensitive(NodeID id) const
{
const MemObj* mem = pag->getBaseObj(id);
return mem->isFieldInsensitive();
const BaseObjVar* baseObj = pag->getBaseObject(id);
return baseObj->isFieldInsensitive();
}
///@}

12 changes: 0 additions & 12 deletions svf/include/SVFIR/SVFFileSystem.h
Original file line number Diff line number Diff line change
@@ -129,8 +129,6 @@ class SVFMetadataAsValue;

class SVFLoopAndDomInfo; // Part of SVFFunction

// Classes created upon buildSymbolTableInfo
class MemObj;

// Classes created upon ICFG construction
class ICFGNode;
@@ -422,7 +420,6 @@ class SVFIRWriter

cJSON* toJson(const AccessPath& ap);
cJSON* toJson(const SVFLoop* loop);
cJSON* toJson(const MemObj* memObj);
cJSON* toJson(const ObjTypeInfo* objTypeInfo); // Only owned by MemObj
cJSON* toJson(const SVFLoopAndDomInfo* ldInfo); // Only owned by SVFFunction
cJSON* toJson(const StInfo* stInfo);
@@ -528,7 +525,6 @@ class SVFIRWriter

// Other classes
cJSON* contentToJson(const SVFLoop* loop);
cJSON* contentToJson(const MemObj* memObj); // Owned by SymbolTable->objMap
cJSON* contentToJson(const StInfo* stInfo);
///@}

@@ -899,13 +895,8 @@ class SymbolTableInfoReader

private:
const cJSON* symTabFieldJson = nullptr;
ReaderIDToObjMap<MemObj> memObjMap;

public:
inline MemObj* getMemObjPtr(unsigned id) const
{
return memObjMap.getPtr(id);
}

template <typename MemObjCreator>
void createObjs(const cJSON* symTabJson, MemObjCreator memObjCreator)
@@ -915,7 +906,6 @@ class SymbolTableInfoReader

const cJSON* const allMemObj = symTabJson->child;
CHECK_JSON_KEY(allMemObj);
memObjMap.createObjs(allMemObj, memObjCreator);

symTabFieldJson = allMemObj->next;
}
@@ -1104,7 +1094,6 @@ class SVFIRReader

void readJson(const cJSON* obj, AccessPath& ap);
void readJson(const cJSON* obj, SVFLoop*& loop);
void readJson(const cJSON* obj, MemObj*& memObj);
void readJson(const cJSON* obj,
ObjTypeInfo*& objTypeInfo); // Only owned by MemObj
void readJson(const cJSON* obj,
@@ -1257,7 +1246,6 @@ class SVFIRReader
void fill(const cJSON*& fieldJson, TDForkPE* stmt);
void fill(const cJSON*& fieldJson, TDJoinPE* stmt);

void fill(const cJSON*& fieldJson, MemObj* memObj);
void fill(const cJSON*& fieldJson, StInfo* stInfo);
// ICFG
void virtFill(const cJSON*& fieldJson, ICFGNode* node);
Loading

0 comments on commit 89b8815

Please sign in to comment.