[compiler-rt] [scudo] Added test fixture for cache tests. (PR #102230)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 6 15:08:49 PDT 2024


================
@@ -265,3 +265,99 @@ TEST_F(MapAllocatorWithReleaseTest, SecondaryThreadsRace) {
   Allocator->getStats(&Str);
   Str.output();
 }
+
+struct MapAllocatorCacheTest : public Test {
+  static constexpr scudo::u32 MarkerBytes = 0xDEADBEEF;
+
+  static void testUnmapCallback(scudo::MemMapT &MemMap) {
+    scudo::u32 *Ptr = reinterpret_cast<scudo::u32 *>(MemMap.getBase());
+    *Ptr = MarkerBytes;
+  }
+
+  using Config = scudo::DefaultConfig;
+  using SecondaryConfig = scudo::SecondaryConfig<Config>;
+  using CacheConfig = SecondaryConfig::CacheConfig;
+  using CacheT = scudo::MapAllocatorCache<CacheConfig, testUnmapCallback>;
+
+  std::unique_ptr<CacheT> Cache = std::make_unique<CacheT>();
+
+  const scudo::uptr PageSize = scudo::getPageSizeCached();
+  static constexpr scudo::uptr DefaultAllocSize = scudo::uptr(1U) << 16;
+
+  scudo::Options Options = getOptionsForConfig<SecondaryConfig>();
+
+  void SetUp() override { Cache->init(/*ReleaseToOsInterval=*/-1); }
+
+  void TearDown() override { Cache->unmapTestOnly(); }
+
+  scudo::MemMapT allocate(scudo::uptr Size) {
+    scudo::uptr MapSize = scudo::roundUp(Size, PageSize);
+    scudo::ReservedMemoryT ReservedMemory;
+    CHECK(ReservedMemory.create(0U, MapSize, nullptr, MAP_ALLOWNOMEM));
+
+    scudo::MemMapT MemMap = ReservedMemory.dispatch(
+        ReservedMemory.getBase(), ReservedMemory.getCapacity());
+    MemMap.remap(MemMap.getBase(), MemMap.getCapacity(), "scudo:test",
+                 MAP_RESIZABLE | MAP_ALLOWNOMEM);
+    return MemMap;
+  }
+
+  void fillCacheWithSameSizeBlocks(std::vector<scudo::MemMapT> &MemMaps,
+                                   scudo::uptr NumEntries, scudo::uptr Size) {
+    for (scudo::uptr I = 0; I < NumEntries; I++)
+      MemMaps.emplace_back(allocate(Size));
+    for (scudo::uptr I = 0; I < NumEntries; I++) {
+      scudo::MemMapT &MemMap = MemMaps[I];
+      Cache->store(Options, MemMap.getBase(), MemMap.getCapacity(),
+                   MemMap.getBase(), MemMap);
+    }
+  }
+};
+
+TEST_F(MapAllocatorCacheTest, CacheOrder) {
+  std::vector<scudo::MemMapT> MemMaps;
----------------
ChiaHungDuan wrote:

It was the class variable and my opinion is that this is used/needed for some tests but not all. About the clean up, I was wondering the same question then I think in most scudo tests, we usually ensure everything is returned/cleaned up before exit, for example, in primary test, we have to make sure all the blocks are released at the end so the verification in TearDown won't complain memory leak. It's also kind of a reminder for writing the scudo test that we need to know what we have created and what we have to do afterwards. Therefore, I suggested Jush to make the container local.

https://github.com/llvm/llvm-project/pull/102230


More information about the llvm-commits mailing list