15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011, Google Inc. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All rights reserved. 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met: 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions of source code must retain the above copyright 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer. 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions in binary form must reproduce the above 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution. 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Neither the name of Google Inc. nor the names of its 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission. 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --- 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: Craig Silverstein <opensource@google.com> 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Used to override malloc routines on systems that are using glibc. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MALLOC_HOOK_MAYBE_VOLATILE is defined at config.h in the original gperftools. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Chromium does this check with the macro __MALLOC_HOOK_VOLATILE. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// GLibc 2.14+ requires the hook functions be declared volatile, based on the 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// value of the define __MALLOC_HOOK_VOLATILE. For compatibility with 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// older/non-GLibc implementations, provide an empty definition. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(__MALLOC_HOOK_VOLATILE) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MALLOC_HOOK_MAYBE_VOLATILE /**/ 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MALLOC_HOOK_MAYBE_VOLATILE __MALLOC_HOOK_VOLATILE 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <config.h> 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <features.h> // for __GLIBC__ 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_SYS_CDEFS_H 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/cdefs.h> // for __THROW 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <gperftools/tcmalloc.h> 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef __GLIBC__ 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# error libc_override_glibc.h is for glibc distributions only. 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In glibc, the memory-allocation methods are weak symbols, so we can 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// just override them with our own. If we're using gcc, we can use 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// __attribute__((alias)) to do the overriding easily (exception: 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Mach-O, which doesn't support aliases). Otherwise we have to use a 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// function call. 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(__GNUC__) || defined(__MACH__) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This also defines ReplaceSystemAlloc(). 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# include "libc_override_redefine.h" // defines functions malloc()/etc 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else // #if !defined(__GNUC__) || defined(__MACH__) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If we get here, we're a gcc system, so do all the overriding we do 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with gcc. This does the overriding of all the 'normal' memory 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// allocation. This also defines ReplaceSystemAlloc(). 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# include "libc_override_gcc_and_weak.h" 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We also have to do some glibc-specific overriding. Some library 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// routines on RedHat 9 allocate memory using malloc() and free it 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// using __libc_free() (or vice-versa). Since we provide our own 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// implementations of malloc/free, we need to make sure that the 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// __libc_XXX variants (defined as part of glibc) also point to the 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// same implementations. Since it only matters for redhat, we 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// do it inside the gcc #ifdef, since redhat uses gcc. 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(csilvers): only do this if we detect we're an old enough glibc? 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ALIAS(tc_fn) __attribute__ ((alias (#tc_fn))) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" { 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* __libc_malloc(size_t size) ALIAS(tc_malloc); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void __libc_free(void* ptr) ALIAS(tc_free); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* __libc_realloc(void* ptr, size_t size) ALIAS(tc_realloc); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* __libc_calloc(size_t n, size_t size) ALIAS(tc_calloc); 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void __libc_cfree(void* ptr) ALIAS(tc_cfree); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* __libc_memalign(size_t align, size_t s) ALIAS(tc_memalign); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* __libc_valloc(size_t size) ALIAS(tc_valloc); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* __libc_pvalloc(size_t size) ALIAS(tc_pvalloc); 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int __posix_memalign(void** r, size_t a, size_t s) ALIAS(tc_posix_memalign); 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // extern "C" 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef ALIAS 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // #if defined(__GNUC__) && !defined(__MACH__) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We also have to hook libc malloc. While our work with weak symbols 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should make sure libc malloc is never called in most situations, it 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// can be worked around by shared libraries with the DEEPBIND 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// environment variable set. The below hooks libc to call our malloc 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// routines even in that situation. In other situations, this hook 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should never be called. 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" { 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void* glibc_override_malloc(size_t size, const void *caller) { 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return tc_malloc(size); 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void* glibc_override_realloc(void *ptr, size_t size, 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const void *caller) { 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return tc_realloc(ptr, size); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void glibc_override_free(void *ptr, const void *caller) { 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) tc_free(ptr); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void* glibc_override_memalign(size_t align, size_t size, 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const void *caller) { 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return tc_memalign(align, size); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We should be using __malloc_initialize_hook here, like the #if 0 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// code below. (See http://swoolley.org/man.cgi/3/malloc_hook.) 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// However, this causes weird linker errors with programs that link 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with -static, so instead we just assign the vars directly at 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static-constructor time. That should serve the same effect of 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// making sure the hooks are set before the first malloc call the 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// program makes. 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if 0 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <malloc.h> // for __malloc_hook, etc. 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void glibc_override_malloc_init_hook(void) { 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) __malloc_hook = glibc_override_malloc; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) __realloc_hook = glibc_override_realloc; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) __free_hook = glibc_override_free; 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) __memalign_hook = glibc_override_memalign; 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_initialize_hook)(void) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) = &glibc_override_malloc_init_hook; 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_hook)(size_t, const void*) 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) = &glibc_override_malloc; 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* (* MALLOC_HOOK_MAYBE_VOLATILE __realloc_hook)(void*, size_t, const void*) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) = &glibc_override_realloc; 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void (* MALLOC_HOOK_MAYBE_VOLATILE __free_hook)(void*, const void*) 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) = &glibc_override_free; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* (* MALLOC_HOOK_MAYBE_VOLATILE __memalign_hook)(size_t,size_t, const void*) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) = &glibc_override_memalign; 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // extern "C" 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// No need to write ReplaceSystemAlloc(); one of the #includes above 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// did it for us. 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ 160