Skip to content

Commit 892e6cd

Browse files
committed
[asan] fix false dynamic-stack-buffer-overflow report with constantly-sized dynamic allocas, LLVM part
See the bug report at google/sanitizers#691. When a dynamic alloca has a constant size, ASan instrumentation will treat it as a regular dynamic alloca (insert calls to poison and unpoison), but the backend will turn it into a regular stack variable. The poisoning/unpoisoning is then broken. This patch will treat such allocas as static. Differential Revision: http://reviews.llvm.org/D21509 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273888 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 59ca41f commit 892e6cd

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,20 @@ struct AddressSanitizer : public FunctionPass {
417417
AU.addRequired<TargetLibraryInfoWrapperPass>();
418418
}
419419
uint64_t getAllocaSizeInBytes(AllocaInst *AI) const {
420+
uint64_t ArraySize = 1;
421+
if (AI->isArrayAllocation()) {
422+
ConstantInt *CI = dyn_cast<ConstantInt>(AI->getArraySize());
423+
assert(CI && "non-constant array size");
424+
ArraySize = CI->getZExtValue();
425+
}
420426
Type *Ty = AI->getAllocatedType();
421427
uint64_t SizeInBytes =
422428
AI->getModule()->getDataLayout().getTypeAllocSize(Ty);
423-
return SizeInBytes;
429+
return SizeInBytes * ArraySize;
424430
}
425431
/// Check if we want (and can) handle this alloca.
426432
bool isInterestingAlloca(AllocaInst &AI);
427433

428-
// Check if we have dynamic alloca.
429-
bool isDynamicAlloca(AllocaInst &AI) const {
430-
return AI.isArrayAllocation() || !AI.isStaticAlloca();
431-
}
432-
433434
/// If it is an interesting memory access, return the PointerOperand
434435
/// and set IsWrite/Alignment. Otherwise return nullptr.
435436
Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite,
@@ -680,7 +681,7 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
680681
}
681682

682683
StackAlignment = std::max(StackAlignment, AI.getAlignment());
683-
if (ASan.isDynamicAlloca(AI))
684+
if (!AI.isStaticAlloca())
684685
DynamicAllocaVec.push_back(&AI);
685686
else
686687
AllocaVec.push_back(&AI);
@@ -858,7 +859,7 @@ bool AddressSanitizer::isInterestingAlloca(AllocaInst &AI) {
858859
bool IsInteresting =
859860
(AI.getAllocatedType()->isSized() &&
860861
// alloca() may be called with 0 size, ignore it.
861-
getAllocaSizeInBytes(&AI) > 0 &&
862+
((!AI.isStaticAlloca()) || getAllocaSizeInBytes(&AI) > 0) &&
862863
// We are only interested in allocas not promotable to registers.
863864
// Promotable allocas are common under -O0.
864865
(!ClSkipPromotableAllocas || !isAllocaPromotable(&AI)) &&

0 commit comments

Comments
 (0)