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
GlibcMallocHook(size_t size,const void * caller)41 void* GlibcMallocHook(size_t size, const void* caller) {
42 return ShimMalloc(size, nullptr);
43 }
44
GlibcReallocHook(void * ptr,size_t size,const void * caller)45 void* GlibcReallocHook(void* ptr, size_t size, const void* caller) {
46 return ShimRealloc(ptr, size, nullptr);
47 }
48
GlibcFreeHook(void * ptr,const void * caller)49 void GlibcFreeHook(void* ptr, const void* caller) {
50 return ShimFree(ptr, nullptr);
51 }
52
GlibcMemalignHook(size_t align,size_t size,const void * caller)53 void* GlibcMemalignHook(size_t align, size_t size, const void* caller) {
54 return ShimMemalign(align, size, nullptr);
55 }
56
57 } // namespace
58
59 __attribute__((visibility("default"))) void* (
60 *MALLOC_HOOK_MAYBE_VOLATILE __malloc_hook)(size_t,
61 const void*) = &GlibcMallocHook;
62
63 __attribute__((visibility("default"))) void* (
64 *MALLOC_HOOK_MAYBE_VOLATILE __realloc_hook)(void*, size_t, const void*) =
65 &GlibcReallocHook;
66
67 __attribute__((visibility("default"))) void (
68 *MALLOC_HOOK_MAYBE_VOLATILE __free_hook)(void*,
69 const void*) = &GlibcFreeHook;
70
71 __attribute__((visibility("default"))) void* (
72 *MALLOC_HOOK_MAYBE_VOLATILE __memalign_hook)(size_t, size_t, const void*) =
73 &GlibcMemalignHook;
74
75 // 2) Redefine libc symbols themselves.
76
__libc_malloc(size_t size)77 SHIM_ALWAYS_EXPORT void* __libc_malloc(size_t size) {
78 return ShimMalloc(size, nullptr);
79 }
80
__libc_free(void * ptr)81 SHIM_ALWAYS_EXPORT void __libc_free(void* ptr) {
82 ShimFree(ptr, nullptr);
83 }
84
__libc_realloc(void * ptr,size_t size)85 SHIM_ALWAYS_EXPORT void* __libc_realloc(void* ptr, size_t size) {
86 return ShimRealloc(ptr, size, nullptr);
87 }
88
__libc_calloc(size_t n,size_t size)89 SHIM_ALWAYS_EXPORT void* __libc_calloc(size_t n, size_t size) {
90 return ShimCalloc(n, size, nullptr);
91 }
92
__libc_cfree(void * ptr)93 SHIM_ALWAYS_EXPORT void __libc_cfree(void* ptr) {
94 return ShimFree(ptr, nullptr);
95 }
96
__libc_memalign(size_t align,size_t s)97 SHIM_ALWAYS_EXPORT void* __libc_memalign(size_t align, size_t s) {
98 return ShimMemalign(align, s, nullptr);
99 }
100
__libc_valloc(size_t size)101 SHIM_ALWAYS_EXPORT void* __libc_valloc(size_t size) {
102 return ShimValloc(size, nullptr);
103 }
104
__libc_pvalloc(size_t size)105 SHIM_ALWAYS_EXPORT void* __libc_pvalloc(size_t size) {
106 return ShimPvalloc(size);
107 }
108
__posix_memalign(void ** r,size_t a,size_t s)109 SHIM_ALWAYS_EXPORT int __posix_memalign(void** r, size_t a, size_t s) {
110 return ShimPosixMemalign(r, a, s);
111 }
112
113 } // extern "C"
114
115 // Safety check.
116 #if !defined(__GLIBC__)
117 #error The target platform does not seem to use Glibc. Disable the allocator \
118 shim by setting use_allocator_shim=false in GN args.
119 #endif
120