master
 1//===-- tsan_malloc_mac.cpp -----------------------------------------------===//
 2//
 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 4// See https://llvm.org/LICENSE.txt for license information.
 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 6//
 7//===----------------------------------------------------------------------===//
 8//
 9// This file is a part of ThreadSanitizer (TSan), a race detector.
10//
11// Mac-specific malloc interception.
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_common/sanitizer_platform.h"
15#if SANITIZER_APPLE
16
17#include "sanitizer_common/sanitizer_errno.h"
18#include "tsan_interceptors.h"
19#include "tsan_stack_trace.h"
20#include "tsan_mman.h"
21
22using namespace __tsan;
23#define COMMON_MALLOC_ZONE_NAME "tsan"
24#define COMMON_MALLOC_ENTER()
25#define COMMON_MALLOC_SANITIZER_INITIALIZED (cur_thread()->is_inited)
26#define COMMON_MALLOC_FORCE_LOCK()
27#define COMMON_MALLOC_FORCE_UNLOCK()
28#define COMMON_MALLOC_MEMALIGN(alignment, size) \
29  void *p =                                     \
30      user_memalign(cur_thread(), StackTrace::GetCurrentPc(), alignment, size)
31#define COMMON_MALLOC_MALLOC(size)                             \
32  if (in_symbolizer()) return InternalAlloc(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)
39#define COMMON_MALLOC_REALLOC(ptr, size)                              \
40  if (in_symbolizer()) return InternalRealloc(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)
49#define COMMON_MALLOC_CALLOC(count, size)                              \
50  if (in_symbolizer()) return InternalCalloc(count, size);             \
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)
57#define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size)      \
58  if (in_symbolizer()) {                                           \
59    void *p = InternalAlloc(size, nullptr, alignment);             \
60    if (!p) return errno_ENOMEM;                                   \
61    *memptr = p;                                                   \
62    return 0;                                                      \
63  }                                                                \
64  SCOPED_INTERCEPTOR_RAW(posix_memalign, memptr, alignment, size); \
65  int res = user_posix_memalign(thr, pc, memptr, alignment, size);
66#define COMMON_MALLOC_VALLOC(size)                            \
67  if (in_symbolizer())                                        \
68    return InternalAlloc(size, nullptr, GetPageSizeCached()); \
69  SCOPED_INTERCEPTOR_RAW(valloc, size);                       \
70  void *p = user_valloc(thr, pc, size)
71#define COMMON_MALLOC_FREE(ptr)                              \
72  if (in_symbolizer()) return InternalFree(ptr);             \
73  invoke_free_hook(ptr);                                     \
74  SCOPED_INTERCEPTOR_RAW(free, ptr);                         \
75  user_free(thr, pc, ptr)
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
88
89#  include "sanitizer_common/sanitizer_malloc_mac.inc"
90
91#endif