Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_malloc_mac.cpp
35233 views
//===-- asan_malloc_mac.cpp -----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// Mac-specific malloc interception.11//===----------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"14#if SANITIZER_APPLE1516#include "asan_interceptors.h"17#include "asan_report.h"18#include "asan_stack.h"19#include "asan_stats.h"20#include "lsan/lsan_common.h"2122using namespace __asan;23#define COMMON_MALLOC_ZONE_NAME "asan"24# define COMMON_MALLOC_ENTER() \25do { \26AsanInitFromRtl(); \27} while (false)28# define COMMON_MALLOC_SANITIZER_INITIALIZED AsanInited()29# define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()30# define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()31# define COMMON_MALLOC_MEMALIGN(alignment, size) \32GET_STACK_TRACE_MALLOC; \33void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)34# define COMMON_MALLOC_MALLOC(size) \35GET_STACK_TRACE_MALLOC; \36void *p = asan_malloc(size, &stack)37# define COMMON_MALLOC_REALLOC(ptr, size) \38GET_STACK_TRACE_MALLOC; \39void *p = asan_realloc(ptr, size, &stack);40# define COMMON_MALLOC_CALLOC(count, size) \41GET_STACK_TRACE_MALLOC; \42void *p = asan_calloc(count, size, &stack);43# define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \44GET_STACK_TRACE_MALLOC; \45int res = asan_posix_memalign(memptr, alignment, size, &stack);46# define COMMON_MALLOC_VALLOC(size) \47GET_STACK_TRACE_MALLOC; \48void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);49# define COMMON_MALLOC_FREE(ptr) \50GET_STACK_TRACE_FREE; \51asan_free(ptr, &stack, FROM_MALLOC);52# define COMMON_MALLOC_SIZE(ptr) uptr size = asan_mz_size(ptr);53# define COMMON_MALLOC_FILL_STATS(zone, stats) \54AsanMallocStats malloc_stats; \55FillMallocStatistics(&malloc_stats); \56CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \57internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));58# define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \59GET_STACK_TRACE_FREE; \60ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);61# define COMMON_MALLOC_NAMESPACE __asan62# define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 063# define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 16465# include "sanitizer_common/sanitizer_malloc_mac.inc"6667namespace COMMON_MALLOC_NAMESPACE {6869bool HandleDlopenInit() {70static_assert(SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,71"Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be true");72// We have no reliable way of knowing how we are being loaded73// so make it a requirement on Apple platforms to set this environment74// variable to indicate that we want to perform initialization via75// dlopen().76auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");77if (!init_str)78return false;79if (internal_strncmp(init_str, "1", 1) != 0)80return false;81// When we are loaded via `dlopen()` path we still initialize the malloc zone82// so Symbolication clients (e.g. `leaks`) that load the ASan allocator can83// find an initialized malloc zone.84InitMallocZoneFields();85return true;86}87} // namespace COMMON_MALLOC_NAMESPACE8889namespace {9091void mi_extra_init(sanitizer_malloc_introspection_t *mi) {92uptr last_byte_plus_one = 0;93mi->allocator_ptr = 0;94// Range is [begin_ptr, end_ptr)95__lsan::GetAllocatorGlobalRange(&(mi->allocator_ptr), &last_byte_plus_one);96CHECK_NE(mi->allocator_ptr, 0);97CHECK_GT(last_byte_plus_one, mi->allocator_ptr);98mi->allocator_size = last_byte_plus_one - (mi->allocator_ptr);99CHECK_GT(mi->allocator_size, 0);100}101} // namespace102103#endif104105106