[compiler-rt] 67a1c45 - [NFC][sanitizer] Add StackStoreTest
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 23 13:02:15 PST 2021
Author: Vitaly Buka
Date: 2021-11-23T13:02:11-08:00
New Revision: 67a1c45def8a75061203461ab0060c75c864df1c
URL: https://github.com/llvm/llvm-project/commit/67a1c45def8a75061203461ab0060c75c864df1c
DIFF: https://github.com/llvm/llvm-project/commit/67a1c45def8a75061203461ab0060c75c864df1c.diff
LOG: [NFC][sanitizer] Add StackStoreTest
Reviewed By: morehouse
Differential Revision: https://reviews.llvm.org/D114463
Added:
compiler-rt/lib/sanitizer_common/tests/sanitizer_stack_store_test.cpp
Modified:
compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
Removed:
################################################################################
diff --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
index 570da2670b533..2cff2b0288059 100644
--- a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
@@ -34,6 +34,7 @@ set(SANITIZER_UNITTESTS
sanitizer_procmaps_test.cpp
sanitizer_ring_buffer_test.cpp
sanitizer_quarantine_test.cpp
+ sanitizer_stack_store_test.cpp
sanitizer_stackdepot_test.cpp
sanitizer_stacktrace_printer_test.cpp
sanitizer_stacktrace_test.cpp
diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_stack_store_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_stack_store_test.cpp
new file mode 100644
index 0000000000000..3a2db148f2d58
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_stack_store_test.cpp
@@ -0,0 +1,68 @@
+//===-- sanitizer_stack_store_test.cpp --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_stack_store.h"
+
+#include <algorithm>
+#include <numeric>
+#include <vector>
+
+#include "gtest/gtest.h"
+#include "sanitizer_hash.h"
+#include "sanitizer_stacktrace.h"
+
+namespace __sanitizer {
+
+class StackStoreTest : public testing::Test {
+ protected:
+ void SetUp() override {}
+ void TearDown() override { store_.TestOnlyUnmap(); }
+
+ template <typename Fn>
+ void ForEachTrace(Fn fn, uptr n = 1000000) {
+ std::vector<uptr> frames(kStackTraceMax);
+ std::iota(frames.begin(), frames.end(), 1);
+ MurMur2HashBuilder h(0);
+ for (uptr i = 0; i < n; ++i) {
+ h.add(i);
+ u32 size = h.get() % kStackTraceMax;
+ h.add(i);
+ uptr tag = h.get() % 256;
+ StackTrace s(frames.data(), size, tag);
+ fn(s);
+ std::next_permutation(frames.begin(), frames.end());
+ };
+ }
+
+ StackStore store_ = {};
+};
+
+TEST_F(StackStoreTest, Basic) {
+ std::vector<StackStore::Id> ids;
+ ForEachTrace([&](const StackTrace& s) { ids.push_back(store_.Store(s)); });
+
+ auto id = ids.begin();
+ ForEachTrace([&](const StackTrace& s) {
+ StackTrace trace = store_.Load(*(id++));
+ EXPECT_EQ(s.size, trace.size);
+ EXPECT_EQ(s.tag, trace.tag);
+ EXPECT_EQ(std::vector<uptr>(s.trace, s.trace + s.size),
+ std::vector<uptr>(trace.trace, trace.trace + trace.size));
+ });
+}
+
+TEST_F(StackStoreTest, Allocated) {
+ EXPECT_LE(store_.Allocated(), 0x100000u);
+ std::vector<StackStore::Id> ids;
+ ForEachTrace([&](const StackTrace& s) { ids.push_back(store_.Store(s)); });
+ EXPECT_NEAR(store_.Allocated(), FIRST_32_SECOND_64(500000000u, 1000000000u),
+ FIRST_32_SECOND_64(50000000u, 100000000u));
+ store_.TestOnlyUnmap();
+ EXPECT_LE(store_.Allocated(), 0x100000u);
+}
+
+} // namespace __sanitizer
More information about the llvm-commits
mailing list