[compiler-rt] [scudo] Relax MemtagTag.SelectRandomTag. (PR #68048)

Evgenii Stepanov via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 2 15:48:47 PDT 2023


https://github.com/eugenis created https://github.com/llvm/llvm-project/pull/68048

As it turns out, PRNGs have varying quality.
Relax the test to accept less-then-perfect tag distribution.

>From 1173faec544dd6589d6750ab34da5fd27669eb81 Mon Sep 17 00:00:00 2001
From: Evgenii Stepanov <eugenis at google.com>
Date: Mon, 2 Oct 2023 15:42:58 -0700
Subject: [PATCH] [scudo] Relax MemtagTag.SelectRandomTag.

As it turns out, PRNGs have varying quality.
Relax the test to accept less-then-perfect tag distribution.
---
 compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
index 2488e04856bd972..fd277f962a9aaac 100644
--- a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
@@ -120,7 +120,15 @@ TEST_F(MemtagTest, SelectRandomTag) {
     uptr Tags = 0;
     for (uptr I = 0; I < 100000; ++I)
       Tags = Tags | (1u << extractTag(selectRandomTag(Ptr, 0)));
-    EXPECT_EQ(0xfffeull, Tags);
+    // std::popcnt is C++20
+    int PopCnt = 0;
+    while (Tags) {
+      PopCnt += Tags & 1;
+      Tags >>= 1;
+    }
+    // Random tags are not always very random, and this test is not about PRNG
+    // quality.  Anything above half would be satisfactory.
+    EXPECT_GE(PopCnt, 8);
   }
 }
 



More information about the llvm-commits mailing list