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 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 std::unique_ptr<SharedMemory> shared_memory( | 366 std::unique_ptr<SharedMemory> shared_memory( |
367 CreateSharedMemory(s_memory_size)); | 367 CreateSharedMemory(s_memory_size)); |
368 | 368 |
369 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); | 369 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); |
370 ASSERT_TRUE(shm2.IsValid()); | 370 ASSERT_TRUE(shm2.IsValid()); |
371 SharedMemory shared_memory2(shm2, true); | 371 SharedMemory shared_memory2(shm2, true); |
372 shared_memory2.Map(s_memory_size); | 372 shared_memory2.Map(s_memory_size); |
373 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); | 373 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); |
374 } | 374 } |
375 | 375 |
376 // Tests that the method ShareToProcess() works. | 376 // Tests that duplication of the underlying handle works. |
377 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcess) { | 377 TEST_F(SharedMemoryMacMultiProcessTest, MachDuplicate) { |
378 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 378 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
379 | 379 |
380 { | 380 { |
381 std::unique_ptr<SharedMemory> shared_memory( | 381 std::unique_ptr<SharedMemory> shared_memory( |
382 CreateSharedMemory(s_memory_size)); | 382 CreateSharedMemory(s_memory_size)); |
383 | 383 |
384 SharedMemoryHandle shm2; | 384 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); |
385 ASSERT_TRUE(shared_memory->ShareToProcess(GetCurrentProcId(), &shm2)); | |
386 ASSERT_TRUE(shm2.IsValid()); | 385 ASSERT_TRUE(shm2.IsValid()); |
387 SharedMemory shared_memory2(shm2, true); | 386 SharedMemory shared_memory2(shm2, true); |
388 shared_memory2.Map(s_memory_size); | 387 shared_memory2.Map(s_memory_size); |
389 | 388 |
390 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), | 389 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), |
391 s_memory_size)); | 390 s_memory_size)); |
392 } | 391 } |
393 | 392 |
394 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 393 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
395 } | 394 } |
396 | 395 |
397 // Tests that the method GetReadOnlyHandle() creates a memory object that | 396 // Tests that the method GetReadOnlyHandle() creates a memory object that |
398 // is read only. | 397 // is read only. |
399 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { | 398 TEST_F(SharedMemoryMacMultiProcessTest, MachReadonly) { |
400 std::unique_ptr<SharedMemory> shared_memory( | 399 std::unique_ptr<SharedMemory> shared_memory( |
401 CreateSharedMemory(s_memory_size)); | 400 CreateSharedMemory(s_memory_size)); |
402 | 401 |
403 // Check the protection levels. | 402 // Check the protection levels. |
404 int current_prot, max_prot; | 403 int current_prot, max_prot; |
405 ASSERT_TRUE(GetProtections(shared_memory->memory(), | 404 ASSERT_TRUE(GetProtections(shared_memory->memory(), |
406 shared_memory->mapped_size(), ¤t_prot, | 405 shared_memory->mapped_size(), ¤t_prot, |
407 &max_prot)); | 406 &max_prot)); |
408 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); | 407 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); |
409 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); | 408 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); |
(...skipping 18 matching lines...) Expand all Loading... |
428 &max_prot)); | 427 &max_prot)); |
429 ASSERT_EQ(VM_PROT_READ, current_prot); | 428 ASSERT_EQ(VM_PROT_READ, current_prot); |
430 ASSERT_EQ(VM_PROT_READ, max_prot); | 429 ASSERT_EQ(VM_PROT_READ, max_prot); |
431 | 430 |
432 // The memory should still be readonly, since the underlying memory object | 431 // The memory should still be readonly, since the underlying memory object |
433 // is readonly. | 432 // is readonly. |
434 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); | 433 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); |
435 } | 434 } |
436 | 435 |
437 // Tests that the method GetReadOnlyHandle() doesn't leak. | 436 // Tests that the method GetReadOnlyHandle() doesn't leak. |
438 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { | 437 TEST_F(SharedMemoryMacMultiProcessTest, MachReadonlyLeak) { |
439 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 438 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
440 | 439 |
441 { | 440 { |
442 std::unique_ptr<SharedMemory> shared_memory( | 441 std::unique_ptr<SharedMemory> shared_memory( |
443 CreateSharedMemory(s_memory_size)); | 442 CreateSharedMemory(s_memory_size)); |
444 | 443 |
445 SharedMemoryHandle shm2 = shared_memory->GetReadOnlyHandle(); | 444 SharedMemoryHandle shm2 = shared_memory->GetReadOnlyHandle(); |
446 ASSERT_TRUE(shm2.IsValid()); | 445 ASSERT_TRUE(shm2.IsValid()); |
447 | 446 |
448 // Intentionally map with |readonly| set to |false|. | 447 // Intentionally map with |readonly| set to |false|. |
449 SharedMemory shared_memory2(shm2, false); | 448 SharedMemory shared_memory2(shm2, false); |
450 shared_memory2.Map(s_memory_size); | 449 shared_memory2.Map(s_memory_size); |
451 } | 450 } |
452 | 451 |
453 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 452 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
454 } | 453 } |
455 | 454 |
456 } // namespace base | 455 } // namespace base |
OLD | NEW |