1 //===-- asan_malloc_linux.cc ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
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 //===----------------------------------------------------------------------===//
17 #include "sanitizer_common/sanitizer_platform.h"
18 #if SANITIZER_FREEBSD || SANITIZER_LINUX
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"
26 // ---------------------- Replacement functions ---------------- {{{1
27 using namespace __asan; // NOLINT
29 static uptr allocated_for_dlsym;
30 static const uptr kDlsymAllocPoolSize = 1024;
31 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
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);
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);
46 INTERCEPTOR(void, free, void *ptr) {
48 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
50 asan_free(ptr, &stack, FROM_MALLOC);
53 #if SANITIZER_INTERCEPT_CFREE
54 INTERCEPTOR(void, cfree, void *ptr) {
56 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
58 asan_free(ptr, &stack, FROM_MALLOC);
60 #endif // SANITIZER_INTERCEPT_CFREE
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);
67 GET_STACK_TRACE_MALLOC;
68 return asan_malloc(size, &stack);
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);
76 GET_STACK_TRACE_MALLOC;
77 return asan_calloc(nmemb, size, &stack);
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);
85 if (UNLIKELY(asan_init_is_running)) {
86 new_ptr = AllocateFromLocalPool(size);
89 GET_STACK_TRACE_MALLOC;
90 new_ptr = asan_malloc(size, &stack);
92 internal_memcpy(new_ptr, ptr, copy_size);
95 if (UNLIKELY(asan_init_is_running))
96 return AllocateFromLocalPool(size);
98 GET_STACK_TRACE_MALLOC;
99 return asan_realloc(ptr, size, &stack);
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);
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);
114 #endif // SANITIZER_INTERCEPT_MEMALIGN
116 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
117 GET_STACK_TRACE_MALLOC;
118 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
121 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
122 GET_CURRENT_PC_BP_SP;
124 return asan_malloc_usable_size(ptr, pc, bp);
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
132 struct fake_mallinfo {
136 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
137 struct fake_mallinfo res;
138 REAL(memset)(&res, 0, sizeof(res));
142 INTERCEPTOR(int, mallopt, int cmd, int value) {
145 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
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);
153 INTERCEPTOR(void*, valloc, uptr size) {
154 GET_STACK_TRACE_MALLOC;
155 return asan_valloc(size, &stack);
158 #if SANITIZER_INTERCEPT_PVALLOC
159 INTERCEPTOR(void*, pvalloc, uptr size) {
160 GET_STACK_TRACE_MALLOC;
161 return asan_pvalloc(size, &stack);
163 #endif // SANITIZER_INTERCEPT_PVALLOC
165 INTERCEPTOR(void, malloc_stats, void) {
166 __asan_print_accumulated_stats();
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);
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);
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)};
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),
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;
216 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
219 } // namespace __asan
221 #else // SANITIZER_ANDROID
224 void ReplaceSystemMalloc() {
226 } // namespace __asan
227 #endif // SANITIZER_ANDROID
229 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX