1 /*
2     Copyright (c) 2005-2022 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 // The original source for this code is
18 // Copyright (c) 2011, Google Inc.
19 // All rights reserved.
20 //
21 // Redistribution and use in source and binary forms, with or without
22 // modification, are permitted provided that the following conditions are
23 // met:
24 //
25 //     * Redistributions of source code must retain the above copyright
26 // notice, this list of conditions and the following disclaimer.
27 //     * Redistributions in binary form must reproduce the above
28 // copyright notice, this list of conditions and the following disclaimer
29 // in the documentation and/or other materials provided with the
30 // distribution.
31 //     * Neither the name of Google Inc. nor the names of its
32 // contributors may be used to endorse or promote products derived from
33 // this software without specific prior written permission.
34 //
35 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 
47 #include <AvailabilityMacros.h>
48 #include <malloc/malloc.h>
49 #include <mach/mach.h>
50 #include <stdlib.h>
51 
enumerator(task_t,void *,unsigned,vm_address_t,memory_reader_t,vm_range_recorder_t)52 static kern_return_t enumerator(task_t, void *, unsigned, vm_address_t,
53                                 memory_reader_t, vm_range_recorder_t)
54 {
55     return KERN_FAILURE;
56 }
57 
good_size(malloc_zone_t *,size_t size)58 static size_t good_size(malloc_zone_t *, size_t size)
59 {
60     return size;
61 }
62 
zone_check(malloc_zone_t *)63 static boolean_t zone_check(malloc_zone_t *) /* Consistency checker */
64 {
65     return true;
66 }
67 
zone_print(malloc_zone_t *,boolean_t)68 static void zone_print(malloc_zone_t *, boolean_t) { }
zone_log(malloc_zone_t *,void *)69 static void zone_log(malloc_zone_t *, void *) {}
zone_force_lock(malloc_zone_t *)70 static void zone_force_lock(malloc_zone_t *) {}
zone_force_unlock(malloc_zone_t *)71 static void zone_force_unlock(malloc_zone_t *) {}
72 
zone_statistics(malloc_zone_t *,malloc_statistics_t * s)73 static void zone_statistics(malloc_zone_t *, malloc_statistics_t *s)
74 {
75     s->blocks_in_use = 0;
76     s->size_in_use = s->max_size_in_use = s->size_allocated = 0;
77 }
78 
zone_locked(malloc_zone_t *)79 static boolean_t zone_locked(malloc_zone_t *)
80 {
81     return false;
82 }
83 
impl_zone_enable_discharge_checking(malloc_zone_t *)84 static boolean_t impl_zone_enable_discharge_checking(malloc_zone_t *)
85 {
86     return false;
87 }
88 
impl_zone_disable_discharge_checking(malloc_zone_t *)89 static void impl_zone_disable_discharge_checking(malloc_zone_t *) {}
impl_zone_discharge(malloc_zone_t *,void *)90 static void impl_zone_discharge(malloc_zone_t *, void *) {}
impl_zone_destroy(struct _malloc_zone_t *)91 static void impl_zone_destroy(struct _malloc_zone_t *) {}
92 
93 /* note: impl_malloc_usable_size() is called for each free() call, so it must be fast */
impl_malloc_usable_size(struct _malloc_zone_t *,const void * ptr)94 static size_t impl_malloc_usable_size(struct _malloc_zone_t *, const void *ptr)
95 {
96     // malloc_usable_size() is used by macOS* to recognize which memory manager
97     // allocated the address, so our wrapper must not redirect to the original function.
98     return __TBB_malloc_safer_msize(const_cast<void*>(ptr), nullptr);
99 }
100 
101 static void *impl_malloc(struct _malloc_zone_t *, size_t size);
102 static void *impl_calloc(struct _malloc_zone_t *, size_t num_items, size_t size);
103 static void *impl_valloc(struct _malloc_zone_t *, size_t size);
104 static void impl_free(struct _malloc_zone_t *, void *ptr);
105 static void *impl_realloc(struct _malloc_zone_t *, void *ptr, size_t size);
106 static void *impl_memalign(struct _malloc_zone_t *, size_t alignment, size_t size);
107 
108 /* ptr is in zone and have reported size */
impl_free_definite_size(struct _malloc_zone_t *,void * ptr,size_t size)109 static void impl_free_definite_size(struct _malloc_zone_t*, void *ptr, size_t size)
110 {
111     __TBB_malloc_free_definite_size(ptr, size);
112 }
113 
114 /* Empty out caches in the face of memory pressure. */
impl_pressure_relief(struct _malloc_zone_t *,size_t)115 static size_t impl_pressure_relief(struct _malloc_zone_t *, size_t  /* goal */)
116 {
117     return 0;
118 }
119 
120 static malloc_zone_t *system_zone = nullptr;
121 
122 struct DoMallocReplacement {
DoMallocReplacementDoMallocReplacement123     DoMallocReplacement() {
124         static malloc_introspection_t introspect;
125         memset(&introspect, 0, sizeof(malloc_introspection_t));
126         static malloc_zone_t zone;
127         memset(&zone, 0, sizeof(malloc_zone_t));
128 
129         introspect.enumerator = &enumerator;
130         introspect.good_size = &good_size;
131         introspect.check = &zone_check;
132         introspect.print = &zone_print;
133         introspect.log = zone_log;
134         introspect.force_lock = &zone_force_lock;
135         introspect.force_unlock = &zone_force_unlock;
136         introspect.statistics = zone_statistics;
137         introspect.zone_locked = &zone_locked;
138         introspect.enable_discharge_checking = &impl_zone_enable_discharge_checking;
139         introspect.disable_discharge_checking = &impl_zone_disable_discharge_checking;
140         introspect.discharge = &impl_zone_discharge;
141 
142         zone.size = &impl_malloc_usable_size;
143         zone.malloc = &impl_malloc;
144         zone.calloc = &impl_calloc;
145         zone.valloc = &impl_valloc;
146         zone.free = &impl_free;
147         zone.realloc = &impl_realloc;
148         zone.destroy = &impl_zone_destroy;
149         zone.zone_name = "tbbmalloc";
150         zone.introspect = &introspect;
151         zone.version = 8;
152         zone.memalign = impl_memalign;
153         zone.free_definite_size = &impl_free_definite_size;
154         zone.pressure_relief = &impl_pressure_relief;
155 
156         // make sure that default purgeable zone is initialized
157         malloc_default_purgeable_zone();
158         void* ptr = malloc(1);
159         // get all registered memory zones
160         unsigned zcount = 0;
161         malloc_zone_t** zone_array = nullptr;
162         kern_return_t errorcode = malloc_get_all_zones(mach_task_self(),nullptr,(vm_address_t**)&zone_array,&zcount);
163         if (!errorcode && zone_array && zcount>0) {
164             // find the zone that allocated ptr
165             for (unsigned i=0; i<zcount; ++i) {
166                 malloc_zone_t* z = zone_array[i];
167                 if (z && z->size(z,ptr)>0) { // the right one is found
168                     system_zone = z;
169                     break;
170                 }
171             }
172         }
173         free(ptr);
174 
175         malloc_zone_register(&zone);
176         if (system_zone) {
177             // after unregistration of the system zone, the last registered (i.e. our) zone becomes the default
178             malloc_zone_unregister(system_zone);
179             // register the system zone back
180             malloc_zone_register(system_zone);
181         }
182     }
183 };
184 
185 static DoMallocReplacement doMallocReplacement;
186 
187