diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp index a4bc3d6cff91a..14b25a8995dab 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp @@ -22,6 +22,7 @@ #include "sanitizer_common/sanitizer_internal_defs.h" #include "sanitizer_common/sanitizer_libc.h" #include "sanitizer_common/sanitizer_linux.h" +#include "sanitizer_common/sanitizer_platform_interceptors.h" #include "sanitizer_common/sanitizer_platform_limits_netbsd.h" #include "sanitizer_common/sanitizer_platform_limits_posix.h" #include "sanitizer_common/sanitizer_posix.h" @@ -747,6 +748,41 @@ TSAN_INTERCEPTOR(void, free, void *p) { user_free(thr, pc, p); } +# if SANITIZER_INTERCEPT_FREE_SIZED +TSAN_INTERCEPTOR(void, free_sized, void *p, uptr size) { + if (UNLIKELY(!p)) + return; + if (in_symbolizer()) + return InternalFree(p); + if (DlsymAlloc::PointerIsMine(p)) + return DlsymAlloc::Free(p); + invoke_free_hook(p); + SCOPED_INTERCEPTOR_RAW(free_sized, p, size); + user_free(thr, pc, p); +} +# define TSAN_MAYBE_INTERCEPT_FREE_SIZED INTERCEPT_FUNCTION(free_sized) +# else +# define TSAN_MAYBE_INTERCEPT_FREE_SIZED +# endif + +# if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED +TSAN_INTERCEPTOR(void, free_aligned_sized, void *p, uptr alignment, uptr size) { + if (UNLIKELY(!p)) + return; + if (in_symbolizer()) + return InternalFree(p); + if (DlsymAlloc::PointerIsMine(p)) + return DlsymAlloc::Free(p); + invoke_free_hook(p); + SCOPED_INTERCEPTOR_RAW(free_aligned_sized, p, alignment, size); + user_free(thr, pc, p); +} +# define TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED \ + INTERCEPT_FUNCTION(free_aligned_sized) +# else +# define TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED +# endif + TSAN_INTERCEPTOR(void, cfree, void *p) { if (UNLIKELY(!p)) return; @@ -763,6 +799,9 @@ TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) { SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p); return user_alloc_usable_size(p); } +#else +# define TSAN_MAYBE_INTERCEPT_FREE_SIZED +# define TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED #endif TSAN_INTERCEPTOR(char *, strcpy, char *dst, const char *src) { @@ -2963,6 +3002,8 @@ void InitializeInterceptors() { TSAN_INTERCEPT(realloc); TSAN_INTERCEPT(reallocarray); TSAN_INTERCEPT(free); + TSAN_MAYBE_INTERCEPT_FREE_SIZED; + TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED; TSAN_INTERCEPT(cfree); TSAN_INTERCEPT(munmap); TSAN_MAYBE_INTERCEPT_MEMALIGN; diff --git a/compiler-rt/lib/tsan/rtl/tsan_malloc_mac.cpp b/compiler-rt/lib/tsan/rtl/tsan_malloc_mac.cpp index e973be963e575..9d097806fa9b2 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_malloc_mac.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_malloc_mac.cpp @@ -73,15 +73,19 @@ using namespace __tsan; invoke_free_hook(ptr); \ SCOPED_INTERCEPTOR_RAW(free, ptr); \ user_free(thr, pc, ptr) -#define COMMON_MALLOC_SIZE(ptr) uptr size = user_alloc_usable_size(ptr); -#define COMMON_MALLOC_FILL_STATS(zone, stats) -#define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \ - (void)zone_name; \ - Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", ptr); -#define COMMON_MALLOC_NAMESPACE __tsan -#define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0 -#define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0 +# define COMMON_MALLOC_FREE_SIZED(ptr, size) COMMON_MALLOC_FREE(ptr) +# define COMMON_MALLOC_FREE_ALIGNED_SIZED(ptr, alignment, size) \ + COMMON_MALLOC_FREE(ptr) +# define COMMON_MALLOC_SIZE(ptr) uptr size = user_alloc_usable_size(ptr); +# define COMMON_MALLOC_FILL_STATS(zone, stats) +# define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \ + (void)zone_name; \ + Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", \ + ptr); +# define COMMON_MALLOC_NAMESPACE __tsan +# define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0 +# define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0 -#include "sanitizer_common/sanitizer_malloc_mac.inc" +# include "sanitizer_common/sanitizer_malloc_mac.inc" #endif diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c index 7710c62368191..dc11e7fa5e9b3 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c @@ -1,5 +1,5 @@ // RUN: %clang -std=c23 -O0 %s -o %t && %run %t -// UNSUPPORTED: asan, hwasan, rtsan, tsan, ubsan +// UNSUPPORTED: asan, hwasan, rtsan, ubsan #include #include diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c index 9eac562fecb03..7a993d481bc5a 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c @@ -1,5 +1,5 @@ // RUN: %clang -std=c23 -O0 %s -o %t && %run %t -// UNSUPPORTED: asan, hwasan, rtsan, tsan, ubsan +// UNSUPPORTED: asan, hwasan, rtsan, ubsan #include #include