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_MALLOC_ZONE_FUNCTIONS_MAC_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_MALLOC_ZONE_FUNCTIONS_MAC_H_
7
8 #include <malloc/malloc.h>
9 #include <stddef.h>
10
11 #include "base/allocator/partition_allocator/partition_alloc_base/immediate_crash.h"
12 #include "base/base_export.h"
13 #include "third_party/apple_apsl/malloc.h"
14
15 namespace allocator_shim {
16
17 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, size_t size);
18 typedef void* (*calloc_type)(struct _malloc_zone_t* zone,
19 size_t num_items,
20 size_t size);
21 typedef void* (*valloc_type)(struct _malloc_zone_t* zone, size_t size);
22 typedef void (*free_type)(struct _malloc_zone_t* zone, void* ptr);
23 typedef void* (*realloc_type)(struct _malloc_zone_t* zone,
24 void* ptr,
25 size_t size);
26 typedef void* (*memalign_type)(struct _malloc_zone_t* zone,
27 size_t alignment,
28 size_t size);
29 typedef unsigned (*batch_malloc_type)(struct _malloc_zone_t* zone,
30 size_t size,
31 void** results,
32 unsigned num_requested);
33 typedef void (*batch_free_type)(struct _malloc_zone_t* zone,
34 void** to_be_freed,
35 unsigned num_to_be_freed);
36 typedef void (*free_definite_size_type)(struct _malloc_zone_t* zone,
37 void* ptr,
38 size_t size);
39 typedef void (*try_free_default_type)(struct _malloc_zone_t* zone, void* ptr);
40 typedef size_t (*size_fn_type)(struct _malloc_zone_t* zone, const void* ptr);
41 typedef boolean_t (*claimed_address_type)(struct _malloc_zone_t* zone,
42 void* ptr);
43
44 struct MallocZoneFunctions {
45 malloc_type malloc;
46 calloc_type calloc;
47 valloc_type valloc;
48 free_type free;
49 realloc_type realloc;
50 memalign_type memalign;
51 batch_malloc_type batch_malloc;
52 batch_free_type batch_free;
53 free_definite_size_type free_definite_size;
54 try_free_default_type try_free_default;
55 size_fn_type size;
56 claimed_address_type claimed_address;
57 const ChromeMallocZone* context;
58 };
59
60 BASE_EXPORT void StoreZoneFunctions(const ChromeMallocZone* zone,
61 MallocZoneFunctions* functions);
62 static constexpr int kMaxZoneCount = 30;
63 BASE_EXPORT extern MallocZoneFunctions g_malloc_zones[kMaxZoneCount];
64
65 // The array g_malloc_zones stores all information about malloc zones before
66 // they are shimmed. This information needs to be accessed during dispatch back
67 // into the zone, and additional zones may be added later in the execution fo
68 // the program, so the array needs to be both thread-safe and high-performance.
69 //
70 // We begin by creating an array of MallocZoneFunctions of fixed size. We will
71 // never modify the container, which provides thread-safety to iterators. When
72 // we want to add a MallocZoneFunctions to the container, we:
73 // 1. Fill in all the fields.
74 // 2. Update the total zone count.
75 // 3. Insert a memory barrier.
76 // 4. Insert our shim.
77 //
78 // Each MallocZoneFunctions is uniquely identified by |context|, which is a
79 // pointer to the original malloc zone. When we wish to dispatch back to the
80 // original malloc zones, we iterate through the array, looking for a matching
81 // |context|.
82 //
83 // Most allocations go through the default allocator. We will ensure that the
84 // default allocator is stored as the first MallocZoneFunctions.
85 //
86 // Returns whether the zone was successfully stored.
87 BASE_EXPORT bool StoreMallocZone(ChromeMallocZone* zone);
88 BASE_EXPORT bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone);
89 BASE_EXPORT bool DoesMallocZoneNeedReplacing(
90 ChromeMallocZone* zone,
91 const MallocZoneFunctions* functions);
92
93 BASE_EXPORT int GetMallocZoneCountForTesting();
94 BASE_EXPORT void ClearAllMallocZonesForTesting();
95
GetFunctionsForZone(void * zone)96 inline MallocZoneFunctions& GetFunctionsForZone(void* zone) {
97 for (unsigned int i = 0; i < kMaxZoneCount; ++i) {
98 if (g_malloc_zones[i].context == zone)
99 return g_malloc_zones[i];
100 }
101 PA_IMMEDIATE_CRASH();
102 }
103
104 } // namespace allocator_shim
105
106 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_MALLOC_ZONE_FUNCTIONS_MAC_H_
107