[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
Thu Oct 17 14:41:22 PDT 2024


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

This follows the pattern seen here, where tsan uses libdispatch, even if the functions aren't available in our minimum version.

https://github.com/llvm/llvm-project/blob/f7669ba3d9443bc95dd63fa25beea13e6265fdc5/compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp#L226-L247

I think it would be good to propagate this change to the other sanitizers, as currently, no sanitizer actually intercepts aligned alloc on mac, stemming from:
https://github.com/llvm/llvm-project/blob/f7669ba3d9443bc95dd63fa25beea13e6265fdc5/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h#L497

Ideally, if this pattern looked good, we could add it to sanitizer_malloc_mac.inc, and the other sanitizers would have this enabled.

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

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

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 63b0ca28a1f409..7e2e9c71786c06 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);
@@ -471,6 +481,8 @@ INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
 #define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
 #endif
 
+#undef RTSAN_DARWIN_INTERCEPT_ALIGNED_ALLOC
+
 INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
   __rtsan_notify_intercepted_call("posix_memalign");
   return REAL(posix_memalign)(memptr, 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) {



More information about the llvm-commits mailing list