blob: 715a6295b79fc86b589f567fe9cd82c6bb67c009 [file] [log] [blame]
Christopher Ferrisdb478a62018-02-07 18:42:14 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
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
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <malloc.h>
31#include <stdint.h>
32#include <string.h>
33#include <sys/param.h>
34#include <unistd.h>
35
36#include <private/bionic_malloc_dispatch.h>
37
38// ------------------------------------------------------------------------
39// Global Data
40// ------------------------------------------------------------------------
41const MallocDispatch* g_dispatch;
42// ------------------------------------------------------------------------
43
44// ------------------------------------------------------------------------
45// Use C style prototypes for all exported functions. This makes it easy
46// to do dlsym lookups during libc initialization when hooks are enabled.
47// ------------------------------------------------------------------------
48__BEGIN_DECLS
49
50bool hooks_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
51 const char* options);
52void hooks_finalize();
53void hooks_get_malloc_leak_info(
54 uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
55 size_t* backtrace_size);
56ssize_t hooks_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
57void hooks_free_malloc_leak_info(uint8_t* info);
58size_t hooks_malloc_usable_size(void* pointer);
59void* hooks_malloc(size_t size);
60void hooks_free(void* pointer);
61void* hooks_memalign(size_t alignment, size_t bytes);
62void* hooks_aligned_alloc(size_t alignment, size_t bytes);
63void* hooks_realloc(void* pointer, size_t bytes);
64void* hooks_calloc(size_t nmemb, size_t bytes);
65struct mallinfo hooks_mallinfo();
66int hooks_mallopt(int param, int value);
67int hooks_posix_memalign(void** memptr, size_t alignment, size_t size);
68int hooks_iterate(uintptr_t base, size_t size,
69 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
70void hooks_malloc_disable();
71void hooks_malloc_enable();
Florian Mayer2d6030b2018-06-05 18:26:53 +010072bool hooks_write_malloc_leak_info(FILE*);
Christopher Ferrisdb478a62018-02-07 18:42:14 -080073
74#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
75void* hooks_pvalloc(size_t bytes);
76void* hooks_valloc(size_t size);
77#endif
78
79static void* default_malloc_hook(size_t bytes, const void*) {
80 return g_dispatch->malloc(bytes);
81}
82
83static void* default_realloc_hook(void* pointer, size_t bytes, const void*) {
84 return g_dispatch->realloc(pointer, bytes);
85}
86
87static void default_free_hook(void* pointer, const void*) {
88 g_dispatch->free(pointer);
89}
90
91static void* default_memalign_hook(size_t alignment, size_t bytes, const void*) {
92 return g_dispatch->memalign(alignment, bytes);
93}
94
95__END_DECLS
96// ------------------------------------------------------------------------
97
98bool hooks_initialize(const MallocDispatch* malloc_dispatch, int*, const char*) {
99 g_dispatch = malloc_dispatch;
100 __malloc_hook = default_malloc_hook;
101 __realloc_hook = default_realloc_hook;
102 __free_hook = default_free_hook;
103 __memalign_hook = default_memalign_hook;
104 return true;
105}
106
107void hooks_finalize() {
108}
109
110void hooks_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
111 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
112 *info = nullptr;
113 *overall_size = 0;
114 *info_size = 0;
115 *total_memory = 0;
116 *backtrace_size = 0;
117}
118
119void hooks_free_malloc_leak_info(uint8_t*) {
120}
121
122size_t hooks_malloc_usable_size(void* pointer) {
123 return g_dispatch->malloc_usable_size(pointer);
124}
125
126void* hooks_malloc(size_t size) {
127 if (__malloc_hook != nullptr && __malloc_hook != default_malloc_hook) {
128 return __malloc_hook(size, __builtin_return_address(0));
129 }
130 return g_dispatch->malloc(size);
131}
132
133void hooks_free(void* pointer) {
134 if (__free_hook != nullptr && __free_hook != default_free_hook) {
135 return __free_hook(pointer, __builtin_return_address(0));
136 }
137 return g_dispatch->free(pointer);
138}
139
140void* hooks_memalign(size_t alignment, size_t bytes) {
141 if (__memalign_hook != nullptr && __memalign_hook != default_memalign_hook) {
142 return __memalign_hook(alignment, bytes, __builtin_return_address(0));
143 }
144 return g_dispatch->memalign(alignment, bytes);
145}
146
147void* hooks_realloc(void* pointer, size_t bytes) {
148 if (__realloc_hook != nullptr && __realloc_hook != default_realloc_hook) {
149 return __realloc_hook(pointer, bytes, __builtin_return_address(0));
150 }
151 return g_dispatch->realloc(pointer, bytes);
152}
153
154void* hooks_calloc(size_t nmemb, size_t bytes) {
155 if (__malloc_hook != nullptr && __malloc_hook != default_malloc_hook) {
156 size_t size;
157 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
158 return nullptr;
159 }
160 void* ptr = __malloc_hook(size, __builtin_return_address(0));
161 if (ptr != nullptr) {
162 memset(ptr, 0, size);
163 }
164 return ptr;
165 }
166 return g_dispatch->calloc(nmemb, bytes);
167}
168
169struct mallinfo hooks_mallinfo() {
170 return g_dispatch->mallinfo();
171}
172
173int hooks_mallopt(int param, int value) {
174 return g_dispatch->mallopt(param, value);
175}
176
177void* hooks_aligned_alloc(size_t alignment, size_t size) {
178 if (__memalign_hook != nullptr && __memalign_hook != default_memalign_hook) {
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800179 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800180 errno = EINVAL;
181 return nullptr;
182 }
183 void* ptr = __memalign_hook(alignment, size, __builtin_return_address(0));
184 if (ptr == nullptr) {
185 errno = ENOMEM;
186 }
187 return ptr;
188 }
189 return g_dispatch->aligned_alloc(alignment, size);
190}
191
192int hooks_posix_memalign(void** memptr, size_t alignment, size_t size) {
193 if (__memalign_hook != nullptr && __memalign_hook != default_memalign_hook) {
194 if (!powerof2(alignment)) {
195 return EINVAL;
196 }
197 *memptr = __memalign_hook(alignment, size, __builtin_return_address(0));
198 if (*memptr == nullptr) {
199 return ENOMEM;
200 }
201 return 0;
202 }
203 return g_dispatch->posix_memalign(memptr, alignment, size);
204}
205
206int hooks_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*) {
207 return 0;
208}
209
210void hooks_malloc_disable() {
211}
212
213void hooks_malloc_enable() {
214}
215
216ssize_t hooks_malloc_backtrace(void*, uintptr_t*, size_t) {
217 return 0;
218}
219
Florian Mayer2d6030b2018-06-05 18:26:53 +0100220bool hooks_write_malloc_leak_info(FILE*) {
221 return true;
222}
223
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800224#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
225void* hooks_pvalloc(size_t bytes) {
226 size_t pagesize = getpagesize();
227 size_t size = __BIONIC_ALIGN(bytes, pagesize);
228 if (size < bytes) {
229 // Overflow
230 errno = ENOMEM;
231 return nullptr;
232 }
233 return hooks_memalign(pagesize, size);
234}
235
236void* hooks_valloc(size_t size) {
237 return hooks_memalign(getpagesize(), size);
238}
239#endif