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