[llvm] [MTE] fix bug that prevented stack coloring with MTE (PR #84422)
Florian Mayer via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 8 11:29:49 PST 2024
https://github.com/fmayer updated https://github.com/llvm/llvm-project/pull/84422
>From a1c0ad83a3f6227c8b969a1dcbe4a4b9f76ff2ae Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Thu, 7 Mar 2024 18:15:57 -0800
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
---
.../llvm/Transforms/Utils/MemoryTaggingSupport.h | 1 +
llvm/lib/Target/AArch64/AArch64StackTagging.cpp | 5 ++++-
.../Instrumentation/HWAddressSanitizer.cpp | 8 ++------
.../Transforms/Utils/MemoryTaggingSupport.cpp | 5 +++++
.../AArch64/stack-tagging-stack-coloring.ll | 16 ++++++++--------
5 files changed, 20 insertions(+), 15 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
index eb00e6c4e856df..df61f60de4f261 100644
--- a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
+++ b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
@@ -78,6 +78,7 @@ class StackInfoBuilder {
uint64_t getAllocaSizeInBytes(const AllocaInst &AI);
void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align);
+bool isLifetimeIntrinsic(Value *V);
} // namespace memtag
} // namespace llvm
diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index ef7c517732ef3f..53418694bf26c1 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -533,7 +533,10 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
if (Info.AI->hasName())
TagPCall->setName(Info.AI->getName() + ".tag");
// Does not replace metadata, so we don't have to handle DPValues.
- Info.AI->replaceNonMetadataUsesWith(TagPCall);
+ Info.AI->replaceUsesWithIf(TagPCall, [](const Use &U) {
+ auto *User = U.getUser();
+ return !memtag::isLifetimeIntrinsic(User);
+ });
TagPCall->setOperand(0, Info.AI);
// Calls to functions that may return twice (e.g. setjmp) confuse the
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 6bae679e11be23..88553d49b1b5e9 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1391,11 +1391,6 @@ bool HWAddressSanitizer::instrumentLandingPads(
return true;
}
-static bool isLifetimeIntrinsic(Value *V) {
- auto *II = dyn_cast<IntrinsicInst>(V);
- return II && II->isLifetimeStartOrEnd();
-}
-
static DbgAssignIntrinsic *DynCastToDbgAssign(DbgVariableIntrinsic *DVI) {
return dyn_cast<DbgAssignIntrinsic>(DVI);
}
@@ -1455,7 +1450,8 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
AI->replaceUsesWithIf(Replacement, [AICast, AILong](const Use &U) {
auto *User = U.getUser();
- return User != AILong && User != AICast && !isLifetimeIntrinsic(User);
+ return User != AILong && User != AICast &&
+ !memtag::isLifetimeIntrinsic(User);
});
// Helper utility for adding DW_OP_LLVM_tag_offset to debug-info records,
diff --git a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
index bfe474d8204578..f4b9b155827a99 100644
--- a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
+++ b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
@@ -236,5 +236,10 @@ void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
Info.AI = NewAI;
}
+bool isLifetimeIntrinsic(Value *V) {
+ auto *II = dyn_cast<IntrinsicInst>(V);
+ return II && II->isLifetimeStartOrEnd();
+}
+
} // namespace memtag
} // namespace llvm
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-stack-coloring.ll b/llvm/test/CodeGen/AArch64/stack-tagging-stack-coloring.ll
index 6eb72013fb0ed0..81349620fb7725 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-stack-coloring.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-stack-coloring.ll
@@ -1,20 +1,20 @@
; Test that storage for allocas with disjoint lifetimes is reused with stack
; tagging.
-; RUN: opt -S -aarch64-stack-tagging %s -o - | \
-; RUN: llc -no-stack-coloring=false -o - | \
+; RUN: opt -S -aarch64-stack-tagging -stack-tagging-use-stack-safety=0 %s -o - | \
+; RUN: llc --mattr=+mte -no-stack-coloring=false -o - | \
; RUN: FileCheck %s --check-prefix=COLOR
-; RUN: opt -S -aarch64-stack-tagging %s -o - | \
-; RUN: llc -no-stack-coloring=true -o - | \
+; RUN: opt -S -aarch64-stack-tagging %s -stack-tagging-use-stack-safety=0 -o - | \
+; RUN: llc --mattr=+mte -no-stack-coloring=true -o - | \
; RUN: FileCheck %s --check-prefix=NOCOLOR
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
-target triple = "aarch64-unknown-linux-android29"
+target triple = "aarch64"
-; COLOR: sub sp, sp, #192
-; NOCOLOR: sub sp, sp, #320
+; COLOR: sub sp, sp, #208
+; NOCOLOR: sub sp, sp, #336
-define i32 @myCall_w2(i32 %in) sanitize_hwaddress {
+define i32 @myCall_w2(i32 %in) sanitize_memtag {
entry:
%a = alloca [17 x ptr], align 8
%a2 = alloca [16 x ptr], align 8
>From e9337b82a61bcb0235baeccba0ced3c72f68e353 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Fri, 8 Mar 2024 11:29:37 -0800
Subject: [PATCH 2/2] update
Created using spr 1.3.4
---
llvm/lib/Target/AArch64/AArch64StackTagging.cpp | 7 +++----
.../CodeGen/AArch64/stack-tagging-initializer-merge.ll | 4 ++--
llvm/test/CodeGen/AArch64/stack-tagging-untag-placement.ll | 2 +-
3 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index 53418694bf26c1..f2812d2b49bc65 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -533,9 +533,8 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
if (Info.AI->hasName())
TagPCall->setName(Info.AI->getName() + ".tag");
// Does not replace metadata, so we don't have to handle DPValues.
- Info.AI->replaceUsesWithIf(TagPCall, [](const Use &U) {
- auto *User = U.getUser();
- return !memtag::isLifetimeIntrinsic(User);
+ Info.AI->replaceUsesWithIf(TagPCall, [&](const Use &U) {
+ return !memtag::isLifetimeIntrinsic(U.getUser());
});
TagPCall->setOperand(0, Info.AI);
@@ -553,7 +552,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
uint64_t Size =
cast<ConstantInt>(Start->getArgOperand(0))->getZExtValue();
Size = alignTo(Size, kTagGranuleSize);
- tagAlloca(AI, Start->getNextNode(), Start->getArgOperand(1), Size);
+ tagAlloca(AI, Start->getNextNode(), TagPCall, Size);
auto TagEnd = [&](Instruction *Node) { untagAlloca(AI, Node, Size); };
if (!DT || !PDT ||
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-initializer-merge.ll b/llvm/test/CodeGen/AArch64/stack-tagging-initializer-merge.ll
index d8969fc9bebdbf..22d177ca3267e9 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-initializer-merge.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-initializer-merge.ll
@@ -20,10 +20,10 @@ entry:
; CHECK-LABEL: define void @OneVarNoInit(
; CHECK-DAG: [[X:%.*]] = alloca { i32, [12 x i8] }, align 16
; CHECK-DAG: [[TX:%.*]] = call ptr @llvm.aarch64.tagp.{{.*}}(ptr [[X]], {{.*}}, i64 0)
-; CHECK-DAG: call void @llvm.lifetime.start.p0(i64 4, ptr nonnull [[TX]])
+; CHECK-DAG: call void @llvm.lifetime.start.p0(i64 4, ptr nonnull [[X]])
; CHECK-DAG: call void @llvm.aarch64.settag(ptr [[TX]], i64 16)
; CHECK-DAG: call void @use(ptr nonnull [[TX]])
-; CHECK-DAG: call void @llvm.lifetime.end.p0(i64 4, ptr nonnull [[TX]])
+; CHECK-DAG: call void @llvm.lifetime.end.p0(i64 4, ptr nonnull [[X]])
define void @OneVarInitConst() sanitize_memtag {
entry:
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-untag-placement.ll b/llvm/test/CodeGen/AArch64/stack-tagging-untag-placement.ll
index 06f8cd5241ebfb..aa9cccc58712da 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-untag-placement.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-untag-placement.ll
@@ -27,7 +27,7 @@ S1:
; CHECK: call void @llvm.aarch64.settag(ptr %w, i64 48)
; CHECK-NOT: settag{{.*}}%v
call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %w) #1
-; CHECK: call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %w.tag)
+; CHECK: call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %w)
%b1 = icmp eq i32 %t1, 0
br i1 %b1, label %S2, label %S3
; CHECK-NOT: settag
More information about the llvm-commits
mailing list