168d75effSDimitry Andric //===-- asan_malloc_mac.cpp -----------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric // Mac-specific malloc interception.
1268d75effSDimitry Andric //===----------------------------------------------------------------------===//
1368d75effSDimitry Andric
1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_platform.h"
1581ad6265SDimitry Andric #if SANITIZER_APPLE
1668d75effSDimitry Andric
1768d75effSDimitry Andric #include "asan_interceptors.h"
1868d75effSDimitry Andric #include "asan_report.h"
1968d75effSDimitry Andric #include "asan_stack.h"
2068d75effSDimitry Andric #include "asan_stats.h"
2168d75effSDimitry Andric #include "lsan/lsan_common.h"
2268d75effSDimitry Andric
2368d75effSDimitry Andric using namespace __asan;
2468d75effSDimitry Andric #define COMMON_MALLOC_ZONE_NAME "asan"
25*5f757f3fSDimitry Andric # define COMMON_MALLOC_ENTER() \
26*5f757f3fSDimitry Andric do { \
27*5f757f3fSDimitry Andric AsanInitFromRtl(); \
28*5f757f3fSDimitry Andric } while (false)
29*5f757f3fSDimitry Andric # define COMMON_MALLOC_SANITIZER_INITIALIZED AsanInited()
3068d75effSDimitry Andric # define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()
3168d75effSDimitry Andric # define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()
3268d75effSDimitry Andric # define COMMON_MALLOC_MEMALIGN(alignment, size) \
3368d75effSDimitry Andric GET_STACK_TRACE_MALLOC; \
3468d75effSDimitry Andric void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)
3568d75effSDimitry Andric # define COMMON_MALLOC_MALLOC(size) \
3668d75effSDimitry Andric GET_STACK_TRACE_MALLOC; \
3768d75effSDimitry Andric void *p = asan_malloc(size, &stack)
3868d75effSDimitry Andric # define COMMON_MALLOC_REALLOC(ptr, size) \
3968d75effSDimitry Andric GET_STACK_TRACE_MALLOC; \
4068d75effSDimitry Andric void *p = asan_realloc(ptr, size, &stack);
4168d75effSDimitry Andric # define COMMON_MALLOC_CALLOC(count, size) \
4268d75effSDimitry Andric GET_STACK_TRACE_MALLOC; \
4368d75effSDimitry Andric void *p = asan_calloc(count, size, &stack);
4468d75effSDimitry Andric # define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
4568d75effSDimitry Andric GET_STACK_TRACE_MALLOC; \
4668d75effSDimitry Andric int res = asan_posix_memalign(memptr, alignment, size, &stack);
4768d75effSDimitry Andric # define COMMON_MALLOC_VALLOC(size) \
4868d75effSDimitry Andric GET_STACK_TRACE_MALLOC; \
4968d75effSDimitry Andric void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
5068d75effSDimitry Andric # define COMMON_MALLOC_FREE(ptr) \
5168d75effSDimitry Andric GET_STACK_TRACE_FREE; \
5268d75effSDimitry Andric asan_free(ptr, &stack, FROM_MALLOC);
53*5f757f3fSDimitry Andric # define COMMON_MALLOC_SIZE(ptr) uptr size = asan_mz_size(ptr);
5468d75effSDimitry Andric # define COMMON_MALLOC_FILL_STATS(zone, stats) \
5568d75effSDimitry Andric AsanMallocStats malloc_stats; \
5668d75effSDimitry Andric FillMallocStatistics(&malloc_stats); \
5768d75effSDimitry Andric CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
5868d75effSDimitry Andric internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
5968d75effSDimitry Andric # define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
6068d75effSDimitry Andric GET_STACK_TRACE_FREE; \
6168d75effSDimitry Andric ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
6268d75effSDimitry Andric # define COMMON_MALLOC_NAMESPACE __asan
6368d75effSDimitry Andric # define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
6468d75effSDimitry Andric # define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 1
6568d75effSDimitry Andric
6668d75effSDimitry Andric # include "sanitizer_common/sanitizer_malloc_mac.inc"
6768d75effSDimitry Andric
6868d75effSDimitry Andric namespace COMMON_MALLOC_NAMESPACE {
6968d75effSDimitry Andric
HandleDlopenInit()7068d75effSDimitry Andric bool HandleDlopenInit() {
7168d75effSDimitry Andric static_assert(SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
7268d75effSDimitry Andric "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be true");
7368d75effSDimitry Andric // We have no reliable way of knowing how we are being loaded
7468d75effSDimitry Andric // so make it a requirement on Apple platforms to set this environment
7568d75effSDimitry Andric // variable to indicate that we want to perform initialization via
7668d75effSDimitry Andric // dlopen().
7768d75effSDimitry Andric auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");
7868d75effSDimitry Andric if (!init_str)
7968d75effSDimitry Andric return false;
8068d75effSDimitry Andric if (internal_strncmp(init_str, "1", 1) != 0)
8168d75effSDimitry Andric return false;
8268d75effSDimitry Andric // When we are loaded via `dlopen()` path we still initialize the malloc zone
8368d75effSDimitry Andric // so Symbolication clients (e.g. `leaks`) that load the ASan allocator can
8468d75effSDimitry Andric // find an initialized malloc zone.
8568d75effSDimitry Andric InitMallocZoneFields();
8668d75effSDimitry Andric return true;
8768d75effSDimitry Andric }
8868d75effSDimitry Andric } // namespace COMMON_MALLOC_NAMESPACE
8968d75effSDimitry Andric
9068d75effSDimitry Andric namespace {
9168d75effSDimitry Andric
mi_extra_init(sanitizer_malloc_introspection_t * mi)9268d75effSDimitry Andric void mi_extra_init(sanitizer_malloc_introspection_t *mi) {
9368d75effSDimitry Andric uptr last_byte_plus_one = 0;
9468d75effSDimitry Andric mi->allocator_ptr = 0;
9568d75effSDimitry Andric // Range is [begin_ptr, end_ptr)
9668d75effSDimitry Andric __lsan::GetAllocatorGlobalRange(&(mi->allocator_ptr), &last_byte_plus_one);
9768d75effSDimitry Andric CHECK_NE(mi->allocator_ptr, 0);
9868d75effSDimitry Andric CHECK_GT(last_byte_plus_one, mi->allocator_ptr);
9968d75effSDimitry Andric mi->allocator_size = last_byte_plus_one - (mi->allocator_ptr);
10068d75effSDimitry Andric CHECK_GT(mi->allocator_size, 0);
10168d75effSDimitry Andric }
10268d75effSDimitry Andric } // namespace
10368d75effSDimitry Andric
10468d75effSDimitry Andric #endif
105