Chromium Code Reviews| 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..6c0b08c2729bfcc3f7bd19ab2ded31d5c9371e58 |
| --- /dev/null |
| +++ b/base/memory/shared_memory_allocator_unittest.cc |
| @@ -0,0 +1,243 @@ |
| +// 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 int32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB |
| +const int32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB |
| + |
| +} // namespace |
| + |
| +namespace base { |
| + |
| +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; |
| + int32_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); |
| + |
| + int32_t block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| + EXPECT_NE(0, block1); |
| + EXPECT_NE(nullptr, allocator_->GetType<TestObject1>(block1, 1)); |
| + EXPECT_EQ(nullptr, allocator_->GetType<TestObject2>(block1, 1)); |
| + SharedMemoryAllocator::MemoryInfo meminfo1; |
| + allocator_->GetMemoryInfo(&meminfo1); |
| + EXPECT_EQ(meminfo0.total, meminfo1.total); |
| + EXPECT_GT(meminfo0.free, meminfo1.free); |
| + |
| + SharedMemoryAllocator::Iterator iter; |
| + int32_t type; |
| + allocator_->CreateIterator(&iter); |
| + EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| + allocator_->MakeIterable(block1); |
| + EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); |
| + EXPECT_EQ(1, type); |
| + EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| + |
| + int32_t block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
| + EXPECT_NE(0, block2); |
| + EXPECT_NE(nullptr, allocator_->GetType<TestObject2>(block2, 2)); |
| + EXPECT_EQ(nullptr, allocator_->GetType<TestObject2>(block2, 1)); |
| + 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(2, type); |
| + EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| + |
| + EXPECT_FALSE(allocator_->IsFull()); |
| + EXPECT_FALSE(allocator_->IsCorrupted()); |
| +} |
| + |
| +TEST_F(SharedMemoryAllocatorTest, PageTest) { |
| + int32_t block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
| + EXPECT_LT(0, block1); |
| + EXPECT_GT(TEST_MEMORY_PAGE, block1); |
| + |
| + int32_t block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2); |
| + EXPECT_EQ(TEST_MEMORY_PAGE, block2); |
| + |
| + int32_t block3 = allocator_->Allocate(99, 3); |
| + EXPECT_EQ(2 * 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); |
| + int32_t type = (int32_t)base::RandInt(100, 999); |
| + int32_t 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); |
|
Dmitry Vyukov
2015/11/03 14:06:46
Also run iteration thread concurrently.
bcwhite
2015/11/03 16:28:20
The test thread is doing that concurrently with th
|
| + |
| + 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_->IsCorrupted() && !allocator_->IsFull()); |
| + |
| + t1.Join(); |
| + t2.Join(); |
| + t3.Join(); |
| + t4.Join(); |
| + t5.Join(); |
| + |
| + EXPECT_FALSE(allocator_->IsCorrupted()); |
| + 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); |
|
Dmitry Vyukov
2015/11/03 14:06:46
Also run iteration thread concurrently.
|
| + |
| + 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_->IsCorrupted() && !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) { |
| + int32_t block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| + int32_t block2 = allocator_->Allocate(sizeof(TestObject1), 2); |
| + int32_t block3 = allocator_->Allocate(sizeof(TestObject1), 3); |
| + int32_t block4 = allocator_->Allocate(sizeof(TestObject1), 3); |
| + int32_t 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_->IsCorrupted()); |
| + |
| + // 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_->IsCorrupted()); |
| +} |
| + |
| +} // namespace base |