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"
27 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc
, uptr size
)
28 DECLARE_REAL_AND_INTERCEPTOR(void, free
, void *ptr
)
29 DECLARE_REAL_AND_INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
)
30 DECLARE_REAL_AND_INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
)
31 DECLARE_REAL_AND_INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
)
34 void* (*malloc
)(uptr bytes
);
35 void (*free
)(void* mem
);
36 void* (*calloc
)(uptr n_elements
, uptr elem_size
);
37 void* (*realloc
)(void* oldMem
, uptr bytes
);
38 void* (*memalign
)(uptr alignment
, uptr bytes
);
41 const MallocDebug asan_malloc_dispatch
ALIGNED(32) = {
42 WRAP(malloc
), WRAP(free
), WRAP(calloc
), WRAP(realloc
), WRAP(memalign
)
45 extern "C" const MallocDebug
* __libc_malloc_dispatch
;
48 void ReplaceSystemMalloc() {
49 __libc_malloc_dispatch
= &asan_malloc_dispatch
;
56 void ReplaceSystemMalloc() {
61 // ---------------------- Replacement functions ---------------- {{{1
62 using namespace __asan
; // NOLINT
64 INTERCEPTOR(void, free
, void *ptr
) {
66 asan_free(ptr
, &stack
, FROM_MALLOC
);
69 INTERCEPTOR(void, cfree
, void *ptr
) {
71 asan_free(ptr
, &stack
, FROM_MALLOC
);
74 INTERCEPTOR(void*, malloc
, uptr size
) {
75 GET_STACK_TRACE_MALLOC
;
76 return asan_malloc(size
, &stack
);
79 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
81 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
82 const uptr kCallocPoolSize
= 1024;
83 static uptr calloc_memory_for_dlsym
[kCallocPoolSize
];
84 static uptr allocated
;
85 uptr size_in_words
= ((nmemb
* size
) + kWordSize
- 1) / kWordSize
;
86 void *mem
= (void*)&calloc_memory_for_dlsym
[allocated
];
87 allocated
+= size_in_words
;
88 CHECK(allocated
< kCallocPoolSize
);
91 GET_STACK_TRACE_MALLOC
;
92 return asan_calloc(nmemb
, size
, &stack
);
95 INTERCEPTOR(void*, realloc
, void *ptr
, uptr size
) {
96 GET_STACK_TRACE_MALLOC
;
97 return asan_realloc(ptr
, size
, &stack
);
100 INTERCEPTOR(void*, memalign
, uptr boundary
, uptr size
) {
101 GET_STACK_TRACE_MALLOC
;
102 return asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
105 INTERCEPTOR(void*, __libc_memalign
, uptr boundary
, uptr size
) {
106 GET_STACK_TRACE_MALLOC
;
107 void *res
= asan_memalign(boundary
, size
, &stack
, FROM_MALLOC
);
108 DTLS_on_libc_memalign(res
, size
* boundary
);
112 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
113 GET_CURRENT_PC_BP_SP
;
115 return asan_malloc_usable_size(ptr
, pc
, bp
);
118 // We avoid including malloc.h for portability reasons.
119 // man mallinfo says the fields are "long", but the implementation uses int.
120 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
122 struct fake_mallinfo
{
126 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
127 struct fake_mallinfo res
;
128 REAL(memset
)(&res
, 0, sizeof(res
));
132 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
136 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
137 GET_STACK_TRACE_MALLOC
;
138 // Printf("posix_memalign: %zx %zu\n", alignment, size);
139 return asan_posix_memalign(memptr
, alignment
, size
, &stack
);
142 INTERCEPTOR(void*, valloc
, uptr size
) {
143 GET_STACK_TRACE_MALLOC
;
144 return asan_valloc(size
, &stack
);
147 INTERCEPTOR(void*, pvalloc
, uptr size
) {
148 GET_STACK_TRACE_MALLOC
;
149 return asan_pvalloc(size
, &stack
);
152 INTERCEPTOR(void, malloc_stats
, void) {
153 __asan_print_accumulated_stats();
156 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX