1 //===-- asan_malloc_mac.cc ------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Mac-specific malloc interception.
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_MAC
15 
16 #include "asan_interceptors.h"
17 #include "asan_report.h"
18 #include "asan_stack.h"
19 #include "asan_stats.h"
20 
21 using namespace __asan;
22 #define COMMON_MALLOC_ZONE_NAME "asan"
23 #define COMMON_MALLOC_ENTER() ENSURE_ASAN_INITED()
24 #define COMMON_MALLOC_SANITIZER_INITIALIZED asan_inited
25 #define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()
26 #define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()
27 #define COMMON_MALLOC_MEMALIGN(alignment, size) \
28   GET_STACK_TRACE_MALLOC; \
29   void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)
30 #define COMMON_MALLOC_MALLOC(size) \
31   GET_STACK_TRACE_MALLOC; \
32   void *p = asan_malloc(size, &stack)
33 #define COMMON_MALLOC_REALLOC(ptr, size) \
34   GET_STACK_TRACE_MALLOC; \
35   void *p = asan_realloc(ptr, size, &stack);
36 #define COMMON_MALLOC_CALLOC(count, size) \
37   GET_STACK_TRACE_MALLOC; \
38   void *p = asan_calloc(count, size, &stack);
39 #define COMMON_MALLOC_VALLOC(size) \
40   GET_STACK_TRACE_MALLOC; \
41   void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
42 #define COMMON_MALLOC_FREE(ptr) \
43   GET_STACK_TRACE_FREE; \
44   asan_free(ptr, &stack, FROM_MALLOC);
45 #define COMMON_MALLOC_SIZE(ptr) \
46   uptr size = asan_mz_size(ptr);
47 #define COMMON_MALLOC_FILL_STATS(zone, stats) \
48   AsanMallocStats malloc_stats; \
49   FillMallocStatistics(&malloc_stats); \
50   CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
51   internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
52 #define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
53   GET_STACK_TRACE_FREE; \
54   ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
55 #define COMMON_MALLOC_NAMESPACE __asan
56 
57 #include "sanitizer_common/sanitizer_malloc_mac.inc"
58 
59 #endif
60