[compiler-rt] bc0ae48 - [test][asan] Speedup the test

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 23:49:15 PST 2022


Author: Vitaly Buka
Date: 2022-11-27T23:44:29-08:00
New Revision: bc0ae48382a3a5426153928d5f222d6d31f57bac

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

LOG: [test][asan] Speedup the test

Added: 
    

Modified: 
    compiler-rt/test/asan/TestCases/contiguous_container.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/test/asan/TestCases/contiguous_container.cpp b/compiler-rt/test/asan/TestCases/contiguous_container.cpp
index c796d04579da..e778c00f8dfa 100644
--- a/compiler-rt/test/asan/TestCases/contiguous_container.cpp
+++ b/compiler-rt/test/asan/TestCases/contiguous_container.cpp
@@ -3,6 +3,7 @@
 // Test __sanitizer_annotate_contiguous_container.
 
 #include <algorithm>
+#include <numeric>
 #include <vector>
 
 #include <assert.h>
@@ -18,18 +19,26 @@ template <class T> static constexpr T RoundDown(T x) {
                              ~(kGranularity - 1));
 }
 
-static std::vector<bool> GetPoisonedMask(char *begin, char *end) {
-  std::vector<bool> result;
-  result.reserve(end - begin);
-  for (; begin != end; ++begin)
-    result.push_back(__asan_address_is_poisoned(begin));
+static std::vector<int> GetPoisonedState(char *begin, char *end) {
+  std::vector<int> result;
+  for (; begin != end;) {
+    int poisoned = 0;
+    for (; begin != end && __asan_address_is_poisoned(begin); ++begin)
+      ++poisoned;
+    result.push_back(poisoned);
+    int unpoisoned = 0;
+    for (; begin != end && !__asan_address_is_poisoned(begin); ++begin)
+      ++unpoisoned;
+    result.push_back(unpoisoned);
+  }
   return result;
 }
 
-static int GetFirstMismatch(const std::vector<bool> &a,
-                            const std::vector<bool> &b) {
-  return std::mismatch(a.begin(), a.end(), b.begin(), b.end()).first -
-         a.begin();
+static int GetFirstMismatch(const std::vector<int> &a,
+                            const std::vector<int> &b) {
+  auto mismatch = std::mismatch(a.begin(), a.end(), b.begin(), b.end());
+  return std::accumulate(a.begin(), mismatch.first, 0) +
+         std::min(*mismatch.first, *mismatch.second);
 }
 
 void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
@@ -66,12 +75,12 @@ void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
   }
 
   // Precalculate masks.
-  std::vector<std::vector<bool>> masks(capacity + 1);
+  std::vector<std::vector<int>> masks(capacity + 1);
   for (int i = 0; i <= capacity; i++) {
     char *old_end = end;
     end = st_beg + i;
     __sanitizer_annotate_contiguous_container(st_beg, st_end, old_end, end);
-    masks[i] = GetPoisonedMask(st_beg, st_end);
+    masks[i] = GetPoisonedState(st_beg, st_end);
   }
   for (int i = 0; i <= capacity; i++) {
     char *old_end = end;
@@ -150,74 +159,79 @@ void TestDoubleEndedContainer(size_t capacity, size_t off_begin,
       assert(__asan_address_is_poisoned(cur) == poison_buffer);
   }
 
-  // Precalculate masks.
-  std::vector<std::vector<std::vector<bool>>> masks(
-      capacity + 1, std::vector<std::vector<bool>>(capacity + 1));
-  for (int i = 0; i <= capacity; i++) {
-    for (int j = i; j <= capacity; j++) {
-      char *old_beg = beg;
-      char *old_end = end;
-      beg = st_beg + i;
-      end = st_beg + j;
-      __sanitizer_annotate_double_ended_contiguous_container(
-          st_beg, st_end, old_beg, old_end, beg, end);
-      masks[i][j] = GetPoisonedMask(st_beg, st_end);
+  if (capacity < 32) {
+
+    // Precalculate masks.
+    std::vector<std::vector<std::vector<int>>> masks(
+        capacity + 1, std::vector<std::vector<int>>(capacity + 1));
+    for (int i = 0; i <= capacity; i++) {
+      for (int j = i; j <= capacity; j++) {
+        char *old_beg = beg;
+        char *old_end = end;
+        beg = st_beg + i;
+        end = st_beg + j;
+        __sanitizer_annotate_double_ended_contiguous_container(
+            st_beg, st_end, old_beg, old_end, beg, end);
+        masks[i][j] = GetPoisonedState(st_beg, st_end);
+      }
     }
-  }
 
-  for (int i = 0; i <= capacity; i++) {
-    for (int j = i; j <= capacity; j++) {
-      char *old_beg = beg;
-      char *old_end = end;
-      beg = st_beg + i;
-      end = st_beg + j;
-      __sanitizer_annotate_double_ended_contiguous_container(
-          st_beg, st_end, old_beg, old_end, beg, end);
+    for (int i = 0; i <= capacity; i++) {
+      for (int j = i; j <= capacity; j++) {
+        char *old_beg = beg;
+        char *old_end = end;
+        beg = st_beg + i;
+        end = st_beg + j;
+        __sanitizer_annotate_double_ended_contiguous_container(
+            st_beg, st_end, old_beg, old_end, beg, end);
 
-      // Try to mismatch the end of the container.
-      char *cur_first = std::max(end - 2 * kGranularity, beg);
-      char *cur_last = std::min(end + 2 * kGranularity, st_end);
-      for (char *cur = cur_first; cur <= cur_last; ++cur) {
-        bool is_valid = __sanitizer_verify_double_ended_contiguous_container(
-            st_beg, beg, cur, st_end);
-        const void *bad_address =
-            __sanitizer_double_ended_contiguous_container_find_bad_address(
-                st_beg, beg, cur, st_end);
+        // Try to mismatch the end of the container.
+        char *cur_first = std::max(end - 2 * kGranularity, beg);
+        char *cur_last = std::min(end + 2 * kGranularity, st_end);
+        for (char *cur = cur_first; cur <= cur_last; ++cur) {
+          bool is_valid = __sanitizer_verify_double_ended_contiguous_container(
+              st_beg, beg, cur, st_end);
+          const void *bad_address =
+              __sanitizer_double_ended_contiguous_container_find_bad_address(
+                  st_beg, beg, cur, st_end);
 
-        if (cur == end) {
-          assert(is_valid);
-          assert(!bad_address);
-          continue;
-        }
+          if (cur == end) {
+            assert(is_valid);
+            assert(!bad_address);
+            continue;
+          }
 
-        assert(!is_valid);
-        assert(bad_address);
-        assert(bad_address ==
-               st_beg + GetFirstMismatch(masks[i][j], masks[i][cur - st_beg]));
-      }
+          assert(!is_valid);
+          assert(bad_address);
+          assert(bad_address ==
+                 st_beg +
+                     GetFirstMismatch(masks[i][j], masks[i][cur - st_beg]));
+        }
 
-      // Try to mismatch the begin of the container.
-      cur_first = std::max(beg - 2 * kGranularity, st_beg);
-      cur_last = std::min(beg + 2 * kGranularity, end);
-      for (char *cur = cur_first; cur <= cur_last; ++cur) {
-        bool is_valid = __sanitizer_verify_double_ended_contiguous_container(
-            st_beg, cur, end, st_end);
-        const void *bad_address =
-            __sanitizer_double_ended_contiguous_container_find_bad_address(
-                st_beg, cur, end, st_end);
+        // Try to mismatch the begin of the container.
+        cur_first = std::max(beg - 2 * kGranularity, st_beg);
+        cur_last = std::min(beg + 2 * kGranularity, end);
+        for (char *cur = cur_first; cur <= cur_last; ++cur) {
+          bool is_valid = __sanitizer_verify_double_ended_contiguous_container(
+              st_beg, cur, end, st_end);
+          const void *bad_address =
+              __sanitizer_double_ended_contiguous_container_find_bad_address(
+                  st_beg, cur, end, st_end);
 
-        if (cur == beg ||
-            // The first unaligned granule of non-empty container looks the
-            // same.
-            (std::max(beg, cur) < end && RoundDown(beg) == RoundDown(cur))) {
-          assert(is_valid);
-          assert(!bad_address);
-          continue;
+          if (cur == beg ||
+              // The first unaligned granule of non-empty container looks the
+              // same.
+              (std::max(beg, cur) < end && RoundDown(beg) == RoundDown(cur))) {
+            assert(is_valid);
+            assert(!bad_address);
+            continue;
+          }
+          assert(!is_valid);
+          assert(bad_address);
+          assert(bad_address ==
+                 st_beg +
+                     GetFirstMismatch(masks[i][j], masks[cur - st_beg][j]));
         }
-        assert(!is_valid);
-        assert(bad_address);
-        assert(bad_address ==
-               st_beg + GetFirstMismatch(masks[i][j], masks[cur - st_beg][j]));
       }
     }
   }


        


More information about the llvm-commits mailing list