OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <mach/mach.h> | 5 #include <mach/mach.h> |
6 #include <mach/mach_vm.h> | 6 #include <mach/mach_vm.h> |
7 #include <servers/bootstrap.h> | 7 #include <servers/bootstrap.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 SharedMemory shared_memory2(shm2, true); | 387 SharedMemory shared_memory2(shm2, true); |
388 shared_memory2.Map(s_memory_size); | 388 shared_memory2.Map(s_memory_size); |
389 | 389 |
390 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), | 390 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), |
391 s_memory_size)); | 391 s_memory_size)); |
392 } | 392 } |
393 | 393 |
394 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 394 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
395 } | 395 } |
396 | 396 |
397 // Tests that the method ShareReadOnlyToProcess() creates a memory object that | 397 // Tests that the method GetReadOnlyHandle() creates a memory object that |
398 // is read only. | 398 // is read only. |
399 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { | 399 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { |
400 std::unique_ptr<SharedMemory> shared_memory( | 400 std::unique_ptr<SharedMemory> shared_memory( |
401 CreateSharedMemory(s_memory_size)); | 401 CreateSharedMemory(s_memory_size)); |
402 | 402 |
403 // Check the protection levels. | 403 // Check the protection levels. |
404 int current_prot, max_prot; | 404 int current_prot, max_prot; |
405 ASSERT_TRUE(GetProtections(shared_memory->memory(), | 405 ASSERT_TRUE(GetProtections(shared_memory->memory(), |
406 shared_memory->mapped_size(), ¤t_prot, | 406 shared_memory->mapped_size(), ¤t_prot, |
407 &max_prot)); | 407 &max_prot)); |
408 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); | 408 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); |
409 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); | 409 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); |
410 | 410 |
411 // Make a new memory object. | 411 // Make a new memory object. |
412 SharedMemoryHandle shm2; | 412 SharedMemoryHandle shm2 = shared_memory->GetReadOnlyHandle(); |
413 ASSERT_TRUE(shared_memory->ShareReadOnlyToProcess(GetCurrentProcId(), &shm2)); | |
414 ASSERT_TRUE(shm2.IsValid()); | 413 ASSERT_TRUE(shm2.IsValid()); |
415 | 414 |
416 // Mapping with |readonly| set to |false| should fail. | 415 // Mapping with |readonly| set to |false| should fail. |
417 SharedMemory shared_memory2(shm2, false); | 416 SharedMemory shared_memory2(shm2, false); |
418 shared_memory2.Map(s_memory_size); | 417 shared_memory2.Map(s_memory_size); |
419 ASSERT_EQ(nullptr, shared_memory2.memory()); | 418 ASSERT_EQ(nullptr, shared_memory2.memory()); |
420 | 419 |
421 // Now trying mapping with |readonly| set to |true|. | 420 // Now trying mapping with |readonly| set to |true|. |
422 SharedMemory shared_memory3(shm2.Duplicate(), true); | 421 SharedMemory shared_memory3(shm2.Duplicate(), true); |
423 shared_memory3.Map(s_memory_size); | 422 shared_memory3.Map(s_memory_size); |
424 ASSERT_NE(nullptr, shared_memory3.memory()); | 423 ASSERT_NE(nullptr, shared_memory3.memory()); |
425 | 424 |
426 // Check the protection levels. | 425 // Check the protection levels. |
427 ASSERT_TRUE(GetProtections(shared_memory3.memory(), | 426 ASSERT_TRUE(GetProtections(shared_memory3.memory(), |
428 shared_memory3.mapped_size(), ¤t_prot, | 427 shared_memory3.mapped_size(), ¤t_prot, |
429 &max_prot)); | 428 &max_prot)); |
430 ASSERT_EQ(VM_PROT_READ, current_prot); | 429 ASSERT_EQ(VM_PROT_READ, current_prot); |
431 ASSERT_EQ(VM_PROT_READ, max_prot); | 430 ASSERT_EQ(VM_PROT_READ, max_prot); |
432 | 431 |
433 // The memory should still be readonly, since the underlying memory object | 432 // The memory should still be readonly, since the underlying memory object |
434 // is readonly. | 433 // is readonly. |
435 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); | 434 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); |
436 } | 435 } |
437 | 436 |
438 // Tests that the method ShareReadOnlyToProcess() doesn't leak. | 437 // Tests that the method GetReadOnlyHandle() doesn't leak. |
439 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { | 438 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { |
440 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 439 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
441 | 440 |
442 { | 441 { |
443 std::unique_ptr<SharedMemory> shared_memory( | 442 std::unique_ptr<SharedMemory> shared_memory( |
444 CreateSharedMemory(s_memory_size)); | 443 CreateSharedMemory(s_memory_size)); |
445 | 444 |
446 SharedMemoryHandle shm2; | 445 SharedMemoryHandle shm2 = shared_memory->GetReadOnlyHandle(); |
447 ASSERT_TRUE( | |
448 shared_memory->ShareReadOnlyToProcess(GetCurrentProcId(), &shm2)); | |
449 ASSERT_TRUE(shm2.IsValid()); | 446 ASSERT_TRUE(shm2.IsValid()); |
450 | 447 |
451 // Intentionally map with |readonly| set to |false|. | 448 // Intentionally map with |readonly| set to |false|. |
452 SharedMemory shared_memory2(shm2, false); | 449 SharedMemory shared_memory2(shm2, false); |
453 shared_memory2.Map(s_memory_size); | 450 shared_memory2.Map(s_memory_size); |
454 } | 451 } |
455 | 452 |
456 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 453 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
457 } | 454 } |
458 | 455 |
459 } // namespace base | 456 } // namespace base |
OLD | NEW |