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> |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 else | 309 else |
310 EXPECT_EQ(0, pointers[j][0]); | 310 EXPECT_EQ(0, pointers[j][0]); |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
314 for (int i = 0; i < count; i++) { | 314 for (int i = 0; i < count; i++) { |
315 memories[i].Close(); | 315 memories[i].Close(); |
316 } | 316 } |
317 } | 317 } |
318 | 318 |
319 // The Mach functionality is tested in shared_memory_mac_unittest.cc. | |
320 #if !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
321 TEST(SharedMemoryTest, ShareReadOnly) { | 319 TEST(SharedMemoryTest, ShareReadOnly) { |
322 StringPiece contents = "Hello World"; | 320 StringPiece contents = "Hello World"; |
323 | 321 |
324 SharedMemory writable_shmem; | 322 SharedMemory writable_shmem; |
325 SharedMemoryCreateOptions options; | 323 SharedMemoryCreateOptions options; |
326 options.size = contents.size(); | 324 options.size = contents.size(); |
327 options.share_read_only = true; | 325 options.share_read_only = true; |
| 326 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 327 // The Mach functionality is tested in shared_memory_mac_unittest.cc. |
| 328 options.type = SharedMemoryHandle::POSIX; |
| 329 #endif |
328 ASSERT_TRUE(writable_shmem.Create(options)); | 330 ASSERT_TRUE(writable_shmem.Create(options)); |
329 ASSERT_TRUE(writable_shmem.Map(options.size)); | 331 ASSERT_TRUE(writable_shmem.Map(options.size)); |
330 memcpy(writable_shmem.memory(), contents.data(), contents.size()); | 332 memcpy(writable_shmem.memory(), contents.data(), contents.size()); |
331 EXPECT_TRUE(writable_shmem.Unmap()); | 333 EXPECT_TRUE(writable_shmem.Unmap()); |
332 | 334 |
333 SharedMemoryHandle readonly_handle; | 335 SharedMemoryHandle readonly_handle; |
334 ASSERT_TRUE(writable_shmem.ShareReadOnlyToProcess(GetCurrentProcessHandle(), | 336 ASSERT_TRUE(writable_shmem.ShareReadOnlyToProcess(GetCurrentProcessHandle(), |
335 &readonly_handle)); | 337 &readonly_handle)); |
336 SharedMemory readonly_shmem(readonly_handle, /*readonly=*/true); | 338 SharedMemory readonly_shmem(readonly_handle, /*readonly=*/true); |
337 | 339 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 GetCurrentProcess(), &temp_handle, FILE_MAP_READ, | 395 GetCurrentProcess(), &temp_handle, FILE_MAP_READ, |
394 false, 0); | 396 false, 0); |
395 EXPECT_EQ(TRUE, rv) | 397 EXPECT_EQ(TRUE, rv) |
396 << "Should be able to duplicate the handle into a readable one."; | 398 << "Should be able to duplicate the handle into a readable one."; |
397 if (rv) | 399 if (rv) |
398 win::ScopedHandle writable_handle(temp_handle); | 400 win::ScopedHandle writable_handle(temp_handle); |
399 #else | 401 #else |
400 #error Unexpected platform; write a test that tries to make 'handle' writable. | 402 #error Unexpected platform; write a test that tries to make 'handle' writable. |
401 #endif // defined(OS_POSIX) || defined(OS_WIN) | 403 #endif // defined(OS_POSIX) || defined(OS_WIN) |
402 } | 404 } |
403 #endif // !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
404 | 405 |
405 TEST(SharedMemoryTest, ShareToSelf) { | 406 TEST(SharedMemoryTest, ShareToSelf) { |
406 StringPiece contents = "Hello World"; | 407 StringPiece contents = "Hello World"; |
407 | 408 |
408 SharedMemory shmem; | 409 SharedMemory shmem; |
409 ASSERT_TRUE(shmem.CreateAndMapAnonymous(contents.size())); | 410 ASSERT_TRUE(shmem.CreateAndMapAnonymous(contents.size())); |
410 memcpy(shmem.memory(), contents.data(), contents.size()); | 411 memcpy(shmem.memory(), contents.data(), contents.size()); |
411 EXPECT_TRUE(shmem.Unmap()); | 412 EXPECT_TRUE(shmem.Unmap()); |
412 | 413 |
413 SharedMemoryHandle shared_handle; | 414 SharedMemoryHandle shared_handle; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 bool rv = memory.CreateAndMapAnonymous(kDataSize); | 468 bool rv = memory.CreateAndMapAnonymous(kDataSize); |
468 EXPECT_TRUE(rv); | 469 EXPECT_TRUE(rv); |
469 | 470 |
470 void* old_address = memory.memory(); | 471 void* old_address = memory.memory(); |
471 | 472 |
472 rv = memory.Map(kDataSize); | 473 rv = memory.Map(kDataSize); |
473 EXPECT_FALSE(rv); | 474 EXPECT_FALSE(rv); |
474 EXPECT_EQ(old_address, memory.memory()); | 475 EXPECT_EQ(old_address, memory.memory()); |
475 } | 476 } |
476 | 477 |
477 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | 478 #if defined(OS_POSIX) |
478 // This test is not applicable for iOS (crbug.com/399384). | 479 // This test is not applicable for iOS (crbug.com/399384). |
479 #if !defined(OS_IOS) | 480 #if !defined(OS_IOS) |
480 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. | 481 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. |
481 TEST(SharedMemoryTest, AnonymousExecutable) { | 482 TEST(SharedMemoryTest, AnonymousExecutable) { |
482 const uint32_t kTestSize = 1 << 16; | 483 const uint32_t kTestSize = 1 << 16; |
483 | 484 |
484 SharedMemory shared_memory; | 485 SharedMemory shared_memory; |
485 SharedMemoryCreateOptions options; | 486 SharedMemoryCreateOptions options; |
486 options.size = kTestSize; | 487 options.size = kTestSize; |
487 options.executable = true; | 488 options.executable = true; |
| 489 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 490 // The Mach functionality is tested in shared_memory_mac_unittest.cc. |
| 491 options.type = SharedMemoryHandle::POSIX; |
| 492 #endif |
488 | 493 |
489 EXPECT_TRUE(shared_memory.Create(options)); | 494 EXPECT_TRUE(shared_memory.Create(options)); |
490 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size())); | 495 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size())); |
491 | 496 |
492 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), | 497 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), |
493 PROT_READ | PROT_EXEC)); | 498 PROT_READ | PROT_EXEC)); |
494 } | 499 } |
495 #endif // !defined(OS_IOS) | 500 #endif // !defined(OS_IOS) |
496 | 501 |
497 // Android supports a different permission model than POSIX for its "ashmem" | 502 // Android supports a different permission model than POSIX for its "ashmem" |
(...skipping 13 matching lines...) Expand all Loading... |
511 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); | 516 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
512 }; | 517 }; |
513 | 518 |
514 // Create a shared memory object, check its permissions. | 519 // Create a shared memory object, check its permissions. |
515 TEST(SharedMemoryTest, FilePermissionsAnonymous) { | 520 TEST(SharedMemoryTest, FilePermissionsAnonymous) { |
516 const uint32_t kTestSize = 1 << 8; | 521 const uint32_t kTestSize = 1 << 8; |
517 | 522 |
518 SharedMemory shared_memory; | 523 SharedMemory shared_memory; |
519 SharedMemoryCreateOptions options; | 524 SharedMemoryCreateOptions options; |
520 options.size = kTestSize; | 525 options.size = kTestSize; |
| 526 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 527 // The Mach functionality is tested in shared_memory_mac_unittest.cc. |
| 528 options.type = SharedMemoryHandle::POSIX; |
| 529 #endif |
521 // Set a file mode creation mask that gives all permissions. | 530 // Set a file mode creation mask that gives all permissions. |
522 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); | 531 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); |
523 | 532 |
524 EXPECT_TRUE(shared_memory.Create(options)); | 533 EXPECT_TRUE(shared_memory.Create(options)); |
525 | 534 |
526 int shm_fd = | 535 int shm_fd = |
527 SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); | 536 SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); |
528 struct stat shm_stat; | 537 struct stat shm_stat; |
529 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | 538 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); |
530 // Neither the group, nor others should be able to read the shared memory | 539 // Neither the group, nor others should be able to read the shared memory |
531 // file. | 540 // file. |
532 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | 541 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
533 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | 542 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
534 } | 543 } |
535 | 544 |
536 // Create a shared memory object, check its permissions. | 545 // Create a shared memory object, check its permissions. |
537 TEST(SharedMemoryTest, FilePermissionsNamed) { | 546 TEST(SharedMemoryTest, FilePermissionsNamed) { |
538 const uint32_t kTestSize = 1 << 8; | 547 const uint32_t kTestSize = 1 << 8; |
539 | 548 |
540 SharedMemory shared_memory; | 549 SharedMemory shared_memory; |
541 SharedMemoryCreateOptions options; | 550 SharedMemoryCreateOptions options; |
542 options.size = kTestSize; | 551 options.size = kTestSize; |
| 552 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 553 // The Mach functionality is tested in shared_memory_mac_unittest.cc. |
| 554 options.type = SharedMemoryHandle::POSIX; |
| 555 #endif |
543 | 556 |
544 // Set a file mode creation mask that gives all permissions. | 557 // Set a file mode creation mask that gives all permissions. |
545 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); | 558 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); |
546 | 559 |
547 EXPECT_TRUE(shared_memory.Create(options)); | 560 EXPECT_TRUE(shared_memory.Create(options)); |
548 | 561 |
549 int fd = SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); | 562 int fd = SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); |
550 struct stat shm_stat; | 563 struct stat shm_stat; |
551 EXPECT_EQ(0, fstat(fd, &shm_stat)); | 564 EXPECT_EQ(0, fstat(fd, &shm_stat)); |
552 // Neither the group, nor others should have been able to open the shared | 565 // Neither the group, nor others should have been able to open the shared |
553 // memory file while its name existed. | 566 // memory file while its name existed. |
554 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | 567 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
555 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | 568 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
556 } | 569 } |
557 #endif // !defined(OS_ANDROID) | 570 #endif // !defined(OS_ANDROID) |
558 | 571 |
559 #endif // defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | 572 #endif // defined(OS_POSIX) |
560 | 573 |
561 // Map() will return addresses which are aligned to the platform page size, this | 574 // Map() will return addresses which are aligned to the platform page size, this |
562 // varies from platform to platform though. Since we'd like to advertise a | 575 // varies from platform to platform though. Since we'd like to advertise a |
563 // minimum alignment that callers can count on, test for it here. | 576 // minimum alignment that callers can count on, test for it here. |
564 TEST(SharedMemoryTest, MapMinimumAlignment) { | 577 TEST(SharedMemoryTest, MapMinimumAlignment) { |
565 static const int kDataSize = 8192; | 578 static const int kDataSize = 8192; |
566 | 579 |
567 SharedMemory shared_memory; | 580 SharedMemory shared_memory; |
568 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize)); | 581 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize)); |
569 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>( | 582 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 memory.Close(); | 701 memory.Close(); |
689 SharedMemoryProcessTest::CleanUp(); | 702 SharedMemoryProcessTest::CleanUp(); |
690 } | 703 } |
691 | 704 |
692 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 705 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
693 return SharedMemoryProcessTest::TaskTestMain(); | 706 return SharedMemoryProcessTest::TaskTestMain(); |
694 } | 707 } |
695 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 708 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
696 | 709 |
697 } // namespace base | 710 } // namespace base |
OLD | NEW |