1 //===-- asan_malloc_linux.cc ----------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Linux-specific malloc interception.
11 // We simply define functions like malloc, free, realloc, etc.
12 // They will replace the corresponding libc functions automagically.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX
18 #include "sanitizer_common/sanitizer_tls_get_addr.h"
19 #include "asan_allocator.h"
20 #include "asan_interceptors.h"
21 #include "asan_internal.h"
22 #include "asan_stack.h"
24 // ---------------------- Replacement functions ---------------- {{{1
25 using namespace __asan
; // NOLINT
27 static uptr allocated_for_dlsym
;
28 static const uptr kDlsymAllocPoolSize
= 1024;
29 static uptr alloc_memory_for_dlsym
[kDlsymAllocPoolSize
];
31 static bool IsInDlsymAllocPool(const void *ptr
) {
32 uptr off
= (uptr
)ptr
- (uptr
)alloc_memory_for_dlsym
;
33 return off
< sizeof(alloc_memory_for_dlsym
);
36 static void *AllocateFromLocalPool(uptr size_in_bytes
) {
37 uptr size_in_words
= RoundUpTo(size_in_bytes
, kWordSize
) / kWordSize
;
38 void *mem
= (void*)&alloc_memory_for_dlsym
[allocated_for_dlsym
];
39 allocated_for_dlsym
+= size_in_words
;
40 CHECK_LT(allocated_for_dlsym
, kDlsymAllocPoolSize
);
44 INTERCEPTOR(void, free
, void *ptr
) {
46 if (UNLIKELY(IsInDlsymAllocPool(ptr
)))
48 asan_free(ptr
, &stack
, FROM_MALLOC
);
51 INTERCEPTOR(void, cfree
, void *ptr
) {
53 if (UNLIKELY(IsInDlsymAllocPool(ptr
)))
55 asan_free(ptr
, &stack
, FROM_MALLOC
);
58 INTERCEPTOR(void*, malloc
, uptr size
) {
59 if (UNLIKELY(!asan_inited
))
60 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
61 return AllocateFromLocalPool(size
);
62 GET_STACK_TRACE_MALLOC
;
63 return asan_malloc(size
, &stack
);
66 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
67 if (UNLIKELY(!asan_inited
))
68 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
69 return AllocateFromLocalPool(nmemb
* size
);
70 GET_STACK_TRACE_MALLOC
;
71 return asan_calloc(nmemb
, size
, &stack
);
74 INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
) {
75 GET_STACK_TRACE_MALLOC
;
76 if (UNLIKELY(IsInDlsymAllocPool(ptr
))) {
77 uptr offset
= (uptr
)ptr
- (uptr
)alloc_memory_for_dlsym
;
78 uptr copy_size
= Min(size
, kDlsymAllocPoolSize
- offset
);
79 void *new_ptr
= asan_malloc(size
, &stack
);
80 internal_memcpy(new_ptr
, ptr
, copy_size
);
83 return asan_realloc(ptr
, size
, &stack
);
86 INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
) {
87 GET_STACK_TRACE_MALLOC
;
88 return asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
91 INTERCEPTOR(void*, aligned_alloc
, uptr boundary
, uptr size
) {
92 GET_STACK_TRACE_MALLOC
;
93 return asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
96 INTERCEPTOR(void*, __libc_memalign
, uptr boundary
, uptr size
) {
97 GET_STACK_TRACE_MALLOC
;
98 void *res
= asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
99 DTLS_on_libc_memalign(res
, size
* boundary
);
103 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
104 GET_CURRENT_PC_BP_SP
;
106 return asan_malloc_usable_size(ptr
, pc
, bp
);
109 // We avoid including malloc.h for portability reasons.
110 // man mallinfo says the fields are "long", but the implementation uses int.
111 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
113 struct fake_mallinfo
{
117 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
118 struct fake_mallinfo res
;
119 REAL(memset
)(&res
, 0, sizeof(res
));
123 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
127 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
128 GET_STACK_TRACE_MALLOC
;
129 // Printf("posix_memalign: %zx %zu\n", alignment, size);
130 return asan_posix_memalign(memptr
, alignment
, size
, &stack
);
133 INTERCEPTOR(void*, valloc
, uptr size
) {
134 GET_STACK_TRACE_MALLOC
;
135 return asan_valloc(size
, &stack
);
138 INTERCEPTOR(void*, pvalloc
, uptr size
) {
139 GET_STACK_TRACE_MALLOC
;
140 return asan_pvalloc(size
, &stack
);
143 INTERCEPTOR(void, malloc_stats
, void) {
144 __asan_print_accumulated_stats();
147 #if SANITIZER_ANDROID
148 // Format of __libc_malloc_dispatch has changed in Android L.
149 // While we are moving towards a solution that does not depend on bionic
150 // internals, here is something to support both K* and L releases.
151 struct MallocDebugK
{
152 void *(*malloc
)(uptr bytes
);
153 void (*free
)(void *mem
);
154 void *(*calloc
)(uptr n_elements
, uptr elem_size
);
155 void *(*realloc
)(void *oldMem
, uptr bytes
);
156 void *(*memalign
)(uptr alignment
, uptr bytes
);
157 uptr (*malloc_usable_size
)(void *mem
);
160 struct MallocDebugL
{
161 void *(*calloc
)(uptr n_elements
, uptr elem_size
);
162 void (*free
)(void *mem
);
163 fake_mallinfo (*mallinfo
)(void);
164 void *(*malloc
)(uptr bytes
);
165 uptr (*malloc_usable_size
)(void *mem
);
166 void *(*memalign
)(uptr alignment
, uptr bytes
);
167 int (*posix_memalign
)(void **memptr
, uptr alignment
, uptr size
);
168 void* (*pvalloc
)(uptr size
);
169 void *(*realloc
)(void *oldMem
, uptr bytes
);
170 void* (*valloc
)(uptr size
);
173 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k
= {
174 WRAP(malloc
), WRAP(free
), WRAP(calloc
),
175 WRAP(realloc
), WRAP(memalign
), WRAP(malloc_usable_size
)};
177 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l
= {
178 WRAP(calloc
), WRAP(free
), WRAP(mallinfo
),
179 WRAP(malloc
), WRAP(malloc_usable_size
), WRAP(memalign
),
180 WRAP(posix_memalign
), WRAP(pvalloc
), WRAP(realloc
),
184 void ReplaceSystemMalloc() {
185 void **__libc_malloc_dispatch_p
=
186 (void **)AsanDlSymNext("__libc_malloc_dispatch");
187 if (__libc_malloc_dispatch_p
) {
188 // Decide on K vs L dispatch format by the presence of
189 // __libc_malloc_default_dispatch export in libc.
190 void *default_dispatch_p
= AsanDlSymNext("__libc_malloc_default_dispatch");
191 if (default_dispatch_p
)
192 *__libc_malloc_dispatch_p
= (void *)&asan_malloc_dispatch_k
;
194 *__libc_malloc_dispatch_p
= (void *)&asan_malloc_dispatch_l
;
197 } // namespace __asan
199 #else // SANITIZER_ANDROID
202 void ReplaceSystemMalloc() {
204 } // namespace __asan
205 #endif // SANITIZER_ANDROID
207 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX