[llvm-branch-commits] [compiler-rt] 4e74480 - [NFC][sanitizer] Simplify InternalLowerBound

Vitaly Buka via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 29 14:06:35 PST 2020


Author: Vitaly Buka
Date: 2020-12-29T14:01:43-08:00
New Revision: 4e74480e0234f27f40e26dec085b2a9eb2149578

URL: https://github.com/llvm/llvm-project/commit/4e74480e0234f27f40e26dec085b2a9eb2149578
DIFF: https://github.com/llvm/llvm-project/commit/4e74480e0234f27f40e26dec085b2a9eb2149578.diff

LOG: [NFC][sanitizer] Simplify InternalLowerBound

Added: 
    

Modified: 
    compiler-rt/lib/lsan/lsan_common.cpp
    compiler-rt/lib/lsan/lsan_common_fuchsia.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp
    compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/lsan/lsan_common.cpp b/compiler-rt/lib/lsan/lsan_common.cpp
index 30c154a4fc94..ddedfa0830e1 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<tid_t> &suspended_threads =
       *(const InternalMmapVector<tid_t> *)arg;
   if (tctx->status == ThreadStatusRunning) {
-    uptr i = InternalLowerBound(suspended_threads, 0, suspended_threads.size(),
-                                tctx->os_id, CompareLess<int>());
+    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 3c62c9433d3d..2d35fa5b1cff 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<const Params *>(data);
     uptr begin = reinterpret_cast<uptr>(chunk);
     uptr end = begin + size;
-    auto i = __sanitizer::InternalLowerBound(params->allocator_caches, 0,
-                                             params->allocator_caches.size(),
-                                             begin, CompareLess<uptr>());
+    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 bce24d68045b..60d1c62b0681 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<typename T>
 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 <class Container, class Value, class Compare>
-uptr InternalLowerBound(const Container &v, uptr first, uptr last,
-                        const Value &val, Compare comp) {
+template <class Container,
+          class Compare = CompareLess<typename Container::value_type>>
+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 4692f50d3237..44a95214e38b 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 259bd99324a2..1d0806c4c404 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<int> 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<int>()));
+        EXPECT_EQ(expected,
+                  InternalLowerBound(data, to_find, std::less<int>()));
       }
     }
   }


        


More information about the llvm-branch-commits mailing list