264 lines · cpp
1//===-- quarantine_test.cpp -------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "quarantine.h"10 11#include <pthread.h>12#include <stdlib.h>13 14#include "tests/scudo_unit_test.h"15 16namespace {17 18void *FakePtr = reinterpret_cast<void *>(0xFA83FA83);19const scudo::uptr BlockSize = 8UL;20const scudo::uptr LargeBlockSize = 16384UL;21 22struct QuarantineCallback {23 void recycle(void *P) { EXPECT_EQ(P, FakePtr); }24 void *allocate(scudo::uptr Size) { return malloc(Size); }25 void deallocate(void *P) { free(P); }26};27 28typedef scudo::GlobalQuarantine<QuarantineCallback, void> QuarantineT;29typedef typename QuarantineT::CacheT CacheT;30 31QuarantineCallback Cb;32 33void deallocateCache(CacheT *Cache) {34 while (scudo::QuarantineBatch *Batch = Cache->dequeueBatch())35 Cb.deallocate(Batch);36}37 38TEST(ScudoQuarantineTest, QuarantineBatchMerge) {39 // Verify the trivial case.40 scudo::QuarantineBatch Into;41 Into.init(FakePtr, 4UL);42 scudo::QuarantineBatch From;43 From.init(FakePtr, 8UL);44 45 Into.merge(&From);46 47 EXPECT_EQ(Into.Count, 2UL);48 EXPECT_EQ(Into.Batch[0], FakePtr);49 EXPECT_EQ(Into.Batch[1], FakePtr);50 EXPECT_EQ(Into.Size, 12UL + sizeof(scudo::QuarantineBatch));51 EXPECT_EQ(Into.getQuarantinedSize(), 12UL);52 53 EXPECT_EQ(From.Count, 0UL);54 EXPECT_EQ(From.Size, sizeof(scudo::QuarantineBatch));55 EXPECT_EQ(From.getQuarantinedSize(), 0UL);56 57 // Merge the batch to the limit.58 for (scudo::uptr I = 2; I < scudo::QuarantineBatch::MaxCount; ++I)59 From.push_back(FakePtr, 8UL);60 EXPECT_TRUE(Into.Count + From.Count == scudo::QuarantineBatch::MaxCount);61 EXPECT_TRUE(Into.canMerge(&From));62 63 Into.merge(&From);64 EXPECT_TRUE(Into.Count == scudo::QuarantineBatch::MaxCount);65 66 // No more space, not even for one element.67 From.init(FakePtr, 8UL);68 69 EXPECT_FALSE(Into.canMerge(&From));70}71 72TEST(ScudoQuarantineTest, QuarantineCacheMergeBatchesEmpty) {73 CacheT Cache;74 CacheT ToDeallocate;75 Cache.init();76 ToDeallocate.init();77 Cache.mergeBatches(&ToDeallocate);78 79 EXPECT_EQ(ToDeallocate.getSize(), 0UL);80 EXPECT_EQ(ToDeallocate.dequeueBatch(), nullptr);81}82 83TEST(SanitizerCommon, QuarantineCacheMergeBatchesOneBatch) {84 CacheT Cache;85 Cache.init();86 Cache.enqueue(Cb, FakePtr, BlockSize);87 EXPECT_EQ(BlockSize + sizeof(scudo::QuarantineBatch), Cache.getSize());88 89 CacheT ToDeallocate;90 ToDeallocate.init();91 Cache.mergeBatches(&ToDeallocate);92 93 // Nothing to merge, nothing to deallocate.94 EXPECT_EQ(BlockSize + sizeof(scudo::QuarantineBatch), Cache.getSize());95 96 EXPECT_EQ(ToDeallocate.getSize(), 0UL);97 EXPECT_EQ(ToDeallocate.dequeueBatch(), nullptr);98 99 deallocateCache(&Cache);100}101 102TEST(ScudoQuarantineTest, QuarantineCacheMergeBatchesSmallBatches) {103 // Make a Cache with two batches small enough to merge.104 CacheT From;105 From.init();106 From.enqueue(Cb, FakePtr, BlockSize);107 CacheT Cache;108 Cache.init();109 Cache.enqueue(Cb, FakePtr, BlockSize);110 111 Cache.transfer(&From);112 EXPECT_EQ(BlockSize * 2 + sizeof(scudo::QuarantineBatch) * 2,113 Cache.getSize());114 115 CacheT ToDeallocate;116 ToDeallocate.init();117 Cache.mergeBatches(&ToDeallocate);118 119 // Batches merged, one batch to deallocate.120 EXPECT_EQ(BlockSize * 2 + sizeof(scudo::QuarantineBatch), Cache.getSize());121 EXPECT_EQ(ToDeallocate.getSize(), sizeof(scudo::QuarantineBatch));122 123 deallocateCache(&Cache);124 deallocateCache(&ToDeallocate);125}126 127TEST(ScudoQuarantineTest, QuarantineCacheMergeBatchesTooBigToMerge) {128 const scudo::uptr NumBlocks = scudo::QuarantineBatch::MaxCount - 1;129 130 // Make a Cache with two batches small enough to merge.131 CacheT From;132 CacheT Cache;133 From.init();134 Cache.init();135 for (scudo::uptr I = 0; I < NumBlocks; ++I) {136 From.enqueue(Cb, FakePtr, BlockSize);137 Cache.enqueue(Cb, FakePtr, BlockSize);138 }139 Cache.transfer(&From);140 EXPECT_EQ(BlockSize * NumBlocks * 2 + sizeof(scudo::QuarantineBatch) * 2,141 Cache.getSize());142 143 CacheT ToDeallocate;144 ToDeallocate.init();145 Cache.mergeBatches(&ToDeallocate);146 147 // Batches cannot be merged.148 EXPECT_EQ(BlockSize * NumBlocks * 2 + sizeof(scudo::QuarantineBatch) * 2,149 Cache.getSize());150 EXPECT_EQ(ToDeallocate.getSize(), 0UL);151 152 deallocateCache(&Cache);153}154 155TEST(ScudoQuarantineTest, QuarantineCacheMergeBatchesALotOfBatches) {156 const scudo::uptr NumBatchesAfterMerge = 3;157 const scudo::uptr NumBlocks =158 scudo::QuarantineBatch::MaxCount * NumBatchesAfterMerge;159 const scudo::uptr NumBatchesBeforeMerge = NumBlocks;160 161 // Make a Cache with many small batches.162 CacheT Cache;163 Cache.init();164 for (scudo::uptr I = 0; I < NumBlocks; ++I) {165 CacheT From;166 From.init();167 From.enqueue(Cb, FakePtr, BlockSize);168 Cache.transfer(&From);169 }170 171 EXPECT_EQ(BlockSize * NumBlocks +172 sizeof(scudo::QuarantineBatch) * NumBatchesBeforeMerge,173 Cache.getSize());174 175 CacheT ToDeallocate;176 ToDeallocate.init();177 Cache.mergeBatches(&ToDeallocate);178 179 // All blocks should fit Into 3 batches.180 EXPECT_EQ(BlockSize * NumBlocks +181 sizeof(scudo::QuarantineBatch) * NumBatchesAfterMerge,182 Cache.getSize());183 184 EXPECT_EQ(ToDeallocate.getSize(),185 sizeof(scudo::QuarantineBatch) *186 (NumBatchesBeforeMerge - NumBatchesAfterMerge));187 188 deallocateCache(&Cache);189 deallocateCache(&ToDeallocate);190}191 192const scudo::uptr MaxQuarantineSize = 1024UL << 10; // 1MB193const scudo::uptr MaxCacheSize = 256UL << 10; // 256KB194 195TEST(ScudoQuarantineTest, GlobalQuarantine) {196 QuarantineT Quarantine;197 CacheT Cache;198 Cache.init();199 Quarantine.init(MaxQuarantineSize, MaxCacheSize);200 EXPECT_EQ(Quarantine.getMaxSize(), MaxQuarantineSize);201 EXPECT_EQ(Quarantine.getCacheSize(), MaxCacheSize);202 203 bool DrainOccurred = false;204 scudo::uptr CacheSize = Cache.getSize();205 EXPECT_EQ(Cache.getSize(), 0UL);206 // We quarantine enough blocks that a drain has to occur. Verify this by207 // looking for a decrease of the size of the cache.208 for (scudo::uptr I = 0; I < 128UL; I++) {209 Quarantine.put(&Cache, Cb, FakePtr, LargeBlockSize);210 if (!DrainOccurred && Cache.getSize() < CacheSize)211 DrainOccurred = true;212 CacheSize = Cache.getSize();213 }214 EXPECT_TRUE(DrainOccurred);215 216 Quarantine.drainAndRecycle(&Cache, Cb);217 EXPECT_EQ(Cache.getSize(), 0UL);218 219 if (TEST_HAS_FAILURE) {220 scudo::ScopedString Str;221 Quarantine.getStats(&Str);222 Str.output();223 }224}225 226struct PopulateQuarantineThread {227 pthread_t Thread;228 QuarantineT *Quarantine;229 CacheT Cache;230};231 232void *populateQuarantine(void *Param) {233 PopulateQuarantineThread *P = static_cast<PopulateQuarantineThread *>(Param);234 P->Cache.init();235 for (scudo::uptr I = 0; I < 128UL; I++)236 P->Quarantine->put(&P->Cache, Cb, FakePtr, LargeBlockSize);237 return 0;238}239 240TEST(ScudoQuarantineTest, ThreadedGlobalQuarantine) {241 QuarantineT Quarantine;242 Quarantine.init(MaxQuarantineSize, MaxCacheSize);243 244 const scudo::uptr NumberOfThreads = 32U;245 PopulateQuarantineThread T[NumberOfThreads];246 for (scudo::uptr I = 0; I < NumberOfThreads; I++) {247 T[I].Quarantine = &Quarantine;248 pthread_create(&T[I].Thread, 0, populateQuarantine, &T[I]);249 }250 for (scudo::uptr I = 0; I < NumberOfThreads; I++)251 pthread_join(T[I].Thread, 0);252 253 if (TEST_HAS_FAILURE) {254 scudo::ScopedString Str;255 Quarantine.getStats(&Str);256 Str.output();257 }258 259 for (scudo::uptr I = 0; I < NumberOfThreads; I++)260 Quarantine.drainAndRecycle(&T[I].Cache, Cb);261}262 263} // namespace264