xref: /netbsd-src/external/gpl3/gcc.old/dist/libsanitizer/asan/asan_malloc_linux.cc (revision 33881f779a77dce6440bdc44610d94de75bebefe)
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;
85     if (UNLIKELY(!asan_inited)) {
86       new_ptr = AllocateFromLocalPool(size);
87     } else {
88       copy_size = size;
89       new_ptr = asan_malloc(copy_size, &stack);
90     }
91     internal_memcpy(new_ptr, ptr, copy_size);
92     return new_ptr;
93   }
94   return asan_realloc(ptr, size, &stack);
95 }
96 
97 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
98   GET_STACK_TRACE_MALLOC;
99   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
100 }
101 
102 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
103   GET_STACK_TRACE_MALLOC;
104   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
105 }
106 
107 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
108   GET_STACK_TRACE_MALLOC;
109   void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
110   DTLS_on_libc_memalign(res, size);
111   return res;
112 }
113 
114 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
115   GET_CURRENT_PC_BP_SP;
116   (void)sp;
117   return asan_malloc_usable_size(ptr, pc, bp);
118 }
119 
120 // We avoid including malloc.h for portability reasons.
121 // man mallinfo says the fields are "long", but the implementation uses int.
122 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
123 // is not called.
124 struct fake_mallinfo {
125   int x[10];
126 };
127 
128 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
129   struct fake_mallinfo res;
130   REAL(memset)(&res, 0, sizeof(res));
131   return res;
132 }
133 
134 INTERCEPTOR(int, mallopt, int cmd, int value) {
135   return -1;
136 }
137 
138 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
139   GET_STACK_TRACE_MALLOC;
140   // Printf("posix_memalign: %zx %zu\n", alignment, size);
141   return asan_posix_memalign(memptr, alignment, size, &stack);
142 }
143 
144 INTERCEPTOR(void*, valloc, uptr size) {
145   GET_STACK_TRACE_MALLOC;
146   return asan_valloc(size, &stack);
147 }
148 
149 INTERCEPTOR(void*, pvalloc, uptr size) {
150   GET_STACK_TRACE_MALLOC;
151   return asan_pvalloc(size, &stack);
152 }
153 
154 INTERCEPTOR(void, malloc_stats, void) {
155   __asan_print_accumulated_stats();
156 }
157 
158 #if SANITIZER_ANDROID
159 // Format of __libc_malloc_dispatch has changed in Android L.
160 // While we are moving towards a solution that does not depend on bionic
161 // internals, here is something to support both K* and L releases.
162 struct MallocDebugK {
163   void *(*malloc)(uptr bytes);
164   void (*free)(void *mem);
165   void *(*calloc)(uptr n_elements, uptr elem_size);
166   void *(*realloc)(void *oldMem, uptr bytes);
167   void *(*memalign)(uptr alignment, uptr bytes);
168   uptr (*malloc_usable_size)(void *mem);
169 };
170 
171 struct MallocDebugL {
172   void *(*calloc)(uptr n_elements, uptr elem_size);
173   void (*free)(void *mem);
174   fake_mallinfo (*mallinfo)(void);
175   void *(*malloc)(uptr bytes);
176   uptr (*malloc_usable_size)(void *mem);
177   void *(*memalign)(uptr alignment, uptr bytes);
178   int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
179   void* (*pvalloc)(uptr size);
180   void *(*realloc)(void *oldMem, uptr bytes);
181   void* (*valloc)(uptr size);
182 };
183 
184 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
185     WRAP(malloc),  WRAP(free),     WRAP(calloc),
186     WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
187 
188 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
189     WRAP(calloc),         WRAP(free),               WRAP(mallinfo),
190     WRAP(malloc),         WRAP(malloc_usable_size), WRAP(memalign),
191     WRAP(posix_memalign), WRAP(pvalloc),            WRAP(realloc),
192     WRAP(valloc)};
193 
194 namespace __asan {
195 void ReplaceSystemMalloc() {
196   void **__libc_malloc_dispatch_p =
197       (void **)AsanDlSymNext("__libc_malloc_dispatch");
198   if (__libc_malloc_dispatch_p) {
199     // Decide on K vs L dispatch format by the presence of
200     // __libc_malloc_default_dispatch export in libc.
201     void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
202     if (default_dispatch_p)
203       *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
204     else
205       *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
206   }
207 }
208 }  // namespace __asan
209 
210 #else  // SANITIZER_ANDROID
211 
212 namespace __asan {
213 void ReplaceSystemMalloc() {
214 }
215 }  // namespace __asan
216 #endif  // SANITIZER_ANDROID
217 
218 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
219