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

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

Issue 2601573002: mac: Hook up allocator shim. (Closed)
Patch Set: Clean up. Created 3 years, 12 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
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 <CoreFoundation/CoreFoundation.h> 7 #include <CoreFoundation/CoreFoundation.h>
8 #import <Foundation/Foundation.h> 8 #import <Foundation/Foundation.h>
9 #include <errno.h> 9 #include <errno.h>
10 #include <mach/mach.h> 10 #include <mach/mach.h>
11 #include <mach/mach_vm.h> 11 #include <mach/mach_vm.h>
12 #include <malloc/malloc.h> 12 #include <malloc/malloc.h>
13 #import <objc/runtime.h> 13 #import <objc/runtime.h>
14 #include <stddef.h> 14 #include <stddef.h>
15 15
16 #include <new> 16 #include <new>
17 17
18 #include "base/allocator/allocator_shim.h"
19 #include "base/allocator/features.h"
18 #include "base/lazy_instance.h" 20 #include "base/lazy_instance.h"
19 #include "base/logging.h" 21 #include "base/logging.h"
20 #include "base/mac/mac_util.h" 22 #include "base/mac/mac_util.h"
21 #include "base/mac/mach_logging.h" 23 #include "base/mac/mach_logging.h"
22 #include "base/scoped_clear_errno.h" 24 #include "base/scoped_clear_errno.h"
23 #include "build/build_config.h" 25 #include "build/build_config.h"
24 #include "third_party/apple_apsl/CFBase.h" 26 #include "third_party/apple_apsl/CFBase.h"
25 #include "third_party/apple_apsl/malloc.h" 27 #include "third_party/apple_apsl/malloc.h"
26 28
27 namespace base { 29 namespace base {
28 30
29 void EnableTerminationOnHeapCorruption() { 31 void EnableTerminationOnHeapCorruption() {
30 #if !ARCH_CPU_64_BITS 32 #if !ARCH_CPU_64_BITS
31 DLOG(WARNING) << "EnableTerminationOnHeapCorruption only works on 64-bit"; 33 DLOG(WARNING) << "EnableTerminationOnHeapCorruption only works on 64-bit";
32 #endif 34 #endif
33 } 35 }
34 36
35 // ------------------------------------------------------------------------
36
37 namespace {
38
39 bool g_oom_killer_enabled;
40
41 #if !defined(ADDRESS_SANITIZER)
42
43 // Starting with Mac OS X 10.7, the zone allocators set up by the system are 37 // Starting with Mac OS X 10.7, the zone allocators set up by the system are
44 // read-only, to prevent them from being overwritten in an attack. However, 38 // read-only, to prevent them from being overwritten in an attack. However,
45 // blindly unprotecting and reprotecting the zone allocators fails with 39 // blindly unprotecting and reprotecting the zone allocators fails with
46 // GuardMalloc because GuardMalloc sets up its zone allocator using a block of 40 // GuardMalloc because GuardMalloc sets up its zone allocator using a block of
47 // memory in its bss. Explicit saving/restoring of the protection is required. 41 // memory in its bss. Explicit saving/restoring of the protection is required.
48 // 42 //
49 // This function takes a pointer to a malloc zone, de-protects it if necessary, 43 // This function takes a pointer to a malloc zone, de-protects it if necessary,
50 // and returns (in the out parameters) a region of memory (if any) to be 44 // and returns (in the out parameters) a region of memory (if any) to be
51 // re-protected when modifications are complete. This approach assumes that 45 // re-protected when modifications are complete. This approach assumes that
52 // there is no contention for the protection of this memory. 46 // there is no contention for the protection of this memory.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 *reprotection_value = info.protection; 85 *reprotection_value = info.protection;
92 result = mach_vm_protect(mach_task_self(), 86 result = mach_vm_protect(mach_task_self(),
93 *reprotection_start, 87 *reprotection_start,
94 *reprotection_length, 88 *reprotection_length,
95 false, 89 false,
96 info.protection | VM_PROT_WRITE); 90 info.protection | VM_PROT_WRITE);
97 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; 91 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
98 } 92 }
99 } 93 }
100 94
95 // ------------------------------------------------------------------------
96
97 namespace {
98
99 bool g_oom_killer_enabled = false;
100
101 #if !defined(ADDRESS_SANITIZER)
102
101 // === C malloc/calloc/valloc/realloc/posix_memalign === 103 // === C malloc/calloc/valloc/realloc/posix_memalign ===
102 104
103 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, 105 typedef void* (*malloc_type)(struct _malloc_zone_t* zone,
104 size_t size); 106 size_t size);
105 typedef void* (*calloc_type)(struct _malloc_zone_t* zone, 107 typedef void* (*calloc_type)(struct _malloc_zone_t* zone,
106 size_t num_items, 108 size_t num_items,
107 size_t size); 109 size_t size);
108 typedef void* (*valloc_type)(struct _malloc_zone_t* zone, 110 typedef void* (*valloc_type)(struct _malloc_zone_t* zone,
109 size_t size); 111 size_t size);
110 typedef void (*free_type)(struct _malloc_zone_t* zone, 112 typedef void (*free_type)(struct _malloc_zone_t* zone,
111 void* ptr); 113 void* ptr);
112 typedef void* (*realloc_type)(struct _malloc_zone_t* zone, 114 typedef void* (*realloc_type)(struct _malloc_zone_t* zone,
113 void* ptr, 115 void* ptr,
114 size_t size); 116 size_t size);
115 typedef void* (*memalign_type)(struct _malloc_zone_t* zone, 117 typedef void* (*memalign_type)(struct _malloc_zone_t* zone,
116 size_t alignment, 118 size_t alignment,
117 size_t size); 119 size_t size);
118 120
119 malloc_type g_old_malloc;
120 calloc_type g_old_calloc;
121 valloc_type g_old_valloc;
122 free_type g_old_free;
123 realloc_type g_old_realloc;
124 memalign_type g_old_memalign;
125
126 malloc_type g_old_malloc_purgeable; 121 malloc_type g_old_malloc_purgeable;
127 calloc_type g_old_calloc_purgeable; 122 calloc_type g_old_calloc_purgeable;
128 valloc_type g_old_valloc_purgeable; 123 valloc_type g_old_valloc_purgeable;
129 free_type g_old_free_purgeable; 124 free_type g_old_free_purgeable;
130 realloc_type g_old_realloc_purgeable; 125 realloc_type g_old_realloc_purgeable;
131 memalign_type g_old_memalign_purgeable; 126 memalign_type g_old_memalign_purgeable;
132
133 void* oom_killer_malloc(struct _malloc_zone_t* zone,
Robert Sesek 2017/01/03 19:09:07 I think this may break OOM crash bucketing on Mac,
134 size_t size) {
135 void* result = g_old_malloc(zone, size);
136 if (!result && size)
137 TerminateBecauseOutOfMemory(size);
138 return result;
139 }
140
141 void* oom_killer_calloc(struct _malloc_zone_t* zone,
142 size_t num_items,
143 size_t size) {
144 void* result = g_old_calloc(zone, num_items, size);
145 if (!result && num_items && size)
146 TerminateBecauseOutOfMemory(num_items * size);
147 return result;
148 }
149
150 void* oom_killer_valloc(struct _malloc_zone_t* zone,
151 size_t size) {
152 void* result = g_old_valloc(zone, size);
153 if (!result && size)
154 TerminateBecauseOutOfMemory(size);
155 return result;
156 }
157
158 void oom_killer_free(struct _malloc_zone_t* zone,
159 void* ptr) {
160 g_old_free(zone, ptr);
161 }
162
163 void* oom_killer_realloc(struct _malloc_zone_t* zone,
164 void* ptr,
165 size_t size) {
166 void* result = g_old_realloc(zone, ptr, size);
167 if (!result && size)
168 TerminateBecauseOutOfMemory(size);
169 return result;
170 }
171
172 void* oom_killer_memalign(struct _malloc_zone_t* zone,
173 size_t alignment,
174 size_t size) {
175 void* result = g_old_memalign(zone, alignment, size);
176 // Only die if posix_memalign would have returned ENOMEM, since there are
177 // other reasons why NULL might be returned (see
178 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ).
179 if (!result && size && alignment >= sizeof(void*) &&
180 (alignment & (alignment - 1)) == 0) {
181 TerminateBecauseOutOfMemory(size);
182 }
183 return result;
184 }
185
186 void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone, 127 void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone,
187 size_t size) { 128 size_t size) {
188 void* result = g_old_malloc_purgeable(zone, size); 129 void* result = g_old_malloc_purgeable(zone, size);
189 if (!result && size) 130 if (!result && size)
190 TerminateBecauseOutOfMemory(size); 131 TerminateBecauseOutOfMemory(size);
191 return result; 132 return result;
192 } 133 }
193 134
194 void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone, 135 void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone,
195 size_t num_items, 136 size_t num_items,
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 { 242 {
302 id result = g_old_allocWithZone(self, _cmd, zone); 243 id result = g_old_allocWithZone(self, _cmd, zone);
303 if (!result) 244 if (!result)
304 TerminateBecauseOutOfMemory(0); 245 TerminateBecauseOutOfMemory(0);
305 return result; 246 return result;
306 } 247 }
307 248
308 } // namespace 249 } // namespace
309 250
310 bool UncheckedMalloc(size_t size, void** result) { 251 bool UncheckedMalloc(size_t size, void** result) {
311 #if defined(ADDRESS_SANITIZER) 252 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
253 *result = allocator::UncheckedAlloc(size);
254 #else
312 *result = malloc(size); 255 *result = malloc(size);
313 #else 256 #endif
314 if (g_old_malloc) {
315 *result = g_old_malloc(malloc_default_zone(), size);
316 } else {
317 *result = malloc(size);
318 }
319 #endif // defined(ADDRESS_SANITIZER)
320
321 return *result != NULL; 257 return *result != NULL;
322 } 258 }
323 259
324 bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
325 #if defined(ADDRESS_SANITIZER)
326 *result = calloc(num_items, size);
327 #else
328 if (g_old_calloc) {
329 *result = g_old_calloc(malloc_default_zone(), num_items, size);
330 } else {
331 *result = calloc(num_items, size);
332 }
333 #endif // defined(ADDRESS_SANITIZER)
334
335 return *result != NULL;
336 }
337
338 void* UncheckedMalloc(size_t size) {
339 void* address;
340 return UncheckedMalloc(size, &address) ? address : NULL;
341 }
342
343 void* UncheckedCalloc(size_t num_items, size_t size) {
344 void* address;
345 return UncheckedCalloc(num_items, size, &address) ? address : NULL;
346 }
347
348 void EnableTerminationOnOutOfMemory() { 260 void EnableTerminationOnOutOfMemory() {
349 if (g_oom_killer_enabled) 261 if (g_oom_killer_enabled)
350 return; 262 return;
351 263
352 g_oom_killer_enabled = true; 264 g_oom_killer_enabled = true;
353 265
354 // === C malloc/calloc/valloc/realloc/posix_memalign === 266 // === C malloc/calloc/valloc/realloc/posix_memalign ===
355 267
356 // This approach is not perfect, as requests for amounts of memory larger than 268 // This approach is not perfect, as requests for amounts of memory larger than
357 // MALLOC_ABSOLUTE_MAX_SIZE (currently SIZE_T_MAX - (2 * PAGE_SIZE)) will 269 // MALLOC_ABSOLUTE_MAX_SIZE (currently SIZE_T_MAX - (2 * PAGE_SIZE)) will
358 // still fail with a NULL rather than dying (see 270 // still fail with a NULL rather than dying (see
359 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c for details). 271 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c for details).
360 // Unfortunately, it's the best we can do. Also note that this does not affect 272 // Unfortunately, it's the best we can do. Also note that this only affects
361 // allocations from non-default zones. 273 // allocations from the purgeable zone. Allocations from the default zone are
274 // handled by base/allocator/allocator_shim_override_mac_symbols.h.
362 275
363 #if !defined(ADDRESS_SANITIZER) 276 #if !defined(ADDRESS_SANITIZER)
364 // Don't do anything special on OOM for the malloc zones replaced by 277 // Don't do anything special on OOM for the malloc zones replaced by
365 // AddressSanitizer, as modifying or protecting them may not work correctly. 278 // AddressSanitizer, as modifying or protecting them may not work correctly.
366 279
367 CHECK(!g_old_malloc && !g_old_calloc && !g_old_valloc && !g_old_realloc &&
368 !g_old_memalign) << "Old allocators unexpectedly non-null";
369
370 CHECK(!g_old_malloc_purgeable && !g_old_calloc_purgeable && 280 CHECK(!g_old_malloc_purgeable && !g_old_calloc_purgeable &&
371 !g_old_valloc_purgeable && !g_old_realloc_purgeable && 281 !g_old_valloc_purgeable && !g_old_realloc_purgeable &&
372 !g_old_memalign_purgeable) << "Old allocators unexpectedly non-null"; 282 !g_old_memalign_purgeable) << "Old allocators unexpectedly non-null";
373 283
374 ChromeMallocZone* default_zone =
375 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
376 ChromeMallocZone* purgeable_zone = 284 ChromeMallocZone* purgeable_zone =
377 reinterpret_cast<ChromeMallocZone*>(malloc_default_purgeable_zone()); 285 reinterpret_cast<ChromeMallocZone*>(malloc_default_purgeable_zone());
378 286
379 mach_vm_address_t default_reprotection_start = 0;
380 mach_vm_size_t default_reprotection_length = 0;
381 vm_prot_t default_reprotection_value = VM_PROT_NONE;
382 DeprotectMallocZone(default_zone,
383 &default_reprotection_start,
384 &default_reprotection_length,
385 &default_reprotection_value);
386
387 mach_vm_address_t purgeable_reprotection_start = 0; 287 mach_vm_address_t purgeable_reprotection_start = 0;
388 mach_vm_size_t purgeable_reprotection_length = 0; 288 mach_vm_size_t purgeable_reprotection_length = 0;
389 vm_prot_t purgeable_reprotection_value = VM_PROT_NONE; 289 vm_prot_t purgeable_reprotection_value = VM_PROT_NONE;
390 if (purgeable_zone) { 290 if (purgeable_zone) {
391 DeprotectMallocZone(purgeable_zone, 291 DeprotectMallocZone(purgeable_zone,
392 &purgeable_reprotection_start, 292 &purgeable_reprotection_start,
393 &purgeable_reprotection_length, 293 &purgeable_reprotection_length,
394 &purgeable_reprotection_value); 294 &purgeable_reprotection_value);
395 }
396
397 // Default zone
398
399 g_old_malloc = default_zone->malloc;
400 g_old_calloc = default_zone->calloc;
401 g_old_valloc = default_zone->valloc;
402 g_old_free = default_zone->free;
403 g_old_realloc = default_zone->realloc;
404 CHECK(g_old_malloc && g_old_calloc && g_old_valloc && g_old_free &&
405 g_old_realloc)
406 << "Failed to get system allocation functions.";
407
408 default_zone->malloc = oom_killer_malloc;
409 default_zone->calloc = oom_killer_calloc;
410 default_zone->valloc = oom_killer_valloc;
411 default_zone->free = oom_killer_free;
412 default_zone->realloc = oom_killer_realloc;
413
414 if (default_zone->version >= 5) {
415 g_old_memalign = default_zone->memalign;
416 if (g_old_memalign)
417 default_zone->memalign = oom_killer_memalign;
418 }
419
420 // Purgeable zone (if it exists)
421
422 if (purgeable_zone) {
423 g_old_malloc_purgeable = purgeable_zone->malloc; 295 g_old_malloc_purgeable = purgeable_zone->malloc;
424 g_old_calloc_purgeable = purgeable_zone->calloc; 296 g_old_calloc_purgeable = purgeable_zone->calloc;
425 g_old_valloc_purgeable = purgeable_zone->valloc; 297 g_old_valloc_purgeable = purgeable_zone->valloc;
426 g_old_free_purgeable = purgeable_zone->free; 298 g_old_free_purgeable = purgeable_zone->free;
427 g_old_realloc_purgeable = purgeable_zone->realloc; 299 g_old_realloc_purgeable = purgeable_zone->realloc;
428 CHECK(g_old_malloc_purgeable && g_old_calloc_purgeable && 300 CHECK(g_old_malloc_purgeable && g_old_calloc_purgeable &&
429 g_old_valloc_purgeable && g_old_free_purgeable && 301 g_old_valloc_purgeable && g_old_free_purgeable &&
430 g_old_realloc_purgeable) 302 g_old_realloc_purgeable)
431 << "Failed to get system allocation functions."; 303 << "Failed to get system allocation functions.";
432 304
433 purgeable_zone->malloc = oom_killer_malloc_purgeable; 305 purgeable_zone->malloc = oom_killer_malloc_purgeable;
434 purgeable_zone->calloc = oom_killer_calloc_purgeable; 306 purgeable_zone->calloc = oom_killer_calloc_purgeable;
435 purgeable_zone->valloc = oom_killer_valloc_purgeable; 307 purgeable_zone->valloc = oom_killer_valloc_purgeable;
436 purgeable_zone->free = oom_killer_free_purgeable; 308 purgeable_zone->free = oom_killer_free_purgeable;
437 purgeable_zone->realloc = oom_killer_realloc_purgeable; 309 purgeable_zone->realloc = oom_killer_realloc_purgeable;
438 310
439 if (purgeable_zone->version >= 5) { 311 if (purgeable_zone->version >= 5) {
440 g_old_memalign_purgeable = purgeable_zone->memalign; 312 g_old_memalign_purgeable = purgeable_zone->memalign;
441 if (g_old_memalign_purgeable) 313 if (g_old_memalign_purgeable)
442 purgeable_zone->memalign = oom_killer_memalign_purgeable; 314 purgeable_zone->memalign = oom_killer_memalign_purgeable;
443 } 315 }
444 } 316 }
445 317
446 // Restore protection if it was active.
447
448 if (default_reprotection_start) {
449 kern_return_t result = mach_vm_protect(mach_task_self(),
450 default_reprotection_start,
451 default_reprotection_length,
452 false,
453 default_reprotection_value);
454 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
455 }
456
457 if (purgeable_reprotection_start) { 318 if (purgeable_reprotection_start) {
458 kern_return_t result = mach_vm_protect(mach_task_self(), 319 kern_return_t result = mach_vm_protect(mach_task_self(),
459 purgeable_reprotection_start, 320 purgeable_reprotection_start,
460 purgeable_reprotection_length, 321 purgeable_reprotection_length,
461 false, 322 false,
462 purgeable_reprotection_value); 323 purgeable_reprotection_value);
463 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; 324 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
464 } 325 }
465 #endif 326 #endif
466 327
(...skipping 14 matching lines...) Expand all
481 // to batch_malloc is malloc_zone_batch_malloc, which is specific to the 342 // to batch_malloc is malloc_zone_batch_malloc, which is specific to the
482 // system's malloc implementation. It's unlikely that anyone's even heard of 343 // system's malloc implementation. It's unlikely that anyone's even heard of
483 // it. 344 // it.
484 345
485 // === C++ operator new === 346 // === C++ operator new ===
486 347
487 // Yes, operator new does call through to malloc, but this will catch failures 348 // Yes, operator new does call through to malloc, but this will catch failures
488 // that our imperfect handling of malloc cannot. 349 // that our imperfect handling of malloc cannot.
489 350
490 std::set_new_handler(oom_killer_new); 351 std::set_new_handler(oom_killer_new);
352 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
353 allocator::SetCallNewHandlerOnMallocFailure(true);
354 #endif
491 355
492 #ifndef ADDRESS_SANITIZER 356 #ifndef ADDRESS_SANITIZER
493 // === Core Foundation CFAllocators === 357 // === Core Foundation CFAllocators ===
494 358
495 // This will not catch allocation done by custom allocators, but will catch 359 // This will not catch allocation done by custom allocators, but will catch
496 // all allocation done by system-provided ones. 360 // all allocation done by system-provided ones.
497 361
498 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && 362 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc &&
499 !g_old_cfallocator_malloc_zone) 363 !g_old_cfallocator_malloc_zone)
500 << "Old allocators unexpectedly non-null"; 364 << "Old allocators unexpectedly non-null";
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 @selector(allocWithZone:)); 407 @selector(allocWithZone:));
544 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( 408 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>(
545 method_getImplementation(orig_method)); 409 method_getImplementation(orig_method));
546 CHECK(g_old_allocWithZone) 410 CHECK(g_old_allocWithZone)
547 << "Failed to get allocWithZone allocation function."; 411 << "Failed to get allocWithZone allocation function.";
548 method_setImplementation(orig_method, 412 method_setImplementation(orig_method,
549 reinterpret_cast<IMP>(oom_killer_allocWithZone)); 413 reinterpret_cast<IMP>(oom_killer_allocWithZone));
550 } 414 }
551 415
552 } // namespace base 416 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698