xref: /netbsd-src/external/gpl3/gcc.old/dist/libsanitizer/asan/asan_malloc_linux.cc (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
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 //===----------------------------------------------------------------------===//
14 
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
17 
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"
23 
24 // ---------------------- Replacement functions ---------------- {{{1
25 using namespace __asan;  // NOLINT
26 
27 static uptr allocated_for_dlsym;
28 static const uptr kDlsymAllocPoolSize = 1024;
29 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
30 
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);
34 }
35 
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;
42 }
43 
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);
49 }
50 
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);
56 }
57 
58 INTERCEPTOR(void*, malloc, uptr size) {
59 #if SANITIZER_LINUX
60   // This is a workaround for glibc, by which asan_malloc() fails into infinite
61   // recursion of AsanInitInternal(): http://reviews.llvm.org/rL254395
62   // It is irrelevant to us, rather causes abort due to shortage of buffer.
63   if (UNLIKELY(!asan_inited))
64     // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
65     return AllocateFromLocalPool(size);
66 #endif
67   GET_STACK_TRACE_MALLOC;
68   return asan_malloc(size, &stack);
69 }
70 
71 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
72   if (UNLIKELY(!asan_inited))
73     // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
74     return AllocateFromLocalPool(nmemb * size);
75   GET_STACK_TRACE_MALLOC;
76   return asan_calloc(nmemb, size, &stack);
77 }
78 
79 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
80   GET_STACK_TRACE_MALLOC;
81   if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
82     uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
83     uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
84     void *new_ptr = asan_malloc(size, &stack);
85     internal_memcpy(new_ptr, ptr, copy_size);
86     return new_ptr;
87   }
88   return asan_realloc(ptr, size, &stack);
89 }
90 
91 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
92   GET_STACK_TRACE_MALLOC;
93   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
94 }
95 
96 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
97   GET_STACK_TRACE_MALLOC;
98   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
99 }
100 
101 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
102   GET_STACK_TRACE_MALLOC;
103   void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
104   DTLS_on_libc_memalign(res, size * boundary);
105   return res;
106 }
107 
108 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
109   GET_CURRENT_PC_BP_SP;
110   (void)sp;
111   return asan_malloc_usable_size(ptr, pc, bp);
112 }
113 
114 // We avoid including malloc.h for portability reasons.
115 // man mallinfo says the fields are "long", but the implementation uses int.
116 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
117 // is not called.
118 struct fake_mallinfo {
119   int x[10];
120 };
121 
122 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
123   struct fake_mallinfo res;
124   REAL(memset)(&res, 0, sizeof(res));
125   return res;
126 }
127 
128 INTERCEPTOR(int, mallopt, int cmd, int value) {
129   return -1;
130 }
131 
132 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
133   GET_STACK_TRACE_MALLOC;
134   // Printf("posix_memalign: %zx %zu\n", alignment, size);
135   return asan_posix_memalign(memptr, alignment, size, &stack);
136 }
137 
138 INTERCEPTOR(void*, valloc, uptr size) {
139   GET_STACK_TRACE_MALLOC;
140   return asan_valloc(size, &stack);
141 }
142 
143 INTERCEPTOR(void*, pvalloc, uptr size) {
144   GET_STACK_TRACE_MALLOC;
145   return asan_pvalloc(size, &stack);
146 }
147 
148 INTERCEPTOR(void, malloc_stats, void) {
149   __asan_print_accumulated_stats();
150 }
151 
152 #if SANITIZER_ANDROID
153 // Format of __libc_malloc_dispatch has changed in Android L.
154 // While we are moving towards a solution that does not depend on bionic
155 // internals, here is something to support both K* and L releases.
156 struct MallocDebugK {
157   void *(*malloc)(uptr bytes);
158   void (*free)(void *mem);
159   void *(*calloc)(uptr n_elements, uptr elem_size);
160   void *(*realloc)(void *oldMem, uptr bytes);
161   void *(*memalign)(uptr alignment, uptr bytes);
162   uptr (*malloc_usable_size)(void *mem);
163 };
164 
165 struct MallocDebugL {
166   void *(*calloc)(uptr n_elements, uptr elem_size);
167   void (*free)(void *mem);
168   fake_mallinfo (*mallinfo)(void);
169   void *(*malloc)(uptr bytes);
170   uptr (*malloc_usable_size)(void *mem);
171   void *(*memalign)(uptr alignment, uptr bytes);
172   int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
173   void* (*pvalloc)(uptr size);
174   void *(*realloc)(void *oldMem, uptr bytes);
175   void* (*valloc)(uptr size);
176 };
177 
178 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
179     WRAP(malloc),  WRAP(free),     WRAP(calloc),
180     WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
181 
182 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
183     WRAP(calloc),         WRAP(free),               WRAP(mallinfo),
184     WRAP(malloc),         WRAP(malloc_usable_size), WRAP(memalign),
185     WRAP(posix_memalign), WRAP(pvalloc),            WRAP(realloc),
186     WRAP(valloc)};
187 
188 namespace __asan {
189 void ReplaceSystemMalloc() {
190   void **__libc_malloc_dispatch_p =
191       (void **)AsanDlSymNext("__libc_malloc_dispatch");
192   if (__libc_malloc_dispatch_p) {
193     // Decide on K vs L dispatch format by the presence of
194     // __libc_malloc_default_dispatch export in libc.
195     void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
196     if (default_dispatch_p)
197       *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
198     else
199       *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
200   }
201 }
202 }  // namespace __asan
203 
204 #else  // SANITIZER_ANDROID
205 
206 namespace __asan {
207 void ReplaceSystemMalloc() {
208 }
209 }  // namespace __asan
210 #endif  // SANITIZER_ANDROID
211 
212 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
213