asan_malloc_mac.cpp 3.8 KB

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