tsan_malloc_mac.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_APPLE
  15. #include "sanitizer_common/sanitizer_errno.h"
  16. #include "tsan_interceptors.h"
  17. #include "tsan_stack_trace.h"
  18. #include "tsan_mman.h"
  19. using namespace __tsan;
  20. #define COMMON_MALLOC_ZONE_NAME "tsan"
  21. #define COMMON_MALLOC_ENTER()
  22. #define COMMON_MALLOC_SANITIZER_INITIALIZED (cur_thread()->is_inited)
  23. #define COMMON_MALLOC_FORCE_LOCK()
  24. #define COMMON_MALLOC_FORCE_UNLOCK()
  25. #define COMMON_MALLOC_MEMALIGN(alignment, size) \
  26. void *p = \
  27. user_memalign(cur_thread(), StackTrace::GetCurrentPc(), alignment, size)
  28. #define COMMON_MALLOC_MALLOC(size) \
  29. if (in_symbolizer()) return InternalAlloc(size); \
  30. void *p = 0; \
  31. { \
  32. SCOPED_INTERCEPTOR_RAW(malloc, size); \
  33. p = user_alloc(thr, pc, size); \
  34. } \
  35. invoke_malloc_hook(p, size)
  36. #define COMMON_MALLOC_REALLOC(ptr, size) \
  37. if (in_symbolizer()) return InternalRealloc(ptr, size); \
  38. if (ptr) \
  39. invoke_free_hook(ptr); \
  40. void *p = 0; \
  41. { \
  42. SCOPED_INTERCEPTOR_RAW(realloc, ptr, size); \
  43. p = user_realloc(thr, pc, ptr, size); \
  44. } \
  45. invoke_malloc_hook(p, size)
  46. #define COMMON_MALLOC_CALLOC(count, size) \
  47. if (in_symbolizer()) return InternalCalloc(count, size); \
  48. void *p = 0; \
  49. { \
  50. SCOPED_INTERCEPTOR_RAW(calloc, size, count); \
  51. p = user_calloc(thr, pc, size, count); \
  52. } \
  53. invoke_malloc_hook(p, size * count)
  54. #define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
  55. if (in_symbolizer()) { \
  56. void *p = InternalAlloc(size, nullptr, alignment); \
  57. if (!p) return errno_ENOMEM; \
  58. *memptr = p; \
  59. return 0; \
  60. } \
  61. SCOPED_INTERCEPTOR_RAW(posix_memalign, memptr, alignment, size); \
  62. int res = user_posix_memalign(thr, pc, memptr, alignment, size);
  63. #define COMMON_MALLOC_VALLOC(size) \
  64. if (in_symbolizer()) \
  65. return InternalAlloc(size, nullptr, GetPageSizeCached()); \
  66. SCOPED_INTERCEPTOR_RAW(valloc, size); \
  67. void *p = user_valloc(thr, pc, size)
  68. #define COMMON_MALLOC_FREE(ptr) \
  69. if (in_symbolizer()) return InternalFree(ptr); \
  70. invoke_free_hook(ptr); \
  71. SCOPED_INTERCEPTOR_RAW(free, ptr); \
  72. user_free(thr, pc, ptr)
  73. #define COMMON_MALLOC_SIZE(ptr) uptr size = user_alloc_usable_size(ptr);
  74. #define COMMON_MALLOC_FILL_STATS(zone, stats)
  75. #define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
  76. (void)zone_name; \
  77. Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", ptr);
  78. #define COMMON_MALLOC_NAMESPACE __tsan
  79. #define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
  80. #define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0
  81. #include "sanitizer_common/sanitizer_malloc_mac.inc"
  82. #endif