1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 // Copyright (c) 2011, Google Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // ---
32 // Author: Craig Silverstein <opensource@google.com>
33 //
34 // Used to override malloc routines on OS X systems.  We use the
35 // malloc-zone functionality built into OS X to register our malloc
36 // routine.
37 //
38 // 1) We used to use the normal 'override weak libc malloc/etc'
39 // technique for OS X.  This is not optimal because mach does not
40 // support the 'alias' attribute, so we had to have forwarding
41 // functions.  It also does not work very well with OS X shared
42 // libraries (dylibs) -- in general, the shared libs don't use
43 // tcmalloc unless run with the DYLD_FORCE_FLAT_NAMESPACE envvar.
44 //
45 // 2) Another approach would be to use an interposition array:
46 //      static const interpose_t interposers[] __attribute__((section("__DATA, __interpose"))) = {
47 //        { (void *)tc_malloc, (void *)malloc },
48 //        { (void *)tc_free, (void *)free },
49 //      };
50 // This requires the user to set the DYLD_INSERT_LIBRARIES envvar, so
51 // is not much better.
52 //
53 // 3) Registering a new malloc zone avoids all these issues:
54 //  http://www.opensource.apple.com/source/Libc/Libc-583/include/malloc/malloc.h
55 //  http://www.opensource.apple.com/source/Libc/Libc-583/gen/malloc.c
56 // If we make tcmalloc the default malloc zone (undocumented but
57 // possible) then all new allocs use it, even those in shared
58 // libraries.  Allocs done before tcmalloc was installed, or in libs
59 // that aren't using tcmalloc for some reason, will correctly go
60 // through the malloc-zone interface when free-ing, and will pick up
61 // the libc free rather than tcmalloc free.  So it should "never"
62 // cause a crash (famous last words).
63 //
64 // 4) The routines one must define for one's own malloc have changed
65 // between OS X versions.  This requires some hoops on our part, but
66 // is only really annoying when it comes to posix_memalign.  The right
67 // behavior there depends on what OS version tcmalloc was compiled on,
68 // but also what OS version the program is running on.  For now, we
69 // punt and don't implement our own posix_memalign.  Apps that really
70 // care can use tc_posix_memalign directly.
71 
72 #ifndef TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_
73 #define TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_
74 
75 #include <config.h>
76 #ifdef HAVE_FEATURES_H
77 #include <features.h>
78 #endif
79 #include <gperftools/tcmalloc.h>
80 
81 #if !defined(__APPLE__)
82 # error libc_override_glibc-osx.h is for OS X distributions only.
83 #endif
84 
85 #include <AvailabilityMacros.h>
86 #include <malloc/malloc.h>
87 
88 namespace tcmalloc {
89   void CentralCacheLockAll();
90   void CentralCacheUnlockAll();
91 }
92 
93 // from AvailabilityMacros.h
94 #if defined(MAC_OS_X_VERSION_10_6) && \
95     MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
96 extern "C" {
97   // This function is only available on 10.6 (and later) but the
98   // LibSystem headers do not use AvailabilityMacros.h to handle weak
99   // importing automatically.  This prototype is a copy of the one in
100   // <malloc/malloc.h> with the WEAK_IMPORT_ATTRBIUTE added.
101   extern malloc_zone_t *malloc_default_purgeable_zone(void)
102       WEAK_IMPORT_ATTRIBUTE;
103 }
104 #endif
105 
106 // We need to provide wrappers around all the libc functions.
107 namespace {
mz_size(malloc_zone_t * zone,const void * ptr)108 size_t mz_size(malloc_zone_t* zone, const void* ptr) {
109   if (MallocExtension::instance()->GetOwnership(ptr) != MallocExtension::kOwned)
110     return 0;  // malloc_zone semantics: return 0 if we don't own the memory
111 
112   // TODO(csilvers): change this method to take a const void*, one day.
113   return MallocExtension::instance()->GetAllocatedSize(const_cast<void*>(ptr));
114 }
115 
mz_malloc(malloc_zone_t * zone,size_t size)116 void* mz_malloc(malloc_zone_t* zone, size_t size) {
117   return tc_malloc(size);
118 }
119 
mz_calloc(malloc_zone_t * zone,size_t num_items,size_t size)120 void* mz_calloc(malloc_zone_t* zone, size_t num_items, size_t size) {
121   return tc_calloc(num_items, size);
122 }
123 
mz_valloc(malloc_zone_t * zone,size_t size)124 void* mz_valloc(malloc_zone_t* zone, size_t size) {
125   return tc_valloc(size);
126 }
127 
mz_free(malloc_zone_t * zone,void * ptr)128 void mz_free(malloc_zone_t* zone, void* ptr) {
129   return tc_free(ptr);
130 }
131 
mz_realloc(malloc_zone_t * zone,void * ptr,size_t size)132 void* mz_realloc(malloc_zone_t* zone, void* ptr, size_t size) {
133   return tc_realloc(ptr, size);
134 }
135 
mz_memalign(malloc_zone_t * zone,size_t align,size_t size)136 void* mz_memalign(malloc_zone_t* zone, size_t align, size_t size) {
137   return tc_memalign(align, size);
138 }
139 
mz_destroy(malloc_zone_t * zone)140 void mz_destroy(malloc_zone_t* zone) {
141   // A no-op -- we will not be destroyed!
142 }
143 
144 // malloc_introspection callbacks.  I'm not clear on what all of these do.
mi_enumerator(task_t task,void *,unsigned type_mask,vm_address_t zone_address,memory_reader_t reader,vm_range_recorder_t recorder)145 kern_return_t mi_enumerator(task_t task, void *,
146                             unsigned type_mask, vm_address_t zone_address,
147                             memory_reader_t reader,
148                             vm_range_recorder_t recorder) {
149   // Should enumerate all the pointers we have.  Seems like a lot of work.
150   return KERN_FAILURE;
151 }
152 
mi_good_size(malloc_zone_t * zone,size_t size)153 size_t mi_good_size(malloc_zone_t *zone, size_t size) {
154   // I think it's always safe to return size, but we maybe could do better.
155   return size;
156 }
157 
mi_check(malloc_zone_t * zone)158 boolean_t mi_check(malloc_zone_t *zone) {
159   return MallocExtension::instance()->VerifyAllMemory();
160 }
161 
mi_print(malloc_zone_t * zone,boolean_t verbose)162 void mi_print(malloc_zone_t *zone, boolean_t verbose) {
163   int bufsize = 8192;
164   if (verbose)
165     bufsize = 102400;   // I picked this size arbitrarily
166   char* buffer = new char[bufsize];
167   MallocExtension::instance()->GetStats(buffer, bufsize);
168   fprintf(stdout, "%s", buffer);
169   delete[] buffer;
170 }
171 
mi_log(malloc_zone_t * zone,void * address)172 void mi_log(malloc_zone_t *zone, void *address) {
173   // I don't think we support anything like this
174 }
175 
mi_force_lock(malloc_zone_t * zone)176 void mi_force_lock(malloc_zone_t *zone) {
177   tcmalloc::CentralCacheLockAll();
178 }
179 
mi_force_unlock(malloc_zone_t * zone)180 void mi_force_unlock(malloc_zone_t *zone) {
181   tcmalloc::CentralCacheUnlockAll();
182 }
183 
mi_statistics(malloc_zone_t * zone,malloc_statistics_t * stats)184 void mi_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {
185   // TODO(csilvers): figure out how to fill these out
186   stats->blocks_in_use = 0;
187   stats->size_in_use = 0;
188   stats->max_size_in_use = 0;
189   stats->size_allocated = 0;
190 }
191 
mi_zone_locked(malloc_zone_t * zone)192 boolean_t mi_zone_locked(malloc_zone_t *zone) {
193   return false;  // Hopefully unneeded by us!
194 }
195 
196 }  // unnamed namespace
197 
198 // OS X doesn't have pvalloc, cfree, malloc_statc, etc, so we can just
199 // define our own. :-)  OS X supplies posix_memalign in some versions
200 // but not others, either strongly or weakly linked, in a way that's
201 // difficult enough to code to correctly, that I just don't try to
202 // support either memalign() or posix_memalign().  If you need them
203 // and are willing to code to tcmalloc, you can use tc_posix_memalign().
204 extern "C" {
cfree(void * p)205   void  cfree(void* p)                   { tc_cfree(p);               }
pvalloc(size_t s)206   void* pvalloc(size_t s)                { return tc_pvalloc(s);      }
malloc_stats(void)207   void malloc_stats(void)                { tc_malloc_stats();         }
mallopt(int cmd,int v)208   int mallopt(int cmd, int v)            { return tc_mallopt(cmd, v); }
209   // No struct mallinfo on OS X, so don't define mallinfo().
210   // An alias for malloc_size(), which OS X defines.
malloc_usable_size(void * p)211   size_t malloc_usable_size(void* p)     { return tc_malloc_size(p); }
212 }  // extern "C"
213 
get_default_zone()214 static malloc_zone_t *get_default_zone() {
215    malloc_zone_t **zones = NULL;
216    unsigned int num_zones = 0;
217 
218    /*
219     * On OSX 10.12, malloc_default_zone returns a special zone that is not
220     * present in the list of registered zones. That zone uses a "lite zone"
221     * if one is present (apparently enabled when malloc stack logging is
222     * enabled), or the first registered zone otherwise. In practice this
223     * means unless malloc stack logging is enabled, the first registered
224     * zone is the default.
225     * So get the list of zones to get the first one, instead of relying on
226     * malloc_default_zone.
227     */
228    if (KERN_SUCCESS != malloc_get_all_zones(0, NULL, (vm_address_t**) &zones,
229                                             &num_zones)) {
230        /* Reset the value in case the failure happened after it was set. */
231        num_zones = 0;
232    }
233 
234    if (num_zones)
235      return zones[0];
236 
237    return malloc_default_zone();
238 }
239 
240 
ReplaceSystemAlloc()241 static void ReplaceSystemAlloc() {
242   static malloc_introspection_t tcmalloc_introspection;
243   memset(&tcmalloc_introspection, 0, sizeof(tcmalloc_introspection));
244 
245   tcmalloc_introspection.enumerator = &mi_enumerator;
246   tcmalloc_introspection.good_size = &mi_good_size;
247   tcmalloc_introspection.check = &mi_check;
248   tcmalloc_introspection.print = &mi_print;
249   tcmalloc_introspection.log = &mi_log;
250   tcmalloc_introspection.force_lock = &mi_force_lock;
251   tcmalloc_introspection.force_unlock = &mi_force_unlock;
252 
253   static malloc_zone_t tcmalloc_zone;
254   memset(&tcmalloc_zone, 0, sizeof(malloc_zone_t));
255 
256   // Start with a version 4 zone which is used for OS X 10.4 and 10.5.
257   tcmalloc_zone.version = 4;
258   tcmalloc_zone.zone_name = "tcmalloc";
259   tcmalloc_zone.size = &mz_size;
260   tcmalloc_zone.malloc = &mz_malloc;
261   tcmalloc_zone.calloc = &mz_calloc;
262   tcmalloc_zone.valloc = &mz_valloc;
263   tcmalloc_zone.free = &mz_free;
264   tcmalloc_zone.realloc = &mz_realloc;
265   tcmalloc_zone.destroy = &mz_destroy;
266   tcmalloc_zone.batch_malloc = NULL;
267   tcmalloc_zone.batch_free = NULL;
268   tcmalloc_zone.introspect = &tcmalloc_introspection;
269 
270   // from AvailabilityMacros.h
271 #if defined(MAC_OS_X_VERSION_10_6) && \
272     MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
273   // Switch to version 6 on OSX 10.6 to support memalign.
274   tcmalloc_zone.version = 6;
275   tcmalloc_zone.free_definite_size = NULL;
276   tcmalloc_zone.memalign = &mz_memalign;
277   tcmalloc_introspection.zone_locked = &mi_zone_locked;
278 
279   // Request the default purgable zone to force its creation. The
280   // current default zone is registered with the purgable zone for
281   // doing tiny and small allocs.  Sadly, it assumes that the default
282   // zone is the szone implementation from OS X and will crash if it
283   // isn't.  By creating the zone now, this will be true and changing
284   // the default zone won't cause a problem.  This only needs to
285   // happen when actually running on OS X 10.6 and higher (note the
286   // ifdef above only checks if we were *compiled* with 10.6 or
287   // higher; at runtime we have to check if this symbol is defined.)
288   if (malloc_default_purgeable_zone) {
289     malloc_default_purgeable_zone();
290   }
291 #endif
292 
293   // Register the tcmalloc zone. At this point, it will not be the
294   // default zone.
295   malloc_zone_register(&tcmalloc_zone);
296 
297   // Unregister and reregister the default zone.  Unregistering swaps
298   // the specified zone with the last one registered which for the
299   // default zone makes the more recently registered zone the default
300   // zone.  The default zone is then re-registered to ensure that
301   // allocations made from it earlier will be handled correctly.
302   // Things are not guaranteed to work that way, but it's how they work now.
303   malloc_zone_t *default_zone = get_default_zone();
304   malloc_zone_unregister(default_zone);
305   malloc_zone_register(default_zone);
306 }
307 
308 #endif  // TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_
309