]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_malloc_mac.cc
MFV: r347413
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_malloc_mac.cc
1 //===-- asan_malloc_mac.cc ------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Mac-specific malloc interception.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_MAC
17
18 #include "asan_interceptors.h"
19 #include "asan_report.h"
20 #include "asan_stack.h"
21 #include "asan_stats.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
62 #include "sanitizer_common/sanitizer_malloc_mac.inc"
63
64 namespace COMMON_MALLOC_NAMESPACE {
65 bool HandleDlopenInit() {
66   static_assert(SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
67                 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be true");
68   // We have no reliable way of knowing how we are being loaded
69   // so make it a requirement on Apple platforms to set this environment
70   // variable to indicate that we want to perform initialization via
71   // dlopen().
72   auto init_str = GetEnv("APPLE_ASAN_INIT_FOR_DLOPEN");
73   if (!init_str)
74     return false;
75   if (internal_strncmp(init_str, "1", 1) != 0)
76     return false;
77   // When we are loaded via `dlopen()` path we still initialize the malloc zone
78   // so Symbolication clients (e.g. `leaks`) that load the ASan allocator can
79   // find an initialized malloc zone.
80   InitMallocZoneFields();
81   return true;
82 }
83 }  // namespace COMMON_MALLOC_NAMESPACE
84
85 #endif