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 #ifndef BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_ | 5 #ifndef BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_ |
6 #define BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_ | 6 #define BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_ |
7 | 7 |
8 #include <malloc/malloc.h> | 8 #include <malloc/malloc.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "third_party/apple_apsl/malloc.h" | 12 #include "third_party/apple_apsl/malloc.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace allocator { | 15 namespace allocator { |
16 | 16 |
(...skipping 26 matching lines...) Expand all Loading... |
43 malloc_type malloc = nullptr; | 43 malloc_type malloc = nullptr; |
44 calloc_type calloc = nullptr; | 44 calloc_type calloc = nullptr; |
45 valloc_type valloc = nullptr; | 45 valloc_type valloc = nullptr; |
46 free_type free = nullptr; | 46 free_type free = nullptr; |
47 realloc_type realloc = nullptr; | 47 realloc_type realloc = nullptr; |
48 memalign_type memalign = nullptr; | 48 memalign_type memalign = nullptr; |
49 batch_malloc_type batch_malloc = nullptr; | 49 batch_malloc_type batch_malloc = nullptr; |
50 batch_free_type batch_free = nullptr; | 50 batch_free_type batch_free = nullptr; |
51 free_definite_size_type free_definite_size = nullptr; | 51 free_definite_size_type free_definite_size = nullptr; |
52 size_fn_type size = nullptr; | 52 size_fn_type size = nullptr; |
| 53 const ChromeMallocZone* context = nullptr; |
53 }; | 54 }; |
54 | 55 |
55 // Saves the function pointers currently used by default zone into |functions|. | 56 void StoreZoneFunctions(const ChromeMallocZone* zone, |
56 void StoreFunctionsForDefaultZone(MallocZoneFunctions* functions); | 57 MallocZoneFunctions* functions); |
| 58 static constexpr int kMaxZoneCount = 30; |
| 59 BASE_EXPORT extern MallocZoneFunctions g_malloc_zones[kMaxZoneCount]; |
57 | 60 |
58 // Updates the default malloc zone to use the functions specified by | 61 // The array g_malloc_zones stores all information about malloc zones before |
59 // |functions|. | 62 // they are shimmed. This information needs to be accessed during dispatch back |
60 void ReplaceFunctionsForDefaultZone(const MallocZoneFunctions* functions); | 63 // into the zone, and additional zones may be added later in the execution fo |
| 64 // the program, so the array needs to be both thread-safe and high-performance. |
| 65 // |
| 66 // We begin by creating an array of MallocZoneFunctions of fixed size. We will |
| 67 // never modify the container, which provides thread-safety to iterators. When |
| 68 // we want to add a MallocZoneFunctions to the container, we: |
| 69 // 1. Fill in all the fields. |
| 70 // 2. Update the total zone count. |
| 71 // 3. Insert a memory barrier. |
| 72 // 4. Insert our shim. |
| 73 // |
| 74 // Each MallocZoneFunctions is uniquely identified by |context|, which is a |
| 75 // pointer to the original malloc zone. When we wish to dispatch back to the |
| 76 // original malloc zones, we iterate through the array, looking for a matching |
| 77 // |context|. |
| 78 // |
| 79 // Most allocations go through the default allocator. We will ensure that the |
| 80 // default allocator is stored as the first MallocZoneFunctions. |
| 81 BASE_EXPORT void StoreMallocZone(ChromeMallocZone* zone); |
| 82 BASE_EXPORT bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone); |
61 | 83 |
62 extern bool g_replaced_default_zone; | 84 BASE_EXPORT int GetMallocZoneCountForTesting(); |
| 85 BASE_EXPORT void ClearAllMallocZonesForTesting(); |
63 | 86 |
64 // Calls the original implementation of malloc/calloc prior to interception. | |
65 bool UncheckedMallocMac(size_t size, void** result); | |
66 bool UncheckedCallocMac(size_t num_items, size_t size, void** result); | |
67 | |
68 // Intercepts calls to default and purgeable malloc zones. Intercepts Core | |
69 // Foundation and Objective-C allocations. | |
70 // Has no effect on the default malloc zone if the allocator shim already | |
71 // performs that interception. | |
72 BASE_EXPORT void InterceptAllocationsMac(); | |
73 } // namespace allocator | 87 } // namespace allocator |
74 } // namespace base | 88 } // namespace base |
75 | 89 |
76 #endif // BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_ | 90 #endif // BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_ |
OLD | NEW |