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