tsan_malloc_mac.cpp 3.4 KB

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