shared_memory_region_unittest.cc 10 KB

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