| 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 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 PAGE_READONLY | SEC_IMAGE, 0, 0, kTestSectionName)); | 599 PAGE_READONLY | SEC_IMAGE, 0, 0, kTestSectionName)); |
| 599 EXPECT_TRUE(section_handle.IsValid()); | 600 EXPECT_TRUE(section_handle.IsValid()); |
| 600 | 601 |
| 601 // Check direct opening by name, from handle and duplicated from handle. | 602 // Check direct opening by name, from handle and duplicated from handle. |
| 602 SharedMemory shared_memory_open; | 603 SharedMemory shared_memory_open; |
| 603 EXPECT_TRUE(shared_memory_open.Open(kTestSectionName, true)); | 604 EXPECT_TRUE(shared_memory_open.Open(kTestSectionName, true)); |
| 604 EXPECT_FALSE(shared_memory_open.Map(1)); | 605 EXPECT_FALSE(shared_memory_open.Map(1)); |
| 605 EXPECT_EQ(nullptr, shared_memory_open.memory()); | 606 EXPECT_EQ(nullptr, shared_memory_open.memory()); |
| 606 | 607 |
| 607 SharedMemory shared_memory_handle_local( | 608 SharedMemory shared_memory_handle_local( |
| 608 SharedMemoryHandle(section_handle.Take()), true); | 609 SharedMemoryHandle(section_handle.Take(), UnguessableToken::Create()), |
| 610 true); |
| 609 EXPECT_FALSE(shared_memory_handle_local.Map(1)); | 611 EXPECT_FALSE(shared_memory_handle_local.Map(1)); |
| 610 EXPECT_EQ(nullptr, shared_memory_handle_local.memory()); | 612 EXPECT_EQ(nullptr, shared_memory_handle_local.memory()); |
| 611 | 613 |
| 612 // Check that a handle without SECTION_QUERY also can't be mapped as it can't | 614 // Check that a handle without SECTION_QUERY also can't be mapped as it can't |
| 613 // be checked. | 615 // be checked. |
| 614 SharedMemory shared_memory_handle_dummy; | 616 SharedMemory shared_memory_handle_dummy; |
| 615 SharedMemoryCreateOptions options; | 617 SharedMemoryCreateOptions options; |
| 616 options.size = 0x1000; | 618 options.size = 0x1000; |
| 617 EXPECT_TRUE(shared_memory_handle_dummy.Create(options)); | 619 EXPECT_TRUE(shared_memory_handle_dummy.Create(options)); |
| 618 HANDLE handle_no_query; | 620 HANDLE handle_no_query; |
| 619 EXPECT_TRUE(::DuplicateHandle( | 621 EXPECT_TRUE(::DuplicateHandle( |
| 620 ::GetCurrentProcess(), shared_memory_handle_dummy.handle().GetHandle(), | 622 ::GetCurrentProcess(), shared_memory_handle_dummy.handle().GetHandle(), |
| 621 ::GetCurrentProcess(), &handle_no_query, FILE_MAP_READ, FALSE, 0)); | 623 ::GetCurrentProcess(), &handle_no_query, FILE_MAP_READ, FALSE, 0)); |
| 622 SharedMemory shared_memory_handle_no_query( | 624 SharedMemory shared_memory_handle_no_query( |
| 623 SharedMemoryHandle(handle_no_query), true); | 625 SharedMemoryHandle(handle_no_query, UnguessableToken::Create()), true); |
| 624 EXPECT_FALSE(shared_memory_handle_no_query.Map(1)); | 626 EXPECT_FALSE(shared_memory_handle_no_query.Map(1)); |
| 625 EXPECT_EQ(nullptr, shared_memory_handle_no_query.memory()); | 627 EXPECT_EQ(nullptr, shared_memory_handle_no_query.memory()); |
| 626 } | 628 } |
| 627 #endif // defined(OS_WIN) | 629 #endif // defined(OS_WIN) |
| 628 | 630 |
| 629 // iOS does not allow multiple processes. | 631 // iOS does not allow multiple processes. |
| 630 // Android ashmem does not support named shared memory. | 632 // Android ashmem does not support named shared memory. |
| 631 // Mac SharedMemory does not support named shared memory. crbug.com/345734 | 633 // Mac SharedMemory does not support named shared memory. crbug.com/345734 |
| 632 #if !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 634 #if !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 633 // On POSIX it is especially important we test shmem across processes, | 635 // 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(); | 702 memory.Close(); |
| 701 SharedMemoryProcessTest::CleanUp(); | 703 SharedMemoryProcessTest::CleanUp(); |
| 702 } | 704 } |
| 703 | 705 |
| 704 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 706 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
| 705 return SharedMemoryProcessTest::TaskTestMain(); | 707 return SharedMemoryProcessTest::TaskTestMain(); |
| 706 } | 708 } |
| 707 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 709 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 708 | 710 |
| 709 } // namespace base | 711 } // namespace base |
| OLD | NEW |