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

infer object type in "createObjTypeInfo" instead of "analyzeHeapObjType" #1297

Merged
merged 1 commit into from
Dec 22, 2023
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
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 @@ -264,36 +264,36 @@
// e.g., for `gep %struct.foo, ptr %base, i64 0`, we return struct.foo
return gepInst->getSourceElementType();
}
else if (const GEPOperator* gepOperator = SVFUtil::dyn_cast<GEPOperator>(value))

Check warning on line 267 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L267

Added line #L267 was not covered by tests
{
// get the source element type of a gep instruction
// e.g., for `gep %struct.foo, ptr %base, i64 0`, we return struct.foo
return gepOperator->getSourceElementType();
}
else if (const CallInst *callInst = SVFUtil::dyn_cast<CallInst>(value))

Check warning on line 273 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L273

Added line #L273 was not covered by tests
{
// get the pointee type of return value
// e.g., for `%call = call ptr @_Znwm(i64 noundef 8)`, we return i64
return callInst->getFunctionType();
}
else if (const CallBase *callBase = SVFUtil::dyn_cast<CallBase>(value))

Check warning on line 279 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L279

Added line #L279 was not covered by tests
{
// get the pointee type of return value
// e.g., for `%call = call ptr @_Znwm(i64 noundef 8)`, we return i64
return callBase->getFunctionType();
}
else if (const AllocaInst *allocaInst = SVFUtil::dyn_cast<AllocaInst>(value))

Check warning on line 285 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L285

Added line #L285 was not covered by tests
{
// get the type of the allocated memory
// e.g., for `%retval = alloca i64, align 4`, we return i64
return allocaInst->getAllocatedType();
}
else if (const GlobalVariable *globalVar = SVFUtil::dyn_cast<GlobalVariable>(value))

Check warning on line 291 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L291

Added line #L291 was not covered by tests
{
// get the pointee type of the global pointer
return globalVar->getValueType();
}
else if (const Function* func = SVFUtil::dyn_cast<Function>(value))

Check warning on line 296 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L296

Added line #L296 was not covered by tests
{
// get the pointee type of return value
// e.g., for `%call = call ptr @_Znwm(i64 noundef 8)`, we return i64
Expand Down Expand Up @@ -438,7 +438,7 @@
/*!
* 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
Loading