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

Side by Side Diff: base/process/memory_linux.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 | « no previous file | base/process/memory_unittest.cc » ('j') | base/process/memory_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <new> 9 #include <new>
10 10
11 #include "base/allocator/allocator_shim.h" 11 #include "base/allocator/allocator_shim.h"
12 #include "base/allocator/features.h" 12 #include "base/allocator/features.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/process/internal_linux.h" 16 #include "base/process/internal_linux.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 19
20 #if defined(USE_TCMALLOC) 20 #if defined(USE_TCMALLOC)
21 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" 21 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h"
22 #endif 22 #endif
23 23
24 namespace base { 24 namespace base {
25 25
26 size_t g_oom_size = 0U; 26 size_t g_oom_size = 0U;
27 27
28 namespace { 28 namespace {
29 29
30 #if !defined(OS_ANDROID)
31 void OnNoMemorySize(size_t size) { 30 void OnNoMemorySize(size_t size) {
32 g_oom_size = size; 31 g_oom_size = size;
33 32
34 if (size != 0) 33 if (size != 0)
35 LOG(FATAL) << "Out of memory, size = " << size; 34 LOG(FATAL) << "Out of memory, size = " << size;
36 LOG(FATAL) << "Out of memory."; 35 LOG(FATAL) << "Out of memory.";
37 } 36 }
38 37
39 void OnNoMemory() { 38 void OnNoMemory() {
40 OnNoMemorySize(0); 39 OnNoMemorySize(0);
41 } 40 }
42 #endif // !defined(OS_ANDROID)
43 41
44 } // namespace 42 } // namespace
45 43
46 // TODO(primiano): Once the unified shim is on by default (crbug.com/550886) 44 // TODO(primiano): Once the unified shim is on by default (crbug.com/550886)
47 // get rid of the code in this entire #if section. The whole termination-on-OOM 45 // get rid of the code in this entire #if section. The whole termination-on-OOM
48 // logic is implemented in the shim. 46 // logic is implemented in the shim.
49 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ 47 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \
50 !defined(THREAD_SANITIZER) && !defined(LEAK_SANITIZER) && \ 48 !defined(THREAD_SANITIZER) && !defined(LEAK_SANITIZER) && \
51 !BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) 49 !BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
52 50
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 138
141 #endif // LIBC_GLIBC && !USE_TCMALLOC 139 #endif // LIBC_GLIBC && !USE_TCMALLOC
142 140
143 #endif // !*_SANITIZER 141 #endif // !*_SANITIZER
144 142
145 void EnableTerminationOnHeapCorruption() { 143 void EnableTerminationOnHeapCorruption() {
146 // On Linux, there nothing to do AFAIK. 144 // On Linux, there nothing to do AFAIK.
147 } 145 }
148 146
149 void EnableTerminationOnOutOfMemory() { 147 void EnableTerminationOnOutOfMemory() {
150 #if defined(OS_ANDROID)
151 // Android doesn't support setting a new handler.
152 DLOG(WARNING) << "Not feasible.";
153 #else
154 // Set the new-out of memory handler. 148 // Set the new-out of memory handler.
155 std::set_new_handler(&OnNoMemory); 149 std::set_new_handler(&OnNoMemory);
156 // If we're using glibc's allocator, the above functions will override 150 // If we're using glibc's allocator, the above functions will override
157 // malloc and friends and make them die on out of memory. 151 // malloc and friends and make them die on out of memory.
158 #endif
159 152
160 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) 153 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
161 allocator::SetCallNewHandlerOnMallocFailure(true); 154 allocator::SetCallNewHandlerOnMallocFailure(true);
162 #elif defined(USE_TCMALLOC) 155 #elif defined(USE_TCMALLOC)
163 // For tcmalloc, we need to tell it to behave like new. 156 // For tcmalloc, we need to tell it to behave like new.
164 tc_set_new_mode(1); 157 tc_set_new_mode(1);
165 #endif 158 #endif
166 } 159 }
167 160
168 // NOTE: This is not the only version of this function in the source: 161 // NOTE: This is not the only version of this function in the source:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 *result = malloc(size); 203 *result = malloc(size);
211 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) 204 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
212 *result = __libc_malloc(size); 205 *result = __libc_malloc(size);
213 #elif defined(USE_TCMALLOC) 206 #elif defined(USE_TCMALLOC)
214 *result = tc_malloc_skip_new_handler(size); 207 *result = tc_malloc_skip_new_handler(size);
215 #endif 208 #endif
216 return *result != NULL; 209 return *result != NULL;
217 } 210 }
218 211
219 } // namespace base 212 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/process/memory_unittest.cc » ('j') | base/process/memory_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698