PR tree-optimization/83698 - bogus offset in -Wrestrict messages for strcat of unknow...
[official-gcc.git] / libsanitizer / asan / asan_malloc_linux.cc
blob19f45fc3cee330556544638e96d953967ae37e3b
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_FUCHSIA || SANITIZER_LINUX || \
17 SANITIZER_NETBSD
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);
42 return mem;
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);
53 void *new_ptr;
54 if (UNLIKELY(MaybeInDlsym())) {
55 new_ptr = AllocateFromLocalPool(size);
56 } else {
57 ENSURE_ASAN_INITED();
58 GET_STACK_TRACE_MALLOC;
59 new_ptr = asan_malloc(size, &stack);
61 internal_memcpy(new_ptr, ptr, copy_size);
62 return new_ptr;
65 INTERCEPTOR(void, free, void *ptr) {
66 GET_STACK_TRACE_FREE;
67 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
68 return;
69 asan_free(ptr, &stack, FROM_MALLOC);
72 #if SANITIZER_INTERCEPT_CFREE
73 INTERCEPTOR(void, cfree, void *ptr) {
74 GET_STACK_TRACE_FREE;
75 if (UNLIKELY(IsInDlsymAllocPool(ptr)))
76 return;
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);
85 ENSURE_ASAN_INITED();
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);
94 ENSURE_ASAN_INITED();
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);
119 return res;
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;
130 (void)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
138 // is not called.
139 struct fake_mallinfo {
140 int x[10];
143 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
144 struct fake_mallinfo res;
145 REAL(memset)(&res, 0, sizeof(res));
146 return res;
149 INTERCEPTOR(int, mallopt, int cmd, int value) {
150 return -1;
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),
210 WRAP(valloc)};
212 namespace __asan {
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;
222 else
223 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
226 } // namespace __asan
228 #else // SANITIZER_ANDROID
230 namespace __asan {
231 void ReplaceSystemMalloc() {
233 } // namespace __asan
234 #endif // SANITIZER_ANDROID
236 #endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||
237 // SANITIZER_NETBSD