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 INTERCEPTOR(void, free, void *ptr) {
31 asan_free(ptr, &stack, FROM_MALLOC);
34 INTERCEPTOR(void, cfree, void *ptr) {
36 asan_free(ptr, &stack, FROM_MALLOC);
39 INTERCEPTOR(void*, malloc, uptr size) {
40 GET_STACK_TRACE_MALLOC;
41 return asan_malloc(size, &stack);
44 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
45 if (UNLIKELY(!asan_inited)) {
46 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
47 const uptr kCallocPoolSize = 1024;
48 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
49 static uptr allocated;
50 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
51 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
52 allocated += size_in_words;
53 CHECK(allocated < kCallocPoolSize);
56 GET_STACK_TRACE_MALLOC;
57 return asan_calloc(nmemb, size, &stack);
60 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
61 GET_STACK_TRACE_MALLOC;
62 return asan_realloc(ptr, size, &stack);
65 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
66 GET_STACK_TRACE_MALLOC;
67 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
70 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
71 GET_STACK_TRACE_MALLOC;
72 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
75 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
76 GET_STACK_TRACE_MALLOC;
77 void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
78 DTLS_on_libc_memalign(res, size * boundary);
82 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
85 return asan_malloc_usable_size(ptr, pc, bp);
88 // We avoid including malloc.h for portability reasons.
89 // man mallinfo says the fields are "long", but the implementation uses int.
90 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
92 struct fake_mallinfo {
96 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
97 struct fake_mallinfo res;
98 REAL(memset)(&res, 0, sizeof(res));
102 INTERCEPTOR(int, mallopt, int cmd, int value) {
106 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
107 GET_STACK_TRACE_MALLOC;
108 // Printf("posix_memalign: %zx %zu\n", alignment, size);
109 return asan_posix_memalign(memptr, alignment, size, &stack);
112 INTERCEPTOR(void*, valloc, uptr size) {
113 GET_STACK_TRACE_MALLOC;
114 return asan_valloc(size, &stack);
117 INTERCEPTOR(void*, pvalloc, uptr size) {
118 GET_STACK_TRACE_MALLOC;
119 return asan_pvalloc(size, &stack);
122 INTERCEPTOR(void, malloc_stats, void) {
123 __asan_print_accumulated_stats();
126 #if SANITIZER_ANDROID
127 // Format of __libc_malloc_dispatch has changed in Android L.
128 // While we are moving towards a solution that does not depend on bionic
129 // internals, here is something to support both K* and L releases.
130 struct MallocDebugK {
131 void *(*malloc)(uptr bytes);
132 void (*free)(void *mem);
133 void *(*calloc)(uptr n_elements, uptr elem_size);
134 void *(*realloc)(void *oldMem, uptr bytes);
135 void *(*memalign)(uptr alignment, uptr bytes);
136 uptr (*malloc_usable_size)(void *mem);
139 struct MallocDebugL {
140 void *(*calloc)(uptr n_elements, uptr elem_size);
141 void (*free)(void *mem);
142 fake_mallinfo (*mallinfo)(void);
143 void *(*malloc)(uptr bytes);
144 uptr (*malloc_usable_size)(void *mem);
145 void *(*memalign)(uptr alignment, uptr bytes);
146 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
147 void* (*pvalloc)(uptr size);
148 void *(*realloc)(void *oldMem, uptr bytes);
149 void* (*valloc)(uptr size);
152 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
153 WRAP(malloc), WRAP(free), WRAP(calloc),
154 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
156 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
157 WRAP(calloc), WRAP(free), WRAP(mallinfo),
158 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
159 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
163 void ReplaceSystemMalloc() {
164 void **__libc_malloc_dispatch_p =
165 (void **)AsanDlSymNext("__libc_malloc_dispatch");
166 if (__libc_malloc_dispatch_p) {
167 // Decide on K vs L dispatch format by the presence of
168 // __libc_malloc_default_dispatch export in libc.
169 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
170 if (default_dispatch_p)
171 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
173 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
176 } // namespace __asan
178 #else // SANITIZER_ANDROID
181 void ReplaceSystemMalloc() {
183 } // namespace __asan
184 #endif // SANITIZER_ANDROID
186 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX