[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
Fri Oct 18 10:40:09 PDT 2024


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

>From d5b090a249ed417927b301edfd22e2bebc0b5da9 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/3] [rtsan] Intercept aligned_alloc on all versions of OSX if
 available on the build machine

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

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 63b0ca28a1f409..6b325c028e425f 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.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.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
index f7a281f13e2ff6..fb227a1d1bc05a 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.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 a41ca2349e8c8cec061624d0ed28a547f6a4a826 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/3] Refactor builtin available to macro

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

diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
index fb227a1d1bc05a..d9964abf76f29a 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.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 1e261bd9b9097ded11d9447a33c1e3d0893c53ea 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/3] [PR] fmayer - redef SANITIZER_ALIGNED_ALLOC as appopriate

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

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 6b325c028e425f..9adb558b6de7cd 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.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);



More information about the llvm-commits mailing list