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 "ALLOC_STACK_RET" annotation in extapi.c #1616

Merged
merged 2 commits into from
Dec 20, 2024
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: 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 @@
return isHeapAllocExtCallViaRet(inst) || isHeapAllocExtCallViaArg(inst);
}

bool isStackAllocExtCallViaRet(const Instruction *inst);

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

Check warning on line 367 in svf-llvm/include/SVF-LLVM/LLVMUtil.h

View check run for this annotation

Codecov / codecov/patch

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

Added line #L367 was not covered by tests
}

// 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::isStackAllocExtCallViaRet(const Instruction *inst)

Check warning on line 649 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L649

Added line #L649 was not covered by tests
{
LLVMModuleSet* pSet = LLVMModuleSet::getLLVMModuleSet();
ExtAPI* extApi = ExtAPI::getExtAPI();

Check warning on line 652 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L651-L652

Added lines #L651 - L652 were not covered by tests
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));

Check warning on line 658 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L656-L658

Added lines #L656 - L658 were not covered by tests
}
else
return false;
}

/**
* Check if a given value represents a heap object.
*
Expand All @@ -670,6 +685,26 @@
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)))

Check warning on line 700 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L700

Added line #L700 was not covered by tests
{
return true;

Check warning on line 702 in svf-llvm/lib/LLVMUtil.cpp

View check run for this annotation

Codecov / codecov/patch

svf-llvm/lib/LLVMUtil.cpp#L702

Added line #L702 was not covered by tests
}
// 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
Loading