llvm-project
104 строки · 4.2 Кб
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
23using namespace __asan;
24#define COMMON_MALLOC_ZONE_NAME "asan"
25# define COMMON_MALLOC_ENTER() \
26do { \
27AsanInitFromRtl(); \
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) \
33GET_STACK_TRACE_MALLOC; \
34void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)
35# define COMMON_MALLOC_MALLOC(size) \
36GET_STACK_TRACE_MALLOC; \
37void *p = asan_malloc(size, &stack)
38# define COMMON_MALLOC_REALLOC(ptr, size) \
39GET_STACK_TRACE_MALLOC; \
40void *p = asan_realloc(ptr, size, &stack);
41# define COMMON_MALLOC_CALLOC(count, size) \
42GET_STACK_TRACE_MALLOC; \
43void *p = asan_calloc(count, size, &stack);
44# define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
45GET_STACK_TRACE_MALLOC; \
46int res = asan_posix_memalign(memptr, alignment, size, &stack);
47# define COMMON_MALLOC_VALLOC(size) \
48GET_STACK_TRACE_MALLOC; \
49void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
50# define COMMON_MALLOC_FREE(ptr) \
51GET_STACK_TRACE_FREE; \
52asan_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) \
55AsanMallocStats malloc_stats; \
56FillMallocStatistics(&malloc_stats); \
57CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
58internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
59# define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
60GET_STACK_TRACE_FREE; \
61ReportMacMzReallocUnknown((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
68namespace COMMON_MALLOC_NAMESPACE {
69
70bool HandleDlopenInit() {
71static_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().
77auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");
78if (!init_str)
79return false;
80if (internal_strncmp(init_str, "1", 1) != 0)
81return 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.
85InitMallocZoneFields();
86return true;
87}
88} // namespace COMMON_MALLOC_NAMESPACE
89
90namespace {
91
92void mi_extra_init(sanitizer_malloc_introspection_t *mi) {
93uptr last_byte_plus_one = 0;
94mi->allocator_ptr = 0;
95// Range is [begin_ptr, end_ptr)
96__lsan::GetAllocatorGlobalRange(&(mi->allocator_ptr), &last_byte_plus_one);
97CHECK_NE(mi->allocator_ptr, 0);
98CHECK_GT(last_byte_plus_one, mi->allocator_ptr);
99mi->allocator_size = last_byte_plus_one - (mi->allocator_ptr);
100CHECK_GT(mi->allocator_size, 0);
101}
102} // namespace
103
104#endif
105