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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/heap/cppgc/page-memory.h"
#include "src/base/page-allocator.h"
#include "src/heap/cppgc/page-memory-inl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cppgc {
namespace internal {
TEST(MemoryRegionTest, Construct) {
constexpr size_t kSize = 17;
uint8_t dummy[kSize];
const MemoryRegion region(dummy, kSize);
EXPECT_EQ(dummy, region.base());
EXPECT_EQ(kSize, region.size());
EXPECT_EQ(dummy + kSize, region.end());
}
namespace {
Address AtOffset(uint8_t* base, intptr_t offset) {
return reinterpret_cast<Address>(reinterpret_cast<intptr_t>(base) + offset);
}
} // namespace
TEST(MemoryRegionTest, ContainsAddress) {
constexpr size_t kSize = 7;
uint8_t dummy[kSize];
const MemoryRegion region(dummy, kSize);
EXPECT_FALSE(region.Contains(AtOffset(dummy, -1)));
EXPECT_TRUE(region.Contains(dummy));
EXPECT_TRUE(region.Contains(dummy + kSize - 1));
EXPECT_FALSE(region.Contains(AtOffset(dummy, kSize)));
}
TEST(MemoryRegionTest, ContainsMemoryRegion) {
constexpr size_t kSize = 7;
uint8_t dummy[kSize];
const MemoryRegion region(dummy, kSize);
const MemoryRegion contained_region1(dummy, kSize - 1);
EXPECT_TRUE(region.Contains(contained_region1));
const MemoryRegion contained_region2(dummy + 1, kSize - 1);
EXPECT_TRUE(region.Contains(contained_region2));
const MemoryRegion not_contained_region1(AtOffset(dummy, -1), kSize);
EXPECT_FALSE(region.Contains(not_contained_region1));
const MemoryRegion not_contained_region2(AtOffset(dummy, kSize), 1);
EXPECT_FALSE(region.Contains(not_contained_region2));
}
TEST(PageMemoryTest, Construct) {
constexpr size_t kOverallSize = 17;
uint8_t dummy[kOverallSize];
const MemoryRegion overall_region(dummy, kOverallSize);
const MemoryRegion writeable_region(dummy + 1, kOverallSize - 2);
const PageMemory page_memory(overall_region, writeable_region);
EXPECT_EQ(dummy, page_memory.overall_region().base());
EXPECT_EQ(dummy + kOverallSize, page_memory.overall_region().end());
EXPECT_EQ(dummy + 1, page_memory.writeable_region().base());
EXPECT_EQ(dummy + kOverallSize - 1, page_memory.writeable_region().end());
}
#if DEBUG
TEST(PageMemoryDeathTest, ConstructNonContainedRegions) {
constexpr size_t kOverallSize = 17;
uint8_t dummy[kOverallSize];
const MemoryRegion overall_region(dummy, kOverallSize);
const MemoryRegion writeable_region(dummy + 1, kOverallSize);
EXPECT_DEATH_IF_SUPPORTED(PageMemory(overall_region, writeable_region), "");
}
#endif // DEBUG
TEST(PageMemoryRegionTest, NormalPageMemoryRegion) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<NormalPageMemoryRegion>(&allocator);
pmr->UnprotectForTesting();
MemoryRegion prev_overall;
for (size_t i = 0; i < NormalPageMemoryRegion::kNumPageRegions; ++i) {
const PageMemory pm = pmr->GetPageMemory(i);
// Previous PageMemory aligns with the current one.
if (prev_overall.base()) {
EXPECT_EQ(prev_overall.end(), pm.overall_region().base());
}
prev_overall =
MemoryRegion(pm.overall_region().base(), pm.overall_region().size());
// Writeable region is contained in overall region.
EXPECT_TRUE(pm.overall_region().Contains(pm.writeable_region()));
EXPECT_EQ(0u, pm.writeable_region().base()[0]);
EXPECT_EQ(0u, pm.writeable_region().end()[-1]);
// Front guard page.
EXPECT_EQ(pm.writeable_region().base(),
pm.overall_region().base() + kGuardPageSize);
// Back guard page.
EXPECT_EQ(pm.overall_region().end(),
pm.writeable_region().end() + kGuardPageSize);
}
}
TEST(PageMemoryRegionTest, LargePageMemoryRegion) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<LargePageMemoryRegion>(&allocator, 1024);
pmr->UnprotectForTesting();
const PageMemory pm = pmr->GetPageMemory();
EXPECT_LE(1024u, pm.writeable_region().size());
EXPECT_EQ(0u, pm.writeable_region().base()[0]);
EXPECT_EQ(0u, pm.writeable_region().end()[-1]);
}
TEST(PageMemoryRegionTest, PlatformUsesGuardPages) {
// This tests that the testing allocator actually uses protected guard
// regions.
v8::base::PageAllocator allocator;
#if defined(V8_HOST_ARCH_PPC64) && !defined(_AIX)
EXPECT_FALSE(SupportsCommittingGuardPages(&allocator));
#else // !V8_HOST_ARCH_PPC64
EXPECT_TRUE(SupportsCommittingGuardPages(&allocator));
#endif // !V8_HOST_ARCH_PPC64
}
namespace {
V8_NOINLINE uint8_t access(volatile const uint8_t& u) { return u; }
} // namespace
TEST(PageMemoryRegionDeathTest, ReservationIsFreed) {
// Full sequence as part of the death test macro as otherwise, the macro
// may expand to statements that re-purpose the previously freed memory
// and thus not crash.
EXPECT_DEATH_IF_SUPPORTED(
v8::base::PageAllocator allocator; Address base; {
auto pmr = std::make_unique<LargePageMemoryRegion>(&allocator, 1024);
base = pmr->reserved_region().base();
} access(base[0]);
, "");
}
TEST(PageMemoryRegionDeathTest, FrontGuardPageAccessCrashes) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<NormalPageMemoryRegion>(&allocator);
if (SupportsCommittingGuardPages(&allocator)) {
EXPECT_DEATH_IF_SUPPORTED(
access(pmr->GetPageMemory(0).overall_region().base()[0]), "");
}
}
TEST(PageMemoryRegionDeathTest, BackGuardPageAccessCrashes) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<NormalPageMemoryRegion>(&allocator);
if (SupportsCommittingGuardPages(&allocator)) {
EXPECT_DEATH_IF_SUPPORTED(
access(pmr->GetPageMemory(0).writeable_region().end()[0]), "");
}
}
TEST(PageMemoryRegionTreeTest, AddNormalLookupRemove) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<NormalPageMemoryRegion>(&allocator);
PageMemoryRegionTree tree;
tree.Add(pmr.get());
ASSERT_EQ(pmr.get(), tree.Lookup(pmr->reserved_region().base()));
ASSERT_EQ(pmr.get(), tree.Lookup(pmr->reserved_region().end() - 1));
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().base() - 1));
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().end()));
tree.Remove(pmr.get());
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().base()));
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().end() - 1));
}
TEST(PageMemoryRegionTreeTest, AddLargeLookupRemove) {
v8::base::PageAllocator allocator;
constexpr size_t kLargeSize = 5012;
auto pmr = std::make_unique<LargePageMemoryRegion>(&allocator, kLargeSize);
PageMemoryRegionTree tree;
tree.Add(pmr.get());
ASSERT_EQ(pmr.get(), tree.Lookup(pmr->reserved_region().base()));
ASSERT_EQ(pmr.get(), tree.Lookup(pmr->reserved_region().end() - 1));
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().base() - 1));
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().end()));
tree.Remove(pmr.get());
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().base()));
ASSERT_EQ(nullptr, tree.Lookup(pmr->reserved_region().end() - 1));
}
TEST(PageMemoryRegionTreeTest, AddLookupRemoveMultiple) {
v8::base::PageAllocator allocator;
auto pmr1 = std::make_unique<NormalPageMemoryRegion>(&allocator);
constexpr size_t kLargeSize = 3127;
auto pmr2 = std::make_unique<LargePageMemoryRegion>(&allocator, kLargeSize);
PageMemoryRegionTree tree;
tree.Add(pmr1.get());
tree.Add(pmr2.get());
ASSERT_EQ(pmr1.get(), tree.Lookup(pmr1->reserved_region().base()));
ASSERT_EQ(pmr1.get(), tree.Lookup(pmr1->reserved_region().end() - 1));
ASSERT_EQ(pmr2.get(), tree.Lookup(pmr2->reserved_region().base()));
ASSERT_EQ(pmr2.get(), tree.Lookup(pmr2->reserved_region().end() - 1));
tree.Remove(pmr1.get());
ASSERT_EQ(pmr2.get(), tree.Lookup(pmr2->reserved_region().base()));
ASSERT_EQ(pmr2.get(), tree.Lookup(pmr2->reserved_region().end() - 1));
tree.Remove(pmr2.get());
ASSERT_EQ(nullptr, tree.Lookup(pmr2->reserved_region().base()));
ASSERT_EQ(nullptr, tree.Lookup(pmr2->reserved_region().end() - 1));
}
TEST(NormalPageMemoryPool, ConstructorEmpty) {
v8::base::PageAllocator allocator;
NormalPageMemoryPool pool;
constexpr size_t kBucket = 0;
EXPECT_EQ(NormalPageMemoryPool::Result(nullptr, nullptr), pool.Take(kBucket));
}
TEST(NormalPageMemoryPool, AddTakeSameBucket) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<NormalPageMemoryRegion>(&allocator);
const PageMemory pm = pmr->GetPageMemory(0);
NormalPageMemoryPool pool;
constexpr size_t kBucket = 0;
pool.Add(kBucket, pmr.get(), pm.writeable_region().base());
EXPECT_EQ(
NormalPageMemoryPool::Result(pmr.get(), pm.writeable_region().base()),
pool.Take(kBucket));
}
TEST(NormalPageMemoryPool, AddTakeNotFoundDifferentBucket) {
v8::base::PageAllocator allocator;
auto pmr = std::make_unique<NormalPageMemoryRegion>(&allocator);
const PageMemory pm = pmr->GetPageMemory(0);
NormalPageMemoryPool pool;
constexpr size_t kFirstBucket = 0;
constexpr size_t kSecondBucket = 1;
pool.Add(kFirstBucket, pmr.get(), pm.writeable_region().base());
EXPECT_EQ(NormalPageMemoryPool::Result(nullptr, nullptr),
pool.Take(kSecondBucket));
EXPECT_EQ(
NormalPageMemoryPool::Result(pmr.get(), pm.writeable_region().base()),
pool.Take(kFirstBucket));
}
TEST(PageBackendTest, AllocateNormalUsesPool) {
v8::base::PageAllocator allocator;
PageBackend backend(&allocator);
constexpr size_t kBucket = 0;
Address writeable_base1 = backend.AllocateNormalPageMemory(kBucket);
EXPECT_NE(nullptr, writeable_base1);
backend.FreeNormalPageMemory(kBucket, writeable_base1);
Address writeable_base2 = backend.AllocateNormalPageMemory(kBucket);
EXPECT_NE(nullptr, writeable_base2);
EXPECT_EQ(writeable_base1, writeable_base2);
}
TEST(PageBackendTest, AllocateLarge) {
v8::base::PageAllocator allocator;
PageBackend backend(&allocator);
Address writeable_base1 = backend.AllocateLargePageMemory(13731);
EXPECT_NE(nullptr, writeable_base1);
Address writeable_base2 = backend.AllocateLargePageMemory(9478);
EXPECT_NE(nullptr, writeable_base2);
EXPECT_NE(writeable_base1, writeable_base2);
backend.FreeLargePageMemory(writeable_base1);
backend.FreeLargePageMemory(writeable_base2);
}
TEST(PageBackendTest, LookupNormal) {
v8::base::PageAllocator allocator;
PageBackend backend(&allocator);
constexpr size_t kBucket = 0;
Address writeable_base = backend.AllocateNormalPageMemory(kBucket);
EXPECT_EQ(nullptr, backend.Lookup(writeable_base - kGuardPageSize));
EXPECT_EQ(nullptr, backend.Lookup(writeable_base - 1));
EXPECT_EQ(writeable_base, backend.Lookup(writeable_base));
EXPECT_EQ(writeable_base, backend.Lookup(writeable_base + kPageSize -
2 * kGuardPageSize - 1));
EXPECT_EQ(nullptr,
backend.Lookup(writeable_base + kPageSize - 2 * kGuardPageSize));
EXPECT_EQ(nullptr,
backend.Lookup(writeable_base - kGuardPageSize + kPageSize - 1));
}
TEST(PageBackendTest, LookupLarge) {
v8::base::PageAllocator allocator;
PageBackend backend(&allocator);
constexpr size_t kSize = 7934;
Address writeable_base = backend.AllocateLargePageMemory(kSize);
EXPECT_EQ(nullptr, backend.Lookup(writeable_base - kGuardPageSize));
EXPECT_EQ(nullptr, backend.Lookup(writeable_base - 1));
EXPECT_EQ(writeable_base, backend.Lookup(writeable_base));
EXPECT_EQ(writeable_base, backend.Lookup(writeable_base + kSize - 1));
}
TEST(PageBackendDeathTest, DestructingBackendDestroysPageMemory) {
v8::base::PageAllocator allocator;
Address base;
{
PageBackend backend(&allocator);
constexpr size_t kBucket = 0;
base = backend.AllocateNormalPageMemory(kBucket);
}
EXPECT_DEATH_IF_SUPPORTED(access(base[0]), "");
}
} // namespace internal
} // namespace cppgc
|