Skip to content

Commit

Permalink
Merge pull request #1616 from shuangxiangkan/master
Browse files Browse the repository at this point in the history
Add "ALLOC_STACK_RET" annotation in extapi.c
  • Loading branch information
yuleisui authored Dec 20, 2024
2 parents 27656e7 + f530cc0 commit 21f5718
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 145 deletions.
10 changes: 10 additions & 0 deletions svf-llvm/include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,19 @@ inline bool isHeapAllocExtCall(const Instruction *inst)
return isHeapAllocExtCallViaRet(inst) || isHeapAllocExtCallViaArg(inst);
}

bool isStackAllocExtCallViaRet(const Instruction *inst);

inline bool isStackAllocExtCall(const Instruction *inst)
{
return isStackAllocExtCallViaRet(inst);
}

// Check if a given value represents a heap object.
bool isHeapObj(const Value* val);

// Check if a given value represents a stack object.
bool isStackObj(const Value* val);

/// Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls
bool isNonInstricCallSite(const Instruction* inst);

Expand Down
35 changes: 35 additions & 0 deletions svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,21 @@ bool LLVMUtil::isHeapAllocExtCallViaArg(const Instruction* inst)
}
}

bool LLVMUtil::isStackAllocExtCallViaRet(const Instruction *inst)
{
LLVMModuleSet* pSet = LLVMModuleSet::getLLVMModuleSet();
ExtAPI* extApi = ExtAPI::getExtAPI();
bool isPtrTy = inst->getType()->isPointerTy();
if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
{
const Function* fun = call->getCalledFunction();
return fun && isPtrTy &&
extApi->is_alloc_stack_ret(pSet->getSVFFunction(fun));
}
else
return false;
}

/**
* Check if a given value represents a heap object.
*
Expand All @@ -670,6 +685,26 @@ bool LLVMUtil::isHeapObj(const Value* val)
return false;
}

/**
* @param val The value to check.
* @return True if the value represents a stack object, false otherwise.
*/
bool LLVMUtil::isStackObj(const Value* val)
{
if (SVFUtil::isa<AllocaInst>(val))
{
return true;
}
// Check if the value is an instruction and if it is a stack allocation external call
else if (SVFUtil::isa<Instruction>(val) &&
LLVMUtil::isStackAllocExtCall(SVFUtil::cast<Instruction>(val)))
{
return true;
}
// Return false if none of the above conditions are met
return false;
}

bool LLVMUtil::isNonInstricCallSite(const Instruction* inst)
{
bool res = false;
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 @@ -282,7 +282,7 @@ void SVFIRBuilder::initialiseNodes()
llvmModuleSet()->setValueAttr(llvmValue,pag->getGNode(iter->second));
}
// Check if the value is an alloca instruction and add a stack object node
else if (SVFUtil::isa<AllocaInst>(llvmValue))
else if (LLVMUtil::isStackObj(llvmValue))
{
const SVFFunction* f =
SVFUtil::cast<SVFInstruction>(iter->first)->getFunction();
Expand Down
6 changes: 3 additions & 3 deletions svf-llvm/lib/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,13 @@ void SymbolTableBuilder::analyzeObjType(ObjTypeInfo* typeinfo, const Value* val)

/*!
* Analyze byte size of heap alloc function (e.g. malloc/calloc/...)
* 1) __attribute__((annotate("ALLOC_RET"), annotate("AllocSize:Arg0")))
* 1) __attribute__((annotate("ALLOC_HEAP_RET"), annotate("AllocSize:Arg0")))
void* safe_malloc(unsigned long size).
Byte Size is the size(Arg0)
2)__attribute__((annotate("ALLOC_RET"), annotate("AllocSize:Arg0*Arg1")))
2)__attribute__((annotate("ALLOC_HEAP_RET"), annotate("AllocSize:Arg0*Arg1")))
char* safecalloc(int a, int b)
Byte Size is a(Arg0) * b(Arg1)
3)__attribute__((annotate("ALLOC_RET"), annotate("UNKNOWN")))
3)__attribute__((annotate("ALLOC_HEAP_RET"), annotate("UNKNOWN")))
void* __sysv_signal(int a, void *b)
Byte Size is Unknown
If all required arg values are constant, byte Size is also constant,
Expand Down
Loading

0 comments on commit 21f5718

Please sign in to comment.