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

Side by Side Diff: base/allocator/allocator_interception_mac.mm

Issue 2713553002: mac: Periodically shim new malloc zones immediately after startup. (Closed)
Patch Set: Rebase. Created 3 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
OLDNEW
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
(...skipping 12 matching lines...) Expand all
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" 30 #include "base/allocator/allocator_shim.h"
31 #include "base/allocator/features.h" 31 #include "base/allocator/features.h"
32 #include "base/allocator/malloc_zone_functions_mac.h" 32 #include "base/allocator/malloc_zone_functions_mac.h"
33 #include "base/base_switches.h"
34 #include "base/bind.h"
35 #include "base/command_line.h"
33 #include "base/logging.h" 36 #include "base/logging.h"
34 #include "base/mac/mac_util.h" 37 #include "base/mac/mac_util.h"
35 #include "base/mac/mach_logging.h" 38 #include "base/mac/mach_logging.h"
36 #include "base/process/memory.h" 39 #include "base/process/memory.h"
37 #include "base/scoped_clear_errno.h" 40 #include "base/scoped_clear_errno.h"
41 #include "base/threading/sequenced_task_runner_handle.h"
38 #include "build/build_config.h" 42 #include "build/build_config.h"
39 #include "third_party/apple_apsl/CFBase.h" 43 #include "third_party/apple_apsl/CFBase.h"
40 44
41 namespace base { 45 namespace base {
42 namespace allocator { 46 namespace allocator {
43 47
44 bool g_replaced_default_zone = false; 48 bool g_replaced_default_zone = false;
45 49
46 namespace { 50 namespace {
47 51
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 Method orig_method = 495 Method orig_method =
492 class_getClassMethod(nsobject_class, @selector(allocWithZone:)); 496 class_getClassMethod(nsobject_class, @selector(allocWithZone:));
493 g_old_allocWithZone = 497 g_old_allocWithZone =
494 reinterpret_cast<allocWithZone_t>(method_getImplementation(orig_method)); 498 reinterpret_cast<allocWithZone_t>(method_getImplementation(orig_method));
495 CHECK(g_old_allocWithZone) 499 CHECK(g_old_allocWithZone)
496 << "Failed to get allocWithZone allocation function."; 500 << "Failed to get allocWithZone allocation function.";
497 method_setImplementation(orig_method, 501 method_setImplementation(orig_method,
498 reinterpret_cast<IMP>(oom_killer_allocWithZone)); 502 reinterpret_cast<IMP>(oom_killer_allocWithZone));
499 } 503 }
500 504
505 namespace {
506
507 void ShimNewMallocZones(base::Time end_time, base::TimeDelta delay) {
508 StoreFunctionsForAllZones();
509
510 // Use the functions for the default zone as a template to replace those
511 // new zones.
512 ChromeMallocZone* default_zone =
513 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
514 DCHECK(IsMallocZoneAlreadyStored(default_zone));
515
516 MallocZoneFunctions new_functions;
517 StoreZoneFunctions(default_zone, &new_functions);
518 ReplaceFunctionsForStoredZones(&new_functions);
519
520 if (base::Time::Now() > end_time)
521 return;
522
523 base::TimeDelta next_delay = delay * 2;
524 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
525 FROM_HERE, base::Bind(&ShimNewMallocZones, end_time, next_delay), delay);
526 }
527
528 } // namespace
529
530 void PeriodicallyShimNewMallocZones() {
531 if (!CommandLine::InitializedForCurrentProcess() ||
532 !CommandLine::ForCurrentProcess()->HasSwitch(
533 switches::kEnableHeapProfiling)) {
534 return;
535 }
536 base::Time end_time = base::Time::Now() + base::TimeDelta::FromMinutes(1);
537 base::TimeDelta initial_delay = base::TimeDelta::FromSeconds(1);
538 ShimNewMallocZones(end_time, initial_delay);
539 }
540
501 } // namespace allocator 541 } // namespace allocator
502 } // namespace base 542 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698