| 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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 // process. | 221 // process. |
| 222 mac::ScopedMachSendRight client_port_; | 222 mac::ScopedMachSendRight client_port_; |
| 223 | 223 |
| 224 base::Process child_process_; | 224 base::Process child_process_; |
| 225 DISALLOW_COPY_AND_ASSIGN(SharedMemoryMacMultiProcessTest); | 225 DISALLOW_COPY_AND_ASSIGN(SharedMemoryMacMultiProcessTest); |
| 226 }; | 226 }; |
| 227 | 227 |
| 228 // Tests that content written to shared memory in the server process can be read | 228 // Tests that content written to shared memory in the server process can be read |
| 229 // by the child process. | 229 // by the child process. |
| 230 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemory) { | 230 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemory) { |
| 231 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 232 if (mac::IsOSSnowLeopard()) | |
| 233 return; | |
| 234 | |
| 235 SetUpChild("MachBasedSharedMemoryClient"); | 231 SetUpChild("MachBasedSharedMemoryClient"); |
| 236 | 232 |
| 237 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); | 233 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
| 238 | 234 |
| 239 // Send the underlying memory object to the client process. | 235 // Send the underlying memory object to the client process. |
| 240 SendMachPort(client_port_.get(), shared_memory->handle().GetMemoryObject(), | 236 SendMachPort(client_port_.get(), shared_memory->handle().GetMemoryObject(), |
| 241 MACH_MSG_TYPE_COPY_SEND); | 237 MACH_MSG_TYPE_COPY_SEND); |
| 242 int rv = -1; | 238 int rv = -1; |
| 243 ASSERT_TRUE(child_process_.WaitForExitWithTimeout( | 239 ASSERT_TRUE(child_process_.WaitForExitWithTimeout( |
| 244 TestTimeouts::action_timeout(), &rv)); | 240 TestTimeouts::action_timeout(), &rv)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 256 shared_memory.Map(SharedMemoryMacMultiProcessTest::s_memory_size); | 252 shared_memory.Map(SharedMemoryMacMultiProcessTest::s_memory_size); |
| 257 const char* start = static_cast<const char*>(shared_memory.memory()); | 253 const char* start = static_cast<const char*>(shared_memory.memory()); |
| 258 for (int i = 0; i < SharedMemoryMacMultiProcessTest::s_memory_size; ++i) { | 254 for (int i = 0; i < SharedMemoryMacMultiProcessTest::s_memory_size; ++i) { |
| 259 DCHECK_EQ(start[i], 'a'); | 255 DCHECK_EQ(start[i], 'a'); |
| 260 } | 256 } |
| 261 return 0; | 257 return 0; |
| 262 } | 258 } |
| 263 | 259 |
| 264 // Tests that mapping shared memory with an offset works correctly. | 260 // Tests that mapping shared memory with an offset works correctly. |
| 265 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemoryWithOffset) { | 261 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemoryWithOffset) { |
| 266 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 267 if (mac::IsOSSnowLeopard()) | |
| 268 return; | |
| 269 | |
| 270 SetUpChild("MachBasedSharedMemoryWithOffsetClient"); | 262 SetUpChild("MachBasedSharedMemoryWithOffsetClient"); |
| 271 | 263 |
| 272 SharedMemoryHandle shm(s_memory_size); | 264 SharedMemoryHandle shm(s_memory_size); |
| 273 ASSERT_TRUE(shm.IsValid()); | 265 ASSERT_TRUE(shm.IsValid()); |
| 274 SharedMemory shared_memory(shm, false); | 266 SharedMemory shared_memory(shm, false); |
| 275 shared_memory.Map(s_memory_size); | 267 shared_memory.Map(s_memory_size); |
| 276 | 268 |
| 277 size_t page_size = SysInfo::VMAllocationGranularity(); | 269 size_t page_size = SysInfo::VMAllocationGranularity(); |
| 278 char* start = static_cast<char*>(shared_memory.memory()); | 270 char* start = static_cast<char*>(shared_memory.memory()); |
| 279 memset(start, 'a', page_size); | 271 memset(start, 'a', page_size); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 305 } | 297 } |
| 306 for (size_t i = page_size; i < 2 * page_size; ++i) { | 298 for (size_t i = page_size; i < 2 * page_size; ++i) { |
| 307 DCHECK_EQ(start[i], 'c'); | 299 DCHECK_EQ(start[i], 'c'); |
| 308 } | 300 } |
| 309 return 0; | 301 return 0; |
| 310 } | 302 } |
| 311 | 303 |
| 312 // Tests that duplication and closing has the right effect on Mach reference | 304 // Tests that duplication and closing has the right effect on Mach reference |
| 313 // counts. | 305 // counts. |
| 314 TEST_F(SharedMemoryMacMultiProcessTest, MachDuplicateAndClose) { | 306 TEST_F(SharedMemoryMacMultiProcessTest, MachDuplicateAndClose) { |
| 315 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 316 if (mac::IsOSSnowLeopard()) | |
| 317 return; | |
| 318 | |
| 319 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 307 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
| 320 | 308 |
| 321 // Making a new SharedMemoryHandle increments the name count. | 309 // Making a new SharedMemoryHandle increments the name count. |
| 322 SharedMemoryHandle shm(s_memory_size); | 310 SharedMemoryHandle shm(s_memory_size); |
| 323 ASSERT_TRUE(shm.IsValid()); | 311 ASSERT_TRUE(shm.IsValid()); |
| 324 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); | 312 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); |
| 325 | 313 |
| 326 // Duplicating the SharedMemoryHandle increments the ref count, but doesn't | 314 // Duplicating the SharedMemoryHandle increments the ref count, but doesn't |
| 327 // make a new name. | 315 // make a new name. |
| 328 shm.Duplicate(); | 316 shm.Duplicate(); |
| 329 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); | 317 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); |
| 330 | 318 |
| 331 // Closing the SharedMemoryHandle decrements the ref count. The first time has | 319 // Closing the SharedMemoryHandle decrements the ref count. The first time has |
| 332 // no effect. | 320 // no effect. |
| 333 shm.Close(); | 321 shm.Close(); |
| 334 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); | 322 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); |
| 335 | 323 |
| 336 // Closing the SharedMemoryHandle decrements the ref count. The second time | 324 // Closing the SharedMemoryHandle decrements the ref count. The second time |
| 337 // destroys the port. | 325 // destroys the port. |
| 338 shm.Close(); | 326 shm.Close(); |
| 339 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 327 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
| 340 } | 328 } |
| 341 | 329 |
| 342 // Tests that Mach shared memory can be mapped and unmapped. | 330 // Tests that Mach shared memory can be mapped and unmapped. |
| 343 TEST_F(SharedMemoryMacMultiProcessTest, MachUnmapMap) { | 331 TEST_F(SharedMemoryMacMultiProcessTest, MachUnmapMap) { |
| 344 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 345 if (mac::IsOSSnowLeopard()) | |
| 346 return; | |
| 347 | |
| 348 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 332 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
| 349 | 333 |
| 350 scoped_ptr<SharedMemory> shared_memory = CreateSharedMemory(s_memory_size); | 334 scoped_ptr<SharedMemory> shared_memory = CreateSharedMemory(s_memory_size); |
| 351 ASSERT_TRUE(shared_memory->Unmap()); | 335 ASSERT_TRUE(shared_memory->Unmap()); |
| 352 ASSERT_TRUE(shared_memory->Map(s_memory_size)); | 336 ASSERT_TRUE(shared_memory->Map(s_memory_size)); |
| 353 shared_memory.reset(); | 337 shared_memory.reset(); |
| 354 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 338 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
| 355 } | 339 } |
| 356 | 340 |
| 357 // Tests that passing a SharedMemoryHandle to a SharedMemory object also passes | 341 // Tests that passing a SharedMemoryHandle to a SharedMemory object also passes |
| 358 // ownership, and that destroying the SharedMemory closes the SharedMemoryHandle | 342 // ownership, and that destroying the SharedMemory closes the SharedMemoryHandle |
| 359 // as well. | 343 // as well. |
| 360 TEST_F(SharedMemoryMacMultiProcessTest, MachSharedMemoryTakesOwnership) { | 344 TEST_F(SharedMemoryMacMultiProcessTest, MachSharedMemoryTakesOwnership) { |
| 361 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 362 if (mac::IsOSSnowLeopard()) | |
| 363 return; | |
| 364 | |
| 365 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 345 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
| 366 | 346 |
| 367 // Making a new SharedMemoryHandle increments the name count. | 347 // Making a new SharedMemoryHandle increments the name count. |
| 368 SharedMemoryHandle shm(s_memory_size); | 348 SharedMemoryHandle shm(s_memory_size); |
| 369 ASSERT_TRUE(shm.IsValid()); | 349 ASSERT_TRUE(shm.IsValid()); |
| 370 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); | 350 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); |
| 371 | 351 |
| 372 // Name count doesn't change when mapping the memory. | 352 // Name count doesn't change when mapping the memory. |
| 373 scoped_ptr<SharedMemory> shared_memory(new SharedMemory(shm, false)); | 353 scoped_ptr<SharedMemory> shared_memory(new SharedMemory(shm, false)); |
| 374 shared_memory->Map(s_memory_size); | 354 shared_memory->Map(s_memory_size); |
| 375 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); | 355 EXPECT_EQ(active_name_count + 1, GetActiveNameCount()); |
| 376 | 356 |
| 377 // Destroying the SharedMemory object frees the resource. | 357 // Destroying the SharedMemory object frees the resource. |
| 378 shared_memory.reset(); | 358 shared_memory.reset(); |
| 379 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 359 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
| 380 } | 360 } |
| 381 | 361 |
| 382 // Tests that the read-only flag works. | 362 // Tests that the read-only flag works. |
| 383 TEST_F(SharedMemoryMacMultiProcessTest, MachReadOnly) { | 363 TEST_F(SharedMemoryMacMultiProcessTest, MachReadOnly) { |
| 384 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 385 if (mac::IsOSSnowLeopard()) | |
| 386 return; | |
| 387 | |
| 388 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); | 364 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
| 389 | 365 |
| 390 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); | 366 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); |
| 391 ASSERT_TRUE(shm2.IsValid()); | 367 ASSERT_TRUE(shm2.IsValid()); |
| 392 SharedMemory shared_memory2(shm2, true); | 368 SharedMemory shared_memory2(shm2, true); |
| 393 shared_memory2.Map(s_memory_size); | 369 shared_memory2.Map(s_memory_size); |
| 394 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); | 370 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); |
| 395 } | 371 } |
| 396 | 372 |
| 397 // Tests that the method ShareToProcess() works. | 373 // Tests that the method ShareToProcess() works. |
| 398 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcess) { | 374 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcess) { |
| 399 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 400 if (mac::IsOSSnowLeopard()) | |
| 401 return; | |
| 402 | |
| 403 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 375 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
| 404 | 376 |
| 405 { | 377 { |
| 406 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); | 378 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
| 407 | 379 |
| 408 SharedMemoryHandle shm2; | 380 SharedMemoryHandle shm2; |
| 409 ASSERT_TRUE(shared_memory->ShareToProcess(GetCurrentProcId(), &shm2)); | 381 ASSERT_TRUE(shared_memory->ShareToProcess(GetCurrentProcId(), &shm2)); |
| 410 ASSERT_TRUE(shm2.IsValid()); | 382 ASSERT_TRUE(shm2.IsValid()); |
| 411 SharedMemory shared_memory2(shm2, true); | 383 SharedMemory shared_memory2(shm2, true); |
| 412 shared_memory2.Map(s_memory_size); | 384 shared_memory2.Map(s_memory_size); |
| 413 | 385 |
| 414 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), | 386 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), |
| 415 s_memory_size)); | 387 s_memory_size)); |
| 416 } | 388 } |
| 417 | 389 |
| 418 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 390 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
| 419 } | 391 } |
| 420 | 392 |
| 421 // Tests that the method ShareReadOnlyToProcess() creates a memory object that | 393 // Tests that the method ShareReadOnlyToProcess() creates a memory object that |
| 422 // is read only. | 394 // is read only. |
| 423 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { | 395 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { |
| 424 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 425 if (mac::IsOSSnowLeopard()) | |
| 426 return; | |
| 427 | |
| 428 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); | 396 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
| 429 | 397 |
| 430 // Check the protection levels. | 398 // Check the protection levels. |
| 431 int current_prot, max_prot; | 399 int current_prot, max_prot; |
| 432 ASSERT_TRUE(GetProtections(shared_memory->memory(), | 400 ASSERT_TRUE(GetProtections(shared_memory->memory(), |
| 433 shared_memory->mapped_size(), ¤t_prot, | 401 shared_memory->mapped_size(), ¤t_prot, |
| 434 &max_prot)); | 402 &max_prot)); |
| 435 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); | 403 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); |
| 436 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); | 404 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); |
| 437 | 405 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 457 ASSERT_EQ(VM_PROT_READ, current_prot); | 425 ASSERT_EQ(VM_PROT_READ, current_prot); |
| 458 ASSERT_EQ(VM_PROT_READ, max_prot); | 426 ASSERT_EQ(VM_PROT_READ, max_prot); |
| 459 | 427 |
| 460 // The memory should still be readonly, since the underlying memory object | 428 // The memory should still be readonly, since the underlying memory object |
| 461 // is readonly. | 429 // is readonly. |
| 462 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); | 430 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); |
| 463 } | 431 } |
| 464 | 432 |
| 465 // Tests that the method ShareReadOnlyToProcess() doesn't leak. | 433 // Tests that the method ShareReadOnlyToProcess() doesn't leak. |
| 466 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { | 434 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { |
| 467 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 468 if (mac::IsOSSnowLeopard()) | |
| 469 return; | |
| 470 | |
| 471 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 435 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
| 472 | 436 |
| 473 { | 437 { |
| 474 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); | 438 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
| 475 | 439 |
| 476 SharedMemoryHandle shm2; | 440 SharedMemoryHandle shm2; |
| 477 ASSERT_TRUE( | 441 ASSERT_TRUE( |
| 478 shared_memory->ShareReadOnlyToProcess(GetCurrentProcId(), &shm2)); | 442 shared_memory->ShareReadOnlyToProcess(GetCurrentProcId(), &shm2)); |
| 479 ASSERT_TRUE(shm2.IsValid()); | 443 ASSERT_TRUE(shm2.IsValid()); |
| 480 | 444 |
| 481 // Intentionally map with |readonly| set to |false|. | 445 // Intentionally map with |readonly| set to |false|. |
| 482 SharedMemory shared_memory2(shm2, false); | 446 SharedMemory shared_memory2(shm2, false); |
| 483 shared_memory2.Map(s_memory_size); | 447 shared_memory2.Map(s_memory_size); |
| 484 } | 448 } |
| 485 | 449 |
| 486 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 450 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
| 487 } | 451 } |
| 488 | 452 |
| 489 } // namespace base | 453 } // namespace base |
| OLD | NEW |