OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "base/process/memory.h" | 5 #include "base/process/memory.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/mach_vm.h> | 10 #include <mach/mach_vm.h> |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 | 102 |
103 private: | 103 private: |
104 ThreadLocalBoolean* scoped_tlb_; | 104 ThreadLocalBoolean* scoped_tlb_; |
105 bool original_value_; | 105 bool original_value_; |
106 | 106 |
107 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBooleanAutoReset); | 107 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBooleanAutoReset); |
108 }; | 108 }; |
109 | 109 |
110 base::LazyInstance<ThreadLocalBoolean>::Leaky | 110 base::LazyInstance<ThreadLocalBoolean>::Leaky |
111 g_unchecked_malloc = LAZY_INSTANCE_INITIALIZER; | 111 g_unchecked_alloc = LAZY_INSTANCE_INITIALIZER; |
112 | 112 |
113 // NOTE(shess): This is called when the malloc library noticed that the heap | 113 // NOTE(shess): This is called when the malloc library noticed that the heap |
114 // is fubar. Avoid calls which will re-enter the malloc library. | 114 // is fubar. Avoid calls which will re-enter the malloc library. |
115 void CrMallocErrorBreak() { | 115 void CrMallocErrorBreak() { |
116 g_original_malloc_error_break(); | 116 g_original_malloc_error_break(); |
117 | 117 |
118 // Out of memory is certainly not heap corruption, and not necessarily | 118 // Out of memory is certainly not heap corruption, and not necessarily |
119 // something for which the process should be terminated. Leave that decision | 119 // something for which the process should be terminated. Leave that decision |
120 // to the OOM killer. The EBADF case comes up because the malloc library | 120 // to the OOM killer. The EBADF case comes up because the malloc library |
121 // attempts to log to ASL (syslog) before calling this code, which fails | 121 // attempts to log to ASL (syslog) before calling this code, which fails |
122 // accessing a Unix-domain socket because of sandboxing. | 122 // accessing a Unix-domain socket because of sandboxing. |
123 if (errno == ENOMEM || (errno == EBADF && g_unchecked_malloc.Get().Get())) | 123 if (errno == ENOMEM || (errno == EBADF && g_unchecked_alloc.Get().Get())) |
124 return; | 124 return; |
125 | 125 |
126 // A unit test checks this error message, so it needs to be in release builds. | 126 // A unit test checks this error message, so it needs to be in release builds. |
127 char buf[1024] = | 127 char buf[1024] = |
128 "Terminating process due to a potential for future heap corruption: " | 128 "Terminating process due to a potential for future heap corruption: " |
129 "errno="; | 129 "errno="; |
130 char errnobuf[] = { | 130 char errnobuf[] = { |
131 '0' + ((errno / 100) % 10), | 131 '0' + ((errno / 100) % 10), |
132 '0' + ((errno / 10) % 10), | 132 '0' + ((errno / 10) % 10), |
133 '0' + (errno % 10), | 133 '0' + (errno % 10), |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 debug::BreakDebugger(); | 484 debug::BreakDebugger(); |
485 return result; | 485 return result; |
486 } | 486 } |
487 | 487 |
488 } // namespace | 488 } // namespace |
489 | 489 |
490 void* UncheckedMalloc(size_t size) { | 490 void* UncheckedMalloc(size_t size) { |
491 if (g_old_malloc) { | 491 if (g_old_malloc) { |
492 #if ARCH_CPU_32_BITS | 492 #if ARCH_CPU_32_BITS |
493 ScopedClearErrno clear_errno; | 493 ScopedClearErrno clear_errno; |
494 ThreadLocalBooleanAutoReset flag(g_unchecked_malloc.Pointer(), true); | 494 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
495 #endif // ARCH_CPU_32_BITS | 495 #endif // ARCH_CPU_32_BITS |
496 return g_old_malloc(malloc_default_zone(), size); | 496 return g_old_malloc(malloc_default_zone(), size); |
497 } | 497 } |
498 return malloc(size); | 498 return malloc(size); |
499 } | 499 } |
500 | 500 |
| 501 void* UncheckedCalloc(size_t num_items, size_t size) { |
| 502 if (g_old_calloc) { |
| 503 #if ARCH_CPU_32_BITS |
| 504 ScopedClearErrno clear_errno; |
| 505 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
| 506 #endif // ARCH_CPU_32_BITS |
| 507 return g_old_calloc(malloc_default_zone(), num_items, size); |
| 508 } |
| 509 return calloc(num_items, size); |
| 510 } |
| 511 |
501 void EnableTerminationOnOutOfMemory() { | 512 void EnableTerminationOnOutOfMemory() { |
502 if (g_oom_killer_enabled) | 513 if (g_oom_killer_enabled) |
503 return; | 514 return; |
504 | 515 |
505 g_oom_killer_enabled = true; | 516 g_oom_killer_enabled = true; |
506 | 517 |
507 // === C malloc/calloc/valloc/realloc/posix_memalign === | 518 // === C malloc/calloc/valloc/realloc/posix_memalign === |
508 | 519 |
509 // This approach is not perfect, as requests for amounts of memory larger than | 520 // This approach is not perfect, as requests for amounts of memory larger than |
510 // MALLOC_ABSOLUTE_MAX_SIZE (currently SIZE_T_MAX - (2 * PAGE_SIZE)) will | 521 // MALLOC_ABSOLUTE_MAX_SIZE (currently SIZE_T_MAX - (2 * PAGE_SIZE)) will |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 @selector(allocWithZone:)); | 706 @selector(allocWithZone:)); |
696 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 707 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
697 method_getImplementation(orig_method)); | 708 method_getImplementation(orig_method)); |
698 CHECK(g_old_allocWithZone) | 709 CHECK(g_old_allocWithZone) |
699 << "Failed to get allocWithZone allocation function."; | 710 << "Failed to get allocWithZone allocation function."; |
700 method_setImplementation(orig_method, | 711 method_setImplementation(orig_method, |
701 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 712 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
702 } | 713 } |
703 | 714 |
704 } // namespace base | 715 } // namespace base |
OLD | NEW |