From b4db2a500dca7a427736bc7074023b7c303d5c9d Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Mon, 11 Oct 2021 21:29:06 -0700 Subject: [PATCH] [sanitizer] Fix StackDepotPrintAll 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 --- .../lib/sanitizer_common/sanitizer_stackdepotbase.h | 4 +--- .../sanitizer_common/tests/sanitizer_stackdepot_test.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h index b7f47b4..309be54 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h @@ -180,14 +180,12 @@ template void StackDepotBase::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); } } diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp index 998bda6..bc461d6 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp @@ -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 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}; -- 2.7.4