Skip to content

Commit 2de54b9

Browse files
committed
[tsan] Invoke malloc/free hooks on darwin
Matches behaviour from tsan_interceptors_posix. This is covered by sanitizer_common/TestCases/malloc_hook.cpp (which is currently failing on darwin) I've tested it on an arm-based Mac & also compiled to x86_64 on it. Differential Revision: https://reviews.llvm.org/D151865
1 parent e48b1e8 commit 2de54b9

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "sanitizer_common/sanitizer_errno.h"
1818
#include "tsan_interceptors.h"
1919
#include "tsan_stack_trace.h"
20+
#include "tsan_mman.h"
2021

2122
using namespace __tsan;
2223
#define COMMON_MALLOC_ZONE_NAME "tsan"
@@ -29,16 +30,30 @@ using namespace __tsan;
2930
user_memalign(cur_thread(), StackTrace::GetCurrentPc(), alignment, size)
3031
#define COMMON_MALLOC_MALLOC(size) \
3132
if (in_symbolizer()) return InternalAlloc(size); \
32-
SCOPED_INTERCEPTOR_RAW(malloc, size); \
33-
void *p = user_alloc(thr, pc, size)
33+
void *p = 0; \
34+
{ \
35+
SCOPED_INTERCEPTOR_RAW(malloc, size); \
36+
p = user_alloc(thr, pc, size); \
37+
} \
38+
invoke_malloc_hook(p, size)
3439
#define COMMON_MALLOC_REALLOC(ptr, size) \
3540
if (in_symbolizer()) return InternalRealloc(ptr, size); \
36-
SCOPED_INTERCEPTOR_RAW(realloc, ptr, size); \
37-
void *p = user_realloc(thr, pc, ptr, size)
41+
if (ptr) \
42+
invoke_free_hook(ptr); \
43+
void *p = 0; \
44+
{ \
45+
SCOPED_INTERCEPTOR_RAW(realloc, ptr, size); \
46+
p = user_realloc(thr, pc, ptr, size); \
47+
} \
48+
invoke_malloc_hook(p, size)
3849
#define COMMON_MALLOC_CALLOC(count, size) \
3950
if (in_symbolizer()) return InternalCalloc(count, size); \
40-
SCOPED_INTERCEPTOR_RAW(calloc, size, count); \
41-
void *p = user_calloc(thr, pc, size, count)
51+
void *p = 0; \
52+
{ \
53+
SCOPED_INTERCEPTOR_RAW(calloc, size, count); \
54+
p = user_calloc(thr, pc, size, count); \
55+
} \
56+
invoke_malloc_hook(p, size * count)
4257
#define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
4358
if (in_symbolizer()) { \
4459
void *p = InternalAlloc(size, nullptr, alignment); \
@@ -55,6 +70,7 @@ using namespace __tsan;
5570
void *p = user_valloc(thr, pc, size)
5671
#define COMMON_MALLOC_FREE(ptr) \
5772
if (in_symbolizer()) return InternalFree(ptr); \
73+
invoke_free_hook(ptr); \
5874
SCOPED_INTERCEPTOR_RAW(free, ptr); \
5975
user_free(thr, pc, ptr)
6076
#define COMMON_MALLOC_SIZE(ptr) uptr size = user_alloc_usable_size(ptr);

0 commit comments

Comments
 (0)