Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 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 systems that are using glibc. |
| 35 | |
| 36 | #ifndef TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ |
| 37 | #define TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ |
| 38 | |
| 39 | #include <config.h> |
| 40 | #include <features.h> // for __GLIBC__ |
| 41 | #ifdef HAVE_SYS_CDEFS_H |
| 42 | #include <sys/cdefs.h> // for __THROW |
| 43 | #endif |
| 44 | #include <gperftools/tcmalloc.h> |
| 45 | |
| 46 | #ifndef __GLIBC__ |
| 47 | # error libc_override_glibc.h is for glibc distributions only. |
| 48 | #endif |
| 49 | |
| 50 | // In glibc, the memory-allocation methods are weak symbols, so we can |
| 51 | // just override them with our own. If we're using gcc, we can use |
| 52 | // __attribute__((alias)) to do the overriding easily (exception: |
| 53 | // Mach-O, which doesn't support aliases). Otherwise we have to use a |
| 54 | // function call. |
| 55 | #if !defined(__GNUC__) || defined(__MACH__) |
| 56 | |
| 57 | // This also defines ReplaceSystemAlloc(). |
| 58 | # include "libc_override_redefine.h" // defines functions malloc()/etc |
| 59 | |
| 60 | #else // #if !defined(__GNUC__) || defined(__MACH__) |
| 61 | |
| 62 | // If we get here, we're a gcc system, so do all the overriding we do |
| 63 | // with gcc. This does the overriding of all the 'normal' memory |
| 64 | // allocation. This also defines ReplaceSystemAlloc(). |
| 65 | # include "libc_override_gcc_and_weak.h" |
| 66 | |
| 67 | // We also have to do some glibc-specific overriding. Some library |
| 68 | // routines on RedHat 9 allocate memory using malloc() and free it |
| 69 | // using __libc_free() (or vice-versa). Since we provide our own |
| 70 | // implementations of malloc/free, we need to make sure that the |
| 71 | // __libc_XXX variants (defined as part of glibc) also point to the |
| 72 | // same implementations. Since it only matters for redhat, we |
| 73 | // do it inside the gcc #ifdef, since redhat uses gcc. |
| 74 | // TODO(csilvers): only do this if we detect we're an old enough glibc? |
| 75 | |
| 76 | #define ALIAS(tc_fn) __attribute__ ((alias (#tc_fn))) |
| 77 | extern "C" { |
| 78 | void* __libc_malloc(size_t size) ALIAS(tc_malloc); |
| 79 | void __libc_free(void* ptr) ALIAS(tc_free); |
| 80 | void* __libc_realloc(void* ptr, size_t size) ALIAS(tc_realloc); |
| 81 | void* __libc_calloc(size_t n, size_t size) ALIAS(tc_calloc); |
| 82 | void __libc_cfree(void* ptr) ALIAS(tc_cfree); |
| 83 | void* __libc_memalign(size_t align, size_t s) ALIAS(tc_memalign); |
| 84 | void* __libc_valloc(size_t size) ALIAS(tc_valloc); |
| 85 | void* __libc_pvalloc(size_t size) ALIAS(tc_pvalloc); |
| 86 | int __posix_memalign(void** r, size_t a, size_t s) ALIAS(tc_posix_memalign); |
| 87 | } // extern "C" |
| 88 | #undef ALIAS |
| 89 | |
| 90 | #endif // #if defined(__GNUC__) && !defined(__MACH__) |
| 91 | |
| 92 | |
| 93 | // We also have to hook libc malloc. While our work with weak symbols |
| 94 | // should make sure libc malloc is never called in most situations, it |
| 95 | // can be worked around by shared libraries with the DEEPBIND |
| 96 | // environment variable set. The below hooks libc to call our malloc |
| 97 | // routines even in that situation. In other situations, this hook |
| 98 | // should never be called. |
| 99 | extern "C" { |
| 100 | static void* glibc_override_malloc(size_t size, const void *caller) { |
| 101 | return tc_malloc(size); |
| 102 | } |
| 103 | static void* glibc_override_realloc(void *ptr, size_t size, |
| 104 | const void *caller) { |
| 105 | return tc_realloc(ptr, size); |
| 106 | } |
| 107 | static void glibc_override_free(void *ptr, const void *caller) { |
| 108 | tc_free(ptr); |
| 109 | } |
| 110 | static void* glibc_override_memalign(size_t align, size_t size, |
| 111 | const void *caller) { |
| 112 | return tc_memalign(align, size); |
| 113 | } |
| 114 | |
| 115 | // We should be using __malloc_initialize_hook here, like the #if 0 |
| 116 | // code below. (See http://swoolley.org/man.cgi/3/malloc_hook.) |
| 117 | // However, this causes weird linker errors with programs that link |
| 118 | // with -static, so instead we just assign the vars directly at |
| 119 | // static-constructor time. That should serve the same effect of |
| 120 | // making sure the hooks are set before the first malloc call the |
| 121 | // program makes. |
| 122 | #if 0 |
| 123 | #include <malloc.h> // for __malloc_hook, etc. |
| 124 | void glibc_override_malloc_init_hook(void) { |
| 125 | __malloc_hook = glibc_override_malloc; |
| 126 | __realloc_hook = glibc_override_realloc; |
| 127 | __free_hook = glibc_override_free; |
| 128 | __memalign_hook = glibc_override_memalign; |
| 129 | } |
| 130 | |
| 131 | void (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_initialize_hook)(void) |
| 132 | = &glibc_override_malloc_init_hook; |
| 133 | #endif |
| 134 | |
| 135 | void* (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_hook)(size_t, const void*) |
| 136 | = &glibc_override_malloc; |
| 137 | void* (* MALLOC_HOOK_MAYBE_VOLATILE __realloc_hook)(void*, size_t, const void*) |
| 138 | = &glibc_override_realloc; |
| 139 | void (* MALLOC_HOOK_MAYBE_VOLATILE __free_hook)(void*, const void*) |
| 140 | = &glibc_override_free; |
| 141 | void* (* MALLOC_HOOK_MAYBE_VOLATILE __memalign_hook)(size_t,size_t, const void*) |
| 142 | = &glibc_override_memalign; |
| 143 | |
| 144 | } // extern "C" |
| 145 | |
| 146 | // No need to write ReplaceSystemAlloc(); one of the #includes above |
| 147 | // did it for us. |
| 148 | |
| 149 | #endif // TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ |