Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 // This file contains all the logic necessary to intercept allocations on | 5 // This file contains all the logic necessary to intercept allocations on |
| 6 // macOS. "malloc zones" are an abstraction that allows the process to intercept | 6 // macOS. "malloc zones" are an abstraction that allows the process to intercept |
| 7 // all malloc-related functions. There is no good mechanism [short of | 7 // all malloc-related functions. There is no good mechanism [short of |
| 8 // interposition] to determine new malloc zones are added, so there's no clean | 8 // interposition] to determine new malloc zones are added, so there's no clean |
| 9 // mechanism to intercept all malloc zones. This file contains logic to | 9 // mechanism to intercept all malloc zones. This file contains logic to |
| 10 // intercept the default and purgeable zones, which always exist. A cursory | 10 // intercept the default and purgeable zones, which always exist. A cursory |
| 11 // review of Chrome seems to imply that non-default zones are almost never used. | 11 // review of Chrome seems to imply that non-default zones are almost never used. |
| 12 // | 12 // |
| 13 // This file also contains logic to intercept Core Foundation and Objective-C | 13 // This file also contains logic to intercept Core Foundation and Objective-C |
| 14 // allocations. The implementations forward to the default malloc zone, so the | 14 // allocations. The implementations forward to the default malloc zone, so the |
| 15 // only reason to intercept these calls is to re-label OOM crashes with slightly | 15 // only reason to intercept these calls is to re-label OOM crashes with slightly |
| 16 // more details. | 16 // more details. |
| 17 | 17 |
| 18 #include "base/allocator/allocator_interception_mac.h" | 18 #include "base/allocator/allocator_interception_mac.h" |
| 19 | 19 |
| 20 #include <CoreFoundation/CoreFoundation.h> | 20 #include <CoreFoundation/CoreFoundation.h> |
| 21 #import <Foundation/Foundation.h> | 21 #import <Foundation/Foundation.h> |
| 22 #include <errno.h> | 22 #include <errno.h> |
| 23 #include <mach/mach.h> | 23 #include <mach/mach.h> |
| 24 #include <mach/mach_vm.h> | 24 #include <mach/mach_vm.h> |
| 25 #import <objc/runtime.h> | 25 #import <objc/runtime.h> |
| 26 #include <stddef.h> | 26 #include <stddef.h> |
| 27 | 27 |
| 28 #include <new> | 28 #include <new> |
| 29 | 29 |
| 30 #include "base/allocator/allocator_shim.h" | |
| 31 #include "base/allocator/features.h" | |
| 30 #include "base/logging.h" | 32 #include "base/logging.h" |
| 31 #include "base/mac/mac_util.h" | 33 #include "base/mac/mac_util.h" |
| 32 #include "base/mac/mach_logging.h" | 34 #include "base/mac/mach_logging.h" |
| 33 #include "base/process/memory.h" | 35 #include "base/process/memory.h" |
| 34 #include "base/scoped_clear_errno.h" | 36 #include "base/scoped_clear_errno.h" |
| 35 #include "build/build_config.h" | 37 #include "build/build_config.h" |
| 36 #include "third_party/apple_apsl/CFBase.h" | 38 #include "third_party/apple_apsl/CFBase.h" |
| 37 | 39 |
| 38 namespace base { | 40 namespace base { |
| 39 namespace allocator { | 41 namespace allocator { |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 439 // where memory pressure is not a concern. Finally, the only public interface | 441 // where memory pressure is not a concern. Finally, the only public interface |
| 440 // to batch_malloc is malloc_zone_batch_malloc, which is specific to the | 442 // to batch_malloc is malloc_zone_batch_malloc, which is specific to the |
| 441 // system's malloc implementation. It's unlikely that anyone's even heard of | 443 // system's malloc implementation. It's unlikely that anyone's even heard of |
| 442 // it. | 444 // it. |
| 443 | 445 |
| 444 // === C++ operator new === | 446 // === C++ operator new === |
| 445 | 447 |
| 446 // Yes, operator new does call through to malloc, but this will catch failures | 448 // Yes, operator new does call through to malloc, but this will catch failures |
| 447 // that our imperfect handling of malloc cannot. | 449 // that our imperfect handling of malloc cannot. |
| 448 | 450 |
| 449 std::set_new_handler(oom_killer_new); | 451 std::set_new_handler(oom_killer_new); |
|
Primiano Tucci (use gerrit)
2017/02/07 11:58:08
(read this comment after the one below)
Honestly I
erikchen
2017/02/09 23:49:04
Done.
| |
| 450 | 452 |
| 453 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | |
| 454 allocator::SetCallNewHandlerOnMallocFailure(true); | |
|
Primiano Tucci (use gerrit)
2017/02/07 11:58:08
why here and not in memory_mac.mm -> EnableTermina
erikchen
2017/02/09 23:49:04
Done.
| |
| 455 #endif | |
| 456 | |
| 451 #ifndef ADDRESS_SANITIZER | 457 #ifndef ADDRESS_SANITIZER |
| 452 // === Core Foundation CFAllocators === | 458 // === Core Foundation CFAllocators === |
| 453 | 459 |
| 454 // This will not catch allocation done by custom allocators, but will catch | 460 // This will not catch allocation done by custom allocators, but will catch |
| 455 // all allocation done by system-provided ones. | 461 // all allocation done by system-provided ones. |
| 456 | 462 |
| 457 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && | 463 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && |
| 458 !g_old_cfallocator_malloc_zone) | 464 !g_old_cfallocator_malloc_zone) |
| 459 << "Old allocators unexpectedly non-null"; | 465 << "Old allocators unexpectedly non-null"; |
| 460 | 466 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 502 g_old_allocWithZone = | 508 g_old_allocWithZone = |
| 503 reinterpret_cast<allocWithZone_t>(method_getImplementation(orig_method)); | 509 reinterpret_cast<allocWithZone_t>(method_getImplementation(orig_method)); |
| 504 CHECK(g_old_allocWithZone) | 510 CHECK(g_old_allocWithZone) |
| 505 << "Failed to get allocWithZone allocation function."; | 511 << "Failed to get allocWithZone allocation function."; |
| 506 method_setImplementation(orig_method, | 512 method_setImplementation(orig_method, |
| 507 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 513 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
| 508 } | 514 } |
| 509 | 515 |
| 510 } // namespace allocator | 516 } // namespace allocator |
| 511 } // namespace base | 517 } // namespace base |
| OLD | NEW |