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 //===----------------------------------------------------------------------===//
16 #include "asan_allocator.h"
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_stack.h"
22 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc
, uptr size
)
23 DECLARE_REAL_AND_INTERCEPTOR(void, free
, void *ptr
)
24 DECLARE_REAL_AND_INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
)
25 DECLARE_REAL_AND_INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
)
26 DECLARE_REAL_AND_INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
)
29 void* (*malloc
)(uptr bytes
);
30 void (*free
)(void* mem
);
31 void* (*calloc
)(uptr n_elements
, uptr elem_size
);
32 void* (*realloc
)(void* oldMem
, uptr bytes
);
33 void* (*memalign
)(uptr alignment
, uptr bytes
);
36 const MallocDebug asan_malloc_dispatch
ALIGNED(32) = {
37 WRAP(malloc
), WRAP(free
), WRAP(calloc
), WRAP(realloc
), WRAP(memalign
)
40 extern "C" const MallocDebug
* __libc_malloc_dispatch
;
43 void ReplaceSystemMalloc() {
44 __libc_malloc_dispatch
= &asan_malloc_dispatch
;
51 void ReplaceSystemMalloc() {
56 // ---------------------- Replacement functions ---------------- {{{1
57 using namespace __asan
; // NOLINT
59 INTERCEPTOR(void, free
, void *ptr
) {
60 GET_STACK_TRACE_HERE_FOR_FREE(ptr
);
61 asan_free(ptr
, &stack
);
64 INTERCEPTOR(void, cfree
, void *ptr
) {
65 GET_STACK_TRACE_HERE_FOR_FREE(ptr
);
66 asan_free(ptr
, &stack
);
69 INTERCEPTOR(void*, malloc
, uptr size
) {
70 GET_STACK_TRACE_HERE_FOR_MALLOC
;
71 return asan_malloc(size
, &stack
);
74 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
76 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
77 const uptr kCallocPoolSize
= 1024;
78 static uptr calloc_memory_for_dlsym
[kCallocPoolSize
];
79 static uptr allocated
;
80 uptr size_in_words
= ((nmemb
* size
) + kWordSize
- 1) / kWordSize
;
81 void *mem
= (void*)&calloc_memory_for_dlsym
[allocated
];
82 allocated
+= size_in_words
;
83 CHECK(allocated
< kCallocPoolSize
);
86 GET_STACK_TRACE_HERE_FOR_MALLOC
;
87 return asan_calloc(nmemb
, size
, &stack
);
90 INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
) {
91 GET_STACK_TRACE_HERE_FOR_MALLOC
;
92 return asan_realloc(ptr
, size
, &stack
);
95 INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
) {
96 GET_STACK_TRACE_HERE_FOR_MALLOC
;
97 return asan_memalign(boundary
, size
, &stack
);
100 INTERCEPTOR(void*, __libc_memalign
, uptr align
, uptr s
)
103 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
104 GET_STACK_TRACE_HERE_FOR_MALLOC
;
105 return asan_malloc_usable_size(ptr
, &stack
);
108 // We avoid including malloc.h for portability reasons.
109 // man mallinfo says the fields are "long", but the implementation uses int.
110 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
112 struct fake_mallinfo
{
116 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
117 struct fake_mallinfo res
;
118 REAL(memset
)(&res
, 0, sizeof(res
));
122 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
126 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
127 GET_STACK_TRACE_HERE_FOR_MALLOC
;
128 // Printf("posix_memalign: %zx %zu\n", alignment, size);
129 return asan_posix_memalign(memptr
, alignment
, size
, &stack
);
132 INTERCEPTOR(void*, valloc
, uptr size
) {
133 GET_STACK_TRACE_HERE_FOR_MALLOC
;
134 return asan_valloc(size
, &stack
);
137 INTERCEPTOR(void*, pvalloc
, uptr size
) {
138 GET_STACK_TRACE_HERE_FOR_MALLOC
;
139 return asan_pvalloc(size
, &stack
);