1 // Copyright 2018 The Chromium Authors
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 auto [region, rw_mapping] = CreateMappedRegion<TypeParam>(kDataSize);
161 ASSERT_TRUE(region.IsValid());
162 ASSERT_TRUE(rw_mapping.IsValid());
163 uint32_t* ptr = static_cast<uint32_t*>(rw_mapping.memory());
164
165 for (size_t i = 0; i < kCount; ++i)
166 ptr[i] = i;
167
168 rw_mapping = WritableSharedMemoryMapping();
169
170 for (size_t bytes_offset = sizeof(uint32_t); bytes_offset <= kPageSize;
171 bytes_offset += sizeof(uint32_t)) {
172 typename TypeParam::MappingType mapping =
173 region.MapAt(bytes_offset, kDataSize - bytes_offset);
174 ASSERT_TRUE(mapping.IsValid());
175
176 size_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 }
183
TYPED_TEST(SharedMemoryRegionTest,MapZeroBytesFails)184 TYPED_TEST(SharedMemoryRegionTest, MapZeroBytesFails) {
185 typename TypeParam::MappingType mapping = this->region_.MapAt(0, 0);
186 EXPECT_FALSE(mapping.IsValid());
187 }
188
TYPED_TEST(SharedMemoryRegionTest,MapMoreBytesThanRegionSizeFails)189 TYPED_TEST(SharedMemoryRegionTest, MapMoreBytesThanRegionSizeFails) {
190 size_t region_real_size = this->region_.GetSize();
191 typename TypeParam::MappingType mapping =
192 this->region_.MapAt(0, region_real_size + 1);
193 EXPECT_FALSE(mapping.IsValid());
194 }
195
196 template <typename DuplicatableSharedMemoryRegion>
197 class DuplicatableSharedMemoryRegionTest
198 : public SharedMemoryRegionTest<DuplicatableSharedMemoryRegion> {};
199
200 typedef ::testing::Types<UnsafeSharedMemoryRegion, ReadOnlySharedMemoryRegion>
201 DuplicatableRegionTypes;
202 TYPED_TEST_SUITE(DuplicatableSharedMemoryRegionTest, DuplicatableRegionTypes);
203
TYPED_TEST(DuplicatableSharedMemoryRegionTest,Duplicate)204 TYPED_TEST(DuplicatableSharedMemoryRegionTest, Duplicate) {
205 TypeParam dup_region = this->region_.Duplicate();
206 EXPECT_EQ(this->region_.GetGUID(), dup_region.GetGUID());
207 typename TypeParam::MappingType mapping = dup_region.Map();
208 ASSERT_TRUE(mapping.IsValid());
209 EXPECT_NE(this->rw_mapping_.memory(), mapping.memory());
210 EXPECT_EQ(this->rw_mapping_.guid(), mapping.guid());
211 EXPECT_TRUE(IsMemoryFilledWithByte(mapping.memory(), kRegionSize, 'G'));
212 }
213
214 class ReadOnlySharedMemoryRegionTest : public ::testing::Test {
215 public:
GetInitiallyReadOnlyRegion(size_t size)216 ReadOnlySharedMemoryRegion GetInitiallyReadOnlyRegion(size_t size) {
217 MappedReadOnlyRegion mapped_region =
218 ReadOnlySharedMemoryRegion::Create(size);
219 ReadOnlySharedMemoryRegion region = std::move(mapped_region.region);
220 return region;
221 }
222
GetConvertedToReadOnlyRegion(size_t size)223 ReadOnlySharedMemoryRegion GetConvertedToReadOnlyRegion(size_t size) {
224 WritableSharedMemoryRegion region =
225 WritableSharedMemoryRegion::Create(kRegionSize);
226 ReadOnlySharedMemoryRegion ro_region =
227 WritableSharedMemoryRegion::ConvertToReadOnly(std::move(region));
228 return ro_region;
229 }
230 };
231
TEST_F(ReadOnlySharedMemoryRegionTest,InitiallyReadOnlyRegionCannotBeMappedAsWritable)232 TEST_F(ReadOnlySharedMemoryRegionTest,
233 InitiallyReadOnlyRegionCannotBeMappedAsWritable) {
234 ReadOnlySharedMemoryRegion region = GetInitiallyReadOnlyRegion(kRegionSize);
235 ASSERT_TRUE(region.IsValid());
236
237 EXPECT_TRUE(CheckReadOnlyPlatformSharedMemoryRegionForTesting(
238 ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
239 std::move(region))));
240 }
241
TEST_F(ReadOnlySharedMemoryRegionTest,ConvertedToReadOnlyRegionCannotBeMappedAsWritable)242 TEST_F(ReadOnlySharedMemoryRegionTest,
243 ConvertedToReadOnlyRegionCannotBeMappedAsWritable) {
244 ReadOnlySharedMemoryRegion region = GetConvertedToReadOnlyRegion(kRegionSize);
245 ASSERT_TRUE(region.IsValid());
246
247 EXPECT_TRUE(CheckReadOnlyPlatformSharedMemoryRegionForTesting(
248 ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
249 std::move(region))));
250 }
251
TEST_F(ReadOnlySharedMemoryRegionTest,InitiallyReadOnlyRegionProducedMappingWriteDeathTest)252 TEST_F(ReadOnlySharedMemoryRegionTest,
253 InitiallyReadOnlyRegionProducedMappingWriteDeathTest) {
254 ReadOnlySharedMemoryRegion region = GetInitiallyReadOnlyRegion(kRegionSize);
255 ASSERT_TRUE(region.IsValid());
256 ReadOnlySharedMemoryMapping mapping = region.Map();
257 ASSERT_TRUE(mapping.IsValid());
258 void* memory_ptr = const_cast<void*>(mapping.memory());
259 EXPECT_DEATH_IF_SUPPORTED(memset(memory_ptr, 'G', kRegionSize), "");
260 }
261
TEST_F(ReadOnlySharedMemoryRegionTest,ConvertedToReadOnlyRegionProducedMappingWriteDeathTest)262 TEST_F(ReadOnlySharedMemoryRegionTest,
263 ConvertedToReadOnlyRegionProducedMappingWriteDeathTest) {
264 ReadOnlySharedMemoryRegion region = GetConvertedToReadOnlyRegion(kRegionSize);
265 ASSERT_TRUE(region.IsValid());
266 ReadOnlySharedMemoryMapping mapping = region.Map();
267 ASSERT_TRUE(mapping.IsValid());
268 void* memory_ptr = const_cast<void*>(mapping.memory());
269 EXPECT_DEATH_IF_SUPPORTED(memset(memory_ptr, 'G', kRegionSize), "");
270 }
271
272 } // namespace base
273