From 67a1c45def8a75061203461ab0060c75c864df1c Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Mon, 15 Nov 2021 20:58:51 -0800 Subject: [PATCH] [NFC][sanitizer] Add StackStoreTest Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D114463 --- .../lib/sanitizer_common/tests/CMakeLists.txt | 1 + .../tests/sanitizer_stack_store_test.cpp | 68 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 compiler-rt/lib/sanitizer_common/tests/sanitizer_stack_store_test.cpp diff --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt index 570da26..2cff2b0 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 0000000..3a2db14 --- /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 +#include +#include + +#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 + void ForEachTrace(Fn fn, uptr n = 1000000) { + std::vector 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 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(s.trace, s.trace + s.size), + std::vector(trace.trace, trace.trace + trace.size)); + }); +} + +TEST_F(StackStoreTest, Allocated) { + EXPECT_LE(store_.Allocated(), 0x100000u); + std::vector 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 -- 2.7.4