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

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

Issue 2703803004: macOS: Shim all malloc zones. (Closed)
Patch Set: more base export. 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 "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
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 ChromeMallocZone* context = nullptr;
53 }; 54 };
54 55
55 // Saves the function pointers currently used by default zone into |functions|. 56 void StoreZoneFunctions(ChromeMallocZone* zone, MallocZoneFunctions* functions);
Primiano Tucci (use gerrit) 2017/02/23 00:40:18 +const ChromeMallocZone? Mostly for readability pu
erikchen 2017/02/23 01:43:14 Done. I just use clang-format.
56 void StoreFunctionsForDefaultZone(MallocZoneFunctions* functions); 57 static constexpr int kMaxZoneCount = 30;
58 BASE_EXPORT extern MallocZoneFunctions g_malloc_zones[kMaxZoneCount];
57 59
58 // Updates the default malloc zone to use the functions specified by 60 // The array g_malloc_zones stores all information about malloc zones before
59 // |functions|. 61 // they are shimmed. This information needs to be accessed during dispatch back
60 void ReplaceFunctionsForDefaultZone(const MallocZoneFunctions* functions); 62 // into the zone, and additional zones may be added later in the execution fo
63 // the program, so the array needs to be both thread-safe and high-performance.
64 //
65 // We begin by creating an array of MallocZoneFunctions of fixed size. We will
66 // never modify the container, which provides thread-safety to iterators. When
67 // we want to add a MallocZoneFunctions to the container, we:
68 // 1. Fill in all the fields.
69 // 2. Update the total zone count.
70 // 3. Insert a memory barrier.
71 // 4. Insert our shim.
72 //
73 // Each MallocZoneFunctions is uniquely identified by |context|, which is a
74 // pointer to the original malloc zone. When we wish to dispatch back to the
75 // original malloc zones, we iterate through the array, looking for a matching
76 // |context|.
77 //
78 // Most allocations go through the default allocator. We will ensure that the
79 // default allocator is stored as the first MallocZoneFunctions.
80 BASE_EXPORT void StoreMallocZone(ChromeMallocZone* zone);
81 BASE_EXPORT bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone);
61 82
62 extern bool g_replaced_default_zone; 83 BASE_EXPORT int GetMallocZoneCountForTesting();
84 BASE_EXPORT void ClearAllMallocZonesForTesting();
63 85
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 86 } // namespace allocator
74 } // namespace base 87 } // namespace base
75 88
76 #endif // BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_ 89 #endif // BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698