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