41 SVFUtil::errs() << SVFUtil::errMsg("Error ") << __FILE__ << ':' \
42 << __LINE__ << ": " << (msg) << '\n'; \
44#define ABORT_MSG(msg) \
50#define ABORT_IFNOT(condition, msg) \
58#define WARN_MSG(msg) \
61 SVFUtil::outs() << SVFUtil::wrnMsg("Warning ") << __FILE__ << ':' \
62 << __LINE__ << ": " << msg << '\n'; \
64#define WARN_IFNOT(condition, msg) \
72#define WARN_IFNOT(condition, msg)
86#if LLVM_VERSION_MAJOR >= 21
89 return v->hasUseList();
105 assert(val &&
"value cannot be empty");
108 return llvm::getLoadStoreType(
const_cast<Value *
>(val));
112 return gepInst->getSourceElementType();
116 return call->getFunctionType();
120 return allocaInst->getAllocatedType();
124 return globalValue->getValueType();
173 for (
const auto& use: var->users())
177 if (
const Function* calledFun = cs->getCalledFunction())
180 assert(cs->getNumOperands() > 1 &&
"arguments should be greater than 1");
181 const Value* dst = cs->getArgOperand(0);
182 const Value* src = cs->getArgOperand(1);
183 if(calledFun->getName().find(
"iconv") != std::string::npos)
185 if(var == cs->getArgOperand(0))
187 dst = cs->getArgOperand(3), src = cs->getArgOperand(1);
212 for (
const auto &source: sources)
218 ABORT_IFNOT(largestTy,
"return type cannot be null");
246 return tIt->second ? tIt->second :
defaultType(var);
252 workList.
push({var,
false});
254 while (!workList.
empty())
256 auto curPair = workList.
pop();
257 if (visited.count(curPair))
259 visited.insert(curPair);
260 const Value* curValue = curPair.first;
261 bool canUpdate = curPair.second;
264 auto insertInferSite = [&infersites,
265 &canUpdate](
const Value* infersite)
268 infersites.insert(infersite);
270 auto insertInferSitesOrPushWorklist =
271 [
this, &infersites, &workList, &canUpdate](
const auto& pUser)
278 infersites.insert(vIt->second.begin(),
285 workList.push({pUser,
false});
290 workList.push({curValue,
true});
292 if (
const auto* gepInst =
294 insertInferSite(gepInst);
298 for (
const auto it : curValue->users())
308 insertInferSite(loadInst);
310 else if (
const auto* storeInst =
313 if (storeInst->getPointerOperand() == curValue)
321 insertInferSite(storeInst);
323 else if (
hasUseList(storeInst->getPointerOperand()))
325 for (
const auto nit :
326 storeInst->getPointerOperand()->users())
336 insertInferSitesOrPushWorklist(nit);
341 if (
const auto* gepInst =
343 storeInst->getPointerOperand()))
363 const Value* gepBase = gepInst->getPointerOperand();
365 if (load &&
hasUseList(load->getPointerOperand()))
367 for (
const auto loadUse :
368 load->getPointerOperand()->users())
370 if (loadUse == load ||
373 for (
const auto gepUse : loadUse->users())
378 for (
const auto loadUse2 :
384 insertInferSitesOrPushWorklist(
391 else if (
const auto* alloc =
406 for (
const auto gepUse : alloc->users())
411 for (
const auto loadUse2 : gepUse->users())
415 insertInferSitesOrPushWorklist(
424 else if (
const auto* gepInst =
434 if (gepInst->getPointerOperand() == curValue)
435 insertInferSite(gepInst);
437 else if (
const auto* bitcast =
441 insertInferSitesOrPushWorklist(bitcast);
446 insertInferSitesOrPushWorklist(phiNode);
448 else if (
const auto* retInst =
462 for (
const auto callsite : retInst->getFunction()->users())
464 if (
const auto* callBase =
469 if (callBase->getCalledFunction() !=
470 retInst->getFunction())
472 insertInferSitesOrPushWorklist(callBase);
492 curValue == callBase->getCalledFunction())
496 if (!callBase->hasArgument(curValue))
498 if (
Function* calleeFunc = callBase->getCalledFunction())
504 if (calleeFunc->isVarArg())
507 for (
auto& I : instructions(calleeFunc))
510 llvm::dyn_cast<llvm::LoadInst>(&I))
512 llvm::Value* loadPointer =
513 load->getPointerOperand();
514 if (loadPointer->getName().compare(
517 insertInferSitesOrPushWorklist(load);
522 else if (!calleeFunc->isDeclaration())
524 insertInferSitesOrPushWorklist(
525 calleeFunc->getArg(pos));
533 std::transform(infersites.begin(), infersites.end(),
534 std::inserter(types, types.begin()),
544 WARN_MSG(
"Using default type, trace ID is " +
570 workList.
push({var,
false});
571 while (!workList.
empty())
573 auto curPair = workList.
pop();
574 if (visited.count(curPair))
continue;
575 visited.insert(curPair);
576 const Value *curValue = curPair.first;
577 bool canUpdate = curPair.second;
580 auto insertAllocs = [&sources, &canUpdate](
const Value *source)
582 if (canUpdate) sources.insert(source);
584 auto insertAllocsOrPushWorklist = [
this, &sources, &workList, &canUpdate](
const auto &pUser)
591 sources.insert(vIt->second.begin(), vIt->second.end());
602 workList.push({curValue,
true});
607 insertAllocs(curValue);
611 Value *prevVal = bitCastInst->getOperand(0);
612 insertAllocsOrPushWorklist(prevVal);
616 for (
u32_t i = 0; i < phiNode->getNumOperands(); ++i)
618 insertAllocsOrPushWorklist(phiNode->getOperand(i));
628 const Value* ptr = loadInst->getPointerOperand();
631 for (
const auto use: ptr->users())
635 if (storeInst->getPointerOperand() == loadInst->getPointerOperand())
637 insertAllocsOrPushWorklist(storeInst->getValueOperand());
645 for (
const auto use: argument->getParent()->users())
651 if (callBase->getCalledFunction() != argument->getParent())
continue;
652 u32_t pos = argument->getParent()->isVarArg() ? 0 : argument->getArgNo();
653 insertAllocsOrPushWorklist(callBase->getArgOperand(pos));
668 callBase->doesNotReturn() ? nullptr : callBase->getCalledFunction();
671 if (!callee->isDeclaration())
676 assert (exitBB &&
"exit bb is not a basic block?");
677 const Value *pValue = &exitBB->back();
681 if (retInst && retInst->getReturnValue())
682 insertAllocsOrPushWorklist(retInst->getReturnValue());
710 if (
const Function *func = cs->getCalledFunction())
712 if (func->getName().find(
TYPEMALLOC) != std::string::npos)
717 assert(pInt &&
"the second argument is a integer");
735 assert(callBase->hasArgument(arg) &&
"callInst does not have argument arg?");
736 auto it = std::find(callBase->arg_begin(), callBase->arg_end(), arg);
737 assert(it != callBase->arg_end() &&
"Didn't find argument?");
738 return std::distance(callBase->arg_begin(), it);
744 if (objTys.empty())
return nullptr;
747 for (
const Type *ty: objTys)
751 assert(!typeSzToTypes.empty() &&
"typeSzToTypes cannot be empty");
753 std::tie(std::ignore, largestTypes) = *typeSzToTypes.begin();
754 assert(!largestTypes.empty() &&
"largest element cannot be empty");
755 return *largestTypes.begin();
791 auto addNamesFromFunc = [&names](
const Function *func) ->
void
798 auto addNamesFromCall = [&names, &addNamesFromFunc](
const CallBase *call) ->
void
802 const auto *func = call->getCalledFunction();
804 else addNamesFromFunc(func);
815 if (val == thisPtr)
continue;
820 addNamesFromFunc(func);
869 workList.
push({startValue,
false});
870 while (!workList.
empty())
872 auto curPair = workList.
pop();
873 if (visited.count(curPair))
continue;
874 visited.insert(curPair);
875 const Value *curValue = curPair.first;
876 bool canUpdate = curPair.second;
879 auto insertSource = [&sources, &canUpdate](
const Value *source)
881 if (canUpdate) sources.insert(source);
883 auto insertSourcesOrPushWorklist = [
this, &sources, &workList, &canUpdate](
const auto &pUser)
890 sources.insert(vIt->second.begin(), vIt->second.end());
901 workList.push({curValue,
true});
907 if (
const auto *parent = inst->getFunction())
917 insertSource(curValue);
923 insertSourcesOrPushWorklist(getElementPtrInst->getPointerOperand());
927 insertSourcesOrPushWorklist(bitCastInst->getOperand(0));
931 for (
const auto *op : phiNode->operand_values())
933 insertSourcesOrPushWorklist(op);
938 const Value* ptr = loadInst->getPointerOperand();
941 for (
const auto *user : ptr->users())
945 if (storeInst->getPointerOperand() == loadInst->getPointerOperand())
947 insertSourcesOrPushWorklist(storeInst->getValueOperand());
955 for (
const auto *user: argument->getParent()->users())
961 if (callBase->getCalledFunction() != argument->getParent())
continue;
962 u32_t pos = argument->getParent()->isVarArg() ? 0 : argument->getArgNo();
963 insertSourcesOrPushWorklist(callBase->getArgOperand(pos));
978 callBase->doesNotReturn() ? nullptr : callBase->getCalledFunction();
981 if (!callee->isDeclaration())
985 assert (exitBB &&
"exit bb is not a basic block?");
986 const Value *pValue = &exitBB->back();
990 if (retInst && retInst->getReturnValue())
991 insertSourcesOrPushWorklist(retInst->getReturnValue());
1008 assert(startValue &&
"startValue was null?");
1020 auto inferViaCppCall = [&sources](
const CallBase *caller)
1022 if (!caller)
return;
1027 for (
const auto *user : startValue->users())
1031 inferViaCppCall(caller);
1035 for (
const auto *cast_user : bitcast->users())
1039 inferViaCppCall(caller);
const std::string TYPEMALLOC
const Type * infersiteToType(const Value *val)
#define ABORT_IFNOT(condition, msg)
static bool hasUseList(const Value *v)
bool push(const Data &data)
static LLVMModuleSet * getLLVMModuleSet()
const BasicBlock * getFunExitBB(const Function *fun) const
LLVMContext & getContext() const
ValueToInferSites _valueToInferSites
LLVMContext & getLLVMCtx()
ValueToSources _valueToAllocs
const Type * selectLargestSizedType(Set< const Type * > &objTys)
select the largest (conservative) type from all types
const Type * inferPointsToType(const Value *var)
Set< const Value * > & bwFindAllocOfVar(const Value *var)
backward collect all possible allocation sites (stack, static, heap) of var
u32_t objTyToNumFields(const Type *objTy)
bool isAlloc(const SVF::Value *val)
is allocation (stack, static, heap)
u32_t getArgPosInCall(const CallBase *callBase, const Value *arg)
Set< const Value * > & bwFindAllocOrClsNameSources(const Value *startValue)
ValueToClassNames _thisPtrClassNames
Set< std::string > & inferThisPtrClsName(const Value *thisPtr)
get or infer the class names of thisptr
const Type * fwInferObjType(const Value *var)
forward infer the type of the object pointed by var
const IntegerType * int8Type()
int8 type
const Type * ptrType()
pointer type
const Type * inferObjType(const Value *var)
get or infer the type of the object pointed by the value
const Type * defaultType(const Value *val)
default type
ObjToClsNameSources _objToClsNameSources
Set< const CallBase * > & fwFindClsNameSources(const Value *startValue)
forward find class name sources starting from an allocation
void validateTypeCheck(const CallBase *cs)
validate type inference
ValueToSources _valueToAllocOrClsNameSources
static const Option< u32_t > MaxFieldLimit
Maximum number of field derivations for an object.
bool isHeapAllocExtCallViaRet(const Instruction *inst)
bool isMemcpyExtFun(const Function *fun)
std::string dumpType(const Type *type)
std::pair< s64_t, u64_t > getIntegerValue(const ConstantInt *intValue)
std::string dumpValueAndDbgInfo(const Value *val)
u32_t getNumOfElements(const Type *ety)
Return size of this object based on LLVM value.
bool isObject(const Value *ref)
Return true if this value refers to a object.
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
LLVM_NODISCARD bool isa(const Y &Val)
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
std::ostream & errs()
Overwrite llvm::errs().
std::ostream & outs()
Overwrite llvm::outs().
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
std::string extractClsNameFromDynCast(const CallBase *callBase)
extract class name from cpp dyncast function
bool classTyHasVTable(const StructType *ty)
bool isClsNameSource(const Value *val)
Set< std::string > extractClsNamesFromFunc(const Function *foo)
extract class name from the c++ function name, e.g., constructor/destructors
bool isDynCast(const Function *foo)
whether foo is a cpp dyncast function
std::map< Key, Value, Compare, Allocator > OrderedMap
llvm::BasicBlock BasicBlock
llvm::AllocaInst AllocaInst
std::set< Key, Compare, Allocator > OrderedSet
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
llvm::GlobalValue GlobalValue
llvm::Value Value
LLVM Basic classes.
llvm::StoreInst StoreInst
llvm::LLVMContext LLVMContext