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_FUCHSIA || SANITIZER_LINUX || \
19 #include "sanitizer_common/sanitizer_tls_get_addr.h"
20 #include "asan_allocator.h"
21 #include "asan_interceptors.h"
22 #include "asan_internal.h"
23 #include "asan_stack.h"
25 // ---------------------- Replacement functions ---------------- {{{1
26 using namespace __asan
; // NOLINT
28 static uptr allocated_for_dlsym
;
29 static const uptr kDlsymAllocPoolSize
= 1024;
30 static uptr alloc_memory_for_dlsym
[kDlsymAllocPoolSize
];
32 static INLINE
bool IsInDlsymAllocPool(const void *ptr
) {
33 uptr off
= (uptr
)ptr
- (uptr
)alloc_memory_for_dlsym
;
34 return off
< allocated_for_dlsym
* sizeof(alloc_memory_for_dlsym
[0]);
37 static void *AllocateFromLocalPool(uptr size_in_bytes
) {
38 uptr size_in_words
= RoundUpTo(size_in_bytes
, kWordSize
) / kWordSize
;
39 void *mem
= (void*)&alloc_memory_for_dlsym
[allocated_for_dlsym
];
40 allocated_for_dlsym
+= size_in_words
;
41 CHECK_LT(allocated_for_dlsym
, kDlsymAllocPoolSize
);
45 static INLINE
bool MaybeInDlsym() {
46 // Fuchsia doesn't use dlsym-based interceptors.
47 return !SANITIZER_FUCHSIA
&& asan_init_is_running
;
50 static void *ReallocFromLocalPool(void *ptr
, uptr size
) {
51 const uptr offset
= (uptr
)ptr
- (uptr
)alloc_memory_for_dlsym
;
52 const uptr copy_size
= Min(size
, kDlsymAllocPoolSize
- offset
);
54 if (UNLIKELY(MaybeInDlsym())) {
55 new_ptr
= AllocateFromLocalPool(size
);
58 GET_STACK_TRACE_MALLOC
;
59 new_ptr
= asan_malloc(size
, &stack
);
61 internal_memcpy(new_ptr
, ptr
, copy_size
);
65 INTERCEPTOR(void, free
, void *ptr
) {
67 if (UNLIKELY(IsInDlsymAllocPool(ptr
)))
69 asan_free(ptr
, &stack
, FROM_MALLOC
);
72 #if SANITIZER_INTERCEPT_CFREE
73 INTERCEPTOR(void, cfree
, void *ptr
) {
75 if (UNLIKELY(IsInDlsymAllocPool(ptr
)))
77 asan_free(ptr
, &stack
, FROM_MALLOC
);
79 #endif // SANITIZER_INTERCEPT_CFREE
81 INTERCEPTOR(void*, malloc
, uptr size
) {
82 if (UNLIKELY(MaybeInDlsym()))
83 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
84 return AllocateFromLocalPool(size
);
86 GET_STACK_TRACE_MALLOC
;
87 return asan_malloc(size
, &stack
);
90 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
91 if (UNLIKELY(MaybeInDlsym()))
92 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
93 return AllocateFromLocalPool(nmemb
* size
);
95 GET_STACK_TRACE_MALLOC
;
96 return asan_calloc(nmemb
, size
, &stack
);
99 INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
) {
100 if (UNLIKELY(IsInDlsymAllocPool(ptr
)))
101 return ReallocFromLocalPool(ptr
, size
);
102 if (UNLIKELY(MaybeInDlsym()))
103 return AllocateFromLocalPool(size
);
104 ENSURE_ASAN_INITED();
105 GET_STACK_TRACE_MALLOC
;
106 return asan_realloc(ptr
, size
, &stack
);
109 #if SANITIZER_INTERCEPT_MEMALIGN
110 INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
) {
111 GET_STACK_TRACE_MALLOC
;
112 return asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
115 INTERCEPTOR(void*, __libc_memalign
, uptr boundary
, uptr size
) {
116 GET_STACK_TRACE_MALLOC
;
117 void *res
= asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
118 DTLS_on_libc_memalign(res
, size
);
121 #endif // SANITIZER_INTERCEPT_MEMALIGN
123 INTERCEPTOR(void*, aligned_alloc
, uptr boundary
, uptr size
) {
124 GET_STACK_TRACE_MALLOC
;
125 return asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
128 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
129 GET_CURRENT_PC_BP_SP
;
131 return asan_malloc_usable_size(ptr
, pc
, bp
);
134 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
135 // We avoid including malloc.h for portability reasons.
136 // man mallinfo says the fields are "long", but the implementation uses int.
137 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
139 struct fake_mallinfo
{
143 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
144 struct fake_mallinfo res
;
145 REAL(memset
)(&res
, 0, sizeof(res
));
149 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
152 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
154 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
155 GET_STACK_TRACE_MALLOC
;
156 // Printf("posix_memalign: %zx %zu\n", alignment, size);
157 return asan_posix_memalign(memptr
, alignment
, size
, &stack
);
160 INTERCEPTOR(void*, valloc
, uptr size
) {
161 GET_STACK_TRACE_MALLOC
;
162 return asan_valloc(size
, &stack
);
165 #if SANITIZER_INTERCEPT_PVALLOC
166 INTERCEPTOR(void*, pvalloc
, uptr size
) {
167 GET_STACK_TRACE_MALLOC
;
168 return asan_pvalloc(size
, &stack
);
170 #endif // SANITIZER_INTERCEPT_PVALLOC
172 INTERCEPTOR(void, malloc_stats
, void) {
173 __asan_print_accumulated_stats();
176 #if SANITIZER_ANDROID
177 // Format of __libc_malloc_dispatch has changed in Android L.
178 // While we are moving towards a solution that does not depend on bionic
179 // internals, here is something to support both K* and L releases.
180 struct MallocDebugK
{
181 void *(*malloc
)(uptr bytes
);
182 void (*free
)(void *mem
);
183 void *(*calloc
)(uptr n_elements
, uptr elem_size
);
184 void *(*realloc
)(void *oldMem
, uptr bytes
);
185 void *(*memalign
)(uptr alignment
, uptr bytes
);
186 uptr (*malloc_usable_size
)(void *mem
);
189 struct MallocDebugL
{
190 void *(*calloc
)(uptr n_elements
, uptr elem_size
);
191 void (*free
)(void *mem
);
192 fake_mallinfo (*mallinfo
)(void);
193 void *(*malloc
)(uptr bytes
);
194 uptr (*malloc_usable_size
)(void *mem
);
195 void *(*memalign
)(uptr alignment
, uptr bytes
);
196 int (*posix_memalign
)(void **memptr
, uptr alignment
, uptr size
);
197 void* (*pvalloc
)(uptr size
);
198 void *(*realloc
)(void *oldMem
, uptr bytes
);
199 void* (*valloc
)(uptr size
);
202 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k
= {
203 WRAP(malloc
), WRAP(free
), WRAP(calloc
),
204 WRAP(realloc
), WRAP(memalign
), WRAP(malloc_usable_size
)};
206 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l
= {
207 WRAP(calloc
), WRAP(free
), WRAP(mallinfo
),
208 WRAP(malloc
), WRAP(malloc_usable_size
), WRAP(memalign
),
209 WRAP(posix_memalign
), WRAP(pvalloc
), WRAP(realloc
),
213 void ReplaceSystemMalloc() {
214 void **__libc_malloc_dispatch_p
=
215 (void **)AsanDlSymNext("__libc_malloc_dispatch");
216 if (__libc_malloc_dispatch_p
) {
217 // Decide on K vs L dispatch format by the presence of
218 // __libc_malloc_default_dispatch export in libc.
219 void *default_dispatch_p
= AsanDlSymNext("__libc_malloc_default_dispatch");
220 if (default_dispatch_p
)
221 *__libc_malloc_dispatch_p
= (void *)&asan_malloc_dispatch_k
;
223 *__libc_malloc_dispatch_p
= (void *)&asan_malloc_dispatch_l
;
226 } // namespace __asan
228 #else // SANITIZER_ANDROID
231 void ReplaceSystemMalloc() {
233 } // namespace __asan
234 #endif // SANITIZER_ANDROID
236 #endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||