[RS6000] Out-of-line register save functions can't be used from crtend.o
[official-gcc.git] / libsanitizer / asan / asan_malloc_linux.cc
blobcc50a388495f58e5b851c5d9a3f0483432288654
1 //===-- asan_malloc_linux.cc ----------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
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);
41 return mem;
44 INTERCEPTOR(void, free, void *ptr) {
45 GET_STACK_TRACE_FREE;
46 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
47 return;
48 asan_free(ptr, &stack, FROM_MALLOC);
51 INTERCEPTOR(void, cfree, void *ptr) {
52 GET_STACK_TRACE_FREE;
53 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
54 return;
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;
80 if (UNLIKELY(!asan_inited)) {
81 new_ptr = AllocateFromLocalPool(size);
82 } else {
83 copy_size = size;
84 new_ptr = asan_malloc(copy_size, &stack);
86 internal_memcpy(new_ptr, ptr, copy_size);
87 return new_ptr;
89 return asan_realloc(ptr, size, &stack);
92 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
93 GET_STACK_TRACE_MALLOC;
94 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
97 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
98 GET_STACK_TRACE_MALLOC;
99 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
102 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
103 GET_STACK_TRACE_MALLOC;
104 void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
105 DTLS_on_libc_memalign(res, size);
106 return res;
109 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
110 GET_CURRENT_PC_BP_SP;
111 (void)sp;
112 return asan_malloc_usable_size(ptr, pc, bp);
115 // We avoid including malloc.h for portability reasons.
116 // man mallinfo says the fields are "long", but the implementation uses int.
117 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
118 // is not called.
119 struct fake_mallinfo {
120 int x[10];
123 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
124 struct fake_mallinfo res;
125 REAL(memset)(&res, 0, sizeof(res));
126 return res;
129 INTERCEPTOR(int, mallopt, int cmd, int value) {
130 return -1;
133 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
134 GET_STACK_TRACE_MALLOC;
135 // Printf("posix_memalign: %zx %zu\n", alignment, size);
136 return asan_posix_memalign(memptr, alignment, size, &stack);
139 INTERCEPTOR(void*, valloc, uptr size) {
140 GET_STACK_TRACE_MALLOC;
141 return asan_valloc(size, &stack);
144 INTERCEPTOR(void*, pvalloc, uptr size) {
145 GET_STACK_TRACE_MALLOC;
146 return asan_pvalloc(size, &stack);
149 INTERCEPTOR(void, malloc_stats, void) {
150 __asan_print_accumulated_stats();
153 #if SANITIZER_ANDROID
154 // Format of __libc_malloc_dispatch has changed in Android L.
155 // While we are moving towards a solution that does not depend on bionic
156 // internals, here is something to support both K* and L releases.
157 struct MallocDebugK {
158 void *(*malloc)(uptr bytes);
159 void (*free)(void *mem);
160 void *(*calloc)(uptr n_elements, uptr elem_size);
161 void *(*realloc)(void *oldMem, uptr bytes);
162 void *(*memalign)(uptr alignment, uptr bytes);
163 uptr (*malloc_usable_size)(void *mem);
166 struct MallocDebugL {
167 void *(*calloc)(uptr n_elements, uptr elem_size);
168 void (*free)(void *mem);
169 fake_mallinfo (*mallinfo)(void);
170 void *(*malloc)(uptr bytes);
171 uptr (*malloc_usable_size)(void *mem);
172 void *(*memalign)(uptr alignment, uptr bytes);
173 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
174 void* (*pvalloc)(uptr size);
175 void *(*realloc)(void *oldMem, uptr bytes);
176 void* (*valloc)(uptr size);
179 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
180 WRAP(malloc), WRAP(free), WRAP(calloc),
181 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
183 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
184 WRAP(calloc), WRAP(free), WRAP(mallinfo),
185 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
186 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
187 WRAP(valloc)};
189 namespace __asan {
190 void ReplaceSystemMalloc() {
191 void **__libc_malloc_dispatch_p =
192 (void **)AsanDlSymNext("__libc_malloc_dispatch");
193 if (__libc_malloc_dispatch_p) {
194 // Decide on K vs L dispatch format by the presence of
195 // __libc_malloc_default_dispatch export in libc.
196 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
197 if (default_dispatch_p)
198 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
199 else
200 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
203 } // namespace __asan
205 #else // SANITIZER_ANDROID
207 namespace __asan {
208 void ReplaceSystemMalloc() {
210 } // namespace __asan
211 #endif // SANITIZER_ANDROID
213 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX