1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PARTITION_ALLOC_SHIM_ALLOCATOR_INTERCEPTION_APPLE_H_
6 #define PARTITION_ALLOC_SHIM_ALLOCATOR_INTERCEPTION_APPLE_H_
7 
8 #include "partition_alloc/partition_alloc_buildflags.h"
9 
10 #if BUILDFLAG(USE_ALLOCATOR_SHIM)
11 #include <cstddef>
12 
13 #include "partition_alloc/partition_alloc_base/component_export.h"
14 #include "partition_alloc/third_party/apple_apsl/malloc.h"
15 
16 namespace allocator_shim {
17 
18 struct MallocZoneFunctions;
19 
20 // This initializes AllocatorDispatch::default_dispatch by saving pointers to
21 // the functions in the current default malloc zone. This must be called before
22 // the default malloc zone is changed to have its intended effect.
23 void InitializeDefaultDispatchToMacAllocator();
24 
25 // Saves the function pointers currently used by the default zone.
26 void StoreFunctionsForDefaultZone();
27 
28 // Same as StoreFunctionsForDefaultZone, but for all malloc zones.
29 void StoreFunctionsForAllZones();
30 
31 // For all malloc zones that have been stored, replace their functions with
32 // |functions|.
33 void ReplaceFunctionsForStoredZones(const MallocZoneFunctions* functions);
34 
35 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) extern bool g_replaced_default_zone;
36 
37 // Calls the original implementation of malloc/calloc prior to interception.
38 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
39 bool UncheckedMallocMac(size_t size, void** result);
40 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
41 bool UncheckedCallocMac(size_t num_items, size_t size, void** result);
42 
43 // Intercepts calls to default and purgeable malloc zones. Intercepts Core
44 // Foundation and Objective-C allocations.
45 // Has no effect on the default malloc zone if the allocator shim already
46 // performs that interception.
47 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) void InterceptAllocationsMac();
48 
49 // Updates all malloc zones to use their original functions.
50 // Also calls ClearAllMallocZonesForTesting.
51 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) void UninterceptMallocZonesForTesting();
52 
53 // Returns true if allocations are successfully being intercepted for all malloc
54 // zones.
55 bool AreMallocZonesIntercepted();
56 
57 // heap_profiling::ProfilingClient needs to shim all malloc zones even ones
58 // that are registered after the start-up time. ProfilingClient periodically
59 // calls this API to make it sure that all malloc zones are shimmed.
60 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) void ShimNewMallocZones();
61 
62 // Exposed for testing.
63 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
64 void ReplaceZoneFunctions(ChromeMallocZone* zone,
65                           const MallocZoneFunctions* functions);
66 
67 }  // namespace allocator_shim
68 
69 #endif  // BUILDFLAG(USE_ALLOCATOR_SHIM)
70 
71 #endif  // PARTITION_ALLOC_SHIM_ALLOCATOR_INTERCEPTION_APPLE_H_
72