Add more debug code for https://github.com/google/sanitizers/issues/1193 (getting...
authorKostya Serebryany <kcc@google.com>
Wed, 29 Jul 2020 17:34:07 +0000 (10:34 -0700)
committerKostya Serebryany <kcc@google.com>
Wed, 29 Jul 2020 18:13:11 +0000 (11:13 -0700)
mode debug code

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D84819

compiler-rt/lib/asan/asan_allocator.cpp
compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h
compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h
compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary64.h

index 126d26d0823b3a8481481c95a9d2757c42620963..c2f2804769d10b483b1b628634058dff61030191 100644 (file)
@@ -732,6 +732,25 @@ struct Allocator {
     return reinterpret_cast<AsanChunk *>(alloc_beg);
   }
 
+  AsanChunk *GetAsanChunkDebug(void *alloc_beg) {
+    if (!alloc_beg) return nullptr;
+    if (!allocator.FromPrimary(alloc_beg)) {
+      uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(alloc_beg));
+      AsanChunk *m = reinterpret_cast<AsanChunk *>(meta[1]);
+      Printf("GetAsanChunkDebug1 alloc_beg %p meta %p m %p\n", alloc_beg, meta, m);
+      return m;
+    }
+    uptr *alloc_magic = reinterpret_cast<uptr *>(alloc_beg);
+    Printf(
+        "GetAsanChunkDebug2 alloc_beg %p  alloc_magic %p alloc_magic[0] %p "
+        "alloc_magic[1] %p\n",
+        alloc_beg, alloc_magic, alloc_magic[0], alloc_magic[1]);
+    if (alloc_magic[0] == kAllocBegMagic)
+      return reinterpret_cast<AsanChunk *>(alloc_magic[1]);
+    return reinterpret_cast<AsanChunk *>(alloc_beg);
+  }
+
+
   AsanChunk *GetAsanChunkByAddr(uptr p) {
     void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));
     return GetAsanChunk(alloc_beg);
@@ -744,6 +763,13 @@ struct Allocator {
     return GetAsanChunk(alloc_beg);
   }
 
+  AsanChunk *GetAsanChunkByAddrFastLockedDebug(uptr p) {
+    void *alloc_beg =
+        allocator.GetBlockBeginFastLockedDebug(reinterpret_cast<void *>(p));
+    Printf("GetAsanChunkByAddrFastLockedDebug p %p alloc_beg %p\n", p, alloc_beg);
+    return GetAsanChunkDebug(alloc_beg);
+  }
+
   uptr AllocationSize(uptr p) {
     AsanChunk *m = GetAsanChunkByAddr(p);
     if (!m) return 0;
@@ -1040,16 +1066,25 @@ uptr PointsIntoChunk(void* p) {
 // Debug code. Delete once issue #1193 is chased down.
 extern "C" SANITIZER_WEAK_ATTRIBUTE const char *__lsan_current_stage;
 
+void GetUserBeginDebug(uptr chunk) {
+  Printf("GetUserBeginDebug1 chunk %p\n", chunk);
+  __asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLockedDebug(chunk);
+  Printf("GetUserBeginDebug2 m     %p\n", m);
+}
+
 uptr GetUserBegin(uptr chunk) {
   __asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(chunk);
-  if (!m)
+  if (!m) {
     Printf(
         "ASAN is about to crash with a CHECK failure.\n"
         "The ASAN developers are trying to chase down this bug,\n"
         "so if you've encountered this bug please let us know.\n"
         "See also: https://github.com/google/sanitizers/issues/1193\n"
+        "Internal ref b/149237057\n"
         "chunk: %p caller %p __lsan_current_stage %s\n",
         chunk, GET_CALLER_PC(), __lsan_current_stage);
+    GetUserBeginDebug(chunk);
+  }
   CHECK(m);
   return m->Beg();
 }
index 33f89d6d49928c1bfb4d8f1ed0286b51a745cac9..6d73784d77d09e3e8958a80724e4abf05c5f0823 100644 (file)
@@ -142,6 +142,13 @@ class CombinedAllocator {
     return secondary_.GetBlockBeginFastLocked(p);
   }
 
+  void *GetBlockBeginFastLockedDebug(void *p) {
+    if (primary_.PointerIsMine(p))
+      return primary_.GetBlockBeginDebug(p);
+    return secondary_.GetBlockBeginFastLocked(p);
+  }
+
+
   uptr GetActuallyAllocatedSize(void *p) {
     if (primary_.PointerIsMine(p))
       return primary_.GetActuallyAllocatedSize(p);
index 3b1838b3985ac1ebb6df2b6c5dc5bc5d09e4b201..47cc42cb411c224ee250540b9bb5afd0d326053b 100644 (file)
@@ -210,6 +210,7 @@ class SizeClassAllocator32 {
     uptr res = beg + (n * (u32)size);
     return reinterpret_cast<void*>(res);
   }
+  void *GetBlockBeginDebug(const void *p) { return GetBlockBegin(p); }
 
   uptr GetActuallyAllocatedSize(void *p) {
     CHECK(PointerIsMine(p));
index 1d9a29c70f308b4d589880d6d652b1066b39188d..acc61cc6ba8dd28a6e090b3cd89db3f4225fc183 100644 (file)
@@ -199,6 +199,29 @@ class SizeClassAllocator64 {
     return nullptr;
   }
 
+  void *GetBlockBeginDebug(const void *p) {
+    uptr class_id = GetSizeClass(p);
+    uptr size = ClassIdToSize(class_id);
+    Printf("GetBlockBeginDebug1 p %p class_id %p size %p\n", p, class_id, size);
+    if (!size) return nullptr;
+    uptr chunk_idx = GetChunkIdx((uptr)p, size);
+    uptr reg_beg = GetRegionBegin(p);
+    uptr beg = chunk_idx * size;
+    uptr next_beg = beg + size;
+    Printf(
+        "GetBlockBeginDebug2 chunk_idx %p reg_beg %p beg %p next_beg %p "
+        "kNumClasses %p\n",
+        chunk_idx, reg_beg, beg, next_beg, kNumClasses);
+    if (class_id >= kNumClasses) return nullptr;
+    const RegionInfo *region = AddressSpaceView::Load(GetRegionInfo(class_id));
+    Printf("GetBlockBeginDebug3 region %p region->mapped_user %p\n", region,
+           region->mapped_user);
+    if (region->mapped_user >= next_beg)
+      return reinterpret_cast<void*>(reg_beg + beg);
+    return nullptr;
+  }
+
+
   uptr GetActuallyAllocatedSize(void *p) {
     CHECK(PointerIsMine(p));
     return ClassIdToSize(GetSizeClass(p));