diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp --- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp +++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp @@ -334,6 +334,8 @@ AU.addRequired(); if (MergeInit) AU.addRequired(); + AU.addRequired(); + AU.addRequired(); } }; @@ -345,6 +347,8 @@ false, false) INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) INITIALIZE_PASS_DEPENDENCY(StackSafetyGlobalInfoWrapperPass) +INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) +INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) INITIALIZE_PASS_END(AArch64StackTagging, DEBUG_TYPE, "AArch64 Stack Tagging", false, false) @@ -503,31 +507,14 @@ if (SInfo.AllocasToInstrument.empty()) return false; - std::unique_ptr DeleteDT; - DominatorTree *DT = nullptr; - if (auto *P = getAnalysisIfAvailable()) - DT = &P->getDomTree(); - - if (DT == nullptr && (SInfo.AllocasToInstrument.size() > 1 || - !F->hasFnAttribute(Attribute::OptimizeNone))) { - DeleteDT = std::make_unique(*F); - DT = DeleteDT.get(); - } - - std::unique_ptr DeletePDT; - PostDominatorTree *PDT = nullptr; - if (auto *P = getAnalysisIfAvailable()) - PDT = &P->getPostDomTree(); - - if (PDT == nullptr && !F->hasFnAttribute(Attribute::OptimizeNone)) { - DeletePDT = std::make_unique(*F); - PDT = DeletePDT.get(); - } + DominatorTree &DT = getAnalysis().getDomTree(); + PostDominatorTree &PDT = + getAnalysis().getPostDomTree(); SetTagFunc = Intrinsic::getDeclaration(F->getParent(), Intrinsic::aarch64_settag); - Instruction *Base = insertBaseTaggedPointer(SInfo.AllocasToInstrument, DT); + Instruction *Base = insertBaseTaggedPointer(SInfo.AllocasToInstrument, &DT); int NextTag = 0; for (auto &I : SInfo.AllocasToInstrument) { @@ -552,7 +539,7 @@ bool StandardLifetime = SInfo.UnrecognizedLifetimes.empty() && - memtag::isStandardLifetime(Info.LifetimeStart, Info.LifetimeEnd, DT, + memtag::isStandardLifetime(Info.LifetimeStart, Info.LifetimeEnd, &DT, ClMaxLifetimes); // Calls to functions that may return twice (e.g. setjmp) confuse the // postdominator analysis, and will leave us to keep memory tagged after @@ -567,8 +554,7 @@ tagAlloca(AI, Start->getNextNode(), Start->getArgOperand(1), Size); auto TagEnd = [&](Instruction *Node) { untagAlloca(AI, Node, Size); }; - if (!DT || !PDT || - !memtag::forAllReachableExits(*DT, *PDT, Start, Info.LifetimeEnd, + if (!memtag::forAllReachableExits(DT, PDT, Start, Info.LifetimeEnd, SInfo.RetVec, TagEnd)) { for (auto *End : Info.LifetimeEnd) End->eraseFromParent(); diff --git a/llvm/test/CodeGen/AArch64/stack-tagging.ll b/llvm/test/CodeGen/AArch64/stack-tagging.ll --- a/llvm/test/CodeGen/AArch64/stack-tagging.ll +++ b/llvm/test/CodeGen/AArch64/stack-tagging.ll @@ -102,6 +102,34 @@ ; CHECK: ret void +define void @ScopeOptnone(i32 %b) sanitize_memtag optnone noinline { +entry: + %x = alloca i32, align 4 + %tobool = icmp eq i32 %b, 0 + br i1 %tobool, label %if.end, label %if.then + +if.then: + %0 = bitcast i32* %x to i8* + call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0) + call void @use8(i8* %0) #3 + call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %0) + br label %if.end + +if.end: + ret void +} + +; CHECK-LABEL: define void @ScopeOptnone( +; CHECK: br i1 +; CHECK: call void @llvm.lifetime.start.p0i8( +; CHECK: call void @llvm.aarch64.settag( +; CHECK: call void @use8( +; CHECK: call void @llvm.aarch64.settag( +; CHECK: call void @llvm.lifetime.end.p0i8( +; CHECK: br label +; CHECK: ret void + + ; Spooked by the multiple lifetime ranges, StackTagging remove all of them and sets tags on entry and exit. define void @BadScope(i32 %b) sanitize_memtag { entry: