Skip to content

Commit

Permalink
Merge pull request #1534 from yuleisui/master
Browse files Browse the repository at this point in the history
Refactoring SVFInstruction: stage 7
  • Loading branch information
yuleisui authored Aug 26, 2024
2 parents e8df0a9 + 8b216ef commit 86ac4b8
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 158 deletions.
2 changes: 1 addition & 1 deletion svf-llvm/lib/CHGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ void CHGBuilder::buildCSToCHAVtblsAndVfnsMap()
}
if (vtbls.size() > 0)
{
CallSite cs = SVFUtil::getSVFCallSite(LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(callInst));
CallSite cs(LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(callInst));
chg->csToCHAVtblsMap[cs] = vtbls;
VFunSet virtualFunctions;
chg->getVFnsFromVtbls(cs, vtbls, virtualFunctions);
Expand Down
10 changes: 5 additions & 5 deletions svf-llvm/lib/SVFIRExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle
{
const SVFInstruction* svfInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(cs);
const SVFCallInst* svfCall = SVFUtil::cast<SVFCallInst>(svfInst);
const CallICFGNode *callICFGNode = pag->getICFG()->getCallICFGNode(svfInst);

if (isHeapAllocExtCallViaRet(svfCall))
{
Expand Down Expand Up @@ -253,12 +254,12 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle
}
}

if (isThreadForkCall(svfInst))
if (isThreadForkCall(callICFGNode))
{
if (const SVFFunction* forkedFun = SVFUtil::dyn_cast<SVFFunction>(getForkedFun(svfInst)))
if (const SVFFunction* forkedFun = SVFUtil::dyn_cast<SVFFunction>(getForkedFun(callICFGNode)))
{
forkedFun = forkedFun->getDefFunForMultipleModule();
const SVFValue* actualParm = getActualParmAtForkSite(svfInst);
const SVFValue* actualParm = getActualParmAtForkSite(callICFGNode);
/// pthread_create has 1 arg.
/// apr_thread_create has 2 arg.
assert((forkedFun->arg_size() <= 2) && "Size of formal parameter of start routine should be one");
Expand All @@ -268,9 +269,8 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle
/// Connect actual parameter to formal parameter of the start routine
if (actualParm->getType()->isPointerTy() && formalParm->getType()->isPointerTy())
{
CallICFGNode *icfgNode = pag->getICFG()->getCallICFGNode(svfInst);
FunEntryICFGNode *entry = pag->getICFG()->getFunEntryICFGNode(forkedFun);
addThreadForkEdge(pag->getValueNode(actualParm), pag->getValueNode(formalParm), icfgNode, entry);
addThreadForkEdge(pag->getValueNode(actualParm), pag->getValueNode(formalParm), callICFGNode, entry);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ const Type* SymbolTableBuilder::inferTypeOfHeapObjOrStaticObj(const Instruction
else if(SVFUtil::isHeapAllocExtCallViaArg(svfinst))
{
const CallBase* cs = LLVMUtil::getLLVMCallSite(inst);
int arg_pos = SVFUtil::getHeapAllocHoldingArgPosition(SVFUtil::getSVFCallSite(svfinst));
int arg_pos = SVFUtil::getHeapAllocHoldingArgPosition(getCallee(svfinst));
const Value* arg = cs->getArgOperand(arg_pos);
originalPType = SVFUtil::dyn_cast<PointerType>(arg->getType());
inferedType = inferObjType(startValue = arg);
Expand Down
52 changes: 15 additions & 37 deletions svf/include/Util/SVFUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,8 @@ void dumpPointsToList(const PointsToList& ptl);
/// Return true if it is an llvm intrinsic instruction
bool isIntrinsicInst(const SVFInstruction* inst);
bool isIntrinsicInst(const ICFGNode* inst);

//@}

/// Whether an instruction is a call or invoke instruction
inline bool isCallSite(const SVFInstruction* inst)
{
return SVFUtil::isa<SVFCallInst>(inst);
}
/// Whether an instruction is a call or invoke instruction
inline bool isCallSite(const SVFValue* val)
{
Expand Down Expand Up @@ -207,31 +201,15 @@ inline bool isNonInstricCallSite(const ICFGNode* inst)
}



/// Return callsite given an instruction
CallSite getSVFCallSite(const ICFGNode* inst);

/// Return callsite given an instruction
inline CallSite getSVFCallSite(const SVFInstruction* inst)
{
assert(isCallSite(inst) && "not a callsite?");
CallSite cs(inst);
return cs;
}

/// Match arguments for callsite at caller and callee
/// if the arg size does not match then we do not need to connect this parameter
/// unless the callee is a variadic function (the first parameter of variadic function is its parameter number)
bool matchArgs(const CallSite cs, const SVFFunction* callee);
bool matchArgs(const CallICFGNode* cs, const SVFFunction* callee);

/// Return LLVM callsite given a value
inline CallSite getSVFCallSite(const SVFValue* value)
{
assert(isCallSite(value) && "not a callsite?");
const SVFCallInst* svfInst = SVFUtil::cast<SVFCallInst>(value);
CallSite cs(svfInst);
return cs;
}

/// Split into two substrings around the first occurrence of a separator string.
inline std::vector<std::string> split(const std::string& s, char separator)
Expand Down Expand Up @@ -423,7 +401,7 @@ inline bool isArgOfUncalledFunction(const SVFValue* svfval)

/// Return thread fork function
//@{
inline const SVFValue* getForkedFun(const SVFInstruction *inst)
inline const SVFValue* getForkedFun(const ICFGNode *inst)
{
return ThreadAPI::getThreadAPI()->getForkedFun(inst);
}
Expand Down Expand Up @@ -488,57 +466,57 @@ inline bool isReallocExtCall(const CallSite cs)

/// Return true if this is a thread creation call
///@{
inline bool isThreadForkCall(const SVFInstruction *inst)
inline bool isThreadForkCall(const ICFGNode *inst)
{
return ThreadAPI::getThreadAPI()->isTDFork(inst);
}
//@}

/// Return true if this is a thread join call
///@{
inline bool isThreadJoinCall(const CallSite cs)
inline bool isThreadJoinCall(const ICFGNode* cs)
{
return ThreadAPI::getThreadAPI()->isTDJoin(cs.getInstruction());
return ThreadAPI::getThreadAPI()->isTDJoin(cs);
}
//@}

/// Return true if this is a thread exit call
///@{
inline bool isThreadExitCall(const CallSite cs)
inline bool isThreadExitCall(const ICFGNode* cs)
{
return ThreadAPI::getThreadAPI()->isTDExit(cs.getInstruction());
return ThreadAPI::getThreadAPI()->isTDExit(cs);
}
//@}

/// Return true if this is a lock acquire call
///@{
inline bool isLockAquireCall(const CallSite cs)
inline bool isLockAquireCall(const ICFGNode* cs)
{
return ThreadAPI::getThreadAPI()->isTDAcquire(cs.getInstruction());
return ThreadAPI::getThreadAPI()->isTDAcquire(cs);
}
//@}

/// Return true if this is a lock acquire call
///@{
inline bool isLockReleaseCall(const CallSite cs)
inline bool isLockReleaseCall(const ICFGNode* cs)
{
return ThreadAPI::getThreadAPI()->isTDRelease(cs.getInstruction());
return ThreadAPI::getThreadAPI()->isTDRelease(cs);
}
//@}

/// Return true if this is a barrier wait call
//@{
inline bool isBarrierWaitCall(const CallSite cs)
inline bool isBarrierWaitCall(const ICFGNode* cs)
{
return ThreadAPI::getThreadAPI()->isTDBarWait(cs.getInstruction());
return ThreadAPI::getThreadAPI()->isTDBarWait(cs);
}
//@}

/// Return sole argument of the thread routine
//@{
inline const SVFValue* getActualParmAtForkSite(const CallSite cs)
inline const SVFValue* getActualParmAtForkSite(const ICFGNode* cs)
{
return ThreadAPI::getThreadAPI()->getActualParmAtForkSite(cs.getInstruction());
return ThreadAPI::getThreadAPI()->getActualParmAtForkSite(cs);
}
//@}

Expand Down
56 changes: 1 addition & 55 deletions svf/include/Util/ThreadAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace SVF

class SVFModule;
class ICFGNode;
class CallICFGNode;

/*
* ThreadAPI class contains interfaces for pthread programs
Expand Down Expand Up @@ -121,8 +122,6 @@ class ThreadAPI
/// Return the callee/callsite/func
//@{
const SVFFunction* getCallee(const ICFGNode *inst) const;
const SVFFunction* getCallee(const SVFInstruction *inst) const;
const CallSite getSVFCallSite(const SVFInstruction *inst) const;
const CallSite getSVFCallSite(const ICFGNode *inst) const;
//@}

Expand All @@ -132,10 +131,6 @@ class ThreadAPI
{
return getType(getCallee(inst)) == TD_FORK;
}
inline bool isTDFork(const SVFInstruction* cs) const
{
return getType(getCallee(cs)) == TD_FORK;
}
//@}

/// Return arguments/attributes of pthread_create / hare_parallel_for
Expand All @@ -148,13 +143,6 @@ class ThreadAPI
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(0);
}
inline const SVFValue* getForkedThread(const SVFInstruction* inst) const
{
assert(isTDFork(inst) && "not a thread fork function!");
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(0);
}

/// Return the third argument of the call,
/// Note that, it could be function type or a void* pointer
inline const SVFValue* getForkedFun(const ICFGNode *inst) const
Expand All @@ -163,12 +151,6 @@ class ThreadAPI
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(2);
}
inline const SVFValue* getForkedFun(const SVFInstruction* inst) const
{
assert(isTDFork(inst) && "not a thread fork function!");
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(2);
}

/// Return the forth argument of the call,
/// Note that, it is the sole argument of start routine ( a void* pointer )
Expand All @@ -178,12 +160,6 @@ class ThreadAPI
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(3);
}
inline const SVFValue* getActualParmAtForkSite(const SVFInstruction* inst) const
{
assert(isTDFork(inst) && "not a thread fork function!");
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(3);
}
//@}

/// Return true if this call wait for a worker thread
Expand All @@ -192,10 +168,6 @@ class ThreadAPI
{
return getType(getCallee(inst)) == TD_JOIN;
}
inline bool isTDJoin(const SVFInstruction* inst) const
{
return getType(getCallee(inst)) == TD_JOIN;
}
//@}

/// Return arguments/attributes of pthread_join
Expand All @@ -211,12 +183,6 @@ class ThreadAPI
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(1);
}
inline const SVFValue* getRetParmAtJoinedSite(const SVFInstruction* inst) const
{
assert(isTDJoin(inst) && "not a thread join function!");
CallSite cs = getSVFCallSite(inst);
return cs.getArgument(1);
}
//@}


Expand All @@ -226,11 +192,6 @@ class ThreadAPI
{
return getType(getCallee(inst)) == TD_EXIT;
}

inline bool isTDExit(const SVFInstruction* inst) const
{
return getType(getCallee(inst)) == TD_EXIT;
}
//@}

/// Return true if this call acquire a lock
Expand All @@ -239,11 +200,6 @@ class ThreadAPI
{
return getType(getCallee(inst)) == TD_ACQUIRE;
}

inline bool isTDAcquire(const SVFInstruction* inst) const
{
return getType(getCallee(inst)) == TD_ACQUIRE;
}
//@}

/// Return true if this call release a lock
Expand All @@ -252,11 +208,6 @@ class ThreadAPI
{
return getType(getCallee(inst)) == TD_RELEASE;
}

inline bool isTDRelease(const SVFInstruction* inst) const
{
return getType(getCallee(inst)) == TD_RELEASE;
}
//@}

/// Return lock value
Expand All @@ -271,11 +222,6 @@ class ThreadAPI
{
return getType(getCallee(inst)) == TD_BAR_WAIT;
}

inline bool isTDBarWait(const SVFInstruction* inst) const
{
return getType(getCallee(inst)) == TD_BAR_WAIT;
}
//@}

void performAPIStat(SVFModule* m);
Expand Down
8 changes: 4 additions & 4 deletions svf/lib/AE/Svfexe/AEDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void BufOverflowDetector::detectExtAPI(AbstractState& as,
SVFIR* svfir = PAG::getPAG();
const SVFFunction *fun = SVFUtil::getCallee(call->getCallSite());
assert(fun && "SVFFunction* is nullptr");
CallSite cs = SVFUtil::getSVFCallSite(call->getCallSite());
CallSite cs = SVFUtil::getSVFCallSite(call);

AbstractInterpretation::ExtAPIType extType = AbstractInterpretation::UNCLASSIFIED;

Expand Down Expand Up @@ -322,7 +322,7 @@ void BufOverflowDetector::updateGepObjOffsetFromBase(SVF::AddressValue gepAddrs,
*/
bool BufOverflowDetector::detectStrcpy(AbstractState& as, const CallICFGNode *call)
{
CallSite cs = SVFUtil::getSVFCallSite(call->getCallSite());
CallSite cs = SVFUtil::getSVFCallSite(call);
const SVFValue* arg0Val = cs.getArgument(0);
const SVFValue* arg1Val = cs.getArgument(1);
IntervalValue strLen = AbstractInterpretation::getAEInstance().getStrlen(as, arg1Val);
Expand All @@ -349,7 +349,7 @@ bool BufOverflowDetector::detectStrcat(AbstractState& as, const CallICFGNode *ca

if (std::find(strcatGroup.begin(), strcatGroup.end(), fun->getName()) != strcatGroup.end())
{
CallSite cs = SVFUtil::getSVFCallSite(call->getCallSite());
CallSite cs = SVFUtil::getSVFCallSite(call);
const SVFValue* arg0Val = cs.getArgument(0);
const SVFValue* arg1Val = cs.getArgument(1);
IntervalValue strLen0 = AbstractInterpretation::getAEInstance().getStrlen(as, arg0Val);
Expand All @@ -359,7 +359,7 @@ bool BufOverflowDetector::detectStrcat(AbstractState& as, const CallICFGNode *ca
}
else if (std::find(strncatGroup.begin(), strncatGroup.end(), fun->getName()) != strncatGroup.end())
{
CallSite cs = SVFUtil::getSVFCallSite(call->getCallSite());
CallSite cs = SVFUtil::getSVFCallSite(call);
const SVFValue* arg0Val = cs.getArgument(0);
const SVFValue* arg2Val = cs.getArgument(2);
IntervalValue arg2Num = as[svfir->getValueNode(arg2Val)].getInterval();
Expand Down
Loading

0 comments on commit 86ac4b8

Please sign in to comment.