[compiler-rt] [llvm] Msan zero alloc test (PR #155934)

Thurston Dang via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 28 15:17:32 PDT 2025


https://github.com/thurstond created https://github.com/llvm/llvm-project/pull/155934

None

>From 6d24167141f855658ea7003b4ba91b2148512123 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 21 Aug 2025 17:33:39 +0000
Subject: [PATCH 1/3] [hwasan] Port "[Asan] Skip pre-split coroutine and noop
 coroutine frame (#99415)"

Originally suggested by rnk@
(this is the simplified function-level skip version, to unblock builds ASAP)
---
 llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index fc34d14259d1f..7457565269050 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1574,6 +1574,9 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
   if (F.empty())
     return;
 
+  if(F.isPresplitCoroutine())
+    return;
+
   NumTotalFuncs++;
 
   OptimizationRemarkEmitter &ORE =

>From 9f3451515fc13c594e269ee9516e3a355357ad19 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 21 Aug 2025 17:44:09 +0000
Subject: [PATCH 2/3] clang-format

---
 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 7457565269050..66cdbfcf998c6 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1574,7 +1574,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
   if (F.empty())
     return;
 
-  if(F.isPresplitCoroutine())
+  if (F.isPresplitCoroutine())
     return;
 
   NumTotalFuncs++;

>From 132129a2508d560f670b5c2f8e00835b9e08b153 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 28 Aug 2025 22:12:49 +0000
Subject: [PATCH 3/3] [msan] Add test for deferencing zero-sized malloc/calloc

MSan fails to catch this, because 0-byte allocations are converted into
1-byte allocations.

Bug originally reported by dvyukov
---
 compiler-rt/test/msan/zero_alloc.cpp | 37 ++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 compiler-rt/test/msan/zero_alloc.cpp

diff --git a/compiler-rt/test/msan/zero_alloc.cpp b/compiler-rt/test/msan/zero_alloc.cpp
new file mode 100644
index 0000000000000..4b60c161efb36
--- /dev/null
+++ b/compiler-rt/test/msan/zero_alloc.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_msan -Wno-alloc-size -fsanitize-recover=memory %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// XFAIL: *
+
+#include <malloc.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  {
+     char* p1 = (char*)calloc(1, 0);
+     printf ("p1 is %p\n", p1);
+     printf ("Content of p1 is: %d\n", *p1);
+     // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
+     // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
+     free(p1);
+  }
+
+  {
+     char* p2 = (char*)calloc(0, 1);
+     printf ("p2 is %p\n", p2);
+     printf ("Content of p2 is: %d\n", *p2);
+     // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
+     // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
+     free(p2);
+  }
+
+  {
+     char* p3 = (char*)malloc(0);
+     printf ("p3 is %p\n", p3);
+     printf ("Content of p2 is: %d\n", *p3);
+     // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
+     // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
+     free(p3);
+  }
+
+  return 0;
+}



More information about the llvm-commits mailing list