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