[sanitizer] Fix StackDepotPrintAll
authorVitaly Buka <vitalybuka@google.com>
Tue, 12 Oct 2021 04:29:06 +0000 (21:29 -0700)
committerVitaly Buka <vitalybuka@google.com>
Tue, 12 Oct 2021 17:57:40 +0000 (10:57 -0700)
unlock corrupted backets by using s set by loop to nullptr.
Also StackDepot supports iterating without locking.

Reviewed By: dvyukov

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

compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h
compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp

index b7f47b4..309be54 100644 (file)
@@ -180,14 +180,12 @@ template <class Node, int kReservedBits, int kTabSizeLog>
 void StackDepotBase<Node, kReservedBits, kTabSizeLog>::PrintAll() {
   for (int i = 0; i < kTabSize; ++i) {
     atomic_uintptr_t *p = &tab[i];
-    lock(p);
-    uptr v = atomic_load(p, memory_order_relaxed);
+    uptr v = atomic_load(p, memory_order_consume);
     Node *s = (Node *)(v & ~1UL);
     for (; s; s = s->link) {
       Printf("Stack for id %u:\n", s->id);
       s->load().Print();
     }
-    unlock(p, s);
   }
 }
 
index 998bda6..bc461d6 100644 (file)
@@ -86,6 +86,22 @@ TEST(SanitizerCommon, Maybe_StackDepotPrint) {
       "Stack for id .*#0 0x1.*#1 0x2.*#2 0x3.*#3 0x4.*#4 0x8.*#5 0x9.*");
 }
 
+TEST(SanitizerCommon, StackDepotPrintNoLock) {
+  u32 n = 2000;
+  std::vector<u32> idx2id(n);
+  for (u32 i = 0; i < n; ++i) {
+    uptr array[] = {0x111, 0x222, i, 0x444, 0x777};
+    StackTrace s(array, ARRAY_SIZE(array));
+    idx2id[i] = StackDepotPut(s);
+  }
+  StackDepotPrintAll();
+  for (u32 i = 1; i < n; ++i) {
+    uptr array[] = {0x111, 0x222, i, 0x444, 0x777};
+    StackTrace s(array, ARRAY_SIZE(array));
+    CHECK_EQ(idx2id[i], StackDepotPut(s));
+  }
+}
+
 TEST(SanitizerCommon, StackDepotReverseMap) {
   uptr array1[] = {1, 2, 3, 4, 5};
   uptr array2[] = {7, 1, 3, 0};