OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/persistent_memory_allocator.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/rand_util.h" |
| 10 #include "base/threading/simple_thread.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const uint32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB |
| 16 const uint32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB |
| 17 |
| 18 } // namespace |
| 19 |
| 20 namespace base { |
| 21 |
| 22 typedef PersistentMemoryAllocator::Reference Reference; |
| 23 |
| 24 class PersistentMemoryAllocatorTest : public testing::Test { |
| 25 public: |
| 26 struct TestObject1 { |
| 27 int32_t onething; |
| 28 char oranother; |
| 29 }; |
| 30 |
| 31 struct TestObject2 { |
| 32 int thiis; |
| 33 long that; |
| 34 float andthe; |
| 35 char other; |
| 36 double thing; |
| 37 }; |
| 38 |
| 39 PersistentMemoryAllocatorTest() { |
| 40 mem_segment_.reset(new char[TEST_MEMORY_SIZE]); |
| 41 } |
| 42 |
| 43 void SetUp() override { |
| 44 allocator_.reset(); |
| 45 memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE); |
| 46 allocator_.reset(new PersistentMemoryAllocator( |
| 47 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, |
| 48 "TestAllocator")); |
| 49 } |
| 50 |
| 51 void TearDown() override { |
| 52 allocator_.reset(); |
| 53 } |
| 54 |
| 55 int CountIterables() { |
| 56 PersistentMemoryAllocator::Iterator iter; |
| 57 uint32_t type; |
| 58 int count = 0; |
| 59 for (allocator_->CreateIterator(&iter); |
| 60 allocator_->GetNextIterable(&iter, &type) != 0;) { |
| 61 count++; |
| 62 } |
| 63 return count; |
| 64 } |
| 65 |
| 66 scoped_ptr<char[]> mem_segment_; |
| 67 scoped_ptr<PersistentMemoryAllocator> allocator_; |
| 68 }; |
| 69 |
| 70 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { |
| 71 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 72 allocator_->GetMemoryInfo(&meminfo0); |
| 73 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); |
| 74 EXPECT_GT(meminfo0.total, meminfo0.free); |
| 75 |
| 76 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| 77 EXPECT_NE(0, block1); |
| 78 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject1>(block1, 1)); |
| 79 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1)); |
| 80 EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1)); |
| 81 EXPECT_GE(sizeof(TestObject1) + 7, allocator_->GetAllocSize(block1)); |
| 82 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 83 allocator_->GetMemoryInfo(&meminfo1); |
| 84 EXPECT_EQ(meminfo0.total, meminfo1.total); |
| 85 EXPECT_GT(meminfo0.free, meminfo1.free); |
| 86 |
| 87 PersistentMemoryAllocator::Iterator iter; |
| 88 uint32_t type; |
| 89 allocator_->CreateIterator(&iter); |
| 90 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| 91 allocator_->MakeIterable(block1); |
| 92 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); |
| 93 EXPECT_EQ(1U, type); |
| 94 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| 95 |
| 96 Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
| 97 EXPECT_NE(0, block2); |
| 98 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2)); |
| 99 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1)); |
| 100 EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2)); |
| 101 EXPECT_GE(sizeof(TestObject2) + 7, allocator_->GetAllocSize(block2)); |
| 102 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 103 allocator_->GetMemoryInfo(&meminfo2); |
| 104 EXPECT_EQ(meminfo1.total, meminfo2.total); |
| 105 EXPECT_GT(meminfo1.free, meminfo2.free); |
| 106 |
| 107 allocator_->MakeIterable(block2); |
| 108 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); |
| 109 EXPECT_EQ(2U, type); |
| 110 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| 111 |
| 112 EXPECT_FALSE(allocator_->IsFull()); |
| 113 EXPECT_FALSE(allocator_->IsCorrupt()); |
| 114 |
| 115 allocator_->UpdateStaticHistograms(); |
| 116 scoped_ptr<HistogramSamples> used_samples( |
| 117 allocator_->used_histogram_->SnapshotSamples()); |
| 118 EXPECT_TRUE(used_samples); |
| 119 EXPECT_EQ(1, used_samples->TotalCount()); |
| 120 |
| 121 scoped_ptr<HistogramSamples> allocs_samples( |
| 122 allocator_->allocs_histogram_->SnapshotSamples()); |
| 123 EXPECT_TRUE(allocs_samples); |
| 124 EXPECT_EQ(2, allocs_samples->TotalCount()); |
| 125 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject1))); |
| 126 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject2))); |
| 127 |
| 128 EXPECT_EQ(2, allocator_->GetType(block2)); |
| 129 allocator_->SetType(block2, 3); |
| 130 EXPECT_EQ(3, allocator_->GetType(block2)); |
| 131 } |
| 132 |
| 133 TEST_F(PersistentMemoryAllocatorTest, PageTest) { |
| 134 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
| 135 EXPECT_LT(0, block1); |
| 136 EXPECT_GT((int)TEST_MEMORY_PAGE, block1); |
| 137 |
| 138 Reference block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2); |
| 139 EXPECT_EQ((int)TEST_MEMORY_PAGE, block2); |
| 140 |
| 141 Reference block3 = allocator_->Allocate(99, 3); |
| 142 EXPECT_EQ(2 * (int)TEST_MEMORY_PAGE, block3); |
| 143 } |
| 144 |
| 145 class AllocatorThread : public SimpleThread { |
| 146 public: |
| 147 AllocatorThread(const std::string& name, |
| 148 void* base, |
| 149 int32_t size, |
| 150 int32_t page_size) |
| 151 : SimpleThread(name, Options()), |
| 152 count_(0), |
| 153 iterable_(0), |
| 154 allocator_(base, size, page_size, std::string()) {} |
| 155 |
| 156 void Run() override { |
| 157 for (;;) { |
| 158 int32_t size = (int32_t)base::RandInt(1, 99); |
| 159 uint32_t type = (int32_t)base::RandInt(100, 999); |
| 160 Reference block = allocator_.Allocate(size, type); |
| 161 if (!block) |
| 162 break; |
| 163 |
| 164 count_++; |
| 165 if (base::RandInt(0, 1)) { |
| 166 allocator_.MakeIterable(block); |
| 167 iterable_++; |
| 168 } |
| 169 } |
| 170 } |
| 171 |
| 172 int count_; |
| 173 int iterable_; |
| 174 |
| 175 private: |
| 176 PersistentMemoryAllocator allocator_; |
| 177 }; |
| 178 |
| 179 TEST_F(PersistentMemoryAllocatorTest, ParallelismTest) { |
| 180 void* memory = mem_segment_.get(); |
| 181 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 182 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 183 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 184 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 185 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 186 |
| 187 t1.Start(); |
| 188 t2.Start(); |
| 189 t3.Start(); |
| 190 t4.Start(); |
| 191 t5.Start(); |
| 192 |
| 193 int last_count = 0; |
| 194 do { |
| 195 int count = CountIterables(); |
| 196 EXPECT_LE(last_count, count); |
| 197 } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); |
| 198 |
| 199 t1.Join(); |
| 200 t2.Join(); |
| 201 t3.Join(); |
| 202 t4.Join(); |
| 203 t5.Join(); |
| 204 |
| 205 EXPECT_FALSE(allocator_->IsCorrupt()); |
| 206 EXPECT_EQ(CountIterables(), |
| 207 t1.iterable_ + t2.iterable_ + t3.iterable_ + t4.iterable_ + |
| 208 t5.iterable_); |
| 209 } |
| 210 |
| 211 // This test doesn't verify anything other than it doesn't crash. |
| 212 TEST_F(PersistentMemoryAllocatorTest, CorruptionTest) { |
| 213 char* memory = mem_segment_.get(); |
| 214 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 215 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 216 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 217 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 218 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 219 |
| 220 t1.Start(); |
| 221 t2.Start(); |
| 222 t3.Start(); |
| 223 t4.Start(); |
| 224 t5.Start(); |
| 225 |
| 226 do { |
| 227 size_t offset = base::RandInt(0, TEST_MEMORY_SIZE - 1); |
| 228 char value = base::RandInt(0, 255); |
| 229 memory[offset] = value; |
| 230 } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); |
| 231 |
| 232 t1.Join(); |
| 233 t2.Join(); |
| 234 t3.Join(); |
| 235 t4.Join(); |
| 236 t5.Join(); |
| 237 |
| 238 CountIterables(); |
| 239 } |
| 240 |
| 241 // Attempt to cause crashes or loops by expressly creating dangerous coditions. |
| 242 TEST_F(PersistentMemoryAllocatorTest, MaliciousTest) { |
| 243 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| 244 Reference block2 = allocator_->Allocate(sizeof(TestObject1), 2); |
| 245 Reference block3 = allocator_->Allocate(sizeof(TestObject1), 3); |
| 246 Reference block4 = allocator_->Allocate(sizeof(TestObject1), 3); |
| 247 Reference block5 = allocator_->Allocate(sizeof(TestObject1), 3); |
| 248 allocator_->MakeIterable(block1); |
| 249 allocator_->MakeIterable(block2); |
| 250 allocator_->MakeIterable(block3); |
| 251 allocator_->MakeIterable(block4); |
| 252 allocator_->MakeIterable(block5); |
| 253 EXPECT_EQ(5, CountIterables()); |
| 254 EXPECT_FALSE(allocator_->IsCorrupt()); |
| 255 |
| 256 // Create loop in iterable list and ensure it doesn't hang. |
| 257 int32_t* header4 = (int32_t*)(mem_segment_.get() + block4); |
| 258 EXPECT_EQ(block5, header4[3]); |
| 259 header4[3] = block3; |
| 260 CountIterables(); // loop: 1-2-3-4-3 |
| 261 header4[3] = block2; |
| 262 CountIterables(); // loop: 1-2-3-4-2 |
| 263 header4[3] = block1; |
| 264 CountIterables(); // loop: 1-2-3-4-1 |
| 265 EXPECT_TRUE(allocator_->IsCorrupt()); |
| 266 } |
| 267 |
| 268 } // namespace base |
OLD | NEW |