[llvm] 8de457e - [HWASAN] use common alignAndPadAlloca
Florian Mayer via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 14 15:28:41 PST 2022
Author: Florian Mayer
Date: 2022-02-14T15:28:32-08:00
New Revision: 8de457eafc08f6f05dc70ecf0f3d461e35ba478f
URL: https://github.com/llvm/llvm-project/commit/8de457eafc08f6f05dc70ecf0f3d461e35ba478f
DIFF: https://github.com/llvm/llvm-project/commit/8de457eafc08f6f05dc70ecf0f3d461e35ba478f.diff
LOG: [HWASAN] use common alignAndPadAlloca
Reviewed By: eugenis
Differential Revision: https://reviews.llvm.org/D119614
Added:
Modified:
llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
llvm/lib/Target/AArch64/AArch64StackTagging.cpp
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
index a6218136ce890..c00d2c6445a9c 100644
--- a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
+++ b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
@@ -102,7 +102,7 @@ class StackInfoBuilder {
};
uint64_t getAllocaSizeInBytes(const AllocaInst &AI);
-void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align);
+bool alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align);
} // namespace memtag
} // namespace llvm
diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index 88315869a6ffc..11b34b1bf90ad 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -505,7 +505,9 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
for (auto &I : SInfo.AllocasToInstrument) {
memtag::AllocaInfo &Info = I.second;
assert(Info.AI && isInterestingAlloca(*Info.AI));
- memtag::alignAndPadAlloca(Info, kTagGranuleSize);
+ auto *PrevAI = Info.AI;
+ if (memtag::alignAndPadAlloca(Info, kTagGranuleSize))
+ PrevAI->eraseFromParent();
}
std::unique_ptr<DominatorTree> DeleteDT;
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 57197295f0340..1c54955d34523 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -261,8 +261,6 @@ class HWAddressSanitizer {
void setSSI(const StackSafetyGlobalInfo *S) { SSI = S; }
- DenseMap<AllocaInst *, AllocaInst *> padInterestingAllocas(
- const MapVector<AllocaInst *, memtag::AllocaInfo> &AllocasToInstrument);
bool sanitizeFunction(Function &F,
llvm::function_ref<const DominatorTree &()> GetDT,
llvm::function_ref<const PostDominatorTree &()> GetPDT);
@@ -1380,6 +1378,19 @@ bool HWAddressSanitizer::instrumentStack(
II->eraseFromParent();
}
}
+ if (memtag::alignAndPadAlloca(Info, Align(Mapping.getObjectAlignment()))) {
+ for (auto DVI : Info.DbgVariableIntrinsics) {
+ SmallDenseSet<Value *> LocationOps(DVI->location_ops().begin(),
+ DVI->location_ops().end());
+ for (Value *V : LocationOps) {
+ if (auto *AI = dyn_cast_or_null<AllocaInst>(V)) {
+ if (V == AI)
+ DVI->replaceVariableLocationOp(V, Info.AI);
+ }
+ }
+ }
+ AI->eraseFromParent();
+ }
}
for (auto &I : SInfo.UnrecognizedLifetimes)
I->eraseFromParent();
@@ -1404,39 +1415,6 @@ bool HWAddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
!(SSI && SSI->isSafe(AI));
}
-DenseMap<AllocaInst *, AllocaInst *> HWAddressSanitizer::padInterestingAllocas(
- const MapVector<AllocaInst *, memtag::AllocaInfo> &AllocasToInstrument) {
- DenseMap<AllocaInst *, AllocaInst *> AllocaToPaddedAllocaMap;
- for (auto &KV : AllocasToInstrument) {
- AllocaInst *AI = KV.first;
- uint64_t Size = memtag::getAllocaSizeInBytes(*AI);
- uint64_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment());
- AI->setAlignment(
- Align(std::max(AI->getAlignment(), Mapping.getObjectAlignment())));
- if (Size != AlignedSize) {
- Type *AllocatedType = AI->getAllocatedType();
- if (AI->isArrayAllocation()) {
- uint64_t ArraySize =
- cast<ConstantInt>(AI->getArraySize())->getZExtValue();
- AllocatedType = ArrayType::get(AllocatedType, ArraySize);
- }
- Type *TypeWithPadding = StructType::get(
- AllocatedType, ArrayType::get(Int8Ty, AlignedSize - Size));
- auto *NewAI = new AllocaInst(
- TypeWithPadding, AI->getType()->getAddressSpace(), nullptr, "", AI);
- NewAI->takeName(AI);
- NewAI->setAlignment(AI->getAlign());
- NewAI->setUsedWithInAlloca(AI->isUsedWithInAlloca());
- NewAI->setSwiftError(AI->isSwiftError());
- NewAI->copyMetadata(*AI);
- auto *Bitcast = new BitCastInst(NewAI, AI->getType(), "", AI);
- AI->replaceAllUsesWith(Bitcast);
- AllocaToPaddedAllocaMap[AI] = NewAI;
- }
- }
- return AllocaToPaddedAllocaMap;
-}
-
bool HWAddressSanitizer::sanitizeFunction(
Function &F, llvm::function_ref<const DominatorTree &()> GetDT,
llvm::function_ref<const PostDominatorTree &()> GetPDT) {
@@ -1509,28 +1487,6 @@ bool HWAddressSanitizer::sanitizeFunction(
instrumentStack(DetectUseAfterScope && !SInfo.CallsReturnTwice, SIB.get(),
StackTag, GetDT, GetPDT);
}
- // Pad and align each of the allocas that we instrumented to stop small
- // uninteresting allocas from hiding in instrumented alloca's padding and so
- // that we have enough space to store real tags for short granules.
- DenseMap<AllocaInst *, AllocaInst *> AllocaToPaddedAllocaMap =
- padInterestingAllocas(SInfo.AllocasToInstrument);
-
- if (!AllocaToPaddedAllocaMap.empty()) {
- for (auto &Inst : instructions(F)) {
- if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
- SmallDenseSet<Value *> LocationOps(DVI->location_ops().begin(),
- DVI->location_ops().end());
- for (Value *V : LocationOps) {
- if (auto *AI = dyn_cast_or_null<AllocaInst>(V)) {
- if (auto *NewAI = AllocaToPaddedAllocaMap.lookup(AI))
- DVI->replaceVariableLocationOp(V, NewAI);
- }
- }
- }
- }
- for (auto &P : AllocaToPaddedAllocaMap)
- P.first->eraseFromParent();
- }
// If we split the entry block, move any allocas that were originally in the
// entry block back into the entry block so that they aren't treated as
diff --git a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
index da77457f42fc3..602a2fe4ed3cd 100644
--- a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
+++ b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
@@ -109,7 +109,7 @@ uint64_t getAllocaSizeInBytes(const AllocaInst &AI) {
return AI.getAllocationSizeInBits(DL).getValue() / 8;
}
-void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
+bool alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
const Align NewAlignment = max(MaybeAlign(Info.AI->getAlign()), Alignment);
Info.AI->setAlignment(NewAlignment);
auto &Ctx = Info.AI->getFunction()->getContext();
@@ -117,7 +117,7 @@ void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
uint64_t Size = getAllocaSizeInBytes(*Info.AI);
uint64_t AlignedSize = alignTo(Size, Alignment);
if (Size == AlignedSize)
- return;
+ return false;
// Add padding to the alloca.
Type *AllocatedType =
@@ -139,8 +139,8 @@ void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
auto *NewPtr = new BitCastInst(NewAI, Info.AI->getType(), "", Info.AI);
Info.AI->replaceAllUsesWith(NewPtr);
- Info.AI->eraseFromParent();
Info.AI = NewAI;
+ return true;
}
} // namespace memtag
More information about the llvm-commits
mailing list