Skip to content

Commit b59aaf7

Browse files
authored
[Sanitizers] Remove handling for lifetimes on non-alloca insts (NFC) (#149994)
After #149310 the pointer argument of lifetime.start/lifetime.end is guaranteed to be an alloca, so we don't need to go through findAllocaForValue() anymore, and don't have to have special handling for the case where it fails.
1 parent 1e24b53 commit b59aaf7

File tree

8 files changed

+6
-42
lines changed

8 files changed

+6
-42
lines changed

llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ struct AllocaInfo {
5757

5858
struct StackInfo {
5959
MapVector<AllocaInst *, AllocaInfo> AllocasToInstrument;
60-
SmallVector<Instruction *, 4> UnrecognizedLifetimes;
6160
SmallVector<Instruction *, 8> RetVec;
6261
bool CallsReturnTwice = false;
6362
};

llvm/lib/Analysis/StackLifetime.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ bool StackLifetime::isAliveAfter(const AllocaInst *AI,
6363
// markers has the same size and points to the alloca start.
6464
static const AllocaInst *findMatchingAlloca(const IntrinsicInst &II,
6565
const DataLayout &DL) {
66-
const AllocaInst *AI = findAllocaForValue(II.getArgOperand(1), true);
67-
if (!AI)
68-
return nullptr;
69-
66+
const AllocaInst *AI = cast<AllocaInst>(II.getArgOperand(1));
7067
auto AllocaSize = AI->getAllocationSize(DL);
7168
if (!AllocaSize)
7269
return nullptr;

llvm/lib/Target/AArch64/AArch64StackTagging.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
581581
// statement if return_twice functions are called.
582582
bool StandardLifetime =
583583
!SInfo.CallsReturnTwice &&
584-
SInfo.UnrecognizedLifetimes.empty() &&
585584
memtag::isStandardLifetime(Info.LifetimeStart, Info.LifetimeEnd, DT, LI,
586585
ClMaxLifetimes);
587586
if (StandardLifetime) {
@@ -616,10 +615,5 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
616615
memtag::annotateDebugRecords(Info, Tag);
617616
}
618617

619-
// If we have instrumented at least one alloca, all unrecognized lifetime
620-
// intrinsics have to go.
621-
for (auto *I : SInfo.UnrecognizedLifetimes)
622-
I->eraseFromParent();
623-
624618
return true;
625619
}

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,6 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
10631063
};
10641064
SmallVector<AllocaPoisonCall, 8> DynamicAllocaPoisonCallVec;
10651065
SmallVector<AllocaPoisonCall, 8> StaticAllocaPoisonCallVec;
1066-
bool HasUntracedLifetimeIntrinsic = false;
10671066

10681067
SmallVector<AllocaInst *, 1> DynamicAllocaVec;
10691068
SmallVector<IntrinsicInst *, 1> StackRestoreVec;
@@ -1097,14 +1096,6 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
10971096

10981097
initializeCallbacks(*F.getParent());
10991098

1100-
if (HasUntracedLifetimeIntrinsic) {
1101-
// If there are lifetime intrinsics which couldn't be traced back to an
1102-
// alloca, we may not know exactly when a variable enters scope, and
1103-
// therefore should "fail safe" by not poisoning them.
1104-
StaticAllocaPoisonCallVec.clear();
1105-
DynamicAllocaPoisonCallVec.clear();
1106-
}
1107-
11081099
processDynamicAllocas();
11091100
processStaticAllocas();
11101101

@@ -1231,13 +1222,7 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
12311222
!ConstantInt::isValueValidForType(IntptrTy, SizeValue))
12321223
return;
12331224
// Find alloca instruction that corresponds to llvm.lifetime argument.
1234-
// Currently we can only handle lifetime markers pointing to the
1235-
// beginning of the alloca.
1236-
AllocaInst *AI = findAllocaForValue(II.getArgOperand(1), true);
1237-
if (!AI) {
1238-
HasUntracedLifetimeIntrinsic = true;
1239-
return;
1240-
}
1225+
AllocaInst *AI = cast<AllocaInst>(II.getArgOperand(1));
12411226
// We're interested only in allocas we can handle.
12421227
if (!ASan.isInterestingAlloca(*AI))
12431228
return;

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,6 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
15001500
// statement if return_twice functions are called.
15011501
bool StandardLifetime =
15021502
!SInfo.CallsReturnTwice &&
1503-
SInfo.UnrecognizedLifetimes.empty() &&
15041503
memtag::isStandardLifetime(Info.LifetimeStart, Info.LifetimeEnd, &DT,
15051504
&LI, ClMaxLifetimes);
15061505
if (DetectUseAfterScope && StandardLifetime) {
@@ -1525,8 +1524,6 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
15251524
}
15261525
memtag::alignAndPadAlloca(Info, Mapping.getObjectAlignment());
15271526
}
1528-
for (auto &I : SInfo.UnrecognizedLifetimes)
1529-
I->eraseFromParent();
15301527
return true;
15311528
}
15321529

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
12161216
};
12171217
SmallVector<ShadowOriginAndInsertPoint, 16> InstrumentationList;
12181218
DenseMap<const DILocation *, int> LazyWarningDebugLocationCount;
1219-
bool InstrumentLifetimeStart = ClHandleLifetimeIntrinsics;
12201219
SmallSetVector<AllocaInst *, 16> AllocaSet;
12211220
SmallVector<std::pair<IntrinsicInst *, AllocaInst *>, 16> LifetimeStartList;
12221221
SmallVector<StoreInst *, 16> StoreList;
@@ -1623,7 +1622,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
16231622

16241623
// Poison llvm.lifetime.start intrinsics, if we haven't fallen back to
16251624
// instrumenting only allocas.
1626-
if (InstrumentLifetimeStart) {
1625+
if (ClHandleLifetimeIntrinsics) {
16271626
for (auto Item : LifetimeStartList) {
16281627
instrumentAlloca(*Item.second, Item.first);
16291628
AllocaSet.remove(Item.second);
@@ -3303,9 +3302,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
33033302
void handleLifetimeStart(IntrinsicInst &I) {
33043303
if (!PoisonStack)
33053304
return;
3306-
AllocaInst *AI = llvm::findAllocaForValue(I.getArgOperand(1));
3307-
if (!AI)
3308-
InstrumentLifetimeStart = false;
3305+
AllocaInst *AI = cast<AllocaInst>(I.getArgOperand(1));
33093306
LifetimeStartList.push_back(std::make_pair(&I, AI));
33103307
}
33113308

llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ static bool markTails(Function &F, OptimizationRemarkEmitter *ORE) {
343343
///
344344
static bool canMoveAboveCall(Instruction *I, CallInst *CI, AliasAnalysis *AA) {
345345
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
346-
if (II->getIntrinsicID() == Intrinsic::lifetime_end &&
347-
llvm::findAllocaForValue(II->getArgOperand(1)))
346+
if (II->getIntrinsicID() == Intrinsic::lifetime_end)
348347
return true;
349348

350349
// FIXME: We can move load/store/call/free instructions above the call if the

llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ void StackInfoBuilder::visit(OptimizationRemarkEmitter &ORE,
155155
return;
156156
}
157157
if (auto *II = dyn_cast<LifetimeIntrinsic>(&Inst)) {
158-
AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
159-
if (!AI) {
160-
Info.UnrecognizedLifetimes.push_back(&Inst);
161-
return;
162-
}
158+
AllocaInst *AI = cast<AllocaInst>(II->getArgOperand(1));
163159
if (getAllocaInterestingness(*AI) != AllocaInterestingness::kInteresting)
164160
return;
165161
if (II->getIntrinsicID() == Intrinsic::lifetime_start)

0 commit comments

Comments
 (0)