heap-checker.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  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 are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Maxim Lifantsev (with design ideas by Sanjay Ghemawat)
  32. //
  33. //
  34. // Module for detecing heap (memory) leaks.
  35. //
  36. // For full(er) information, see doc/heap_checker.html
  37. //
  38. // This module can be linked into programs with
  39. // no slowdown caused by this unless you activate the leak-checker:
  40. //
  41. // 1. Set the environment variable HEAPCHEK to _type_ before
  42. // running the program.
  43. //
  44. // _type_ is usually "normal" but can also be "minimal", "strict", or
  45. // "draconian". (See the html file for other options, like 'local'.)
  46. //
  47. // After that, just run your binary. If the heap-checker detects
  48. // a memory leak at program-exit, it will print instructions on how
  49. // to track down the leak.
  50. #ifndef BASE_HEAP_CHECKER_H_
  51. #define BASE_HEAP_CHECKER_H_
  52. #include <sys/types.h> // for size_t
  53. // I can't #include config.h in this public API file, but I should
  54. // really use configure (and make malloc_extension.h a .in file) to
  55. // figure out if the system has stdint.h or not. But I'm lazy, so
  56. // for now I'm assuming it's a problem only with MSVC.
  57. #ifndef _MSC_VER
  58. #include <stdint.h> // for uintptr_t
  59. #endif
  60. #include <stdarg.h> // for va_list
  61. #include <vector>
  62. // Annoying stuff for windows -- makes sure clients can import these functions
  63. #ifndef PERFTOOLS_DLL_DECL
  64. # ifdef _WIN32
  65. # define PERFTOOLS_DLL_DECL __declspec(dllimport)
  66. # else
  67. # define PERFTOOLS_DLL_DECL
  68. # endif
  69. #endif
  70. // The class is thread-safe with respect to all the provided static methods,
  71. // as well as HeapLeakChecker objects: they can be accessed by multiple threads.
  72. class PERFTOOLS_DLL_DECL HeapLeakChecker {
  73. public:
  74. // ----------------------------------------------------------------------- //
  75. // Static functions for working with (whole-program) leak checking.
  76. // If heap leak checking is currently active in some mode
  77. // e.g. if leak checking was started (and is still active now)
  78. // due to HEAPCHECK=... defined in the environment.
  79. // The return value reflects iff HeapLeakChecker objects manually
  80. // constructed right now will be doing leak checking or nothing.
  81. // Note that we can go from active to inactive state during InitGoogle()
  82. // if FLAGS_heap_check gets set to "" by some code before/during InitGoogle().
  83. static bool IsActive();
  84. // Return pointer to the whole-program checker if it has been created
  85. // and NULL otherwise.
  86. // Once GlobalChecker() returns non-NULL that object will not disappear and
  87. // will be returned by all later GlobalChecker calls.
  88. // This is mainly to access BytesLeaked() and ObjectsLeaked() (see below)
  89. // for the whole-program checker after one calls NoGlobalLeaks()
  90. // or similar and gets false.
  91. static HeapLeakChecker* GlobalChecker();
  92. // Do whole-program leak check now (if it was activated for this binary);
  93. // return false only if it was activated and has failed.
  94. // The mode of the check is controlled by the command-line flags.
  95. // This method can be called repeatedly.
  96. // Things like GlobalChecker()->SameHeap() can also be called explicitly
  97. // to do the desired flavor of the check.
  98. static bool NoGlobalLeaks();
  99. // If whole-program checker if active,
  100. // cancel its automatic execution after main() exits.
  101. // This requires that some leak check (e.g. NoGlobalLeaks())
  102. // has been called at least once on the whole-program checker.
  103. static void CancelGlobalCheck();
  104. // ----------------------------------------------------------------------- //
  105. // Non-static functions for starting and doing leak checking.
  106. // Start checking and name the leak check performed.
  107. // The name is used in naming dumped profiles
  108. // and needs to be unique only within your binary.
  109. // It must also be a string that can be a part of a file name,
  110. // in particular not contain path expressions.
  111. explicit HeapLeakChecker(const char *name);
  112. // Destructor (verifies that some *NoLeaks or *SameHeap method
  113. // has been called at least once).
  114. ~HeapLeakChecker();
  115. // These used to be different but are all the same now: they return
  116. // true iff all memory allocated since this HeapLeakChecker object
  117. // was constructor is still reachable from global state.
  118. //
  119. // Because we fork to convert addresses to symbol-names, and forking
  120. // is not thread-safe, and we may be called in a threaded context,
  121. // we do not try to symbolize addresses when called manually.
  122. bool NoLeaks() { return DoNoLeaks(DO_NOT_SYMBOLIZE); }
  123. // These forms are obsolete; use NoLeaks() instead.
  124. // TODO(csilvers): mark as DEPRECATED.
  125. bool QuickNoLeaks() { return NoLeaks(); }
  126. bool BriefNoLeaks() { return NoLeaks(); }
  127. bool SameHeap() { return NoLeaks(); }
  128. bool QuickSameHeap() { return NoLeaks(); }
  129. bool BriefSameHeap() { return NoLeaks(); }
  130. // Detailed information about the number of leaked bytes and objects
  131. // (both of these can be negative as well).
  132. // These are available only after a *SameHeap or *NoLeaks
  133. // method has been called.
  134. // Note that it's possible for both of these to be zero
  135. // while SameHeap() or NoLeaks() returned false in case
  136. // of a heap state change that is significant
  137. // but preserves the byte and object counts.
  138. ssize_t BytesLeaked() const;
  139. ssize_t ObjectsLeaked() const;
  140. // ----------------------------------------------------------------------- //
  141. // Static helpers to make us ignore certain leaks.
  142. // Scoped helper class. Should be allocated on the stack inside a
  143. // block of code. Any heap allocations done in the code block
  144. // covered by the scoped object (including in nested function calls
  145. // done by the code block) will not be reported as leaks. This is
  146. // the recommended replacement for the GetDisableChecksStart() and
  147. // DisableChecksToHereFrom() routines below.
  148. //
  149. // Example:
  150. // void Foo() {
  151. // HeapLeakChecker::Disabler disabler;
  152. // ... code that allocates objects whose leaks should be ignored ...
  153. // }
  154. //
  155. // REQUIRES: Destructor runs in same thread as constructor
  156. class Disabler {
  157. public:
  158. Disabler();
  159. ~Disabler();
  160. private:
  161. Disabler(const Disabler&); // disallow copy
  162. void operator=(const Disabler&); // and assign
  163. };
  164. // Ignore an object located at 'ptr' (can go at the start or into the object)
  165. // as well as all heap objects (transitively) referenced from it for the
  166. // purposes of heap leak checking. Returns 'ptr' so that one can write
  167. // static T* obj = IgnoreObject(new T(...));
  168. //
  169. // If 'ptr' does not point to an active allocated object at the time of this
  170. // call, it is ignored; but if it does, the object must not get deleted from
  171. // the heap later on.
  172. //
  173. // See also HiddenPointer, below, if you need to prevent a pointer from
  174. // being traversed by the heap checker but do not wish to transitively
  175. // whitelist objects referenced through it.
  176. template <typename T>
  177. static T* IgnoreObject(T* ptr) {
  178. DoIgnoreObject(static_cast<const void*>(const_cast<const T*>(ptr)));
  179. return ptr;
  180. }
  181. // Undo what an earlier IgnoreObject() call promised and asked to do.
  182. // At the time of this call 'ptr' must point at or inside of an active
  183. // allocated object which was previously registered with IgnoreObject().
  184. static void UnIgnoreObject(const void* ptr);
  185. // ----------------------------------------------------------------------- //
  186. // Internal types defined in .cc
  187. class Allocator;
  188. struct RangeValue;
  189. private:
  190. // ----------------------------------------------------------------------- //
  191. // Various helpers
  192. // Create the name of the heap profile file.
  193. // Should be deleted via Allocator::Free().
  194. char* MakeProfileNameLocked();
  195. // Helper for constructors
  196. void Create(const char *name, bool make_start_snapshot);
  197. enum ShouldSymbolize { SYMBOLIZE, DO_NOT_SYMBOLIZE };
  198. // Helper for *NoLeaks and *SameHeap
  199. bool DoNoLeaks(ShouldSymbolize should_symbolize);
  200. // Helper for NoGlobalLeaks, also called by the global destructor.
  201. static bool NoGlobalLeaksMaybeSymbolize(ShouldSymbolize should_symbolize);
  202. // These used to be public, but they are now deprecated.
  203. // Will remove entirely when all internal uses are fixed.
  204. // In the meantime, use friendship so the unittest can still test them.
  205. static void* GetDisableChecksStart();
  206. static void DisableChecksToHereFrom(const void* start_address);
  207. static void DisableChecksIn(const char* pattern);
  208. friend void RangeDisabledLeaks();
  209. friend void NamedTwoDisabledLeaks();
  210. friend void* RunNamedDisabledLeaks(void*);
  211. friend void TestHeapLeakCheckerNamedDisabling();
  212. // Actually implements IgnoreObject().
  213. static void DoIgnoreObject(const void* ptr);
  214. // Disable checks based on stack trace entry at a depth <=
  215. // max_depth. Used to hide allocations done inside some special
  216. // libraries.
  217. static void DisableChecksFromToLocked(const void* start_address,
  218. const void* end_address,
  219. int max_depth);
  220. // Helper for DoNoLeaks to ignore all objects reachable from all live data
  221. static void IgnoreAllLiveObjectsLocked(const void* self_stack_top);
  222. // Callback we pass to TCMalloc_ListAllProcessThreads (see thread_lister.h)
  223. // that is invoked when all threads of our process are found and stopped.
  224. // The call back does the things needed to ignore live data reachable from
  225. // thread stacks and registers for all our threads
  226. // as well as do other global-live-data ignoring
  227. // (via IgnoreNonThreadLiveObjectsLocked)
  228. // during the quiet state of all threads being stopped.
  229. // For the argument meaning see the comment by TCMalloc_ListAllProcessThreads.
  230. // Here we only use num_threads and thread_pids, that TCMalloc_ListAllProcessThreads
  231. // fills for us with the number and pids of all the threads of our process
  232. // it found and attached to.
  233. static int IgnoreLiveThreadsLocked(void* parameter,
  234. int num_threads,
  235. pid_t* thread_pids,
  236. va_list ap);
  237. // Helper for IgnoreAllLiveObjectsLocked and IgnoreLiveThreadsLocked
  238. // that we prefer to execute from IgnoreLiveThreadsLocked
  239. // while all threads are stopped.
  240. // This helper does live object discovery and ignoring
  241. // for all objects that are reachable from everything
  242. // not related to thread stacks and registers.
  243. static void IgnoreNonThreadLiveObjectsLocked();
  244. // Helper for IgnoreNonThreadLiveObjectsLocked and IgnoreLiveThreadsLocked
  245. // to discover and ignore all heap objects
  246. // reachable from currently considered live objects
  247. // (live_objects static global variable in out .cc file).
  248. // "name", "name2" are two strings that we print one after another
  249. // in a debug message to describe what kind of live object sources
  250. // are being used.
  251. static void IgnoreLiveObjectsLocked(const char* name, const char* name2);
  252. // Do the overall whole-program heap leak check if needed;
  253. // returns true when did the leak check.
  254. static bool DoMainHeapCheck();
  255. // Type of task for UseProcMapsLocked
  256. enum ProcMapsTask {
  257. RECORD_GLOBAL_DATA,
  258. DISABLE_LIBRARY_ALLOCS
  259. };
  260. // Success/Error Return codes for UseProcMapsLocked.
  261. enum ProcMapsResult {
  262. PROC_MAPS_USED,
  263. CANT_OPEN_PROC_MAPS,
  264. NO_SHARED_LIBS_IN_PROC_MAPS
  265. };
  266. // Read /proc/self/maps, parse it, and do the 'proc_maps_task' for each line.
  267. static ProcMapsResult UseProcMapsLocked(ProcMapsTask proc_maps_task);
  268. // A ProcMapsTask to disable allocations from 'library'
  269. // that is mapped to [start_address..end_address)
  270. // (only if library is a certain system library).
  271. static void DisableLibraryAllocsLocked(const char* library,
  272. uintptr_t start_address,
  273. uintptr_t end_address);
  274. // Return true iff "*ptr" points to a heap object
  275. // ("*ptr" can point at the start or inside of a heap object
  276. // so that this works e.g. for pointers to C++ arrays, C++ strings,
  277. // multiple-inherited objects, or pointers to members).
  278. // We also fill *object_size for this object then
  279. // and we move "*ptr" to point to the very start of the heap object.
  280. static inline bool HaveOnHeapLocked(const void** ptr, size_t* object_size);
  281. // Helper to shutdown heap leak checker when it's not needed
  282. // or can't function properly.
  283. static void TurnItselfOffLocked();
  284. // Internally-used c-tor to start whole-executable checking.
  285. HeapLeakChecker();
  286. // ----------------------------------------------------------------------- //
  287. // Friends and externally accessed helpers.
  288. // Helper for VerifyHeapProfileTableStackGet in the unittest
  289. // to get the recorded allocation caller for ptr,
  290. // which must be a heap object.
  291. static const void* GetAllocCaller(void* ptr);
  292. friend void VerifyHeapProfileTableStackGet();
  293. // This gets to execute before constructors for all global objects
  294. static void BeforeConstructorsLocked();
  295. friend void HeapLeakChecker_BeforeConstructors();
  296. // This gets to execute after destructors for all global objects
  297. friend void HeapLeakChecker_AfterDestructors();
  298. // Full starting of recommended whole-program checking.
  299. friend void HeapLeakChecker_InternalInitStart();
  300. // Runs REGISTER_HEAPCHECK_CLEANUP cleanups and potentially
  301. // calls DoMainHeapCheck
  302. friend void HeapLeakChecker_RunHeapCleanups();
  303. // ----------------------------------------------------------------------- //
  304. // Member data.
  305. class SpinLock* lock_; // to make HeapLeakChecker objects thread-safe
  306. const char* name_; // our remembered name (we own it)
  307. // NULL means this leak checker is a noop
  308. // Snapshot taken when the checker was created. May be NULL
  309. // for the global heap checker object. We use void* instead of
  310. // HeapProfileTable::Snapshot* to avoid including heap-profile-table.h.
  311. void* start_snapshot_;
  312. bool has_checked_; // if we have done the leak check, so these are ready:
  313. ssize_t inuse_bytes_increase_; // bytes-in-use increase for this checker
  314. ssize_t inuse_allocs_increase_; // allocations-in-use increase
  315. // for this checker
  316. bool keep_profiles_; // iff we should keep the heap profiles we've made
  317. // ----------------------------------------------------------------------- //
  318. // Disallow "evil" constructors.
  319. HeapLeakChecker(const HeapLeakChecker&);
  320. void operator=(const HeapLeakChecker&);
  321. };
  322. // Holds a pointer that will not be traversed by the heap checker.
  323. // Contrast with HeapLeakChecker::IgnoreObject(o), in which o and
  324. // all objects reachable from o are ignored by the heap checker.
  325. template <class T>
  326. class HiddenPointer {
  327. public:
  328. explicit HiddenPointer(T* t)
  329. : masked_t_(reinterpret_cast<uintptr_t>(t) ^ kHideMask) {
  330. }
  331. // Returns unhidden pointer. Be careful where you save the result.
  332. T* get() const { return reinterpret_cast<T*>(masked_t_ ^ kHideMask); }
  333. private:
  334. // Arbitrary value, but not such that xor'ing with it is likely
  335. // to map one valid pointer to another valid pointer:
  336. static const uintptr_t kHideMask =
  337. static_cast<uintptr_t>(0xF03A5F7BF03A5F7Bll);
  338. uintptr_t masked_t_;
  339. };
  340. // A class that exists solely to run its destructor. This class should not be
  341. // used directly, but instead by the REGISTER_HEAPCHECK_CLEANUP macro below.
  342. class PERFTOOLS_DLL_DECL HeapCleaner {
  343. public:
  344. typedef void (*void_function)(void);
  345. HeapCleaner(void_function f);
  346. static void RunHeapCleanups();
  347. private:
  348. static std::vector<void_function>* heap_cleanups_;
  349. };
  350. // A macro to declare module heap check cleanup tasks
  351. // (they run only if we are doing heap leak checking.)
  352. // 'body' should be the cleanup code to run. 'name' doesn't matter,
  353. // but must be unique amongst all REGISTER_HEAPCHECK_CLEANUP calls.
  354. #define REGISTER_HEAPCHECK_CLEANUP(name, body) \
  355. namespace { \
  356. void heapcheck_cleanup_##name() { body; } \
  357. static HeapCleaner heapcheck_cleaner_##name(&heapcheck_cleanup_##name); \
  358. }
  359. #endif // BASE_HEAP_CHECKER_H_