1 //===-- asan_malloc_linux.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 // Linux-specific malloc interception.
11 // We simply define functions like malloc, free, realloc, etc.
12 // They will replace the corresponding libc functions automagically.
13 //===----------------------------------------------------------------------===//
14 
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_LINUX
17 
18 #include "asan_allocator.h"
19 #include "asan_interceptors.h"
20 #include "asan_internal.h"
21 #include "asan_stack.h"
22 
23 #if SANITIZER_ANDROID
24 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size)
25 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
26 DECLARE_REAL_AND_INTERCEPTOR(void*, calloc, uptr nmemb, uptr size)
27 DECLARE_REAL_AND_INTERCEPTOR(void*, realloc, void *ptr, uptr size)
28 DECLARE_REAL_AND_INTERCEPTOR(void*, memalign, uptr boundary, uptr size)
29 
30 struct MallocDebug {
31   void* (*malloc)(uptr bytes);
32   void  (*free)(void* mem);
33   void* (*calloc)(uptr n_elements, uptr elem_size);
34   void* (*realloc)(void* oldMem, uptr bytes);
35   void* (*memalign)(uptr alignment, uptr bytes);
36 };
37 
38 const MallocDebug asan_malloc_dispatch ALIGNED(32) = {
39   WRAP(malloc), WRAP(free), WRAP(calloc), WRAP(realloc), WRAP(memalign)
40 };
41 
42 extern "C" const MallocDebug* __libc_malloc_dispatch;
43 
44 namespace __asan {
ReplaceSystemMalloc()45 void ReplaceSystemMalloc() {
46   __libc_malloc_dispatch = &asan_malloc_dispatch;
47 }
48 }  // namespace __asan
49 
50 #else  // ANDROID
51 
52 namespace __asan {
ReplaceSystemMalloc()53 void ReplaceSystemMalloc() {
54 }
55 }  // namespace __asan
56 #endif  // ANDROID
57 
58 // ---------------------- Replacement functions ---------------- {{{1
59 using namespace __asan;  // NOLINT
60 
INTERCEPTOR(void,free,void * ptr)61 INTERCEPTOR(void, free, void *ptr) {
62   GET_STACK_TRACE_FREE;
63   asan_free(ptr, &stack, FROM_MALLOC);
64 }
65 
INTERCEPTOR(void,cfree,void * ptr)66 INTERCEPTOR(void, cfree, void *ptr) {
67   GET_STACK_TRACE_FREE;
68   asan_free(ptr, &stack, FROM_MALLOC);
69 }
70 
INTERCEPTOR(void *,malloc,uptr size)71 INTERCEPTOR(void*, malloc, uptr size) {
72   GET_STACK_TRACE_MALLOC;
73   return asan_malloc(size, &stack);
74 }
75 
INTERCEPTOR(void *,calloc,uptr nmemb,uptr size)76 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
77   if (!asan_inited) {
78     // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
79     const uptr kCallocPoolSize = 1024;
80     static uptr calloc_memory_for_dlsym[kCallocPoolSize];
81     static uptr allocated;
82     uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
83     void *mem = (void*)&calloc_memory_for_dlsym[allocated];
84     allocated += size_in_words;
85     CHECK(allocated < kCallocPoolSize);
86     return mem;
87   }
88   GET_STACK_TRACE_MALLOC;
89   return asan_calloc(nmemb, size, &stack);
90 }
91 
INTERCEPTOR(void *,realloc,void * ptr,uptr size)92 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
93   GET_STACK_TRACE_MALLOC;
94   return asan_realloc(ptr, size, &stack);
95 }
96 
INTERCEPTOR(void *,memalign,uptr boundary,uptr size)97 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
98   GET_STACK_TRACE_MALLOC;
99   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
100 }
101 
102 INTERCEPTOR(void*, __libc_memalign, uptr align, uptr s)
103   ALIAS("memalign");
104 
INTERCEPTOR(uptr,malloc_usable_size,void * ptr)105 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
106   GET_CURRENT_PC_BP_SP;
107   (void)sp;
108   return asan_malloc_usable_size(ptr, pc, bp);
109 }
110 
111 // We avoid including malloc.h for portability reasons.
112 // man mallinfo says the fields are "long", but the implementation uses int.
113 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
114 // is not called.
115 struct fake_mallinfo {
116   int x[10];
117 };
118 
INTERCEPTOR(struct fake_mallinfo,mallinfo,void)119 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
120   struct fake_mallinfo res;
121   REAL(memset)(&res, 0, sizeof(res));
122   return res;
123 }
124 
INTERCEPTOR(int,mallopt,int cmd,int value)125 INTERCEPTOR(int, mallopt, int cmd, int value) {
126   return -1;
127 }
128 
INTERCEPTOR(int,posix_memalign,void ** memptr,uptr alignment,uptr size)129 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
130   GET_STACK_TRACE_MALLOC;
131   // Printf("posix_memalign: %zx %zu\n", alignment, size);
132   return asan_posix_memalign(memptr, alignment, size, &stack);
133 }
134 
INTERCEPTOR(void *,valloc,uptr size)135 INTERCEPTOR(void*, valloc, uptr size) {
136   GET_STACK_TRACE_MALLOC;
137   return asan_valloc(size, &stack);
138 }
139 
INTERCEPTOR(void *,pvalloc,uptr size)140 INTERCEPTOR(void*, pvalloc, uptr size) {
141   GET_STACK_TRACE_MALLOC;
142   return asan_pvalloc(size, &stack);
143 }
144 
INTERCEPTOR(void,malloc_stats,void)145 INTERCEPTOR(void, malloc_stats, void) {
146   __asan_print_accumulated_stats();
147 }
148 
149 #endif  // SANITIZER_LINUX
150