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

Side by Side Diff: base/process/memory_mac.mm

Issue 895853003: Update from https://crrev.com/314320 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 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/launch_posix.cc ('k') | base/process/memory_unittest.cc » ('j') | 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) 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 // AddressSanitizer handles heap corruption, and on 64 bit Macs, the malloc
8 // system automatically abort()s on heap corruption.
9 #if !defined(ADDRESS_SANITIZER) && ARCH_CPU_32_BITS
10 #define HANDLE_MEMORY_CORRUPTION_MANUALLY
11 #endif
12
13 #include <CoreFoundation/CoreFoundation.h> 7 #include <CoreFoundation/CoreFoundation.h>
14 #include <errno.h> 8 #include <errno.h>
15 #include <mach/mach.h> 9 #include <mach/mach.h>
16 #include <mach/mach_vm.h> 10 #include <mach/mach_vm.h>
17 #include <malloc/malloc.h> 11 #include <malloc/malloc.h>
18 #import <objc/runtime.h> 12 #import <objc/runtime.h>
19 13
20 #include <new> 14 #include <new>
21 15
22 #include "base/lazy_instance.h" 16 #include "base/lazy_instance.h"
23 #include "base/logging.h" 17 #include "base/logging.h"
24 #include "base/mac/mac_util.h" 18 #include "base/mac/mac_util.h"
25 #include "base/mac/mach_logging.h" 19 #include "base/mac/mach_logging.h"
26 #include "base/scoped_clear_errno.h" 20 #include "base/scoped_clear_errno.h"
27 #include "third_party/apple_apsl/CFBase.h" 21 #include "third_party/apple_apsl/CFBase.h"
28 #include "third_party/apple_apsl/malloc.h" 22 #include "third_party/apple_apsl/malloc.h"
29 23
30 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
31 #include <dlfcn.h>
32 #include <mach-o/nlist.h>
33
34 #include "base/threading/thread_local.h"
35 #include "third_party/mach_override/mach_override.h"
36 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
37
38 namespace base { 24 namespace base {
39 25
40 // These are helpers for EnableTerminationOnHeapCorruption, which is a no-op
41 // on 64 bit Macs.
42 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
43 namespace {
44
45 // Finds the library path for malloc() and thus the libC part of libSystem,
46 // which in Lion is in a separate image.
47 const char* LookUpLibCPath() {
48 const void* addr = reinterpret_cast<void*>(&malloc);
49
50 Dl_info info;
51 if (dladdr(addr, &info))
52 return info.dli_fname;
53
54 DLOG(WARNING) << "Could not find image path for malloc()";
55 return NULL;
56 }
57
58 typedef void(*malloc_error_break_t)(void);
59 malloc_error_break_t g_original_malloc_error_break = NULL;
60
61 // Returns the function pointer for malloc_error_break. This symbol is declared
62 // as __private_extern__ and cannot be dlsym()ed. Instead, use nlist() to
63 // get it.
64 malloc_error_break_t LookUpMallocErrorBreak() {
65 const char* lib_c_path = LookUpLibCPath();
66 if (!lib_c_path)
67 return NULL;
68
69 // Only need to look up two symbols, but nlist() requires a NULL-terminated
70 // array and takes no count.
71 struct nlist nl[3];
72 bzero(&nl, sizeof(nl));
73
74 // The symbol to find.
75 nl[0].n_un.n_name = const_cast<char*>("_malloc_error_break");
76
77 // A reference symbol by which the address of the desired symbol will be
78 // calculated.
79 nl[1].n_un.n_name = const_cast<char*>("_malloc");
80
81 int rv = nlist(lib_c_path, nl);
82 if (rv != 0 || nl[0].n_type == N_UNDF || nl[1].n_type == N_UNDF) {
83 return NULL;
84 }
85
86 // nlist() returns addresses as offsets in the image, not the instruction
87 // pointer in memory. Use the known in-memory address of malloc()
88 // to compute the offset for malloc_error_break().
89 uintptr_t reference_addr = reinterpret_cast<uintptr_t>(&malloc);
90 reference_addr -= nl[1].n_value;
91 reference_addr += nl[0].n_value;
92
93 return reinterpret_cast<malloc_error_break_t>(reference_addr);
94 }
95
96 // Combines ThreadLocalBoolean with AutoReset. It would be convenient
97 // to compose ThreadLocalPointer<bool> with base::AutoReset<bool>, but that
98 // would require allocating some storage for the bool.
99 class ThreadLocalBooleanAutoReset {
100 public:
101 ThreadLocalBooleanAutoReset(ThreadLocalBoolean* tlb, bool new_value)
102 : scoped_tlb_(tlb),
103 original_value_(tlb->Get()) {
104 scoped_tlb_->Set(new_value);
105 }
106 ~ThreadLocalBooleanAutoReset() {
107 scoped_tlb_->Set(original_value_);
108 }
109
110 private:
111 ThreadLocalBoolean* scoped_tlb_;
112 bool original_value_;
113
114 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBooleanAutoReset);
115 };
116
117 base::LazyInstance<ThreadLocalBoolean>::Leaky
118 g_unchecked_alloc = LAZY_INSTANCE_INITIALIZER;
119
120 // NOTE(shess): This is called when the malloc library noticed that the heap
121 // is fubar. Avoid calls which will re-enter the malloc library.
122 void CrMallocErrorBreak() {
123 g_original_malloc_error_break();
124
125 // Out of memory is certainly not heap corruption, and not necessarily
126 // something for which the process should be terminated. Leave that decision
127 // to the OOM killer.
128 if (errno == ENOMEM)
129 return;
130
131 // The malloc library attempts to log to ASL (syslog) before calling this
132 // code, which fails accessing a Unix-domain socket when sandboxed. The
133 // failed socket results in writing to a -1 fd, leaving EBADF in errno. If
134 // UncheckedMalloc() is on the stack, for large allocations (15k and up) only
135 // an OOM failure leads here. Smaller allocations could also arrive here due
136 // to freelist corruption, but there is no way to distinguish that from OOM at
137 // this point.
138 //
139 // NOTE(shess): I hypothesize that EPERM case in 10.9 is the same root cause
140 // as EBADF. Unfortunately, 10.9's opensource releases don't include malloc
141 // source code at this time.
142 // <http://crbug.com/312234>
143 if ((errno == EBADF || errno == EPERM) && g_unchecked_alloc.Get().Get())
144 return;
145
146 // A unit test checks this error message, so it needs to be in release builds.
147 char buf[1024] =
148 "Terminating process due to a potential for future heap corruption: "
149 "errno=";
150 char errnobuf[] = {
151 '0' + ((errno / 100) % 10),
152 '0' + ((errno / 10) % 10),
153 '0' + (errno % 10),
154 '\000'
155 };
156 COMPILE_ASSERT(ELAST <= 999, errno_too_large_to_encode);
157 strlcat(buf, errnobuf, sizeof(buf));
158 RAW_LOG(ERROR, buf);
159
160 // Crash by writing to NULL+errno to allow analyzing errno from
161 // crash dump info (setting a breakpad key would re-enter the malloc
162 // library). Max documented errno in intro(2) is actually 102, but
163 // it really just needs to be "small" to stay on the right vm page.
164 const int kMaxErrno = 256;
165 char* volatile death_ptr = NULL;
166 death_ptr += std::min(errno, kMaxErrno);
167 *death_ptr = '!';
168 }
169
170 } // namespace
171 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
172
173 void EnableTerminationOnHeapCorruption() { 26 void EnableTerminationOnHeapCorruption() {
174 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY) 27 #if !ARCH_CPU_64_BITS
175 // Only override once, otherwise CrMallocErrorBreak() will recurse 28 DLOG(WARNING) << "EnableTerminationOnHeapCorruption only works on 64-bit";
176 // to itself. 29 #endif
177 if (g_original_malloc_error_break)
178 return;
179
180 malloc_error_break_t malloc_error_break = LookUpMallocErrorBreak();
181 if (!malloc_error_break) {
182 DLOG(WARNING) << "Could not find malloc_error_break";
183 return;
184 }
185
186 mach_error_t err = mach_override_ptr(
187 (void*)malloc_error_break,
188 (void*)&CrMallocErrorBreak,
189 (void**)&g_original_malloc_error_break);
190
191 if (err != err_none)
192 DLOG(WARNING) << "Could not override malloc_error_break; error = " << err;
193 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
194 } 30 }
195 31
196 // ------------------------------------------------------------------------ 32 // ------------------------------------------------------------------------
197 33
198 namespace { 34 namespace {
199 35
200 bool g_oom_killer_enabled; 36 bool g_oom_killer_enabled;
201 37
202 #if !defined(ADDRESS_SANITIZER) 38 #if !defined(ADDRESS_SANITIZER)
203 39
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 122
287 malloc_type g_old_malloc_purgeable; 123 malloc_type g_old_malloc_purgeable;
288 calloc_type g_old_calloc_purgeable; 124 calloc_type g_old_calloc_purgeable;
289 valloc_type g_old_valloc_purgeable; 125 valloc_type g_old_valloc_purgeable;
290 free_type g_old_free_purgeable; 126 free_type g_old_free_purgeable;
291 realloc_type g_old_realloc_purgeable; 127 realloc_type g_old_realloc_purgeable;
292 memalign_type g_old_memalign_purgeable; 128 memalign_type g_old_memalign_purgeable;
293 129
294 void* oom_killer_malloc(struct _malloc_zone_t* zone, 130 void* oom_killer_malloc(struct _malloc_zone_t* zone,
295 size_t size) { 131 size_t size) {
296 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
297 ScopedClearErrno clear_errno;
298 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
299 void* result = g_old_malloc(zone, size); 132 void* result = g_old_malloc(zone, size);
300 if (!result && size) 133 if (!result && size)
301 debug::BreakDebugger(); 134 debug::BreakDebugger();
302 return result; 135 return result;
303 } 136 }
304 137
305 void* oom_killer_calloc(struct _malloc_zone_t* zone, 138 void* oom_killer_calloc(struct _malloc_zone_t* zone,
306 size_t num_items, 139 size_t num_items,
307 size_t size) { 140 size_t size) {
308 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
309 ScopedClearErrno clear_errno;
310 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
311 void* result = g_old_calloc(zone, num_items, size); 141 void* result = g_old_calloc(zone, num_items, size);
312 if (!result && num_items && size) 142 if (!result && num_items && size)
313 debug::BreakDebugger(); 143 debug::BreakDebugger();
314 return result; 144 return result;
315 } 145 }
316 146
317 void* oom_killer_valloc(struct _malloc_zone_t* zone, 147 void* oom_killer_valloc(struct _malloc_zone_t* zone,
318 size_t size) { 148 size_t size) {
319 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
320 ScopedClearErrno clear_errno;
321 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
322 void* result = g_old_valloc(zone, size); 149 void* result = g_old_valloc(zone, size);
323 if (!result && size) 150 if (!result && size)
324 debug::BreakDebugger(); 151 debug::BreakDebugger();
325 return result; 152 return result;
326 } 153 }
327 154
328 void oom_killer_free(struct _malloc_zone_t* zone, 155 void oom_killer_free(struct _malloc_zone_t* zone,
329 void* ptr) { 156 void* ptr) {
330 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
331 ScopedClearErrno clear_errno;
332 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
333 g_old_free(zone, ptr); 157 g_old_free(zone, ptr);
334 } 158 }
335 159
336 void* oom_killer_realloc(struct _malloc_zone_t* zone, 160 void* oom_killer_realloc(struct _malloc_zone_t* zone,
337 void* ptr, 161 void* ptr,
338 size_t size) { 162 size_t size) {
339 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
340 ScopedClearErrno clear_errno;
341 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
342 void* result = g_old_realloc(zone, ptr, size); 163 void* result = g_old_realloc(zone, ptr, size);
343 if (!result && size) 164 if (!result && size)
344 debug::BreakDebugger(); 165 debug::BreakDebugger();
345 return result; 166 return result;
346 } 167 }
347 168
348 void* oom_killer_memalign(struct _malloc_zone_t* zone, 169 void* oom_killer_memalign(struct _malloc_zone_t* zone,
349 size_t alignment, 170 size_t alignment,
350 size_t size) { 171 size_t size) {
351 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
352 ScopedClearErrno clear_errno;
353 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
354 void* result = g_old_memalign(zone, alignment, size); 172 void* result = g_old_memalign(zone, alignment, size);
355 // Only die if posix_memalign would have returned ENOMEM, since there are 173 // Only die if posix_memalign would have returned ENOMEM, since there are
356 // other reasons why NULL might be returned (see 174 // other reasons why NULL might be returned (see
357 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ). 175 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ).
358 if (!result && size && alignment >= sizeof(void*) 176 if (!result && size && alignment >= sizeof(void*) &&
359 && (alignment & (alignment - 1)) == 0) { 177 (alignment & (alignment - 1)) == 0) {
360 debug::BreakDebugger(); 178 debug::BreakDebugger();
361 } 179 }
362 return result; 180 return result;
363 } 181 }
364 182
365 void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone, 183 void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone,
366 size_t size) { 184 size_t size) {
367 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
368 ScopedClearErrno clear_errno;
369 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
370 void* result = g_old_malloc_purgeable(zone, size); 185 void* result = g_old_malloc_purgeable(zone, size);
371 if (!result && size) 186 if (!result && size)
372 debug::BreakDebugger(); 187 debug::BreakDebugger();
373 return result; 188 return result;
374 } 189 }
375 190
376 void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone, 191 void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone,
377 size_t num_items, 192 size_t num_items,
378 size_t size) { 193 size_t size) {
379 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
380 ScopedClearErrno clear_errno;
381 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
382 void* result = g_old_calloc_purgeable(zone, num_items, size); 194 void* result = g_old_calloc_purgeable(zone, num_items, size);
383 if (!result && num_items && size) 195 if (!result && num_items && size)
384 debug::BreakDebugger(); 196 debug::BreakDebugger();
385 return result; 197 return result;
386 } 198 }
387 199
388 void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone, 200 void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone,
389 size_t size) { 201 size_t size) {
390 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
391 ScopedClearErrno clear_errno;
392 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
393 void* result = g_old_valloc_purgeable(zone, size); 202 void* result = g_old_valloc_purgeable(zone, size);
394 if (!result && size) 203 if (!result && size)
395 debug::BreakDebugger(); 204 debug::BreakDebugger();
396 return result; 205 return result;
397 } 206 }
398 207
399 void oom_killer_free_purgeable(struct _malloc_zone_t* zone, 208 void oom_killer_free_purgeable(struct _malloc_zone_t* zone,
400 void* ptr) { 209 void* ptr) {
401 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
402 ScopedClearErrno clear_errno;
403 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
404 g_old_free_purgeable(zone, ptr); 210 g_old_free_purgeable(zone, ptr);
405 } 211 }
406 212
407 void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone, 213 void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone,
408 void* ptr, 214 void* ptr,
409 size_t size) { 215 size_t size) {
410 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
411 ScopedClearErrno clear_errno;
412 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
413 void* result = g_old_realloc_purgeable(zone, ptr, size); 216 void* result = g_old_realloc_purgeable(zone, ptr, size);
414 if (!result && size) 217 if (!result && size)
415 debug::BreakDebugger(); 218 debug::BreakDebugger();
416 return result; 219 return result;
417 } 220 }
418 221
419 void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone, 222 void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone,
420 size_t alignment, 223 size_t alignment,
421 size_t size) { 224 size_t size) {
422 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
423 ScopedClearErrno clear_errno;
424 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
425 void* result = g_old_memalign_purgeable(zone, alignment, size); 225 void* result = g_old_memalign_purgeable(zone, alignment, size);
426 // Only die if posix_memalign would have returned ENOMEM, since there are 226 // Only die if posix_memalign would have returned ENOMEM, since there are
427 // other reasons why NULL might be returned (see 227 // other reasons why NULL might be returned (see
428 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ). 228 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ).
429 if (!result && size && alignment >= sizeof(void*) 229 if (!result && size && alignment >= sizeof(void*)
430 && (alignment & (alignment - 1)) == 0) { 230 && (alignment & (alignment - 1)) == 0) {
431 debug::BreakDebugger(); 231 debug::BreakDebugger();
432 } 232 }
433 return result; 233 return result;
434 } 234 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 return result; 314 return result;
515 } 315 }
516 316
517 } // namespace 317 } // namespace
518 318
519 bool UncheckedMalloc(size_t size, void** result) { 319 bool UncheckedMalloc(size_t size, void** result) {
520 #if defined(ADDRESS_SANITIZER) 320 #if defined(ADDRESS_SANITIZER)
521 *result = malloc(size); 321 *result = malloc(size);
522 #else 322 #else
523 if (g_old_malloc) { 323 if (g_old_malloc) {
524 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
525 ScopedClearErrno clear_errno;
526 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true);
527 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
528 *result = g_old_malloc(malloc_default_zone(), size); 324 *result = g_old_malloc(malloc_default_zone(), size);
529 } else { 325 } else {
530 *result = malloc(size); 326 *result = malloc(size);
531 } 327 }
532 #endif // defined(ADDRESS_SANITIZER) 328 #endif // defined(ADDRESS_SANITIZER)
533 329
534 return *result != NULL; 330 return *result != NULL;
535 } 331 }
536 332
537 bool UncheckedCalloc(size_t num_items, size_t size, void** result) { 333 bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
538 #if defined(ADDRESS_SANITIZER) 334 #if defined(ADDRESS_SANITIZER)
539 *result = calloc(num_items, size); 335 *result = calloc(num_items, size);
540 #else 336 #else
541 if (g_old_calloc) { 337 if (g_old_calloc) {
542 #if defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
543 ScopedClearErrno clear_errno;
544 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true);
545 #endif // defined(HANDLE_MEMORY_CORRUPTION_MANUALLY)
546 *result = g_old_calloc(malloc_default_zone(), num_items, size); 338 *result = g_old_calloc(malloc_default_zone(), num_items, size);
547 } else { 339 } else {
548 *result = calloc(num_items, size); 340 *result = calloc(num_items, size);
549 } 341 }
550 #endif // defined(ADDRESS_SANITIZER) 342 #endif // defined(ADDRESS_SANITIZER)
551 343
552 return *result != NULL; 344 return *result != NULL;
553 } 345 }
554 346
555 void* UncheckedMalloc(size_t size) { 347 void* UncheckedMalloc(size_t size) {
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 @selector(allocWithZone:)); 552 @selector(allocWithZone:));
761 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( 553 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>(
762 method_getImplementation(orig_method)); 554 method_getImplementation(orig_method));
763 CHECK(g_old_allocWithZone) 555 CHECK(g_old_allocWithZone)
764 << "Failed to get allocWithZone allocation function."; 556 << "Failed to get allocWithZone allocation function.";
765 method_setImplementation(orig_method, 557 method_setImplementation(orig_method,
766 reinterpret_cast<IMP>(oom_killer_allocWithZone)); 558 reinterpret_cast<IMP>(oom_killer_allocWithZone));
767 } 559 }
768 560
769 } // namespace base 561 } // namespace base
OLDNEW
« no previous file with comments | « base/process/launch_posix.cc ('k') | base/process/memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698