| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2012 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 <inttypes.h> | 
|  | 31 | #include <malloc.h> | 
|  | 32 | #include <string.h> | 
|  | 33 | #include <sys/cdefs.h> | 
|  | 34 | #include <sys/param.h> | 
|  | 35 | #include <unistd.h> | 
|  | 36 |  | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 37 | #include <mutex> | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 38 | #include <vector> | 
|  | 39 |  | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 41 | #include <android-base/properties.h> | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 43 | #include <private/bionic_malloc_dispatch.h> | 
|  | 44 |  | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 45 | #include "Config.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 46 | #include "DebugData.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 47 | #include "backtrace.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 48 | #include "debug_disable.h" | 
|  | 49 | #include "debug_log.h" | 
|  | 50 | #include "malloc_debug.h" | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 51 | #include "UnwindBacktrace.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | // ------------------------------------------------------------------------ | 
|  | 54 | // Global Data | 
|  | 55 | // ------------------------------------------------------------------------ | 
|  | 56 | DebugData* g_debug; | 
|  | 57 |  | 
|  | 58 | int* g_malloc_zygote_child; | 
|  | 59 |  | 
|  | 60 | const MallocDispatch* g_dispatch; | 
|  | 61 | // ------------------------------------------------------------------------ | 
|  | 62 |  | 
|  | 63 | // ------------------------------------------------------------------------ | 
|  | 64 | // Use C style prototypes for all exported functions. This makes it easy | 
|  | 65 | // to do dlsym lookups during libc initialization when malloc debug | 
|  | 66 | // is enabled. | 
|  | 67 | // ------------------------------------------------------------------------ | 
|  | 68 | __BEGIN_DECLS | 
|  | 69 |  | 
| Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 70 | bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 71 | const char* options); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 72 | void debug_finalize(); | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 73 | void debug_dump_heap(const char* file_name); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 74 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, | 
|  | 75 | size_t* total_memory, size_t* backtrace_size); | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 76 | bool debug_write_malloc_leak_info(FILE* fp); | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 77 | ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 78 | void debug_free_malloc_leak_info(uint8_t* info); | 
|  | 79 | size_t debug_malloc_usable_size(void* pointer); | 
|  | 80 | void* debug_malloc(size_t size); | 
|  | 81 | void debug_free(void* pointer); | 
| Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 82 | void* debug_aligned_alloc(size_t alignment, size_t size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 83 | void* debug_memalign(size_t alignment, size_t bytes); | 
|  | 84 | void* debug_realloc(void* pointer, size_t bytes); | 
|  | 85 | void* debug_calloc(size_t nmemb, size_t bytes); | 
|  | 86 | struct mallinfo debug_mallinfo(); | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 87 | int debug_mallopt(int param, int value); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 88 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 89 | int debug_iterate(uintptr_t base, size_t size, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 90 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 91 | void debug_malloc_disable(); | 
|  | 92 | void debug_malloc_enable(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
|  | 95 | void* debug_pvalloc(size_t bytes); | 
|  | 96 | void* debug_valloc(size_t size); | 
|  | 97 | #endif | 
|  | 98 |  | 
|  | 99 | __END_DECLS | 
|  | 100 | // ------------------------------------------------------------------------ | 
|  | 101 |  | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 102 | static void InitAtfork() { | 
|  | 103 | static pthread_once_t atfork_init = PTHREAD_ONCE_INIT; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 104 | pthread_once(&atfork_init, []() { | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 105 | pthread_atfork( | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 106 | []() { | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 107 | if (g_debug != nullptr) { | 
|  | 108 | g_debug->PrepareFork(); | 
|  | 109 | } | 
|  | 110 | }, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 111 | []() { | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 112 | if (g_debug != nullptr) { | 
|  | 113 | g_debug->PostForkParent(); | 
|  | 114 | } | 
|  | 115 | }, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 116 | []() { | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 117 | if (g_debug != nullptr) { | 
|  | 118 | g_debug->PostForkChild(); | 
|  | 119 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 120 | }); | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 121 | }); | 
|  | 122 | } | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 123 |  | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 124 | void BacktraceAndLog() { | 
|  | 125 | if (g_debug->config().options() & BACKTRACE_FULL) { | 
|  | 126 | std::vector<uintptr_t> frames; | 
|  | 127 | std::vector<unwindstack::LocalFrameData> frames_info; | 
|  | 128 | if (!Unwind(&frames, &frames_info, 256)) { | 
|  | 129 | error_log("  Backtrace failed to get any frames."); | 
|  | 130 | } else { | 
|  | 131 | UnwindLog(frames_info); | 
|  | 132 | } | 
|  | 133 | } else { | 
|  | 134 | std::vector<uintptr_t> frames(256); | 
|  | 135 | size_t num_frames = backtrace_get(frames.data(), frames.size()); | 
|  | 136 | if (num_frames == 0) { | 
|  | 137 | error_log("  Backtrace failed to get any frames."); | 
|  | 138 | } else { | 
|  | 139 | backtrace_log(frames.data(), num_frames); | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 | } | 
|  | 143 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 144 | static void LogError(const void* pointer, const char* error_str) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 145 | error_log(LOG_DIVIDER); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 146 | error_log("+++ ALLOCATION %p %s", pointer, error_str); | 
|  | 147 |  | 
|  | 148 | // If we are tracking already freed pointers, check to see if this is | 
|  | 149 | // one so we can print extra information. | 
|  | 150 | if (g_debug->config().options() & FREE_TRACK) { | 
|  | 151 | PointerData::LogFreeBacktrace(pointer); | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 152 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 153 |  | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 154 | error_log("Backtrace at time of failure:"); | 
|  | 155 | BacktraceAndLog(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 156 | error_log(LOG_DIVIDER); | 
|  | 157 | } | 
|  | 158 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 159 | static bool VerifyPointer(const void* pointer, const char* function_name) { | 
|  | 160 | if (g_debug->HeaderEnabled()) { | 
|  | 161 | Header* header = g_debug->GetHeader(pointer); | 
|  | 162 | if (header->tag != DEBUG_TAG) { | 
|  | 163 | std::string error_str; | 
|  | 164 | if (header->tag == DEBUG_FREE_TAG) { | 
|  | 165 | error_str = std::string("USED AFTER FREE (") + function_name + ")"; | 
|  | 166 | } else { | 
|  | 167 | error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag, | 
|  | 168 | function_name); | 
|  | 169 | } | 
|  | 170 | LogError(pointer, error_str.c_str()); | 
|  | 171 | return false; | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | if (g_debug->TrackPointers()) { | 
|  | 176 | if (!PointerData::Exists(pointer)) { | 
|  | 177 | std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")"); | 
|  | 178 | LogError(pointer, error_str.c_str()); | 
|  | 179 | return false; | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 | return true; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | static size_t InternalMallocUsableSize(void* pointer) { | 
|  | 186 | if (g_debug->HeaderEnabled()) { | 
|  | 187 | return g_debug->GetHeader(pointer)->usable_size; | 
|  | 188 | } else { | 
|  | 189 | return g_dispatch->malloc_usable_size(pointer); | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 193 | static void* InitHeader(Header* header, void* orig_pointer, size_t size) { | 
|  | 194 | header->tag = DEBUG_TAG; | 
|  | 195 | header->orig_pointer = orig_pointer; | 
|  | 196 | header->size = size; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 197 | header->usable_size = g_dispatch->malloc_usable_size(orig_pointer); | 
|  | 198 | if (header->usable_size == 0) { | 
|  | 199 | g_dispatch->free(orig_pointer); | 
|  | 200 | return nullptr; | 
|  | 201 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 202 | header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) - | 
|  | 203 | reinterpret_cast<uintptr_t>(orig_pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 204 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 205 | if (g_debug->config().options() & FRONT_GUARD) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 206 | uint8_t* guard = g_debug->GetFrontGuard(header); | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 207 | memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes()); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 210 | if (g_debug->config().options() & REAR_GUARD) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 211 | uint8_t* guard = g_debug->GetRearGuard(header); | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 212 | memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes()); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 213 | // If the rear guard is enabled, set the usable size to the exact size | 
|  | 214 | // of the allocation. | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 215 | header->usable_size = header->size; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
|  | 218 | return g_debug->GetPointer(header); | 
|  | 219 | } | 
|  | 220 |  | 
| Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 221 | bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 222 | const char* options) { | 
| Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 223 | if (malloc_zygote_child == nullptr || options == nullptr) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 224 | return false; | 
|  | 225 | } | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 226 |  | 
|  | 227 | InitAtfork(); | 
|  | 228 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 229 | g_malloc_zygote_child = malloc_zygote_child; | 
|  | 230 |  | 
|  | 231 | g_dispatch = malloc_dispatch; | 
|  | 232 |  | 
|  | 233 | if (!DebugDisableInitialize()) { | 
|  | 234 | return false; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | DebugData* debug = new DebugData(); | 
| Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 238 | if (!debug->Initialize(options)) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 239 | delete debug; | 
|  | 240 | DebugDisableFinalize(); | 
|  | 241 | return false; | 
|  | 242 | } | 
|  | 243 | g_debug = debug; | 
|  | 244 |  | 
|  | 245 | // Always enable the backtrace code since we will use it in a number | 
|  | 246 | // of different error cases. | 
|  | 247 | backtrace_startup(); | 
|  | 248 |  | 
|  | 249 | return true; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | void debug_finalize() { | 
|  | 253 | if (g_debug == nullptr) { | 
|  | 254 | return; | 
|  | 255 | } | 
|  | 256 |  | 
| Christopher Ferris | 97b4747 | 2018-07-10 14:45:24 -0700 | [diff] [blame] | 257 | // Turn off capturing allocations calls. | 
|  | 258 | DebugDisableSet(true); | 
|  | 259 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 260 | if (g_debug->config().options() & FREE_TRACK) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 261 | PointerData::VerifyAllFreed(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 264 | if (g_debug->config().options() & LEAK_TRACK) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 265 | PointerData::LogLeaks(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 268 | if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 269 | debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt", | 
|  | 270 | g_debug->config().backtrace_dump_prefix().c_str(), | 
| Christopher Ferris | 97b4747 | 2018-07-10 14:45:24 -0700 | [diff] [blame] | 271 | getpid()).c_str()); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 274 | backtrace_shutdown(); | 
|  | 275 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 276 | delete g_debug; | 
|  | 277 | g_debug = nullptr; | 
|  | 278 |  | 
|  | 279 | DebugDisableFinalize(); | 
|  | 280 | } | 
|  | 281 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 282 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, | 
|  | 283 | size_t* total_memory, size_t* backtrace_size) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 284 | ScopedDisableDebugCalls disable; | 
|  | 285 |  | 
|  | 286 | // Verify the arguments. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 287 | if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr || | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 288 | backtrace_size == nullptr) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 289 | error_log("get_malloc_leak_info: At least one invalid parameter."); | 
|  | 290 | return; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | *info = nullptr; | 
|  | 294 | *overall_size = 0; | 
|  | 295 | *info_size = 0; | 
|  | 296 | *total_memory = 0; | 
|  | 297 | *backtrace_size = 0; | 
|  | 298 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 299 | if (!(g_debug->config().options() & BACKTRACE)) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 300 | error_log( | 
|  | 301 | "get_malloc_leak_info: Allocations not being tracked, to enable " | 
|  | 302 | "set the option 'backtrace'."); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 303 | return; | 
|  | 304 | } | 
|  | 305 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 306 | PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
|  | 309 | void debug_free_malloc_leak_info(uint8_t* info) { | 
|  | 310 | g_dispatch->free(info); | 
|  | 311 | } | 
|  | 312 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 313 | size_t debug_malloc_usable_size(void* pointer) { | 
|  | 314 | if (DebugCallsDisabled() || pointer == nullptr) { | 
|  | 315 | return g_dispatch->malloc_usable_size(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 316 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 317 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 318 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 319 | if (!VerifyPointer(pointer, "malloc_usable_size")) { | 
|  | 320 | return 0; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | return InternalMallocUsableSize(pointer); | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 326 | static void* InternalMalloc(size_t size) { | 
|  | 327 | if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { | 
|  | 328 | debug_dump_heap(android::base::StringPrintf( | 
|  | 329 | "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) | 
|  | 330 | .c_str()); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 333 | if (size == 0) { | 
|  | 334 | size = 1; | 
|  | 335 | } | 
|  | 336 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 337 | size_t real_size = size + g_debug->extra_bytes(); | 
|  | 338 | if (real_size < size) { | 
|  | 339 | // Overflow. | 
|  | 340 | errno = ENOMEM; | 
|  | 341 | return nullptr; | 
|  | 342 | } | 
|  | 343 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 344 | if (size > PointerInfoType::MaxSize()) { | 
|  | 345 | errno = ENOMEM; | 
|  | 346 | return nullptr; | 
|  | 347 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 348 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 349 | void* pointer; | 
|  | 350 | if (g_debug->HeaderEnabled()) { | 
|  | 351 | Header* header = | 
|  | 352 | reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 353 | if (header == nullptr) { | 
|  | 354 | return nullptr; | 
|  | 355 | } | 
|  | 356 | pointer = InitHeader(header, header, size); | 
|  | 357 | } else { | 
|  | 358 | pointer = g_dispatch->malloc(real_size); | 
|  | 359 | } | 
|  | 360 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 361 | if (pointer != nullptr) { | 
|  | 362 | if (g_debug->TrackPointers()) { | 
|  | 363 | PointerData::Add(pointer, size); | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | if (g_debug->config().options() & FILL_ON_ALLOC) { | 
|  | 367 | size_t bytes = InternalMallocUsableSize(pointer); | 
|  | 368 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); | 
|  | 369 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; | 
|  | 370 | memset(pointer, g_debug->config().fill_alloc_value(), bytes); | 
|  | 371 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 372 | } | 
|  | 373 | return pointer; | 
|  | 374 | } | 
|  | 375 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 376 | void* debug_malloc(size_t size) { | 
|  | 377 | if (DebugCallsDisabled()) { | 
|  | 378 | return g_dispatch->malloc(size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 379 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 380 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 381 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 382 | void* pointer = InternalMalloc(size); | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 383 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 384 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 385 | g_debug->record->AddEntry(new MallocEntry(pointer, size)); | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | return pointer; | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 391 | static void InternalFree(void* pointer) { | 
|  | 392 | if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { | 
|  | 393 | debug_dump_heap(android::base::StringPrintf( | 
|  | 394 | "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) | 
|  | 395 | .c_str()); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 398 | void* free_pointer = pointer; | 
|  | 399 | size_t bytes; | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 400 | Header* header; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 401 | if (g_debug->HeaderEnabled()) { | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 402 | header = g_debug->GetHeader(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 403 | free_pointer = header->orig_pointer; | 
|  | 404 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 405 | if (g_debug->config().options() & FRONT_GUARD) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 406 | if (!g_debug->front_guard->Valid(header)) { | 
|  | 407 | g_debug->front_guard->LogFailure(header); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 408 | } | 
|  | 409 | } | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 410 | if (g_debug->config().options() & REAR_GUARD) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 411 | if (!g_debug->rear_guard->Valid(header)) { | 
|  | 412 | g_debug->rear_guard->LogFailure(header); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 413 | } | 
|  | 414 | } | 
|  | 415 |  | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 416 | header->tag = DEBUG_FREE_TAG; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 417 |  | 
|  | 418 | bytes = header->usable_size; | 
|  | 419 | } else { | 
|  | 420 | bytes = g_dispatch->malloc_usable_size(pointer); | 
|  | 421 | } | 
|  | 422 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 423 | if (g_debug->config().options() & FILL_ON_FREE) { | 
|  | 424 | size_t fill_bytes = g_debug->config().fill_on_free_bytes(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 425 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 426 | memset(pointer, g_debug->config().fill_free_value(), bytes); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 429 | if (g_debug->TrackPointers()) { | 
|  | 430 | PointerData::Remove(pointer); | 
|  | 431 | } | 
|  | 432 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 433 | if (g_debug->config().options() & FREE_TRACK) { | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 434 | // Do not add the allocation until we are done modifying the pointer | 
|  | 435 | // itself. This avoids a race if a lot of threads are all doing | 
|  | 436 | // frees at the same time and we wind up trying to really free this | 
|  | 437 | // pointer from another thread, while still trying to free it in | 
|  | 438 | // this function. | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 439 | pointer = PointerData::AddFreed(pointer); | 
|  | 440 | if (pointer != nullptr) { | 
|  | 441 | if (g_debug->HeaderEnabled()) { | 
|  | 442 | pointer = g_debug->GetHeader(pointer)->orig_pointer; | 
|  | 443 | } | 
|  | 444 | g_dispatch->free(pointer); | 
|  | 445 | } | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 446 | } else { | 
|  | 447 | g_dispatch->free(free_pointer); | 
|  | 448 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 449 | } | 
|  | 450 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 451 | void debug_free(void* pointer) { | 
|  | 452 | if (DebugCallsDisabled() || pointer == nullptr) { | 
|  | 453 | return g_dispatch->free(pointer); | 
|  | 454 | } | 
|  | 455 | ScopedDisableDebugCalls disable; | 
|  | 456 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 457 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 458 | g_debug->record->AddEntry(new FreeEntry(pointer)); | 
|  | 459 | } | 
|  | 460 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 461 | if (!VerifyPointer(pointer, "free")) { | 
|  | 462 | return; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | InternalFree(pointer); | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 468 | void* debug_memalign(size_t alignment, size_t bytes) { | 
|  | 469 | if (DebugCallsDisabled()) { | 
|  | 470 | return g_dispatch->memalign(alignment, bytes); | 
|  | 471 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 472 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 473 |  | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 474 | if (bytes == 0) { | 
|  | 475 | bytes = 1; | 
|  | 476 | } | 
|  | 477 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 478 | if (bytes > PointerInfoType::MaxSize()) { | 
|  | 479 | errno = ENOMEM; | 
|  | 480 | return nullptr; | 
|  | 481 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 482 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 483 | void* pointer; | 
|  | 484 | if (g_debug->HeaderEnabled()) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 485 | // Make the alignment a power of two. | 
|  | 486 | if (!powerof2(alignment)) { | 
|  | 487 | alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); | 
|  | 488 | } | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 489 | // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 490 | // that the header is aligned properly. | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 491 | if (alignment < MINIMUM_ALIGNMENT_BYTES) { | 
|  | 492 | alignment = MINIMUM_ALIGNMENT_BYTES; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 493 | } | 
|  | 494 |  | 
|  | 495 | // We don't have any idea what the natural alignment of | 
|  | 496 | // the underlying native allocator is, so we always need to | 
|  | 497 | // over allocate. | 
|  | 498 | size_t real_size = alignment + bytes + g_debug->extra_bytes(); | 
|  | 499 | if (real_size < bytes) { | 
|  | 500 | // Overflow. | 
|  | 501 | errno = ENOMEM; | 
|  | 502 | return nullptr; | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | pointer = g_dispatch->malloc(real_size); | 
|  | 506 | if (pointer == nullptr) { | 
|  | 507 | return nullptr; | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset(); | 
|  | 511 | // Now align the pointer. | 
|  | 512 | value += (-value % alignment); | 
|  | 513 |  | 
|  | 514 | Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value)); | 
|  | 515 | pointer = InitHeader(header, pointer, bytes); | 
|  | 516 | } else { | 
|  | 517 | size_t real_size = bytes + g_debug->extra_bytes(); | 
|  | 518 | if (real_size < bytes) { | 
|  | 519 | // Overflow. | 
|  | 520 | errno = ENOMEM; | 
|  | 521 | return nullptr; | 
|  | 522 | } | 
|  | 523 | pointer = g_dispatch->memalign(alignment, real_size); | 
|  | 524 | } | 
|  | 525 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 526 | if (pointer != nullptr) { | 
|  | 527 | if (g_debug->TrackPointers()) { | 
|  | 528 | PointerData::Add(pointer, bytes); | 
|  | 529 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 530 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 531 | if (g_debug->config().options() & FILL_ON_ALLOC) { | 
|  | 532 | size_t bytes = InternalMallocUsableSize(pointer); | 
|  | 533 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); | 
|  | 534 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; | 
|  | 535 | memset(pointer, g_debug->config().fill_alloc_value(), bytes); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
|  | 539 | g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment)); | 
|  | 540 | } | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 541 | } | 
|  | 542 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 543 | return pointer; | 
|  | 544 | } | 
|  | 545 |  | 
|  | 546 | void* debug_realloc(void* pointer, size_t bytes) { | 
|  | 547 | if (DebugCallsDisabled()) { | 
|  | 548 | return g_dispatch->realloc(pointer, bytes); | 
|  | 549 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 550 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 551 |  | 
|  | 552 | if (pointer == nullptr) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 553 | pointer = InternalMalloc(bytes); | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 554 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 555 | g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr)); | 
|  | 556 | } | 
|  | 557 | return pointer; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 560 | if (!VerifyPointer(pointer, "realloc")) { | 
|  | 561 | return nullptr; | 
|  | 562 | } | 
|  | 563 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 564 | if (bytes == 0) { | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 565 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 566 | g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer)); | 
|  | 567 | } | 
|  | 568 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 569 | InternalFree(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 570 | return nullptr; | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | size_t real_size = bytes; | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 574 | if (g_debug->config().options() & EXPAND_ALLOC) { | 
|  | 575 | real_size += g_debug->config().expand_alloc_bytes(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 576 | if (real_size < bytes) { | 
|  | 577 | // Overflow. | 
|  | 578 | errno = ENOMEM; | 
|  | 579 | return nullptr; | 
|  | 580 | } | 
|  | 581 | } | 
|  | 582 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 583 | if (bytes > PointerInfoType::MaxSize()) { | 
|  | 584 | errno = ENOMEM; | 
|  | 585 | return nullptr; | 
|  | 586 | } | 
|  | 587 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 588 | void* new_pointer; | 
|  | 589 | size_t prev_size; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 590 | if (g_debug->HeaderEnabled()) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 591 | // Same size, do nothing. | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 592 | Header* header = g_debug->GetHeader(pointer); | 
|  | 593 | if (real_size == header->size) { | 
|  | 594 | if (g_debug->TrackPointers()) { | 
|  | 595 | // Remove and re-add so that the backtrace is updated. | 
|  | 596 | PointerData::Remove(pointer); | 
|  | 597 | PointerData::Add(pointer, real_size); | 
|  | 598 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 599 | return pointer; | 
|  | 600 | } | 
|  | 601 |  | 
|  | 602 | // Allocation is shrinking. | 
|  | 603 | if (real_size < header->usable_size) { | 
|  | 604 | header->size = real_size; | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 605 | if (g_debug->config().options() & REAR_GUARD) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 606 | // Don't bother allocating a smaller pointer in this case, simply | 
|  | 607 | // change the header usable_size and reset the rear guard. | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 608 | header->usable_size = header->size; | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 609 | memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(), | 
|  | 610 | g_debug->config().rear_guard_bytes()); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 611 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 612 | if (g_debug->TrackPointers()) { | 
|  | 613 | // Remove and re-add so that the backtrace is updated. | 
|  | 614 | PointerData::Remove(pointer); | 
|  | 615 | PointerData::Add(pointer, real_size); | 
|  | 616 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 617 | return pointer; | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | // Allocate the new size. | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 621 | new_pointer = InternalMalloc(bytes); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 622 | if (new_pointer == nullptr) { | 
|  | 623 | errno = ENOMEM; | 
|  | 624 | return nullptr; | 
|  | 625 | } | 
|  | 626 |  | 
|  | 627 | prev_size = header->usable_size; | 
|  | 628 | memcpy(new_pointer, pointer, prev_size); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 629 | InternalFree(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 630 | } else { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 631 | if (g_debug->TrackPointers()) { | 
|  | 632 | PointerData::Remove(pointer); | 
|  | 633 | } | 
|  | 634 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 635 | prev_size = g_dispatch->malloc_usable_size(pointer); | 
|  | 636 | new_pointer = g_dispatch->realloc(pointer, real_size); | 
|  | 637 | if (new_pointer == nullptr) { | 
|  | 638 | return nullptr; | 
|  | 639 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 640 |  | 
|  | 641 | if (g_debug->TrackPointers()) { | 
|  | 642 | PointerData::Add(new_pointer, real_size); | 
|  | 643 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 644 | } | 
|  | 645 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 646 | if (g_debug->config().options() & FILL_ON_ALLOC) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 647 | size_t bytes = InternalMallocUsableSize(new_pointer); | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 648 | if (bytes > g_debug->config().fill_on_alloc_bytes()) { | 
|  | 649 | bytes = g_debug->config().fill_on_alloc_bytes(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 650 | } | 
|  | 651 | if (bytes > prev_size) { | 
|  | 652 | memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size), | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 653 | g_debug->config().fill_alloc_value(), bytes - prev_size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 654 | } | 
|  | 655 | } | 
|  | 656 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 657 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 658 | g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer)); | 
|  | 659 | } | 
|  | 660 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 661 | return new_pointer; | 
|  | 662 | } | 
|  | 663 |  | 
|  | 664 | void* debug_calloc(size_t nmemb, size_t bytes) { | 
|  | 665 | if (DebugCallsDisabled()) { | 
|  | 666 | return g_dispatch->calloc(nmemb, bytes); | 
|  | 667 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 668 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 669 |  | 
| Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 670 | size_t size; | 
|  | 671 | if (__builtin_mul_overflow(nmemb, bytes, &size)) { | 
|  | 672 | // Overflow | 
|  | 673 | errno = ENOMEM; | 
|  | 674 | return nullptr; | 
|  | 675 | } | 
|  | 676 |  | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 677 | if (size == 0) { | 
|  | 678 | size = 1; | 
|  | 679 | } | 
|  | 680 |  | 
| Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 681 | size_t real_size; | 
|  | 682 | if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 683 | // Overflow. | 
|  | 684 | errno = ENOMEM; | 
|  | 685 | return nullptr; | 
|  | 686 | } | 
|  | 687 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 688 | if (real_size > PointerInfoType::MaxSize()) { | 
|  | 689 | errno = ENOMEM; | 
|  | 690 | return nullptr; | 
|  | 691 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 692 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 693 | void* pointer; | 
|  | 694 | if (g_debug->HeaderEnabled()) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 695 | // Need to guarantee the alignment of the header. | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 696 | Header* header = | 
|  | 697 | reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 698 | if (header == nullptr) { | 
|  | 699 | return nullptr; | 
|  | 700 | } | 
|  | 701 | memset(header, 0, g_dispatch->malloc_usable_size(header)); | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 702 | pointer = InitHeader(header, header, size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 703 | } else { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 704 | pointer = g_dispatch->calloc(1, real_size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 705 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 706 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 707 | if (g_debug->config().options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 708 | g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb)); | 
|  | 709 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 710 |  | 
|  | 711 | if (pointer != nullptr && g_debug->TrackPointers()) { | 
|  | 712 | PointerData::Add(pointer, size); | 
|  | 713 | } | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 714 | return pointer; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 715 | } | 
|  | 716 |  | 
|  | 717 | struct mallinfo debug_mallinfo() { | 
|  | 718 | return g_dispatch->mallinfo(); | 
|  | 719 | } | 
|  | 720 |  | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 721 | int debug_mallopt(int param, int value) { | 
|  | 722 | return g_dispatch->mallopt(param, value); | 
|  | 723 | } | 
|  | 724 |  | 
| Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 725 | void* debug_aligned_alloc(size_t alignment, size_t size) { | 
|  | 726 | if (DebugCallsDisabled()) { | 
|  | 727 | return g_dispatch->aligned_alloc(alignment, size); | 
|  | 728 | } | 
|  | 729 | if (!powerof2(alignment)) { | 
|  | 730 | errno = EINVAL; | 
|  | 731 | return nullptr; | 
|  | 732 | } | 
|  | 733 | return debug_memalign(alignment, size); | 
|  | 734 | } | 
|  | 735 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 736 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size) { | 
|  | 737 | if (DebugCallsDisabled()) { | 
|  | 738 | return g_dispatch->posix_memalign(memptr, alignment, size); | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | if (!powerof2(alignment)) { | 
|  | 742 | return EINVAL; | 
|  | 743 | } | 
|  | 744 | int saved_errno = errno; | 
|  | 745 | *memptr = debug_memalign(alignment, size); | 
|  | 746 | errno = saved_errno; | 
|  | 747 | return (*memptr != nullptr) ? 0 : ENOMEM; | 
|  | 748 | } | 
|  | 749 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 750 | int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*), | 
|  | 751 | void* arg) { | 
|  | 752 | if (g_debug->TrackPointers()) { | 
|  | 753 | // Since malloc is disabled, don't bother acquiring any locks. | 
|  | 754 | for (auto it = PointerData::begin(); it != PointerData::end(); ++it) { | 
|  | 755 | callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg); | 
|  | 756 | } | 
|  | 757 | return 0; | 
|  | 758 | } | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 759 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 760 | // An option that adds a header will add pointer tracking, so no need to | 
|  | 761 | // check if headers are enabled. | 
|  | 762 | return g_dispatch->iterate(base, size, callback, arg); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 763 | } | 
|  | 764 |  | 
|  | 765 | void debug_malloc_disable() { | 
|  | 766 | g_dispatch->malloc_disable(); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 767 | if (g_debug->pointer) { | 
|  | 768 | g_debug->pointer->PrepareFork(); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 769 | } | 
|  | 770 | } | 
|  | 771 |  | 
|  | 772 | void debug_malloc_enable() { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 773 | if (g_debug->pointer) { | 
|  | 774 | g_debug->pointer->PostForkParent(); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 775 | } | 
|  | 776 | g_dispatch->malloc_enable(); | 
|  | 777 | } | 
|  | 778 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 779 | ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) { | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 780 | if (DebugCallsDisabled() || pointer == nullptr) { | 
|  | 781 | return 0; | 
|  | 782 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 783 | ScopedDisableDebugCalls disable; | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 784 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 785 | if (!(g_debug->config().options() & BACKTRACE)) { | 
|  | 786 | return 0; | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 787 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 788 | return PointerData::GetFrames(pointer, frames, max_frames); | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 789 | } | 
|  | 790 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 791 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
|  | 792 | void* debug_pvalloc(size_t bytes) { | 
|  | 793 | if (DebugCallsDisabled()) { | 
|  | 794 | return g_dispatch->pvalloc(bytes); | 
|  | 795 | } | 
|  | 796 |  | 
|  | 797 | size_t pagesize = getpagesize(); | 
| Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 798 | size_t size = __BIONIC_ALIGN(bytes, pagesize); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 799 | if (size < bytes) { | 
|  | 800 | // Overflow | 
|  | 801 | errno = ENOMEM; | 
|  | 802 | return nullptr; | 
|  | 803 | } | 
|  | 804 | return debug_memalign(pagesize, size); | 
|  | 805 | } | 
|  | 806 |  | 
|  | 807 | void* debug_valloc(size_t size) { | 
|  | 808 | if (DebugCallsDisabled()) { | 
|  | 809 | return g_dispatch->valloc(size); | 
|  | 810 | } | 
|  | 811 | return debug_memalign(getpagesize(), size); | 
|  | 812 | } | 
|  | 813 | #endif | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 814 |  | 
|  | 815 | static std::mutex g_dump_lock; | 
|  | 816 |  | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 817 | static void write_dump(FILE* fp) { | 
|  | 818 | fprintf(fp, "Android Native Heap Dump v1.2\n\n"); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 819 |  | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 820 | std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown"); | 
|  | 821 | fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str()); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 822 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 823 | PointerData::DumpLiveToFile(fp); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 824 |  | 
|  | 825 | fprintf(fp, "MAPS\n"); | 
|  | 826 | std::string content; | 
|  | 827 | if (!android::base::ReadFileToString("/proc/self/maps", &content)) { | 
|  | 828 | fprintf(fp, "Could not open /proc/self/maps\n"); | 
|  | 829 | } else { | 
|  | 830 | fprintf(fp, "%s", content.c_str()); | 
|  | 831 | } | 
|  | 832 | fprintf(fp, "END\n"); | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 833 | } | 
|  | 834 |  | 
|  | 835 | bool debug_write_malloc_leak_info(FILE* fp) { | 
|  | 836 | ScopedDisableDebugCalls disable; | 
|  | 837 |  | 
|  | 838 | std::lock_guard<std::mutex> guard(g_dump_lock); | 
|  | 839 |  | 
|  | 840 | if (!(g_debug->config().options() & BACKTRACE)) { | 
|  | 841 | return false; | 
|  | 842 | } | 
|  | 843 |  | 
|  | 844 | write_dump(fp); | 
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 845 | return true; | 
|  | 846 | } | 
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 847 |  | 
|  | 848 | void debug_dump_heap(const char* file_name) { | 
|  | 849 | ScopedDisableDebugCalls disable; | 
|  | 850 |  | 
|  | 851 | std::lock_guard<std::mutex> guard(g_dump_lock); | 
|  | 852 |  | 
|  | 853 | FILE* fp = fopen(file_name, "w+e"); | 
|  | 854 | if (fp == nullptr) { | 
|  | 855 | error_log("Unable to create file: %s", file_name); | 
|  | 856 | return; | 
|  | 857 | } | 
|  | 858 |  | 
|  | 859 | error_log("Dumping to file: %s\n", file_name); | 
|  | 860 | write_dump(fp); | 
|  | 861 | fclose(fp); | 
|  | 862 | } |