OLD | NEW |
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 <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*" | 145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*" |
146 "Terminating process due to a potential for future heap corruption"); | 146 "Terminating process due to a potential for future heap corruption"); |
147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) | 147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) |
148 } | 148 } |
149 | 149 |
150 #endif // defined(OS_MACOSX) | 150 #endif // defined(OS_MACOSX) |
151 | 151 |
152 // Android doesn't implement set_new_handler, so we can't use the | 152 // Android doesn't implement set_new_handler, so we can't use the |
153 // OutOfMemoryTest cases. | 153 // OutOfMemoryTest cases. |
154 // OpenBSD does not support these tests either. | 154 // OpenBSD does not support these tests either. |
155 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. | |
156 // functions so that they don't crash if the program is out of memory, so the | |
157 // OOM tests aren't supposed to work. | |
158 // TODO(vandebo) make this work on Windows too. | 155 // TODO(vandebo) make this work on Windows too. |
159 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | 156 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
160 !defined(OS_WIN) && \ | 157 !defined(OS_WIN) |
161 !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) | |
162 | 158 |
163 #if defined(USE_TCMALLOC) | 159 #if defined(USE_TCMALLOC) |
164 extern "C" { | 160 extern "C" { |
165 int tc_set_new_mode(int mode); | 161 int tc_set_new_mode(int mode); |
166 } | 162 } |
167 #endif // defined(USE_TCMALLOC) | 163 #endif // defined(USE_TCMALLOC) |
168 | 164 |
169 class OutOfMemoryTest : public testing::Test { | 165 class OutOfMemoryTest : public testing::Test { |
170 public: | 166 public: |
171 OutOfMemoryTest() | 167 OutOfMemoryTest() |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 386 |
391 // We enable termination on OOM - just as Chrome does at early | 387 // We enable termination on OOM - just as Chrome does at early |
392 // initialization - and test that UncheckedMalloc and UncheckedCalloc | 388 // initialization - and test that UncheckedMalloc and UncheckedCalloc |
393 // properly by-pass this in order to allow the caller to handle OOM. | 389 // properly by-pass this in order to allow the caller to handle OOM. |
394 base::EnableTerminationOnOutOfMemory(); | 390 base::EnableTerminationOnOutOfMemory(); |
395 } | 391 } |
396 }; | 392 }; |
397 | 393 |
398 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work | 394 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work |
399 // on Windows as well. | 395 // on Windows as well. |
400 | 396 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc() |
| 397 // under sanitizer tools. |
| 398 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
401 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { | 399 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
402 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); | 400 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); |
403 EXPECT_TRUE(value_ != NULL); | 401 EXPECT_TRUE(value_ != NULL); |
404 free(value_); | 402 free(value_); |
405 | 403 |
406 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); | 404 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); |
407 EXPECT_TRUE(value_ == NULL); | 405 EXPECT_TRUE(value_ == NULL); |
408 } | 406 } |
409 | 407 |
410 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { | 408 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { |
411 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); | 409 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); |
412 EXPECT_TRUE(value_ != NULL); | 410 EXPECT_TRUE(value_ != NULL); |
413 const char* bytes = static_cast<const char*>(value_); | 411 const char* bytes = static_cast<const char*>(value_); |
414 for (size_t i = 0; i < kSafeMallocSize; ++i) | 412 for (size_t i = 0; i < kSafeMallocSize; ++i) |
415 EXPECT_EQ(0, bytes[i]); | 413 EXPECT_EQ(0, bytes[i]); |
416 free(value_); | 414 free(value_); |
417 | 415 |
418 EXPECT_TRUE( | 416 EXPECT_TRUE( |
419 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); | 417 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); |
420 EXPECT_TRUE(value_ != NULL); | 418 EXPECT_TRUE(value_ != NULL); |
421 bytes = static_cast<const char*>(value_); | 419 bytes = static_cast<const char*>(value_); |
422 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 420 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
423 EXPECT_EQ(0, bytes[i]); | 421 EXPECT_EQ(0, bytes[i]); |
424 free(value_); | 422 free(value_); |
425 | 423 |
426 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 424 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
427 EXPECT_TRUE(value_ == NULL); | 425 EXPECT_TRUE(value_ == NULL); |
428 } | 426 } |
429 | 427 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
430 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && | 428 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) |
431 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | |
OLD | NEW |