| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/shared_memory_handle.h" | 14 #include "base/memory/shared_memory_handle.h" |
| 15 #include "base/process/kill.h" | 15 #include "base/process/kill.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
| 19 #include "base/test/multiprocess_test.h" | 19 #include "base/test/multiprocess_test.h" |
| 20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "base/unguessable_token.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "testing/multiprocess_func_list.h" | 25 #include "testing/multiprocess_func_list.h" |
| 25 | 26 |
| 26 #if defined(OS_POSIX) | 27 #if defined(OS_POSIX) |
| 27 #include <errno.h> | 28 #include <errno.h> |
| 28 #include <fcntl.h> | 29 #include <fcntl.h> |
| 29 #include <sys/mman.h> | 30 #include <sys/mman.h> |
| 30 #include <sys/stat.h> | 31 #include <sys/stat.h> |
| 31 #include <sys/types.h> | 32 #include <sys/types.h> |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 #if defined(OS_MACOSX) && !defined(OS_IOS) | 327 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 327 // The Mach functionality is tested in shared_memory_mac_unittest.cc. | 328 // The Mach functionality is tested in shared_memory_mac_unittest.cc. |
| 328 options.type = SharedMemoryHandle::POSIX; | 329 options.type = SharedMemoryHandle::POSIX; |
| 329 #endif | 330 #endif |
| 330 ASSERT_TRUE(writable_shmem.Create(options)); | 331 ASSERT_TRUE(writable_shmem.Create(options)); |
| 331 ASSERT_TRUE(writable_shmem.Map(options.size)); | 332 ASSERT_TRUE(writable_shmem.Map(options.size)); |
| 332 memcpy(writable_shmem.memory(), contents.data(), contents.size()); | 333 memcpy(writable_shmem.memory(), contents.data(), contents.size()); |
| 333 EXPECT_TRUE(writable_shmem.Unmap()); | 334 EXPECT_TRUE(writable_shmem.Unmap()); |
| 334 | 335 |
| 335 SharedMemoryHandle readonly_handle = writable_shmem.GetReadOnlyHandle(); | 336 SharedMemoryHandle readonly_handle = writable_shmem.GetReadOnlyHandle(); |
| 337 EXPECT_EQ(writable_shmem.handle().GetGUID(), readonly_handle.GetGUID()); |
| 336 ASSERT_TRUE(readonly_handle.IsValid()); | 338 ASSERT_TRUE(readonly_handle.IsValid()); |
| 337 SharedMemory readonly_shmem(readonly_handle, /*readonly=*/true); | 339 SharedMemory readonly_shmem(readonly_handle, /*readonly=*/true); |
| 338 | 340 |
| 339 ASSERT_TRUE(readonly_shmem.Map(contents.size())); | 341 ASSERT_TRUE(readonly_shmem.Map(contents.size())); |
| 340 EXPECT_EQ(contents, | 342 EXPECT_EQ(contents, |
| 341 StringPiece(static_cast<const char*>(readonly_shmem.memory()), | 343 StringPiece(static_cast<const char*>(readonly_shmem.memory()), |
| 342 contents.size())); | 344 contents.size())); |
| 343 EXPECT_TRUE(readonly_shmem.Unmap()); | 345 EXPECT_TRUE(readonly_shmem.Unmap()); |
| 344 | 346 |
| 345 // Make sure the writable instance is still writable. | 347 // Make sure the writable instance is still writable. |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 PAGE_READONLY | SEC_IMAGE, 0, 0, kTestSectionName)); | 600 PAGE_READONLY | SEC_IMAGE, 0, 0, kTestSectionName)); |
| 599 EXPECT_TRUE(section_handle.IsValid()); | 601 EXPECT_TRUE(section_handle.IsValid()); |
| 600 | 602 |
| 601 // Check direct opening by name, from handle and duplicated from handle. | 603 // Check direct opening by name, from handle and duplicated from handle. |
| 602 SharedMemory shared_memory_open; | 604 SharedMemory shared_memory_open; |
| 603 EXPECT_TRUE(shared_memory_open.Open(kTestSectionName, true)); | 605 EXPECT_TRUE(shared_memory_open.Open(kTestSectionName, true)); |
| 604 EXPECT_FALSE(shared_memory_open.Map(1)); | 606 EXPECT_FALSE(shared_memory_open.Map(1)); |
| 605 EXPECT_EQ(nullptr, shared_memory_open.memory()); | 607 EXPECT_EQ(nullptr, shared_memory_open.memory()); |
| 606 | 608 |
| 607 SharedMemory shared_memory_handle_local( | 609 SharedMemory shared_memory_handle_local( |
| 608 SharedMemoryHandle(section_handle.Take()), true); | 610 SharedMemoryHandle(section_handle.Take(), UnguessableToken::Create()), |
| 611 true); |
| 609 EXPECT_FALSE(shared_memory_handle_local.Map(1)); | 612 EXPECT_FALSE(shared_memory_handle_local.Map(1)); |
| 610 EXPECT_EQ(nullptr, shared_memory_handle_local.memory()); | 613 EXPECT_EQ(nullptr, shared_memory_handle_local.memory()); |
| 611 | 614 |
| 612 // Check that a handle without SECTION_QUERY also can't be mapped as it can't | 615 // Check that a handle without SECTION_QUERY also can't be mapped as it can't |
| 613 // be checked. | 616 // be checked. |
| 614 SharedMemory shared_memory_handle_dummy; | 617 SharedMemory shared_memory_handle_dummy; |
| 615 SharedMemoryCreateOptions options; | 618 SharedMemoryCreateOptions options; |
| 616 options.size = 0x1000; | 619 options.size = 0x1000; |
| 617 EXPECT_TRUE(shared_memory_handle_dummy.Create(options)); | 620 EXPECT_TRUE(shared_memory_handle_dummy.Create(options)); |
| 618 HANDLE handle_no_query; | 621 HANDLE handle_no_query; |
| 619 EXPECT_TRUE(::DuplicateHandle( | 622 EXPECT_TRUE(::DuplicateHandle( |
| 620 ::GetCurrentProcess(), shared_memory_handle_dummy.handle().GetHandle(), | 623 ::GetCurrentProcess(), shared_memory_handle_dummy.handle().GetHandle(), |
| 621 ::GetCurrentProcess(), &handle_no_query, FILE_MAP_READ, FALSE, 0)); | 624 ::GetCurrentProcess(), &handle_no_query, FILE_MAP_READ, FALSE, 0)); |
| 622 SharedMemory shared_memory_handle_no_query( | 625 SharedMemory shared_memory_handle_no_query( |
| 623 SharedMemoryHandle(handle_no_query), true); | 626 SharedMemoryHandle(handle_no_query, UnguessableToken::Create()), true); |
| 624 EXPECT_FALSE(shared_memory_handle_no_query.Map(1)); | 627 EXPECT_FALSE(shared_memory_handle_no_query.Map(1)); |
| 625 EXPECT_EQ(nullptr, shared_memory_handle_no_query.memory()); | 628 EXPECT_EQ(nullptr, shared_memory_handle_no_query.memory()); |
| 626 } | 629 } |
| 627 #endif // defined(OS_WIN) | 630 #endif // defined(OS_WIN) |
| 628 | 631 |
| 629 // iOS does not allow multiple processes. | 632 // iOS does not allow multiple processes. |
| 630 // Android ashmem does not support named shared memory. | 633 // Android ashmem does not support named shared memory. |
| 631 // Mac SharedMemory does not support named shared memory. crbug.com/345734 | 634 // Mac SharedMemory does not support named shared memory. crbug.com/345734 |
| 632 #if !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 635 #if !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 633 // On POSIX it is especially important we test shmem across processes, | 636 // On POSIX it is especially important we test shmem across processes, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 memory.Close(); | 703 memory.Close(); |
| 701 SharedMemoryProcessTest::CleanUp(); | 704 SharedMemoryProcessTest::CleanUp(); |
| 702 } | 705 } |
| 703 | 706 |
| 704 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 707 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
| 705 return SharedMemoryProcessTest::TaskTestMain(); | 708 return SharedMemoryProcessTest::TaskTestMain(); |
| 706 } | 709 } |
| 707 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 710 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 708 | 711 |
| 709 } // namespace base | 712 } // namespace base |
| OLD | NEW |