Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SVFInstruction to node maps in ICFG #1547

Merged
merged 17 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion svf-llvm/include/SVF-LLVM/DCHG.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

typedef std::vector<const Function*> FuncVector;

DCHNode(const DIType* diType, NodeID i = 0, GNodeK k = 0)
DCHNode(const DIType* diType, NodeID i = 0, GNodeK k = GNodeK::DCHNodeKd)

Check warning on line 76 in svf-llvm/include/SVF-LLVM/DCHG.h

View check run for this annotation

Codecov / codecov/patch

svf-llvm/include/SVF-LLVM/DCHG.h#L76

Added line #L76 was not covered by tests
: GenericNode<DCHNode, DCHEdge>(i, k), vtable(nullptr), flags(0)
{
this->diType = diType;
Expand Down
107 changes: 83 additions & 24 deletions svf-llvm/include/SVF-LLVM/ICFGBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "Graphs/ICFG.h"
#include "Util/WorkList.h"
#include "BasicTypes.h"
#include "LLVMModule.h"

namespace SVF
{
Expand All @@ -44,6 +45,12 @@ class ICFGBuilder

typedef std::vector<const Instruction*> InstVec;
typedef Set<const Instruction*> BBSet;
typedef Map<const Instruction*, CallICFGNode *> CSToCallNodeMapTy;
typedef Map<const Instruction*, RetICFGNode *> CSToRetNodeMapTy;
typedef Map<const Instruction*, IntraICFGNode *> InstToBlockNodeMapTy;
typedef Map<const Function*, FunEntryICFGNode *> FunToFunEntryNodeMapTy;
typedef Map<const Function*, FunExitICFGNode *> FunToFunExitNodeMapTy;


private:
ICFG* icfg;
Expand All @@ -55,9 +62,36 @@ class ICFGBuilder
{

}
void build(SVFModule* svfModule);
void build();

private:

LLVMModuleSet* llvmModuleSet() {
return LLVMModuleSet::getLLVMModuleSet();
}

CSToRetNodeMapTy& csToRetNodeMap() {
return llvmModuleSet()->CSToRetNodeMap;
}

CSToCallNodeMapTy& csToCallNodeMap() {
return llvmModuleSet()->CSToCallNodeMap;
}

InstToBlockNodeMapTy& instToBlockNodeMap() {
return llvmModuleSet()->InstToBlockNodeMap;
}

FunToFunEntryNodeMapTy& funToFunEntryNodeMap() {
return llvmModuleSet()->FunToFunEntryNodeMap;
}

FunToFunExitNodeMapTy& funToFunExitNodeMap() {
return llvmModuleSet()->FunToFunExitNodeMap;
}

private:

/// Create edges between ICFG nodes within a function
///@{
void processFunEntry(const Function* fun, WorkList& worklist);
Expand All @@ -71,41 +105,66 @@ class ICFGBuilder

void checkICFGNodesVisited(const Function* fun);

void connectGlobalToProgEntry(SVFModule* svfModule);
void connectGlobalToProgEntry();

/// Add/Get an inter block ICFGNode
InterICFGNode* addInterBlockICFGNode(const SVFInstruction* inst);

/// Add/Get a basic block ICFGNode
inline ICFGNode* addBlockICFGNode(const SVFInstruction* inst)
{
ICFGNode* node;
if(SVFUtil::isNonInstricCallSite(inst))
node = addInterBlockICFGNode(inst);
else
node = addIntraBlockICFGNode(inst);
const_cast<SVFBasicBlock*>(inst->getParent())->addICFGNode(node);
return node;
/// Create edges between ICFG nodes across functions
void addICFGInterEdges(const Instruction* cs, const Function* callee);

inline ICFGNode* getICFGNode(const Instruction* inst) {
return llvmModuleSet()->getICFGNode(inst);
}

/// Create edges between ICFG nodes across functions
void addICFGInterEdges(const SVFInstruction* cs, const SVFFunction* callee);
inline bool hasICFGNode(const Instruction* inst) {
return llvmModuleSet()->hasICFGNode(inst);
}

/// Add a call node
inline CallICFGNode* getCallICFGNode(const SVFInstruction* cs)
/// get a call node
inline CallICFGNode* getCallICFGNode(const Instruction* cs) {
return llvmModuleSet()->getCallICFGNode(cs);
}
/// get a return node
inline RetICFGNode* getRetICFGNode(const Instruction* cs) {
return llvmModuleSet()->getRetICFGNode(cs);
}
/// get a intra node
inline IntraICFGNode* getIntraICFGNode(const Instruction* inst) {
return llvmModuleSet()->getIntraICFGNode(inst);
}

/// get a function entry node
inline FunEntryICFGNode* getFunEntryICFGNode(const Function* fun)
{
return llvmModuleSet()->getFunEntryICFGNode(fun);
}
/// get a function exit node
inline FunExitICFGNode* getFunExitICFGNode(const Function* fun)
{
return icfg->getCallICFGNode(cs);
return llvmModuleSet()->getFunExitICFGNode(fun);
}
/// Add a return node
inline RetICFGNode* getRetICFGNode(const SVFInstruction* cs)

inline GlobalICFGNode* getGlobalICFGNode() const
{
return icfg->getRetICFGNode(cs);
return icfg->getGlobalICFGNode();
}

/// Add/Get an inter block ICFGNode
InterICFGNode* addInterBlockICFGNode(const Instruction* inst);

/// Add/Get a basic block ICFGNode
inline ICFGNode* addBlockICFGNode(const Instruction* inst);

/// Add and get IntraBlock ICFGNode
IntraICFGNode* addIntraBlockICFGNode(const SVFInstruction* inst)
IntraICFGNode* addIntraBlockICFGNode(const Instruction* inst);

FunEntryICFGNode* addFunEntryBlock(const Function* fun);

FunExitICFGNode* addFunExitBlock(const Function* fun);

inline void addGlobalICFGNode()
{
return icfg->addIntraICFGNode(inst);
icfg->globalBlockNode = new GlobalICFGNode(icfg->totalICFGNode++);
icfg->addICFGNode(icfg->globalBlockNode);
}

private:
Expand Down
83 changes: 83 additions & 0 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
class LLVMModuleSet
{
friend class SVFIRBuilder;
friend class ICFGBuilder;

public:

Expand All @@ -63,6 +64,12 @@
typedef Map<const Type*, StInfo*> Type2TypeInfoMap;
typedef Map<std::string, std::vector<std::string>> Fun2AnnoMap;

typedef Map<const Instruction*, CallICFGNode *> CSToCallNodeMapTy;
typedef Map<const Instruction*, RetICFGNode *> CSToRetNodeMapTy;
typedef Map<const Instruction*, IntraICFGNode *> InstToBlockNodeMapTy;
typedef Map<const Function*, FunEntryICFGNode *> FunToFunEntryNodeMapTy;
typedef Map<const Function*, FunExitICFGNode *> FunToFunExitNodeMapTy;

private:
static LLVMModuleSet* llvmModuleSet;
static bool preProcessed;
Expand Down Expand Up @@ -90,6 +97,12 @@
Type2TypeInfoMap Type2TypeInfo;
ObjTypeInference* typeInference;

CSToCallNodeMapTy CSToCallNodeMap; ///< map a callsite to its CallICFGNode
CSToRetNodeMapTy CSToRetNodeMap; ///< map a callsite to its RetICFGNode
InstToBlockNodeMapTy InstToBlockNodeMap; ///< map a basic block to its ICFGNode
FunToFunEntryNodeMapTy FunToFunEntryNodeMap; ///< map a function to its FunExitICFGNode
FunToFunExitNodeMapTy FunToFunExitNodeMap; ///< map a function to its FunEntryICFGNode

/// Constructor
LLVMModuleSet();

Expand Down Expand Up @@ -266,6 +279,33 @@
return nullptr;
}

ICFGNode* getICFGNode(const Instruction* inst);

bool hasICFGNode(const Instruction* inst);

/// get a call node
CallICFGNode* getCallICFGNode(const Instruction* cs);
/// get a return node
RetICFGNode* getRetICFGNode(const Instruction* cs);
/// get a intra node
IntraICFGNode* getIntraICFGNode(const Instruction* inst);

/// Add a function entry node
inline FunEntryICFGNode* getFunEntryICFGNode(const Function* fun)
{
FunEntryICFGNode* b = getFunEntryBlock(fun);
assert(b && "Function entry not created?");

Check warning on line 297 in svf-llvm/include/SVF-LLVM/LLVMModule.h

View check run for this annotation

Codecov / codecov/patch

svf-llvm/include/SVF-LLVM/LLVMModule.h#L296-L297

Added lines #L296 - L297 were not covered by tests
return b;
}
/// Add a function exit node
inline FunExitICFGNode* getFunExitICFGNode(const Function* fun)
{
FunExitICFGNode* b = getFunExitBlock(fun);
assert(b && "Function exit not created?");

Check warning on line 304 in svf-llvm/include/SVF-LLVM/LLVMModule.h

View check run for this annotation

Codecov / codecov/patch

svf-llvm/include/SVF-LLVM/LLVMModule.h#L303-L304

Added lines #L303 - L304 were not covered by tests
return b;
}


/// Global to rep
bool hasGlobalRep(const GlobalVariable* val) const
{
Expand Down Expand Up @@ -344,6 +384,49 @@
void prePassSchedule();
void buildSymbolTable() const;
void collectExtFunAnnotations(const Module* mod);

/// Get/Add a call node
inline CallICFGNode* getCallBlock(const Instruction* cs) {
CSToCallNodeMapTy::const_iterator it = CSToCallNodeMap.find(cs);
if (it == CSToCallNodeMap.end())
return nullptr;
return it->second;
}

/// Get/Add a return node
inline RetICFGNode* getRetBlock(const Instruction* cs)
{
CSToRetNodeMapTy::const_iterator it = CSToRetNodeMap.find(cs);
if (it == CSToRetNodeMap.end())
return nullptr;
return it->second;
}

inline IntraICFGNode* getIntraBlock(const Instruction* inst)
{
InstToBlockNodeMapTy::const_iterator it = InstToBlockNodeMap.find(inst);
if (it == InstToBlockNodeMap.end())
return nullptr;
return it->second;
}

/// Get/Add a function entry node
inline FunEntryICFGNode* getFunEntryBlock(const Function* fun)
{
FunToFunEntryNodeMapTy::const_iterator it = FunToFunEntryNodeMap.find(fun);
if (it == FunToFunEntryNodeMap.end())
return nullptr;
return it->second;
}

/// Get/Add a function exit node
inline FunExitICFGNode* getFunExitBlock(const Function* fun)
{
FunToFunExitNodeMapTy::const_iterator it = FunToFunExitNodeMap.find(fun);
if (it == FunToFunExitNodeMap.end())
return nullptr;
return it->second;
}
};

} // End namespace SVF
Expand Down
15 changes: 15 additions & 0 deletions svf-llvm/include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,21 @@ inline bool isHeapAllocExtCall(const Instruction *inst)
return isHeapAllocExtCallViaRet(inst) || isHeapAllocExtCallViaArg(inst);
}

/// Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls
bool isNonInstricCallSite(const Instruction* inst);

/// Get program entry function from module.
inline const Function* getProgEntryFunction(Module& module)
{
for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
{
const Function *fun = &(*it);
if (isProgEntryFunction(fun))
return (fun);
}
return nullptr;
}

} // End namespace LLVMUtil

} // End namespace SVF
Expand Down
30 changes: 18 additions & 12 deletions svf-llvm/include/SVF-LLVM/SVFIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
processCE(V);

// strip off the constant cast and return the value node
SVFValue* svfVal = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(V);
SVFValue* svfVal = llvmModuleSet()->getSVFValue(V);
return pag->getValueNode(svfVal);
}

/// GetObject - Return the object node (stack/global/heap/function) according to a LLVM Value
inline NodeID getObjectNode(const Value* V)
{
SVFValue* svfVal = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(V);
SVFValue* svfVal = llvmModuleSet()->getSVFValue(V);
return pag->getObjectNode(svfVal);
}

Expand Down Expand Up @@ -240,8 +240,8 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
/// Set current basic block in order to keep track of control flow information
inline void setCurrentLocation(const Value* val, const BasicBlock* bb)
{
curBB = (bb == nullptr? nullptr : LLVMModuleSet::getLLVMModuleSet()->getSVFBasicBlock(bb));
curVal = (val == nullptr ? nullptr: LLVMModuleSet::getLLVMModuleSet()->getSVFValue(val));
curBB = (bb == nullptr? nullptr : llvmModuleSet()->getSVFBasicBlock(bb));
curVal = (val == nullptr ? nullptr: llvmModuleSet()->getSVFValue(val));
}
inline void setCurrentLocation(const SVFValue* val, const SVFBasicBlock* bb)
{
Expand Down Expand Up @@ -270,9 +270,9 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
/// Add NullPtr PAGNode
inline NodeID addNullPtrNode()
{
LLVMContext& cxt = LLVMModuleSet::getLLVMModuleSet()->getContext();
LLVMContext& cxt = llvmModuleSet()->getContext();
ConstantPointerNull* constNull = ConstantPointerNull::get(PointerType::getUnqual(cxt));
NodeID nullPtr = pag->addValNode(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(constNull),pag->getNullPtr());
NodeID nullPtr = pag->addValNode(llvmModuleSet()->getSVFValue(constNull),pag->getNullPtr(), nullptr);
setCurrentLocation(constNull, nullptr);
addBlackHoleAddrEdge(pag->getBlkPtr());
return nullPtr;
Expand Down Expand Up @@ -305,7 +305,7 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
AddrStmt* edge = addAddrEdge(src, dst);
if (inst.getArraySize())
{
SVFValue* arrSz = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(inst.getArraySize());
SVFValue* arrSz = llvmModuleSet()->getSVFValue(inst.getArraySize());
edge->addArrSize(arrSz);
}
return edge;
Expand All @@ -332,7 +332,7 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
if (cs->arg_size() > 0)
{
const llvm::Value* val = cs->getArgOperand(0);
SVFValue* svfval = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(val);
SVFValue* svfval = llvmModuleSet()->getSVFValue(val);
edge->addArrSize(svfval);
}
}
Expand All @@ -342,16 +342,16 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
{
if (cs->arg_size() > 1)
{
edge->addArrSize(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(cs->getArgOperand(0)));
edge->addArrSize(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(cs->getArgOperand(1)));
edge->addArrSize(llvmModuleSet()->getSVFValue(cs->getArgOperand(0)));
edge->addArrSize(llvmModuleSet()->getSVFValue(cs->getArgOperand(1)));
}
}
else
{
if (cs->arg_size() > 0)
{
const llvm::Value* val = cs->getArgOperand(0);
SVFValue* svfval = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(val);
SVFValue* svfval = llvmModuleSet()->getSVFValue(val);
edge->addArrSize(svfval);
}
}
Expand Down Expand Up @@ -452,7 +452,8 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
{
ICFGNode* node;
if (const SVFInstruction* inst = SVFUtil::dyn_cast<SVFInstruction>(curVal))
node = pag->getICFG()->getICFGNode(inst);
node = llvmModuleSet()->getICFGNode(
SVFUtil::cast<Instruction>(llvmModuleSet()->getLLVMValue(inst)));
else
node = nullptr;
if (StoreStmt* edge = pag->addStoreStmt(src, dst, node))
Expand Down Expand Up @@ -503,6 +504,11 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
//@}

AccessPath getAccessPathFromBaseNode(NodeID nodeId);

private:
LLVMModuleSet* llvmModuleSet() {
return LLVMModuleSet::getLLVMModuleSet();
}
};

} // End namespace SVF
Expand Down
Loading
Loading