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 const uptr kCallocPoolSize = 1024;
30 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
32 static bool IsInCallocPool(const void *ptr) {
33 sptr off = (sptr)ptr - (sptr)calloc_memory_for_dlsym;
34 return 0 <= off && off < (sptr)kCallocPoolSize;
37 INTERCEPTOR(void, free, void *ptr) {
39 if (UNLIKELY(IsInCallocPool(ptr)))
41 asan_free(ptr, &stack, FROM_MALLOC);
44 INTERCEPTOR(void, cfree, void *ptr) {
46 if (UNLIKELY(IsInCallocPool(ptr)))
48 asan_free(ptr, &stack, FROM_MALLOC);
51 INTERCEPTOR(void*, malloc, uptr size) {
52 GET_STACK_TRACE_MALLOC;
53 return asan_malloc(size, &stack);
56 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
57 if (UNLIKELY(!asan_inited)) {
58 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
59 static uptr allocated;
60 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
61 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
62 allocated += size_in_words;
63 CHECK(allocated < kCallocPoolSize);
66 GET_STACK_TRACE_MALLOC;
67 return asan_calloc(nmemb, size, &stack);
70 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
71 GET_STACK_TRACE_MALLOC;
72 if (UNLIKELY(IsInCallocPool(ptr))) {
73 uptr offset = (uptr)ptr - (uptr)calloc_memory_for_dlsym;
74 uptr copy_size = Min(size, kCallocPoolSize - offset);
75 void *new_ptr = asan_malloc(size, &stack);
76 internal_memcpy(new_ptr, ptr, copy_size);
79 return asan_realloc(ptr, size, &stack);
82 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
83 GET_STACK_TRACE_MALLOC;
84 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
87 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
88 GET_STACK_TRACE_MALLOC;
89 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
92 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
93 GET_STACK_TRACE_MALLOC;
94 void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
95 DTLS_on_libc_memalign(res, size * boundary);
99 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
100 GET_CURRENT_PC_BP_SP;
102 return asan_malloc_usable_size(ptr, pc, bp);
105 // We avoid including malloc.h for portability reasons.
106 // man mallinfo says the fields are "long", but the implementation uses int.
107 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
109 struct fake_mallinfo {
113 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
114 struct fake_mallinfo res;
115 REAL(memset)(&res, 0, sizeof(res));
119 INTERCEPTOR(int, mallopt, int cmd, int value) {
123 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
124 GET_STACK_TRACE_MALLOC;
125 // Printf("posix_memalign: %zx %zu\n", alignment, size);
126 return asan_posix_memalign(memptr, alignment, size, &stack);
129 INTERCEPTOR(void*, valloc, uptr size) {
130 GET_STACK_TRACE_MALLOC;
131 return asan_valloc(size, &stack);
134 INTERCEPTOR(void*, pvalloc, uptr size) {
135 GET_STACK_TRACE_MALLOC;
136 return asan_pvalloc(size, &stack);
139 INTERCEPTOR(void, malloc_stats, void) {
140 __asan_print_accumulated_stats();
143 #if SANITIZER_ANDROID
144 // Format of __libc_malloc_dispatch has changed in Android L.
145 // While we are moving towards a solution that does not depend on bionic
146 // internals, here is something to support both K* and L releases.
147 struct MallocDebugK {
148 void *(*malloc)(uptr bytes);
149 void (*free)(void *mem);
150 void *(*calloc)(uptr n_elements, uptr elem_size);
151 void *(*realloc)(void *oldMem, uptr bytes);
152 void *(*memalign)(uptr alignment, uptr bytes);
153 uptr (*malloc_usable_size)(void *mem);
156 struct MallocDebugL {
157 void *(*calloc)(uptr n_elements, uptr elem_size);
158 void (*free)(void *mem);
159 fake_mallinfo (*mallinfo)(void);
160 void *(*malloc)(uptr bytes);
161 uptr (*malloc_usable_size)(void *mem);
162 void *(*memalign)(uptr alignment, uptr bytes);
163 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
164 void* (*pvalloc)(uptr size);
165 void *(*realloc)(void *oldMem, uptr bytes);
166 void* (*valloc)(uptr size);
169 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
170 WRAP(malloc), WRAP(free), WRAP(calloc),
171 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
173 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
174 WRAP(calloc), WRAP(free), WRAP(mallinfo),
175 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
176 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
180 void ReplaceSystemMalloc() {
181 void **__libc_malloc_dispatch_p =
182 (void **)AsanDlSymNext("__libc_malloc_dispatch");
183 if (__libc_malloc_dispatch_p) {
184 // Decide on K vs L dispatch format by the presence of
185 // __libc_malloc_default_dispatch export in libc.
186 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
187 if (default_dispatch_p)
188 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
190 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
193 } // namespace __asan
195 #else // SANITIZER_ANDROID
198 void ReplaceSystemMalloc() {
200 } // namespace __asan
201 #endif // SANITIZER_ANDROID
203 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX