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 |
(...skipping 11 matching lines...) Expand all Loading... | |
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/features.h" | 30 #include "base/allocator/features.h" |
31 #include "base/allocator/malloc_zone_functions_mac.h" | 31 #include "base/allocator/malloc_zone_functions_mac.h" |
32 #include "base/base_switches.h" | |
33 #include "base/bind.h" | |
34 #include "base/command_line.h" | |
Primiano Tucci (use gerrit)
2017/04/03 08:03:24
nit: you don't need command-line and base_switches
| |
32 #include "base/logging.h" | 35 #include "base/logging.h" |
33 #include "base/mac/mac_util.h" | 36 #include "base/mac/mac_util.h" |
34 #include "base/mac/mach_logging.h" | 37 #include "base/mac/mach_logging.h" |
35 #include "base/process/memory.h" | 38 #include "base/process/memory.h" |
36 #include "base/scoped_clear_errno.h" | 39 #include "base/scoped_clear_errno.h" |
40 #include "base/threading/sequenced_task_runner_handle.h" | |
37 #include "build/build_config.h" | 41 #include "build/build_config.h" |
38 #include "third_party/apple_apsl/CFBase.h" | 42 #include "third_party/apple_apsl/CFBase.h" |
39 | 43 |
40 namespace base { | 44 namespace base { |
41 namespace allocator { | 45 namespace allocator { |
42 | 46 |
43 bool g_replaced_default_zone = false; | 47 bool g_replaced_default_zone = false; |
44 | 48 |
45 namespace { | 49 namespace { |
46 | 50 |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 kern_return_t kr = malloc_get_all_zones(mach_task_self(), 0, &zones, &count); | 521 kern_return_t kr = malloc_get_all_zones(mach_task_self(), 0, &zones, &count); |
518 CHECK(kr == KERN_SUCCESS); | 522 CHECK(kr == KERN_SUCCESS); |
519 for (unsigned int i = 0; i < count; ++i) { | 523 for (unsigned int i = 0; i < count; ++i) { |
520 UninterceptMallocZoneForTesting( | 524 UninterceptMallocZoneForTesting( |
521 reinterpret_cast<struct _malloc_zone_t*>(zones[i])); | 525 reinterpret_cast<struct _malloc_zone_t*>(zones[i])); |
522 } | 526 } |
523 | 527 |
524 ClearAllMallocZonesForTesting(); | 528 ClearAllMallocZonesForTesting(); |
525 } | 529 } |
526 | 530 |
531 namespace { | |
532 | |
533 void ShimNewMallocZones(base::Time end_time, base::TimeDelta delay) { | |
534 StoreFunctionsForAllZones(); | |
535 | |
536 // Use the functions for the default zone as a template to replace those | |
537 // new zones. | |
538 ChromeMallocZone* default_zone = | |
539 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone()); | |
540 DCHECK(IsMallocZoneAlreadyStored(default_zone)); | |
541 | |
542 MallocZoneFunctions new_functions; | |
543 StoreZoneFunctions(default_zone, &new_functions); | |
544 ReplaceFunctionsForStoredZones(&new_functions); | |
545 | |
546 if (base::Time::Now() > end_time) | |
547 return; | |
548 | |
549 base::TimeDelta next_delay = delay * 2; | |
550 SequencedTaskRunnerHandle::Get()->PostDelayedTask( | |
551 FROM_HERE, base::Bind(&ShimNewMallocZones, end_time, next_delay), delay); | |
552 } | |
553 | |
554 } // namespace | |
555 | |
556 void PeriodicallyShimNewMallocZones() { | |
557 base::Time end_time = base::Time::Now() + base::TimeDelta::FromMinutes(1); | |
558 base::TimeDelta initial_delay = base::TimeDelta::FromSeconds(1); | |
559 ShimNewMallocZones(end_time, initial_delay); | |
560 } | |
561 | |
527 } // namespace allocator | 562 } // namespace allocator |
528 } // namespace base | 563 } // namespace base |
OLD | NEW |