Index: base/memory/persistent_memory_allocator_unittest.cc |
diff --git a/base/memory/persistent_memory_allocator_unittest.cc b/base/memory/persistent_memory_allocator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..26b949b16c24847a294aa060d1aaead73dd58a18 |
--- /dev/null |
+++ b/base/memory/persistent_memory_allocator_unittest.cc |
@@ -0,0 +1,268 @@ |
+// Copyright 2015 The Chromium 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 "base/memory/persistent_memory_allocator.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/metrics/histogram.h" |
+#include "base/rand_util.h" |
+#include "base/threading/simple_thread.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+ |
+namespace { |
+ |
+const uint32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB |
+const uint32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB |
+ |
+} // namespace |
+ |
+namespace base { |
+ |
+typedef PersistentMemoryAllocator::Reference Reference; |
+ |
+class PersistentMemoryAllocatorTest : public testing::Test { |
+ public: |
+ struct TestObject1 { |
+ int32_t onething; |
+ char oranother; |
+ }; |
+ |
+ struct TestObject2 { |
+ int thiis; |
+ long that; |
+ float andthe; |
+ char other; |
+ double thing; |
+ }; |
+ |
+ PersistentMemoryAllocatorTest() { |
+ mem_segment_.reset(new char[TEST_MEMORY_SIZE]); |
+ } |
+ |
+ void SetUp() override { |
+ allocator_.reset(); |
+ memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE); |
+ allocator_.reset(new PersistentMemoryAllocator( |
+ mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, |
+ "TestAllocator")); |
+ } |
+ |
+ void TearDown() override { |
+ allocator_.reset(); |
+ } |
+ |
+ unsigned CountIterables() { |
+ PersistentMemoryAllocator::Iterator iter; |
+ uint32_t type; |
+ unsigned count = 0; |
+ for (allocator_->CreateIterator(&iter); |
+ allocator_->GetNextIterable(&iter, &type) != 0;) { |
+ count++; |
+ } |
+ return count; |
+ } |
+ |
+ scoped_ptr<char[]> mem_segment_; |
+ scoped_ptr<PersistentMemoryAllocator> allocator_; |
+}; |
+ |
+TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { |
+ PersistentMemoryAllocator::MemoryInfo meminfo0; |
+ allocator_->GetMemoryInfo(&meminfo0); |
+ EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); |
+ EXPECT_GT(meminfo0.total, meminfo0.free); |
+ |
+ Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
+ EXPECT_NE(0U, block1); |
+ EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject1>(block1, 1)); |
+ EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1)); |
+ EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1)); |
+ EXPECT_GE(sizeof(TestObject1) + 7, allocator_->GetAllocSize(block1)); |
+ PersistentMemoryAllocator::MemoryInfo meminfo1; |
+ allocator_->GetMemoryInfo(&meminfo1); |
+ EXPECT_EQ(meminfo0.total, meminfo1.total); |
+ EXPECT_GT(meminfo0.free, meminfo1.free); |
+ |
+ PersistentMemoryAllocator::Iterator iter; |
+ uint32_t type; |
+ allocator_->CreateIterator(&iter); |
+ EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
+ allocator_->MakeIterable(block1); |
+ EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); |
+ EXPECT_EQ(1U, type); |
+ EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
+ |
+ Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
+ EXPECT_NE(0U, block2); |
+ EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2)); |
+ EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1)); |
+ EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2)); |
+ EXPECT_GE(sizeof(TestObject2) + 7, allocator_->GetAllocSize(block2)); |
+ PersistentMemoryAllocator::MemoryInfo meminfo2; |
+ allocator_->GetMemoryInfo(&meminfo2); |
+ EXPECT_EQ(meminfo1.total, meminfo2.total); |
+ EXPECT_GT(meminfo1.free, meminfo2.free); |
+ |
+ allocator_->MakeIterable(block2); |
+ EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); |
+ EXPECT_EQ(2U, type); |
+ EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
+ |
+ EXPECT_FALSE(allocator_->IsFull()); |
+ EXPECT_FALSE(allocator_->IsCorrupt()); |
+ |
+ allocator_->UpdateStaticHistograms(); |
+ scoped_ptr<HistogramSamples> used_samples( |
+ allocator_->used_histogram_->SnapshotSamples()); |
+ EXPECT_TRUE(used_samples); |
+ EXPECT_EQ(1, used_samples->TotalCount()); |
+ |
+ scoped_ptr<HistogramSamples> allocs_samples( |
+ allocator_->allocs_histogram_->SnapshotSamples()); |
+ EXPECT_TRUE(allocs_samples); |
+ EXPECT_EQ(2, allocs_samples->TotalCount()); |
+ EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject1))); |
+ EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject2))); |
+ |
+ EXPECT_EQ(2U, allocator_->GetType(block2)); |
+ allocator_->SetType(block2, 3); |
+ EXPECT_EQ(3U, allocator_->GetType(block2)); |
+} |
+ |
+TEST_F(PersistentMemoryAllocatorTest, PageTest) { |
+ Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
+ EXPECT_LT(0U, block1); |
+ EXPECT_GT(TEST_MEMORY_PAGE, block1); |
+ |
+ Reference block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2); |
+ EXPECT_EQ(TEST_MEMORY_PAGE, block2); |
+ |
+ Reference block3 = allocator_->Allocate(99, 3); |
+ EXPECT_EQ(2U * TEST_MEMORY_PAGE, block3); |
+} |
+ |
+class AllocatorThread : public SimpleThread { |
+ public: |
+ AllocatorThread(const std::string& name, |
+ void* base, |
+ uint32_t size, |
+ uint32_t page_size) |
+ : SimpleThread(name, Options()), |
+ count_(0), |
+ iterable_(0), |
+ allocator_(base, size, page_size, std::string()) {} |
+ |
+ void Run() override { |
+ for (;;) { |
+ uint32_t size = base::RandInt(1, 99); |
+ uint32_t type = base::RandInt(100, 999); |
+ Reference block = allocator_.Allocate(size, type); |
+ if (!block) |
+ break; |
+ |
+ count_++; |
+ if (base::RandInt(0, 1)) { |
+ allocator_.MakeIterable(block); |
+ iterable_++; |
+ } |
+ } |
+ } |
+ |
+ unsigned count_; |
+ unsigned iterable_; |
+ |
+ private: |
+ PersistentMemoryAllocator allocator_; |
+}; |
+ |
+TEST_F(PersistentMemoryAllocatorTest, ParallelismTest) { |
+ void* memory = mem_segment_.get(); |
+ AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ |
+ t1.Start(); |
+ t2.Start(); |
+ t3.Start(); |
+ t4.Start(); |
+ t5.Start(); |
+ |
+ unsigned last_count = 0; |
+ do { |
+ unsigned count = CountIterables(); |
+ EXPECT_LE(last_count, count); |
+ } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); |
+ |
+ t1.Join(); |
+ t2.Join(); |
+ t3.Join(); |
+ t4.Join(); |
+ t5.Join(); |
+ |
+ EXPECT_FALSE(allocator_->IsCorrupt()); |
+ EXPECT_EQ(CountIterables(), |
+ t1.iterable_ + t2.iterable_ + t3.iterable_ + t4.iterable_ + |
+ t5.iterable_); |
+} |
+ |
+// This test doesn't verify anything other than it doesn't crash. |
+TEST_F(PersistentMemoryAllocatorTest, CorruptionTest) { |
+ char* memory = mem_segment_.get(); |
+ AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
+ |
+ t1.Start(); |
+ t2.Start(); |
+ t3.Start(); |
+ t4.Start(); |
+ t5.Start(); |
+ |
+ do { |
+ size_t offset = base::RandInt(0, TEST_MEMORY_SIZE - 1); |
+ char value = base::RandInt(0, 255); |
+ memory[offset] = value; |
+ } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); |
+ |
+ t1.Join(); |
+ t2.Join(); |
+ t3.Join(); |
+ t4.Join(); |
+ t5.Join(); |
+ |
+ CountIterables(); |
+} |
+ |
+// Attempt to cause crashes or loops by expressly creating dangerous coditions. |
+TEST_F(PersistentMemoryAllocatorTest, MaliciousTest) { |
+ Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
+ Reference block2 = allocator_->Allocate(sizeof(TestObject1), 2); |
+ Reference block3 = allocator_->Allocate(sizeof(TestObject1), 3); |
+ Reference block4 = allocator_->Allocate(sizeof(TestObject1), 3); |
+ Reference block5 = allocator_->Allocate(sizeof(TestObject1), 3); |
+ allocator_->MakeIterable(block1); |
+ allocator_->MakeIterable(block2); |
+ allocator_->MakeIterable(block3); |
+ allocator_->MakeIterable(block4); |
+ allocator_->MakeIterable(block5); |
+ EXPECT_EQ(5U, CountIterables()); |
+ EXPECT_FALSE(allocator_->IsCorrupt()); |
+ |
+ // Create loop in iterable list and ensure it doesn't hang. |
+ uint32_t* header4 = (uint32_t*)(mem_segment_.get() + block4); |
+ EXPECT_EQ(block5, header4[3]); |
+ header4[3] = block3; |
+ CountIterables(); // loop: 1-2-3-4-3 |
+ header4[3] = block2; |
+ CountIterables(); // loop: 1-2-3-4-2 |
+ header4[3] = block1; |
+ CountIterables(); // loop: 1-2-3-4-1 |
+ EXPECT_TRUE(allocator_->IsCorrupt()); |
+} |
+ |
+} // namespace base |