[compiler-rt] [TySan] Intercept malloc_size on Apple platforms. (PR #122133)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 8 08:00:03 PST 2025


https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/122133

>From 22fed997621f3954127fc95005a11dd8a5500dc3 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 8 Jan 2025 15:49:01 +0000
Subject: [PATCH 1/2] [TySan] Intercept malloc_size on Apple platforms.

On Apple platforms, malloc_size also needs intercepting with
DlSymAllocator, otherwise all type-sanitized binaries crash on startup
with an objc error:
   realized class 0x12345 has corrupt data pointer: malloc_size(0x567) = 0
---
 .../lib/sanitizer_common/sanitizer_allocator_dlsym.h | 12 ++++++++----
 compiler-rt/lib/tysan/tysan_interceptors.cpp         |  8 ++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h
index b360478a058a54..5465258e6a022d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h
@@ -37,7 +37,7 @@ struct DlSymAllocator {
     void *ptr = InternalAlloc(size_in_bytes, nullptr, align);
     CHECK(internal_allocator()->FromPrimary(ptr));
     Details::OnAllocate(ptr,
-                        internal_allocator()->GetActuallyAllocatedSize(ptr));
+                        Size(ptr));
     return ptr;
   }
 
@@ -45,12 +45,12 @@ struct DlSymAllocator {
     void *ptr = InternalCalloc(nmemb, size);
     CHECK(internal_allocator()->FromPrimary(ptr));
     Details::OnAllocate(ptr,
-                        internal_allocator()->GetActuallyAllocatedSize(ptr));
+                        Size(ptr));
     return ptr;
   }
 
   static void Free(void *ptr) {
-    uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
+    uptr size = Size(ptr);
     Details::OnFree(ptr, size);
     InternalFree(ptr);
   }
@@ -63,7 +63,7 @@ struct DlSymAllocator {
       Free(ptr);
       return nullptr;
     }
-    uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
+    uptr size = Size(ptr);
     uptr memcpy_size = Min(new_size, size);
     void *new_ptr = Allocate(new_size);
     if (new_ptr)
@@ -77,6 +77,10 @@ struct DlSymAllocator {
     return Realloc(ptr, count * size);
   }
 
+  static uptr Size(void *ptr) {
+    return internal_allocator()->GetActuallyAllocatedSize(ptr);
+  }
+
   static void OnAllocate(const void *ptr, uptr size) {}
   static void OnFree(const void *ptr, uptr size) {}
 };
diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp
index 08b1010a48ecf0..4a89f0746230fe 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -108,6 +108,14 @@ INTERCEPTOR(void *, malloc, uptr size) {
   return res;
 }
 
+#if SANITIZER_APPLE
+INTERCEPTOR(uptr , malloc_size, void *ptr) {
+  if (DlsymAlloc::Use())
+    return DlsymAlloc::Size(ptr);
+  return REAL(malloc_size)(ptr);
+}
+#endif
+
 INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
   if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
     return DlsymAlloc::Realloc(ptr, size);

>From 7f3d968f01ad7ce522c9117ba15a577cc04353dc Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 8 Jan 2025 15:59:42 +0000
Subject: [PATCH 2/2] !fixup fix formatting

---
 .../lib/sanitizer_common/sanitizer_allocator_dlsym.h        | 6 ++----
 compiler-rt/lib/tysan/tysan_interceptors.cpp                | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h
index 5465258e6a022d..9d7ebc54ea2fbf 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h
@@ -36,16 +36,14 @@ struct DlSymAllocator {
   static void *Allocate(uptr size_in_bytes, uptr align = kWordSize) {
     void *ptr = InternalAlloc(size_in_bytes, nullptr, align);
     CHECK(internal_allocator()->FromPrimary(ptr));
-    Details::OnAllocate(ptr,
-                        Size(ptr));
+    Details::OnAllocate(ptr, Size(ptr));
     return ptr;
   }
 
   static void *Callocate(usize nmemb, usize size) {
     void *ptr = InternalCalloc(nmemb, size);
     CHECK(internal_allocator()->FromPrimary(ptr));
-    Details::OnAllocate(ptr,
-                        Size(ptr));
+    Details::OnAllocate(ptr, Size(ptr));
     return ptr;
   }
 
diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp
index 4a89f0746230fe..de2daa89f544d8 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -109,7 +109,7 @@ INTERCEPTOR(void *, malloc, uptr size) {
 }
 
 #if SANITIZER_APPLE
-INTERCEPTOR(uptr , malloc_size, void *ptr) {
+INTERCEPTOR(uptr, malloc_size, void *ptr) {
   if (DlsymAlloc::Use())
     return DlsymAlloc::Size(ptr);
   return REAL(malloc_size)(ptr);



More information about the llvm-commits mailing list