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