Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: util/mac/process_reader_test.cc

Issue 577333002: Add and use CheckedReadFD(), CheckedWriteFD(), and CheckedReadFDAtEOF() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/fd_io.cc ('k') | util/mach/exception_ports_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 #else 84 #else
85 EXPECT_TRUE(process_reader.Is64Bit()); 85 EXPECT_TRUE(process_reader.Is64Bit());
86 #endif 86 #endif
87 87
88 EXPECT_EQ(getpid(), process_reader.ParentProcessID()); 88 EXPECT_EQ(getpid(), process_reader.ParentProcessID());
89 EXPECT_EQ(ChildPID(), process_reader.ProcessID()); 89 EXPECT_EQ(ChildPID(), process_reader.ProcessID());
90 90
91 int read_fd = ReadPipeFD(); 91 int read_fd = ReadPipeFD();
92 92
93 mach_vm_address_t address; 93 mach_vm_address_t address;
94 ssize_t rv = ReadFD(read_fd, &address, sizeof(address)); 94 CheckedReadFD(read_fd, &address, sizeof(address));
95 ASSERT_EQ(static_cast<ssize_t>(sizeof(address)), rv)
96 << ErrnoMessage("read");
97 95
98 std::string read_string; 96 std::string read_string;
99 ASSERT_TRUE(process_reader.Memory()->ReadCString(address, &read_string)); 97 ASSERT_TRUE(process_reader.Memory()->ReadCString(address, &read_string));
100 EXPECT_EQ(kTestMemory, read_string); 98 EXPECT_EQ(kTestMemory, read_string);
101
102 // Tell the child that it’s OK to exit. The child needed to be kept alive
103 // until the parent finished working with it.
104 int write_fd = WritePipeFD();
105 char c = '\0';
106 rv = WriteFD(write_fd, &c, 1);
107 ASSERT_EQ(1, rv) << ErrnoMessage("write");
108 } 99 }
109 100
110 void MachMultiprocessChild() override { 101 void MachMultiprocessChild() override {
111 int write_fd = WritePipeFD(); 102 int write_fd = WritePipeFD();
112 103
113 mach_vm_address_t address = 104 mach_vm_address_t address =
114 reinterpret_cast<mach_vm_address_t>(kTestMemory); 105 reinterpret_cast<mach_vm_address_t>(kTestMemory);
115 ssize_t rv = WriteFD(write_fd, &address, sizeof(address)); 106 CheckedWriteFD(write_fd, &address, sizeof(address));
116 ASSERT_EQ(static_cast<ssize_t>(sizeof(address)), rv)
117 << ErrnoMessage("write");
118 107
119 // Wait for the parent to say that it’s OK to exit. 108 // Wait for the parent to signal that it’s OK to exit by closing its end of
120 int read_fd = ReadPipeFD(); 109 // the pipe.
121 char c; 110 CheckedReadFDAtEOF(ReadPipeFD());
122 rv = ReadFD(read_fd, &c, 1);
123 ASSERT_EQ(1, rv) << ErrnoMessage("read");
124 } 111 }
125 112
126 DISALLOW_COPY_AND_ASSIGN(ProcessReaderChild); 113 DISALLOW_COPY_AND_ASSIGN(ProcessReaderChild);
127 }; 114 };
128 115
129 TEST(ProcessReader, ChildBasic) { 116 TEST(ProcessReader, ChildBasic) {
130 ProcessReaderChild process_reader_child; 117 ProcessReaderChild process_reader_child;
131 process_reader_child.Run(); 118 process_reader_child.Run();
132 } 119 }
133 120
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 int read_fd = ReadPipeFD(); 435 int read_fd = ReadPipeFD();
449 436
450 // Build a map of all expected threads, keyed by each thread’s ID, and with 437 // Build a map of all expected threads, keyed by each thread’s ID, and with
451 // addresses that should lie somewhere within each thread’s stack as values. 438 // addresses that should lie somewhere within each thread’s stack as values.
452 // These IDs and addresses all come from the child process via the pipe. 439 // These IDs and addresses all come from the child process via the pipe.
453 ThreadMap thread_map; 440 ThreadMap thread_map;
454 for (size_t thread_index = 0; 441 for (size_t thread_index = 0;
455 thread_index < thread_count_ + 1; 442 thread_index < thread_count_ + 1;
456 ++thread_index) { 443 ++thread_index) {
457 uint64_t thread_id; 444 uint64_t thread_id;
458 ssize_t rv = ReadFD(read_fd, &thread_id, sizeof(thread_id)); 445 CheckedReadFD(read_fd, &thread_id, sizeof(thread_id));
459 ASSERT_EQ(static_cast<ssize_t>(sizeof(thread_id)), rv)
460 << ErrnoMessage("read");
461 446
462 TestThreadPool::ThreadExpectation expectation; 447 TestThreadPool::ThreadExpectation expectation;
463 rv = ReadFD(read_fd, 448 CheckedReadFD(read_fd,
464 &expectation.stack_address, 449 &expectation.stack_address,
465 sizeof(expectation.stack_address)); 450 sizeof(expectation.stack_address));
466 ASSERT_EQ(static_cast<ssize_t>(sizeof(expectation.stack_address)), rv) 451 CheckedReadFD(read_fd,
467 << ErrnoMessage("read"); 452 &expectation.suspend_count,
468 453 sizeof(expectation.suspend_count));
469 rv = ReadFD(read_fd,
470 &expectation.suspend_count,
471 sizeof(expectation.suspend_count));
472 ASSERT_EQ(static_cast<ssize_t>(sizeof(expectation.suspend_count)), rv)
473 << ErrnoMessage("read");
474 454
475 // There can’t be any duplicate thread IDs. 455 // There can’t be any duplicate thread IDs.
476 EXPECT_EQ(0u, thread_map.count(thread_id)); 456 EXPECT_EQ(0u, thread_map.count(thread_id));
477 457
478 thread_map[thread_id] = expectation; 458 thread_map[thread_id] = expectation;
479 } 459 }
480 460
481 const std::vector<ProcessReader::Thread>& threads = process_reader.Threads() ; 461 const std::vector<ProcessReader::Thread>& threads = process_reader.Threads() ;
482 462
483 // The child shouldn’t have any threads other than its main thread and the 463 // The child shouldn’t have any threads other than its main thread and the
484 // ones it created in its pool, so pass false for |tolerate_extra_threads|. 464 // ones it created in its pool, so pass false for |tolerate_extra_threads|.
485 ExpectSeveralThreads(&thread_map, threads, false); 465 ExpectSeveralThreads(&thread_map, threads, false);
486
487 // Tell the child that it’s OK to exit. The child needed to be kept alive
488 // until the parent finished working with it.
489 int write_fd = WritePipeFD();
490 char c = '\0';
491 ssize_t rv = WriteFD(write_fd, &c, 1);
Robert Sesek 2014/09/18 19:02:14 This delete was intentional because you're now rel
Mark Mentovai 2014/09/18 19:03:11 rsesek wrote:
492 ASSERT_EQ(1, rv) << ErrnoMessage("write");
493 } 466 }
494 467
495 void MachMultiprocessChild() override { 468 void MachMultiprocessChild() override {
496 TestThreadPool thread_pool; 469 TestThreadPool thread_pool;
497 thread_pool.StartThreads(thread_count_); 470 thread_pool.StartThreads(thread_count_);
498 if (testing::Test::HasFatalFailure()) { 471 if (testing::Test::HasFatalFailure()) {
499 return; 472 return;
500 } 473 }
501 474
502 int write_fd = WritePipeFD(); 475 int write_fd = WritePipeFD();
503 476
504 // This thread isn’t part of the thread pool, but the parent will be able 477 // This thread isn’t part of the thread pool, but the parent will be able
505 // to inspect it. Write an entry for it. 478 // to inspect it. Write an entry for it.
506 uint64_t thread_id = PthreadToThreadID(pthread_self()); 479 uint64_t thread_id = PthreadToThreadID(pthread_self());
507 480
508 ssize_t rv = WriteFD(write_fd, &thread_id, sizeof(thread_id)); 481 CheckedWriteFD(write_fd, &thread_id, sizeof(thread_id));
509 ASSERT_EQ(static_cast<ssize_t>(sizeof(thread_id)), rv)
510 << ErrnoMessage("write");
511 482
512 TestThreadPool::ThreadExpectation expectation; 483 TestThreadPool::ThreadExpectation expectation;
513 expectation.stack_address = reinterpret_cast<mach_vm_address_t>(&thread_id); 484 expectation.stack_address = reinterpret_cast<mach_vm_address_t>(&thread_id);
514 expectation.suspend_count = 0; 485 expectation.suspend_count = 0;
515 486
516 rv = WriteFD(write_fd, 487 CheckedWriteFD(write_fd,
517 &expectation.stack_address, 488 &expectation.stack_address,
518 sizeof(expectation.stack_address)); 489 sizeof(expectation.stack_address));
519 ASSERT_EQ(static_cast<ssize_t>(sizeof(expectation.stack_address)), rv) 490 CheckedWriteFD(write_fd,
520 << ErrnoMessage("write"); 491 &expectation.suspend_count,
521 492 sizeof(expectation.suspend_count));
522 rv = WriteFD(write_fd,
523 &expectation.suspend_count,
524 sizeof(expectation.suspend_count));
525 ASSERT_EQ(static_cast<ssize_t>(sizeof(expectation.suspend_count)), rv)
526 << ErrnoMessage("write");
527 493
528 // Write an entry for everything in the thread pool. 494 // Write an entry for everything in the thread pool.
529 for (size_t thread_index = 0; 495 for (size_t thread_index = 0;
530 thread_index < thread_count_; 496 thread_index < thread_count_;
531 ++thread_index) { 497 ++thread_index) {
532 uint64_t thread_id = 498 uint64_t thread_id =
533 thread_pool.GetThreadInfo(thread_index, &expectation); 499 thread_pool.GetThreadInfo(thread_index, &expectation);
534 500
535 rv = WriteFD(write_fd, &thread_id, sizeof(thread_id)); 501 CheckedWriteFD(write_fd, &thread_id, sizeof(thread_id));
536 ASSERT_EQ(static_cast<ssize_t>(sizeof(thread_id)), rv) 502 CheckedWriteFD(write_fd,
537 << ErrnoMessage("write"); 503 &expectation.stack_address,
538 504 sizeof(expectation.stack_address));
539 rv = WriteFD(write_fd, 505 CheckedWriteFD(write_fd,
540 &expectation.stack_address, 506 &expectation.suspend_count,
541 sizeof(expectation.stack_address)); 507 sizeof(expectation.suspend_count));
542 ASSERT_EQ(static_cast<ssize_t>(sizeof(expectation.stack_address)), rv)
543 << ErrnoMessage("write");
544
545 rv = WriteFD(write_fd,
546 &expectation.suspend_count,
547 sizeof(expectation.suspend_count));
548 ASSERT_EQ(static_cast<ssize_t>(sizeof(expectation.suspend_count)), rv)
549 << ErrnoMessage("write");
550 } 508 }
551 509
552 // Wait for the parent to say that it’s OK to exit. 510 // Wait for the parent to signal that it’s OK to exit by closing its end of
553 int read_fd = ReadPipeFD(); 511 // the pipe.
554 char c; 512 CheckedReadFDAtEOF(ReadPipeFD());
555 rv = ReadFD(read_fd, &c, 1);
556 ASSERT_EQ(1, rv) << ErrnoMessage("read");
557 } 513 }
558 514
559 size_t thread_count_; 515 size_t thread_count_;
560 516
561 DISALLOW_COPY_AND_ASSIGN(ProcessReaderThreadedChild); 517 DISALLOW_COPY_AND_ASSIGN(ProcessReaderThreadedChild);
562 }; 518 };
563 519
564 TEST(ProcessReader, ChildOneThread) { 520 TEST(ProcessReader, ChildOneThread) {
565 // The main thread plus zero child threads equals one thread. 521 // The main thread plus zero child threads equals one thread.
566 const size_t kChildThreads = 0; 522 const size_t kChildThreads = 0;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 const std::vector<ProcessReader::Module>& modules = 599 const std::vector<ProcessReader::Module>& modules =
644 process_reader.Modules(); 600 process_reader.Modules();
645 601
646 // There needs to be at least an entry for the main executable, for a dylib, 602 // There needs to be at least an entry for the main executable, for a dylib,
647 // and for dyld. 603 // and for dyld.
648 ASSERT_GE(modules.size(), 3u); 604 ASSERT_GE(modules.size(), 3u);
649 605
650 int read_fd = ReadPipeFD(); 606 int read_fd = ReadPipeFD();
651 607
652 uint32_t expect_modules; 608 uint32_t expect_modules;
653 ssize_t rv = ReadFD(read_fd, &expect_modules, sizeof(expect_modules)); 609 CheckedReadFD(read_fd, &expect_modules, sizeof(expect_modules));
654 ASSERT_EQ(static_cast<ssize_t>(sizeof(expect_modules)), rv)
655 << ErrnoMessage("read");
656 610
657 ASSERT_EQ(expect_modules, modules.size()); 611 ASSERT_EQ(expect_modules, modules.size());
658 612
659 for (size_t index = 0; index < modules.size(); ++index) { 613 for (size_t index = 0; index < modules.size(); ++index) {
660 SCOPED_TRACE(base::StringPrintf( 614 SCOPED_TRACE(base::StringPrintf(
661 "index %zu, name %s", index, modules[index].name.c_str())); 615 "index %zu, name %s", index, modules[index].name.c_str()));
662 616
663 uint32_t expect_name_length; 617 uint32_t expect_name_length;
664 rv = ReadFD( 618 CheckedReadFD(
665 read_fd, &expect_name_length, sizeof(expect_name_length)); 619 read_fd, &expect_name_length, sizeof(expect_name_length));
666 ASSERT_EQ(static_cast<ssize_t>(sizeof(expect_name_length)), rv)
667 << ErrnoMessage("read");
668 620
669 // The NUL terminator is not read. 621 // The NUL terminator is not read.
670 std::string expect_name(expect_name_length, '\0'); 622 std::string expect_name(expect_name_length, '\0');
671 rv = ReadFD(read_fd, &expect_name[0], expect_name_length); 623 CheckedReadFD(read_fd, &expect_name[0], expect_name_length);
672 ASSERT_EQ(static_cast<ssize_t>(expect_name_length), rv)
673 << ErrnoMessage("read");
674
675 EXPECT_EQ(expect_name, modules[index].name); 624 EXPECT_EQ(expect_name, modules[index].name);
676 625
677 mach_vm_address_t expect_address; 626 mach_vm_address_t expect_address;
678 rv = ReadFD(read_fd, &expect_address, sizeof(expect_address)); 627 CheckedReadFD(read_fd, &expect_address, sizeof(expect_address));
679 ASSERT_EQ(static_cast<ssize_t>(sizeof(expect_address)), rv)
680 << ErrnoMessage("read");
681
682 EXPECT_EQ(expect_address, modules[index].address); 628 EXPECT_EQ(expect_address, modules[index].address);
683 629
684 if (index == 0 || index == modules.size() - 1) { 630 if (index == 0 || index == modules.size() - 1) {
685 // dyld didn’t load the main executable or itself, so it couldn’t record 631 // dyld didn’t load the main executable or itself, so it couldn’t record
686 // these timestamps, and they are reported as 0. 632 // these timestamps, and they are reported as 0.
687 EXPECT_EQ(0, modules[index].timestamp); 633 EXPECT_EQ(0, modules[index].timestamp);
688 } else { 634 } else {
689 // Hope that the module didn’t change on disk. 635 // Hope that the module didn’t change on disk.
690 struct stat stat_buf; 636 struct stat stat_buf;
691 int rv = stat(expect_name.c_str(), &stat_buf); 637 int rv = stat(expect_name.c_str(), &stat_buf);
692 EXPECT_EQ(0, rv) << ErrnoMessage("stat"); 638 EXPECT_EQ(0, rv) << ErrnoMessage("stat");
693 if (rv == 0) { 639 if (rv == 0) {
694 EXPECT_EQ(stat_buf.st_mtime, modules[index].timestamp); 640 EXPECT_EQ(stat_buf.st_mtime, modules[index].timestamp);
695 } 641 }
696 } 642 }
697 } 643 }
698
699 // Tell the child that it’s OK to exit. The child needed to be kept alive
700 // until the parent finished working with it.
701 int write_fd = WritePipeFD();
702 char c = '\0';
703 rv = WriteFD(write_fd, &c, 1);
704 ASSERT_EQ(1, rv) << ErrnoMessage("write");
705 } 644 }
706 645
707 void MachMultiprocessChild() override { 646 void MachMultiprocessChild() override {
708 int write_fd = WritePipeFD(); 647 int write_fd = WritePipeFD();
709 648
710 uint32_t dyld_image_count = _dyld_image_count(); 649 uint32_t dyld_image_count = _dyld_image_count();
711 const struct dyld_all_image_infos* dyld_image_infos = 650 const struct dyld_all_image_infos* dyld_image_infos =
712 _dyld_get_all_image_infos(); 651 _dyld_get_all_image_infos();
713 652
714 uint32_t write_image_count = dyld_image_count; 653 uint32_t write_image_count = dyld_image_count;
715 if (dyld_image_infos->version >= 2) { 654 if (dyld_image_infos->version >= 2) {
716 // dyld_image_count doesn’t include an entry for dyld itself, but one will 655 // dyld_image_count doesn’t include an entry for dyld itself, but one will
717 // be written. 656 // be written.
718 ++write_image_count; 657 ++write_image_count;
719 } 658 }
720 659
721 ssize_t rv = WriteFD( 660 CheckedWriteFD(write_fd, &write_image_count, sizeof(write_image_count));
722 write_fd, &write_image_count, sizeof(write_image_count));
723 ASSERT_EQ(static_cast<ssize_t>(sizeof(write_image_count)), rv)
724 << ErrnoMessage("write");
725 661
726 for (size_t index = 0; index < write_image_count; ++index) { 662 for (size_t index = 0; index < write_image_count; ++index) {
727 const char* dyld_image_name; 663 const char* dyld_image_name;
728 mach_vm_address_t dyld_image_address; 664 mach_vm_address_t dyld_image_address;
729 665
730 if (index < dyld_image_count) { 666 if (index < dyld_image_count) {
731 dyld_image_name = _dyld_get_image_name(index); 667 dyld_image_name = _dyld_get_image_name(index);
732 dyld_image_address = 668 dyld_image_address =
733 reinterpret_cast<mach_vm_address_t>(_dyld_get_image_header(index)); 669 reinterpret_cast<mach_vm_address_t>(_dyld_get_image_header(index));
734 } else { 670 } else {
735 dyld_image_name = "/usr/lib/dyld"; 671 dyld_image_name = "/usr/lib/dyld";
736 dyld_image_address = reinterpret_cast<mach_vm_address_t>( 672 dyld_image_address = reinterpret_cast<mach_vm_address_t>(
737 dyld_image_infos->dyldImageLoadAddress); 673 dyld_image_infos->dyldImageLoadAddress);
738 } 674 }
739 675
740 uint32_t dyld_image_name_length = strlen(dyld_image_name); 676 uint32_t dyld_image_name_length = strlen(dyld_image_name);
741 rv = WriteFD( 677 CheckedWriteFD(
742 write_fd, &dyld_image_name_length, sizeof(dyld_image_name_length)); 678 write_fd, &dyld_image_name_length, sizeof(dyld_image_name_length));
743 ASSERT_EQ(static_cast<ssize_t>(sizeof(dyld_image_name_length)), rv)
744 << ErrnoMessage("write");
745 679
746 // The NUL terminator is not written. 680 // The NUL terminator is not written.
747 rv = WriteFD(write_fd, dyld_image_name, dyld_image_name_length); 681 CheckedWriteFD(write_fd, dyld_image_name, dyld_image_name_length);
748 ASSERT_EQ(static_cast<ssize_t>(dyld_image_name_length), rv)
749 << ErrnoMessage("write");
750 682
751 rv = WriteFD(write_fd, &dyld_image_address, sizeof(dyld_image_address)); 683 CheckedWriteFD(write_fd, &dyld_image_address, sizeof(dyld_image_address));
752 ASSERT_EQ(static_cast<ssize_t>(sizeof(dyld_image_address)), rv)
753 << ErrnoMessage("write");
754 } 684 }
755 685
756 // Wait for the parent to say that it’s OK to exit. 686 // Wait for the parent to signal that it’s OK to exit by closing its end of
757 int read_fd = ReadPipeFD(); 687 // the pipe.
758 char c; 688 CheckedReadFDAtEOF(ReadPipeFD());
759 rv = ReadFD(read_fd, &c, 1);
760 ASSERT_EQ(1, rv) << ErrnoMessage("read");
761 } 689 }
762 690
763 DISALLOW_COPY_AND_ASSIGN(ProcessReaderModulesChild); 691 DISALLOW_COPY_AND_ASSIGN(ProcessReaderModulesChild);
764 }; 692 };
765 693
766 TEST(ProcessReader, ChildModules) { 694 TEST(ProcessReader, ChildModules) {
767 ProcessReaderModulesChild process_reader_modules_child; 695 ProcessReaderModulesChild process_reader_modules_child;
768 process_reader_modules_child.Run(); 696 process_reader_modules_child.Run();
769 } 697 }
770 698
771 } // namespace 699 } // namespace
OLDNEW
« no previous file with comments | « util/file/fd_io.cc ('k') | util/mach/exception_ports_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698