libc_override_glibc.h 6.2 KB

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