Skip to content

Commit

Permalink
Merge pull request #1241 from bjjwwang/1107
Browse files Browse the repository at this point in the history
add coverage and fix struct gep accumulatedGepByteOffset()
  • Loading branch information
yuleisui authored Nov 10, 2023
2 parents 572da48 + cc2e754 commit bf3ed56
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
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 AccessPath::computeConstantByteOffset() const
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)) {
/// 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();
}
} 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

0 comments on commit bf3ed56

Please sign in to comment.