[llvm] [hwasan] Allow stack traces even when fixed shadow is used (PR #109344)

Thurston Dang via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 20 15:44:14 PDT 2024


https://github.com/thurstond updated https://github.com/llvm/llvm-project/pull/109344

>From 934770dc4d978e57620ac539d473ca422b3a501a Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 19 Sep 2024 22:39:11 +0000
Subject: [PATCH 1/6] [hwasan] Allow stack traces even when fixed shadow is
 used

Previously, fixed shadow implied !InTls, and !InTls implied no stack traces, but InTls implied it would not use a fixed shadow.
This patch changes fixed shadow to be compatible with stack traces.

It maintains the legacy behavior for KHWAsan || InstrumentWithCalls.
---
 .../Instrumentation/HWAddressSanitizer.cpp      | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 7a5c690c7ea512..4bdf5b97aeb704 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1340,7 +1340,7 @@ Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
 }
 
 void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
-  if (!Mapping.InTls)
+  if (!Mapping.InTls || (Mapping.Offset != 0 && Mapping.Offset != kDynamicShadowSentinel))
     ShadowBase = getShadowNonTls(IRB);
   else if (!WithFrameRecord && TargetTriple.isAndroid())
     ShadowBase = getDynamicShadowIfunc(IRB);
@@ -1897,16 +1897,19 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
     InTls = false;
     Offset = 0;
     WithFrameRecord = true;
-  } else if (ClMappingOffset.getNumOccurrences() > 0) {
-    InGlobal = false;
-    InTls = false;
-    Offset = ClMappingOffset;
-    WithFrameRecord = false;
   } else if (ClEnableKhwasan || InstrumentWithCalls) {
     InGlobal = false;
     InTls = false;
-    Offset = 0;
+    if (ClMappingOffset.getNumOccurrences() > 0)
+      Offset = ClMappingOffset;
+    else
+      Offset = 0;
     WithFrameRecord = false;
+  } else if (ClMappingOffset.getNumOccurrences() > 0) {
+    InGlobal = false;
+    InTls = true;
+    Offset = ClMappingOffset;
+    WithFrameRecord = true;
   } else if (ClWithIfunc) {
     InGlobal = true;
     InTls = false;

>From e3526e363b8f035967bd04763af4d267577c664f Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 19 Sep 2024 22:49:10 +0000
Subject: [PATCH 2/6] clang-format

---
 llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 4bdf5b97aeb704..15e9f729292bee 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1340,7 +1340,8 @@ Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
 }
 
 void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
-  if (!Mapping.InTls || (Mapping.Offset != 0 && Mapping.Offset != kDynamicShadowSentinel))
+  if (!Mapping.InTls ||
+      (Mapping.Offset != 0 && Mapping.Offset != kDynamicShadowSentinel))
     ShadowBase = getShadowNonTls(IRB);
   else if (!WithFrameRecord && TargetTriple.isAndroid())
     ShadowBase = getDynamicShadowIfunc(IRB);

>From ad9acd0085a9166bda420ed6aeba6dcadac899fb Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 19 Sep 2024 23:00:55 +0000
Subject: [PATCH 3/6] Use optOr per Florian1

---
 llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 15e9f729292bee..4c4ad7ee9e7462 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1901,10 +1901,7 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
   } else if (ClEnableKhwasan || InstrumentWithCalls) {
     InGlobal = false;
     InTls = false;
-    if (ClMappingOffset.getNumOccurrences() > 0)
-      Offset = ClMappingOffset;
-    else
-      Offset = 0;
+    Offset = optOr(ClMappingOffset, (unsigned long)0);
     WithFrameRecord = false;
   } else if (ClMappingOffset.getNumOccurrences() > 0) {
     InGlobal = false;
@@ -1928,3 +1925,4 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
     WithFrameRecord = false;
   }
 }
+

>From ede736e7adec287b9a145d959a0f014ec056733a Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Fri, 20 Sep 2024 03:49:46 +0000
Subject: [PATCH 4/6] Avoid cast

---
 llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 4c4ad7ee9e7462..ca1d91341891a8 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1901,7 +1901,7 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
   } else if (ClEnableKhwasan || InstrumentWithCalls) {
     InGlobal = false;
     InTls = false;
-    Offset = optOr(ClMappingOffset, (unsigned long)0);
+    Offset = optOr(ClMappingOffset, 0UL);
     WithFrameRecord = false;
   } else if (ClMappingOffset.getNumOccurrences() > 0) {
     InGlobal = false;

>From 22a761282ae9247a14673a696a2552d18abd38bd Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Fri, 20 Sep 2024 03:59:57 +0000
Subject: [PATCH 5/6] clang-format

---
 llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index ca1d91341891a8..22d9fb32b06123 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1925,4 +1925,3 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
     WithFrameRecord = false;
   }
 }
-

>From dbc05f56c4757c5fc08b0670f8ecb9f5a0ef6180 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Fri, 20 Sep 2024 22:43:33 +0000
Subject: [PATCH 6/6] Use uint64_t to (hopefully) avoid compiler warning

---
 llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 22d9fb32b06123..20f99cf484ce16 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1901,7 +1901,7 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
   } else if (ClEnableKhwasan || InstrumentWithCalls) {
     InGlobal = false;
     InTls = false;
-    Offset = optOr(ClMappingOffset, 0UL);
+    Offset = optOr<uint64_t>(ClMappingOffset, (uint64_t)0);
     WithFrameRecord = false;
   } else if (ClMappingOffset.getNumOccurrences() > 0) {
     InGlobal = false;



More information about the llvm-commits mailing list