Skip to content

Commit

Permalink
Merge pull request #1297 from yuleisui/master
Browse files Browse the repository at this point in the history
infer object type in "createObjTypeInfo" instead of "analyzeHeapObjType"
  • Loading branch information
yuleisui authored Dec 22, 2023
2 parents 1195c8e + ee4d36a commit 90a0715
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 42 deletions.
34 changes: 1 addition & 33 deletions svf-llvm/include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,7 @@ Type *getPointeeType(const Value *value);

/// Get the reference type of heap/static object from an allocation site.
//@{
inline const PointerType *getRefTypeOfHeapAllocOrStatic(const CallBase* cs)
{
const PointerType *refType = nullptr;
const SVFInstruction* svfcall = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(cs);
CallSite svfcs = SVFUtil::getSVFCallSite(svfcall);
// Case 1: heap object held by *argument, we should get its element type.
if (SVFUtil::isHeapAllocExtCallViaArg(svfcs))
{
int argPos = SVFUtil::getHeapAllocHoldingArgPosition(svfcs);
const Value* arg = cs->getArgOperand(argPos);
if (const PointerType *argType = SVFUtil::dyn_cast<PointerType>(arg->getType()))
// TODO: getPtrElementType need type inference
refType = SVFUtil::dyn_cast<PointerType>(getPtrElementType(argType));
}
// Case 2: heap object held by return value.
else
{
assert(SVFUtil::isHeapAllocExtCallViaRet(svfcs)
&& "Must be heap alloc via ret, or static allocation site");
refType = SVFUtil::dyn_cast<PointerType>(cs->getType());
}
assert(refType && "Allocated object must be held by a pointer-typed value.");
return refType;
}

inline const PointerType *getRefTypeOfHeapAllocOrStatic(const Instruction* inst)
{
const CallBase* cs = getLLVMCallSite(inst);
return getRefTypeOfHeapAllocOrStatic(cs);
}
const Type *inferTypeOfHeapObjOrStaticObj(const Instruction* inst);
//@}

/// Return true if this value refers to a object
Expand Down Expand Up @@ -227,9 +198,6 @@ const Value* stripConstantCasts(const Value* val);
/// Strip off the all casts
const Value* stripAllCasts(const Value* val);

/// Get the type of the heap allocation
const Type* getTypeOfHeapAlloc(const Instruction* inst);

/// Return the bitcast instruction right next to val, otherwise
/// return nullptr
const Value* getFirstUseViaCastInst(const Value* val);
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ const Value* LLVMUtil::getFirstUseViaCastInst(const Value* val)
/*!
* Return the type of the object from a heap allocation
*/
const Type* LLVMUtil::getTypeOfHeapAlloc(const Instruction *inst)
const Type* LLVMUtil::inferTypeOfHeapObjOrStaticObj(const Instruction *inst)
{
const PointerType* type = SVFUtil::dyn_cast<PointerType>(inst->getType());
const SVFInstruction* svfinst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(inst);
Expand Down
16 changes: 8 additions & 8 deletions svf-llvm/lib/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,23 +577,25 @@ void SymbolTableBuilder::handleGlobalInitializerCE(const Constant* C)
ObjTypeInfo* SymbolTableBuilder::createObjTypeInfo(const Value* val)
{
/// TODO: getPtrElementType to be removed
const PointerType* refTy = nullptr;
const Type* objTy = nullptr;

const Instruction* I = SVFUtil::dyn_cast<Instruction>(val);

// We consider two types of objects:
// (1) A heap/static object from a callsite
if (I && isNonInstricCallSite(LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(I)))
{
refTy = getRefTypeOfHeapAllocOrStatic(I);
objTy = inferTypeOfHeapObjOrStaticObj(I);
}
// (2) Other objects (e.g., alloca, global, etc.)
else
refTy = SVFUtil::dyn_cast<PointerType>(val->getType());
{
if(const PointerType* refTy = SVFUtil::dyn_cast<PointerType>(val->getType()))
objTy = getPtrElementType(refTy);
}

if (refTy)
if (objTy)
{
Type* objTy = getPtrElementType(refTy);
ObjTypeInfo* typeInfo = new ObjTypeInfo(
LLVMModuleSet::getLLVMModuleSet()->getSVFType(objTy),
Options::MaxFieldLimit());
Expand Down Expand Up @@ -761,9 +763,8 @@ u32_t SymbolTableBuilder::analyzeHeapObjType(ObjTypeInfo* typeinfo, const Value*
if(const Value* castUse = getFirstUseViaCastInst(val))
{
typeinfo->setFlag(ObjTypeInfo::HEAP_OBJ);
const Type* objTy = getTypeOfHeapAlloc(SVFUtil::cast<Instruction>(val));
typeinfo->resetTypeForHeapStaticObj(LLVMModuleSet::getLLVMModuleSet()->getSVFType(objTy));
analyzeObjType(typeinfo,castUse);
const Type* objTy = LLVMModuleSet::getLLVMModuleSet()->getLLVMType(typeinfo->getType());
if(SVFUtil::isa<ArrayType>(objTy))
return getNumOfElements(objTy);
else if(const StructType* st = SVFUtil::dyn_cast<StructType>(objTy))
Expand Down Expand Up @@ -792,7 +793,6 @@ void SymbolTableBuilder::analyzeStaticObjType(ObjTypeInfo* typeinfo, const Value
if(const Value* castUse = getFirstUseViaCastInst(val))
{
typeinfo->setFlag(ObjTypeInfo::STATIC_OBJ);
typeinfo->resetTypeForHeapStaticObj(LLVMModuleSet::getLLVMModuleSet()->getSVFType(castUse->getType()));
analyzeObjType(typeinfo,castUse);
}
else
Expand Down

0 comments on commit 90a0715

Please sign in to comment.