From 6eb5550e2e17a427e32cad795777ed876dad93bb Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 8 Jan 2025 15:49:01 +0000 Subject: [PATCH 1/5] [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 b360478a058a5..5465258e6a022 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 08b1010a48ecf..4a89f0746230f 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 d130709f80b358b7ac0367ab32b83755914c7a2c Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 8 Jan 2025 15:59:42 +0000 Subject: [PATCH 2/5] !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 5465258e6a022..9d7ebc54ea2fb 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 4a89f0746230f..de2daa89f544d 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); From 17e82912fa9a67df7512d6889698e965019f33b8 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 8 Jan 2025 16:51:06 +0000 Subject: [PATCH 3/5] !fixup also check PointerIsMine --- compiler-rt/lib/tysan/tysan_interceptors.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp index de2daa89f544d..d0a3735fffac9 100644 --- a/compiler-rt/lib/tysan/tysan_interceptors.cpp +++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp @@ -110,7 +110,7 @@ INTERCEPTOR(void *, malloc, uptr size) { #if SANITIZER_APPLE INTERCEPTOR(uptr, malloc_size, void *ptr) { - if (DlsymAlloc::Use()) + if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr)) return DlsymAlloc::Size(ptr); return REAL(malloc_size)(ptr); } From 5688643f9c633c7e9bcbcfec1f8e9c3e052dd3cd Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Thu, 9 Jan 2025 11:54:04 +0000 Subject: [PATCH 4/5] !fixup only use PointerIsMine --- compiler-rt/lib/tysan/tysan_interceptors.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp index d0a3735fffac9..146c9b751c1b1 100644 --- a/compiler-rt/lib/tysan/tysan_interceptors.cpp +++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp @@ -110,7 +110,7 @@ INTERCEPTOR(void *, malloc, uptr size) { #if SANITIZER_APPLE INTERCEPTOR(uptr, malloc_size, void *ptr) { - if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr)) + if (DlsymAlloc::PointerIsMine(ptr)) return DlsymAlloc::Size(ptr); return REAL(malloc_size)(ptr); } From 3240cb23e670e1ecc56b5121aa97db5c871098c4 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Thu, 9 Jan 2025 17:28:58 +0000 Subject: [PATCH 5/5] !fixup Size -> GetSize() --- .../lib/sanitizer_common/sanitizer_allocator_dlsym.h | 10 +++++----- compiler-rt/lib/tysan/tysan_interceptors.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h index 9d7ebc54ea2fb..6e6cdbd9eeaed 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h @@ -36,19 +36,19 @@ 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, GetSize(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, GetSize(ptr)); return ptr; } static void Free(void *ptr) { - uptr size = Size(ptr); + uptr size = GetSize(ptr); Details::OnFree(ptr, size); InternalFree(ptr); } @@ -61,7 +61,7 @@ struct DlSymAllocator { Free(ptr); return nullptr; } - uptr size = Size(ptr); + uptr size = GetSize(ptr); uptr memcpy_size = Min(new_size, size); void *new_ptr = Allocate(new_size); if (new_ptr) @@ -75,7 +75,7 @@ struct DlSymAllocator { return Realloc(ptr, count * size); } - static uptr Size(void *ptr) { + static uptr GetSize(void *ptr) { return internal_allocator()->GetActuallyAllocatedSize(ptr); } diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp index 146c9b751c1b1..a9c55a3ae0cf0 100644 --- a/compiler-rt/lib/tysan/tysan_interceptors.cpp +++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp @@ -111,7 +111,7 @@ INTERCEPTOR(void *, malloc, uptr size) { #if SANITIZER_APPLE INTERCEPTOR(uptr, malloc_size, void *ptr) { if (DlsymAlloc::PointerIsMine(ptr)) - return DlsymAlloc::Size(ptr); + return DlsymAlloc::GetSize(ptr); return REAL(malloc_size)(ptr); } #endif