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

add coverage and fix struct gep accumulatedGepByteOffset() #1241

Merged
merged 6 commits into from
Nov 10, 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
10 changes: 6 additions & 4 deletions svf-llvm/tools/Example/svf-ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ void traverseOnSVFStmt(const ICFGNode* node)
/*!
* An example to query/collect all successor nodes from a ICFGNode (iNode) along control-flow graph (ICFG)
*/
void traverseOnICFG(ICFG* icfg, const SVFInstruction* svfInst)
void traverseOnICFG(ICFG* icfg, const ICFGNode* iNode)
{
ICFGNode* iNode = icfg->getICFGNode(svfInst);
FIFOWorkList<const ICFGNode*> worklist;
Set<const ICFGNode*> visited;
worklist.push(iNode);
Expand All @@ -154,7 +153,6 @@ void traverseOnICFG(ICFG* icfg, const SVFInstruction* svfInst)
{
ICFGEdge* edge = *it;
ICFGNode* succNode = edge->getDstNode();
traverseOnSVFStmt(succNode);
if (visited.find(succNode) == visited.end())
{
visited.insert(succNode);
Expand Down Expand Up @@ -250,8 +248,12 @@ int main(int argc, char ** argv)
/// Collect uses of an LLVM Value
/// traverseOnVFG(svfg, value);


/// Collect all successor nodes on ICFG
/// traverseOnICFG(icfg, value);
for (const auto &it : *icfg) {
const ICFGNode* node = it.second;
traverseOnICFG(icfg, node);
}

// clean up memory
delete vfg;
Expand Down
31 changes: 24 additions & 7 deletions svf/lib/MemoryModel/AccessPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,43 @@
APOffset totalConstOffset = 0;
for(int i = offsetVarAndGepTypePairs.size() - 1; i >= 0; i--)
{
/// For example, there is struct DEST{int a, char b[10], int c[5]}
/// (1) %c = getelementptr inbounds %struct.DEST, %struct.DEST* %arr, i32 0, i32 2
// (2) %arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %b, i64 0, i64 8
const SVFValue* value = offsetVarAndGepTypePairs[i].first->getValue();
/// for (1) offsetVarAndGepTypePairs.size() = 2
/// i = 0, type: %struct.DEST*, PtrType, op = 0
/// i = 1, type: %struct.DEST, StructType, op = 2
/// for (2) offsetVarAndGepTypePairs.size() = 2
/// i = 0, type: [10 x i8]*, PtrType, op = 0
/// i = 1, type: [10 x i8], ArrType, op = 8
const SVFType* type = offsetVarAndGepTypePairs[i].second;
const SVFType* type2 = type;
if (const SVFArrayType* arrType = SVFUtil::dyn_cast<SVFArrayType>(type))
{
/// for (2) i = 1, arrType: [10 x i8], type2 = i8
type2 = arrType->getTypeOfElement();
}
else if (const SVFPointerType* ptrType = SVFUtil::dyn_cast<SVFPointerType>(type))
{
/// for (1) i = 0, ptrType: %struct.DEST*, type2: %struct.DEST
/// for (2) i = 0, ptrType: [10 x i8]*, type2 = [10 x i8]
type2 = ptrType->getPtrElementType();
}

const SVFConstantInt* op = SVFUtil::dyn_cast<SVFConstantInt>(value);
if (const SVFStructType* structType = SVFUtil::dyn_cast<SVFStructType>(type))
{
type2 = structType->getTypeInfo()->getOriginalElemType(op->getSExtValue());
totalConstOffset += type2->getLLVMByteSize();
}
else
{
if (const SVFStructType* structType = SVFUtil::dyn_cast<SVFStructType>(type)) {

Check warning on line 129 in svf/lib/MemoryModel/AccessPath.cpp

View check run for this annotation

Codecov / codecov/patch

svf/lib/MemoryModel/AccessPath.cpp#L129

Added line #L129 was not covered by tests
/// for (1) structType: %struct.DEST
/// structField = 0, type2: int
/// structField = 1, type2: char[10]
/// structField = 2, type2: int[5]
for (u32_t structField = 0; structField < (u32_t)op->getSExtValue(); ++structField) {
type2 = structType->getTypeInfo()->getOriginalElemType(structField);
totalConstOffset += type2->getLLVMByteSize();

Check warning on line 136 in svf/lib/MemoryModel/AccessPath.cpp

View check run for this annotation

Codecov / codecov/patch

svf/lib/MemoryModel/AccessPath.cpp#L134-L136

Added lines #L134 - L136 were not covered by tests
}
} else {
/// for (2) i = 0, op: 0, type: [10 x i8]*(Ptr), type2: [10 x i8](Arr)
/// i = 1, op: 8, type: [10 x i8](Arr), type2: i8
totalConstOffset += op->getSExtValue() * type2->getLLVMByteSize();
}
}
Expand Down
Loading