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

Side by Side Diff: base/process/memory_unittest.cc

Issue 2201363002: android: Enable death on malloc/operator new failure. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 | « base/process/memory_linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/process/memory.h" 7 #include "base/process/memory.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 ADD_FAILURE() << "This test is not supported in this build configuration."; 75 ADD_FAILURE() << "This test is not supported in this build configuration.";
76 #endif 76 #endif
77 } 77 }
78 78
79 #endif // defined(OS_MACOSX) 79 #endif // defined(OS_MACOSX)
80 80
81 TEST(MemoryTest, AllocatorShimWorking) { 81 TEST(MemoryTest, AllocatorShimWorking) {
82 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); 82 ASSERT_TRUE(base::allocator::IsAllocatorInitialized());
83 } 83 }
84 84
85 // Android doesn't implement set_new_handler, so we can't use the 85 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan
86 // OutOfMemoryTest cases. OpenBSD does not support these tests either. 86 // configurations: only test the real allocator.
87 // Don't test these on ASan/TSan/MSan configurations: only test the real
88 // allocator.
89 // Windows only supports these tests with the allocator shim in place. 87 // Windows only supports these tests with the allocator shim in place.
90 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ 88 #if !defined(OS_OPENBSD) && \
91 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ 89 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \
92 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) 90 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
93 91
94 namespace { 92 namespace {
95 #if defined(OS_WIN) 93 #if defined(OS_WIN)
96 // Windows raises an exception rather than using LOG(FATAL) in order to make the 94 // Windows raises an exception rather than using LOG(FATAL) in order to make the
97 // exit code unique to OOM. 95 // exit code unique to OOM.
98 const char* kOomRegex = ""; 96 const char* kOomRegex = "";
99 const int kExitCode = base::win::kOomExceptionCode; 97 const int kExitCode = base::win::kOomExceptionCode;
100 #else 98 #else
101 const char* kOomRegex = "Out of memory"; 99 const char* kOomRegex = "Out of memory";
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 404
407 // We enable termination on OOM - just as Chrome does at early 405 // We enable termination on OOM - just as Chrome does at early
408 // initialization - and test that UncheckedMalloc and UncheckedCalloc 406 // initialization - and test that UncheckedMalloc and UncheckedCalloc
409 // properly by-pass this in order to allow the caller to handle OOM. 407 // properly by-pass this in order to allow the caller to handle OOM.
410 base::EnableTerminationOnOutOfMemory(); 408 base::EnableTerminationOnOutOfMemory();
411 } 409 }
412 }; 410 };
413 411
414 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work 412 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work
415 // on Windows as well. 413 // on Windows as well.
416 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc()
417 // under sanitizer tools.
418 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
Primiano Tucci (use gerrit) 2016/08/03 11:30:14 This #if is redundant as the entire block is alrea
419 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { 414 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) {
420 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); 415 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_));
421 EXPECT_TRUE(value_ != NULL); 416 EXPECT_TRUE(value_ != NULL);
422 free(value_); 417 free(value_);
423 418
424 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); 419 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_));
425 EXPECT_TRUE(value_ == NULL); 420 EXPECT_TRUE(value_ == NULL);
426 } 421 }
427 422
428 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { 423 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) {
429 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); 424 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_));
430 EXPECT_TRUE(value_ != NULL); 425 EXPECT_TRUE(value_ != NULL);
431 const char* bytes = static_cast<const char*>(value_); 426 const char* bytes = static_cast<const char*>(value_);
432 for (size_t i = 0; i < kSafeMallocSize; ++i) 427 for (size_t i = 0; i < kSafeMallocSize; ++i)
433 EXPECT_EQ(0, bytes[i]); 428 EXPECT_EQ(0, bytes[i]);
434 free(value_); 429 free(value_);
435 430
436 EXPECT_TRUE( 431 EXPECT_TRUE(
437 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); 432 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_));
438 EXPECT_TRUE(value_ != NULL); 433 EXPECT_TRUE(value_ != NULL);
439 bytes = static_cast<const char*>(value_); 434 bytes = static_cast<const char*>(value_);
440 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) 435 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i)
441 EXPECT_EQ(0, bytes[i]); 436 EXPECT_EQ(0, bytes[i]);
442 free(value_); 437 free(value_);
443 438
444 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); 439 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_));
445 EXPECT_TRUE(value_ == NULL); 440 EXPECT_TRUE(value_ == NULL);
446 } 441 }
447 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) 442 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) &&
448 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && 443 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
449 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
OLDNEW
« no previous file with comments | « base/process/memory_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698