]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_malloc_linux.cc
Import 1.14.3
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_malloc_linux.cc
1 //===-- asan_malloc_linux.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 // Linux-specific malloc interception.
13 // We simply define functions like malloc, free, realloc, etc.
14 // They will replace the corresponding libc functions automagically.
15 //===----------------------------------------------------------------------===//
16
17 #include "sanitizer_common/sanitizer_platform.h"
18 #if SANITIZER_FREEBSD || SANITIZER_LINUX
19
20 #include "sanitizer_common/sanitizer_tls_get_addr.h"
21 #include "asan_allocator.h"
22 #include "asan_interceptors.h"
23 #include "asan_internal.h"
24 #include "asan_stack.h"
25
26 // ---------------------- Replacement functions ---------------- {{{1
27 using namespace __asan;  // NOLINT
28
29 static uptr allocated_for_dlsym;
30 static const uptr kDlsymAllocPoolSize = 1024;
31 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
32
33 static bool IsInDlsymAllocPool(const void *ptr) {
34   uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
35   return off < sizeof(alloc_memory_for_dlsym);
36 }
37
38 static void *AllocateFromLocalPool(uptr size_in_bytes) {
39   uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
40   void *mem = (void*)&alloc_memory_for_dlsym[allocated_for_dlsym];
41   allocated_for_dlsym += size_in_words;
42   CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
43   return mem;
44 }
45
46 INTERCEPTOR(void, free, void *ptr) {
47   GET_STACK_TRACE_FREE;
48   if (UNLIKELY(IsInDlsymAllocPool(ptr)))
49     return;
50   asan_free(ptr, &stack, FROM_MALLOC);
51 }
52
53 #if SANITIZER_INTERCEPT_CFREE
54 INTERCEPTOR(void, cfree, void *ptr) {
55   GET_STACK_TRACE_FREE;
56   if (UNLIKELY(IsInDlsymAllocPool(ptr)))
57     return;
58   asan_free(ptr, &stack, FROM_MALLOC);
59 }
60 #endif // SANITIZER_INTERCEPT_CFREE
61
62 INTERCEPTOR(void*, malloc, uptr size) {
63   if (UNLIKELY(asan_init_is_running))
64     // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
65     return AllocateFromLocalPool(size);
66   ENSURE_ASAN_INITED();
67   GET_STACK_TRACE_MALLOC;
68   return asan_malloc(size, &stack);
69 }
70
71 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
72   if (UNLIKELY(asan_init_is_running))
73     // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
74     return AllocateFromLocalPool(nmemb * size);
75   ENSURE_ASAN_INITED();
76   GET_STACK_TRACE_MALLOC;
77   return asan_calloc(nmemb, size, &stack);
78 }
79
80 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
81   if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
82     const uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
83     const uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
84     void *new_ptr;
85     if (UNLIKELY(asan_init_is_running)) {
86       new_ptr = AllocateFromLocalPool(size);
87     } else {
88       ENSURE_ASAN_INITED();
89       GET_STACK_TRACE_MALLOC;
90       new_ptr = asan_malloc(size, &stack);
91     }
92     internal_memcpy(new_ptr, ptr, copy_size);
93     return new_ptr;
94   }
95   if (UNLIKELY(asan_init_is_running))
96     return AllocateFromLocalPool(size);
97   ENSURE_ASAN_INITED();
98   GET_STACK_TRACE_MALLOC;
99   return asan_realloc(ptr, size, &stack);
100 }
101
102 #if SANITIZER_INTERCEPT_MEMALIGN
103 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
104   GET_STACK_TRACE_MALLOC;
105   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
106 }
107
108 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
109   GET_STACK_TRACE_MALLOC;
110   void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
111   DTLS_on_libc_memalign(res, size);
112   return res;
113 }
114 #endif // SANITIZER_INTERCEPT_MEMALIGN
115
116 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
117   GET_STACK_TRACE_MALLOC;
118   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
119 }
120
121 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
122   GET_CURRENT_PC_BP_SP;
123   (void)sp;
124   return asan_malloc_usable_size(ptr, pc, bp);
125 }
126
127 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
128 // We avoid including malloc.h for portability reasons.
129 // man mallinfo says the fields are "long", but the implementation uses int.
130 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
131 // is not called.
132 struct fake_mallinfo {
133   int x[10];
134 };
135
136 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
137   struct fake_mallinfo res;
138   REAL(memset)(&res, 0, sizeof(res));
139   return res;
140 }
141
142 INTERCEPTOR(int, mallopt, int cmd, int value) {
143   return -1;
144 }
145 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
146
147 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
148   GET_STACK_TRACE_MALLOC;
149   // Printf("posix_memalign: %zx %zu\n", alignment, size);
150   return asan_posix_memalign(memptr, alignment, size, &stack);
151 }
152
153 INTERCEPTOR(void*, valloc, uptr size) {
154   GET_STACK_TRACE_MALLOC;
155   return asan_valloc(size, &stack);
156 }
157
158 #if SANITIZER_INTERCEPT_PVALLOC
159 INTERCEPTOR(void*, pvalloc, uptr size) {
160   GET_STACK_TRACE_MALLOC;
161   return asan_pvalloc(size, &stack);
162 }
163 #endif // SANITIZER_INTERCEPT_PVALLOC
164
165 INTERCEPTOR(void, malloc_stats, void) {
166   __asan_print_accumulated_stats();
167 }
168
169 #if SANITIZER_ANDROID
170 // Format of __libc_malloc_dispatch has changed in Android L.
171 // While we are moving towards a solution that does not depend on bionic
172 // internals, here is something to support both K* and L releases.
173 struct MallocDebugK {
174   void *(*malloc)(uptr bytes);
175   void (*free)(void *mem);
176   void *(*calloc)(uptr n_elements, uptr elem_size);
177   void *(*realloc)(void *oldMem, uptr bytes);
178   void *(*memalign)(uptr alignment, uptr bytes);
179   uptr (*malloc_usable_size)(void *mem);
180 };
181
182 struct MallocDebugL {
183   void *(*calloc)(uptr n_elements, uptr elem_size);
184   void (*free)(void *mem);
185   fake_mallinfo (*mallinfo)(void);
186   void *(*malloc)(uptr bytes);
187   uptr (*malloc_usable_size)(void *mem);
188   void *(*memalign)(uptr alignment, uptr bytes);
189   int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
190   void* (*pvalloc)(uptr size);
191   void *(*realloc)(void *oldMem, uptr bytes);
192   void* (*valloc)(uptr size);
193 };
194
195 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
196     WRAP(malloc),  WRAP(free),     WRAP(calloc),
197     WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
198
199 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
200     WRAP(calloc),         WRAP(free),               WRAP(mallinfo),
201     WRAP(malloc),         WRAP(malloc_usable_size), WRAP(memalign),
202     WRAP(posix_memalign), WRAP(pvalloc),            WRAP(realloc),
203     WRAP(valloc)};
204
205 namespace __asan {
206 void ReplaceSystemMalloc() {
207   void **__libc_malloc_dispatch_p =
208       (void **)AsanDlSymNext("__libc_malloc_dispatch");
209   if (__libc_malloc_dispatch_p) {
210     // Decide on K vs L dispatch format by the presence of
211     // __libc_malloc_default_dispatch export in libc.
212     void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
213     if (default_dispatch_p)
214       *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
215     else
216       *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
217   }
218 }
219 }  // namespace __asan
220
221 #else  // SANITIZER_ANDROID
222
223 namespace __asan {
224 void ReplaceSystemMalloc() {
225 }
226 }  // namespace __asan
227 #endif  // SANITIZER_ANDROID
228
229 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX