1 // Copyright 2018 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 <utility>
6 
7 #include "base/memory/platform_shared_memory_region.h"
8 #include "base/memory/read_only_shared_memory_region.h"
9 #include "base/memory/unsafe_shared_memory_region.h"
10 #include "base/memory/writable_shared_memory_region.h"
11 #include "base/system/sys_info.h"
12 #include "base/test/test_shared_memory_util.h"
13 #include "build/build_config.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace base {
17 
18 const size_t kRegionSize = 1024;
19 
IsMemoryFilledWithByte(const void * memory,size_t size,char byte)20 bool IsMemoryFilledWithByte(const void* memory, size_t size, char byte) {
21   const char* start_ptr = static_cast<const char*>(memory);
22   const char* end_ptr = start_ptr + size;
23   for (const char* ptr = start_ptr; ptr < end_ptr; ++ptr) {
24     if (*ptr != byte)
25       return false;
26   }
27 
28   return true;
29 }
30 
31 template <typename SharedMemoryRegionType>
32 class SharedMemoryRegionTest : public ::testing::Test {
33  public:
SetUp()34   void SetUp() override {
35     std::tie(region_, rw_mapping_) =
36         CreateMappedRegion<SharedMemoryRegionType>(kRegionSize);
37     ASSERT_TRUE(region_.IsValid());
38     ASSERT_TRUE(rw_mapping_.IsValid());
39     memset(rw_mapping_.memory(), 'G', kRegionSize);
40     EXPECT_TRUE(IsMemoryFilledWithByte(rw_mapping_.memory(), kRegionSize, 'G'));
41   }
42 
43  protected:
44   SharedMemoryRegionType region_;
45   WritableSharedMemoryMapping rw_mapping_;
46 };
47 
48 typedef ::testing::Types<WritableSharedMemoryRegion,
49                          UnsafeSharedMemoryRegion,
50                          ReadOnlySharedMemoryRegion>
51     AllRegionTypes;
52 TYPED_TEST_SUITE(SharedMemoryRegionTest, AllRegionTypes);
53 
TYPED_TEST(SharedMemoryRegionTest,NonValidRegion)54 TYPED_TEST(SharedMemoryRegionTest, NonValidRegion) {
55   TypeParam region;
56   EXPECT_FALSE(region.IsValid());
57   // We shouldn't crash on Map but should return an invalid mapping.
58   typename TypeParam::MappingType mapping = region.Map();
59   EXPECT_FALSE(mapping.IsValid());
60 }
61 
TYPED_TEST(SharedMemoryRegionTest,MoveRegion)62 TYPED_TEST(SharedMemoryRegionTest, MoveRegion) {
63   TypeParam moved_region = std::move(this->region_);
64   EXPECT_FALSE(this->region_.IsValid());
65   ASSERT_TRUE(moved_region.IsValid());
66 
67   // Check that moved region maps correctly.
68   typename TypeParam::MappingType mapping = moved_region.Map();
69   ASSERT_TRUE(mapping.IsValid());
70   EXPECT_NE(this->rw_mapping_.memory(), mapping.memory());
71   EXPECT_EQ(memcmp(this->rw_mapping_.memory(), mapping.memory(), kRegionSize),
72             0);
73 
74   // Verify that the second mapping reflects changes in the first.
75   memset(this->rw_mapping_.memory(), '#', kRegionSize);
76   EXPECT_EQ(memcmp(this->rw_mapping_.memory(), mapping.memory(), kRegionSize),
77             0);
78 }
79 
TYPED_TEST(SharedMemoryRegionTest,MappingValidAfterClose)80 TYPED_TEST(SharedMemoryRegionTest, MappingValidAfterClose) {
81   // Check the mapping is still valid after the region is closed.
82   this->region_ = TypeParam();
83   EXPECT_FALSE(this->region_.IsValid());
84   ASSERT_TRUE(this->rw_mapping_.IsValid());
85   EXPECT_TRUE(
86       IsMemoryFilledWithByte(this->rw_mapping_.memory(), kRegionSize, 'G'));
87 }
88 
TYPED_TEST(SharedMemoryRegionTest,MapTwice)89 TYPED_TEST(SharedMemoryRegionTest, MapTwice) {
90   // The second mapping is either writable or read-only.
91   typename TypeParam::MappingType mapping = this->region_.Map();
92   ASSERT_TRUE(mapping.IsValid());
93   EXPECT_NE(this->rw_mapping_.memory(), mapping.memory());
94   EXPECT_EQ(memcmp(this->rw_mapping_.memory(), mapping.memory(), kRegionSize),
95             0);
96 
97   // Verify that the second mapping reflects changes in the first.
98   memset(this->rw_mapping_.memory(), '#', kRegionSize);
99   EXPECT_EQ(memcmp(this->rw_mapping_.memory(), mapping.memory(), kRegionSize),
100             0);
101 
102   // Close the region and unmap the first memory segment, verify the second
103   // still has the right data.
104   this->region_ = TypeParam();
105   this->rw_mapping_ = WritableSharedMemoryMapping();
106   EXPECT_TRUE(IsMemoryFilledWithByte(mapping.memory(), kRegionSize, '#'));
107 }
108 
TYPED_TEST(SharedMemoryRegionTest,MapUnmapMap)109 TYPED_TEST(SharedMemoryRegionTest, MapUnmapMap) {
110   this->rw_mapping_ = WritableSharedMemoryMapping();
111 
112   typename TypeParam::MappingType mapping = this->region_.Map();
113   ASSERT_TRUE(mapping.IsValid());
114   EXPECT_TRUE(IsMemoryFilledWithByte(mapping.memory(), kRegionSize, 'G'));
115 }
116 
TYPED_TEST(SharedMemoryRegionTest,SerializeAndDeserialize)117 TYPED_TEST(SharedMemoryRegionTest, SerializeAndDeserialize) {
118   subtle::PlatformSharedMemoryRegion platform_region =
119       TypeParam::TakeHandleForSerialization(std::move(this->region_));
120   EXPECT_EQ(platform_region.GetGUID(), this->rw_mapping_.guid());
121   TypeParam region = TypeParam::Deserialize(std::move(platform_region));
122   EXPECT_TRUE(region.IsValid());
123   EXPECT_FALSE(this->region_.IsValid());
124   typename TypeParam::MappingType mapping = region.Map();
125   ASSERT_TRUE(mapping.IsValid());
126   EXPECT_TRUE(IsMemoryFilledWithByte(mapping.memory(), kRegionSize, 'G'));
127 
128   // Verify that the second mapping reflects changes in the first.
129   memset(this->rw_mapping_.memory(), '#', kRegionSize);
130   EXPECT_EQ(memcmp(this->rw_mapping_.memory(), mapping.memory(), kRegionSize),
131             0);
132 }
133 
134 // Map() will return addresses which are aligned to the platform page size, this
135 // varies from platform to platform though.  Since we'd like to advertise a
136 // minimum alignment that callers can count on, test for it here.
TYPED_TEST(SharedMemoryRegionTest,MapMinimumAlignment)137 TYPED_TEST(SharedMemoryRegionTest, MapMinimumAlignment) {
138   EXPECT_EQ(0U,
139             reinterpret_cast<uintptr_t>(this->rw_mapping_.memory()) &
140                 (subtle::PlatformSharedMemoryRegion::kMapMinimumAlignment - 1));
141 }
142 
TYPED_TEST(SharedMemoryRegionTest,MapSize)143 TYPED_TEST(SharedMemoryRegionTest, MapSize) {
144   EXPECT_EQ(this->rw_mapping_.size(), kRegionSize);
145   EXPECT_GE(this->rw_mapping_.mapped_size(), kRegionSize);
146 }
147 
TYPED_TEST(SharedMemoryRegionTest,MapGranularity)148 TYPED_TEST(SharedMemoryRegionTest, MapGranularity) {
149   EXPECT_LT(this->rw_mapping_.mapped_size(),
150             kRegionSize + SysInfo::VMAllocationGranularity());
151 }
152 
TYPED_TEST(SharedMemoryRegionTest,MapAt)153 TYPED_TEST(SharedMemoryRegionTest, MapAt) {
154   const size_t kPageSize = SysInfo::VMAllocationGranularity();
155   ASSERT_TRUE(kPageSize >= sizeof(uint32_t));
156   ASSERT_EQ(kPageSize % sizeof(uint32_t), 0U);
157   const size_t kDataSize = kPageSize * 2;
158   const size_t kCount = kDataSize / sizeof(uint32_t);
159 
160   TypeParam region;
161   WritableSharedMemoryMapping rw_mapping;
162   std::tie(region, rw_mapping) = CreateMappedRegion<TypeParam>(kDataSize);
163   ASSERT_TRUE(region.IsValid());
164   ASSERT_TRUE(rw_mapping.IsValid());
165   uint32_t* ptr = static_cast<uint32_t*>(rw_mapping.memory());
166 
167   for (size_t i = 0; i < kCount; ++i)
168     ptr[i] = i;
169 
170   rw_mapping = WritableSharedMemoryMapping();
171   off_t bytes_offset = kPageSize;
172   typename TypeParam::MappingType mapping =
173       region.MapAt(bytes_offset, kDataSize - bytes_offset);
174   ASSERT_TRUE(mapping.IsValid());
175 
176   off_t int_offset = bytes_offset / sizeof(uint32_t);
177   const uint32_t* ptr2 = static_cast<const uint32_t*>(mapping.memory());
178   for (size_t i = int_offset; i < kCount; ++i) {
179     EXPECT_EQ(ptr2[i - int_offset], i);
180   }
181 }
182 
TYPED_TEST(SharedMemoryRegionTest,MapAtNotAlignedOffsetFails)183 TYPED_TEST(SharedMemoryRegionTest, MapAtNotAlignedOffsetFails) {
184   const size_t kDataSize = SysInfo::VMAllocationGranularity();
185 
186   TypeParam region;
187   WritableSharedMemoryMapping rw_mapping;
188   std::tie(region, rw_mapping) = CreateMappedRegion<TypeParam>(kDataSize);
189   ASSERT_TRUE(region.IsValid());
190   ASSERT_TRUE(rw_mapping.IsValid());
191 #if !defined(OS_BSD)
192   // On FreeBSD, mmap() does not require an aligned offset
193   off_t offset = kDataSize / 2;
194   typename TypeParam::MappingType mapping =
195       region.MapAt(offset, kDataSize - offset);
196   EXPECT_FALSE(mapping.IsValid());
197 #endif
198 }
199 
TYPED_TEST(SharedMemoryRegionTest,MapZeroBytesFails)200 TYPED_TEST(SharedMemoryRegionTest, MapZeroBytesFails) {
201   typename TypeParam::MappingType mapping = this->region_.MapAt(0, 0);
202   EXPECT_FALSE(mapping.IsValid());
203 }
204 
TYPED_TEST(SharedMemoryRegionTest,MapMoreBytesThanRegionSizeFails)205 TYPED_TEST(SharedMemoryRegionTest, MapMoreBytesThanRegionSizeFails) {
206   size_t region_real_size = this->region_.GetSize();
207   typename TypeParam::MappingType mapping =
208       this->region_.MapAt(0, region_real_size + 1);
209   EXPECT_FALSE(mapping.IsValid());
210 }
211 
212 template <typename DuplicatableSharedMemoryRegion>
213 class DuplicatableSharedMemoryRegionTest
214     : public SharedMemoryRegionTest<DuplicatableSharedMemoryRegion> {};
215 
216 typedef ::testing::Types<UnsafeSharedMemoryRegion, ReadOnlySharedMemoryRegion>
217     DuplicatableRegionTypes;
218 TYPED_TEST_SUITE(DuplicatableSharedMemoryRegionTest, DuplicatableRegionTypes);
219 
TYPED_TEST(DuplicatableSharedMemoryRegionTest,Duplicate)220 TYPED_TEST(DuplicatableSharedMemoryRegionTest, Duplicate) {
221   TypeParam dup_region = this->region_.Duplicate();
222   EXPECT_EQ(this->region_.GetGUID(), dup_region.GetGUID());
223   typename TypeParam::MappingType mapping = dup_region.Map();
224   ASSERT_TRUE(mapping.IsValid());
225   EXPECT_NE(this->rw_mapping_.memory(), mapping.memory());
226   EXPECT_EQ(this->rw_mapping_.guid(), mapping.guid());
227   EXPECT_TRUE(IsMemoryFilledWithByte(mapping.memory(), kRegionSize, 'G'));
228 }
229 
230 class ReadOnlySharedMemoryRegionTest : public ::testing::Test {
231  public:
GetInitiallyReadOnlyRegion(size_t size)232   ReadOnlySharedMemoryRegion GetInitiallyReadOnlyRegion(size_t size) {
233     MappedReadOnlyRegion mapped_region =
234         ReadOnlySharedMemoryRegion::Create(size);
235     ReadOnlySharedMemoryRegion region = std::move(mapped_region.region);
236     return region;
237   }
238 
GetConvertedToReadOnlyRegion(size_t size)239   ReadOnlySharedMemoryRegion GetConvertedToReadOnlyRegion(size_t size) {
240     WritableSharedMemoryRegion region =
241         WritableSharedMemoryRegion::Create(kRegionSize);
242     ReadOnlySharedMemoryRegion ro_region =
243         WritableSharedMemoryRegion::ConvertToReadOnly(std::move(region));
244     return ro_region;
245   }
246 };
247 
TEST_F(ReadOnlySharedMemoryRegionTest,InitiallyReadOnlyRegionCannotBeMappedAsWritable)248 TEST_F(ReadOnlySharedMemoryRegionTest,
249        InitiallyReadOnlyRegionCannotBeMappedAsWritable) {
250   ReadOnlySharedMemoryRegion region = GetInitiallyReadOnlyRegion(kRegionSize);
251   ASSERT_TRUE(region.IsValid());
252 
253   EXPECT_TRUE(CheckReadOnlyPlatformSharedMemoryRegionForTesting(
254       ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
255           std::move(region))));
256 }
257 
TEST_F(ReadOnlySharedMemoryRegionTest,ConvertedToReadOnlyRegionCannotBeMappedAsWritable)258 TEST_F(ReadOnlySharedMemoryRegionTest,
259        ConvertedToReadOnlyRegionCannotBeMappedAsWritable) {
260   ReadOnlySharedMemoryRegion region = GetConvertedToReadOnlyRegion(kRegionSize);
261   ASSERT_TRUE(region.IsValid());
262 
263   EXPECT_TRUE(CheckReadOnlyPlatformSharedMemoryRegionForTesting(
264       ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
265           std::move(region))));
266 }
267 
TEST_F(ReadOnlySharedMemoryRegionTest,InitiallyReadOnlyRegionProducedMappingWriteDeathTest)268 TEST_F(ReadOnlySharedMemoryRegionTest,
269        InitiallyReadOnlyRegionProducedMappingWriteDeathTest) {
270   ReadOnlySharedMemoryRegion region = GetInitiallyReadOnlyRegion(kRegionSize);
271   ASSERT_TRUE(region.IsValid());
272   ReadOnlySharedMemoryMapping mapping = region.Map();
273   ASSERT_TRUE(mapping.IsValid());
274   void* memory_ptr = const_cast<void*>(mapping.memory());
275   EXPECT_DEATH_IF_SUPPORTED(memset(memory_ptr, 'G', kRegionSize), "");
276 }
277 
TEST_F(ReadOnlySharedMemoryRegionTest,ConvertedToReadOnlyRegionProducedMappingWriteDeathTest)278 TEST_F(ReadOnlySharedMemoryRegionTest,
279        ConvertedToReadOnlyRegionProducedMappingWriteDeathTest) {
280   ReadOnlySharedMemoryRegion region = GetConvertedToReadOnlyRegion(kRegionSize);
281   ASSERT_TRUE(region.IsValid());
282   ReadOnlySharedMemoryMapping mapping = region.Map();
283   ASSERT_TRUE(mapping.IsValid());
284   void* memory_ptr = const_cast<void*>(mapping.memory());
285   EXPECT_DEATH_IF_SUPPORTED(memset(memory_ptr, 'G', kRegionSize), "");
286 }
287 
288 }  // namespace base
289