[compiler-rt] [rtsan] Intercept aligned_alloc on all versions of OSX if available on build machine (PR #112780)

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 12:38:12 PDT 2024


https://github.com/cjappl updated https://github.com/llvm/llvm-project/pull/112780

>From 0a01c4cb8c0858e739a806972641c6f4b5d78092 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Thu, 17 Oct 2024 14:32:15 -0700
Subject: [PATCH 1/5] [rtsan] Intercept aligned_alloc on all versions of OSX if
 available on the build machine

---
 compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp | 12 +++++++++++-
 .../rtsan/tests/rtsan_test_interceptors_posix.cpp  | 14 +++++++++-----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 63b0ca28a1f409..6b325c028e425f 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -461,7 +461,17 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
   return REAL(valloc)(size);
 }
 
-#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
+// aligned_alloc was introduced in OSX 10.15
+// Linking will fail when using an older SDK
+#if defined(__MAC_10_15)
+// macOS 10.15 is greater than our minimal deployment target.  To ensure we
+// generate a weak reference so the dylib continues to work on older
+// systems, we need to forward declare the intercepted function as "weak
+// imports".
+SANITIZER_WEAK_IMPORT void *aligned_alloc(SIZE_T __alignment, SIZE_T __size);
+#endif // defined(__MAC_10_15)
+
+#if SANITIZER_INTERCEPT_ALIGNED_ALLOC || SANITIZER_APPLE
 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
   __rtsan_notify_intercepted_call("aligned_alloc");
   return REAL(aligned_alloc)(alignment, size);
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index f7a281f13e2ff6..fb227a1d1bc05a 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -120,13 +120,17 @@ TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
   ExpectNonRealtimeSurvival(Func);
 }
 
-#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
 TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
-  auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
-  ExpectRealtimeDeath(Func, "aligned_alloc");
-  ExpectNonRealtimeSurvival(Func);
-}
+#if SANITIZER_APPLE
+  if (__builtin_available(macOS 10.15, *)) {
+#endif // SANITIZER_APPLE
+    auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
+    ExpectRealtimeDeath(Func, "aligned_alloc");
+    ExpectNonRealtimeSurvival(Func);
+#if SANITIZER_APPLE
+  }
 #endif
+}
 
 // free_sized and free_aligned_sized (both C23) are not yet supported
 TEST(TestRtsanInterceptors, FreeDiesWhenRealtime) {

>From 4c83de3342cf0d3433eea63ac17a87f09bedf6c4 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Fri, 18 Oct 2024 10:19:12 -0700
Subject: [PATCH 2/5] Refactor builtin available to macro

---
 .../rtsan/tests/rtsan_test_interceptors_posix.cpp   | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index fb227a1d1bc05a..d9964abf76f29a 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -120,16 +120,19 @@ TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
   ExpectNonRealtimeSurvival(Func);
 }
 
+#if __has_builtin(__builtin_available) && SANITIZER_APPLE
+#define ALIGNED_ALLOC_AVAILABLE() __builtin_available(macOS 10.15, *)
+#else
+// We are going to assume this is true until we hit systems where it isn't
+#define ALIGNED_ALLOC_AVAILABLE() true
+#endif
+
 TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
-#if SANITIZER_APPLE
-  if (__builtin_available(macOS 10.15, *)) {
-#endif // SANITIZER_APPLE
+  if (ALIGNED_ALLOC_AVAILABLE()) {
     auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
     ExpectRealtimeDeath(Func, "aligned_alloc");
     ExpectNonRealtimeSurvival(Func);
-#if SANITIZER_APPLE
   }
-#endif
 }
 
 // free_sized and free_aligned_sized (both C23) are not yet supported

>From f2c352351ad0d3c2f183ff52d3fb52b38f249a1f Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Fri, 18 Oct 2024 10:39:55 -0700
Subject: [PATCH 3/5] [PR] fmayer - redef SANITIZER_ALIGNED_ALLOC as appopriate

---
 compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 6b325c028e425f..9adb558b6de7cd 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -463,15 +463,18 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
 
 // aligned_alloc was introduced in OSX 10.15
 // Linking will fail when using an older SDK
-#if defined(__MAC_10_15)
+#if SANITIZER_APPLE && defined(__MAC_10_15)
 // macOS 10.15 is greater than our minimal deployment target.  To ensure we
 // generate a weak reference so the dylib continues to work on older
 // systems, we need to forward declare the intercepted function as "weak
 // imports".
 SANITIZER_WEAK_IMPORT void *aligned_alloc(SIZE_T __alignment, SIZE_T __size);
-#endif // defined(__MAC_10_15)
 
-#if SANITIZER_INTERCEPT_ALIGNED_ALLOC || SANITIZER_APPLE
+#undef SANITIZER_INTERCEPT_ALIGNED_ALLOC
+#define SANITIZER_INTERCEPT_ALIGNED_ALLOC 1
+#endif // SANITIZER_APPLE && defined(__MAC_10_15)
+
+#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
   __rtsan_notify_intercepted_call("aligned_alloc");
   return REAL(aligned_alloc)(alignment, size);

>From 1e127da88cd90ccabe6d327260bd169bb42179f5 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Tue, 22 Oct 2024 11:05:02 -0700
Subject: [PATCH 4/5] [PR] vitalybuka - move aligned_alloc def up to all
 interceptors

---
 .../lib/rtsan/rtsan_interceptors_posix.cpp    | 13 ----------
 .../tests/rtsan_test_interceptors_posix.cpp   |  4 +--
 .../sanitizer_platform_interceptors.h         | 26 ++++++++++++++++++-
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 9adb558b6de7cd..63b0ca28a1f409 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -461,19 +461,6 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
   return REAL(valloc)(size);
 }
 
-// aligned_alloc was introduced in OSX 10.15
-// Linking will fail when using an older SDK
-#if SANITIZER_APPLE && defined(__MAC_10_15)
-// macOS 10.15 is greater than our minimal deployment target.  To ensure we
-// generate a weak reference so the dylib continues to work on older
-// systems, we need to forward declare the intercepted function as "weak
-// imports".
-SANITIZER_WEAK_IMPORT void *aligned_alloc(SIZE_T __alignment, SIZE_T __size);
-
-#undef SANITIZER_INTERCEPT_ALIGNED_ALLOC
-#define SANITIZER_INTERCEPT_ALIGNED_ALLOC 1
-#endif // SANITIZER_APPLE && defined(__MAC_10_15)
-
 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
   __rtsan_notify_intercepted_call("aligned_alloc");
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index d9964abf76f29a..40ad863d1bfba5 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -121,10 +121,10 @@ TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
 }
 
 #if __has_builtin(__builtin_available) && SANITIZER_APPLE
-#define ALIGNED_ALLOC_AVAILABLE() __builtin_available(macOS 10.15, *)
+#define ALIGNED_ALLOC_AVAILABLE() (__builtin_available(macOS 10.15, *))
 #else
 // We are going to assume this is true until we hit systems where it isn't
-#define ALIGNED_ALLOC_AVAILABLE() true
+#define ALIGNED_ALLOC_AVAILABLE() (true)
 #endif
 
 TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 36fafdc642642b..cf8e4f169a14c6 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -84,6 +84,10 @@
 #define SI_NOT_MAC 1
 #endif
 
+#if SANITIZER_APPLE
+#  include <Availability.h>
+#endif
+
 #if SANITIZER_IOS
 #define SI_IOS 1
 #else
@@ -503,7 +507,27 @@
 #define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
 #define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
 #define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
-#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
+
+#if SANITIZER_APPLE && defined(__MAC_10_15)
+#  define SI_MAC_SDK_10_15_AVAILABLE 1
+#else
+#  define SI_MAC_SDK_10_15_AVAILABLE 0
+#endif  // SANITIZER_APPLE && defined(__MAC_10_15)
+
+// aligned_alloc was introduced in OSX 10.15
+// Linking will fail when using an older SDK
+#if SI_MAC_SDK_10_15_AVAILABLE
+// macOS 10.15 is greater than our minimal deployment target.  To ensure we
+// generate a weak reference so the dylib continues to work on older
+// systems, we need to forward declare the intercepted function as "weak
+// imports".
+SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
+                                          __sanitizer::usize __size);
+#endif  // SI_MAC_SDK_10_15_AVAILABLE
+
+#define SANITIZER_INTERCEPT_ALIGNED_ALLOC \
+  (!SI_MAC || SI_MAC_SDK_10_15_AVAILABLE)
+
 #define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
 #define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
 #define SANITIZER_INTERCEPT_WCSLEN 1

>From 55e1021c7a6fc5a57ada1106635e2dca490ef987 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Tue, 22 Oct 2024 12:37:59 -0700
Subject: [PATCH 5/5] [PR] vitalybuka - clean up header

---
 .../sanitizer_platform_interceptors.h         | 36 +++++++++----------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index cf8e4f169a14c6..30be173369603d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -86,7 +86,22 @@
 
 #if SANITIZER_APPLE
 #  include <Availability.h>
-#endif
+
+// aligned_alloc was introduced in OSX 10.15
+// Linking will fail when using an older SDK
+#  if defined(__MAC_10_15)
+// macOS 10.15 is greater than our minimal deployment target.  To ensure we
+// generate a weak reference so the dylib continues to work on older
+// systems, we need to forward declare the intercepted function as "weak
+// imports".
+SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
+                                          __sanitizer::usize __size);
+#    define SI_MAC_SDK_10_15_AVAILABLE 1
+#  else
+#    define SI_MAC_SDK_10_15_AVAILABLE 0
+#  endif  // defined(__MAC_10_15)
+
+#endif  // SANITIZER_APPLE
 
 #if SANITIZER_IOS
 #define SI_IOS 1
@@ -507,27 +522,8 @@
 #define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
 #define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
 #define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
-
-#if SANITIZER_APPLE && defined(__MAC_10_15)
-#  define SI_MAC_SDK_10_15_AVAILABLE 1
-#else
-#  define SI_MAC_SDK_10_15_AVAILABLE 0
-#endif  // SANITIZER_APPLE && defined(__MAC_10_15)
-
-// aligned_alloc was introduced in OSX 10.15
-// Linking will fail when using an older SDK
-#if SI_MAC_SDK_10_15_AVAILABLE
-// macOS 10.15 is greater than our minimal deployment target.  To ensure we
-// generate a weak reference so the dylib continues to work on older
-// systems, we need to forward declare the intercepted function as "weak
-// imports".
-SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
-                                          __sanitizer::usize __size);
-#endif  // SI_MAC_SDK_10_15_AVAILABLE
-
 #define SANITIZER_INTERCEPT_ALIGNED_ALLOC \
   (!SI_MAC || SI_MAC_SDK_10_15_AVAILABLE)
-
 #define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
 #define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
 #define SANITIZER_INTERCEPT_WCSLEN 1



More information about the llvm-commits mailing list