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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/sys_info.h" | 11 #include "base/sys_info.h" |
12 #include "base/test/multiprocess_test.h" | 12 #include "base/test/multiprocess_test.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/multiprocess_func_list.h" | 16 #include "testing/multiprocess_func_list.h" |
17 | 17 |
18 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
19 #include "base/mac/scoped_nsautorelease_pool.h" | 19 #include "base/mac/scoped_nsautorelease_pool.h" |
20 #endif | 20 #endif |
21 | 21 |
22 #if defined(OS_POSIX) | 22 #if defined(OS_POSIX) |
| 23 #include <errno.h> |
| 24 #include <fcntl.h> |
23 #include <sys/mman.h> | 25 #include <sys/mman.h> |
24 #include <sys/stat.h> | 26 #include <sys/stat.h> |
25 #include <sys/types.h> | 27 #include <sys/types.h> |
26 #include <unistd.h> | 28 #include <unistd.h> |
27 #endif | 29 #endif |
28 | 30 |
| 31 #if defined(OS_WIN) |
| 32 #include "base/win/scoped_handle.h" |
| 33 #endif |
| 34 |
29 static const int kNumThreads = 5; | 35 static const int kNumThreads = 5; |
30 static const int kNumTasks = 5; | 36 static const int kNumTasks = 5; |
31 | 37 |
32 namespace base { | 38 namespace base { |
33 | 39 |
34 namespace { | 40 namespace { |
35 | 41 |
36 // Each thread will open the shared memory. Each thread will take a different 4 | 42 // Each thread will open the shared memory. Each thread will take a different 4 |
37 // byte int pointer, and keep changing it, with some small pauses in between. | 43 // byte int pointer, and keep changing it, with some small pauses in between. |
38 // Verify that each thread's value in the shared memory is always correct. | 44 // Verify that each thread's value in the shared memory is always correct. |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 else | 360 else |
355 EXPECT_EQ(0, pointers[j][0]); | 361 EXPECT_EQ(0, pointers[j][0]); |
356 } | 362 } |
357 } | 363 } |
358 | 364 |
359 for (int i = 0; i < count; i++) { | 365 for (int i = 0; i < count; i++) { |
360 memories[i].Close(); | 366 memories[i].Close(); |
361 } | 367 } |
362 } | 368 } |
363 | 369 |
| 370 TEST(SharedMemoryTest, ShareReadOnly) { |
| 371 StringPiece contents = "Hello World"; |
| 372 |
| 373 SharedMemory writable_shmem; |
| 374 ASSERT_TRUE(writable_shmem.CreateAndMapAnonymous(contents.size())); |
| 375 memcpy(writable_shmem.memory(), contents.data(), contents.size()); |
| 376 EXPECT_TRUE(writable_shmem.Unmap()); |
| 377 |
| 378 SharedMemoryHandle readonly_handle; |
| 379 ASSERT_TRUE(writable_shmem.ShareReadOnlyToProcess(GetCurrentProcessHandle(), |
| 380 &readonly_handle)); |
| 381 SharedMemory readonly_shmem(readonly_handle, /*readonly=*/true); |
| 382 |
| 383 ASSERT_TRUE(readonly_shmem.Map(contents.size())); |
| 384 EXPECT_EQ(contents, |
| 385 StringPiece(static_cast<const char*>(readonly_shmem.memory()), |
| 386 contents.size())); |
| 387 EXPECT_TRUE(readonly_shmem.Unmap()); |
| 388 |
| 389 // Make sure the writable instance is still writable. |
| 390 ASSERT_TRUE(writable_shmem.Map(contents.size())); |
| 391 StringPiece new_contents = "Goodbye"; |
| 392 memcpy(writable_shmem.memory(), new_contents.data(), new_contents.size()); |
| 393 EXPECT_EQ(new_contents, |
| 394 StringPiece(static_cast<const char*>(writable_shmem.memory()), |
| 395 new_contents.size())); |
| 396 |
| 397 // We'd like to check that if we send the read-only segment to another |
| 398 // process, then that other process can't reopen it read/write. (Since that |
| 399 // would be a security hole.) Setting up multiple processes is hard in a |
| 400 // unittest, so this test checks that the *current* process can't reopen the |
| 401 // segment read/write. I think the test here is stronger than we actually |
| 402 // care about, but there's a remote possibility that sending a file over a |
| 403 // pipe would transform it into read/write. |
| 404 SharedMemoryHandle handle = readonly_shmem.handle(); |
| 405 |
| 406 #if defined(OS_ANDROID) |
| 407 // The "read-only" handle is still writable on Android: |
| 408 // http://crbug.com/320865 |
| 409 (void)handle; |
| 410 #elif defined(OS_POSIX) |
| 411 EXPECT_EQ(O_RDONLY, fcntl(handle.fd, F_GETFL) & O_ACCMODE) |
| 412 << "The descriptor itself should be read-only."; |
| 413 |
| 414 errno = 0; |
| 415 void* writable = mmap( |
| 416 NULL, contents.size(), PROT_READ | PROT_WRITE, MAP_SHARED, handle.fd, 0); |
| 417 int mmap_errno = errno; |
| 418 EXPECT_EQ(MAP_FAILED, writable) |
| 419 << "It shouldn't be possible to re-mmap the descriptor writable."; |
| 420 EXPECT_EQ(EACCES, mmap_errno) << strerror(mmap_errno); |
| 421 if (writable != MAP_FAILED) |
| 422 EXPECT_EQ(0, munmap(writable, readonly_shmem.mapped_size())); |
| 423 |
| 424 #elif defined(OS_WIN) |
| 425 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0)) |
| 426 << "Shouldn't be able to map memory writable."; |
| 427 |
| 428 base::win::ScopedHandle writable_handle; |
| 429 EXPECT_EQ(0, |
| 430 ::DuplicateHandle(GetCurrentProcess(), |
| 431 handle, |
| 432 GetCurrentProcess, |
| 433 writable_handle.Receive(), |
| 434 FILE_MAP_ALL_ACCESS, |
| 435 false, |
| 436 0)) |
| 437 << "Shouldn't be able to duplicate the handle into a writable one."; |
| 438 #else |
| 439 #error Unexpected platform; write a test that tries to make 'handle' writable. |
| 440 #endif // defined(OS_POSIX) || defined(OS_WIN) |
| 441 } |
| 442 |
364 TEST(SharedMemoryTest, ShareToSelf) { | 443 TEST(SharedMemoryTest, ShareToSelf) { |
365 StringPiece contents = "Hello World"; | 444 StringPiece contents = "Hello World"; |
366 | 445 |
367 SharedMemory shmem; | 446 SharedMemory shmem; |
368 ASSERT_TRUE(shmem.CreateAndMapAnonymous(contents.size())); | 447 ASSERT_TRUE(shmem.CreateAndMapAnonymous(contents.size())); |
369 memcpy(shmem.memory(), contents.data(), contents.size()); | 448 memcpy(shmem.memory(), contents.data(), contents.size()); |
370 EXPECT_TRUE(shmem.Unmap()); | 449 EXPECT_TRUE(shmem.Unmap()); |
371 | 450 |
372 SharedMemoryHandle shared_handle; | 451 SharedMemoryHandle shared_handle; |
373 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); | 452 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); |
374 SharedMemory shared(shared_handle, /*readonly=*/false); | 453 SharedMemory shared(shared_handle, /*readonly=*/false); |
375 | 454 |
376 ASSERT_TRUE(shared.Map(contents.size())); | 455 ASSERT_TRUE(shared.Map(contents.size())); |
377 EXPECT_EQ( | 456 EXPECT_EQ( |
378 contents, | 457 contents, |
379 StringPiece(static_cast<const char*>(shared.memory()), contents.size())); | 458 StringPiece(static_cast<const char*>(shared.memory()), contents.size())); |
| 459 |
| 460 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); |
| 461 SharedMemory readonly(shared_handle, /*readonly=*/true); |
| 462 |
| 463 ASSERT_TRUE(readonly.Map(contents.size())); |
| 464 EXPECT_EQ(contents, |
| 465 StringPiece(static_cast<const char*>(readonly.memory()), |
| 466 contents.size())); |
380 } | 467 } |
381 | 468 |
382 TEST(SharedMemoryTest, MapAt) { | 469 TEST(SharedMemoryTest, MapAt) { |
383 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); | 470 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); |
384 const size_t kCount = SysInfo::VMAllocationGranularity(); | 471 const size_t kCount = SysInfo::VMAllocationGranularity(); |
385 const size_t kDataSize = kCount * sizeof(uint32); | 472 const size_t kDataSize = kCount * sizeof(uint32); |
386 | 473 |
387 SharedMemory memory; | 474 SharedMemory memory; |
388 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); | 475 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); |
389 ASSERT_TRUE(memory.Map(kDataSize)); | 476 ASSERT_TRUE(memory.Map(kDataSize)); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 SharedMemoryProcessTest::CleanUp(); | 657 SharedMemoryProcessTest::CleanUp(); |
571 } | 658 } |
572 | 659 |
573 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 660 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
574 return SharedMemoryProcessTest::TaskTestMain(); | 661 return SharedMemoryProcessTest::TaskTestMain(); |
575 } | 662 } |
576 | 663 |
577 #endif // !OS_IOS | 664 #endif // !OS_IOS |
578 | 665 |
579 } // namespace base | 666 } // namespace base |
OLD | NEW |