Skip to content

Commit

Permalink
add arraySize to AddrStmt
Browse files Browse the repository at this point in the history
1) add addAddrWithAllocArraySz, the arg is AllocaInst and CallBase
2) add arraySize getter/setter to AddrStmt
  • Loading branch information
bjjwwang committed Jan 16, 2024
1 parent 5ba90ce commit 9629983
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
43 changes: 43 additions & 0 deletions svf-llvm/include/SVF-LLVM/SVFIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,49 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
}
return nullptr;
}

/// Add Address edge from allocinst with arraysize like "%4 = alloca i8, i64 3"
inline AddrStmt* addAddrWithAllocArraySz(NodeID src, NodeID dst, llvm::AllocaInst& inst) {
AddrStmt* edge = addAddrEdge(src, dst);
if (inst.getArraySize()) {
SVFValue* arrSz = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(inst.getArraySize());
edge->addArrSize(arrSz);
}
return edge;
}

/// Add Address edge from ext call with args like "%5 = call i8* @malloc(i64 noundef 5)"
inline AddrStmt* addAddrWithAllocArraySz(NodeID src, NodeID dst, const CallBase* cs) {
// get name of called function
AddrStmt* edge = addAddrEdge(src, dst);
llvm::Function* calledFunc = cs->getCalledFunction();
if (calledFunc) {
const std::string& functionName = calledFunc->getName().str();
if (functionName == "malloc") {
if (cs->getNumOperands() > 0) {
const llvm::Value* val = cs->getArgOperand(0);
SVFValue* svfval = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(val);
edge->addArrSize(svfval);
}
}
// Check if the function called is 'calloc' and process its arguments.
else if (functionName == "calloc") {
if (cs->getNumOperands() > 1) {
edge->addArrSize(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(cs->getArgOperand(0)));
edge->addArrSize(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(cs->getArgOperand(1)));
}
}
else {
if (cs->getNumOperands() > 0) {
const llvm::Value* val = cs->getArgOperand(0);
SVFValue* svfval = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(val);
edge->addArrSize(svfval);
}
}
}
return edge;
}

/// Add Copy edge
inline CopyStmt* addCopyEdge(NodeID src, NodeID dst)
{
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ void SVFIRBuilder::visitAllocaInst(AllocaInst &inst)

NodeID src = getObjectNode(&inst);

addAddrEdge(src, dst);
addAddrWithAllocArraySz(src, dst, inst);

}

Expand Down
4 changes: 2 additions & 2 deletions svf-llvm/lib/SVFIRExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle
{
NodeID val = pag->getValueNode(svfInst);
NodeID obj = pag->getObjectNode(svfInst);
addAddrEdge(obj, val);
addAddrWithAllocArraySz(obj, val, cs);
}
else if (isHeapAllocExtCallViaArg(svfCall))
{
Expand All @@ -146,7 +146,7 @@ void SVFIRBuilder::handleExtCall(const CallBase* cs, const SVFFunction* svfCalle
NodeID obj = pag->addDummyObjNode(arg->getType());
if (vnArg && dummy && obj)
{
addAddrEdge(obj, dummy);
addAddrWithAllocArraySz(obj, dummy, cs);
addStoreEdge(dummy, vnArg);
}
}
Expand Down
11 changes: 11 additions & 0 deletions svf/include/SVFIR/SVFStatements.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ class AddrStmt: public AssignStmt
AddrStmt(const AddrStmt&); ///< place holder
void operator=(const AddrStmt&); ///< place holder

std::vector<SVFValue*> arrSize; ///< Array size of the allocated memory

public:
/// Methods for support type inquiry through isa, cast, and dyn_cast:
//@{
Expand All @@ -343,6 +345,15 @@ class AddrStmt: public AssignStmt

virtual const std::string toString() const override;

inline void addArrSize(SVFValue* size) {
arrSize.push_back(size);
}

///< get array size of the allocated memory
inline const std::vector<SVFValue*>& getArrSize() const {
return arrSize;
}

};

/*!
Expand Down

0 comments on commit 9629983

Please sign in to comment.