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 number of fields of a heap object #1290

Merged
merged 3 commits into from
Dec 20, 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
13 changes: 5 additions & 8 deletions svf-llvm/include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ const Argument* getConstructorThisPtr(const Function* fun);
const Value* getVCallThisPtr(const CallBase* cs);
const Value* getVCallVtblPtr(const CallBase* cs);
s32_t getVCallIdx(const CallBase* cs);
std::string getClassNameFromType(const Type* ty);
std::string getClassNameFromType(const StructType* ty);
std::string getClassNameOfThisPtr(const CallBase* cs);
std::string getFunNameOfVCallSite(const CallBase* cs);
bool VCallInCtorOrDtor(const CallBase* cs);
Expand All @@ -513,13 +513,10 @@ bool VCallInCtorOrDtor(const CallBase* cs);
bool isSameThisPtrInConstructor(const Argument* thisPtr1,
const Value* thisPtr2);

template<typename T>
std::string llvmToString(const T& val)
{
std::string str;
llvm::raw_string_ostream(str) << val;
return str;
}
std::string dumpValue(const Value* val);

std::string dumpType(const Type* type);


/**
* See more: https://github.com/SVF-tools/SVF/pull/1191
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/include/SVF-LLVM/SymbolTableBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SymbolTableBuilder
/// Analyse types of all flattened fields of this object
void analyzeObjType(ObjTypeInfo* typeinfo, const Value* val);
/// Analyse types of heap and static objects
void analyzeHeapObjType(ObjTypeInfo* typeinfo, const Value* val);
u32_t analyzeHeapObjType(ObjTypeInfo* typeinfo, const Value* val);
/// Analyse types of heap and static objects
void analyzeStaticObjType(ObjTypeInfo* typeinfo, const Value* val);

Expand Down
60 changes: 42 additions & 18 deletions svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,12 @@
if (const FunctionType* functy = SVFUtil::dyn_cast<FunctionType>(elemTy))
{
const Type* paramty = functy->getParamType(0);
std::string className = LLVMUtil::getClassNameFromType(paramty);
std::string className = "";

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L926

Added line #L926 was not covered by tests
if(const PointerType* ptrTy = SVFUtil::dyn_cast<PointerType>(paramty))
{
if(const StructType* st = SVFUtil::dyn_cast<StructType>(getPtrElementType(ptrTy)))
className = LLVMUtil::getClassNameFromType(st);

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L930

Added line #L930 was not covered by tests
}
if (className.size() > 0)
{
return true;
Expand Down Expand Up @@ -1156,33 +1161,27 @@
return false;
}

std::string LLVMUtil::getClassNameFromType(const Type* ty)
std::string LLVMUtil::getClassNameFromType(const StructType* ty)

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1164

Added line #L1164 was not covered by tests
{
std::string className = "";
if (const PointerType* ptrType = SVFUtil::dyn_cast<PointerType>(ty))
if (!((SVFUtil::cast<StructType>(ty))->isLiteral()))
{
const Type* elemType = LLVMUtil::getPtrElementType(ptrType);
if (SVFUtil::isa<StructType>(elemType) &&
!((SVFUtil::cast<StructType>(elemType))->isLiteral()))
std::string elemTypeName = ty->getStructName().str();
if (elemTypeName.compare(0, clsName.size(), clsName) == 0)
{
std::string elemTypeName = elemType->getStructName().str();
if (elemTypeName.compare(0, clsName.size(), clsName) == 0)
{
className = elemTypeName.substr(clsName.size());
}
else if (elemTypeName.compare(0, structName.size(), structName) ==
0)
{
className = elemTypeName.substr(structName.size());
}
className = elemTypeName.substr(clsName.size());
}
else if (elemTypeName.compare(0, structName.size(), structName) == 0)
{
className = elemTypeName.substr(structName.size());
}
}
return className;
}

std::string LLVMUtil::getClassNameOfThisPtr(const CallBase* inst)
{
std::string thisPtrClassName;
std::string thisPtrClassName = "";
if (const MDNode* N = inst->getMetadata("VCallPtrType"))
{
const MDString* mdstr = SVFUtil::cast<MDString>(N->getOperand(0).get());
Expand All @@ -1191,7 +1190,9 @@
if (thisPtrClassName.size() == 0)
{
const Value* thisPtr = LLVMUtil::getVCallThisPtr(inst);
thisPtrClassName = getClassNameFromType(thisPtr->getType());
if(const PointerType* ptrTy = SVFUtil::dyn_cast<PointerType>(thisPtr->getType()))
if(const StructType* st = SVFUtil::dyn_cast<StructType>(getPtrElementType(ptrTy)))
thisPtrClassName = getClassNameFromType(st);

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1194-L1195

Added lines #L1194 - L1195 were not covered by tests
}

size_t found = thisPtrClassName.find_last_not_of("0123456789");
Expand Down Expand Up @@ -1278,6 +1279,29 @@
return val;
}

std::string LLVMUtil::dumpValue(const Value* val)
{
std::string str;
llvm::raw_string_ostream rawstr(str);
if (val)
rawstr << " " << *val << " ";
else
rawstr << " llvm Value is null";

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1289

Added line #L1289 was not covered by tests
return rawstr.str();
}

std::string LLVMUtil::dumpType(const Type* type)
{

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1293-L1294

Added lines #L1293 - L1294 were not covered by tests
std::string str;
llvm::raw_string_ostream rawstr(str);
if (type)

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1296-L1297

Added lines #L1296 - L1297 were not covered by tests
rawstr << " " << *type << " ";
else
rawstr << " llvm type is null";

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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1300

Added line #L1300 was not covered by tests
return rawstr.str();
}


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

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L1304

Added line #L1304 was not covered by tests
namespace SVF
{

Expand Down
25 changes: 17 additions & 8 deletions svf-llvm/lib/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,20 +753,32 @@
/*!
* Analyse types of heap and static objects
*/
void SymbolTableBuilder::analyzeHeapObjType(ObjTypeInfo* typeinfo, const Value* val)
u32_t SymbolTableBuilder::analyzeHeapObjType(ObjTypeInfo* typeinfo, const Value* val)
{
if(const Value* castUse = getUniqueUseViaCastInst(val))
{
typeinfo->setFlag(ObjTypeInfo::HEAP_OBJ);
typeinfo->resetTypeForHeapStaticObj(
LLVMModuleSet::getLLVMModuleSet()->getSVFType(castUse->getType()));
const Type* objTy = getTypeOfHeapAlloc(SVFUtil::cast<Instruction>(val));
typeinfo->resetTypeForHeapStaticObj(LLVMModuleSet::getLLVMModuleSet()->getSVFType(objTy));
analyzeObjType(typeinfo,castUse);
if(SVFUtil::isa<ArrayType>(objTy))
return getNumOfElements(objTy);

Check warning on line 765 in svf-llvm/lib/SymbolTableBuilder.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/SymbolTableBuilder.cpp#L765

Added line #L765 was not covered by tests
else if(const StructType* st = SVFUtil::dyn_cast<StructType>(objTy))
{
/// For an C++ class, it can have variant elements depending on the vtable size,
/// Hence we only handle non-cpp-class object, the type of the cpp class is treated as PointerType at the cast site
if(getClassNameFromType(st).empty())
return getNumOfElements(objTy);
else
typeinfo->resetTypeForHeapStaticObj(LLVMModuleSet::getLLVMModuleSet()->getSVFType(castUse->getType()));
}
}
else
{
typeinfo->setFlag(ObjTypeInfo::HEAP_OBJ);
typeinfo->setFlag(ObjTypeInfo::HASPTR_OBJ);
}
return typeinfo->getMaxFieldOffsetLimit();
}

/*!
Expand All @@ -777,8 +789,7 @@
if(const Value* castUse = getUniqueUseViaCastInst(val))
{
typeinfo->setFlag(ObjTypeInfo::STATIC_OBJ);
typeinfo->resetTypeForHeapStaticObj(
LLVMModuleSet::getLLVMModuleSet()->getSVFType(castUse->getType()));
typeinfo->resetTypeForHeapStaticObj(LLVMModuleSet::getLLVMModuleSet()->getSVFType(castUse->getType()));

Check warning on line 792 in svf-llvm/lib/SymbolTableBuilder.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/SymbolTableBuilder.cpp#L792

Added line #L792 was not covered by tests
analyzeObjType(typeinfo,castUse);
}
else
Expand Down Expand Up @@ -844,9 +855,7 @@
LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(
SVFUtil::cast<Instruction>(val))))
{
analyzeHeapObjType(typeinfo,val);
// Heap object, label its field as infinite here
elemNum = typeinfo->getMaxFieldOffsetLimit();
elemNum = analyzeHeapObjType(typeinfo,val);
// analyze heap alloc like (malloc/calloc/...), the alloc functions have
// annotation like "AllocSize:Arg1". Please refer to extapi.c.
// e.g. calloc(4, 10), annotation is "AllocSize:Arg0*Arg1",
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/SVFIR/SymbolTableInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ bool ObjTypeInfo::isNonPtrFieldObj(const APOffset& apOffset)
if (hasPtrObj() == false)
return true;

const SVFType* ety = getType();
const SVFType* ety = type;

if (SVFUtil::isa<SVFStructType, SVFArrayType>(ety))
{
Expand Down
Loading