Daily bump.
[official-gcc.git] / libsanitizer / asan / asan_malloc_mac.cpp
blobd2380ee62bf3dff7cd485d86e2d29fb3161a6499
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.
11 // Mac-specific malloc interception.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_APPLE
17 #include "asan_interceptors.h"
18 #include "asan_report.h"
19 #include "asan_stack.h"
20 #include "asan_stats.h"
21 #include "lsan/lsan_common.h"
23 using namespace __asan;
24 #define COMMON_MALLOC_ZONE_NAME "asan"
25 #define COMMON_MALLOC_ENTER() ENSURE_ASAN_INITED()
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
63 # include "sanitizer_common/sanitizer_malloc_mac.inc"
65 namespace COMMON_MALLOC_NAMESPACE {
67 bool HandleDlopenInit() {
68 static_assert(SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
69 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be true");
70 // We have no reliable way of knowing how we are being loaded
71 // so make it a requirement on Apple platforms to set this environment
72 // variable to indicate that we want to perform initialization via
73 // dlopen().
74 auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");
75 if (!init_str)
76 return false;
77 if (internal_strncmp(init_str, "1", 1) != 0)
78 return false;
79 // When we are loaded via `dlopen()` path we still initialize the malloc zone
80 // so Symbolication clients (e.g. `leaks`) that load the ASan allocator can
81 // find an initialized malloc zone.
82 InitMallocZoneFields();
83 return true;
85 } // namespace COMMON_MALLOC_NAMESPACE
87 namespace {
89 void mi_extra_init(sanitizer_malloc_introspection_t *mi) {
90 uptr last_byte_plus_one = 0;
91 mi->allocator_ptr = 0;
92 // Range is [begin_ptr, end_ptr)
93 __lsan::GetAllocatorGlobalRange(&(mi->allocator_ptr), &last_byte_plus_one);
94 CHECK_NE(mi->allocator_ptr, 0);
95 CHECK_GT(last_byte_plus_one, mi->allocator_ptr);
96 mi->allocator_size = last_byte_plus_one - (mi->allocator_ptr);
97 CHECK_GT(mi->allocator_size, 0);
99 } // namespace
101 #endif