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

Side by Side Diff: base/process_util_unittest.cc

Issue 1039007: Die on an OOM situation in many more cases.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #define _CRT_SECURE_NO_WARNINGS 5 #define _CRT_SECURE_NO_WARNINGS
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/eintr_wrapper.h" 10 #include "base/eintr_wrapper.h"
(...skipping 11 matching lines...) Expand all
22 #include <glib.h> 22 #include <glib.h>
23 #endif 23 #endif
24 #if defined(OS_POSIX) 24 #if defined(OS_POSIX)
25 #include <fcntl.h> 25 #include <fcntl.h>
26 #include <sys/resource.h> 26 #include <sys/resource.h>
27 #include <sys/socket.h> 27 #include <sys/socket.h>
28 #endif 28 #endif
29 #if defined(OS_WIN) 29 #if defined(OS_WIN)
30 #include <windows.h> 30 #include <windows.h>
31 #endif 31 #endif
32 #if defined(OS_MACOSX)
33 #include "base/process_util_unittest_mac.h"
34 #endif
32 35
33 namespace base { 36 namespace base {
34 37
35 class ProcessUtilTest : public MultiProcessTest { 38 class ProcessUtilTest : public MultiProcessTest {
36 #if defined(OS_POSIX) 39 #if defined(OS_POSIX)
37 public: 40 public:
38 // Spawn a child process that counts how many file descriptors are open. 41 // Spawn a child process that counts how many file descriptors are open.
39 int CountOpenFDsInChild(); 42 int CountOpenFDsInChild();
40 #endif 43 #endif
41 }; 44 };
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 "0 0 0 0 " // <- No CPU, apparently. 502 "0 0 0 0 " // <- No CPU, apparently.
500 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " 503 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
501 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; 504 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
502 505
503 EXPECT_EQ(0, ParseProcStatCPU(kSelfStat)); 506 EXPECT_EQ(0, ParseProcStatCPU(kSelfStat));
504 } 507 }
505 #endif 508 #endif
506 509
507 #endif // defined(OS_POSIX) 510 #endif // defined(OS_POSIX)
508 511
509 // TODO(vandebo) make this work on Windows and Mac too. 512 // TODO(vandebo) make this work on Windows too.
510 #if defined(OS_LINUX) 513 #if !defined(OS_WIN)
511 514
512 #if defined(USE_TCMALLOC) 515 #if defined(USE_TCMALLOC)
513 extern "C" { 516 extern "C" {
514 int tc_set_new_mode(int mode); 517 int tc_set_new_mode(int mode);
515 } 518 }
516 #endif // defined(USE_TCMALLOC) 519 #endif // defined(USE_TCMALLOC)
517 520
518 class OutOfMemoryTest : public testing::Test { 521 class OutOfMemoryTest : public testing::Test {
519 public: 522 public:
520 OutOfMemoryTest() 523 OutOfMemoryTest()
521 : value_(NULL), 524 : value_(NULL),
522 // Make test size as large as possible minus a few pages so 525 // Make test size as large as possible minus a few pages so
523 // that alignment or other rounding doesn't make it wrap. 526 // that alignment or other rounding doesn't make it wrap.
524 test_size_(std::numeric_limits<std::size_t>::max() - 8192) { 527 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024),
528 signed_test_size_(std::numeric_limits<ssize_t>::max()) {
525 } 529 }
526 530
527 virtual void SetUp() { 531 virtual void SetUp() {
528 // Must call EnableTerminationOnOutOfMemory() because that is called from 532 // Must call EnableTerminationOnOutOfMemory() because that is called from
529 // chrome's main function and therefore hasn't been called yet. 533 // chrome's main function and therefore hasn't been called yet.
530 EnableTerminationOnOutOfMemory(); 534 EnableTerminationOnOutOfMemory();
531 #if defined(USE_TCMALLOC) 535 #if defined(USE_TCMALLOC)
532 tc_set_new_mode(1); 536 tc_set_new_mode(1);
533 } 537 }
534 538
535 virtual void TearDown() { 539 virtual void TearDown() {
536 tc_set_new_mode(0); 540 tc_set_new_mode(0);
537 #endif // defined(USE_TCMALLOC) 541 #endif // defined(USE_TCMALLOC)
538 } 542 }
539 543
540 void* value_; 544 void* value_;
541 size_t test_size_; 545 size_t test_size_;
546 ssize_t signed_test_size_;
542 }; 547 };
543 548
544 TEST_F(OutOfMemoryTest, New) { 549 TEST_F(OutOfMemoryTest, New) {
550 ASSERT_DEATH(value_ = operator new(test_size_), "");
551 }
552
553 TEST_F(OutOfMemoryTest, NewArray) {
545 ASSERT_DEATH(value_ = new char[test_size_], ""); 554 ASSERT_DEATH(value_ = new char[test_size_], "");
546 } 555 }
547 556
548 TEST_F(OutOfMemoryTest, Malloc) { 557 TEST_F(OutOfMemoryTest, Malloc) {
549 ASSERT_DEATH(value_ = malloc(test_size_), ""); 558 ASSERT_DEATH(value_ = malloc(test_size_), "");
550 } 559 }
551 560
552 TEST_F(OutOfMemoryTest, Realloc) { 561 TEST_F(OutOfMemoryTest, Realloc) {
553 ASSERT_DEATH(value_ = realloc(NULL, test_size_), ""); 562 ASSERT_DEATH(value_ = realloc(NULL, test_size_), "");
554 } 563 }
555 564
556 TEST_F(OutOfMemoryTest, Calloc) { 565 TEST_F(OutOfMemoryTest, Calloc) {
557 ASSERT_DEATH(value_ = calloc(1024, test_size_ / 1024L), ""); 566 ASSERT_DEATH(value_ = calloc(1024, test_size_ / 1024L), "");
558 } 567 }
559 568
560 TEST_F(OutOfMemoryTest, Valloc) { 569 TEST_F(OutOfMemoryTest, Valloc) {
561 ASSERT_DEATH(value_ = valloc(test_size_), ""); 570 ASSERT_DEATH(value_ = valloc(test_size_), "");
562 } 571 }
563 572
573 #if defined(OS_LINUX)
564 TEST_F(OutOfMemoryTest, Pvalloc) { 574 TEST_F(OutOfMemoryTest, Pvalloc) {
565 ASSERT_DEATH(value_ = pvalloc(test_size_), ""); 575 ASSERT_DEATH(value_ = pvalloc(test_size_), "");
566 } 576 }
567 577
568 TEST_F(OutOfMemoryTest, Memalign) { 578 TEST_F(OutOfMemoryTest, Memalign) {
569 ASSERT_DEATH(value_ = memalign(4, test_size_), ""); 579 ASSERT_DEATH(value_ = memalign(4, test_size_), "");
570 } 580 }
571 581
572 TEST_F(OutOfMemoryTest, ViaSharedLibraries) { 582 TEST_F(OutOfMemoryTest, ViaSharedLibraries) {
573 // g_try_malloc is documented to return NULL on failure. (g_malloc is the 583 // g_try_malloc is documented to return NULL on failure. (g_malloc is the
574 // 'safe' default that crashes if allocation fails). However, since we have 584 // 'safe' default that crashes if allocation fails). However, since we have
575 // hopefully overridden malloc, even g_try_malloc should fail. This tests 585 // hopefully overridden malloc, even g_try_malloc should fail. This tests
576 // that the run-time symbol resolution is overriding malloc for shared 586 // that the run-time symbol resolution is overriding malloc for shared
577 // libraries as well as for our code. 587 // libraries as well as for our code.
578 ASSERT_DEATH(value_ = g_try_malloc(test_size_), ""); 588 ASSERT_DEATH(value_ = g_try_malloc(test_size_), "");
579 } 589 }
580 590
581 591
582 TEST_F(OutOfMemoryTest, Posix_memalign) { 592 TEST_F(OutOfMemoryTest, Posix_memalign) {
583 // Grab the return value of posix_memalign to silence a compiler warning 593 // Grab the return value of posix_memalign to silence a compiler warning
584 // about unused return values. We don't actually care about the return 594 // about unused return values. We don't actually care about the return
585 // value, since we're asserting death. 595 // value, since we're asserting death.
586 ASSERT_DEATH(EXPECT_EQ(ENOMEM, posix_memalign(&value_, 8, test_size_)), ""); 596 ASSERT_DEATH(EXPECT_EQ(ENOMEM, posix_memalign(&value_, 8, test_size_)), "");
587 } 597 }
598 #endif // OS_LINUX
588 599
589 #endif // defined(OS_LINUX) 600 #if defined(OS_MACOSX)
601
602 // Since these allocation functions take a signed size, it's possible that
603 // calling them just once won't be enough to exhaust memory.
604
605 TEST_F(OutOfMemoryTest, CFAllocatorSystemDefault) {
606 ASSERT_DEATH(while ((value_ =
607 AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {}, "");
608 }
609
610 TEST_F(OutOfMemoryTest, CFAllocatorMalloc) {
611 ASSERT_DEATH(while ((value_ =
612 AllocateViaCFAllocatorMalloc(signed_test_size_))) {}, "");
613 }
614
615 TEST_F(OutOfMemoryTest, CFAllocatorMallocZone) {
616 ASSERT_DEATH(while ((value_ =
617 AllocateViaCFAllocatorMallocZone(signed_test_size_))) {}, "");
618 }
619
620 TEST_F(OutOfMemoryTest, PsychoticallyBigObjCObject) {
621 ASSERT_DEATH(while ((value_ =
622 AllocatePsychoticallyBigObjCObject())) {}, "");
623 }
624
625 #endif // OS_MACOSX
626
627 #endif // !defined(OS_WIN)
590 628
591 } // namespace base 629 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698