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 INTERCEPTOR(void, cfree, void *ptr) {
55 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
57 asan_free(ptr, &stack, FROM_MALLOC);
60 INTERCEPTOR(void*, malloc, uptr size) {
61 if (UNLIKELY(!asan_inited))
62 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
63 return AllocateFromLocalPool(size);
64 GET_STACK_TRACE_MALLOC;
65 return asan_malloc(size, &stack);
68 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
69 if (UNLIKELY(!asan_inited))
70 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
71 return AllocateFromLocalPool(nmemb * size);
72 GET_STACK_TRACE_MALLOC;
73 return asan_calloc(nmemb, size, &stack);
76 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
77 GET_STACK_TRACE_MALLOC;
78 if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
79 uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
80 uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
81 void *new_ptr = asan_malloc(size, &stack);
82 internal_memcpy(new_ptr, ptr, copy_size);
85 return asan_realloc(ptr, size, &stack);
88 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
89 GET_STACK_TRACE_MALLOC;
90 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
93 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
94 GET_STACK_TRACE_MALLOC;
95 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
98 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
99 GET_STACK_TRACE_MALLOC;
100 void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
101 DTLS_on_libc_memalign(res, size);
105 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
106 GET_CURRENT_PC_BP_SP;
108 return asan_malloc_usable_size(ptr, pc, bp);
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
115 struct fake_mallinfo {
119 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
120 struct fake_mallinfo res;
121 REAL(memset)(&res, 0, sizeof(res));
125 INTERCEPTOR(int, mallopt, int cmd, int value) {
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);
135 INTERCEPTOR(void*, valloc, uptr size) {
136 GET_STACK_TRACE_MALLOC;
137 return asan_valloc(size, &stack);
140 INTERCEPTOR(void*, pvalloc, uptr size) {
141 GET_STACK_TRACE_MALLOC;
142 return asan_pvalloc(size, &stack);
145 INTERCEPTOR(void, malloc_stats, void) {
146 __asan_print_accumulated_stats();
149 #if SANITIZER_ANDROID
150 // Format of __libc_malloc_dispatch has changed in Android L.
151 // While we are moving towards a solution that does not depend on bionic
152 // internals, here is something to support both K* and L releases.
153 struct MallocDebugK {
154 void *(*malloc)(uptr bytes);
155 void (*free)(void *mem);
156 void *(*calloc)(uptr n_elements, uptr elem_size);
157 void *(*realloc)(void *oldMem, uptr bytes);
158 void *(*memalign)(uptr alignment, uptr bytes);
159 uptr (*malloc_usable_size)(void *mem);
162 struct MallocDebugL {
163 void *(*calloc)(uptr n_elements, uptr elem_size);
164 void (*free)(void *mem);
165 fake_mallinfo (*mallinfo)(void);
166 void *(*malloc)(uptr bytes);
167 uptr (*malloc_usable_size)(void *mem);
168 void *(*memalign)(uptr alignment, uptr bytes);
169 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
170 void* (*pvalloc)(uptr size);
171 void *(*realloc)(void *oldMem, uptr bytes);
172 void* (*valloc)(uptr size);
175 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
176 WRAP(malloc), WRAP(free), WRAP(calloc),
177 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
179 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
180 WRAP(calloc), WRAP(free), WRAP(mallinfo),
181 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
182 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
186 void ReplaceSystemMalloc() {
187 void **__libc_malloc_dispatch_p =
188 (void **)AsanDlSymNext("__libc_malloc_dispatch");
189 if (__libc_malloc_dispatch_p) {
190 // Decide on K vs L dispatch format by the presence of
191 // __libc_malloc_default_dispatch export in libc.
192 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
193 if (default_dispatch_p)
194 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
196 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
199 } // namespace __asan
201 #else // SANITIZER_ANDROID
204 void ReplaceSystemMalloc() {
206 } // namespace __asan
207 #endif // SANITIZER_ANDROID
209 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX