[PATCH] D24736: [lsan] Prevent initialization failure with newer (2.23+) glibc in use.

Maxim Ostapenko via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 19 10:55:28 PDT 2016


m.ostapenko created this revision.
m.ostapenko added reviewers: kcc, eugenis.
m.ostapenko added subscribers: llvm-commits, ygribov.
m.ostapenko set the repository for this revision to rL LLVM.
m.ostapenko added a project: Sanitizers.

This patch is pretty the same as http://reviews.llvm.org/D20235 that we used for ASan. Just don't wait for standalone LSan to fail on newer Glibc.

Repository:
  rL LLVM

https://reviews.llvm.org/D24736

Files:
  lib/lsan/lsan_interceptors.cc

Index: lib/lsan/lsan_interceptors.cc
===================================================================
--- lib/lsan/lsan_interceptors.cc
+++ lib/lsan/lsan_interceptors.cc
@@ -50,29 +50,43 @@
   struct nothrow_t;
 }
 
+static uptr allocated_for_dlsym;
+static const uptr kDlsymAllocPoolSize = 1024;
+static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
+
+static bool IsInDlsymAllocPool(const void *ptr) {
+  uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
+  return off < sizeof(alloc_memory_for_dlsym);
+}
+
+static void *AllocateFromLocalPool(uptr size_in_bytes) {
+  uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
+  void *mem = (void*)&alloc_memory_for_dlsym[allocated_for_dlsym];
+  allocated_for_dlsym += size_in_words;
+  CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
+  return mem;
+}
+
 INTERCEPTOR(void*, malloc, uptr size) {
+  if (UNLIKELY(!lsan_inited))
+    // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
+    return AllocateFromLocalPool(size);
   ENSURE_LSAN_INITED;
   GET_STACK_TRACE_MALLOC;
   return Allocate(stack, size, 1, kAlwaysClearMemory);
 }
 
 INTERCEPTOR(void, free, void *p) {
   ENSURE_LSAN_INITED;
+  if (UNLIKELY(IsInDlsymAllocPool(p)))
+    return;
   Deallocate(p);
 }
 
 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
-  if (lsan_init_is_running) {
+  if (UNLIKELY(lsan_init_is_running))
     // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
-    const uptr kCallocPoolSize = 1024;
-    static uptr calloc_memory_for_dlsym[kCallocPoolSize];
-    static uptr allocated;
-    uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
-    void *mem = (void*)&calloc_memory_for_dlsym[allocated];
-    allocated += size_in_words;
-    CHECK(allocated < kCallocPoolSize);
-    return mem;
-  }
+    return AllocateFromLocalPool(nmemb * size);
   if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return nullptr;
   ENSURE_LSAN_INITED;
   GET_STACK_TRACE_MALLOC;
@@ -83,6 +97,13 @@
 INTERCEPTOR(void*, realloc, void *q, uptr size) {
   ENSURE_LSAN_INITED;
   GET_STACK_TRACE_MALLOC;
+  if (UNLIKELY(IsInDlsymAllocPool(q))) {
+    uptr offset = (uptr)q - (uptr)alloc_memory_for_dlsym;
+    uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
+    void *new_q = Allocate(stack, size, 1, true);
+    internal_memcpy(new_q, q, copy_size);
+    return new_q;
+  }
   return Reallocate(stack, q, size, 1);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24736.71849.patch
Type: text/x-patch
Size: 2449 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160919/a25e44a7/attachment.bin>


More information about the llvm-commits mailing list