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

Side by Side Diff: base/allocator/malloc_zone_functions_mac.h

Issue 2703803004: macOS: Shim all malloc zones. (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 #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 "base/logging.h"
12 #include "third_party/apple_apsl/malloc.h" 13 #include "third_party/apple_apsl/malloc.h"
13 14
14 namespace base { 15 namespace base {
15 namespace allocator { 16 namespace allocator {
16 17
17 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, size_t size); 18 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, size_t size);
18 typedef void* (*calloc_type)(struct _malloc_zone_t* zone, 19 typedef void* (*calloc_type)(struct _malloc_zone_t* zone,
19 size_t num_items, 20 size_t num_items,
20 size_t size); 21 size_t size);
21 typedef void* (*valloc_type)(struct _malloc_zone_t* zone, size_t size); 22 typedef void* (*valloc_type)(struct _malloc_zone_t* zone, size_t size);
(...skipping 21 matching lines...) Expand all
43 malloc_type malloc = nullptr; 44 malloc_type malloc = nullptr;
44 calloc_type calloc = nullptr; 45 calloc_type calloc = nullptr;
45 valloc_type valloc = nullptr; 46 valloc_type valloc = nullptr;
46 free_type free = nullptr; 47 free_type free = nullptr;
47 realloc_type realloc = nullptr; 48 realloc_type realloc = nullptr;
48 memalign_type memalign = nullptr; 49 memalign_type memalign = nullptr;
49 batch_malloc_type batch_malloc = nullptr; 50 batch_malloc_type batch_malloc = nullptr;
50 batch_free_type batch_free = nullptr; 51 batch_free_type batch_free = nullptr;
51 free_definite_size_type free_definite_size = nullptr; 52 free_definite_size_type free_definite_size = nullptr;
52 size_fn_type size = nullptr; 53 size_fn_type size = nullptr;
54 const ChromeMallocZone* context = nullptr;
53 }; 55 };
54 56
55 // Saves the function pointers currently used by default zone into |functions|. 57 void StoreZoneFunctions(const ChromeMallocZone* zone,
56 void StoreFunctionsForDefaultZone(MallocZoneFunctions* functions); 58 MallocZoneFunctions* functions);
59 static constexpr int kMaxZoneCount = 30;
60 BASE_EXPORT extern MallocZoneFunctions* g_malloc_zones;
57 61
58 // Updates the default malloc zone to use the functions specified by 62 // The array g_malloc_zones stores all information about malloc zones before
59 // |functions|. 63 // they are shimmed. This information needs to be accessed during dispatch back
60 void ReplaceFunctionsForDefaultZone(const MallocZoneFunctions* functions); 64 // into the zone, and additional zones may be added later in the execution fo
65 // the program, so the array needs to be both thread-safe and high-performance.
66 //
67 // We begin by creating an array of MallocZoneFunctions of fixed size. We will
68 // never modify the container, which provides thread-safety to iterators. When
69 // we want to add a MallocZoneFunctions to the container, we:
70 // 1. Fill in all the fields.
71 // 2. Update the total zone count.
72 // 3. Insert a memory barrier.
73 // 4. Insert our shim.
74 //
75 // Each MallocZoneFunctions is uniquely identified by |context|, which is a
76 // pointer to the original malloc zone. When we wish to dispatch back to the
77 // original malloc zones, we iterate through the array, looking for a matching
78 // |context|.
79 //
80 // Most allocations go through the default allocator. We will ensure that the
81 // default allocator is stored as the first MallocZoneFunctions.
82 BASE_EXPORT void StoreMallocZone(ChromeMallocZone* zone);
83 BASE_EXPORT bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone);
61 84
62 extern bool g_replaced_default_zone; 85 BASE_EXPORT int GetMallocZoneCountForTesting();
86 BASE_EXPORT void ClearAllMallocZonesForTesting();
63 87
64 // Calls the original implementation of malloc/calloc prior to interception. 88 inline MallocZoneFunctions& GetFunctionsForZone(void* zone) {
65 bool UncheckedMallocMac(size_t size, void** result); 89 for (unsigned int i = 0; i < kMaxZoneCount; ++i) {
66 bool UncheckedCallocMac(size_t num_items, size_t size, void** result); 90 if (g_malloc_zones[i].context == zone)
91 return g_malloc_zones[i];
92 }
93 IMMEDIATE_CRASH();
94 }
67 95
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 96 } // namespace allocator
74 } // namespace base 97 } // namespace base
75 98
76 #endif // BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_ 99 #endif // BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_
OLDNEW
« no previous file with comments | « base/allocator/allocator_shim_override_mac_symbols.h ('k') | base/allocator/malloc_zone_functions_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698