comparison libsanitizer/asan/asan_malloc_mac.cpp @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
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_MAC
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() ENSURE_ASAN_INITED()
26 #define COMMON_MALLOC_SANITIZER_INITIALIZED asan_inited
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) \
51 uptr size = asan_mz_size(ptr);
52 #define COMMON_MALLOC_FILL_STATS(zone, stats) \
53 AsanMallocStats malloc_stats; \
54 FillMallocStatistics(&malloc_stats); \
55 CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
56 internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
57 #define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
58 GET_STACK_TRACE_FREE; \
59 ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
60 #define COMMON_MALLOC_NAMESPACE __asan
61 #define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
62 #define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 1
63
64 #include "sanitizer_common/sanitizer_malloc_mac.inc"
65
66 namespace COMMON_MALLOC_NAMESPACE {
67
68 bool HandleDlopenInit() {
69 static_assert(SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
70 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be true");
71 // We have no reliable way of knowing how we are being loaded
72 // so make it a requirement on Apple platforms to set this environment
73 // variable to indicate that we want to perform initialization via
74 // dlopen().
75 auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");
76 if (!init_str)
77 return false;
78 if (internal_strncmp(init_str, "1", 1) != 0)
79 return false;
80 // When we are loaded via `dlopen()` path we still initialize the malloc zone
81 // so Symbolication clients (e.g. `leaks`) that load the ASan allocator can
82 // find an initialized malloc zone.
83 InitMallocZoneFields();
84 return true;
85 }
86 } // namespace COMMON_MALLOC_NAMESPACE
87
88 namespace {
89
90 void mi_extra_init(sanitizer_malloc_introspection_t *mi) {
91 uptr last_byte_plus_one = 0;
92 mi->allocator_ptr = 0;
93 // Range is [begin_ptr, end_ptr)
94 __lsan::GetAllocatorGlobalRange(&(mi->allocator_ptr), &last_byte_plus_one);
95 CHECK_NE(mi->allocator_ptr, 0);
96 CHECK_GT(last_byte_plus_one, mi->allocator_ptr);
97 mi->allocator_size = last_byte_plus_one - (mi->allocator_ptr);
98 CHECK_GT(mi->allocator_size, 0);
99 }
100 } // namespace
101
102 #endif