| 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 <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 void SetUp() override { | 402 void SetUp() override { |
| 403 OutOfMemoryTest::SetUp(); | 403 OutOfMemoryTest::SetUp(); |
| 404 | 404 |
| 405 // We enable termination on OOM - just as Chrome does at early | 405 // We enable termination on OOM - just as Chrome does at early |
| 406 // initialization - and test that UncheckedMalloc and UncheckedCalloc | 406 // initialization - and test that UncheckedMalloc and UncheckedCalloc |
| 407 // 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. |
| 408 base::EnableTerminationOnOutOfMemory(); | 408 base::EnableTerminationOnOutOfMemory(); |
| 409 } | 409 } |
| 410 }; | 410 }; |
| 411 | 411 |
| 412 #if defined(OS_WIN) |
| 413 |
| 414 namespace { |
| 415 |
| 416 DWORD HandleOutOfMemoryException(EXCEPTION_POINTERS* exception_ptrs, |
| 417 size_t expected_size) { |
| 418 EXPECT_EQ(base::win::kOomExceptionCode, |
| 419 exception_ptrs->ExceptionRecord->ExceptionCode); |
| 420 EXPECT_LE(1U, exception_ptrs->ExceptionRecord->NumberParameters); |
| 421 EXPECT_EQ(expected_size, |
| 422 exception_ptrs->ExceptionRecord->ExceptionInformation[0]); |
| 423 return EXCEPTION_EXECUTE_HANDLER; |
| 424 } |
| 425 |
| 426 } // namespace |
| 427 |
| 428 TEST_F(OutOfMemoryTest, TerminateBecauseOutOfMemoryReportsAllocSize) { |
| 429 // On Windows, TerminateBecauseOutOfMemory reports the attempted allocation |
| 430 // size in the exception raised. |
| 431 #if defined(ARCH_CPU_64_BITS) |
| 432 // Test with a size larger than 32 bits on 64 bit machines. |
| 433 const size_t kAttemptedAllocationSize = 0xBADA55F00DULL; |
| 434 #else |
| 435 const size_t kAttemptedAllocationSize = 0xBADA55; |
| 436 #endif |
| 437 |
| 438 __try { |
| 439 base::TerminateBecauseOutOfMemory(kAttemptedAllocationSize); |
| 440 } __except (HandleOutOfMemoryException(GetExceptionInformation(), |
| 441 kAttemptedAllocationSize)) { |
| 442 } |
| 443 } |
| 444 #endif // OS_WIN |
| 445 |
| 412 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work | 446 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work |
| 413 // on Windows as well. | 447 // on Windows as well. |
| 414 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { | 448 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
| 415 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); | 449 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); |
| 416 EXPECT_TRUE(value_ != NULL); | 450 EXPECT_TRUE(value_ != NULL); |
| 417 free(value_); | 451 free(value_); |
| 418 | 452 |
| 419 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); | 453 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); |
| 420 EXPECT_TRUE(value_ == NULL); | 454 EXPECT_TRUE(value_ == NULL); |
| 421 } | 455 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 434 bytes = static_cast<const char*>(value_); | 468 bytes = static_cast<const char*>(value_); |
| 435 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 469 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 436 EXPECT_EQ(0, bytes[i]); | 470 EXPECT_EQ(0, bytes[i]); |
| 437 free(value_); | 471 free(value_); |
| 438 | 472 |
| 439 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 473 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 440 EXPECT_TRUE(value_ == NULL); | 474 EXPECT_TRUE(value_ == NULL); |
| 441 } | 475 } |
| 442 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && | 476 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && |
| 443 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 477 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| OLD | NEW |