OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 #include <sys/mman.h> | 45 #include <sys/mman.h> |
46 #endif | 46 #endif |
47 #ifdef HAVE_PTHREAD | 47 #ifdef HAVE_PTHREAD |
48 #include <pthread.h> | 48 #include <pthread.h> |
49 #endif | 49 #endif |
50 #include <sys/stat.h> | 50 #include <sys/stat.h> |
51 #include <sys/types.h> | 51 #include <sys/types.h> |
52 #include <time.h> | 52 #include <time.h> |
53 #include <assert.h> | 53 #include <assert.h> |
54 | 54 |
55 #if defined(HAVE_LINUX_PTRACE_H) && !defined(__native_client__) | 55 #if defined(HAVE_LINUX_PTRACE_H) |
56 #include <linux/ptrace.h> | 56 #include <linux/ptrace.h> |
57 #endif | 57 #endif |
58 #ifdef HAVE_SYS_SYSCALL_H | 58 #ifdef HAVE_SYS_SYSCALL_H |
59 #include <sys/syscall.h> | 59 #include <sys/syscall.h> |
60 #endif | 60 #endif |
61 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(_
_MINGW32__) | 61 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(_
_MINGW32__) |
62 #include <wtypes.h> | 62 #include <wtypes.h> |
63 #include <winbase.h> | 63 #include <winbase.h> |
64 #undef ERROR // windows defines these as macros, which can cause trouble | 64 #undef ERROR // windows defines these as macros, which can cause trouble |
65 #undef max | 65 #undef max |
66 #undef min | 66 #undef min |
67 #endif | 67 #endif |
68 | 68 |
69 #include <string> | 69 #include <string> |
70 #include <vector> | 70 #include <vector> |
71 #include <map> | 71 #include <map> |
72 #include <set> | 72 #include <set> |
73 #include <algorithm> | 73 #include <algorithm> |
74 #include <functional> | 74 #include <functional> |
75 | 75 |
76 #include <google/heap-checker.h> | 76 #include <gperftools/heap-checker.h> |
77 | 77 |
78 #include "base/basictypes.h" | 78 #include "base/basictypes.h" |
79 #include "base/googleinit.h" | 79 #include "base/googleinit.h" |
80 #include "base/logging.h" | 80 #include "base/logging.h" |
81 #include <google/stacktrace.h> | 81 #include <gperftools/stacktrace.h> |
82 #include "base/commandlineflags.h" | 82 #include "base/commandlineflags.h" |
83 #include "base/elfcore.h" // for i386_regs | 83 #include "base/elfcore.h" // for i386_regs |
84 #include "base/thread_lister.h" | 84 #include "base/thread_lister.h" |
85 #include "heap-profile-table.h" | 85 #include "heap-profile-table.h" |
86 #include "base/low_level_alloc.h" | 86 #include "base/low_level_alloc.h" |
87 #include "malloc_hook-inl.h" | 87 #include "malloc_hook-inl.h" |
88 #include <google/malloc_hook.h> | 88 #include <gperftools/malloc_hook.h> |
89 #include <google/malloc_extension.h> | 89 #include <gperftools/malloc_extension.h> |
90 #include "maybe_threads.h" | 90 #include "maybe_threads.h" |
91 #include "memory_region_map.h" | 91 #include "memory_region_map.h" |
92 #include "base/spinlock.h" | 92 #include "base/spinlock.h" |
93 #include "base/sysinfo.h" | 93 #include "base/sysinfo.h" |
94 #include "base/stl_allocator.h" | 94 #include "base/stl_allocator.h" |
95 | 95 |
96 using std::string; | 96 using std::string; |
97 using std::basic_string; | 97 using std::basic_string; |
98 using std::pair; | 98 using std::pair; |
99 using std::map; | 99 using std::map; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 //---------------------------------------------------------------------- | 137 //---------------------------------------------------------------------- |
138 // Flags that control heap-checking | 138 // Flags that control heap-checking |
139 //---------------------------------------------------------------------- | 139 //---------------------------------------------------------------------- |
140 | 140 |
141 DEFINE_string(heap_check, | 141 DEFINE_string(heap_check, |
142 EnvToString("HEAPCHECK", ""), | 142 EnvToString("HEAPCHECK", ""), |
143 "The heap leak checking to be done over the whole executable: " | 143 "The heap leak checking to be done over the whole executable: " |
144 "\"minimal\", \"normal\", \"strict\", " | 144 "\"minimal\", \"normal\", \"strict\", " |
145 "\"draconian\", \"as-is\", and \"local\" " | 145 "\"draconian\", \"as-is\", and \"local\" " |
146 " or the empty string are the supported choices. " | 146 " or the empty string are the supported choices. " |
147 "(See HeapLeakChecker::InternalInitStart for details.)"); | 147 "(See HeapLeakChecker_InternalInitStart for details.)"); |
148 | 148 |
149 DEFINE_bool(heap_check_report, true, "Obsolete"); | 149 DEFINE_bool(heap_check_report, true, "Obsolete"); |
150 | 150 |
151 DEFINE_bool(heap_check_before_constructors, | 151 DEFINE_bool(heap_check_before_constructors, |
152 true, | 152 true, |
153 "deprecated; pretty much always true now"); | 153 "deprecated; pretty much always true now"); |
154 | 154 |
155 DEFINE_bool(heap_check_after_destructors, | 155 DEFINE_bool(heap_check_after_destructors, |
156 EnvToBool("HEAP_CHECK_AFTER_DESTRUCTORS", false), | 156 EnvToBool("HEAP_CHECK_AFTER_DESTRUCTORS", false), |
157 "If overall heap check is to end after global destructors " | 157 "If overall heap check is to end after global destructors " |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 "Number of seconds to delay on-exit heap checking." | 216 "Number of seconds to delay on-exit heap checking." |
217 " If you set this flag," | 217 " If you set this flag," |
218 " you may also want to set exit_timeout_seconds in order to" | 218 " you may also want to set exit_timeout_seconds in order to" |
219 " avoid exit timeouts.\n" | 219 " avoid exit timeouts.\n" |
220 "NOTE: This flag is to be used only to help diagnose issues" | 220 "NOTE: This flag is to be used only to help diagnose issues" |
221 " where it is suspected that the heap checker is reporting" | 221 " where it is suspected that the heap checker is reporting" |
222 " false leaks that will disappear if the heap checker delays" | 222 " false leaks that will disappear if the heap checker delays" |
223 " its checks. Report any such issues to the heap-checker" | 223 " its checks. Report any such issues to the heap-checker" |
224 " maintainer(s)."); | 224 " maintainer(s)."); |
225 | 225 |
| 226 DEFINE_int32(heap_check_error_exit_code, |
| 227 EnvToInt("HEAP_CHECK_ERROR_EXIT_CODE", 1), |
| 228 "Exit code to return if any leaks were detected."); |
| 229 |
226 //---------------------------------------------------------------------- | 230 //---------------------------------------------------------------------- |
227 | 231 |
228 DEFINE_string(heap_profile_pprof, | 232 DEFINE_string(heap_profile_pprof, |
229 EnvToString("PPROF_PATH", "pprof"), | 233 EnvToString("PPROF_PATH", "pprof"), |
230 "OBSOLETE; not used"); | 234 "OBSOLETE; not used"); |
231 | 235 |
232 DEFINE_string(heap_check_dump_directory, | 236 DEFINE_string(heap_check_dump_directory, |
233 EnvToString("HEAP_CHECK_DUMP_DIRECTORY", "/tmp"), | 237 EnvToString("HEAP_CHECK_DUMP_DIRECTORY", "/tmp"), |
234 "Directory to put heap-checker leak dump information"); | 238 "Directory to put heap-checker leak dump information"); |
235 | 239 |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 template<typename T> | 542 template<typename T> |
539 inline static const void* AsPtr(T addr) { | 543 inline static const void* AsPtr(T addr) { |
540 return reinterpret_cast<void*>(addr); | 544 return reinterpret_cast<void*>(addr); |
541 } | 545 } |
542 inline static uintptr_t AsInt(const void* ptr) { | 546 inline static uintptr_t AsInt(const void* ptr) { |
543 return reinterpret_cast<uintptr_t>(ptr); | 547 return reinterpret_cast<uintptr_t>(ptr); |
544 } | 548 } |
545 | 549 |
546 //---------------------------------------------------------------------- | 550 //---------------------------------------------------------------------- |
547 | 551 |
| 552 // We've seen reports that strstr causes heap-checker crashes in some |
| 553 // libc's (?): |
| 554 // http://code.google.com/p/gperftools/issues/detail?id=263 |
| 555 // It's simple enough to use our own. This is not in time-critical code. |
| 556 static const char* hc_strstr(const char* s1, const char* s2) { |
| 557 const size_t len = strlen(s2); |
| 558 RAW_CHECK(len > 0, "Unexpected empty string passed to strstr()"); |
| 559 for (const char* p = strchr(s1, *s2); p != NULL; p = strchr(p+1, *s2)) { |
| 560 if (strncmp(p, s2, len) == 0) { |
| 561 return p; |
| 562 } |
| 563 } |
| 564 return NULL; |
| 565 } |
| 566 |
| 567 //---------------------------------------------------------------------- |
| 568 |
548 // Our hooks for MallocHook | 569 // Our hooks for MallocHook |
549 static void NewHook(const void* ptr, size_t size) { | 570 static void NewHook(const void* ptr, size_t size) { |
550 if (ptr != NULL) { | 571 if (ptr != NULL) { |
551 const int counter = get_thread_disable_counter(); | 572 const int counter = get_thread_disable_counter(); |
552 const bool ignore = (counter > 0); | 573 const bool ignore = (counter > 0); |
553 RAW_VLOG(16, "Recording Alloc: %p of %"PRIuS "; %d", ptr, size, | 574 RAW_VLOG(16, "Recording Alloc: %p of %"PRIuS "; %d", ptr, size, |
554 int(counter)); | 575 int(counter)); |
| 576 |
| 577 // Fetch the caller's stack trace before acquiring heap_checker_lock. |
| 578 void* stack[HeapProfileTable::kMaxStackDepth]; |
| 579 int depth = HeapProfileTable::GetCallerStackTrace(0, stack); |
| 580 |
555 { SpinLockHolder l(&heap_checker_lock); | 581 { SpinLockHolder l(&heap_checker_lock); |
556 if (size > max_heap_object_size) max_heap_object_size = size; | 582 if (size > max_heap_object_size) max_heap_object_size = size; |
557 uintptr_t addr = AsInt(ptr); | 583 uintptr_t addr = AsInt(ptr); |
558 if (addr < min_heap_address) min_heap_address = addr; | 584 if (addr < min_heap_address) min_heap_address = addr; |
559 addr += size; | 585 addr += size; |
560 if (addr > max_heap_address) max_heap_address = addr; | 586 if (addr > max_heap_address) max_heap_address = addr; |
561 if (heap_checker_on) { | 587 if (heap_checker_on) { |
562 heap_profile->RecordAlloc(ptr, size, 0); | 588 heap_profile->RecordAlloc(ptr, size, depth, stack); |
563 if (ignore) { | 589 if (ignore) { |
564 heap_profile->MarkAsIgnored(ptr); | 590 heap_profile->MarkAsIgnored(ptr); |
565 } | 591 } |
566 } | 592 } |
567 } | 593 } |
568 RAW_VLOG(17, "Alloc Recorded: %p of %"PRIuS"", ptr, size); | 594 RAW_VLOG(17, "Alloc Recorded: %p of %"PRIuS"", ptr, size); |
569 } | 595 } |
570 } | 596 } |
571 | 597 |
572 static void DeleteHook(const void* ptr) { | 598 static void DeleteHook(const void* ptr) { |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 return; | 781 return; |
756 } | 782 } |
757 } | 783 } |
758 RAW_VLOG(11, "%s-disabling %"PRIuS" bytes at %p", | 784 RAW_VLOG(11, "%s-disabling %"PRIuS" bytes at %p", |
759 (stack_disable ? "Stack" : "Range"), info.object_size, ptr); | 785 (stack_disable ? "Stack" : "Range"), info.object_size, ptr); |
760 live_objects->push_back(AllocObject(ptr, info.object_size, | 786 live_objects->push_back(AllocObject(ptr, info.object_size, |
761 MUST_BE_ON_HEAP)); | 787 MUST_BE_ON_HEAP)); |
762 } | 788 } |
763 } | 789 } |
764 | 790 |
| 791 static const char kUnnamedProcSelfMapEntry[] = "UNNAMED"; |
| 792 |
765 // This function takes some fields from a /proc/self/maps line: | 793 // This function takes some fields from a /proc/self/maps line: |
766 // | 794 // |
767 // start_address start address of a memory region. | 795 // start_address start address of a memory region. |
768 // end_address end address of a memory region | 796 // end_address end address of a memory region |
769 // permissions rwx + private/shared bit | 797 // permissions rwx + private/shared bit |
770 // filename filename of the mapped file | 798 // filename filename of the mapped file |
771 // | 799 // |
772 // If the region is not writeable, then it cannot have any heap | 800 // If the region is not writeable, then it cannot have any heap |
773 // pointers in it, otherwise we record it as a candidate live region | 801 // pointers in it, otherwise we record it as a candidate live region |
774 // to get filtered later. | 802 // to get filtered later. |
775 static void RecordGlobalDataLocked(uintptr_t start_address, | 803 static void RecordGlobalDataLocked(uintptr_t start_address, |
776 uintptr_t end_address, | 804 uintptr_t end_address, |
777 const char* permissions, | 805 const char* permissions, |
778 const char* filename) { | 806 const char* filename) { |
779 RAW_DCHECK(heap_checker_lock.IsHeld(), ""); | 807 RAW_DCHECK(heap_checker_lock.IsHeld(), ""); |
780 // Ignore non-writeable regions. | 808 // Ignore non-writeable regions. |
781 if (strchr(permissions, 'w') == NULL) return; | 809 if (strchr(permissions, 'w') == NULL) return; |
782 if (filename == NULL || *filename == '\0') filename = "UNNAMED"; | 810 if (filename == NULL || *filename == '\0') { |
| 811 filename = kUnnamedProcSelfMapEntry; |
| 812 } |
783 RAW_VLOG(11, "Looking into %s: 0x%" PRIxPTR "..0x%" PRIxPTR, | 813 RAW_VLOG(11, "Looking into %s: 0x%" PRIxPTR "..0x%" PRIxPTR, |
784 filename, start_address, end_address); | 814 filename, start_address, end_address); |
785 (*library_live_objects)[filename]. | 815 (*library_live_objects)[filename]. |
786 push_back(AllocObject(AsPtr(start_address), | 816 push_back(AllocObject(AsPtr(start_address), |
787 end_address - start_address, | 817 end_address - start_address, |
788 MAYBE_LIVE)); | 818 MAYBE_LIVE)); |
789 } | 819 } |
790 | 820 |
791 // See if 'library' from /proc/self/maps has base name 'library_base' | 821 // See if 'library' from /proc/self/maps has base name 'library_base' |
792 // i.e. contains it and has '.' or '-' after it. | 822 // i.e. contains it and has '.' or '-' after it. |
793 static bool IsLibraryNamed(const char* library, const char* library_base) { | 823 static bool IsLibraryNamed(const char* library, const char* library_base) { |
794 const char* p = strstr(library, library_base); | 824 const char* p = hc_strstr(library, library_base); |
795 size_t sz = strlen(library_base); | 825 size_t sz = strlen(library_base); |
796 return p != NULL && (p[sz] == '.' || p[sz] == '-'); | 826 return p != NULL && (p[sz] == '.' || p[sz] == '-'); |
797 } | 827 } |
798 | 828 |
799 // static | 829 // static |
800 void HeapLeakChecker::DisableLibraryAllocsLocked(const char* library, | 830 void HeapLeakChecker::DisableLibraryAllocsLocked(const char* library, |
801 uintptr_t start_address, | 831 uintptr_t start_address, |
802 uintptr_t end_address) { | 832 uintptr_t end_address) { |
803 RAW_DCHECK(heap_checker_lock.IsHeld(), ""); | 833 RAW_DCHECK(heap_checker_lock.IsHeld(), ""); |
804 int depth = 0; | 834 int depth = 0; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 continue; | 926 continue; |
897 } | 927 } |
898 // Determine if any shared libraries are present (this is the same | 928 // Determine if any shared libraries are present (this is the same |
899 // list of extensions as is found in pprof). We want to ignore | 929 // list of extensions as is found in pprof). We want to ignore |
900 // 'fake' libraries with inode 0 when determining. However, some | 930 // 'fake' libraries with inode 0 when determining. However, some |
901 // systems don't share inodes via /proc, so we turn off this check | 931 // systems don't share inodes via /proc, so we turn off this check |
902 // if we don't see any evidence that we're getting inode info. | 932 // if we don't see any evidence that we're getting inode info. |
903 if (inode != 0) { | 933 if (inode != 0) { |
904 saw_nonzero_inode = true; | 934 saw_nonzero_inode = true; |
905 } | 935 } |
906 if ((strstr(filename, "lib") && strstr(filename, ".so")) || | 936 if ((hc_strstr(filename, "lib") && hc_strstr(filename, ".so")) || |
907 strstr(filename, ".dll") || | 937 hc_strstr(filename, ".dll") || |
908 // not all .dylib filenames start with lib. .dylib is big enough | 938 // not all .dylib filenames start with lib. .dylib is big enough |
909 // that we are unlikely to get false matches just checking that. | 939 // that we are unlikely to get false matches just checking that. |
910 strstr(filename, ".dylib") || strstr(filename, ".bundle")) { | 940 hc_strstr(filename, ".dylib") || hc_strstr(filename, ".bundle")) { |
911 saw_shared_lib = true; | 941 saw_shared_lib = true; |
912 if (inode != 0) { | 942 if (inode != 0) { |
913 saw_shared_lib_with_nonzero_inode = true; | 943 saw_shared_lib_with_nonzero_inode = true; |
914 } | 944 } |
915 } | 945 } |
916 | 946 |
917 switch (proc_maps_task) { | 947 switch (proc_maps_task) { |
918 case DISABLE_LIBRARY_ALLOCS: | 948 case DISABLE_LIBRARY_ALLOCS: |
919 // All lines starting like | 949 // All lines starting like |
920 // "401dc000-4030f000 r??p 00132000 03:01 13991972 lib/bin" | 950 // "401dc000-4030f000 r??p 00132000 03:01 13991972 lib/bin" |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1376 const size_t remainder = AsInt(object) % pointer_source_alignment; | 1406 const size_t remainder = AsInt(object) % pointer_source_alignment; |
1377 if (remainder) { | 1407 if (remainder) { |
1378 object += pointer_source_alignment - remainder; | 1408 object += pointer_source_alignment - remainder; |
1379 if (size >= pointer_source_alignment - remainder) { | 1409 if (size >= pointer_source_alignment - remainder) { |
1380 size -= pointer_source_alignment - remainder; | 1410 size -= pointer_source_alignment - remainder; |
1381 } else { | 1411 } else { |
1382 size = 0; | 1412 size = 0; |
1383 } | 1413 } |
1384 } | 1414 } |
1385 if (size < sizeof(void*)) continue; | 1415 if (size < sizeof(void*)) continue; |
| 1416 |
| 1417 #ifdef NO_FRAME_POINTER |
| 1418 // Frame pointer omission requires us to use libunwind, which uses direct |
| 1419 // mmap and munmap system calls, and that needs special handling. |
| 1420 if (name2 == kUnnamedProcSelfMapEntry) { |
| 1421 static const uintptr_t page_mask = ~(getpagesize() - 1); |
| 1422 const uintptr_t addr = reinterpret_cast<uintptr_t>(object); |
| 1423 if ((addr & page_mask) == 0 && (size & page_mask) == 0) { |
| 1424 // This is an object we slurped from /proc/self/maps. |
| 1425 // It may or may not be readable at this point. |
| 1426 // |
| 1427 // In case all the above conditions made a mistake, and the object is |
| 1428 // not related to libunwind, we also verify that it's not readable |
| 1429 // before ignoring it. |
| 1430 if (msync(const_cast<char*>(object), size, MS_ASYNC) != 0) { |
| 1431 // Skip unreadable object, so we don't crash trying to sweep it. |
| 1432 RAW_VLOG(0, "Ignoring inaccessible object [%p, %p) " |
| 1433 "(msync error %d (%s))", |
| 1434 object, object + size, errno, strerror(errno)); |
| 1435 continue; |
| 1436 } |
| 1437 } |
| 1438 } |
| 1439 #endif |
| 1440 |
1386 const char* const max_object = object + size - sizeof(void*); | 1441 const char* const max_object = object + size - sizeof(void*); |
1387 while (object <= max_object) { | 1442 while (object <= max_object) { |
1388 // potentially unaligned load: | 1443 // potentially unaligned load: |
1389 const uintptr_t addr = *reinterpret_cast<const uintptr_t*>(object); | 1444 const uintptr_t addr = *reinterpret_cast<const uintptr_t*>(object); |
1390 // Do fast check before the more expensive HaveOnHeapLocked lookup: | 1445 // Do fast check before the more expensive HaveOnHeapLocked lookup: |
1391 // this code runs for all memory words that are potentially pointers: | 1446 // this code runs for all memory words that are potentially pointers: |
1392 const bool can_be_on_heap = | 1447 const bool can_be_on_heap = |
1393 // Order tests by the likelyhood of the test failing in 64/32 bit modes. | 1448 // Order tests by the likelyhood of the test failing in 64/32 bit modes. |
1394 // Yes, this matters: we either lose 5..6% speed in 32 bit mode | 1449 // Yes, this matters: we either lose 5..6% speed in 32 bit mode |
1395 // (which is already slower) or by a factor of 1.5..1.91 in 64 bit mode. | 1450 // (which is already slower) or by a factor of 1.5..1.91 in 64 bit mode. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1448 //---------------------------------------------------------------------- | 1503 //---------------------------------------------------------------------- |
1449 // HeapLeakChecker leak check disabling components | 1504 // HeapLeakChecker leak check disabling components |
1450 //---------------------------------------------------------------------- | 1505 //---------------------------------------------------------------------- |
1451 | 1506 |
1452 // static | 1507 // static |
1453 void HeapLeakChecker::DisableChecksIn(const char* pattern) { | 1508 void HeapLeakChecker::DisableChecksIn(const char* pattern) { |
1454 RAW_LOG(WARNING, "DisableChecksIn(%s) is ignored", pattern); | 1509 RAW_LOG(WARNING, "DisableChecksIn(%s) is ignored", pattern); |
1455 } | 1510 } |
1456 | 1511 |
1457 // static | 1512 // static |
1458 void HeapLeakChecker::IgnoreObject(const void* ptr) { | 1513 void HeapLeakChecker::DoIgnoreObject(const void* ptr) { |
1459 SpinLockHolder l(&heap_checker_lock); | 1514 SpinLockHolder l(&heap_checker_lock); |
1460 if (!heap_checker_on) return; | 1515 if (!heap_checker_on) return; |
1461 size_t object_size; | 1516 size_t object_size; |
1462 if (!HaveOnHeapLocked(&ptr, &object_size)) { | 1517 if (!HaveOnHeapLocked(&ptr, &object_size)) { |
1463 RAW_LOG(ERROR, "No live heap object at %p to ignore", ptr); | 1518 RAW_LOG(ERROR, "No live heap object at %p to ignore", ptr); |
1464 } else { | 1519 } else { |
1465 RAW_VLOG(10, "Going to ignore live object at %p of %"PRIuS" bytes", | 1520 RAW_VLOG(10, "Going to ignore live object at %p of %"PRIuS" bytes", |
1466 ptr, object_size); | 1521 ptr, object_size); |
1467 if (ignored_objects == NULL) { | 1522 if (ignored_objects == NULL) { |
1468 ignored_objects = new(Allocator::Allocate(sizeof(IgnoredObjectsMap))) | 1523 ignored_objects = new(Allocator::Allocate(sizeof(IgnoredObjectsMap))) |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1852 if (!heap_cleanups_) | 1907 if (!heap_cleanups_) |
1853 return; | 1908 return; |
1854 for (int i = 0; i < heap_cleanups_->size(); i++) { | 1909 for (int i = 0; i < heap_cleanups_->size(); i++) { |
1855 void (*f)(void) = (*heap_cleanups_)[i]; | 1910 void (*f)(void) = (*heap_cleanups_)[i]; |
1856 f(); | 1911 f(); |
1857 } | 1912 } |
1858 delete heap_cleanups_; | 1913 delete heap_cleanups_; |
1859 heap_cleanups_ = NULL; | 1914 heap_cleanups_ = NULL; |
1860 } | 1915 } |
1861 | 1916 |
1862 // Program exit heap cleanup registered with atexit(). | 1917 // Program exit heap cleanup registered as a module object destructor. |
1863 // Will not get executed when we crash on a signal. | 1918 // Will not get executed when we crash on a signal. |
1864 // | 1919 // |
1865 /*static*/ void HeapLeakChecker::RunHeapCleanups() { | 1920 void HeapLeakChecker_RunHeapCleanups() { |
| 1921 if (FLAGS_heap_check == "local") // don't check heap in this mode |
| 1922 return; |
1866 { SpinLockHolder l(&heap_checker_lock); | 1923 { SpinLockHolder l(&heap_checker_lock); |
1867 // can get here (via forks?) with other pids | 1924 // can get here (via forks?) with other pids |
1868 if (heap_checker_pid != getpid()) return; | 1925 if (heap_checker_pid != getpid()) return; |
1869 } | 1926 } |
1870 HeapCleaner::RunHeapCleanups(); | 1927 HeapCleaner::RunHeapCleanups(); |
1871 if (!FLAGS_heap_check_after_destructors) DoMainHeapCheck(); | 1928 if (!FLAGS_heap_check_after_destructors) HeapLeakChecker::DoMainHeapCheck(); |
1872 } | 1929 } |
1873 | 1930 |
1874 static bool internal_init_start_has_run = false; | 1931 static bool internal_init_start_has_run = false; |
1875 | 1932 |
1876 // Called exactly once, before main() (but hopefully just before). | 1933 // Called exactly once, before main() (but hopefully just before). |
1877 // This picks a good unique name for the dumped leak checking heap profiles. | 1934 // This picks a good unique name for the dumped leak checking heap profiles. |
1878 // | 1935 // |
1879 // Because we crash when InternalInitStart is called more than once, | 1936 // Because we crash when InternalInitStart is called more than once, |
1880 // it's fine that we hold heap_checker_lock only around pieces of | 1937 // it's fine that we hold heap_checker_lock only around pieces of |
1881 // this function: this is still enough for thread-safety w.r.t. other functions | 1938 // this function: this is still enough for thread-safety w.r.t. other functions |
1882 // of this module. | 1939 // of this module. |
1883 // We can't hold heap_checker_lock throughout because it would deadlock | 1940 // We can't hold heap_checker_lock throughout because it would deadlock |
1884 // on a memory allocation since our new/delete hooks can be on. | 1941 // on a memory allocation since our new/delete hooks can be on. |
1885 // | 1942 // |
1886 /*static*/ void HeapLeakChecker::InternalInitStart() { | 1943 void HeapLeakChecker_InternalInitStart() { |
1887 { SpinLockHolder l(&heap_checker_lock); | 1944 { SpinLockHolder l(&heap_checker_lock); |
1888 RAW_CHECK(!internal_init_start_has_run, | 1945 RAW_CHECK(!internal_init_start_has_run, |
1889 "Heap-check constructor called twice. Perhaps you both linked" | 1946 "Heap-check constructor called twice. Perhaps you both linked" |
1890 " in the heap checker, and also used LD_PRELOAD to load it?"); | 1947 " in the heap checker, and also used LD_PRELOAD to load it?"); |
1891 internal_init_start_has_run = true; | 1948 internal_init_start_has_run = true; |
1892 | 1949 |
| 1950 #ifdef ADDRESS_SANITIZER |
| 1951 // AddressSanitizer's custom malloc conflicts with HeapChecker. |
| 1952 FLAGS_heap_check = ""; |
| 1953 #endif |
| 1954 |
1893 if (FLAGS_heap_check.empty()) { | 1955 if (FLAGS_heap_check.empty()) { |
1894 // turns out we do not need checking in the end; can stop profiling | 1956 // turns out we do not need checking in the end; can stop profiling |
1895 TurnItselfOffLocked(); | 1957 HeapLeakChecker::TurnItselfOffLocked(); |
1896 return; | 1958 return; |
1897 } else if (RunningOnValgrind()) { | 1959 } else if (RunningOnValgrind()) { |
1898 // There is no point in trying -- we'll just fail. | 1960 // There is no point in trying -- we'll just fail. |
1899 RAW_LOG(WARNING, "Can't run under Valgrind; will turn itself off"); | 1961 RAW_LOG(WARNING, "Can't run under Valgrind; will turn itself off"); |
1900 TurnItselfOffLocked(); | 1962 HeapLeakChecker::TurnItselfOffLocked(); |
1901 return; | 1963 return; |
1902 } | 1964 } |
1903 } | 1965 } |
1904 | 1966 |
1905 // Changing this to false can be useful when debugging heap-checker itself: | 1967 // Changing this to false can be useful when debugging heap-checker itself: |
1906 if (!FLAGS_heap_check_run_under_gdb && IsDebuggerAttached()) { | 1968 if (!FLAGS_heap_check_run_under_gdb && IsDebuggerAttached()) { |
1907 RAW_LOG(WARNING, "Someone is ptrace()ing us; will turn itself off"); | 1969 RAW_LOG(WARNING, "Someone is ptrace()ing us; will turn itself off"); |
1908 SpinLockHolder l(&heap_checker_lock); | 1970 SpinLockHolder l(&heap_checker_lock); |
1909 TurnItselfOffLocked(); | 1971 HeapLeakChecker::TurnItselfOffLocked(); |
1910 return; | 1972 return; |
1911 } | 1973 } |
1912 | 1974 |
1913 { SpinLockHolder l(&heap_checker_lock); | 1975 { SpinLockHolder l(&heap_checker_lock); |
1914 if (!constructor_heap_profiling) { | 1976 if (!constructor_heap_profiling) { |
1915 RAW_LOG(FATAL, "Can not start so late. You have to enable heap checking " | 1977 RAW_LOG(FATAL, "Can not start so late. You have to enable heap checking " |
1916 "with HEAPCHECK=<mode>."); | 1978 "with HEAPCHECK=<mode>."); |
1917 } | 1979 } |
1918 } | 1980 } |
1919 | 1981 |
(...skipping 30 matching lines...) Expand all Loading... |
1950 FLAGS_heap_check_ignore_thread_live = false; // no live flood (stricter) | 2012 FLAGS_heap_check_ignore_thread_live = false; // no live flood (stricter) |
1951 FLAGS_heap_check_ignore_global_live = false; // no live flood (stricter) | 2013 FLAGS_heap_check_ignore_global_live = false; // no live flood (stricter) |
1952 } else if (FLAGS_heap_check == "as-is") { | 2014 } else if (FLAGS_heap_check == "as-is") { |
1953 // do nothing: use other flags as is | 2015 // do nothing: use other flags as is |
1954 } else if (FLAGS_heap_check == "local") { | 2016 } else if (FLAGS_heap_check == "local") { |
1955 // do nothing | 2017 // do nothing |
1956 } else { | 2018 } else { |
1957 RAW_LOG(FATAL, "Unsupported heap_check flag: %s", | 2019 RAW_LOG(FATAL, "Unsupported heap_check flag: %s", |
1958 FLAGS_heap_check.c_str()); | 2020 FLAGS_heap_check.c_str()); |
1959 } | 2021 } |
| 2022 // FreeBSD doesn't seem to honor atexit execution order: |
| 2023 // http://code.google.com/p/gperftools/issues/detail?id=375 |
| 2024 // Since heap-checking before destructors depends on atexit running |
| 2025 // at the right time, on FreeBSD we always check after, even in the |
| 2026 // less strict modes. This just means FreeBSD is always a bit |
| 2027 // stricter in its checking than other OSes. |
| 2028 #ifdef __FreeBSD__ |
| 2029 FLAGS_heap_check_after_destructors = true; |
| 2030 #endif |
| 2031 |
1960 { SpinLockHolder l(&heap_checker_lock); | 2032 { SpinLockHolder l(&heap_checker_lock); |
1961 RAW_DCHECK(heap_checker_pid == getpid(), ""); | 2033 RAW_DCHECK(heap_checker_pid == getpid(), ""); |
1962 heap_checker_on = true; | 2034 heap_checker_on = true; |
1963 RAW_DCHECK(heap_profile, ""); | 2035 RAW_DCHECK(heap_profile, ""); |
1964 ProcMapsResult pm_result = UseProcMapsLocked(DISABLE_LIBRARY_ALLOCS); | 2036 HeapLeakChecker::ProcMapsResult pm_result = HeapLeakChecker::UseProcMapsLock
ed(HeapLeakChecker::DISABLE_LIBRARY_ALLOCS); |
1965 // might neeed to do this more than once | 2037 // might neeed to do this more than once |
1966 // if one later dynamically loads libraries that we want disabled | 2038 // if one later dynamically loads libraries that we want disabled |
1967 if (pm_result != PROC_MAPS_USED) { // can't function | 2039 if (pm_result != HeapLeakChecker::PROC_MAPS_USED) { // can't function |
1968 TurnItselfOffLocked(); | 2040 HeapLeakChecker::TurnItselfOffLocked(); |
1969 return; | 2041 return; |
1970 } | 2042 } |
1971 } | 2043 } |
1972 | 2044 |
1973 // make a good place and name for heap profile leak dumps | 2045 // make a good place and name for heap profile leak dumps |
1974 string* profile_prefix = | 2046 string* profile_prefix = |
1975 new string(FLAGS_heap_check_dump_directory + "/" + invocation_name()); | 2047 new string(FLAGS_heap_check_dump_directory + "/" + invocation_name()); |
1976 | 2048 |
1977 // Finalize prefix for dumping leak checking profiles. | 2049 // Finalize prefix for dumping leak checking profiles. |
1978 const int32 our_pid = getpid(); // safest to call getpid() outside lock | 2050 const int32 our_pid = getpid(); // safest to call getpid() outside lock |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2012 // "nm <this_binary> | grep new" will show that tcmalloc's new/delete | 2084 // "nm <this_binary> | grep new" will show that tcmalloc's new/delete |
2013 // implementation did not get linked-in into this binary | 2085 // implementation did not get linked-in into this binary |
2014 // (i.e. nm will list __builtin_new and __builtin_vec_new as undefined). | 2086 // (i.e. nm will list __builtin_new and __builtin_vec_new as undefined). |
2015 // If this happens, it is a BUILD bug to be fixed. | 2087 // If this happens, it is a BUILD bug to be fixed. |
2016 | 2088 |
2017 RAW_VLOG(heap_checker_info_level, | 2089 RAW_VLOG(heap_checker_info_level, |
2018 "WARNING: Perftools heap leak checker is active " | 2090 "WARNING: Perftools heap leak checker is active " |
2019 "-- Performance may suffer"); | 2091 "-- Performance may suffer"); |
2020 | 2092 |
2021 if (FLAGS_heap_check != "local") { | 2093 if (FLAGS_heap_check != "local") { |
2022 // Schedule registered heap cleanup | |
2023 atexit(RunHeapCleanups); | |
2024 HeapLeakChecker* main_hc = new HeapLeakChecker(); | 2094 HeapLeakChecker* main_hc = new HeapLeakChecker(); |
2025 SpinLockHolder l(&heap_checker_lock); | 2095 SpinLockHolder l(&heap_checker_lock); |
2026 RAW_DCHECK(main_heap_checker == NULL, | 2096 RAW_DCHECK(main_heap_checker == NULL, |
2027 "Repeated creation of main_heap_checker"); | 2097 "Repeated creation of main_heap_checker"); |
2028 main_heap_checker = main_hc; | 2098 main_heap_checker = main_hc; |
2029 do_main_heap_check = true; | 2099 do_main_heap_check = true; |
2030 } | 2100 } |
2031 | 2101 |
2032 { SpinLockHolder l(&heap_checker_lock); | 2102 { SpinLockHolder l(&heap_checker_lock); |
2033 RAW_CHECK(heap_checker_on && constructor_heap_profiling, | 2103 RAW_CHECK(heap_checker_on && constructor_heap_profiling, |
(...skipping 12 matching lines...) Expand all Loading... |
2046 // TODO(csilvers): support this in some manner. | 2116 // TODO(csilvers): support this in some manner. |
2047 #if 0 | 2117 #if 0 |
2048 SetCommandLineOptionWithMode("max_free_queue_size", "104857600", // 100M | 2118 SetCommandLineOptionWithMode("max_free_queue_size", "104857600", // 100M |
2049 SET_FLAG_IF_DEFAULT); | 2119 SET_FLAG_IF_DEFAULT); |
2050 #endif | 2120 #endif |
2051 } | 2121 } |
2052 | 2122 |
2053 // We want this to run early as well, but not so early as | 2123 // We want this to run early as well, but not so early as |
2054 // ::BeforeConstructors (we want flag assignments to have already | 2124 // ::BeforeConstructors (we want flag assignments to have already |
2055 // happened, for instance). Initializer-registration does the trick. | 2125 // happened, for instance). Initializer-registration does the trick. |
2056 REGISTER_MODULE_INITIALIZER(init_start, HeapLeakChecker::InternalInitStart()); | 2126 REGISTER_MODULE_INITIALIZER(init_start, HeapLeakChecker_InternalInitStart()); |
| 2127 REGISTER_MODULE_DESTRUCTOR(init_start, HeapLeakChecker_RunHeapCleanups()); |
| 2128 |
| 2129 // static |
| 2130 bool HeapLeakChecker::NoGlobalLeaksMaybeSymbolize( |
| 2131 ShouldSymbolize should_symbolize) { |
| 2132 // we never delete or change main_heap_checker once it's set: |
| 2133 HeapLeakChecker* main_hc = GlobalChecker(); |
| 2134 if (main_hc) { |
| 2135 RAW_VLOG(10, "Checking for whole-program memory leaks"); |
| 2136 return main_hc->DoNoLeaks(should_symbolize); |
| 2137 } |
| 2138 return true; |
| 2139 } |
2057 | 2140 |
2058 // static | 2141 // static |
2059 bool HeapLeakChecker::DoMainHeapCheck() { | 2142 bool HeapLeakChecker::DoMainHeapCheck() { |
2060 if (FLAGS_heap_check_delay_seconds > 0) { | 2143 if (FLAGS_heap_check_delay_seconds > 0) { |
2061 sleep(FLAGS_heap_check_delay_seconds); | 2144 sleep(FLAGS_heap_check_delay_seconds); |
2062 } | 2145 } |
2063 { SpinLockHolder l(&heap_checker_lock); | 2146 { SpinLockHolder l(&heap_checker_lock); |
2064 if (!do_main_heap_check) return false; | 2147 if (!do_main_heap_check) return false; |
2065 RAW_DCHECK(heap_checker_pid == getpid(), ""); | 2148 RAW_DCHECK(heap_checker_pid == getpid(), ""); |
2066 do_main_heap_check = false; // will do it now; no need to do it more | 2149 do_main_heap_check = false; // will do it now; no need to do it more |
2067 } | 2150 } |
2068 | 2151 |
2069 if (!NoGlobalLeaks()) { | 2152 // The program is over, so it's safe to symbolize addresses (which |
| 2153 // requires a fork) because no serious work is expected to be done |
| 2154 // after this. Symbolizing is really useful -- knowing what |
| 2155 // function has a leak is better than knowing just an address -- |
| 2156 // and while we can only safely symbolize once in a program run, |
| 2157 // now is the time (after all, there's no "later" that would be better). |
| 2158 if (!NoGlobalLeaksMaybeSymbolize(SYMBOLIZE)) { |
2070 if (FLAGS_heap_check_identify_leaks) { | 2159 if (FLAGS_heap_check_identify_leaks) { |
2071 RAW_LOG(FATAL, "Whole-program memory leaks found."); | 2160 RAW_LOG(FATAL, "Whole-program memory leaks found."); |
2072 } | 2161 } |
2073 RAW_LOG(ERROR, "Exiting with error code (instead of crashing) " | 2162 RAW_LOG(ERROR, "Exiting with error code (instead of crashing) " |
2074 "because of whole-program memory leaks"); | 2163 "because of whole-program memory leaks"); |
2075 _exit(1); // we don't want to call atexit() routines! | 2164 // We don't want to call atexit() routines! |
| 2165 _exit(FLAGS_heap_check_error_exit_code); |
2076 } | 2166 } |
2077 return true; | 2167 return true; |
2078 } | 2168 } |
2079 | 2169 |
2080 // static | 2170 // static |
2081 HeapLeakChecker* HeapLeakChecker::GlobalChecker() { | 2171 HeapLeakChecker* HeapLeakChecker::GlobalChecker() { |
2082 SpinLockHolder l(&heap_checker_lock); | 2172 SpinLockHolder l(&heap_checker_lock); |
2083 return main_heap_checker; | 2173 return main_heap_checker; |
2084 } | 2174 } |
2085 | 2175 |
2086 // static | 2176 // static |
2087 bool HeapLeakChecker::NoGlobalLeaks() { | 2177 bool HeapLeakChecker::NoGlobalLeaks() { |
2088 // we never delete or change main_heap_checker once it's set: | 2178 // symbolizing requires a fork, which isn't safe to do in general. |
2089 HeapLeakChecker* main_hc = GlobalChecker(); | 2179 return NoGlobalLeaksMaybeSymbolize(DO_NOT_SYMBOLIZE); |
2090 if (main_hc) { | |
2091 RAW_VLOG(10, "Checking for whole-program memory leaks"); | |
2092 // The program is over, so it's safe to symbolize addresses (which | |
2093 // requires a fork) because no serious work is expected to be done | |
2094 // after this. Symbolizing is really useful -- knowing what | |
2095 // function has a leak is better than knowing just an address -- | |
2096 // and while we can only safely symbolize once in a program run, | |
2097 // now is the time (after all, there's no "later" that would be better). | |
2098 return main_hc->DoNoLeaks(SYMBOLIZE); | |
2099 } | |
2100 return true; | |
2101 } | 2180 } |
2102 | 2181 |
2103 // static | 2182 // static |
2104 void HeapLeakChecker::CancelGlobalCheck() { | 2183 void HeapLeakChecker::CancelGlobalCheck() { |
2105 SpinLockHolder l(&heap_checker_lock); | 2184 SpinLockHolder l(&heap_checker_lock); |
2106 if (do_main_heap_check) { | 2185 if (do_main_heap_check) { |
2107 RAW_VLOG(heap_checker_info_level, | 2186 RAW_VLOG(heap_checker_info_level, |
2108 "Canceling the automatic at-exit whole-program memory leak check"); | 2187 "Canceling the automatic at-exit whole-program memory leak check"); |
2109 do_main_heap_check = false; | 2188 do_main_heap_check = false; |
2110 } | 2189 } |
2111 } | 2190 } |
2112 | 2191 |
2113 // static | 2192 // static |
2114 void HeapLeakChecker::BeforeConstructorsLocked() { | 2193 void HeapLeakChecker::BeforeConstructorsLocked() { |
2115 RAW_DCHECK(heap_checker_lock.IsHeld(), ""); | 2194 RAW_DCHECK(heap_checker_lock.IsHeld(), ""); |
2116 RAW_CHECK(!constructor_heap_profiling, | 2195 RAW_CHECK(!constructor_heap_profiling, |
2117 "BeforeConstructorsLocked called multiple times"); | 2196 "BeforeConstructorsLocked called multiple times"); |
| 2197 #ifdef ADDRESS_SANITIZER |
| 2198 // AddressSanitizer's custom malloc conflicts with HeapChecker. |
| 2199 return; |
| 2200 #endif |
2118 // Set hooks early to crash if 'new' gets called before we make heap_profile, | 2201 // Set hooks early to crash if 'new' gets called before we make heap_profile, |
2119 // and make sure no other hooks existed: | 2202 // and make sure no other hooks existed: |
2120 RAW_CHECK(MallocHook::AddNewHook(&NewHook), ""); | 2203 RAW_CHECK(MallocHook::AddNewHook(&NewHook), ""); |
2121 RAW_CHECK(MallocHook::AddDeleteHook(&DeleteHook), ""); | 2204 RAW_CHECK(MallocHook::AddDeleteHook(&DeleteHook), ""); |
2122 constructor_heap_profiling = true; | 2205 constructor_heap_profiling = true; |
2123 MemoryRegionMap::Init(1); | 2206 MemoryRegionMap::Init(1); |
2124 // Set up MemoryRegionMap with (at least) one caller stack frame to record | 2207 // Set up MemoryRegionMap with (at least) one caller stack frame to record |
2125 // (important that it's done before HeapProfileTable creation below). | 2208 // (important that it's done before HeapProfileTable creation below). |
2126 Allocator::Init(); | 2209 Allocator::Init(); |
2127 RAW_CHECK(heap_profile == NULL, ""); | 2210 RAW_CHECK(heap_profile == NULL, ""); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2285 // static | 2368 // static |
2286 const void* HeapLeakChecker::GetAllocCaller(void* ptr) { | 2369 const void* HeapLeakChecker::GetAllocCaller(void* ptr) { |
2287 // this is used only in the unittest, so the heavy checks are fine | 2370 // this is used only in the unittest, so the heavy checks are fine |
2288 HeapProfileTable::AllocInfo info; | 2371 HeapProfileTable::AllocInfo info; |
2289 { SpinLockHolder l(&heap_checker_lock); | 2372 { SpinLockHolder l(&heap_checker_lock); |
2290 RAW_CHECK(heap_profile->FindAllocDetails(ptr, &info), ""); | 2373 RAW_CHECK(heap_profile->FindAllocDetails(ptr, &info), ""); |
2291 } | 2374 } |
2292 RAW_CHECK(info.stack_depth >= 1, ""); | 2375 RAW_CHECK(info.stack_depth >= 1, ""); |
2293 return info.call_stack[0]; | 2376 return info.call_stack[0]; |
2294 } | 2377 } |
OLD | NEW |