| Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 | // ------------------------------------------------------------------------ | 
|  | 41 | const 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 |  | 
|  | 50 | bool hooks_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child, | 
|  | 51 | const char* options); | 
|  | 52 | void hooks_finalize(); | 
|  | 53 | void 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); | 
|  | 56 | ssize_t hooks_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); | 
|  | 57 | void hooks_free_malloc_leak_info(uint8_t* info); | 
|  | 58 | size_t hooks_malloc_usable_size(void* pointer); | 
|  | 59 | void* hooks_malloc(size_t size); | 
|  | 60 | void hooks_free(void* pointer); | 
|  | 61 | void* hooks_memalign(size_t alignment, size_t bytes); | 
|  | 62 | void* hooks_aligned_alloc(size_t alignment, size_t bytes); | 
|  | 63 | void* hooks_realloc(void* pointer, size_t bytes); | 
|  | 64 | void* hooks_calloc(size_t nmemb, size_t bytes); | 
|  | 65 | struct mallinfo hooks_mallinfo(); | 
|  | 66 | int hooks_mallopt(int param, int value); | 
|  | 67 | int hooks_posix_memalign(void** memptr, size_t alignment, size_t size); | 
|  | 68 | int hooks_iterate(uintptr_t base, size_t size, | 
|  | 69 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg); | 
|  | 70 | void hooks_malloc_disable(); | 
|  | 71 | void hooks_malloc_enable(); | 
| Florian Mayer | 2d6030b | 2018-06-05 18:26:53 +0100 | [diff] [blame] | 72 | bool hooks_write_malloc_leak_info(FILE*); | 
| Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 73 |  | 
|  | 74 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
|  | 75 | void* hooks_pvalloc(size_t bytes); | 
|  | 76 | void* hooks_valloc(size_t size); | 
|  | 77 | #endif | 
|  | 78 |  | 
|  | 79 | static void* default_malloc_hook(size_t bytes, const void*) { | 
|  | 80 | return g_dispatch->malloc(bytes); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | static void* default_realloc_hook(void* pointer, size_t bytes, const void*) { | 
|  | 84 | return g_dispatch->realloc(pointer, bytes); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | static void default_free_hook(void* pointer, const void*) { | 
|  | 88 | g_dispatch->free(pointer); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | static 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 |  | 
|  | 98 | bool 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 |  | 
|  | 107 | void hooks_finalize() { | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | void 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 |  | 
|  | 119 | void hooks_free_malloc_leak_info(uint8_t*) { | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | size_t hooks_malloc_usable_size(void* pointer) { | 
|  | 123 | return g_dispatch->malloc_usable_size(pointer); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | void* 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 |  | 
|  | 133 | void 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 |  | 
|  | 140 | void* 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 |  | 
|  | 147 | void* 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 |  | 
|  | 154 | void* 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 |  | 
|  | 169 | struct mallinfo hooks_mallinfo() { | 
|  | 170 | return g_dispatch->mallinfo(); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | int hooks_mallopt(int param, int value) { | 
|  | 174 | return g_dispatch->mallopt(param, value); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | void* hooks_aligned_alloc(size_t alignment, size_t size) { | 
|  | 178 | if (__memalign_hook != nullptr && __memalign_hook != default_memalign_hook) { | 
|  | 179 | if (!powerof2(alignment)) { | 
|  | 180 | 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 |  | 
|  | 192 | int 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 |  | 
|  | 206 | int hooks_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*) { | 
|  | 207 | return 0; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | void hooks_malloc_disable() { | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | void hooks_malloc_enable() { | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | ssize_t hooks_malloc_backtrace(void*, uintptr_t*, size_t) { | 
|  | 217 | return 0; | 
|  | 218 | } | 
|  | 219 |  | 
| Florian Mayer | 2d6030b | 2018-06-05 18:26:53 +0100 | [diff] [blame] | 220 | bool hooks_write_malloc_leak_info(FILE*) { | 
|  | 221 | return true; | 
|  | 222 | } | 
|  | 223 |  | 
| Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 224 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
|  | 225 | void* 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 |  | 
|  | 236 | void* hooks_valloc(size_t size) { | 
|  | 237 | return hooks_memalign(getpagesize(), size); | 
|  | 238 | } | 
|  | 239 | #endif |