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