OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifdef BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_GLIBC_WEAK_SYMBOLS_H_ |
| 6 #error This header is meant to be included only once by allocator_shim.cc |
| 7 #endif |
| 8 #define BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_GLIBC_WEAK_SYMBOLS_H_ |
| 9 |
| 10 // Alias the internal Glibc symbols to the shim entry points. |
| 11 // This file is strongly inspired by tcmalloc's libc_override_glibc.h. |
| 12 // Effectively this file does two things: |
| 13 // 1) Re-define the __malloc_hook & co symbols. Those symbols are defined as |
| 14 // weak in glibc and are meant to be defined strongly by client processes |
| 15 // to hook calls initiated from within glibc. |
| 16 // 2) Re-define Glibc-specific symbols (__libc_malloc). The historical reason |
| 17 // is that in the past (in RedHat 9) we had instances of libraries that were |
| 18 // allocating via malloc() and freeing using __libc_free(). |
| 19 // See tcmalloc's libc_override_glibc.h for more context. |
| 20 |
| 21 #include <features.h> // for __GLIBC__ |
| 22 #include <malloc.h> |
| 23 #include <unistd.h> |
| 24 |
| 25 #include <new> |
| 26 |
| 27 #include "base/allocator/allocator_shim_internals.h" |
| 28 |
| 29 // __MALLOC_HOOK_VOLATILE not defined in all Glibc headers. |
| 30 #if !defined(__MALLOC_HOOK_VOLATILE) |
| 31 #define MALLOC_HOOK_MAYBE_VOLATILE /**/ |
| 32 #else |
| 33 #define MALLOC_HOOK_MAYBE_VOLATILE __MALLOC_HOOK_VOLATILE |
| 34 #endif |
| 35 |
| 36 extern "C" { |
| 37 |
| 38 // 1) Re-define malloc_hook weak symbols. |
| 39 namespace { |
| 40 |
| 41 void* GlibcMallocHook(size_t size, const void* caller) { |
| 42 return ShimMalloc(size); |
| 43 } |
| 44 |
| 45 void* GlibcReallocHook(void* ptr, size_t size, const void* caller) { |
| 46 return ShimRealloc(ptr, size); |
| 47 } |
| 48 |
| 49 void GlibcFreeHook(void* ptr, const void* caller) { |
| 50 return ShimFree(ptr); |
| 51 } |
| 52 |
| 53 void* GlibcMemalignHook(size_t align, size_t size, const void* caller) { |
| 54 return ShimMemalign(align, size); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 SHIM_ALWAYS_EXPORT void* (*MALLOC_HOOK_MAYBE_VOLATILE __malloc_hook)( |
| 60 size_t, |
| 61 const void*) = &GlibcMallocHook; |
| 62 |
| 63 SHIM_ALWAYS_EXPORT void* (*MALLOC_HOOK_MAYBE_VOLATILE __realloc_hook)( |
| 64 void*, |
| 65 size_t, |
| 66 const void*) = &GlibcReallocHook; |
| 67 |
| 68 SHIM_ALWAYS_EXPORT void (*MALLOC_HOOK_MAYBE_VOLATILE __free_hook)(void*, |
| 69 const void*) = |
| 70 &GlibcFreeHook; |
| 71 |
| 72 SHIM_ALWAYS_EXPORT void* (*MALLOC_HOOK_MAYBE_VOLATILE __memalign_hook)( |
| 73 size_t, |
| 74 size_t, |
| 75 const void*) = &GlibcMemalignHook; |
| 76 |
| 77 // 2) Redefine libc symbols themselves. |
| 78 |
| 79 SHIM_ALWAYS_EXPORT void* __libc_malloc(size_t size) |
| 80 SHIM_ALIAS_SYMBOL(ShimMalloc); |
| 81 |
| 82 SHIM_ALWAYS_EXPORT void __libc_free(void* ptr) SHIM_ALIAS_SYMBOL(ShimFree); |
| 83 |
| 84 SHIM_ALWAYS_EXPORT void* __libc_realloc(void* ptr, size_t size) |
| 85 SHIM_ALIAS_SYMBOL(ShimRealloc); |
| 86 |
| 87 SHIM_ALWAYS_EXPORT void* __libc_calloc(size_t n, size_t size) |
| 88 SHIM_ALIAS_SYMBOL(ShimCalloc); |
| 89 |
| 90 SHIM_ALWAYS_EXPORT void __libc_cfree(void* ptr) SHIM_ALIAS_SYMBOL(ShimFree); |
| 91 |
| 92 SHIM_ALWAYS_EXPORT void* __libc_memalign(size_t align, size_t s) |
| 93 SHIM_ALIAS_SYMBOL(ShimMemalign); |
| 94 |
| 95 SHIM_ALWAYS_EXPORT void* __libc_valloc(size_t size) |
| 96 SHIM_ALIAS_SYMBOL(ShimValloc); |
| 97 |
| 98 SHIM_ALWAYS_EXPORT void* __libc_pvalloc(size_t size) |
| 99 SHIM_ALIAS_SYMBOL(ShimPvalloc); |
| 100 |
| 101 SHIM_ALWAYS_EXPORT int __posix_memalign(void** r, size_t a, size_t s) |
| 102 SHIM_ALIAS_SYMBOL(ShimPosixMemalign); |
| 103 |
| 104 } // extern "C" |
| 105 |
| 106 // Safety check. |
| 107 #if !defined(__GLIBC__) |
| 108 #error The current platform does not seem to use Glibc. |
| 109 #endif |
OLD | NEW |