Skip to content

Commit

Permalink
Remove CallSite (#1535)
Browse files Browse the repository at this point in the history
* Remove CallSite : stage 1

* Remove CallSite : stage 2

* Remove CallSite stage 3

* remove SVFInstruction in CallICFGEdge

* init nullptr for retPE

* simplifying SVFInstruction class
  • Loading branch information
yuleisui authored Aug 27, 2024
1 parent 0d3a996 commit 5f6e310
Show file tree
Hide file tree
Showing 43 changed files with 394 additions and 598 deletions.
14 changes: 7 additions & 7 deletions svf-llvm/include/SVF-LLVM/DCHG.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ class DCHGraph : public CommonCHGraph, public GenericGraph<DCHNode, DCHEdge>

void print(void);

virtual bool csHasVFnsBasedonCHA(CallSite cs) override
virtual bool csHasVFnsBasedonCHA(const CallICFGNode* cs) override
{
return csHasVtblsBasedonCHA(cs);
}

virtual const VFunSet &getCSVFsBasedonCHA(CallSite cs) override;
virtual const VFunSet &getCSVFsBasedonCHA(const CallICFGNode* cs) override;

virtual bool csHasVtblsBasedonCHA(CallBase* cs)
{
Expand All @@ -268,14 +268,14 @@ class DCHGraph : public CommonCHGraph, public GenericGraph<DCHNode, DCHEdge>
return getNode(type)->getVTable() != nullptr;
}

virtual bool csHasVtblsBasedonCHA(CallSite cs) override
virtual bool csHasVtblsBasedonCHA(const CallICFGNode* cs) override
{
assert(false && "not supported!");
abort();
}

virtual const VTableSet &getCSVtblsBasedonCHA(CallSite cs) override;
virtual void getVFnsFromVtbls(CallSite cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override;
virtual const VTableSet &getCSVtblsBasedonCHA(const CallICFGNode* cs) override;
virtual void getVFnsFromVtbls(const SVFCallInst* cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override;

/// Returns true if a is a transitive base of b. firstField determines
/// whether to consider first-field edges.
Expand Down Expand Up @@ -373,7 +373,7 @@ class DCHGraph : public CommonCHGraph, public GenericGraph<DCHNode, DCHEdge>
/// Maps types to a set with their vtable and all their children's.
Map<const DIType*, VTableSet> vtblCHAMap;
/// Maps callsites to a set of potential virtual functions based on CHA.
Map<CallSite, VFunSet> csCHAMap;
Map<const CallICFGNode*, VFunSet> csCHAMap;
/// Maps types to their canonical type (many-to-one).
Map<const DIType*, const DIType*> canonicalTypeMap;
/// Set of all possible canonical types (i.e. values of canonicalTypeMap).
Expand Down Expand Up @@ -414,7 +414,7 @@ class DCHGraph : public CommonCHGraph, public GenericGraph<DCHNode, DCHEdge>
/// Retrieves the metadata associated with a *virtual* callsite.
const DIType* getCSStaticType(CallBase* cs) const;

const DIType *getCSStaticType(CallSite cs) const
const DIType *getCSStaticType(const CallICFGNode* cs) const
{
assert(false && "not supported!");
abort();
Expand Down
8 changes: 0 additions & 8 deletions svf-llvm/include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,6 @@ inline static DataLayout* getDataLayout(Module* mod)
return dl;
}

/// Get the next instructions following control flow
void getNextInsts(const Instruction* curInst,
std::vector<const SVFInstruction*>& instList);

/// Get the previous instructions following control flow
void getPrevInsts(const Instruction* curInst,
std::vector<const SVFInstruction*>& instList);

/// Get the next instructions following control flow
void getNextInsts(const Instruction* curInst,
std::vector<const Instruction*>& instList);
Expand Down
8 changes: 4 additions & 4 deletions svf-llvm/lib/CHGBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===----- CHGBuiler.cpp -- Class hierarchy graph builder ---------------------------//
//===----- CHGBuilder.cpp -- Class hierarchy graph builder ---------------------------//
//
// SVF: Static Value-Flow Analysis
//
Expand All @@ -21,7 +21,7 @@
//===----------------------------------------------------------------------===//

/*
* CHGBuiler.cpp
* CHGBuilder.cpp
*
* Created on: Jun 4, 2021
* Author: Yulei Sui
Expand Down Expand Up @@ -679,10 +679,10 @@ void CHGBuilder::buildCSToCHAVtblsAndVfnsMap()
}
if (vtbls.size() > 0)
{
CallSite cs(LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(callInst));
const SVFInstruction* cs = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(callInst);
chg->csToCHAVtblsMap[cs] = vtbls;
VFunSet virtualFunctions;
chg->getVFnsFromVtbls(cs, vtbls, virtualFunctions);
chg->getVFnsFromVtbls(SVFUtil::cast<SVFCallInst>(cs), vtbls, virtualFunctions);
if (virtualFunctions.size() > 0)
chg->csToCHAVFnsMap[cs] = virtualFunctions;
}
Expand Down
15 changes: 8 additions & 7 deletions svf-llvm/lib/DCHG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ void DCHGraph::buildCHG(bool extend)
}
}

const VFunSet &DCHGraph::getCSVFsBasedonCHA(CallSite cs)
const VFunSet &DCHGraph::getCSVFsBasedonCHA(const CallICFGNode* cs)
{
if (csCHAMap.find(cs) != csCHAMap.end())
{
Expand All @@ -553,15 +553,15 @@ const VFunSet &DCHGraph::getCSVFsBasedonCHA(CallSite cs)

VFunSet vfns;
const VTableSet &vtbls = getCSVtblsBasedonCHA(cs);
getVFnsFromVtbls(cs, vtbls, vfns);
getVFnsFromVtbls(SVFUtil::cast<SVFCallInst>(cs->getCallSite()), vtbls, vfns);

// Cache.
csCHAMap.insert({cs, vfns});
// Return cached object, not the stack object.
return csCHAMap[cs];
}

const VTableSet &DCHGraph::getCSVtblsBasedonCHA(CallSite cs)
const VTableSet &DCHGraph::getCSVtblsBasedonCHA(const CallICFGNode* cs)
{
const DIType *type = getCanonicalType(getCSStaticType(cs));
// Check if we've already computed.
Expand Down Expand Up @@ -589,10 +589,11 @@ const VTableSet &DCHGraph::getCSVtblsBasedonCHA(CallSite cs)
return vtblCHAMap[type];
}

void DCHGraph::getVFnsFromVtbls(CallSite cs, const VTableSet &vtbls, VFunSet &virtualFunctions)
void DCHGraph::getVFnsFromVtbls(const SVFCallInst* callsite, const VTableSet &vtbls, VFunSet &virtualFunctions)
{
size_t idx = cs.getFunIdxInVtable();
std::string funName = cs.getFunNameOfVirtualCall();
const SVFVirtualCallInst* cs = SVFUtil::cast<SVFVirtualCallInst>(callsite);
size_t idx = cs->getFunIdxInVtable();
std::string funName = cs->getFunNameOfVirtualCall();
for (const SVFGlobalValue *vtbl : vtbls)
{
assert(vtblToTypeMap.find(vtbl) != vtblToTypeMap.end() && "floating vtbl");
Expand All @@ -610,7 +611,7 @@ void DCHGraph::getVFnsFromVtbls(CallSite cs, const VTableSet &vtbls, VFunSet &vi

const Function* callee = vfnV[idx];
// Practically a copy of that in lib/MemoryModel/CHA.cpp
if (cs.arg_size() == callee->arg_size() || (cs.isVarArg() && callee->isVarArg()))
if (cs->arg_size() == callee->arg_size() || (cs->isVarArg() && callee->isVarArg()))
{
cppUtil::DemangledName dname = cppUtil::demangle(callee->getName().str());
std::string calleeName = dname.funcName;
Expand Down
4 changes: 2 additions & 2 deletions svf-llvm/lib/ICFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ void ICFGBuilder::addICFGInterEdges(const SVFInstruction* cs, const SVFFunction*
{
FunEntryICFGNode* calleeEntryNode = icfg->getFunEntryICFGNode(callee);
FunExitICFGNode* calleeExitNode = icfg->getFunExitICFGNode(callee);
icfg->addCallEdge(callICFGNode, calleeEntryNode, cs);
icfg->addRetEdge(calleeExitNode, retBlockNode, cs);
icfg->addCallEdge(callICFGNode, calleeEntryNode);
icfg->addRetEdge(calleeExitNode, retBlockNode);
}
}
/// indirect call (don't know callee)
Expand Down
2 changes: 0 additions & 2 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,6 @@ void LLVMModuleSet::initSVFBasicBlock(const Function* func)
svfcall->addArgument(svfval);
}
}
LLVMUtil::getNextInsts(inst, getSVFInstruction(inst)->getSuccInstructions());
LLVMUtil::getPrevInsts(inst, getSVFInstruction(inst)->getPredInstructions());
}
}
// For no return functions, we set the last block as exit BB
Expand Down
58 changes: 0 additions & 58 deletions svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,64 +297,6 @@ const Value* LLVMUtil::stripAllCasts(const Value* val)
return nullptr;
}

/// Get the next instructions following control flow
void LLVMUtil::getNextInsts(const Instruction* curInst, std::vector<const SVFInstruction*>& instList)
{
if (!curInst->isTerminator())
{
const Instruction* nextInst = curInst->getNextNode();
const SVFInstruction* svfNextInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(nextInst);
if (LLVMUtil::isIntrinsicInst(nextInst))
getNextInsts(nextInst, instList);
else
instList.push_back(svfNextInst);
}
else
{
const BasicBlock* BB = curInst->getParent();
// Visit all successors of BB in the CFG
for (succ_const_iterator it = succ_begin(BB), ie = succ_end(BB); it != ie; ++it)
{
const Instruction* nextInst = &((*it)->front());
const SVFInstruction* svfNextInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(nextInst);
if (LLVMUtil::isIntrinsicInst(nextInst))
getNextInsts(nextInst, instList);
else
instList.push_back(svfNextInst);
}
}
}


/// Get the previous instructions following control flow
void LLVMUtil::getPrevInsts(const Instruction* curInst, std::vector<const SVFInstruction*>& instList)
{

if (curInst != &(curInst->getParent()->front()))
{
const Instruction* prevInst = curInst->getPrevNode();
const SVFInstruction* svfPrevInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(prevInst);
if (LLVMUtil::isIntrinsicInst(prevInst))
getPrevInsts(prevInst, instList);
else
instList.push_back(svfPrevInst);
}
else
{
const BasicBlock* BB = curInst->getParent();
// Visit all successors of BB in the CFG
for (const_pred_iterator it = pred_begin(BB), ie = pred_end(BB); it != ie; ++it)
{
const Instruction* prevInst = &((*it)->back());
const SVFInstruction* svfPrevInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(prevInst);
if (LLVMUtil::isIntrinsicInst(prevInst))
getPrevInsts(prevInst, instList);
else
instList.push_back(svfPrevInst);
}
}
}

/*
* Get the first dominated cast instruction for heap allocations since they typically come from void* (i8*)
* for example, %4 = call align 16 i8* @malloc(i64 10); %5 = bitcast i8* %4 to i32*
Expand Down
4 changes: 2 additions & 2 deletions svf-llvm/lib/SVFIRExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle
const SVFCallInst* svfCall = SVFUtil::cast<SVFCallInst>(svfInst);
const CallICFGNode *callICFGNode = pag->getICFG()->getCallICFGNode(svfInst);

if (isHeapAllocExtCallViaRet(svfCall))
if (isHeapAllocExtCallViaRet(callICFGNode))
{
NodeID val = pag->getValueNode(svfInst);
NodeID obj = pag->getObjectNode(svfInst);
addAddrWithHeapSz(obj, val, cs);
}
else if (isHeapAllocExtCallViaArg(svfCall))
else if (isHeapAllocExtCallViaArg(callICFGNode))
{
u32_t arg_pos = getHeapAllocHoldingArgPosition(svfCallee);
const SVFValue* arg = svfCall->getArgOperand(arg_pos);
Expand Down
6 changes: 3 additions & 3 deletions svf/include/CFL/CFLAlias.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CFLAlias : public CFLBase
{

public:
typedef OrderedMap<CallSite, NodeID> CallSite2DummyValPN;
typedef OrderedMap<const CallICFGNode*, NodeID> CallSite2DummyValPN;

CFLAlias(SVFIR* ir) : CFLBase(ir, PointerAnalysis::CFLFICI_WPA)
{
Expand Down Expand Up @@ -143,9 +143,9 @@ class CFLAlias : public CFLBase
virtual void onTheFlyCallGraphSolve(const CallSiteToFunPtrMap& callsites, CallEdgeMap& newEdges);

/// Connect formal and actual parameters for indirect callsites
void connectCaller2CalleeParams(CallSite cs, const SVFFunction* F);
void connectCaller2CalleeParams(const CallICFGNode* cs, const SVFFunction* F);

void heapAllocatorViaIndCall(CallSite cs);
void heapAllocatorViaIndCall(const CallICFGNode* cs);

private:
CallSite2DummyValPN callsite2DummyValPN; ///< Map an instruction to a dummy obj which created at an indirect callsite, which invokes a heap allocator
Expand Down
44 changes: 13 additions & 31 deletions svf/include/Graphs/CHG.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class CommonCHGraph
DI
};

virtual bool csHasVFnsBasedonCHA(CallSite cs) = 0;
virtual const VFunSet &getCSVFsBasedonCHA(CallSite cs) = 0;
virtual bool csHasVtblsBasedonCHA(CallSite cs) = 0;
virtual const VTableSet &getCSVtblsBasedonCHA(CallSite cs) = 0;
virtual void getVFnsFromVtbls(CallSite cs, const VTableSet& vtbls,
virtual bool csHasVFnsBasedonCHA(const CallICFGNode* cs) = 0;
virtual const VFunSet &getCSVFsBasedonCHA(const CallICFGNode* cs) = 0;
virtual bool csHasVtblsBasedonCHA(const CallICFGNode* cs) = 0;
virtual const VTableSet &getCSVtblsBasedonCHA(const CallICFGNode* cs) = 0;
virtual void getVFnsFromVtbls(const SVFCallInst* cs, const VTableSet& vtbls,
VFunSet& virtualFunctions) = 0;

CHGKind getKind(void) const
Expand Down Expand Up @@ -222,9 +222,9 @@ class CHGraph: public CommonCHGraph, public GenericCHGraphTy
typedef Set<const CHNode*> CHNodeSetTy;
typedef FIFOWorkList<const CHNode*> WorkList;
typedef Map<std::string, CHNodeSetTy> NameToCHNodesMap;
typedef Map<CallSite, CHNodeSetTy> CallSiteToCHNodesMap;
typedef Map<CallSite, VTableSet> CallSiteToVTableSetMap;
typedef Map<CallSite, VFunSet> CallSiteToVFunSetMap;
typedef Map<const SVFInstruction*, CHNodeSetTy> CallSiteToCHNodesMap;
typedef Map<const SVFInstruction*, VTableSet> CallSiteToVTableSetMap;
typedef Map<const SVFInstruction*, VFunSet> CallSiteToVFunSetMap;

typedef enum
{
Expand All @@ -242,7 +242,7 @@ class CHGraph: public CommonCHGraph, public GenericCHGraphTy
const std::string baseClassName,
CHEdge::CHEDGETYPE edgeType);
CHNode *getNode(const std::string name) const;
void getVFnsFromVtbls(CallSite cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override;
void getVFnsFromVtbls(const SVFCallInst* cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override;
void dump(const std::string& filename);
void view();
void printCH();
Expand Down Expand Up @@ -286,28 +286,10 @@ class CHGraph: public CommonCHGraph, public GenericCHGraphTy
return templateNameToInstancesMap[className];
}

inline bool csHasVtblsBasedonCHA(CallSite cs) override
{
CallSiteToVTableSetMap::const_iterator it = csToCHAVtblsMap.find(cs);
return it != csToCHAVtblsMap.end();
}
inline bool csHasVFnsBasedonCHA(CallSite cs) override
{
CallSiteToVFunSetMap::const_iterator it = csToCHAVFnsMap.find(cs);
return it != csToCHAVFnsMap.end();
}
inline const VTableSet &getCSVtblsBasedonCHA(CallSite cs) override
{
CallSiteToVTableSetMap::const_iterator it = csToCHAVtblsMap.find(cs);
assert(it != csToCHAVtblsMap.end() && "cs does not have vtabls based on CHA.");
return it->second;
}
inline const VFunSet &getCSVFsBasedonCHA(CallSite cs) override
{
CallSiteToVFunSetMap::const_iterator it = csToCHAVFnsMap.find(cs);
assert(it != csToCHAVFnsMap.end() && "cs does not have vfns based on CHA.");
return it->second;
}
bool csHasVtblsBasedonCHA(const CallICFGNode* cs) override;
bool csHasVFnsBasedonCHA(const CallICFGNode* cs) override;
const VTableSet &getCSVtblsBasedonCHA(const CallICFGNode* cs) override;
const VFunSet &getCSVFsBasedonCHA(const CallICFGNode* cs) override;

static inline bool classof(const CommonCHGraph *chg)
{
Expand Down
4 changes: 2 additions & 2 deletions svf/include/Graphs/ICFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ class ICFG : public GenericICFGTy
//@{
ICFGEdge* addIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode);
ICFGEdge* addConditionalIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode, const SVFValue* condition, s32_t branchCondVal);
ICFGEdge* addCallEdge(ICFGNode* srcNode, ICFGNode* dstNode, const SVFInstruction* cs);
ICFGEdge* addRetEdge(ICFGNode* srcNode, ICFGNode* dstNode, const SVFInstruction* cs);
ICFGEdge* addCallEdge(ICFGNode* srcNode, ICFGNode* dstNode);
ICFGEdge* addRetEdge(ICFGNode* srcNode, ICFGNode* dstNode);
//@}
/// Remove a ICFG edge
inline void removeICFGEdge(ICFGEdge* edge)
Expand Down
20 changes: 4 additions & 16 deletions svf/include/Graphs/ICFGEdge.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,14 @@ class CallCFGEdge : public ICFGEdge
friend class SVFIRReader;

private:
const SVFInstruction* cs;
std::vector<const CallPE*> callPEs;

public:
/// Constructor
CallCFGEdge(ICFGNode* s, ICFGNode* d, const SVFInstruction* c)
: ICFGEdge(s, d, CallCF), cs(c)
CallCFGEdge(ICFGNode* s, ICFGNode* d)
: ICFGEdge(s, d, CallCF)
{
}
/// Return callsite ID
inline const SVFInstruction* getCallSite() const
{
return cs;
}
/// Add call parameter edge to this CallCFGEdge
inline void addCallPE(const CallPE* callPE)
{
Expand Down Expand Up @@ -230,19 +224,13 @@ class RetCFGEdge : public ICFGEdge
friend class SVFIRReader;

private:
const SVFInstruction* cs;
const RetPE* retPE;

public:
/// Constructor
RetCFGEdge(ICFGNode* s, ICFGNode* d, const SVFInstruction* c)
: ICFGEdge(s, d, RetCF), cs(c), retPE(nullptr)
{
}
/// Return callsite ID
inline const SVFInstruction* getCallSite() const
RetCFGEdge(ICFGNode* s, ICFGNode* d)
: ICFGEdge(s, d, RetCF), retPE(nullptr)
{
return cs;
}
/// Add call parameter edge to this CallCFGEdge
inline void addRetPE(const RetPE* ret)
Expand Down
Loading

0 comments on commit 5f6e310

Please sign in to comment.