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