From 4e74480e0234f27f40e26dec085b2a9eb2149578 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Mon, 28 Dec 2020 17:16:49 -0800 Subject: [PATCH] [NFC][sanitizer] Simplify InternalLowerBound --- compiler-rt/lib/lsan/lsan_common.cpp | 3 +- compiler-rt/lib/lsan/lsan_common_fuchsia.cpp | 4 +-- .../lib/sanitizer_common/sanitizer_common.h | 11 ++++-- .../lib/sanitizer_common/sanitizer_stackdepot.cpp | 3 +- .../tests/sanitizer_common_test.cpp | 40 +++++++++------------- 5 files changed, 28 insertions(+), 33 deletions(-) diff --git a/compiler-rt/lib/lsan/lsan_common.cpp b/compiler-rt/lib/lsan/lsan_common.cpp index 30c154a..ddedfa0 100644 --- a/compiler-rt/lib/lsan/lsan_common.cpp +++ b/compiler-rt/lib/lsan/lsan_common.cpp @@ -551,8 +551,7 @@ static void ReportIfNotSuspended(ThreadContextBase *tctx, void *arg) { const InternalMmapVector &suspended_threads = *(const InternalMmapVector *)arg; if (tctx->status == ThreadStatusRunning) { - uptr i = InternalLowerBound(suspended_threads, 0, suspended_threads.size(), - tctx->os_id, CompareLess()); + uptr i = InternalLowerBound(suspended_threads, tctx->os_id); if (i >= suspended_threads.size() || suspended_threads[i] != tctx->os_id) Report("Running thread %d was not suspended. False leaks are possible.\n", tctx->os_id); diff --git a/compiler-rt/lib/lsan/lsan_common_fuchsia.cpp b/compiler-rt/lib/lsan/lsan_common_fuchsia.cpp index 3c62c94..2d35fa5 100644 --- a/compiler-rt/lib/lsan/lsan_common_fuchsia.cpp +++ b/compiler-rt/lib/lsan/lsan_common_fuchsia.cpp @@ -107,9 +107,7 @@ void LockStuffAndStopTheWorld(StopTheWorldCallback callback, auto params = static_cast(data); uptr begin = reinterpret_cast(chunk); uptr end = begin + size; - auto i = __sanitizer::InternalLowerBound(params->allocator_caches, 0, - params->allocator_caches.size(), - begin, CompareLess()); + auto i = __sanitizer::InternalLowerBound(params->allocator_caches, begin); if (i < params->allocator_caches.size() && params->allocator_caches[i] >= begin && end - params->allocator_caches[i] <= sizeof(AllocatorCache)) { diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index bce24d6..60d1c62 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -467,6 +467,7 @@ inline int ToLower(int c) { template class InternalMmapVectorNoCtor { public: + using value_type = T; void Initialize(uptr initial_capacity) { capacity_bytes_ = 0; size_ = 0; @@ -651,9 +652,13 @@ void Sort(T *v, uptr size, Compare comp = {}) { // Works like std::lower_bound: finds the first element that is not less // than the val. -template -uptr InternalLowerBound(const Container &v, uptr first, uptr last, - const Value &val, Compare comp) { +template > +uptr InternalLowerBound(const Container &v, + const typename Container::value_type &val, + Compare comp = {}) { + uptr first = 0; + uptr last = v.size(); while (last > first) { uptr mid = (first + last) / 2; if (comp(v[mid], val)) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp index 4692f50..44a9521 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp @@ -145,8 +145,7 @@ StackTrace StackDepotReverseMap::Get(u32 id) { if (!map_.size()) return StackTrace(); IdDescPair pair = {id, nullptr}; - uptr idx = - InternalLowerBound(map_, 0, map_.size(), pair, IdDescPair::IdComparator); + uptr idx = InternalLowerBound(map_, pair, IdDescPair::IdComparator); if (idx > map_.size() || map_[idx].id != id) return StackTrace(); return map_[idx].desc->load(); diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp index 259bd99..1d0806c 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp @@ -226,27 +226,21 @@ bool UptrLess(uptr a, uptr b) { } TEST(SanitizerCommon, InternalLowerBound) { - static const uptr kSize = 5; - int arr[kSize]; - arr[0] = 1; - arr[1] = 3; - arr[2] = 5; - arr[3] = 7; - arr[4] = 11; - - EXPECT_EQ(0u, InternalLowerBound(arr, 0, kSize, 0, UptrLess)); - EXPECT_EQ(0u, InternalLowerBound(arr, 0, kSize, 1, UptrLess)); - EXPECT_EQ(1u, InternalLowerBound(arr, 0, kSize, 2, UptrLess)); - EXPECT_EQ(1u, InternalLowerBound(arr, 0, kSize, 3, UptrLess)); - EXPECT_EQ(2u, InternalLowerBound(arr, 0, kSize, 4, UptrLess)); - EXPECT_EQ(2u, InternalLowerBound(arr, 0, kSize, 5, UptrLess)); - EXPECT_EQ(3u, InternalLowerBound(arr, 0, kSize, 6, UptrLess)); - EXPECT_EQ(3u, InternalLowerBound(arr, 0, kSize, 7, UptrLess)); - EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 8, UptrLess)); - EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 9, UptrLess)); - EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 10, UptrLess)); - EXPECT_EQ(4u, InternalLowerBound(arr, 0, kSize, 11, UptrLess)); - EXPECT_EQ(5u, InternalLowerBound(arr, 0, kSize, 12, UptrLess)); + std::vector arr = {1, 3, 5, 7, 11}; + + EXPECT_EQ(0u, InternalLowerBound(arr, 0)); + EXPECT_EQ(0u, InternalLowerBound(arr, 1)); + EXPECT_EQ(1u, InternalLowerBound(arr, 2)); + EXPECT_EQ(1u, InternalLowerBound(arr, 3)); + EXPECT_EQ(2u, InternalLowerBound(arr, 4)); + EXPECT_EQ(2u, InternalLowerBound(arr, 5)); + EXPECT_EQ(3u, InternalLowerBound(arr, 6)); + EXPECT_EQ(3u, InternalLowerBound(arr, 7)); + EXPECT_EQ(4u, InternalLowerBound(arr, 8)); + EXPECT_EQ(4u, InternalLowerBound(arr, 9)); + EXPECT_EQ(4u, InternalLowerBound(arr, 10)); + EXPECT_EQ(4u, InternalLowerBound(arr, 11)); + EXPECT_EQ(5u, InternalLowerBound(arr, 12)); } TEST(SanitizerCommon, InternalLowerBoundVsStdLowerBound) { @@ -268,8 +262,8 @@ TEST(SanitizerCommon, InternalLowerBoundVsStdLowerBound) { for (auto to_find : {val - 1, val, val + 1}) { uptr expected = std::lower_bound(data.begin(), data.end(), to_find) - data.begin(); - EXPECT_EQ(expected, InternalLowerBound(data.data(), 0, data.size(), - to_find, std::less())); + EXPECT_EQ(expected, + InternalLowerBound(data, to_find, std::less())); } } } -- 2.7.4