asan_malloc_mac.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- asan_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 AddressSanitizer, an address sanity checker.
  10. //
  11. // Mac-specific malloc interception.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_APPLE
  15. #include "asan_interceptors.h"
  16. #include "asan_report.h"
  17. #include "asan_stack.h"
  18. #include "asan_stats.h"
  19. #include "lsan/lsan_common.h"
  20. using namespace __asan;
  21. #define COMMON_MALLOC_ZONE_NAME "asan"
  22. # define COMMON_MALLOC_ENTER() \
  23. do { \
  24. AsanInitFromRtl(); \
  25. } while (false)
  26. # define COMMON_MALLOC_SANITIZER_INITIALIZED AsanInited()
  27. # define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()
  28. # define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()
  29. # define COMMON_MALLOC_MEMALIGN(alignment, size) \
  30. GET_STACK_TRACE_MALLOC; \
  31. void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)
  32. # define COMMON_MALLOC_MALLOC(size) \
  33. GET_STACK_TRACE_MALLOC; \
  34. void *p = asan_malloc(size, &stack)
  35. # define COMMON_MALLOC_REALLOC(ptr, size) \
  36. GET_STACK_TRACE_MALLOC; \
  37. void *p = asan_realloc(ptr, size, &stack);
  38. # define COMMON_MALLOC_CALLOC(count, size) \
  39. GET_STACK_TRACE_MALLOC; \
  40. void *p = asan_calloc(count, size, &stack);
  41. # define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
  42. GET_STACK_TRACE_MALLOC; \
  43. int res = asan_posix_memalign(memptr, alignment, size, &stack);
  44. # define COMMON_MALLOC_VALLOC(size) \
  45. GET_STACK_TRACE_MALLOC; \
  46. void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
  47. # define COMMON_MALLOC_FREE(ptr) \
  48. GET_STACK_TRACE_FREE; \
  49. asan_free(ptr, &stack, FROM_MALLOC);
  50. # define COMMON_MALLOC_SIZE(ptr) uptr size = asan_mz_size(ptr);
  51. # define COMMON_MALLOC_FILL_STATS(zone, stats) \
  52. AsanMallocStats malloc_stats; \
  53. FillMallocStatistics(&malloc_stats); \
  54. CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
  55. internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
  56. # define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
  57. GET_STACK_TRACE_FREE; \
  58. ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
  59. # define COMMON_MALLOC_NAMESPACE __asan
  60. # define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
  61. # define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 1
  62. # include "sanitizer_common/sanitizer_malloc_mac.inc"
  63. namespace COMMON_MALLOC_NAMESPACE {
  64. bool HandleDlopenInit() {
  65. static_assert(SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
  66. "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be true");
  67. // We have no reliable way of knowing how we are being loaded
  68. // so make it a requirement on Apple platforms to set this environment
  69. // variable to indicate that we want to perform initialization via
  70. // dlopen().
  71. auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");
  72. if (!init_str)
  73. return false;
  74. if (internal_strncmp(init_str, "1", 1) != 0)
  75. return false;
  76. // When we are loaded via `dlopen()` path we still initialize the malloc zone
  77. // so Symbolication clients (e.g. `leaks`) that load the ASan allocator can
  78. // find an initialized malloc zone.
  79. InitMallocZoneFields();
  80. return true;
  81. }
  82. } // namespace COMMON_MALLOC_NAMESPACE
  83. namespace {
  84. void mi_extra_init(sanitizer_malloc_introspection_t *mi) {
  85. uptr last_byte_plus_one = 0;
  86. mi->allocator_ptr = 0;
  87. // Range is [begin_ptr, end_ptr)
  88. __lsan::GetAllocatorGlobalRange(&(mi->allocator_ptr), &last_byte_plus_one);
  89. CHECK_NE(mi->allocator_ptr, 0);
  90. CHECK_GT(last_byte_plus_one, mi->allocator_ptr);
  91. mi->allocator_size = last_byte_plus_one - (mi->allocator_ptr);
  92. CHECK_GT(mi->allocator_size, 0);
  93. }
  94. } // namespace
  95. #endif