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