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 37 #include <mutex> 38 #include <vector> 39 40 #include <android-base/file.h> 41 #include <android-base/stringprintf.h> 42 #include <private/bionic_malloc_dispatch.h> 43 44 #include "Config.h" 45 #include "DebugData.h" 46 #include "backtrace.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 68 bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child, 69 const char* options); 70 void debug_finalize(); 71 bool debug_dump_heap(const char* file_name); 72 void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, 73 size_t* total_memory, size_t* backtrace_size); 74 ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); 75 void debug_free_malloc_leak_info(uint8_t* info); 76 size_t debug_malloc_usable_size(void* pointer); 77 void* debug_malloc(size_t size); 78 void debug_free(void* pointer); 79 void* debug_aligned_alloc(size_t alignment, size_t size); 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(); 84 int debug_mallopt(int param, int value); 85 int debug_posix_memalign(void** memptr, size_t alignment, size_t size); 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(); 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 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 121 static void LogError(const void* pointer, const char* error_str) { 122 error_log(LOG_DIVIDER); 123 error_log("+++ ALLOCATION %p %s", pointer, error_str); 124 125 // If we are tracking already freed pointers, check to see if this is 126 // one so we can print extra information. 127 if (g_debug->config().options() & FREE_TRACK) { 128 PointerData::LogFreeBacktrace(pointer); 129 } 130 131 std::vector<uintptr_t> frames(128); 132 size_t num_frames = backtrace_get(frames.data(), frames.size()); 133 if (num_frames == 0) { 134 error_log("Backtrace failed to get any frames."); 135 } else { 136 error_log("Backtrace at time of failure:"); 137 backtrace_log(frames.data(), num_frames); 138 } 139 error_log(LOG_DIVIDER); 140 } 141 142 static bool VerifyPointer(const void* pointer, const char* function_name) { 143 if (g_debug->HeaderEnabled()) { 144 Header* header = g_debug->GetHeader(pointer); 145 if (header->tag != DEBUG_TAG) { 146 std::string error_str; 147 if (header->tag == DEBUG_FREE_TAG) { 148 error_str = std::string("USED AFTER FREE (") + function_name + ")"; 149 } else { 150 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag, 151 function_name); 152 } 153 LogError(pointer, error_str.c_str()); 154 return false; 155 } 156 } 157 158 if (g_debug->TrackPointers()) { 159 if (!PointerData::Exists(pointer)) { 160 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")"); 161 LogError(pointer, error_str.c_str()); 162 return false; 163 } 164 } 165 return true; 166 } 167 168 static size_t InternalMallocUsableSize(void* pointer) { 169 if (g_debug->HeaderEnabled()) { 170 return g_debug->GetHeader(pointer)->usable_size; 171 } else { 172 return g_dispatch->malloc_usable_size(pointer); 173 } 174 } 175 176 static void* InitHeader(Header* header, void* orig_pointer, size_t size) { 177 header->tag = DEBUG_TAG; 178 header->orig_pointer = orig_pointer; 179 header->size = size; 180 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer); 181 if (header->usable_size == 0) { 182 g_dispatch->free(orig_pointer); 183 return nullptr; 184 } 185 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) - 186 reinterpret_cast<uintptr_t>(orig_pointer); 187 188 if (g_debug->config().options() & FRONT_GUARD) { 189 uint8_t* guard = g_debug->GetFrontGuard(header); 190 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes()); 191 } 192 193 if (g_debug->config().options() & REAR_GUARD) { 194 uint8_t* guard = g_debug->GetRearGuard(header); 195 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes()); 196 // If the rear guard is enabled, set the usable size to the exact size 197 // of the allocation. 198 header->usable_size = header->size; 199 } 200 201 return g_debug->GetPointer(header); 202 } 203 204 bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child, 205 const char* options) { 206 if (malloc_zygote_child == nullptr || options == nullptr) { 207 return false; 208 } 209 210 InitAtfork(); 211 212 g_malloc_zygote_child = malloc_zygote_child; 213 214 g_dispatch = malloc_dispatch; 215 216 if (!DebugDisableInitialize()) { 217 return false; 218 } 219 220 DebugData* debug = new DebugData(); 221 if (!debug->Initialize(options)) { 222 delete debug; 223 DebugDisableFinalize(); 224 return false; 225 } 226 g_debug = debug; 227 228 // Always enable the backtrace code since we will use it in a number 229 // of different error cases. 230 backtrace_startup(); 231 232 return true; 233 } 234 235 void debug_finalize() { 236 if (g_debug == nullptr) { 237 return; 238 } 239 240 if (g_debug->config().options() & FREE_TRACK) { 241 PointerData::VerifyAllFreed(); 242 } 243 244 if (g_debug->config().options() & LEAK_TRACK) { 245 PointerData::LogLeaks(); 246 } 247 248 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) { 249 ScopedDisableDebugCalls disable; 250 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt", 251 g_debug->config().backtrace_dump_prefix().c_str(), 252 getpid()) 253 .c_str()); 254 } 255 256 DebugDisableSet(true); 257 258 backtrace_shutdown(); 259 260 delete g_debug; 261 g_debug = nullptr; 262 263 DebugDisableFinalize(); 264 } 265 266 void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, 267 size_t* total_memory, size_t* backtrace_size) { 268 ScopedDisableDebugCalls disable; 269 270 // Verify the arguments. 271 if (info == nullptr || overall_size == nullptr || info_size == NULL || total_memory == nullptr || 272 backtrace_size == nullptr) { 273 error_log("get_malloc_leak_info: At least one invalid parameter."); 274 return; 275 } 276 277 *info = nullptr; 278 *overall_size = 0; 279 *info_size = 0; 280 *total_memory = 0; 281 *backtrace_size = 0; 282 283 if (!(g_debug->config().options() & BACKTRACE)) { 284 error_log( 285 "get_malloc_leak_info: Allocations not being tracked, to enable " 286 "set the option 'backtrace'."); 287 return; 288 } 289 290 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size); 291 } 292 293 void debug_free_malloc_leak_info(uint8_t* info) { 294 g_dispatch->free(info); 295 } 296 297 size_t debug_malloc_usable_size(void* pointer) { 298 if (DebugCallsDisabled() || pointer == nullptr) { 299 return g_dispatch->malloc_usable_size(pointer); 300 } 301 ScopedDisableDebugCalls disable; 302 303 if (!VerifyPointer(pointer, "malloc_usable_size")) { 304 return 0; 305 } 306 307 return InternalMallocUsableSize(pointer); 308 } 309 310 static void* InternalMalloc(size_t size) { 311 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { 312 debug_dump_heap(android::base::StringPrintf( 313 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) 314 .c_str()); 315 } 316 317 if (size == 0) { 318 size = 1; 319 } 320 321 size_t real_size = size + g_debug->extra_bytes(); 322 if (real_size < size) { 323 // Overflow. 324 errno = ENOMEM; 325 return nullptr; 326 } 327 328 if (size > PointerInfoType::MaxSize()) { 329 errno = ENOMEM; 330 return nullptr; 331 } 332 333 void* pointer; 334 if (g_debug->HeaderEnabled()) { 335 Header* header = 336 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); 337 if (header == nullptr) { 338 return nullptr; 339 } 340 pointer = InitHeader(header, header, size); 341 } else { 342 pointer = g_dispatch->malloc(real_size); 343 } 344 345 if (pointer != nullptr) { 346 if (g_debug->TrackPointers()) { 347 PointerData::Add(pointer, size); 348 } 349 350 if (g_debug->config().options() & FILL_ON_ALLOC) { 351 size_t bytes = InternalMallocUsableSize(pointer); 352 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); 353 bytes = (bytes < fill_bytes) ? bytes : fill_bytes; 354 memset(pointer, g_debug->config().fill_alloc_value(), bytes); 355 } 356 } 357 return pointer; 358 } 359 360 void* debug_malloc(size_t size) { 361 if (DebugCallsDisabled()) { 362 return g_dispatch->malloc(size); 363 } 364 ScopedDisableDebugCalls disable; 365 366 void* pointer = InternalMalloc(size); 367 368 if (g_debug->config().options() & RECORD_ALLOCS) { 369 g_debug->record->AddEntry(new MallocEntry(pointer, size)); 370 } 371 372 return pointer; 373 } 374 375 static void InternalFree(void* pointer) { 376 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { 377 debug_dump_heap(android::base::StringPrintf( 378 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) 379 .c_str()); 380 } 381 382 void* free_pointer = pointer; 383 size_t bytes; 384 Header* header; 385 if (g_debug->HeaderEnabled()) { 386 header = g_debug->GetHeader(pointer); 387 free_pointer = header->orig_pointer; 388 389 if (g_debug->config().options() & FRONT_GUARD) { 390 if (!g_debug->front_guard->Valid(header)) { 391 g_debug->front_guard->LogFailure(header); 392 } 393 } 394 if (g_debug->config().options() & REAR_GUARD) { 395 if (!g_debug->rear_guard->Valid(header)) { 396 g_debug->rear_guard->LogFailure(header); 397 } 398 } 399 400 header->tag = DEBUG_FREE_TAG; 401 402 bytes = header->usable_size; 403 } else { 404 bytes = g_dispatch->malloc_usable_size(pointer); 405 } 406 407 if (g_debug->config().options() & FILL_ON_FREE) { 408 size_t fill_bytes = g_debug->config().fill_on_free_bytes(); 409 bytes = (bytes < fill_bytes) ? bytes : fill_bytes; 410 memset(pointer, g_debug->config().fill_free_value(), bytes); 411 } 412 413 if (g_debug->TrackPointers()) { 414 PointerData::Remove(pointer); 415 } 416 417 if (g_debug->config().options() & FREE_TRACK) { 418 // Do not add the allocation until we are done modifying the pointer 419 // itself. This avoids a race if a lot of threads are all doing 420 // frees at the same time and we wind up trying to really free this 421 // pointer from another thread, while still trying to free it in 422 // this function. 423 pointer = PointerData::AddFreed(pointer); 424 if (pointer != nullptr) { 425 if (g_debug->HeaderEnabled()) { 426 pointer = g_debug->GetHeader(pointer)->orig_pointer; 427 } 428 g_dispatch->free(pointer); 429 } 430 } else { 431 g_dispatch->free(free_pointer); 432 } 433 } 434 435 void debug_free(void* pointer) { 436 if (DebugCallsDisabled() || pointer == nullptr) { 437 return g_dispatch->free(pointer); 438 } 439 ScopedDisableDebugCalls disable; 440 441 if (g_debug->config().options() & RECORD_ALLOCS) { 442 g_debug->record->AddEntry(new FreeEntry(pointer)); 443 } 444 445 if (!VerifyPointer(pointer, "free")) { 446 return; 447 } 448 449 InternalFree(pointer); 450 } 451 452 void* debug_memalign(size_t alignment, size_t bytes) { 453 if (DebugCallsDisabled()) { 454 return g_dispatch->memalign(alignment, bytes); 455 } 456 ScopedDisableDebugCalls disable; 457 458 if (bytes == 0) { 459 bytes = 1; 460 } 461 462 if (bytes > PointerInfoType::MaxSize()) { 463 errno = ENOMEM; 464 return nullptr; 465 } 466 467 void* pointer; 468 if (g_debug->HeaderEnabled()) { 469 // Make the alignment a power of two. 470 if (!powerof2(alignment)) { 471 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); 472 } 473 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee 474 // that the header is aligned properly. 475 if (alignment < MINIMUM_ALIGNMENT_BYTES) { 476 alignment = MINIMUM_ALIGNMENT_BYTES; 477 } 478 479 // We don't have any idea what the natural alignment of 480 // the underlying native allocator is, so we always need to 481 // over allocate. 482 size_t real_size = alignment + bytes + g_debug->extra_bytes(); 483 if (real_size < bytes) { 484 // Overflow. 485 errno = ENOMEM; 486 return nullptr; 487 } 488 489 pointer = g_dispatch->malloc(real_size); 490 if (pointer == nullptr) { 491 return nullptr; 492 } 493 494 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset(); 495 // Now align the pointer. 496 value += (-value % alignment); 497 498 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value)); 499 pointer = InitHeader(header, pointer, bytes); 500 } else { 501 size_t real_size = bytes + g_debug->extra_bytes(); 502 if (real_size < bytes) { 503 // Overflow. 504 errno = ENOMEM; 505 return nullptr; 506 } 507 pointer = g_dispatch->memalign(alignment, real_size); 508 } 509 510 if (pointer != nullptr) { 511 if (g_debug->TrackPointers()) { 512 PointerData::Add(pointer, bytes); 513 } 514 515 if (g_debug->config().options() & FILL_ON_ALLOC) { 516 size_t bytes = InternalMallocUsableSize(pointer); 517 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); 518 bytes = (bytes < fill_bytes) ? bytes : fill_bytes; 519 memset(pointer, g_debug->config().fill_alloc_value(), bytes); 520 } 521 522 if (g_debug->config().options() & RECORD_ALLOCS) { 523 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment)); 524 } 525 } 526 527 return pointer; 528 } 529 530 void* debug_realloc(void* pointer, size_t bytes) { 531 if (DebugCallsDisabled()) { 532 return g_dispatch->realloc(pointer, bytes); 533 } 534 ScopedDisableDebugCalls disable; 535 536 if (pointer == nullptr) { 537 pointer = InternalMalloc(bytes); 538 if (g_debug->config().options() & RECORD_ALLOCS) { 539 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr)); 540 } 541 return pointer; 542 } 543 544 if (!VerifyPointer(pointer, "realloc")) { 545 return nullptr; 546 } 547 548 if (bytes == 0) { 549 if (g_debug->config().options() & RECORD_ALLOCS) { 550 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer)); 551 } 552 553 InternalFree(pointer); 554 return nullptr; 555 } 556 557 size_t real_size = bytes; 558 if (g_debug->config().options() & EXPAND_ALLOC) { 559 real_size += g_debug->config().expand_alloc_bytes(); 560 if (real_size < bytes) { 561 // Overflow. 562 errno = ENOMEM; 563 return nullptr; 564 } 565 } 566 567 if (bytes > PointerInfoType::MaxSize()) { 568 errno = ENOMEM; 569 return nullptr; 570 } 571 572 void* new_pointer; 573 size_t prev_size; 574 if (g_debug->HeaderEnabled()) { 575 // Same size, do nothing. 576 Header* header = g_debug->GetHeader(pointer); 577 if (real_size == header->size) { 578 if (g_debug->TrackPointers()) { 579 // Remove and re-add so that the backtrace is updated. 580 PointerData::Remove(pointer); 581 PointerData::Add(pointer, real_size); 582 } 583 return pointer; 584 } 585 586 // Allocation is shrinking. 587 if (real_size < header->usable_size) { 588 header->size = real_size; 589 if (g_debug->config().options() & REAR_GUARD) { 590 // Don't bother allocating a smaller pointer in this case, simply 591 // change the header usable_size and reset the rear guard. 592 header->usable_size = header->size; 593 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(), 594 g_debug->config().rear_guard_bytes()); 595 } 596 if (g_debug->TrackPointers()) { 597 // Remove and re-add so that the backtrace is updated. 598 PointerData::Remove(pointer); 599 PointerData::Add(pointer, real_size); 600 } 601 return pointer; 602 } 603 604 // Allocate the new size. 605 new_pointer = InternalMalloc(bytes); 606 if (new_pointer == nullptr) { 607 errno = ENOMEM; 608 return nullptr; 609 } 610 611 prev_size = header->usable_size; 612 memcpy(new_pointer, pointer, prev_size); 613 InternalFree(pointer); 614 } else { 615 if (g_debug->TrackPointers()) { 616 PointerData::Remove(pointer); 617 } 618 619 prev_size = g_dispatch->malloc_usable_size(pointer); 620 new_pointer = g_dispatch->realloc(pointer, real_size); 621 if (new_pointer == nullptr) { 622 return nullptr; 623 } 624 625 if (g_debug->TrackPointers()) { 626 PointerData::Add(new_pointer, real_size); 627 } 628 } 629 630 if (g_debug->config().options() & FILL_ON_ALLOC) { 631 size_t bytes = InternalMallocUsableSize(new_pointer); 632 if (bytes > g_debug->config().fill_on_alloc_bytes()) { 633 bytes = g_debug->config().fill_on_alloc_bytes(); 634 } 635 if (bytes > prev_size) { 636 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size), 637 g_debug->config().fill_alloc_value(), bytes - prev_size); 638 } 639 } 640 641 if (g_debug->config().options() & RECORD_ALLOCS) { 642 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer)); 643 } 644 645 return new_pointer; 646 } 647 648 void* debug_calloc(size_t nmemb, size_t bytes) { 649 if (DebugCallsDisabled()) { 650 return g_dispatch->calloc(nmemb, bytes); 651 } 652 ScopedDisableDebugCalls disable; 653 654 size_t size; 655 if (__builtin_mul_overflow(nmemb, bytes, &size)) { 656 // Overflow 657 errno = ENOMEM; 658 return nullptr; 659 } 660 661 if (size == 0) { 662 size = 1; 663 } 664 665 size_t real_size; 666 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) { 667 // Overflow. 668 errno = ENOMEM; 669 return nullptr; 670 } 671 672 if (real_size > PointerInfoType::MaxSize()) { 673 errno = ENOMEM; 674 return nullptr; 675 } 676 677 void* pointer; 678 if (g_debug->HeaderEnabled()) { 679 // Need to guarantee the alignment of the header. 680 Header* header = 681 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); 682 if (header == nullptr) { 683 return nullptr; 684 } 685 memset(header, 0, g_dispatch->malloc_usable_size(header)); 686 pointer = InitHeader(header, header, size); 687 } else { 688 pointer = g_dispatch->calloc(1, real_size); 689 } 690 691 if (g_debug->config().options() & RECORD_ALLOCS) { 692 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb)); 693 } 694 695 if (pointer != nullptr && g_debug->TrackPointers()) { 696 PointerData::Add(pointer, size); 697 } 698 return pointer; 699 } 700 701 struct mallinfo debug_mallinfo() { 702 return g_dispatch->mallinfo(); 703 } 704 705 int debug_mallopt(int param, int value) { 706 return g_dispatch->mallopt(param, value); 707 } 708 709 void* debug_aligned_alloc(size_t alignment, size_t size) { 710 if (DebugCallsDisabled()) { 711 return g_dispatch->aligned_alloc(alignment, size); 712 } 713 if (!powerof2(alignment)) { 714 errno = EINVAL; 715 return nullptr; 716 } 717 return debug_memalign(alignment, size); 718 } 719 720 int debug_posix_memalign(void** memptr, size_t alignment, size_t size) { 721 if (DebugCallsDisabled()) { 722 return g_dispatch->posix_memalign(memptr, alignment, size); 723 } 724 725 if (!powerof2(alignment)) { 726 return EINVAL; 727 } 728 int saved_errno = errno; 729 *memptr = debug_memalign(alignment, size); 730 errno = saved_errno; 731 return (*memptr != nullptr) ? 0 : ENOMEM; 732 } 733 734 int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*), 735 void* arg) { 736 if (g_debug->TrackPointers()) { 737 // Since malloc is disabled, don't bother acquiring any locks. 738 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) { 739 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg); 740 } 741 return 0; 742 } 743 744 // An option that adds a header will add pointer tracking, so no need to 745 // check if headers are enabled. 746 return g_dispatch->iterate(base, size, callback, arg); 747 } 748 749 void debug_malloc_disable() { 750 g_dispatch->malloc_disable(); 751 if (g_debug->pointer) { 752 g_debug->pointer->PrepareFork(); 753 } 754 } 755 756 void debug_malloc_enable() { 757 if (g_debug->pointer) { 758 g_debug->pointer->PostForkParent(); 759 } 760 g_dispatch->malloc_enable(); 761 } 762 763 ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) { 764 if (DebugCallsDisabled() || pointer == nullptr) { 765 return 0; 766 } 767 ScopedDisableDebugCalls disable; 768 769 if (!(g_debug->config().options() & BACKTRACE)) { 770 return 0; 771 } 772 return PointerData::GetFrames(pointer, frames, max_frames); 773 } 774 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(); 782 size_t size = __BIONIC_ALIGN(bytes, pagesize); 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 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.1\n\n"); 823 824 PointerData::DumpLiveToFile(fp); 825 826 fprintf(fp, "MAPS\n"); 827 std::string content; 828 if (!android::base::ReadFileToString("/proc/self/maps", &content)) { 829 fprintf(fp, "Could not open /proc/self/maps\n"); 830 } else { 831 fprintf(fp, "%s", content.c_str()); 832 } 833 fprintf(fp, "END\n"); 834 fclose(fp); 835 return true; 836 } 837