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 BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_INTERCEPTION_MAC_H_ 6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_INTERCEPTION_MAC_H_ 7 8 #include <stddef.h> 9 10 #include "base/base_export.h" 11 #include "third_party/apple_apsl/malloc.h" 12 13 namespace allocator_shim { 14 15 struct MallocZoneFunctions; 16 17 // This initializes AllocatorDispatch::default_dispatch by saving pointers to 18 // the functions in the current default malloc zone. This must be called before 19 // the default malloc zone is changed to have its intended effect. 20 void InitializeDefaultDispatchToMacAllocator(); 21 22 // Saves the function pointers currently used by the default zone. 23 void StoreFunctionsForDefaultZone(); 24 25 // Same as StoreFunctionsForDefaultZone, but for all malloc zones. 26 void StoreFunctionsForAllZones(); 27 28 // For all malloc zones that have been stored, replace their functions with 29 // |functions|. 30 void ReplaceFunctionsForStoredZones(const MallocZoneFunctions* functions); 31 32 extern bool g_replaced_default_zone; 33 34 // Calls the original implementation of malloc/calloc prior to interception. 35 bool UncheckedMallocMac(size_t size, void** result); 36 bool UncheckedCallocMac(size_t num_items, size_t size, void** result); 37 38 // Intercepts calls to default and purgeable malloc zones. Intercepts Core 39 // Foundation and Objective-C allocations. 40 // Has no effect on the default malloc zone if the allocator shim already 41 // performs that interception. 42 BASE_EXPORT void InterceptAllocationsMac(); 43 44 // Updates all malloc zones to use their original functions. 45 // Also calls ClearAllMallocZonesForTesting. 46 BASE_EXPORT void UninterceptMallocZonesForTesting(); 47 48 // Returns true if allocations are successfully being intercepted for all malloc 49 // zones. 50 bool AreMallocZonesIntercepted(); 51 52 // Periodically checks for, and shims new malloc zones. Stops checking after 1 53 // minute. 54 BASE_EXPORT void PeriodicallyShimNewMallocZones(); 55 56 // Exposed for testing. 57 BASE_EXPORT void ShimNewMallocZones(); 58 BASE_EXPORT void ReplaceZoneFunctions(ChromeMallocZone* zone, 59 const MallocZoneFunctions* functions); 60 61 } // namespace allocator_shim 62 63 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_INTERCEPTION_MAC_H_ 64