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"
25 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc
, uptr size
)
26 DECLARE_REAL_AND_INTERCEPTOR(void, free
, void *ptr
)
27 DECLARE_REAL_AND_INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
)
28 DECLARE_REAL_AND_INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
)
29 DECLARE_REAL_AND_INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
)
32 void* (*malloc
)(uptr bytes
);
33 void (*free
)(void* mem
);
34 void* (*calloc
)(uptr n_elements
, uptr elem_size
);
35 void* (*realloc
)(void* oldMem
, uptr bytes
);
36 void* (*memalign
)(uptr alignment
, uptr bytes
);
39 const MallocDebug asan_malloc_dispatch
ALIGNED(32) = {
40 WRAP(malloc
), WRAP(free
), WRAP(calloc
), WRAP(realloc
), WRAP(memalign
)
43 extern "C" const MallocDebug
* __libc_malloc_dispatch
;
46 void ReplaceSystemMalloc() {
47 __libc_malloc_dispatch
= &asan_malloc_dispatch
;
54 void ReplaceSystemMalloc() {
59 // ---------------------- Replacement functions ---------------- {{{1
60 using namespace __asan
; // NOLINT
62 INTERCEPTOR(void, free
, void *ptr
) {
64 asan_free(ptr
, &stack
, FROM_MALLOC
);
67 INTERCEPTOR(void, cfree
, void *ptr
) {
69 asan_free(ptr
, &stack
, FROM_MALLOC
);
72 INTERCEPTOR(void*, malloc
, uptr size
) {
73 GET_STACK_TRACE_MALLOC
;
74 return asan_malloc(size
, &stack
);
77 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
78 if (UNLIKELY(!asan_inited
)) {
79 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
80 const uptr kCallocPoolSize
= 1024;
81 static uptr calloc_memory_for_dlsym
[kCallocPoolSize
];
82 static uptr allocated
;
83 uptr size_in_words
= ((nmemb
* size
) + kWordSize
- 1) / kWordSize
;
84 void *mem
= (void*)&calloc_memory_for_dlsym
[allocated
];
85 allocated
+= size_in_words
;
86 CHECK(allocated
< kCallocPoolSize
);
89 GET_STACK_TRACE_MALLOC
;
90 return asan_calloc(nmemb
, size
, &stack
);
93 INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
) {
94 GET_STACK_TRACE_MALLOC
;
95 return asan_realloc(ptr
, size
, &stack
);
98 INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
) {
99 GET_STACK_TRACE_MALLOC
;
100 return asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
103 INTERCEPTOR(void*, __libc_memalign
, uptr boundary
, uptr size
) {
104 GET_STACK_TRACE_MALLOC
;
105 void *res
= asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
106 DTLS_on_libc_memalign(res
, size
* boundary
);
110 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
111 GET_CURRENT_PC_BP_SP
;
113 return asan_malloc_usable_size(ptr
, pc
, bp
);
116 // We avoid including malloc.h for portability reasons.
117 // man mallinfo says the fields are "long", but the implementation uses int.
118 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
120 struct fake_mallinfo
{
124 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
125 struct fake_mallinfo res
;
126 REAL(memset
)(&res
, 0, sizeof(res
));
130 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
134 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
135 GET_STACK_TRACE_MALLOC
;
136 // Printf("posix_memalign: %zx %zu\n", alignment, size);
137 return asan_posix_memalign(memptr
, alignment
, size
, &stack
);
140 INTERCEPTOR(void*, valloc
, uptr size
) {
141 GET_STACK_TRACE_MALLOC
;
142 return asan_valloc(size
, &stack
);
145 INTERCEPTOR(void*, pvalloc
, uptr size
) {
146 GET_STACK_TRACE_MALLOC
;
147 return asan_pvalloc(size
, &stack
);
150 INTERCEPTOR(void, malloc_stats
, void) {
151 __asan_print_accumulated_stats();
154 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX