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