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
3 // found in the LICENSE file.
5 #include "base/process/memory.h"
7 #include <CoreFoundation/CoreFoundation.h>
10 #include <mach/mach_vm.h>
11 #include <malloc/malloc.h>
12 #import <objc/runtime.h>
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/mac/mac_util.h"
19 #include "base/scoped_clear_errno.h"
20 #include "third_party/apple_apsl/CFBase.h"
21 #include "third_party/apple_apsl/malloc.h"
25 #include <mach-o/nlist.h>
27 #include "base/threading/thread_local.h"
28 #include "third_party/mach_override/mach_override.h"
29 #endif // ARCH_CPU_32_BITS
33 // These are helpers for EnableTerminationOnHeapCorruption, which is a no-op
38 // Finds the library path for malloc() and thus the libC part of libSystem,
39 // which in Lion is in a separate image.
40 const char* LookUpLibCPath() {
41 const void* addr = reinterpret_cast<void*>(&malloc);
44 if (dladdr(addr, &info))
45 return info.dli_fname;
47 DLOG(WARNING) << "Could not find image path for malloc()";
51 typedef void(*malloc_error_break_t)(void);
52 malloc_error_break_t g_original_malloc_error_break = NULL;
54 // Returns the function pointer for malloc_error_break. This symbol is declared
55 // as __private_extern__ and cannot be dlsym()ed. Instead, use nlist() to
57 malloc_error_break_t LookUpMallocErrorBreak() {
58 const char* lib_c_path = LookUpLibCPath();
62 // Only need to look up two symbols, but nlist() requires a NULL-terminated
63 // array and takes no count.
65 bzero(&nl, sizeof(nl));
67 // The symbol to find.
68 nl[0].n_un.n_name = const_cast<char*>("_malloc_error_break");
70 // A reference symbol by which the address of the desired symbol will be
72 nl[1].n_un.n_name = const_cast<char*>("_malloc");
74 int rv = nlist(lib_c_path, nl);
75 if (rv != 0 || nl[0].n_type == N_UNDF || nl[1].n_type == N_UNDF) {
79 // nlist() returns addresses as offsets in the image, not the instruction
80 // pointer in memory. Use the known in-memory address of malloc()
81 // to compute the offset for malloc_error_break().
82 uintptr_t reference_addr = reinterpret_cast<uintptr_t>(&malloc);
83 reference_addr -= nl[1].n_value;
84 reference_addr += nl[0].n_value;
86 return reinterpret_cast<malloc_error_break_t>(reference_addr);
89 // Combines ThreadLocalBoolean with AutoReset. It would be convenient
90 // to compose ThreadLocalPointer<bool> with base::AutoReset<bool>, but that
91 // would require allocating some storage for the bool.
92 class ThreadLocalBooleanAutoReset {
94 ThreadLocalBooleanAutoReset(ThreadLocalBoolean* tlb, bool new_value)
96 original_value_(tlb->Get()) {
97 scoped_tlb_->Set(new_value);
99 ~ThreadLocalBooleanAutoReset() {
100 scoped_tlb_->Set(original_value_);
104 ThreadLocalBoolean* scoped_tlb_;
105 bool original_value_;
107 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBooleanAutoReset);
110 base::LazyInstance<ThreadLocalBoolean>::Leaky
111 g_unchecked_alloc = LAZY_INSTANCE_INITIALIZER;
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.
115 void CrMallocErrorBreak() {
116 g_original_malloc_error_break();
118 // Out of memory is certainly not heap corruption, and not necessarily
119 // something for which the process should be terminated. Leave that decision
120 // to the OOM killer.
124 // The malloc library attempts to log to ASL (syslog) before calling this
125 // code, which fails accessing a Unix-domain socket when sandboxed. The
126 // failed socket results in writing to a -1 fd, leaving EBADF in errno. If
127 // UncheckedMalloc() is on the stack, for large allocations (15k and up) only
128 // an OOM failure leads here. Smaller allocations could also arrive here due
129 // to freelist corruption, but there is no way to distinguish that from OOM at
132 // NOTE(shess): I hypothesize that EPERM case in 10.9 is the same root cause
133 // as EBADF. Unfortunately, 10.9's opensource releases don't include malloc
134 // source code at this time.
135 // <http://crbug.com/312234>
136 if ((errno == EBADF || errno == EPERM) && g_unchecked_alloc.Get().Get())
139 // A unit test checks this error message, so it needs to be in release builds.
141 "Terminating process due to a potential for future heap corruption: "
144 '0' + ((errno / 100) % 10),
145 '0' + ((errno / 10) % 10),
149 COMPILE_ASSERT(ELAST <= 999, errno_too_large_to_encode);
150 strlcat(buf, errnobuf, sizeof(buf));
153 // Crash by writing to NULL+errno to allow analyzing errno from
154 // crash dump info (setting a breakpad key would re-enter the malloc
155 // library). Max documented errno in intro(2) is actually 102, but
156 // it really just needs to be "small" to stay on the right vm page.
157 const int kMaxErrno = 256;
158 char* volatile death_ptr = NULL;
159 death_ptr += std::min(errno, kMaxErrno);
164 #endif // ARCH_CPU_32_BITS
166 void EnableTerminationOnHeapCorruption() {
167 #if defined(ADDRESS_SANITIZER) || ARCH_CPU_64_BITS
168 // AddressSanitizer handles heap corruption, and on 64 bit Macs, the malloc
169 // system automatically abort()s on heap corruption.
172 // Only override once, otherwise CrMallocErrorBreak() will recurse
174 if (g_original_malloc_error_break)
177 malloc_error_break_t malloc_error_break = LookUpMallocErrorBreak();
178 if (!malloc_error_break) {
179 DLOG(WARNING) << "Could not find malloc_error_break";
183 mach_error_t err = mach_override_ptr(
184 (void*)malloc_error_break,
185 (void*)&CrMallocErrorBreak,
186 (void**)&g_original_malloc_error_break);
189 DLOG(WARNING) << "Could not override malloc_error_break; error = " << err;
190 #endif // defined(ADDRESS_SANITIZER) || ARCH_CPU_64_BITS
193 // ------------------------------------------------------------------------
197 bool g_oom_killer_enabled;
199 // Starting with Mac OS X 10.7, the zone allocators set up by the system are
200 // read-only, to prevent them from being overwritten in an attack. However,
201 // blindly unprotecting and reprotecting the zone allocators fails with
202 // GuardMalloc because GuardMalloc sets up its zone allocator using a block of
203 // memory in its bss. Explicit saving/restoring of the protection is required.
205 // This function takes a pointer to a malloc zone, de-protects it if necessary,
206 // and returns (in the out parameters) a region of memory (if any) to be
207 // re-protected when modifications are complete. This approach assumes that
208 // there is no contention for the protection of this memory.
209 void DeprotectMallocZone(ChromeMallocZone* default_zone,
210 mach_vm_address_t* reprotection_start,
211 mach_vm_size_t* reprotection_length,
212 vm_prot_t* reprotection_value) {
214 *reprotection_start = reinterpret_cast<mach_vm_address_t>(default_zone);
215 struct vm_region_basic_info_64 info;
216 mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
217 kern_return_t result =
218 mach_vm_region(mach_task_self(),
221 VM_REGION_BASIC_INFO_64,
222 reinterpret_cast<vm_region_info_t>(&info),
225 CHECK(result == KERN_SUCCESS);
227 result = mach_port_deallocate(mach_task_self(), unused);
228 CHECK(result == KERN_SUCCESS);
230 // Does the region fully enclose the zone pointers? Possibly unwarranted
231 // simplification used: using the size of a full version 8 malloc zone rather
232 // than the actual smaller size if the passed-in zone is not version 8.
233 CHECK(*reprotection_start <=
234 reinterpret_cast<mach_vm_address_t>(default_zone));
235 mach_vm_size_t zone_offset = reinterpret_cast<mach_vm_size_t>(default_zone) -
236 reinterpret_cast<mach_vm_size_t>(*reprotection_start);
237 CHECK(zone_offset + sizeof(ChromeMallocZone) <= *reprotection_length);
239 if (info.protection & VM_PROT_WRITE) {
240 // No change needed; the zone is already writable.
241 *reprotection_start = 0;
242 *reprotection_length = 0;
243 *reprotection_value = VM_PROT_NONE;
245 *reprotection_value = info.protection;
246 result = mach_vm_protect(mach_task_self(),
248 *reprotection_length,
250 info.protection | VM_PROT_WRITE);
251 CHECK(result == KERN_SUCCESS);
255 // === C malloc/calloc/valloc/realloc/posix_memalign ===
257 typedef void* (*malloc_type)(struct _malloc_zone_t* zone,
259 typedef void* (*calloc_type)(struct _malloc_zone_t* zone,
262 typedef void* (*valloc_type)(struct _malloc_zone_t* zone,
264 typedef void (*free_type)(struct _malloc_zone_t* zone,
266 typedef void* (*realloc_type)(struct _malloc_zone_t* zone,
269 typedef void* (*memalign_type)(struct _malloc_zone_t* zone,
273 malloc_type g_old_malloc;
274 calloc_type g_old_calloc;
275 valloc_type g_old_valloc;
276 free_type g_old_free;
277 realloc_type g_old_realloc;
278 memalign_type g_old_memalign;
280 malloc_type g_old_malloc_purgeable;
281 calloc_type g_old_calloc_purgeable;
282 valloc_type g_old_valloc_purgeable;
283 free_type g_old_free_purgeable;
284 realloc_type g_old_realloc_purgeable;
285 memalign_type g_old_memalign_purgeable;
287 void* oom_killer_malloc(struct _malloc_zone_t* zone,
290 ScopedClearErrno clear_errno;
291 #endif // ARCH_CPU_32_BITS
292 void* result = g_old_malloc(zone, size);
294 debug::BreakDebugger();
298 void* oom_killer_calloc(struct _malloc_zone_t* zone,
302 ScopedClearErrno clear_errno;
303 #endif // ARCH_CPU_32_BITS
304 void* result = g_old_calloc(zone, num_items, size);
305 if (!result && num_items && size)
306 debug::BreakDebugger();
310 void* oom_killer_valloc(struct _malloc_zone_t* zone,
313 ScopedClearErrno clear_errno;
314 #endif // ARCH_CPU_32_BITS
315 void* result = g_old_valloc(zone, size);
317 debug::BreakDebugger();
321 void oom_killer_free(struct _malloc_zone_t* zone,
324 ScopedClearErrno clear_errno;
325 #endif // ARCH_CPU_32_BITS
326 g_old_free(zone, ptr);
329 void* oom_killer_realloc(struct _malloc_zone_t* zone,
333 ScopedClearErrno clear_errno;
334 #endif // ARCH_CPU_32_BITS
335 void* result = g_old_realloc(zone, ptr, size);
337 debug::BreakDebugger();
341 void* oom_killer_memalign(struct _malloc_zone_t* zone,
345 ScopedClearErrno clear_errno;
346 #endif // ARCH_CPU_32_BITS
347 void* result = g_old_memalign(zone, alignment, size);
348 // Only die if posix_memalign would have returned ENOMEM, since there are
349 // other reasons why NULL might be returned (see
350 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ).
351 if (!result && size && alignment >= sizeof(void*)
352 && (alignment & (alignment - 1)) == 0) {
353 debug::BreakDebugger();
358 void* oom_killer_malloc_purgeable(struct _malloc_zone_t* zone,
361 ScopedClearErrno clear_errno;
362 #endif // ARCH_CPU_32_BITS
363 void* result = g_old_malloc_purgeable(zone, size);
365 debug::BreakDebugger();
369 void* oom_killer_calloc_purgeable(struct _malloc_zone_t* zone,
373 ScopedClearErrno clear_errno;
374 #endif // ARCH_CPU_32_BITS
375 void* result = g_old_calloc_purgeable(zone, num_items, size);
376 if (!result && num_items && size)
377 debug::BreakDebugger();
381 void* oom_killer_valloc_purgeable(struct _malloc_zone_t* zone,
384 ScopedClearErrno clear_errno;
385 #endif // ARCH_CPU_32_BITS
386 void* result = g_old_valloc_purgeable(zone, size);
388 debug::BreakDebugger();
392 void oom_killer_free_purgeable(struct _malloc_zone_t* zone,
395 ScopedClearErrno clear_errno;
396 #endif // ARCH_CPU_32_BITS
397 g_old_free_purgeable(zone, ptr);
400 void* oom_killer_realloc_purgeable(struct _malloc_zone_t* zone,
404 ScopedClearErrno clear_errno;
405 #endif // ARCH_CPU_32_BITS
406 void* result = g_old_realloc_purgeable(zone, ptr, size);
408 debug::BreakDebugger();
412 void* oom_killer_memalign_purgeable(struct _malloc_zone_t* zone,
416 ScopedClearErrno clear_errno;
417 #endif // ARCH_CPU_32_BITS
418 void* result = g_old_memalign_purgeable(zone, alignment, size);
419 // Only die if posix_memalign would have returned ENOMEM, since there are
420 // other reasons why NULL might be returned (see
421 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c ).
422 if (!result && size && alignment >= sizeof(void*)
423 && (alignment & (alignment - 1)) == 0) {
424 debug::BreakDebugger();
429 // === C++ operator new ===
431 void oom_killer_new() {
432 debug::BreakDebugger();
435 // === Core Foundation CFAllocators ===
437 bool CanGetContextForCFAllocator() {
438 return !base::mac::IsOSLaterThanMavericks_DontCallThis();
441 CFAllocatorContext* ContextForCFAllocator(CFAllocatorRef allocator) {
442 if (base::mac::IsOSSnowLeopard()) {
443 ChromeCFAllocatorLeopards* our_allocator =
444 const_cast<ChromeCFAllocatorLeopards*>(
445 reinterpret_cast<const ChromeCFAllocatorLeopards*>(allocator));
446 return &our_allocator->_context;
447 } else if (base::mac::IsOSLion() ||
448 base::mac::IsOSMountainLion() ||
449 base::mac::IsOSMavericks()) {
450 ChromeCFAllocatorLions* our_allocator =
451 const_cast<ChromeCFAllocatorLions*>(
452 reinterpret_cast<const ChromeCFAllocatorLions*>(allocator));
453 return &our_allocator->_context;
459 CFAllocatorAllocateCallBack g_old_cfallocator_system_default;
460 CFAllocatorAllocateCallBack g_old_cfallocator_malloc;
461 CFAllocatorAllocateCallBack g_old_cfallocator_malloc_zone;
463 void* oom_killer_cfallocator_system_default(CFIndex alloc_size,
466 void* result = g_old_cfallocator_system_default(alloc_size, hint, info);
468 debug::BreakDebugger();
472 void* oom_killer_cfallocator_malloc(CFIndex alloc_size,
475 void* result = g_old_cfallocator_malloc(alloc_size, hint, info);
477 debug::BreakDebugger();
481 void* oom_killer_cfallocator_malloc_zone(CFIndex alloc_size,
484 void* result = g_old_cfallocator_malloc_zone(alloc_size, hint, info);
486 debug::BreakDebugger();
490 // === Cocoa NSObject allocation ===
492 typedef id (*allocWithZone_t)(id, SEL, NSZone*);
493 allocWithZone_t g_old_allocWithZone;
495 id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone)
497 id result = g_old_allocWithZone(self, _cmd, zone);
499 debug::BreakDebugger();
505 bool UncheckedMalloc(size_t size, void** result) {
508 ScopedClearErrno clear_errno;
509 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true);
510 #endif // ARCH_CPU_32_BITS
511 *result = g_old_malloc(malloc_default_zone(), size);
513 *result = malloc(size);
516 return *result != NULL;
519 bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
522 ScopedClearErrno clear_errno;
523 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true);
524 #endif // ARCH_CPU_32_BITS
525 *result = g_old_calloc(malloc_default_zone(), num_items, size);
527 *result = calloc(num_items, size);
530 return *result != NULL;
533 void* UncheckedMalloc(size_t size) {
535 return UncheckedMalloc(size, &address) ? address : NULL;
538 void* UncheckedCalloc(size_t num_items, size_t size) {
540 return UncheckedCalloc(num_items, size, &address) ? address : NULL;
543 void EnableTerminationOnOutOfMemory() {
544 if (g_oom_killer_enabled)
547 g_oom_killer_enabled = true;
549 // === C malloc/calloc/valloc/realloc/posix_memalign ===
551 // This approach is not perfect, as requests for amounts of memory larger than
552 // MALLOC_ABSOLUTE_MAX_SIZE (currently SIZE_T_MAX - (2 * PAGE_SIZE)) will
553 // still fail with a NULL rather than dying (see
554 // http://opensource.apple.com/source/Libc/Libc-583/gen/malloc.c for details).
555 // Unfortunately, it's the best we can do. Also note that this does not affect
556 // allocations from non-default zones.
558 CHECK(!g_old_malloc && !g_old_calloc && !g_old_valloc && !g_old_realloc &&
559 !g_old_memalign) << "Old allocators unexpectedly non-null";
561 CHECK(!g_old_malloc_purgeable && !g_old_calloc_purgeable &&
562 !g_old_valloc_purgeable && !g_old_realloc_purgeable &&
563 !g_old_memalign_purgeable) << "Old allocators unexpectedly non-null";
565 #if !defined(ADDRESS_SANITIZER)
566 // Don't do anything special on OOM for the malloc zones replaced by
567 // AddressSanitizer, as modifying or protecting them may not work correctly.
569 ChromeMallocZone* default_zone =
570 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
571 ChromeMallocZone* purgeable_zone =
572 reinterpret_cast<ChromeMallocZone*>(malloc_default_purgeable_zone());
574 mach_vm_address_t default_reprotection_start = 0;
575 mach_vm_size_t default_reprotection_length = 0;
576 vm_prot_t default_reprotection_value = VM_PROT_NONE;
577 DeprotectMallocZone(default_zone,
578 &default_reprotection_start,
579 &default_reprotection_length,
580 &default_reprotection_value);
582 mach_vm_address_t purgeable_reprotection_start = 0;
583 mach_vm_size_t purgeable_reprotection_length = 0;
584 vm_prot_t purgeable_reprotection_value = VM_PROT_NONE;
585 if (purgeable_zone) {
586 DeprotectMallocZone(purgeable_zone,
587 &purgeable_reprotection_start,
588 &purgeable_reprotection_length,
589 &purgeable_reprotection_value);
594 g_old_malloc = default_zone->malloc;
595 g_old_calloc = default_zone->calloc;
596 g_old_valloc = default_zone->valloc;
597 g_old_free = default_zone->free;
598 g_old_realloc = default_zone->realloc;
599 CHECK(g_old_malloc && g_old_calloc && g_old_valloc && g_old_free &&
601 << "Failed to get system allocation functions.";
603 default_zone->malloc = oom_killer_malloc;
604 default_zone->calloc = oom_killer_calloc;
605 default_zone->valloc = oom_killer_valloc;
606 default_zone->free = oom_killer_free;
607 default_zone->realloc = oom_killer_realloc;
609 if (default_zone->version >= 5) {
610 g_old_memalign = default_zone->memalign;
612 default_zone->memalign = oom_killer_memalign;
615 // Purgeable zone (if it exists)
617 if (purgeable_zone) {
618 g_old_malloc_purgeable = purgeable_zone->malloc;
619 g_old_calloc_purgeable = purgeable_zone->calloc;
620 g_old_valloc_purgeable = purgeable_zone->valloc;
621 g_old_free_purgeable = purgeable_zone->free;
622 g_old_realloc_purgeable = purgeable_zone->realloc;
623 CHECK(g_old_malloc_purgeable && g_old_calloc_purgeable &&
624 g_old_valloc_purgeable && g_old_free_purgeable &&
625 g_old_realloc_purgeable)
626 << "Failed to get system allocation functions.";
628 purgeable_zone->malloc = oom_killer_malloc_purgeable;
629 purgeable_zone->calloc = oom_killer_calloc_purgeable;
630 purgeable_zone->valloc = oom_killer_valloc_purgeable;
631 purgeable_zone->free = oom_killer_free_purgeable;
632 purgeable_zone->realloc = oom_killer_realloc_purgeable;
634 if (purgeable_zone->version >= 5) {
635 g_old_memalign_purgeable = purgeable_zone->memalign;
636 if (g_old_memalign_purgeable)
637 purgeable_zone->memalign = oom_killer_memalign_purgeable;
641 // Restore protection if it was active.
643 if (default_reprotection_start) {
644 kern_return_t result = mach_vm_protect(mach_task_self(),
645 default_reprotection_start,
646 default_reprotection_length,
648 default_reprotection_value);
649 CHECK(result == KERN_SUCCESS);
652 if (purgeable_reprotection_start) {
653 kern_return_t result = mach_vm_protect(mach_task_self(),
654 purgeable_reprotection_start,
655 purgeable_reprotection_length,
657 purgeable_reprotection_value);
658 CHECK(result == KERN_SUCCESS);
662 // === C malloc_zone_batch_malloc ===
664 // batch_malloc is omitted because the default malloc zone's implementation
665 // only supports batch_malloc for "tiny" allocations from the free list. It
666 // will fail for allocations larger than "tiny", and will only allocate as
667 // many blocks as it's able to from the free list. These factors mean that it
668 // can return less than the requested memory even in a non-out-of-memory
669 // situation. There's no good way to detect whether a batch_malloc failure is
670 // due to these other factors, or due to genuine memory or address space
671 // exhaustion. The fact that it only allocates space from the "tiny" free list
672 // means that it's likely that a failure will not be due to memory exhaustion.
673 // Similarly, these constraints on batch_malloc mean that callers must always
674 // be expecting to receive less memory than was requested, even in situations
675 // where memory pressure is not a concern. Finally, the only public interface
676 // to batch_malloc is malloc_zone_batch_malloc, which is specific to the
677 // system's malloc implementation. It's unlikely that anyone's even heard of
680 // === C++ operator new ===
682 // Yes, operator new does call through to malloc, but this will catch failures
683 // that our imperfect handling of malloc cannot.
685 std::set_new_handler(oom_killer_new);
687 #ifndef ADDRESS_SANITIZER
688 // === Core Foundation CFAllocators ===
690 // This will not catch allocation done by custom allocators, but will catch
691 // all allocation done by system-provided ones.
693 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc &&
694 !g_old_cfallocator_malloc_zone)
695 << "Old allocators unexpectedly non-null";
697 bool cf_allocator_internals_known = CanGetContextForCFAllocator();
699 if (cf_allocator_internals_known) {
700 CFAllocatorContext* context =
701 ContextForCFAllocator(kCFAllocatorSystemDefault);
702 CHECK(context) << "Failed to get context for kCFAllocatorSystemDefault.";
703 g_old_cfallocator_system_default = context->allocate;
704 CHECK(g_old_cfallocator_system_default)
705 << "Failed to get kCFAllocatorSystemDefault allocation function.";
706 context->allocate = oom_killer_cfallocator_system_default;
708 context = ContextForCFAllocator(kCFAllocatorMalloc);
709 CHECK(context) << "Failed to get context for kCFAllocatorMalloc.";
710 g_old_cfallocator_malloc = context->allocate;
711 CHECK(g_old_cfallocator_malloc)
712 << "Failed to get kCFAllocatorMalloc allocation function.";
713 context->allocate = oom_killer_cfallocator_malloc;
715 context = ContextForCFAllocator(kCFAllocatorMallocZone);
716 CHECK(context) << "Failed to get context for kCFAllocatorMallocZone.";
717 g_old_cfallocator_malloc_zone = context->allocate;
718 CHECK(g_old_cfallocator_malloc_zone)
719 << "Failed to get kCFAllocatorMallocZone allocation function.";
720 context->allocate = oom_killer_cfallocator_malloc_zone;
722 NSLog(@"Internals of CFAllocator not known; out-of-memory failures via "
723 "CFAllocator will not result in termination. http://crbug.com/45650");
727 // === Cocoa NSObject allocation ===
729 // Note that both +[NSObject new] and +[NSObject alloc] call through to
730 // +[NSObject allocWithZone:].
732 CHECK(!g_old_allocWithZone)
733 << "Old allocator unexpectedly non-null";
735 Class nsobject_class = [NSObject class];
736 Method orig_method = class_getClassMethod(nsobject_class,
737 @selector(allocWithZone:));
738 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>(
739 method_getImplementation(orig_method));
740 CHECK(g_old_allocWithZone)
741 << "Failed to get allocWithZone allocation function.";
742 method_setImplementation(orig_method,
743 reinterpret_cast<IMP>(oom_killer_allocWithZone));