lsan_interceptors.cc revision 6eb836f9bd2b96a321ddc7fb5ea08aa65131e61f
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_limits_posix.h" 23#include "lsan.h" 24#include "lsan_allocator.h" 25#include "lsan_thread.h" 26 27using namespace __lsan; 28 29extern "C" { 30int pthread_attr_init(void *attr); 31int pthread_attr_destroy(void *attr); 32int pthread_attr_getdetachstate(void *attr, int *v); 33int pthread_key_create(unsigned *key, void (*destructor)(void* v)); 34int pthread_setspecific(unsigned key, const void *v); 35} 36 37#define GET_STACK_TRACE \ 38 StackTrace stack; \ 39 { \ 40 uptr stack_top = 0, stack_bottom = 0; \ 41 ThreadContext *t; \ 42 bool fast = common_flags()->fast_unwind_on_malloc; \ 43 if (fast && (t = CurrentThreadContext())) { \ 44 stack_top = t->stack_end(); \ 45 stack_bottom = t->stack_begin(); \ 46 } \ 47 GetStackTrace(&stack, __sanitizer::common_flags()->malloc_context_size, \ 48 StackTrace::GetCurrentPc(), \ 49 GET_CURRENT_FRAME(), stack_top, stack_bottom, fast); \ 50 } 51 52///// Malloc/free interceptors. ///// 53 54const bool kAlwaysClearMemory = true; 55 56namespace std { 57 struct nothrow_t; 58} 59 60INTERCEPTOR(void*, malloc, uptr size) { 61 Init(); 62 GET_STACK_TRACE; 63 return Allocate(stack, size, 1, kAlwaysClearMemory); 64} 65 66INTERCEPTOR(void, free, void *p) { 67 Init(); 68 Deallocate(p); 69} 70 71INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) { 72 if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0; 73 Init(); 74 GET_STACK_TRACE; 75 size *= nmemb; 76 return Allocate(stack, size, 1, true); 77} 78 79INTERCEPTOR(void*, realloc, void *q, uptr size) { 80 Init(); 81 GET_STACK_TRACE; 82 return Reallocate(stack, q, size, 1); 83} 84 85INTERCEPTOR(void*, memalign, uptr alignment, uptr size) { 86 Init(); 87 GET_STACK_TRACE; 88 return Allocate(stack, size, alignment, kAlwaysClearMemory); 89} 90 91INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) { 92 Init(); 93 GET_STACK_TRACE; 94 *memptr = Allocate(stack, size, alignment, kAlwaysClearMemory); 95 // FIXME: Return ENOMEM if user requested more than max alloc size. 96 return 0; 97} 98 99INTERCEPTOR(void*, valloc, uptr size) { 100 Init(); 101 GET_STACK_TRACE; 102 if (size == 0) 103 size = GetPageSizeCached(); 104 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory); 105} 106 107INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { 108 Init(); 109 return GetMallocUsableSize(ptr); 110} 111 112struct fake_mallinfo { 113 int x[10]; 114}; 115 116INTERCEPTOR(struct fake_mallinfo, mallinfo, void) { 117 struct fake_mallinfo res; 118 internal_memset(&res, 0, sizeof(res)); 119 return res; 120} 121 122INTERCEPTOR(int, mallopt, int cmd, int value) { 123 return -1; 124} 125 126INTERCEPTOR(void*, pvalloc, uptr size) { 127 Init(); 128 GET_STACK_TRACE; 129 uptr PageSize = GetPageSizeCached(); 130 size = RoundUpTo(size, PageSize); 131 if (size == 0) { 132 // pvalloc(0) should allocate one page. 133 size = PageSize; 134 } 135 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory); 136} 137 138INTERCEPTOR(void, cfree, void *p) ALIAS("free"); 139 140#define OPERATOR_NEW_BODY \ 141 Init(); \ 142 GET_STACK_TRACE; \ 143 return Allocate(stack, size, 1, kAlwaysClearMemory); 144 145INTERCEPTOR_ATTRIBUTE 146void *operator new(uptr size) { OPERATOR_NEW_BODY; } 147INTERCEPTOR_ATTRIBUTE 148void *operator new[](uptr size) { OPERATOR_NEW_BODY; } 149INTERCEPTOR_ATTRIBUTE 150void *operator new(uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; } 151INTERCEPTOR_ATTRIBUTE 152void *operator new[](uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; } 153 154#define OPERATOR_DELETE_BODY \ 155 Init(); \ 156 Deallocate(ptr); 157 158INTERCEPTOR_ATTRIBUTE 159void operator delete(void *ptr) { OPERATOR_DELETE_BODY; } 160INTERCEPTOR_ATTRIBUTE 161void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; } 162INTERCEPTOR_ATTRIBUTE 163void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; } 164INTERCEPTOR_ATTRIBUTE 165void operator delete[](void *ptr, std::nothrow_t const &) { 166 OPERATOR_DELETE_BODY; 167} 168 169// We need this to intercept the __libc_memalign calls that are used to 170// allocate dynamic TLS space in ld-linux.so. 171INTERCEPTOR(void *, __libc_memalign, uptr align, uptr s) ALIAS("memalign"); 172 173///// Thread initialization and finalization. ///// 174 175static unsigned g_thread_finalize_key; 176 177static void thread_finalize(void *v) { 178 uptr iter = (uptr)v; 179 if (iter > 1) { 180 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) { 181 Report("LeakSanitizer: failed to set thread key.\n"); 182 Die(); 183 } 184 return; 185 } 186 ThreadFinish(); 187} 188 189struct ThreadParam { 190 void *(*callback)(void *arg); 191 void *param; 192 atomic_uintptr_t tid; 193}; 194 195extern "C" void *__lsan_thread_start_func(void *arg) { 196 ThreadParam *p = (ThreadParam*)arg; 197 void* (*callback)(void *arg) = p->callback; 198 void *param = p->param; 199 // Wait until the last iteration to maximize the chance that we are the last 200 // destructor to run. 201 if (pthread_setspecific(g_thread_finalize_key, 202 (void*)kPthreadDestructorIterations)) { 203 Report("LeakSanitizer: failed to set thread key.\n"); 204 Die(); 205 } 206 int tid = 0; 207 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0) 208 internal_sched_yield(); 209 atomic_store(&p->tid, 0, memory_order_release); 210 SetCurrentThread(tid); 211 ThreadStart(tid, GetTid()); 212 return callback(param); 213} 214 215INTERCEPTOR(int, pthread_create, void *th, void *attr, 216 void *(*callback)(void *), void *param) { 217 Init(); 218 EnsureMainThreadIDIsCorrect(); 219 __sanitizer_pthread_attr_t myattr; 220 if (attr == 0) { 221 pthread_attr_init(&myattr); 222 attr = &myattr; 223 } 224 AdjustStackSizeLinux(attr, 0); 225 int detached = 0; 226 pthread_attr_getdetachstate(attr, &detached); 227 ThreadParam p; 228 p.callback = callback; 229 p.param = param; 230 atomic_store(&p.tid, 0, memory_order_relaxed); 231 int res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p); 232 if (res == 0) { 233 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached); 234 CHECK_NE(tid, 0); 235 atomic_store(&p.tid, tid, memory_order_release); 236 while (atomic_load(&p.tid, memory_order_acquire) != 0) 237 internal_sched_yield(); 238 } 239 if (attr == &myattr) 240 pthread_attr_destroy(&myattr); 241 return res; 242} 243 244INTERCEPTOR(int, pthread_join, void *th, void **ret) { 245 Init(); 246 int tid = ThreadTid((uptr)th); 247 int res = REAL(pthread_join)(th, ret); 248 if (res == 0) 249 ThreadJoin(tid); 250 return res; 251} 252 253namespace __lsan { 254 255void InitializeInterceptors() { 256 INTERCEPT_FUNCTION(malloc); 257 INTERCEPT_FUNCTION(free); 258 INTERCEPT_FUNCTION(cfree); 259 INTERCEPT_FUNCTION(calloc); 260 INTERCEPT_FUNCTION(realloc); 261 INTERCEPT_FUNCTION(memalign); 262 INTERCEPT_FUNCTION(posix_memalign); 263 INTERCEPT_FUNCTION(__libc_memalign); 264 INTERCEPT_FUNCTION(valloc); 265 INTERCEPT_FUNCTION(pvalloc); 266 INTERCEPT_FUNCTION(malloc_usable_size); 267 INTERCEPT_FUNCTION(mallinfo); 268 INTERCEPT_FUNCTION(mallopt); 269 INTERCEPT_FUNCTION(pthread_create); 270 INTERCEPT_FUNCTION(pthread_join); 271 272 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) { 273 Report("LeakSanitizer: failed to create thread key.\n"); 274 Die(); 275 } 276} 277 278} // namespace __lsan 279