Index: base/memory/shared_memory_allocator_unittest.cc |
diff --git a/base/memory/shared_memory_allocator_unittest.cc b/base/memory/shared_memory_allocator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9530d225fa797e12f8cdec8d4c5fdd3f616931d3 |
--- /dev/null |
+++ b/base/memory/shared_memory_allocator_unittest.cc |
@@ -0,0 +1,249 @@ |
+// 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/shared_memory_allocator.h" |
+ |
+#include "base/memory/scoped_ptr.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 { |
+ |
Alexander Potapenko
2015/11/09 18:03:46
You're missing the tests for:
-- using the alloca
bcwhite
2015/11/09 19:21:16
The ParallelismTest and CorruptionTest are intra-p
|
+typedef SharedMemoryAllocator::Reference Reference; |
+ |
+class SharedMemoryAllocatorTest : public testing::Test { |
+ public: |
+ struct TestObject1 { |
+ int32_t onething; |
+ char oranother; |
+ }; |
+ |
+ struct TestObject2 { |
+ int thiis; |
+ long that; |
+ float andthe; |
+ char other; |
+ double thing; |
+ }; |
+ |
+ SharedMemoryAllocatorTest() { |
+ mem_segment_.reset(new char[TEST_MEMORY_SIZE]); |
+ } |
+ |
+ void SetUp() override { |
+ allocator_.reset(); |
+ memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE); |
+ allocator_.reset(new SharedMemoryAllocator( |
+ mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE)); |
+ } |
+ |
+ void TearDown() override { |
+ allocator_.reset(); |
+ } |
+ |
+ int CountIterables() { |
+ SharedMemoryAllocator::Iterator iter; |
+ uint32_t type; |
+ int count = 0; |
+ for (allocator_->CreateIterator(&iter); |
+ allocator_->GetNextIterable(&iter, &type) != 0;) { |
+ count++; |
+ } |
+ return count; |
+ } |
+ |
+ scoped_ptr<char[]> mem_segment_; |
+ scoped_ptr<SharedMemoryAllocator> allocator_; |
+}; |
+ |
+TEST_F(SharedMemoryAllocatorTest, AllocateAndIterate) { |
+ SharedMemoryAllocator::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(0, 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)); |
+ SharedMemoryAllocator::MemoryInfo meminfo1; |
+ allocator_->GetMemoryInfo(&meminfo1); |
+ EXPECT_EQ(meminfo0.total, meminfo1.total); |
+ EXPECT_GT(meminfo0.free, meminfo1.free); |
+ |
+ SharedMemoryAllocator::Iterator iter; |
+ uint32_t type; |
+ allocator_->CreateIterator(&iter); |
+ EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
+ allocator_->MakeIterable(block1); |
+ EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); |
+ EXPECT_EQ(1U, type); |
+ EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
+ |
+ Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
+ EXPECT_NE(0, 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)); |
+ SharedMemoryAllocator::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(0, allocator_->GetNextIterable(&iter, &type)); |
+ |
+ EXPECT_FALSE(allocator_->IsFull()); |
+ EXPECT_FALSE(allocator_->IsCorrupt()); |
+} |
+ |
+TEST_F(SharedMemoryAllocatorTest, PageTest) { |
+ Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
+ EXPECT_LT(0, block1); |
+ EXPECT_GT((int)TEST_MEMORY_PAGE, block1); |
+ |
+ Reference block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2); |
+ EXPECT_EQ((int)TEST_MEMORY_PAGE, block2); |
+ |
+ Reference block3 = allocator_->Allocate(99, 3); |
+ EXPECT_EQ(2 * (int)TEST_MEMORY_PAGE, block3); |
+} |
+ |
+class AllocatorThread : public SimpleThread { |
+ public: |
+ AllocatorThread(const std::string& name, |
+ void* base, |
+ int32_t size, |
+ int32_t page_size) |
+ : SimpleThread(name, Options()), |
+ count_(0), |
+ iterable_(0), |
+ allocator_(base, size, page_size) {} |
+ |
+ void Run() override { |
+ for (;;) { |
+ int32_t size = (int32_t)base::RandInt(1, 99); |
+ uint32_t type = (int32_t)base::RandInt(100, 999); |
+ Reference block = allocator_.Allocate(size, type); |
+ if (!block) |
+ break; |
+ |
+ count_++; |
+ if (base::RandInt(0, 1)) { |
+ allocator_.MakeIterable(block); |
+ iterable_++; |
+ } |
+ } |
+ } |
+ |
+ int count_; |
+ int iterable_; |
+ |
+ private: |
+ SharedMemoryAllocator allocator_; |
+}; |
+ |
+TEST_F(SharedMemoryAllocatorTest, 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(); |
+ |
+ int last_count = 0; |
+ do { |
+ int 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(SharedMemoryAllocatorTest, 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(SharedMemoryAllocatorTest, 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(5, CountIterables()); |
+ EXPECT_FALSE(allocator_->IsCorrupt()); |
+ |
+ // Create loop in iterable list and ensure it doesn't hang. |
+ int32_t* header4 = (int32_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 |