Skip to content

Commit afba3be

Browse files
committed
tsan: Support free_sized and free_aligned_sized from C23
Signed-off-by: Justin King <[email protected]>
1 parent f8ee577 commit afba3be

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "sanitizer_common/sanitizer_internal_defs.h"
2323
#include "sanitizer_common/sanitizer_libc.h"
2424
#include "sanitizer_common/sanitizer_linux.h"
25+
#include "sanitizer_common/sanitizer_platform_interceptors.h"
2526
#include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
2627
#include "sanitizer_common/sanitizer_platform_limits_posix.h"
2728
#include "sanitizer_common/sanitizer_posix.h"
@@ -747,6 +748,41 @@ TSAN_INTERCEPTOR(void, free, void *p) {
747748
user_free(thr, pc, p);
748749
}
749750

751+
# if SANITIZER_INTERCEPT_FREE_SIZED
752+
TSAN_INTERCEPTOR(void, free_sized, void *p, uptr size) {
753+
if (UNLIKELY(!p))
754+
return;
755+
if (in_symbolizer())
756+
return InternalFree(p);
757+
if (DlsymAlloc::PointerIsMine(p))
758+
return DlsymAlloc::Free(p);
759+
invoke_free_hook(p);
760+
SCOPED_INTERCEPTOR_RAW(free_sized, p, size);
761+
user_free(thr, pc, p);
762+
}
763+
# define TSAN_MAYBE_INTERCEPT_FREE_SIZED INTERCEPT_FUNCTION(free_sized)
764+
# else
765+
# define TSAN_MAYBE_INTERCEPT_FREE_SIZED
766+
# endif
767+
768+
# if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
769+
TSAN_INTERCEPTOR(void, free_aligned_sized, void *p, uptr alignment, uptr size) {
770+
if (UNLIKELY(!p))
771+
return;
772+
if (in_symbolizer())
773+
return InternalFree(p);
774+
if (DlsymAlloc::PointerIsMine(p))
775+
return DlsymAlloc::Free(p);
776+
invoke_free_hook(p);
777+
SCOPED_INTERCEPTOR_RAW(free_aligned_sized, p, alignment, size);
778+
user_free(thr, pc, p);
779+
}
780+
# define TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED \
781+
INTERCEPT_FUNCTION(free_aligned_sized)
782+
# else
783+
# define TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED
784+
# endif
785+
750786
TSAN_INTERCEPTOR(void, cfree, void *p) {
751787
if (UNLIKELY(!p))
752788
return;
@@ -763,6 +799,9 @@ TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) {
763799
SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p);
764800
return user_alloc_usable_size(p);
765801
}
802+
#else
803+
# define TSAN_MAYBE_INTERCEPT_FREE_SIZED
804+
# define TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED
766805
#endif
767806

768807
TSAN_INTERCEPTOR(char *, strcpy, char *dst, const char *src) {
@@ -2963,6 +3002,8 @@ void InitializeInterceptors() {
29633002
TSAN_INTERCEPT(realloc);
29643003
TSAN_INTERCEPT(reallocarray);
29653004
TSAN_INTERCEPT(free);
3005+
TSAN_MAYBE_INTERCEPT_FREE_SIZED;
3006+
TSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED;
29663007
TSAN_INTERCEPT(cfree);
29673008
TSAN_INTERCEPT(munmap);
29683009
TSAN_MAYBE_INTERCEPT_MEMALIGN;

compiler-rt/lib/tsan/rtl/tsan_malloc_mac.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ using namespace __tsan;
7373
invoke_free_hook(ptr); \
7474
SCOPED_INTERCEPTOR_RAW(free, ptr); \
7575
user_free(thr, pc, ptr)
76-
#define COMMON_MALLOC_SIZE(ptr) uptr size = user_alloc_usable_size(ptr);
77-
#define COMMON_MALLOC_FILL_STATS(zone, stats)
78-
#define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
79-
(void)zone_name; \
80-
Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", ptr);
81-
#define COMMON_MALLOC_NAMESPACE __tsan
82-
#define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
83-
#define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0
76+
# define COMMON_MALLOC_FREE_SIZED(ptr, size) COMMON_MALLOC_FREE(ptr)
77+
# define COMMON_MALLOC_FREE_ALIGNED_SIZED(ptr, alignment, size) \
78+
COMMON_MALLOC_FREE(ptr)
79+
# define COMMON_MALLOC_SIZE(ptr) uptr size = user_alloc_usable_size(ptr);
80+
# define COMMON_MALLOC_FILL_STATS(zone, stats)
81+
# define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
82+
(void)zone_name; \
83+
Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", \
84+
ptr);
85+
# define COMMON_MALLOC_NAMESPACE __tsan
86+
# define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
87+
# define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0
8488

85-
#include "sanitizer_common/sanitizer_malloc_mac.inc"
89+
# include "sanitizer_common/sanitizer_malloc_mac.inc"
8690

8791
#endif

compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang -std=c23 -O0 %s -o %t && %run %t
2-
// UNSUPPORTED: asan, hwasan, rtsan, tsan, ubsan
2+
// UNSUPPORTED: asan, hwasan, rtsan, ubsan
33

44
#include <stddef.h>
55
#include <stdlib.h>

compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang -std=c23 -O0 %s -o %t && %run %t
2-
// UNSUPPORTED: asan, hwasan, rtsan, tsan, ubsan
2+
// UNSUPPORTED: asan, hwasan, rtsan, ubsan
33

44
#include <stddef.h>
55
#include <stdlib.h>

0 commit comments

Comments
 (0)