Skip to content

Commit 21f5718

Browse files
authored
Merge pull request #1616 from shuangxiangkan/master
Add "ALLOC_STACK_RET" annotation in extapi.c
2 parents 27656e7 + f530cc0 commit 21f5718

File tree

7 files changed

+238
-145
lines changed

7 files changed

+238
-145
lines changed

svf-llvm/include/SVF-LLVM/LLVMUtil.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,19 @@ inline bool isHeapAllocExtCall(const Instruction *inst)
360360
return isHeapAllocExtCallViaRet(inst) || isHeapAllocExtCallViaArg(inst);
361361
}
362362

363+
bool isStackAllocExtCallViaRet(const Instruction *inst);
364+
365+
inline bool isStackAllocExtCall(const Instruction *inst)
366+
{
367+
return isStackAllocExtCallViaRet(inst);
368+
}
369+
363370
// Check if a given value represents a heap object.
364371
bool isHeapObj(const Value* val);
365372

373+
// Check if a given value represents a stack object.
374+
bool isStackObj(const Value* val);
375+
366376
/// Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls
367377
bool isNonInstricCallSite(const Instruction* inst);
368378

svf-llvm/lib/LLVMUtil.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,21 @@ bool LLVMUtil::isHeapAllocExtCallViaArg(const Instruction* inst)
646646
}
647647
}
648648

649+
bool LLVMUtil::isStackAllocExtCallViaRet(const Instruction *inst)
650+
{
651+
LLVMModuleSet* pSet = LLVMModuleSet::getLLVMModuleSet();
652+
ExtAPI* extApi = ExtAPI::getExtAPI();
653+
bool isPtrTy = inst->getType()->isPointerTy();
654+
if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(inst))
655+
{
656+
const Function* fun = call->getCalledFunction();
657+
return fun && isPtrTy &&
658+
extApi->is_alloc_stack_ret(pSet->getSVFFunction(fun));
659+
}
660+
else
661+
return false;
662+
}
663+
649664
/**
650665
* Check if a given value represents a heap object.
651666
*
@@ -670,6 +685,26 @@ bool LLVMUtil::isHeapObj(const Value* val)
670685
return false;
671686
}
672687

688+
/**
689+
* @param val The value to check.
690+
* @return True if the value represents a stack object, false otherwise.
691+
*/
692+
bool LLVMUtil::isStackObj(const Value* val)
693+
{
694+
if (SVFUtil::isa<AllocaInst>(val))
695+
{
696+
return true;
697+
}
698+
// Check if the value is an instruction and if it is a stack allocation external call
699+
else if (SVFUtil::isa<Instruction>(val) &&
700+
LLVMUtil::isStackAllocExtCall(SVFUtil::cast<Instruction>(val)))
701+
{
702+
return true;
703+
}
704+
// Return false if none of the above conditions are met
705+
return false;
706+
}
707+
673708
bool LLVMUtil::isNonInstricCallSite(const Instruction* inst)
674709
{
675710
bool res = false;

svf-llvm/lib/SVFIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void SVFIRBuilder::initialiseNodes()
282282
llvmModuleSet()->setValueAttr(llvmValue,pag->getGNode(iter->second));
283283
}
284284
// Check if the value is an alloca instruction and add a stack object node
285-
else if (SVFUtil::isa<AllocaInst>(llvmValue))
285+
else if (LLVMUtil::isStackObj(llvmValue))
286286
{
287287
const SVFFunction* f =
288288
SVFUtil::cast<SVFInstruction>(iter->first)->getFunction();

svf-llvm/lib/SymbolTableBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,13 @@ void SymbolTableBuilder::analyzeObjType(ObjTypeInfo* typeinfo, const Value* val)
724724

725725
/*!
726726
* Analyze byte size of heap alloc function (e.g. malloc/calloc/...)
727-
* 1) __attribute__((annotate("ALLOC_RET"), annotate("AllocSize:Arg0")))
727+
* 1) __attribute__((annotate("ALLOC_HEAP_RET"), annotate("AllocSize:Arg0")))
728728
void* safe_malloc(unsigned long size).
729729
Byte Size is the size(Arg0)
730-
2)__attribute__((annotate("ALLOC_RET"), annotate("AllocSize:Arg0*Arg1")))
730+
2)__attribute__((annotate("ALLOC_HEAP_RET"), annotate("AllocSize:Arg0*Arg1")))
731731
char* safecalloc(int a, int b)
732732
Byte Size is a(Arg0) * b(Arg1)
733-
3)__attribute__((annotate("ALLOC_RET"), annotate("UNKNOWN")))
733+
3)__attribute__((annotate("ALLOC_HEAP_RET"), annotate("UNKNOWN")))
734734
void* __sysv_signal(int a, void *b)
735735
Byte Size is Unknown
736736
If all required arg values are constant, byte Size is also constant,

0 commit comments

Comments
 (0)