]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/lsan/lsan_interceptors.cc
MFV r315875:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / lsan / lsan_interceptors.cc
1 //=-- lsan_interceptors.cc ------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of LeakSanitizer.
11 // Interceptors for standalone LSan.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "interception/interception.h"
16 #include "sanitizer_common/sanitizer_allocator.h"
17 #include "sanitizer_common/sanitizer_atomic.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 #include "sanitizer_common/sanitizer_linux.h"
22 #include "sanitizer_common/sanitizer_platform_interceptors.h"
23 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
24 #include "sanitizer_common/sanitizer_tls_get_addr.h"
25 #include "lsan.h"
26 #include "lsan_allocator.h"
27 #include "lsan_common.h"
28 #include "lsan_thread.h"
29
30 using namespace __lsan;
31
32 extern "C" {
33 int pthread_attr_init(void *attr);
34 int pthread_attr_destroy(void *attr);
35 int pthread_attr_getdetachstate(void *attr, int *v);
36 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
37 int pthread_setspecific(unsigned key, const void *v);
38 }
39
40 #define ENSURE_LSAN_INITED do {   \
41   CHECK(!lsan_init_is_running);   \
42   if (!lsan_inited)               \
43     __lsan_init();                \
44 } while (0)
45
46 ///// Malloc/free interceptors. /////
47
48 const bool kAlwaysClearMemory = true;
49
50 namespace std {
51   struct nothrow_t;
52 }
53
54 INTERCEPTOR(void*, malloc, uptr size) {
55   ENSURE_LSAN_INITED;
56   GET_STACK_TRACE_MALLOC;
57   return Allocate(stack, size, 1, kAlwaysClearMemory);
58 }
59
60 INTERCEPTOR(void, free, void *p) {
61   ENSURE_LSAN_INITED;
62   Deallocate(p);
63 }
64
65 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
66   if (lsan_init_is_running) {
67     // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
68     const uptr kCallocPoolSize = 1024;
69     static uptr calloc_memory_for_dlsym[kCallocPoolSize];
70     static uptr allocated;
71     uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
72     void *mem = (void*)&calloc_memory_for_dlsym[allocated];
73     allocated += size_in_words;
74     CHECK(allocated < kCallocPoolSize);
75     return mem;
76   }
77   if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return nullptr;
78   ENSURE_LSAN_INITED;
79   GET_STACK_TRACE_MALLOC;
80   size *= nmemb;
81   return Allocate(stack, size, 1, true);
82 }
83
84 INTERCEPTOR(void*, realloc, void *q, uptr size) {
85   ENSURE_LSAN_INITED;
86   GET_STACK_TRACE_MALLOC;
87   return Reallocate(stack, q, size, 1);
88 }
89
90 #if SANITIZER_INTERCEPT_MEMALIGN
91 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
92   ENSURE_LSAN_INITED;
93   GET_STACK_TRACE_MALLOC;
94   return Allocate(stack, size, alignment, kAlwaysClearMemory);
95 }
96 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
97
98 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
99   ENSURE_LSAN_INITED;
100   GET_STACK_TRACE_MALLOC;
101   void *res = Allocate(stack, size, alignment, kAlwaysClearMemory);
102   DTLS_on_libc_memalign(res, size);
103   return res;
104 }
105 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
106 #else
107 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
108 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
109 #endif // SANITIZER_INTERCEPT_MEMALIGN
110
111 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
112   ENSURE_LSAN_INITED;
113   GET_STACK_TRACE_MALLOC;
114   return Allocate(stack, size, alignment, kAlwaysClearMemory);
115 }
116
117 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
118   ENSURE_LSAN_INITED;
119   GET_STACK_TRACE_MALLOC;
120   *memptr = Allocate(stack, size, alignment, kAlwaysClearMemory);
121   // FIXME: Return ENOMEM if user requested more than max alloc size.
122   return 0;
123 }
124
125 INTERCEPTOR(void*, valloc, uptr size) {
126   ENSURE_LSAN_INITED;
127   GET_STACK_TRACE_MALLOC;
128   if (size == 0)
129     size = GetPageSizeCached();
130   return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
131 }
132
133 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
134   ENSURE_LSAN_INITED;
135   return GetMallocUsableSize(ptr);
136 }
137
138 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
139 struct fake_mallinfo {
140   int x[10];
141 };
142
143 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
144   struct fake_mallinfo res;
145   internal_memset(&res, 0, sizeof(res));
146   return res;
147 }
148 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
149
150 INTERCEPTOR(int, mallopt, int cmd, int value) {
151   return -1;
152 }
153 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
154 #else
155 #define LSAN_MAYBE_INTERCEPT_MALLINFO
156 #define LSAN_MAYBE_INTERCEPT_MALLOPT
157 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
158
159 #if SANITIZER_INTERCEPT_PVALLOC
160 INTERCEPTOR(void*, pvalloc, uptr size) {
161   ENSURE_LSAN_INITED;
162   GET_STACK_TRACE_MALLOC;
163   uptr PageSize = GetPageSizeCached();
164   size = RoundUpTo(size, PageSize);
165   if (size == 0) {
166     // pvalloc(0) should allocate one page.
167     size = PageSize;
168   }
169   return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
170 }
171 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
172 #else
173 #define LSAN_MAYBE_INTERCEPT_PVALLOC
174 #endif // SANITIZER_INTERCEPT_PVALLOC
175
176 #if SANITIZER_INTERCEPT_CFREE
177 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
178 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
179 #else
180 #define LSAN_MAYBE_INTERCEPT_CFREE
181 #endif // SANITIZER_INTERCEPT_CFREE
182
183 #define OPERATOR_NEW_BODY                              \
184   ENSURE_LSAN_INITED;                                  \
185   GET_STACK_TRACE_MALLOC;                              \
186   return Allocate(stack, size, 1, kAlwaysClearMemory);
187
188 INTERCEPTOR_ATTRIBUTE
189 void *operator new(uptr size) { OPERATOR_NEW_BODY; }
190 INTERCEPTOR_ATTRIBUTE
191 void *operator new[](uptr size) { OPERATOR_NEW_BODY; }
192 INTERCEPTOR_ATTRIBUTE
193 void *operator new(uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
194 INTERCEPTOR_ATTRIBUTE
195 void *operator new[](uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
196
197 #define OPERATOR_DELETE_BODY \
198   ENSURE_LSAN_INITED;        \
199   Deallocate(ptr);
200
201 INTERCEPTOR_ATTRIBUTE
202 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
203 INTERCEPTOR_ATTRIBUTE
204 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
205 INTERCEPTOR_ATTRIBUTE
206 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
207 INTERCEPTOR_ATTRIBUTE
208 void operator delete[](void *ptr, std::nothrow_t const &) {
209   OPERATOR_DELETE_BODY;
210 }
211
212 ///// Thread initialization and finalization. /////
213
214 static unsigned g_thread_finalize_key;
215
216 static void thread_finalize(void *v) {
217   uptr iter = (uptr)v;
218   if (iter > 1) {
219     if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
220       Report("LeakSanitizer: failed to set thread key.\n");
221       Die();
222     }
223     return;
224   }
225   ThreadFinish();
226 }
227
228 struct ThreadParam {
229   void *(*callback)(void *arg);
230   void *param;
231   atomic_uintptr_t tid;
232 };
233
234 extern "C" void *__lsan_thread_start_func(void *arg) {
235   ThreadParam *p = (ThreadParam*)arg;
236   void* (*callback)(void *arg) = p->callback;
237   void *param = p->param;
238   // Wait until the last iteration to maximize the chance that we are the last
239   // destructor to run.
240   if (pthread_setspecific(g_thread_finalize_key,
241                           (void*)GetPthreadDestructorIterations())) {
242     Report("LeakSanitizer: failed to set thread key.\n");
243     Die();
244   }
245   int tid = 0;
246   while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
247     internal_sched_yield();
248   SetCurrentThread(tid);
249   ThreadStart(tid, GetTid());
250   atomic_store(&p->tid, 0, memory_order_release);
251   return callback(param);
252 }
253
254 INTERCEPTOR(int, pthread_create, void *th, void *attr,
255             void *(*callback)(void *), void *param) {
256   ENSURE_LSAN_INITED;
257   EnsureMainThreadIDIsCorrect();
258   __sanitizer_pthread_attr_t myattr;
259   if (!attr) {
260     pthread_attr_init(&myattr);
261     attr = &myattr;
262   }
263   AdjustStackSize(attr);
264   int detached = 0;
265   pthread_attr_getdetachstate(attr, &detached);
266   ThreadParam p;
267   p.callback = callback;
268   p.param = param;
269   atomic_store(&p.tid, 0, memory_order_relaxed);
270   int res;
271   {
272     // Ignore all allocations made by pthread_create: thread stack/TLS may be
273     // stored by pthread for future reuse even after thread destruction, and
274     // the linked list it's stored in doesn't even hold valid pointers to the
275     // objects, the latter are calculated by obscure pointer arithmetic.
276     ScopedInterceptorDisabler disabler;
277     res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
278   }
279   if (res == 0) {
280     int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached);
281     CHECK_NE(tid, 0);
282     atomic_store(&p.tid, tid, memory_order_release);
283     while (atomic_load(&p.tid, memory_order_acquire) != 0)
284       internal_sched_yield();
285   }
286   if (attr == &myattr)
287     pthread_attr_destroy(&myattr);
288   return res;
289 }
290
291 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
292   ENSURE_LSAN_INITED;
293   int tid = ThreadTid((uptr)th);
294   int res = REAL(pthread_join)(th, ret);
295   if (res == 0)
296     ThreadJoin(tid);
297   return res;
298 }
299
300 namespace __lsan {
301
302 void InitializeInterceptors() {
303   INTERCEPT_FUNCTION(malloc);
304   INTERCEPT_FUNCTION(free);
305   LSAN_MAYBE_INTERCEPT_CFREE;
306   INTERCEPT_FUNCTION(calloc);
307   INTERCEPT_FUNCTION(realloc);
308   LSAN_MAYBE_INTERCEPT_MEMALIGN;
309   LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
310   INTERCEPT_FUNCTION(aligned_alloc);
311   INTERCEPT_FUNCTION(posix_memalign);
312   INTERCEPT_FUNCTION(valloc);
313   LSAN_MAYBE_INTERCEPT_PVALLOC;
314   INTERCEPT_FUNCTION(malloc_usable_size);
315   LSAN_MAYBE_INTERCEPT_MALLINFO;
316   LSAN_MAYBE_INTERCEPT_MALLOPT;
317   INTERCEPT_FUNCTION(pthread_create);
318   INTERCEPT_FUNCTION(pthread_join);
319
320   if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
321     Report("LeakSanitizer: failed to create thread key.\n");
322     Die();
323   }
324 }
325
326 } // namespace __lsan